aboutsummaryrefslogtreecommitdiff
path: root/etpo/lexer.l
blob: 5709d57296a569a93ab9310f80e0bce7eacd67fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
%{
/*
 * Copyright (C) 2004 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <ftw.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include "grammar.h"
%}
%x QUOTED
%%
error_table|et		{ yylval.sval = g_strdup(yytext);
			  return ERROR_TABLE_START; };
end			{ return ERROR_TABLE_END; };
error_code|ec		{ yylval.sval = g_strdup(yytext);
			  return ERROR_CODE_START; };
,			{ return COMMA; };
\\\n			{ };
<INITIAL>[\r\n]		{ };
<INITIAL>[A-Za-z0-9_-]+	{ yylval.sval = g_strdup(yytext);
			  return TOKEN; };
<INITIAL>[ \t]		{ };
<INITIAL>\"		{ BEGIN(QUOTED);
			  yylval.sval = g_strdup("");
			  return QUOTE; };
<QUOTED>\"		{ BEGIN(INITIAL);
			  return QUOTE; };
<QUOTED>[^\"]+		{ yylval.sval = g_strdup(yytext);
			  return LITERAL; };
<QUOTED>\n		{ yylval.sval = g_strdup(yytext);
			  return LITERAL; };
<*>^#.*$		{ };
%%

/* Complete list of filenames, an iterator for that list, and the contents of
 * the current item. */
static GList *filenames = NULL, *filename = NULL;
const char *currentfile = NULL;

int
yyerror(void)
{
	g_print("Syntax error (%s).\n", currentfile);
	exit(1);
}

/* Callback for ftw().  Adds the filename being examined to the global list of
 * filenames. */
static int
fn(const char *file, const struct stat *st, int flag)
{
	int i;
	if (flag == FTW_F) {
		i = strlen(file);
		if ((i > 3) &&
		    (strncmp(file + strlen(file) - 3, ".et", 3) == 0)) {
			filenames = g_list_append(filenames, g_strdup(file));
		}
	}
	return 0;
}

/* Open the next filename in the list of files, if we have a list and we
 * haven't reached its end. */
int
yywrap(void)
{
	if ((filename != NULL) && (g_list_next(filename) != NULL)) {
		fclose(yyin);
		filename = g_list_next(filename);
		currentfile = filename->data;
		yyin = fopen(currentfile, "r");
		return 0;
	}
	return 1;
}

/* Spew forth a gettext .pot header. */
static void
header(void)
{
	struct timeval tv;
	struct timezone tz;
	struct tm *tm;
	const char *boilerplate = 
	"# SOME DESCRIPTIVE TITLE.\n"
	"# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER\n"
	"# This file is distributed under the same license as the PACKAGE package.\n"
	"# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.\n"
	"#\n"
	"#, fuzzy\n"
	"msgid \"\"\n"
	"msgstr \"\"\n"
	"\"Project-Id-Version: PACKAGE VERSION\\n\"\n"
	"\"Report-Msgid-Bugs-To: \\n\"\n"
	"\"POT-Creation-Date: %04d-%02d-%02d %02d:%02d%s\\n\"\n"
	"\"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\\n\"\n"
	"\"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n\"\n"
	"\"Language-Team: LANGUAGE <LL@li.org>\\n\"\n"
	"\"MIME-Version: 1.0\\n\"\n"
	"\"Content-Type: text/plain; charset=CHARSET\\n\"\n"
	"\"Content-Transfer-Encoding: 8bit\\n\"\n";
	gettimeofday(&tv, &tz);
	tm = gmtime(&tv.tv_sec);
	printf(boilerplate, tm->tm_year + 1900, tm->tm_mon, tm->tm_mday,
	       tm->tm_hour, tm->tm_min, "GMT");
}

int
main(int argc, char **argv)
{
	int i;
	/* Call getopt.  We don't provide any options just now, but this lets
	 * us handle "--help" and "-h" queries simply. */
	while ((i = getopt(argc, argv, "")) != -1) {
		switch (i) {
		default:
			printf("Usage: etpo [directory ...]\n");
			return 2;
			break;
		}
	}
	/* Assume that each non-option argument is a directory. */
	for (i = optind; i < argc; i++) {
		if (ftw(argv[i], fn, 10) != 0) {
			perror("ftw");
			return 1;
		}
	}
	/* Spew out a header. */
	header();
	if (g_list_length(filenames) > 0) {
		/* Open the first file and start parsing it. */
		filename = filenames;
		currentfile = filename->data;
		yyin = fopen(currentfile, "r");
		yyparse();
		fclose(yyin);
	} else {
		/* Start parsing stdin. */
		currentfile = "<stdin>";
		yyin = stdin;
	}
	return 0;
}