1 /* $NetBSD: mdb.c,v 1.53 2019/06/23 11:31:10 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.53 2019/06/23 11:31:10 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 = 1; /* Menu 0 is unused (means no menu) */
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 *
get_menu(char * name)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 = calloc(1, 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
check_defined(void)97 check_defined(void)
98 {
99 int i;
100
101 for (i = 1; 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
write_menu_file(char * initcode)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 if (do_expands)
177 (void)fprintf(out_file,
178 " const char *opt_exp_name;\n");
179 (void)fprintf(out_file,
180 " short opt_menu;\n"
181 " short opt_flags;\n"
182 " int (*opt_action)(menudesc *, void *);\n"
183 "};\n\n"
184 "#define OPT_NOMENU 0\n"
185 "#define OPT_SUB 1\n"
186 "#define OPT_ENDWIN 2\n"
187 "#define OPT_EXIT 4\n"
188 "#define OPT_IGNORE 8\n"
189 "#define OPT_NOSHORT 16\n\n"
190 "struct menudesc {\n"
191 " const char *title;\n");
192 if (do_expands)
193 (void)fprintf(out_file,
194 " const char *exp_title;\n");
195 (void)fprintf(out_file,
196 " short y, x;\n"
197 " short h, w;\n"
198 " short mopt;\n"
199 " short numopts;\n"
200 " short cursel;\n"
201 " short topline;\n"
202 " menu_ent *opts;\n"
203 " WINDOW *mw;\n"
204 " WINDOW *sv_mw;\n"
205 " const char *helpstr;\n"
206 " const char *exitstr;\n");
207 if (do_expands)
208 (void)fprintf(out_file,
209 " void (*expand_act)(menudesc *, "
210 "void *);\n");
211 (void)fprintf(out_file,
212 " void (*post_act)(menudesc *, void *);\n"
213 " void (*exit_act)(menudesc *, void *);\n"
214 " void (*draw_line)(menudesc *, int, void *);\n"
215 "};\n"
216 "\n"
217 "/* defines for mopt field. */\n"
218 #define STR(x) #x
219 #define MC_OPT(x) "#define " #x " " STR(x) "\n"
220 MC_OPT(MC_NOEXITOPT)
221 MC_OPT(MC_NOBOX)
222 MC_OPT(MC_SCROLL)
223 MC_OPT(MC_NOSHORTCUT)
224 MC_OPT(MC_NOCLEAR)
225 MC_OPT(MC_DFLTEXIT)
226 MC_OPT(MC_ALWAYS_SCROLL)
227 MC_OPT(MC_SUBMENU)
228 MC_OPT(MC_CONTINUOUS)
229 MC_OPT(MC_VALID)
230 #undef MC_OPT
231 #undef STR
232 );
233
234 (void)fprintf(out_file, "%s",
235 "\n"
236 "/* Prototypes */\n"
237 "int menu_init(void);\n"
238 "void process_menu(int, void *);\n"
239 "__dead void __menu_initerror(void);\n"
240 );
241
242 if (do_dynamic)
243 (void)fprintf(out_file, "%s",
244 "int new_menu(const char *, menu_ent *, int, \n"
245 "\tint, int, int, int, int,\n"
246 "\tvoid (*)(menudesc *, void *), "
247 "void (*)(menudesc *, int, void *),\n"
248 "\tvoid (*)(menudesc *, void *), "
249 "const char *, const char *);\n"
250 "void free_menu(int);\n"
251 "void set_menu_numopts(int, int);\n"
252 "menudesc *get_menudesc(int);\n"
253 );
254
255 (void)fprintf(out_file, "\n/* Menu names */\n");
256 for (i = 1; i < menu_no; i++)
257 (void)fprintf(out_file, "#define MENU_%s\t%d\n",
258 menus[i]->id, i);
259
260 if (do_dynamic)
261 (void)fprintf(out_file, "\n#define DYN_MENU_START\t%d",
262 menu_no);
263
264 (void)fprintf (out_file, "\n#define MAX_STRLEN %d\n"
265 "#endif\n", max_strlen);
266 fclose(out_file);
267
268 /* Now the C file */
269 out_file = fopen(cname, "w");
270 if (out_file == NULL) {
271 (void)fprintf(stderr, "%s: could not open %s.\n",
272 prog_name, cname);
273 exit(1);
274 }
275
276 /* initial code */
277 fprintf(out_file, "#include \"%s\"\n\n", hname);
278 fprintf(out_file, "%s\n\n", initcode);
279
280 /* data definitions */
281
282 /* func defs */
283 for (i = 1; i < menu_no; i++) {
284 if (do_expands && strlen(menus[i]->info->expact.code)) {
285 (void)fprintf(out_file, "/*ARGSUSED*/\n"
286 "static void menu_%d_expact(menudesc *menu, void *arg)\n{\n", i);
287 if (menus[i]->info->expact.endwin)
288 (void)fprintf(out_file, "\tendwin();\n");
289 (void)fprintf(out_file, "\t%s\n}\n\n",
290 menus[i]->info->expact.code);
291 }
292 if (strlen(menus[i]->info->postact.code)) {
293 (void)fprintf(out_file, "/*ARGSUSED*/\n"
294 "static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i);
295 if (menus[i]->info->postact.endwin)
296 (void)fprintf(out_file, "\tendwin();\n");
297 (void)fprintf(out_file, "\t%s\n}\n\n",
298 menus[i]->info->postact.code);
299 }
300 if (strlen(menus[i]->info->exitact.code)) {
301 (void)fprintf(out_file, "/*ARGSUSED*/\n"
302 "static void menu_%d_exitact(menudesc *menu, void *arg)\n{\n", i);
303 if (menus[i]->info->exitact.endwin)
304 (void)fprintf(out_file, "\tendwin();\n");
305 (void)fprintf(out_file, "\t%s\n}\n\n",
306 menus[i]->info->exitact.code);
307 }
308 j = 0;
309 toptn = menus[i]->info->optns;
310 for (; toptn != NULL; j++, toptn = toptn->next) {
311 if (strlen(toptn->optact.code) == 0)
312 continue;
313
314 (void)fprintf(out_file, "/*ARGSUSED*/\n"
315 "static int opt_act_%d_%d(menudesc *m, void *arg)\n"
316 "{\n\t%s\n\treturn %s;\n}\n\n",
317 i, j, toptn->optact.code,
318 (toptn->doexit ? "1" : "0"));
319 }
320
321 }
322
323 /* optentX */
324 for (i = 1; i < menu_no; i++) {
325 if (menus[i]->info->numopt > 53) {
326 (void)fprintf(stderr, "Menu %s has "
327 "too many options.\n",
328 menus[i]->info->title);
329 exit(1);
330 }
331 (void)fprintf(out_file, "static menu_ent optent%d[] = {\n", i);
332 name_is_code = 0;
333 for (j = 0, toptn = menus[i]->info->optns; toptn;
334 toptn = toptn->next, j++) {
335 name_is_code += toptn->name_is_code;
336 (void)fprintf(out_file, "\t{ .opt_name = %s, "
337 ".opt_menu=%d, .opt_flags=%d, .opt_action=",
338 toptn->name_is_code ? "0" : toptn->name,
339 toptn->menu,
340 (toptn->issub ? OPT_SUB : 0)
341 +(toptn->doexit ? OPT_EXIT : 0)
342 +(toptn->optact.endwin ? OPT_ENDWIN : 0));
343 if (strlen(toptn->optact.code))
344 (void)fprintf(out_file, "opt_act_%d_%d}", i, j);
345 else
346 (void)fprintf(out_file, "NULL}");
347 (void)fprintf(out_file, "%s\n",
348 (toptn->next ? "," : ""));
349 }
350 (void)fprintf(out_file, "\t};\n\n");
351
352 if (name_is_code) {
353 menus[i]->info->name_is_code = 1;
354 fprintf(out_file, "static void menu_%d_legend("
355 "menudesc *menu, int opt, void *arg)\n{\n"
356 "\tswitch (opt) {\n", i);
357 for (j = 0, toptn = menus[i]->info->optns; toptn;
358 toptn = toptn->next, j++) {
359 if (!toptn->name_is_code)
360 continue;
361 fprintf(out_file, "\tcase %d:\n\t\t{%s};\n"
362 "\t\tbreak;\n", j, toptn->name);
363 }
364 fprintf(out_file, "\t}\n}\n\n");
365 }
366 }
367
368
369 /* menus */
370 if (!do_dynamic)
371 (void)fprintf(out_file, "static int num_menus = %d;\n",
372 menu_no);
373
374 (void)fprintf(out_file, "static struct menudesc menu_def[] = {\n"
375 "\t{ },\n");
376 for (i = 1; i < menu_no; i++) {
377 (void)fprintf(out_file,
378 "\t{%s,", menus[i]->info->title);
379 if (do_expands)
380 (void)fprintf(out_file,
381 "NULL,");
382 (void)fprintf(out_file,
383 "%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,NULL,",
384 menus[i]->info->y,
385 menus[i]->info->x, menus[i]->info->h,
386 menus[i]->info->w, menus[i]->info->mopt,
387 menus[i]->info->numopt, i);
388 if (menus[i]->info->helpstr == NULL)
389 (void)fprintf(out_file, "NULL");
390 else {
391 tmpstr = menus[i]->info->helpstr;
392 if (*tmpstr != '"')
393 (void)fprintf(out_file, "%s", tmpstr);
394 else {
395 /* Skip an initial newline. */
396 if (tmpstr[1] == '\n')
397 *++tmpstr = '"';
398 (void)fprintf(out_file, "\n");
399 while (*tmpstr) {
400 if (*tmpstr != '\n') {
401 fputc(*tmpstr++, out_file);
402 continue;
403 }
404 (void)fprintf(out_file, "\\n\"\n\"");
405 tmpstr++;
406 }
407 }
408 }
409 (void)fprintf(out_file, ",");
410 if (menus[i]->info->mopt & MC_NOEXITOPT)
411 (void) fprintf (out_file, "NULL");
412 else if (menus[i]->info->exitstr != NULL)
413 (void)fprintf(out_file, "%s", menus[i]->info->exitstr);
414 else
415 (void)fprintf(out_file, "\"Exit\"");
416 if (do_expands) {
417 if (strlen(menus[i]->info->expact.code))
418 (void)fprintf(out_file, ",menu_%d_expact", i);
419 else
420 (void)fprintf(out_file, ",NULL");
421 }
422 if (strlen(menus[i]->info->postact.code))
423 (void)fprintf(out_file, ",menu_%d_postact", i);
424 else
425 (void)fprintf(out_file, ",NULL");
426 if (strlen(menus[i]->info->exitact.code))
427 (void)fprintf(out_file, ",menu_%d_exitact", i);
428 else
429 (void)fprintf(out_file, ",NULL");
430 if (menus[i]->info->name_is_code)
431 (void)fprintf(out_file, ",menu_%d_legend", i);
432 else
433 (void)fprintf(out_file, ",NULL");
434
435 (void)fprintf(out_file, "},\n");
436
437 }
438 (void)fprintf(out_file, "{NULL");
439 if (do_expands)
440 (void)fprintf(out_file, ", NULL");
441 (void)fprintf(out_file, ", 0, 0, 0, 0, 0, 0, 0, 0, "
442 "NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL");
443 if (do_expands)
444 (void)fprintf(out_file, ", NULL");
445 (void)fprintf(out_file, "}};\n\n");
446
447 /* __menu_initerror: initscr failed. */
448 (void)fprintf(out_file,
449 "/* __menu_initerror: initscr failed. */\n"
450 "void __menu_initerror (void) {\n");
451 if (error_act.code == NULL) {
452 (void)fprintf(out_file,
453 "\t(void) fprintf (stderr, "
454 "\"%%s: Could not initialize curses\\n\", "
455 "getprogname());\n"
456 "\texit(1);\n"
457 "}\n");
458 } else {
459 if (error_act.endwin)
460 (void)fprintf(out_file, "\tendwin();\n");
461 (void)fprintf(out_file, "%s\n}\n", error_act.code);
462 }
463
464 /* Copy menu_sys.def file. */
465 while ((ch = fgetc(sys_file)) != '\014') /* Control-L */
466 fputc(ch, out_file);
467
468 if (do_dynamic) {
469 while ((ch = fgetc(sys_file)) != '\n')
470 /* skip it */;
471 while ((ch = fgetc(sys_file)) != EOF)
472 fputc(ch, out_file);
473 }
474 fclose(out_file);
475 fclose(sys_file);
476 }
477