mdb.c revision 1.47 1 /* $NetBSD: mdb.c,v 1.47 2018/11/21 20:04:48 martin Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18 * or promote products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 /* mdb.c - menu database manipulation */
36
37 #if HAVE_NBTOOL_CONFIG_H
38 #include "nbtool_config.h"
39 #endif
40
41 #include <sys/cdefs.h>
42
43 #if defined(__RCSID) && !defined(lint)
44 __RCSID("$NetBSD: mdb.c,v 1.47 2018/11/21 20:04:48 martin Exp $");
45 #endif
46
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "mdb.h"
52 #include "defs.h"
53 #include "pathnames.h"
54
55 /* Data */
56 #undef MAX
57 #define MAX 1000
58 static int menu_no = 0;
59 static id_rec *menus[MAX];
60
61 /* Other defines */
62 #define OPT_SUB 1
63 #define OPT_ENDWIN 2
64 #define OPT_EXIT 4
65
66
67 /* get_menu returns a pointer to a newly created id_rec or an old one. */
68
69 id_rec *
70 get_menu(char *name)
71 {
72 id_rec *temp;
73
74 temp = find_id (root, name);
75
76 if (temp == NULL) {
77 if (menu_no < MAX) {
78 temp = (id_rec *)malloc(sizeof(id_rec));
79 temp->id = strdup(name);
80 temp->info = NULL;
81 temp->menu_no = menu_no;
82 menus[menu_no++] = temp;
83 insert_id(&root, temp);
84 } else {
85 (void)fprintf(stderr, "Too many menus. "
86 "Increase MAX.\n");
87 exit(1);
88 }
89 }
90
91 return temp;
92 }
93
94
95 /* Verify that all menus are defined. */
96 void
97 check_defined(void)
98 {
99 int i;
100
101 for (i = 0; i < menu_no; i++)
102 if (!menus[i]->info)
103 yyerror ("Menu '%s' undefined.", menus[i]->id);
104 }
105
106
107 /* Write out the menu file. */
108 void
109 write_menu_file(char *initcode)
110 {
111 FILE *out_file, *sys_file;
112 int i, j;
113 char hname[1024], cname[1024], sname[1024];
114 char *sys_prefix, *tmpstr;
115 int name_is_code, nlen, ch;
116 optn_info *toptn;
117
118 /* Generate file names */
119 snprintf(hname, 1024, "%s.h", out_name);
120 nlen = strlen(hname);
121 if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
122 (void)fprintf(stderr, "%s: name `%s` too long.\n",
123 prog_name, out_name);
124 exit(1);
125 }
126 snprintf(cname, 1024, "%s.c", out_name);
127
128 /* Open the menu_sys file first. */
129 sys_prefix = getenv("MENUDEF");
130 if (sys_prefix == NULL)
131 sys_prefix = _PATH_DEFSYSPREFIX;
132 snprintf(sname, 1024, "%s/%s", sys_prefix, sys_name);
133 sys_file = fopen (sname, "r");
134 if (sys_file == NULL) {
135 (void)fprintf(stderr, "%s: could not open %s.\n",
136 prog_name, sname);
137 exit(1);
138 }
139
140 /* Output the .h file first. */
141 out_file = fopen(hname, "w");
142 if (out_file == NULL) {
143 (void)fprintf(stderr, "%s: could not open %s.\n",
144 prog_name, hname);
145 exit(1);
146 }
147
148 /* Write it */
149 (void)fprintf(out_file, "%s",
150 "/* menu system definitions. */\n"
151 "\n"
152 "#ifndef MENU_DEFS_H\n"
153 "#define MENU_DEFS_H\n"
154 "#include <stdlib.h>\n"
155 "#include <string.h>\n"
156 "#include <ctype.h>\n"
157 "#include <curses.h>\n\n"
158 );
159
160 if (do_msgxlat)
161 (void)fprintf(out_file, "#define MSG_XLAT(x) msg_string(x)\n");
162 else
163 (void)fprintf(out_file, "#define MSG_XLAT(x) (x)\n");
164 if (do_dynamic)
165 (void)fprintf(out_file, "#define DYNAMIC_MENUS\n");
166 if (do_expands)
167 (void)fprintf(out_file, "#define MENU_EXPANDS\n");
168 if (do_dynamic || do_msgxlat)
169 (void)fprintf(out_file, "\n");
170
171 (void)fprintf(out_file,
172 "typedef struct menudesc menudesc;\n"
173 "typedef struct menu_ent menu_ent;\n"
174 "struct menu_ent {\n"
175 " const char *opt_name;\n"
176 "#ifdef MENU_EXPANDS\n"
177 " const char *opt_exp_name;\n"
178 "#endif\n"
179 " int opt_menu;\n"
180 " int opt_flags;\n"
181 " int (*opt_action)(menudesc *, void *);\n"
182 "};\n\n"
183 "#define OPT_SUB 1\n"
184 "#define OPT_ENDWIN 2\n"
185 "#define OPT_EXIT 4\n"
186 "#define OPT_IGNORE 8\n"
187 "#define OPT_NOMENU -1\n\n"
188 "struct menudesc {\n"
189 " const char *title;\n"
190 " int y, x;\n"
191 " int h, w;\n"
192 " int mopt;\n"
193 " int numopts;\n"
194 " int cursel;\n"
195 " int topline;\n"
196 " menu_ent *opts;\n"
197 " WINDOW *mw;\n"
198 " WINDOW *sv_mw;\n"
199 " const char *helpstr;\n"
200 " const char *exitstr;\n");
201 if (do_expands)
202 (void)fprintf(out_file,
203 " void (*expand_act)(menudesc *, "
204 "void *);\n");
205 (void)fprintf(out_file,
206 " void (*post_act)(menudesc *, void *);\n"
207 " void (*exit_act)(menudesc *, void *);\n"
208 " void (*draw_line)(menudesc *, int, void *);\n"
209 "};\n"
210 "\n"
211 "/* defines for mopt field. */\n"
212 #define STR(x) #x
213 #define MC_OPT(x) "#define " #x " " STR(x) "\n"
214 MC_OPT(MC_NOEXITOPT)
215 MC_OPT(MC_NOBOX)
216 MC_OPT(MC_SCROLL)
217 MC_OPT(MC_NOSHORTCUT)
218 MC_OPT(MC_NOCLEAR)
219 MC_OPT(MC_DFLTEXIT)
220 MC_OPT(MC_ALWAYS_SCROLL)
221 MC_OPT(MC_SUBMENU)
222 MC_OPT(MC_VALID)
223 #undef MC_OPT
224 #undef STR
225 );
226
227 (void)fprintf(out_file, "%s",
228 "\n"
229 "/* Prototypes */\n"
230 "int menu_init(void);\n"
231 "void process_menu(int, void *);\n"
232 "__dead void __menu_initerror(void);\n"
233 );
234
235 if (do_dynamic)
236 (void)fprintf(out_file, "%s",
237 "int new_menu(const char *, menu_ent *, int, \n"
238 "\tint, int, int, int, int,\n"
239 "\tvoid (*)(menudesc *, void *), "
240 "void (*)(menudesc *, int, void *),\n"
241 "\tvoid (*)(menudesc *, void *), "
242 "const char *, const char *);\n"
243 "void free_menu(int);\n"
244 "void set_menu_numopts(int, int);\n"
245 );
246
247 (void)fprintf(out_file, "\n/* Menu names */\n");
248 for (i = 0; i < menu_no; i++)
249 (void)fprintf(out_file, "#define MENU_%s\t%d\n",
250 menus[i]->id, i);
251
252 if (do_dynamic)
253 (void)fprintf(out_file, "\n#define DYN_MENU_START\t%d",
254 menu_no);
255
256 (void)fprintf (out_file, "\n#define MAX_STRLEN %d\n"
257 "#endif\n", max_strlen);
258 fclose(out_file);
259
260 /* Now the C file */
261 out_file = fopen(cname, "w");
262 if (out_file == NULL) {
263 (void)fprintf(stderr, "%s: could not open %s.\n",
264 prog_name, cname);
265 exit(1);
266 }
267
268 /* initial code */
269 fprintf(out_file, "#include \"%s\"\n\n", hname);
270 fprintf(out_file, "%s\n\n", initcode);
271
272 /* data definitions */
273
274 /* func defs */
275 for (i = 0; i < menu_no; i++) {
276 if (do_expands && strlen(menus[i]->info->expact.code)) {
277 (void)fprintf(out_file, "/*ARGSUSED*/\n"
278 "static void menu_%d_expact(menudesc *menu, void *arg)\n{\n", i);
279 if (menus[i]->info->expact.endwin)
280 (void)fprintf(out_file, "\tendwin();\n");
281 (void)fprintf(out_file, "\t%s\n}\n\n",
282 menus[i]->info->expact.code);
283 }
284 if (strlen(menus[i]->info->postact.code)) {
285 (void)fprintf(out_file, "/*ARGSUSED*/\n"
286 "static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i);
287 if (menus[i]->info->postact.endwin)
288 (void)fprintf(out_file, "\tendwin();\n");
289 (void)fprintf(out_file, "\t%s\n}\n\n",
290 menus[i]->info->postact.code);
291 }
292 if (strlen(menus[i]->info->exitact.code)) {
293 (void)fprintf(out_file, "/*ARGSUSED*/\n"
294 "static void menu_%d_exitact(menudesc *menu, void *arg)\n{\n", i);
295 if (menus[i]->info->exitact.endwin)
296 (void)fprintf(out_file, "\tendwin();\n");
297 (void)fprintf(out_file, "\t%s\n}\n\n",
298 menus[i]->info->exitact.code);
299 }
300 j = 0;
301 toptn = menus[i]->info->optns;
302 for (; toptn != NULL; j++, toptn = toptn->next) {
303 if (strlen(toptn->optact.code) == 0)
304 continue;
305
306 (void)fprintf(out_file, "/*ARGSUSED*/\n"
307 "static int opt_act_%d_%d(menudesc *m, void *arg)\n"
308 "{\n\t%s\n\treturn %s;\n}\n\n",
309 i, j, toptn->optact.code,
310 (toptn->doexit ? "1" : "0"));
311 }
312
313 }
314
315 /* optentX */
316 for (i = 0; i < menu_no; i++) {
317 if (menus[i]->info->numopt > 53) {
318 (void)fprintf(stderr, "Menu %s has "
319 "too many options.\n",
320 menus[i]->info->title);
321 exit(1);
322 }
323 (void)fprintf(out_file, "static menu_ent optent%d[] = {\n", i);
324 name_is_code = 0;
325 for (j = 0, toptn = menus[i]->info->optns; toptn;
326 toptn = toptn->next, j++) {
327 name_is_code += toptn->name_is_code;
328 (void)fprintf(out_file, "\t{ .opt_name = %s, "
329 ".opt_menu=%d, .opt_flags=%d, .opt_action=",
330 toptn->name_is_code ? "0" : toptn->name,
331 toptn->menu,
332 (toptn->issub ? OPT_SUB : 0)
333 +(toptn->doexit ? OPT_EXIT : 0)
334 +(toptn->optact.endwin ? OPT_ENDWIN : 0));
335 if (strlen(toptn->optact.code))
336 (void)fprintf(out_file, "opt_act_%d_%d}", i, j);
337 else
338 (void)fprintf(out_file, "NULL}");
339 (void)fprintf(out_file, "%s\n",
340 (toptn->next ? "," : ""));
341 }
342 (void)fprintf(out_file, "\t};\n\n");
343
344 if (name_is_code) {
345 menus[i]->info->name_is_code = 1;
346 fprintf(out_file, "static void menu_%d_legend("
347 "menudesc *menu, int opt, void *arg)\n{\n"
348 "\tswitch (opt) {\n", i);
349 for (j = 0, toptn = menus[i]->info->optns; toptn;
350 toptn = toptn->next, j++) {
351 if (!toptn->name_is_code)
352 continue;
353 fprintf(out_file, "\tcase %d:\n\t\t{%s};\n"
354 "\t\tbreak;\n", j, toptn->name);
355 }
356 fprintf(out_file, "\t}\n}\n\n");
357 }
358 }
359
360
361 /* menus */
362 if (!do_dynamic)
363 (void)fprintf(out_file, "static int num_menus = %d;\n",
364 menu_no);
365
366 (void)fprintf(out_file, "static struct menudesc menu_def[] = {\n");
367 for (i = 0; i < menu_no; i++) {
368 (void)fprintf(out_file,
369 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,NULL,",
370 menus[i]->info->title, menus[i]->info->y,
371 menus[i]->info->x, menus[i]->info->h,
372 menus[i]->info->w, menus[i]->info->mopt,
373 menus[i]->info->numopt, i);
374 if (menus[i]->info->helpstr == NULL)
375 (void)fprintf(out_file, "NULL");
376 else {
377 tmpstr = menus[i]->info->helpstr;
378 if (*tmpstr != '"')
379 (void)fprintf(out_file, "%s", tmpstr);
380 else {
381 /* Skip an initial newline. */
382 if (tmpstr[1] == '\n')
383 *++tmpstr = '"';
384 (void)fprintf(out_file, "\n");
385 while (*tmpstr) {
386 if (*tmpstr != '\n') {
387 fputc(*tmpstr++, out_file);
388 continue;
389 }
390 (void)fprintf(out_file, "\\n\"\n\"");
391 tmpstr++;
392 }
393 }
394 }
395 (void)fprintf(out_file, ",");
396 if (menus[i]->info->mopt & MC_NOEXITOPT)
397 (void) fprintf (out_file, "NULL");
398 else if (menus[i]->info->exitstr != NULL)
399 (void)fprintf(out_file, "%s", menus[i]->info->exitstr);
400 else
401 (void)fprintf(out_file, "\"Exit\"");
402 if (do_expands) {
403 if (strlen(menus[i]->info->expact.code))
404 (void)fprintf(out_file, ",menu_%d_expact", i);
405 else
406 (void)fprintf(out_file, ",NULL");
407 }
408 if (strlen(menus[i]->info->postact.code))
409 (void)fprintf(out_file, ",menu_%d_postact", i);
410 else
411 (void)fprintf(out_file, ",NULL");
412 if (strlen(menus[i]->info->exitact.code))
413 (void)fprintf(out_file, ",menu_%d_exitact", i);
414 else
415 (void)fprintf(out_file, ",NULL");
416 if (menus[i]->info->name_is_code)
417 (void)fprintf(out_file, ",menu_%d_legend", i);
418 else
419 (void)fprintf(out_file, ",NULL");
420
421 (void)fprintf(out_file, "},\n");
422
423 }
424 (void)fprintf(out_file, "{NULL, 0, 0, 0, 0, 0, 0, 0, 0, "
425 "NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL");
426 if (do_expands)
427 (void)fprintf(out_file, ", NULL");
428 (void)fprintf(out_file, "}};\n\n");
429
430 /* __menu_initerror: initscr failed. */
431 (void)fprintf(out_file,
432 "/* __menu_initerror: initscr failed. */\n"
433 "void __menu_initerror (void) {\n");
434 if (error_act.code == NULL) {
435 (void)fprintf(out_file,
436 "\t(void) fprintf (stderr, "
437 "\"%%s: Could not initialize curses\\n\", "
438 "getprogname());\n"
439 "\texit(1);\n"
440 "}\n");
441 } else {
442 if (error_act.endwin)
443 (void)fprintf(out_file, "\tendwin();\n");
444 (void)fprintf(out_file, "%s\n}\n", error_act.code);
445 }
446
447 /* Copy menu_sys.def file. */
448 while ((ch = fgetc(sys_file)) != '\014') /* Control-L */
449 fputc(ch, out_file);
450
451 if (do_dynamic) {
452 while ((ch = fgetc(sys_file)) != '\n')
453 /* skip it */;
454 while ((ch = fgetc(sys_file)) != EOF)
455 fputc(ch, out_file);
456 }
457 fclose(out_file);
458 fclose(sys_file);
459 }
460