s/refreshable/renewable/g
[jelmer/krb5-auth-dialog.git] / etpo / lexer.l
1 %{
2 /*
3  * Copyright (C) 2004 Red Hat, Inc.
4  *
5  * This is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Library General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 #include "config.h"
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <ftw.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <glib.h>
31 #include "grammar.h"
32 %}
33 %x QUOTED
34 %%
35 error_table|et          { yylval.sval = g_strdup(yytext);
36                           return ERROR_TABLE_START; };
37 end                     { return ERROR_TABLE_END; };
38 error_code|ec           { yylval.sval = g_strdup(yytext);
39                           return ERROR_CODE_START; };
40 ,                       { return COMMA; };
41 \\\n                    { };
42 <INITIAL>[\r\n]         { };
43 <INITIAL>[A-Za-z0-9_-]+ { yylval.sval = g_strdup(yytext);
44                           return TOKEN; };
45 <INITIAL>[ \t]          { };
46 <INITIAL>\"             { BEGIN(QUOTED);
47                           yylval.sval = g_strdup("");
48                           return QUOTE; };
49 <QUOTED>\"              { BEGIN(INITIAL);
50                           return QUOTE; };
51 <QUOTED>[^\"]+          { yylval.sval = g_strdup(yytext);
52                           return LITERAL; };
53 <QUOTED>\n              { yylval.sval = g_strdup(yytext);
54                           return LITERAL; };
55 <*>^#.*$                { };
56 %%
57
58 /* Complete list of filenames, an iterator for that list, and the contents of
59  * the current item. */
60 static GList *filenames = NULL, *filename = NULL;
61 const char *currentfile = NULL;
62
63 int
64 yyerror(void)
65 {
66         g_print("Syntax error (%s).\n", currentfile);
67         exit(1);
68 }
69
70 /* Callback for ftw().  Adds the filename being examined to the global list of
71  * filenames. */
72 static int
73 fn(const char *file, const struct stat *st, int flag)
74 {
75         int i;
76         if (flag == FTW_F) {
77                 i = strlen(file);
78                 if ((i > 3) &&
79                     (strncmp(file + strlen(file) - 3, ".et", 3) == 0)) {
80                         filenames = g_list_append(filenames, g_strdup(file));
81                 }
82         }
83         return 0;
84 }
85
86 /* Open the next filename in the list of files, if we have a list and we
87  * haven't reached its end. */
88 int
89 yywrap(void)
90 {
91         if ((filename != NULL) && (g_list_next(filename) != NULL)) {
92                 fclose(yyin);
93                 filename = g_list_next(filename);
94                 currentfile = filename->data;
95                 yyin = fopen(currentfile, "r");
96                 return 0;
97         }
98         return 1;
99 }
100
101 /* Spew forth a gettext .pot header. */
102 static void
103 header(void)
104 {
105         const char *boilerplate = "const char *dummy = {\n";
106         printf(boilerplate);
107 }
108
109 static void
110 tail(void)
111 {
112         const char *boilerplate = "};\n";
113         printf(boilerplate);
114 }
115
116 int
117 main(int argc, char **argv)
118 {
119         int i;
120         /* Call getopt.  We don't provide any options just now, but this lets
121          * us handle "--help" and "-h" queries simply. */
122         while ((i = getopt(argc, argv, "")) != -1) {
123                 switch (i) {
124                 default:
125                         printf("Usage: etpo [directory ...]\n");
126                         return 2;
127                         break;
128                 }
129         }
130         /* Assume that each non-option argument is a directory. */
131         for (i = optind; i < argc; i++) {
132                 if (ftw(argv[i], fn, 10) != 0) {
133                         perror("ftw");
134                         return 1;
135                 }
136         }
137         /* Spew out a header. */
138         header();
139         if (g_list_length(filenames) > 0) {
140                 /* Open the first file and start parsing it. */
141                 filename = filenames;
142                 currentfile = filename->data;
143                 yyin = fopen(currentfile, "r");
144                 yyparse();
145                 fclose(yyin);
146         } else {
147                 /* Start parsing stdin. */
148                 currentfile = "<stdin>";
149                 yyin = stdin;
150         }
151         tail();
152         return 0;
153 }