source: trunk/gsdl/src/recpt/zparse.y@ 1369

Last change on this file since 1369 was 1355, checked in by jrm21, 24 years ago

Fixed up parsing of z39.50 config file, so that errors go to file instead
of stderr, which screws up the cgi headers and page...
Errors goes to etc/recpt/z3950err.txt

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