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