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