-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubr.c
294 lines (244 loc) · 6.19 KB
/
subr.c
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
/* subr.c */
/*
* Supporting subroutines for the menu generation
* language (MGL)
*
* Tony Mason
* November 1988
* Completed by John Levine, August 1992
*/
/* includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mglyac.tab.h"
#include "mglhdr.h"
#include "mgl-code" /* contains definitions of skeleton file to be built */
extern FILE *yyin, *yyout;
/* imports */
extern int screen_done;
extern char *cmd_str, *act_str,*item_str;
/* exports */
/* local */
static char current_screen[100]; /* reasonable? */
static int done_start_init;
static int done_end_init;
static int current_line;
struct item {
char *desc; /* item description */
char *cmd; /* command */
int action; /* action to take */
char *act_str; /* action operation */
int attribute; /* visible/invisible */
struct item *next; /* next member of list */
} *item_list, *last_item;
/* macros */
#define SCREEN_SIZE 80
void mglfree(char *); /* free if not null */
/* code */
/*
* start_screen:
* This routine begins preparation of the screen. It
* writes the preamble and modifies the global state
* variable screen_done to show that a screen is in
* progress (thus, if a screen is in progress when EOF
* is seen, an appropriate error message can be given).
*/
int start_screen(char *name) /* name of screen to create */
{
long time(),tm = time((long *)0);
char *ctime();
if(!done_start_init)
{
fprintf(yyout, "/*\n * Generated by MGL: %s */\n\n", ctime(&tm));
dump_data(screen_init);
done_start_init = 1;
}
if(check_name(name) == 0)
warning("Reuse of name",name);
fprintf(yyout, "/* screen %s */\n", name);
fprintf(yyout, "void menu_%s()\n{\n",name);
fprintf(yyout, "\textern struct item menu_%s_items[];\n\n",name);
fprintf(yyout, "\tif(!init) menu_init();\n\n");
fprintf(yyout, "\tclear();\n\trefresh();\n");
if(strlen(name) > sizeof current_screen)
warning("Screen name is larger than buffer",(char *)0);
strncpy(current_screen, name, sizeof(current_screen) - 1);
screen_done = 0;
current_line = 0;
return 0;
}
/*
* add_title:
* Add centered text to screen code.
*/
int add_title(char *line)
{
int length = strlen(line);
int space = (SCREEN_SIZE - length) / 2;
fprintf(yyout, "\tmove(%d,%d);\n",current_line, space);
current_line++;
fprintf(yyout, "\taddstr(\"%s\");\n",line);
fprintf(yyout, "\trefresh();\n");
return 0;
}
/*
* add_line:
* Add a line to the actions table. It will be written
* out after all lines have been added. Note that some
* of the information is in global variables.
*/
int add_line(int action, int attrib)
{
struct item *new;
new = (struct item *)malloc(sizeof(struct item));
if(!item_list)
{ /* first item */
item_list = last_item = new;
}
else
{ /* already items on the list */
last_item->next = new;
last_item = new;
}
new->next = NULL; /* mark end of list */
new->desc = item_str;
new->cmd = cmd_str;
new->action = action;
switch(action)
{
case EXECUTE:
new->act_str = act_str;
break;
case MENU:
new->act_str = act_str;
break;
default:
new->act_str = 0;
break;
}
new->attribute = attrib;
return 0;
}
/*
* end_screen:
* Finish screen, print out postamble.
*/
int end_screen(char *name)
{
fprintf(yyout, "\tmenu_runtime(menu_%s_items);\n", name);
if(strcmp(current_screen,name) != 0)
{
warning("name mismatch at end of screen",
current_screen);
}
fprintf(yyout, "}\n");
fprintf(yyout, "/* end %s */\n\n", current_screen);
process_items();
/* write initialization code out to file */
if(!done_end_init)
{
done_end_init = 1;
dump_data(menu_init);
}
current_screen[0] = '\0'; /* no current screen */
screen_done = 1;
return 0;
}
/*
* process_items:
* Walk the list of menu items and write them to an
* external initialized array. Also defines the symbolic
* constant used for the run-time support module (which
* is below this table).
*/
int process_items()
{
int cnt = 0;
struct item *ptr;
if(item_list == 0)
return 0; /* nothing to do */
fprintf(yyout, "struct item menu_%s_items[]={\n",current_screen);
ptr = item_list;
/* climb through the list */
while(ptr)
{
struct item *optr;
if(ptr->action == MENU)
fprintf(yyout, "{\"%s\",\"%s\",%d,\"\",%s,%d},\n",
ptr->desc,ptr->cmd, ptr->action,
ptr->act_str,ptr->attribute);
else
fprintf(yyout, "{\"%s\",\"%s\",%d,\"%s\",0,%d},\n",
ptr->desc,ptr->cmd, ptr->action,
ptr->act_str ? ptr->act_str : "",
ptr->attribute);
mglfree(ptr->desc);
mglfree(ptr->cmd);
mglfree(ptr->act_str);
optr = ptr;
ptr = ptr->next;
free(optr);
cnt++;
}
fprintf(yyout, "{(char *)0, (char *)0, 0, (char *)0, 0, 0},\n");
fprintf(yyout, "};\n\n");
item_list = 0;
/* next the run-time module that does all the "work" */;
return 0;
}
/*
* This routine takes a null-terminated list of strings
* and prints them on the standard out. Its sole purpose
* in life is to dump the big static arrays making up the
* runtime code for the menus generated.
*/
int dump_data(char **array)
{
while(*array)
fprintf(yyout, "%s\n",*array++);
return 0;
}
/*
* this routine writes out the run-time support
*/
int end_file()
{
dump_data(menu_runtime);
return 0;
}
/*
* Check a name to see if it has already been used. If
* not, return 1; otherwise, return 0. This routine also
* squirrels away the name for future reference. Note
* that this routine is purely dynamic. It would be
* easier to just set up a static array, but less flexible.
*/
int check_name(char *name)
{
static char **names = 0;
static int name_count = 0;
char **ptr,*newstr;
if(!names)
{
names = (char **)malloc(sizeof(char *));
*names = 0;
}
ptr = names;
while(*ptr)
{
if(strcmp(name,*ptr++) == 0) return 0;
}
/* not in use */
name_count++;
names = (char **)realloc(names, (name_count+1) * sizeof(char *));
names[name_count] = 0;
newstr = strdup(name);
names[name_count-1] = newstr;
return 1;
}
void mglfree(char *p)
{
if(p)
free(p);
}