1 | /* RUN-TIME CONFIGURATION FILE PARSER */
|
---|
2 | /**********************************************************************
|
---|
3 | *
|
---|
4 | * zparse.y --
|
---|
5 | * Copyright (C) 2000 The New Zealand Digital Library Project
|
---|
6 | *
|
---|
7 | * A component of the Greenstone digital library software
|
---|
8 | * from the New Zealand Digital Library Project at the
|
---|
9 | * University of Waikato, New Zealand.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 2 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | *
|
---|
25 | *********************************************************************/
|
---|
26 |
|
---|
27 | %{
|
---|
28 | /* verbatim C code - functions, etc */
|
---|
29 | #include <stdlib.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <string.h>
|
---|
32 | #include <ctype.h>
|
---|
33 | #include "z3950cfg.h"
|
---|
34 |
|
---|
35 | #define YYDEBUG 1
|
---|
36 | #define YYERROR_VERBOSE 1
|
---|
37 | /* can't use default yyparse(), as this name is used elsewhere (eg mg)*/
|
---|
38 | #define yyin zconfigin
|
---|
39 | #define yyparse zconfigparse
|
---|
40 | char defLanguage=0;
|
---|
41 |
|
---|
42 | /* defined in parse.fl */
|
---|
43 | extern int lineno;
|
---|
44 |
|
---|
45 | #ifndef YYRECOVERING
|
---|
46 | int YYRECOVERING=0; /* this is supposed to already be defined as a macro */
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | /* defined in z3950proto.cpp */
|
---|
50 | struct z3950cfg *zserver_list=NULL;
|
---|
51 |
|
---|
52 | /* this is set in z3950proto.cpp */
|
---|
53 | FILE *errfile;
|
---|
54 |
|
---|
55 | int yylex(void);
|
---|
56 |
|
---|
57 | int yyerror(char *string);
|
---|
58 |
|
---|
59 | void errormsg(char *str);
|
---|
60 |
|
---|
61 | %}
|
---|
62 |
|
---|
63 |
|
---|
64 | %union {
|
---|
65 | char *string;
|
---|
66 | struct z3950cfg *cfg;
|
---|
67 | struct z3950aboutlist *about;
|
---|
68 | int number;
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /* keywords */
|
---|
73 | /***** NOTE: can explicitly give values for each token, AND
|
---|
74 | give a string for the token name eg
|
---|
75 | %token GSDLVERSION 257 "Version"
|
---|
76 | *******/
|
---|
77 | %token GSDLVERSION 257
|
---|
78 | %token GENERAL_SECTION 258 SEARCH_SECTION 259
|
---|
79 | %token BROWSE_SECTION 260 MACROS_SECTION 261
|
---|
80 | %token MAINTAINER 262
|
---|
81 | %token PUBLIC 263
|
---|
82 | %token TRUE 264 FALSE 265
|
---|
83 | %token LANGUAGES 266 EN 267 FR 268 MI 269 ZH 270 DE 271
|
---|
84 | %token NAME 272
|
---|
85 | %token <string> STRING 273
|
---|
86 | %token ICON 274 SMALLICON 275
|
---|
87 | %token ABOUT 276
|
---|
88 | %token SEARCHTEXT 277
|
---|
89 | %token DOCUMENT 278 SECTION 279
|
---|
90 | %token BROWSE 280 TYPE 281 FORMAT 282
|
---|
91 | /* classifiers */
|
---|
92 | %token LIST 283 SORTEDLIST 284 SECTIONLIST 285 SORTEDSECTIONLIST 286
|
---|
93 | %token DATELIST 287
|
---|
94 | /* formats */
|
---|
95 | %token ICONLINK 288 TEXTLINK 289
|
---|
96 | /* layouts */
|
---|
97 | %token DOCUMENTIMAGES 290 DOCUMENTHEADER 291 DOCUMENTTEXT 292
|
---|
98 | %token DOCUMENTBUTTONS 293 DOCUMENTARROWSBOTTOM 294
|
---|
99 |
|
---|
100 | /* all other text */
|
---|
101 | %token <string> DATA 295
|
---|
102 |
|
---|
103 | %type <cfg> zserver zserverlist
|
---|
104 | %type <number> port
|
---|
105 | %type <string> icon smallicon lang name
|
---|
106 | %type <about> about aboutLang aboutList
|
---|
107 | %%
|
---|
108 | /* grammar */
|
---|
109 | z3950 : version zserverlist {zserver_list=$2;}
|
---|
110 | | version {zserver_list=NULL;} /* if all commented out or none... */
|
---|
111 |
|
---|
112 |
|
---|
113 | version : GSDLVERSION {;}
|
---|
114 | | {fprintf(errfile,"No version - not GSDL config file?\n");return(1);}
|
---|
115 |
|
---|
116 | zserverlist : zserverlist zserver {if ($2!=NULL) {$2->next=$1;$$=$2;}}
|
---|
117 | | zserver ;
|
---|
118 |
|
---|
119 | zserver : DATA DATA port DATA name icon smallicon about
|
---|
120 | {
|
---|
121 | if (YYRECOVERING)
|
---|
122 | {$$=NULL;YYRECOVERING=0;}
|
---|
123 | else {
|
---|
124 | if(($$=malloc(sizeof(struct z3950cfg)))==NULL)
|
---|
125 | {
|
---|
126 | fprintf(errfile,"malloc failed\n");
|
---|
127 | return(1);
|
---|
128 | }
|
---|
129 | $$->shortname=$1;
|
---|
130 | $$->hostname=$2;
|
---|
131 | $$->port=$3;
|
---|
132 | $$->dbname=$4;
|
---|
133 | $$->longname=$5;
|
---|
134 | $$->icon=$6;
|
---|
135 | $$->smallicon=$7;
|
---|
136 | $$->about=$8;
|
---|
137 | $$->next=NULL;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | | error {$$=NULL;
|
---|
141 | fprintf(errfile,"discarding zserver (line %d)\n",lineno);}
|
---|
142 | ;
|
---|
143 |
|
---|
144 | name : STRING {;}
|
---|
145 | | {errormsg("Database name needs to be followed by a short "
|
---|
146 | "descriptive name (enclosed in \" marks)");
|
---|
147 | $$=NULL;
|
---|
148 | YYRECOVERING=1;} /* we should scrap this server */
|
---|
149 |
|
---|
150 | port : ':' DATA {$$=atoi($2);}
|
---|
151 | | {$$=210;} /* default z39.50 server port */
|
---|
152 | ;
|
---|
153 |
|
---|
154 | icon : ICON STRING {$$=$2;}
|
---|
155 | | ICON DATA {$$=$2;
|
---|
156 | errormsg("Icon must be enclosed in quotes");
|
---|
157 | yylex();yylex();}
|
---|
158 | | {$$=NULL;}
|
---|
159 | smallicon : SMALLICON STRING {$$=$2;}
|
---|
160 | | SMALLICON DATA {$$=$2;
|
---|
161 | /* this is because of the ':' */
|
---|
162 | errormsg("Smallicon must be enclosed in quotes.\n");
|
---|
163 | yylex();yylex();
|
---|
164 | }
|
---|
165 | | {$$=NULL;}
|
---|
166 |
|
---|
167 | about : aboutList {defLanguage=0;}
|
---|
168 | | {$$=NULL;}
|
---|
169 |
|
---|
170 | aboutList : aboutLang {;}
|
---|
171 | /* note this action reverses the order of the list */
|
---|
172 | | aboutList aboutLang {$$=$2;$2->next=$1;}
|
---|
173 |
|
---|
174 |
|
---|
175 | aboutLang : ABOUT lang STRING
|
---|
176 | {
|
---|
177 | if(($$=malloc(sizeof(struct z3950aboutlist)))
|
---|
178 | ==NULL) {
|
---|
179 | fprintf(errfile,"Malloc failed\n");
|
---|
180 | return(1);
|
---|
181 | }
|
---|
182 | $$->lang=$2;
|
---|
183 | $$->text=$3;
|
---|
184 | $$->next=NULL;
|
---|
185 | }
|
---|
186 | | ABOUT STRING
|
---|
187 | {
|
---|
188 | if (defLanguage)
|
---|
189 | {
|
---|
190 | fprintf(errfile,"warning: (line %d): already have a default language in config file\n",lineno);
|
---|
191 | }
|
---|
192 | else defLanguage=1;
|
---|
193 |
|
---|
194 | if(($$=malloc(sizeof(struct z3950aboutlist)))
|
---|
195 | ==NULL) {
|
---|
196 | fprintf(errfile,"Malloc failed\n");
|
---|
197 | return(1);
|
---|
198 | }
|
---|
199 | $$->lang=NULL; /* default lang... */
|
---|
200 | $$->text=$2;
|
---|
201 | $$->next=NULL;
|
---|
202 | }
|
---|
203 | /* this can only happen once (default lang) */
|
---|
204 |
|
---|
205 |
|
---|
206 | /* languages should be done using a lookup, rather than hard-coding
|
---|
207 | them as tokens. Also '_' token for en_BR */
|
---|
208 | lang : DATA '_' DATA
|
---|
209 | {
|
---|
210 | $$=malloc(strlen($1)+strlen($3)+1);
|
---|
211 | strncpy($$,$1,strlen($1));
|
---|
212 | $$[strlen($1)]='_';
|
---|
213 | strncpy($$+strlen($1)+1,$3,strlen($3));
|
---|
214 | free($1);free($3);
|
---|
215 | }
|
---|
216 | | DATA {;}
|
---|
217 | /*lang '_' DATA {;} */
|
---|
218 | /* | EN {;}
|
---|
219 | | FR {;}
|
---|
220 | | MI {;}
|
---|
221 | | ZH {;}
|
---|
222 | | DE {;}
|
---|
223 | | DATA {errormsg("unknown language");}*/
|
---|
224 | | error {errormsg("missing language");}
|
---|
225 |
|
---|
226 | /*documentHeader : DOCUMENTHEADER STRING {;}
|
---|
227 | | {;}
|
---|
228 |
|
---|
229 | documentText : DOCUMENTTEXT STRING {;}
|
---|
230 | | {;}
|
---|
231 | */
|
---|
232 |
|
---|
233 | %%
|
---|
234 | /*int strncasecmp (const char *, const char *,size_t);*/
|
---|
235 | int yyerror(char *string) {
|
---|
236 | fprintf(errfile,"Parse error (line %d) near \n\"%s\"<---:%s\n",lineno,
|
---|
237 | yylval.string,string);
|
---|
238 | return(1);
|
---|
239 | }
|
---|
240 |
|
---|
241 | void errormsg(char *str) {
|
---|
242 | fprintf(errfile,"Err (line %d, near \"%s\"): %s.\n",lineno,
|
---|
243 | yylval.string,str);
|
---|
244 | }
|
---|
245 |
|
---|
246 | /* *
|
---|
247 | int main (int argc, char *argv[]) {
|
---|
248 | if (argc==2)
|
---|
249 | if (argv[1][0]=='-'&&argv[1][1]=='d')
|
---|
250 | yydebug=1;
|
---|
251 |
|
---|
252 | yyparse();
|
---|
253 | return(0);
|
---|
254 | }
|
---|
255 | * */
|
---|