source: gsdl/trunk/src/z3950/zparse.y@ 15495

Last change on this file since 15495 was 15495, checked in by mdewsnip, 16 years ago

Updated to include everything required by the "library" executable when Z39.50 support is compiled in.

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