1 /* $NetBSD: mdb.c,v 1.46 2012/03/06 16:55:18 mbalmer Exp $ */
4 * Copyright 1997 Piermont Information Systems Inc.
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
35 /* mdb.c - menu database manipulation */
37 #if HAVE_NBTOOL_CONFIG_H
38 #include "nbtool_config.h"
41 #include <sys/cdefs.h>
43 #if defined(__RCSID) && !defined(lint)
44 __RCSID("$NetBSD: mdb.c,v 1.46 2012/03/06 16:55:18 mbalmer Exp $");
53 #include "pathnames.h"
58 static int menu_no
= 0;
59 static id_rec
*menus
[MAX
];
67 /* get_menu returns a pointer to a newly created id_rec or an old one. */
74 temp
= find_id (root
, name
);
78 temp
= (id_rec
*)malloc(sizeof(id_rec
));
79 temp
->id
= strdup(name
);
81 temp
->menu_no
= menu_no
;
82 menus
[menu_no
++] = temp
;
83 insert_id(&root
, temp
);
85 (void)fprintf(stderr
, "Too many menus. "
95 /* Verify that all menus are defined. */
101 for (i
= 0; i
< menu_no
; i
++)
103 yyerror ("Menu '%s' undefined.", menus
[i
]->id
);
107 /* Write out the menu file. */
109 write_menu_file(char *initcode
)
111 FILE *out_file
, *sys_file
;
113 char hname
[1024], cname
[1024], sname
[1024];
114 char *sys_prefix
, *tmpstr
;
115 int name_is_code
, nlen
, ch
;
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
);
126 snprintf(cname
, 1024, "%s.c", out_name
);
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",
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",
149 (void)fprintf(out_file
, "%s",
150 "/* menu system definitions. */\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"
161 (void)fprintf(out_file
, "#define MSG_XLAT(x) msg_string(x)\n");
163 (void)fprintf(out_file
, "#define MSG_XLAT(x) (x)\n");
165 (void)fprintf(out_file
, "#define DYNAMIC_MENUS\n");
166 if (do_dynamic
|| do_msgxlat
)
167 (void)fprintf(out_file
, "\n");
169 (void)fprintf(out_file
,
170 "typedef struct menudesc menudesc;\n"
171 "typedef struct menu_ent menu_ent;\n"
172 "struct menu_ent {\n"
173 " const char *opt_name;\n"
176 " int (*opt_action)(menudesc *, void *);\n"
178 "#define OPT_SUB 1\n"
179 "#define OPT_ENDWIN 2\n"
180 "#define OPT_EXIT 4\n"
181 "#define OPT_IGNORE 8\n"
182 "#define OPT_NOMENU -1\n\n"
183 "struct menudesc {\n"
184 " const char *title;\n"
194 " const char *helpstr;\n"
195 " const char *exitstr;\n"
196 " void (*post_act)(menudesc *, void *);\n"
197 " void (*exit_act)(menudesc *, void *);\n"
198 " void (*draw_line)(menudesc *, int, void *);\n"
201 "/* defines for mopt field. */\n"
203 #define MC_OPT(x) "#define " #x " " STR(x) "\n"
207 MC_OPT(MC_NOSHORTCUT
)
210 MC_OPT(MC_ALWAYS_SCROLL
)
217 (void)fprintf(out_file
, "%s",
220 "int menu_init(void);\n"
221 "void process_menu(int, void *);\n"
222 "__dead void __menu_initerror(void);\n"
226 (void)fprintf(out_file
, "%s",
227 "int new_menu(const char *, menu_ent *, int, \n"
228 "\tint, int, int, int, int,\n"
229 "\tvoid (*)(menudesc *, void *), "
230 "void (*)(menudesc *, int, void *),\n"
231 "\tvoid (*)(menudesc *, void *), "
232 "const char *, const char *);\n"
233 "void free_menu(int);\n"
234 "void set_menu_numopts(int, int);\n"
237 (void)fprintf(out_file
, "\n/* Menu names */\n");
238 for (i
= 0; i
< menu_no
; i
++)
239 (void)fprintf(out_file
, "#define MENU_%s\t%d\n",
243 (void)fprintf(out_file
, "\n#define DYN_MENU_START\t%d",
246 (void)fprintf (out_file
, "\n#define MAX_STRLEN %d\n"
247 "#endif\n", max_strlen
);
251 out_file
= fopen(cname
, "w");
252 if (out_file
== NULL
) {
253 (void)fprintf(stderr
, "%s: could not open %s.\n",
259 fprintf(out_file
, "#include \"%s\"\n\n", hname
);
260 fprintf(out_file
, "%s\n\n", initcode
);
262 /* data definitions */
265 for (i
= 0; i
< menu_no
; i
++) {
266 if (strlen(menus
[i
]->info
->postact
.code
)) {
267 (void)fprintf(out_file
, "/*ARGSUSED*/\n"
268 "static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i
);
269 if (menus
[i
]->info
->postact
.endwin
)
270 (void)fprintf(out_file
, "\tendwin();\n");
271 (void)fprintf(out_file
, "\t%s\n}\n\n",
272 menus
[i
]->info
->postact
.code
);
274 if (strlen(menus
[i
]->info
->exitact
.code
)) {
275 (void)fprintf(out_file
, "/*ARGSUSED*/\n"
276 "static void menu_%d_exitact(menudesc *menu, void *arg)\n{\n", i
);
277 if (menus
[i
]->info
->exitact
.endwin
)
278 (void)fprintf(out_file
, "\tendwin();\n");
279 (void)fprintf(out_file
, "\t%s\n}\n\n",
280 menus
[i
]->info
->exitact
.code
);
283 toptn
= menus
[i
]->info
->optns
;
284 for (; toptn
!= NULL
; j
++, toptn
= toptn
->next
) {
285 if (strlen(toptn
->optact
.code
) == 0)
288 (void)fprintf(out_file
, "/*ARGSUSED*/\n"
289 "static int opt_act_%d_%d(menudesc *m, void *arg)\n"
290 "{\n\t%s\n\treturn %s;\n}\n\n",
291 i
, j
, toptn
->optact
.code
,
292 (toptn
->doexit
? "1" : "0"));
298 for (i
= 0; i
< menu_no
; i
++) {
299 if (menus
[i
]->info
->numopt
> 53) {
300 (void)fprintf(stderr
, "Menu %s has "
301 "too many options.\n",
302 menus
[i
]->info
->title
);
305 (void)fprintf(out_file
, "static menu_ent optent%d[] = {\n", i
);
307 for (j
= 0, toptn
= menus
[i
]->info
->optns
; toptn
;
308 toptn
= toptn
->next
, j
++) {
309 name_is_code
+= toptn
->name_is_code
;
310 (void)fprintf(out_file
, "\t{%s,%d,%d,",
311 toptn
->name_is_code
? "0" : toptn
->name
,
313 (toptn
->issub
? OPT_SUB
: 0)
314 +(toptn
->doexit
? OPT_EXIT
: 0)
315 +(toptn
->optact
.endwin
? OPT_ENDWIN
: 0));
316 if (strlen(toptn
->optact
.code
))
317 (void)fprintf(out_file
, "opt_act_%d_%d}", i
, j
);
319 (void)fprintf(out_file
, "NULL}");
320 (void)fprintf(out_file
, "%s\n",
321 (toptn
->next
? "," : ""));
323 (void)fprintf(out_file
, "\t};\n\n");
326 menus
[i
]->info
->name_is_code
= 1;
327 fprintf(out_file
, "static void menu_%d_legend("
328 "menudesc *menu, int opt, void *arg)\n{\n"
329 "\tswitch (opt) {\n", i
);
330 for (j
= 0, toptn
= menus
[i
]->info
->optns
; toptn
;
331 toptn
= toptn
->next
, j
++) {
332 if (!toptn
->name_is_code
)
334 fprintf(out_file
, "\tcase %d:\n\t\t{%s};\n"
335 "\t\tbreak;\n", j
, toptn
->name
);
337 fprintf(out_file
, "\t}\n}\n\n");
344 (void)fprintf(out_file
, "static int num_menus = %d;\n",
347 (void)fprintf(out_file
, "static struct menudesc menu_def[] = {\n");
348 for (i
= 0; i
< menu_no
; i
++) {
349 (void)fprintf(out_file
,
350 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,NULL,",
351 menus
[i
]->info
->title
, menus
[i
]->info
->y
,
352 menus
[i
]->info
->x
, menus
[i
]->info
->h
,
353 menus
[i
]->info
->w
, menus
[i
]->info
->mopt
,
354 menus
[i
]->info
->numopt
, i
);
355 if (menus
[i
]->info
->helpstr
== NULL
)
356 (void)fprintf(out_file
, "NULL");
358 tmpstr
= menus
[i
]->info
->helpstr
;
360 (void)fprintf(out_file
, "%s", tmpstr
);
362 /* Skip an initial newline. */
363 if (tmpstr
[1] == '\n')
365 (void)fprintf(out_file
, "\n");
367 if (*tmpstr
!= '\n') {
368 fputc(*tmpstr
++, out_file
);
371 (void)fprintf(out_file
, "\\n\"\n\"");
376 (void)fprintf(out_file
, ",");
377 if (menus
[i
]->info
->mopt
& MC_NOEXITOPT
)
378 (void) fprintf (out_file
, "NULL");
379 else if (menus
[i
]->info
->exitstr
!= NULL
)
380 (void)fprintf(out_file
, "%s", menus
[i
]->info
->exitstr
);
382 (void)fprintf(out_file
, "\"Exit\"");
383 if (strlen(menus
[i
]->info
->postact
.code
))
384 (void)fprintf(out_file
, ",menu_%d_postact", i
);
386 (void)fprintf(out_file
, ",NULL");
387 if (strlen(menus
[i
]->info
->exitact
.code
))
388 (void)fprintf(out_file
, ",menu_%d_exitact", i
);
390 (void)fprintf(out_file
, ",NULL");
391 if (menus
[i
]->info
->name_is_code
)
392 (void)fprintf(out_file
, ",menu_%d_legend", i
);
394 (void)fprintf(out_file
, ",NULL");
396 (void)fprintf(out_file
, "},\n");
399 (void)fprintf(out_file
, "{NULL, 0, 0, 0, 0, 0, 0, 0, 0, "
400 "NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}};\n\n");
402 /* __menu_initerror: initscr failed. */
403 (void)fprintf(out_file
,
404 "/* __menu_initerror: initscr failed. */\n"
405 "void __menu_initerror (void) {\n");
406 if (error_act
.code
== NULL
) {
407 (void)fprintf(out_file
,
408 "\t(void) fprintf (stderr, "
409 "\"%%s: Could not initialize curses\\n\", "
414 if (error_act
.endwin
)
415 (void)fprintf(out_file
, "\tendwin();\n");
416 (void)fprintf(out_file
, "%s\n}\n", error_act
.code
);
419 /* Copy menu_sys.def file. */
420 while ((ch
= fgetc(sys_file
)) != '\014') /* Control-L */
424 while ((ch
= fgetc(sys_file
)) != '\n')
426 while ((ch
= fgetc(sys_file
)) != EOF
)