summaryrefslogtreecommitdiffstats
path: root/lib/libforms/parser.y
blob: 401e991b12d177caa1cf40665e14fd970c4d2719 (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
%{
/*-
 * Copyright (c) 1995
 *	Paul Richards.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer, 
 *    verbatim and that no modifications are made prior to this 
 *    point in the file.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Paul Richards.
 * 4. The name Paul Richards may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <strhash.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <forms.h>
#include <err.h>

#include "internal.h"

char *cpstr(char *);

extern int yyleng;
int lineno = 1;
int charno = 1;
int off;

char *fieldname;
char *defname;
char *formname;
char *startname;
char *colortable;
int formattr;
char *text;
char *label;
char *function;
char *up, *down, *left, *right, *next;
int height, width;
int y, x;
int width;
int limit;
int attr;
int selattr;
int type;
int lbl_flag;
int selected, no_options=0;

extern FILE *outf;
extern hash_table *global_bindings;

struct MenuList {
	char *option;
	struct MenuList *next;
};

struct MenuList *cur_menu;
struct MenuList *menu_list;
struct MenuList *menu;

struct pair_node {
	char *foreground;
	char *background;
	struct pair_node *next;
};
struct pair_node *pair_list;
struct pair_node *cur_pair;
struct pair_node *pair;

struct color_table {
	char *tablename;
	struct pair_node *pairs;
	struct color_table *next;
};

struct color_table *color_table;
struct color_table *cur_table;
struct color_table *color_tables;

struct Form *form;
struct Field *field_inst_list;
struct Field *field;
struct Field *cur_field;
%}

%union {
	int ival;
	char *sval;
}

%token <ival> FORM
%token <ival> COLORTABLE
%token <ival> COLOR
%token <ival> BLACK
%token <ival> RED
%token <ival> GREEN
%token <ival> YELLOW
%token <ival> BLUE
%token <ival> MAGENTA
%token <ival> CYAN
%token <ival> WHITE
%token <ival> PAIR
%token <sval> NAME
%token <sval> STRING
%token <ival> AT
%token <ival> AS
%token <ival> HEIGHT
%token <ival> EQUALS
%token <ival> NUMBER
%token <ival> WIDTH
%token <ival> STARTFIELD
%token <ival> COMMA
%token <ival> LBRACE
%token <ival> RBRACE
%token <ival> TEXT
%token <ival> ATTR
%token <ival> SELATTR
%token <ival> DEFAULT
%token <ival> LABEL
%token <ival> LIMIT
%token <ival> SELECTED
%token <ival> OPTIONS
%token <ival> ACTION
%token <ival> FUNC
%token <ival> LINK
%token <ival> UP
%token <ival> DOWN
%token <ival> LEFT
%token <ival> RIGHT
%token <ival> NEXT
%token <ival> DEF

%type <sval> a_color

%start spec

%%

spec: /* empty */
	| spec fields
	| spec forms
	| spec colours
	;

colours: COLOR NAME 
		{
			color_table = malloc(sizeof (struct color_table));
			if (!color_table) {
				fprintf(stderr, "Couldn't allocate memory for a color table\n");
				exit (1);
			}
			color_table->tablename = cpstr($2);
		}
	LBRACE color_pairs RBRACE
		{
			color_table->pairs = pair_list;
			cur_pair = 0;
			form_bind_tuple(global_bindings, color_table->tablename, FT_COLTAB, color_table);
		}
	;

color_pairs: /* empty */
	| color_pairs pair
	;

pair: PAIR EQUALS a_color
		{
			pair = malloc(sizeof (struct pair_node));
			if (!pair) {
				fprintf(stderr, "Couldn't allocate memory for a color pair\n");
				exit(1);
			}
			pair->foreground = cpstr($3);
		}
	COMMA a_color
		{
			pair->background = cpstr($6);
			if (!cur_pair) {
				pair_list = pair;
				cur_pair = pair;
			} else {
				cur_pair->next = pair;
				cur_pair = pair;
			}
		}
	;

a_color: BLACK
		{ $$ = "COLOR_BLACK"; }
	| RED
		{ $$ = "COLOR_RED"; }
	| GREEN
		{ $$ = "COLOR_GREEN"; }
	| YELLOW
		{ $$ = "COLOR_YELLOW"; }
	| BLUE
		{ $$ = "COLOR_BLUE"; }
	| MAGENTA
		{ $$ = "COLOR_MAGENTA"; }
	| CYAN
		{ $$ = "COLOR_CYAN"; }
	| WHITE
		{ $$ = "COLOR_WHITE"; }
	;

forms:  FORM NAME 
		 { formname = cpstr($2); }
	AT coord 
		{
			form = malloc(sizeof (struct Form));
			if (!form) {
				fprintf(stderr,"Failed to allocate memory for form\n");
				exit(1);
			}
			form->bindings = hash_create(0);
			if (!form->bindings)
				errx(1, "Failed to allocate hash table for form");
			form->y = y;
			form->x = x;
		}
	LBRACE formspec RBRACE
		{
			form->startfield = startname;
			form->colortable = colortable;
			form->height = height;
			form->width = width;
			form->attr = formattr;
			form_bind_tuple(global_bindings, formname, FT_FORM, form);
		}
	;

formspec: height width startfield colortable formattr fieldlocs
	;

startfield:	/* empty */
		{	startname = 0; 
			printf("Warning: No start field specified for form %s\n", formname);
		}
	| STARTFIELD EQUALS NAME
		{ startname = cpstr($3); }
	;

colortable: /*empty */
		{ colortable = 0; }
	| COLORTABLE EQUALS NAME
		{ colortable = cpstr($3); }
	;

formattr: /* empty */
		{ formattr = 0; }
	| ATTR EQUALS NUMBER
		{ formattr = $3; }
	;

fieldlocs:	/* empty */
	| fieldlocs field_at
	;

field_at: NAME 
		 { fieldname = cpstr($1); }
	field_def AT coord
		{ 
			field = malloc(sizeof (struct Field));
			if (!field) {
				fprintf(stderr,"Failed to allocate memory for form field\n");
				exit(1);
			}
			if (!defname)
				field->defname = fieldname;
			else
				field->defname = defname;
			field->y = y;
			field->x = x;
		}
	links
		{
			field->fup = up;
			field->fdown = down;
			field->fleft = left;
			field->fright = right;
			field->fnext = next;
			up = 0;
			down = 0;
			left = 0;
			right = 0;
			next = 0;
			form_bind_tuple(form->bindings, fieldname, FT_FIELD_INST, field);
		}
	;

fields: NAME 
		{ defname = cpstr($1); }
	field_spec
		{ define_field(defname); }
	;

field_def: /* empty */
		{ defname = 0; }
	| LBRACE NAME 
		{ defname = cpstr($2); }
	  RBRACE
	| field_spec
		{ defname = fieldname; define_field(defname); }
	;

field_spec: LBRACE height width attr selattr type RBRACE
	;

links: /* empty */
	| links COMMA conns
	;

conns: UP EQUALS NAME
		{ up = cpstr($3); }
	| DOWN EQUALS NAME
		{ down = cpstr($3); }
	| LEFT EQUALS NAME
		{ left = cpstr($3); }
	| RIGHT EQUALS NAME
		{ right = cpstr($3); }
	| NEXT EQUALS NAME
		{ next = cpstr($3); }
	;

type: textfield
	| inputfield
	| menufield
	| actionfield
	;

textfield:	TEXT EQUALS STRING
			{ type = FF_TEXT; text = cpstr($3); }
	;

inputfield:	inputspec
			{ type = FF_INPUT; }
	;

inputspec:	LABEL EQUALS STRING limit
			{ lbl_flag = 1; label = cpstr($3); }
	| DEFAULT EQUALS STRING limit
			{ lbl_flag = 0; label = cpstr($3); }
	;

limit: /* empty */
	| LIMIT EQUALS NUMBER
			{ limit = $3; }

menufield: SELECTED EQUALS NUMBER OPTIONS EQUALS menuoptions
			{ type = FF_MENU; selected = $3; }
	;

menuoptions: menuoption
	| menuoptions COMMA menuoption
	;

menuoption: STRING
			{	
				menu = malloc(sizeof(struct MenuList));
				if (!menu) {
						err(1, "Couldn't allocate memory for menu option\n");
				}
				menu->option = cpstr($1);
				if (!cur_menu) {  
					menu_list = menu;
					cur_menu = menu;
				} else {
					cur_menu->next = menu; 
					cur_menu = menu;
				}
			}
;

actionfield: ACTION EQUALS STRING FUNC EQUALS NAME
			{ type = FF_ACTION; text = cpstr($3); function = cpstr($6); }
	;

height: /* empty */
		{ height = 0; }
	| HEIGHT EQUALS NUMBER
		{ height = $3; }
	;

width: /* empty */
			{ width = 0; }
	| WIDTH EQUALS NUMBER
			{ width = $3; }
	;

attr: /* empty */
			{ attr = 0; }
	| ATTR EQUALS NUMBER
			{ attr = $3; }
	;

selattr: /* empty */
			{ selattr = 0; }
	| SELATTR EQUALS NUMBER
			{ selattr = $3; }
	;

coord: NUMBER COMMA NUMBER
		{ y = $1; x = $3; }
	;

%%

void
yyerror (char *error)
{
	fprintf(stderr, "%s at line %d\n",error, lineno);
	exit(1);
}

char *
cpstr(char *ostr)
{
	char *nstr;

	nstr = malloc(strlen(ostr)+1);
	if (!nstr) {
		fprintf(stderr, "Couldn't allocate memory for string\n");
		exit(1);
	}
	strcpy(nstr, ostr);
	return (nstr);
}

void
define_field(char *defname)
{
	struct Field *field;
	struct MenuList *menu_options;
	int no_options;

	field = malloc(sizeof (struct Field));
	if (!field) {
		fprintf(stderr,"Failed to allocate memory for form field\n");
		exit(1);
	}
	field->defname = defname;
	field->type = type;
	field->height = height;
	field->width = width;
	field->attr = attr;
	field->selattr = selattr;
	switch (type) {
		case FF_TEXT:
			field->field.text = malloc(sizeof (struct TextField));
			if (!field->field.text) {
				fprintf(stderr,
						"Failed to allocate memory for text field\n");
				exit (1);
			}
			field->field.text->text = text;
			break;
		case FF_INPUT:
			field->field.input = malloc(sizeof (struct InputField));
			if (!field->field.input) {
				fprintf(stderr,
						"Failed to allocate memory for input field\n");
				exit (1);
			}
			field->field.input->lbl_flag = lbl_flag;
			field->field.input->label = label;
			field->field.input->limit = limit;
			break;
		case FF_MENU:
			printf("field type %s = %d\n", defname,field->type);
			field->field.menu = malloc(sizeof (struct MenuField));
			if (!field->field.menu) {
				fprintf(stderr,
						"Failed to allocate memory for menu field\n");
				exit (1);
			}
			field->field.menu->selected = selected;
			menu_options = menu_list;
			field->field.menu->no_options = 0;
			field->field.menu->options = 0;
			for (; menu_options; menu_options = menu_options->next) {
				no_options = add_menu_option(field->field.menu,
											 menu_options->option);
				if (!no_options)
					err(1, "Couldn't add menu option");
			}
			field->field.menu->no_options = no_options;
			cur_menu = 0;
			break;
		case FF_ACTION:
			field->field.action = malloc(sizeof (struct ActionField));
			if (!field->field.action) {
				fprintf(stderr,
						"Failed to allocate memory for action field\n");
				exit (1);
			}
			field->field.action->text = text;
			field->field.action->fn = (void *) function;
			break;
		default:
			break;
	}
	form_bind_tuple(global_bindings, defname, FT_FIELD_DEF, field);
	width=0;
	height = 0;
	attr=0;
	selattr=0;
	limit=0;
}
OpenPOWER on IntegriCloud