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

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

Moved definition of FILE *errfile from z3950proto.cpp to zparse.y for
portability reasons. (linking C++ to C)

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