forked from virtadpt/eBBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gram.y
374 lines (320 loc) · 7.08 KB
/
gram.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
%token ENVIRONMENT
%token MENU
%token READMENU
%token STRING
%token COMMAND
%token OBRACE
%token CBRACE
%token COMMA
%token OPAREN
%token CPAREN
%token ID_NAME
%token INTEGER
%token KEY
%token OPENFLAG
%token ERROR
%{
#include <stdio.h>
#include "client.h"
extern char *get_yytext() ;
extern int yyleng ;
%}
%start commands
%union {
int ival ;
char * sval ;
NMENUITEM *mival ;
NMENU *mval ;
NREADMENUITEM *rmival ;
NREADMENU *rmval ;
}
%type <ival> integer name ident key openflag
%type <sval> string
%type <mival> menulist menuitem command commandentry
%type <rmival> rmenulist rmenuitem rcommand
%%
commands :
| commands menudef
| commands rmenudef
| error
menudef : MENU string COMMA string COMMA string COMMA string OBRACE menulist CBRACE
{
NMENU *mkmenu(), *mp ;
if(mp = mkmenu($10)) {
mp->menu_id = $2 ;
mp->menu_title = $4 ;
mp->menu_prompt = $6 ;
mp->menu_default = $8 ;
pushmenu(mp) ;
}
}
| ENVIRONMENT OPAREN string CPAREN
{
extern char *default_env ;
default_env = $3 ;
parse_default() ;
}
menulist : menuitem
{
$$ = $1 ;
}
| menulist menuitem
{
if($1)
$2->prev = $1 ;
$$ = $2 ;
}
menuitem : OPAREN string COMMA string COMMA string COMMA ident COMMA commandentry COMMA string CPAREN
{
register NMENUITEM *mip ;
if(mip = $10) {
mip->name = $2 ;
mip->enabled = $8 ;
mip->default_action = $4 ;
mip->error_action = $6 ;
mip->help = $12 ;
}
$$ = mip ;
}
rmenudef : READMENU string COMMA string COMMA string COMMA string COMMA string COMMA string COMMA string COMMA string COMMA string COMMA string OBRACE rmenulist CBRACE
{
NREADMENU *mkreadmenu(), *mp ;
if(mp = mkreadmenu($2, $22)) {
mp->menu_helptitle = $20;
mp->menu_title = $4;
mp->menu_message = $6;
mp->menu_line2 = $8;
mp->menu_line3 = $10;
mp->menu_field1 = $12;
mp->menu_field2 = $14;
mp->menu_field3 = $16;
mp->menu_field4 = $18;
}
}
rmenulist : rmenuitem
{
$$ = $1 ;
}
| rmenulist rmenuitem
{
if($1)
$2->next = $1 ;
$$ = $2 ;
}
rmenuitem : OPAREN key COMMA rcommand COMMA openflag COMMA ident COMMA string CPAREN
{
register NREADMENUITEM *mip ;
if(mip = $4) {
mip->key = $2 ;
mip->mainprivs = $8 ;
mip->boardprivs = $6 ;
mip->help = $10 ;
}
$$ = mip ;
}
ident: integer
{ $$ = $1 ; }
| name
{ $$ = $1 ; }
command : COMMAND
{
NMENUITEM *mkmenuitem() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng] = '\0' ;
$$ = mkmenuitem(buf+1) ;
}
commandentry: command
{ $$ = $1; }
| command string
{
NMENUITEM *mip = $1 ;
if(mip)
mip->action_arg = $2 ;
$$ = mip ;
}
rcommand : COMMAND
{
NREADMENUITEM *mkreadmenuitem() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng] = '\0' ;
$$ = mkreadmenuitem(buf+1) ;
}
string : STRING
{
char *mkstring() ;
char *ExpandString() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng-1] = '\0' ;
$$ = mkstring(ExpandString(buf+1)) ;
}
integer : INTEGER
{
int atoi() ;
$$ = atoi(get_yytext()) ;
}
openflag : OPENFLAG
{
int convert_openflag_to_int() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng] = '\0' ;
$$ = convert_openflag_to_int(buf+2) ;
} | integer { $$ = $1; }
name : ID_NAME
{
int convert_cmd_to_int() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng] = '\0' ;
$$ = convert_cmd_to_int(buf) ;
}
key : KEY
{
int convert_key_to_int() ;
char buf[512] ;
strncpy(buf,get_yytext(),yyleng) ;
buf[yyleng] = '\0' ;
$$ = convert_key_to_int(buf) ;
}
%%
char *mkstring(s)
register char *s ;
{
register char *p ;
p = (char *) malloc(strlen(s)+1) ;
strcpy(p,s) ;
return p ;
}
menuerror()
{
do_echo("WARNING, invalid function in Menu definition\n") ;
return 0 ;
}
rmenuerror()
{
do_echo("WARNING, invalid function in ReadMenu definition\n") ;
return 0 ;
}
NMENUITEM *
mkmenuitem(s)
char *s ;
{
NMENUITEM *mip ;
extern int line_num ;
extern int (*findfunc())() ;
if(!(mip = (NMENUITEM *) malloc(sizeof(NMENUITEM))))
return NULL ;
memset(mip,0,sizeof(NMENUITEM)) ;
if(!(mip->action_func = findfunc(s))) {
char buf[512] ;
sprintf(buf,"WARNING, invalid function in menu file\nfunction = '%s' LINE %d\n",s,line_num) ;
do_echo(buf) ;
mip->action_func = menuerror ;
}
return mip ;
}
char
getmenuletter(s)
char *s ;
{
register char firstch;
for (firstch = *s; *s; s++)
if (*s >= 'A' && *s <= 'Z') return *s;
return firstch;
}
int
getmenuindex(t)
unsigned int t ;
{
t = (t | 0x20) - 'a' ;
return t % MAXMENUSZ ;
}
NMENU *
mkmenu(mip)
NMENUITEM *mip ;
{
NMENU *mp ;
NMENUITEM *tp ;
if(!(mp = (NMENU *)malloc(sizeof(NMENU))))
return NULL ;
memset(mp,0,sizeof(NMENU)) ;
mp->commlist = mip ;
for(tp = mip;tp;tp = tp->prev) {
register char menuletter = getmenuletter(tp->name) ;
register int menuindex = getmenuindex(menuletter) ;
if(mp->menucommands[menuindex]) {
char buf[512] ;
extern int line_num ;
sprintf(buf,
"Warning Menu command %s attempting to use filled slot!\nLINE %d\n",
tp->name,line_num) ;
do_echo(buf) ;
continue ;
}
mp->menucommands[menuindex] = tp ;
}
return mp ;
}
NMENU *bigMenuList = NULL ;
pushmenu(mp)
NMENU *mp ;
{
NMENUITEM *mip = NULL ;
NMENUITEM *tmp, *next ;
for(tmp=mp->commlist;tmp;tmp = next) {
next = tmp->prev ;
tmp->next = mip ;
mip = tmp ;
}
mp->commlist = mip ;
mp->next = bigMenuList ;
bigMenuList = mp ;
}
NREADMENU *PostReadMenu = NULL;
NREADMENU *MailReadMenu = NULL;
NREADMENU *FileReadMenu = NULL;
NREADMENU *
mkreadmenu(name, mip)
char *name ;
NREADMENUITEM *mip ;
{
NREADMENU *mp ;
if(!(mp = (NREADMENU *)malloc(sizeof(NREADMENU))))
return NULL ;
if (!strcasecmp(name, "Main")) PostReadMenu = mp;
else if (!strcasecmp(name, "Mail")) MailReadMenu = mp;
else if (!strcasecmp(name, "File")) FileReadMenu = mp;
else {
char buf[512] ;
extern int line_num ;
sprintf(buf,
"Warning Invalid ReadMenu '%s'!\nLINE %d\n", name, line_num) ;
do_echo(buf) ;
free(mp);
return NULL;
}
memset(mp,0,sizeof(NREADMENU)) ;
mp->commlist = mip ;
return mp ;
}
NREADMENUITEM *
mkreadmenuitem(s)
char *s ;
{
NREADMENUITEM *mip ;
extern int line_num ;
extern int (*findrfunc())() ;
if(!(mip = (NREADMENUITEM *) malloc(sizeof(NREADMENUITEM))))
return NULL ;
memset(mip,0,sizeof(NREADMENUITEM)) ;
if(!(mip->action_func = findrfunc(s))) {
char buf[512] ;
sprintf(buf,"WARNING, invalid function in menu file\nfunction = '%s' LINE %d\n",s,line_num) ;
do_echo(buf) ;
mip->action_func = rmenuerror ;
}
return mip ;
}