mdb.c revision 1.11 1 /* $NetBSD: mdb.c,v 1.11 1998/07/16 07:08:26 phil 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 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "mdb.h"
45 #include "defs.h"
46
47 /* Data */
48 #define MAX 1000
49 static int menu_no = 0;
50 static id_rec *menus[MAX];
51
52 /* Other defines */
53 #define OPT_SUB 1
54 #define OPT_ENDWIN 2
55 #define OPT_EXIT 4
56
57
58 /* get_menu returns a pointer to a newly created id_rec or an old one. */
59
60 id_rec *
61 get_menu (char *name)
62 {
63 id_rec *temp;
64
65 temp = find_id (root, name);
66
67 if (temp == NULL) {
68 if (menu_no < MAX) {
69 temp = (id_rec *) malloc (sizeof(id_rec));
70 temp->id = strdup(name);
71 temp->info = NULL;
72 temp->menu_no = menu_no;
73 menus[menu_no++] = temp;
74 insert_id (&root, temp);
75 } else {
76 (void) fprintf (stderr, "Too many menus. "
77 "Increase MAX.\n");
78 exit(1);
79 }
80 }
81
82 return temp;
83 }
84
85
86 /* Verify that all menus are defined. */
87
88 void
89 check_defined (void)
90 {
91 int i;
92
93 for (i=0; i<menu_no; i++)
94 if (!menus[i]->info)
95 yyerror ("Menu '%s' undefined.", menus[i]->id);
96 }
97
98
99 /* Write out the menu file. */
100 void
101 write_menu_file (char *initcode)
102 {
103 FILE *out_file;
104 FILE *sys_file;
105 int i, j;
106 char hname[1024];
107 char cname[1024];
108 char sname[1024];
109 char *sys_prefix;
110 char *tmpstr;
111
112 int nlen;
113
114 int ch;
115
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 = "/usr/share/misc";
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_dynamic)
161 (void) fprintf (out_file, "#define DYNAMIC_MENUS\n\n");
162
163 (void) fprintf (out_file,
164 "typedef\n"
165 "struct menu_ent {\n"
166 " char *opt_name;\n"
167 " int opt_menu;\n"
168 " int opt_flags;\n"
169 " void (*opt_action)(void);\n"
170 "} menu_ent ;\n\n"
171 "#define OPT_SUB 1\n"
172 "#define OPT_ENDWIN 2\n"
173 "#define OPT_EXIT 4\n"
174 "#define OPT_NOMENU -1\n\n"
175 "typedef\n"
176 "struct menudesc {\n"
177 " char *title;\n"
178 " int y, x;\n"
179 " int h, w;\n"
180 " int mopt;\n"
181 " int numopts;\n"
182 " int cursel;\n"
183 " int topline;\n"
184 " menu_ent *opts;\n"
185 " WINDOW *mw;\n"
186 " char *helpstr;\n"
187 " void (*post_act)(void);\n"
188 " void (*exit_act)(void);\n"
189 "} menudesc ;\n"
190 "\n"
191 "/* defines for mopt field. */\n"
192 "#define MC_NOEXITOPT 1\n"
193 "#define MC_NOBOX 2\n"
194 "#define MC_SCROLL 4\n"
195 );
196
197 if (do_dynamic)
198 (void) fprintf (out_file, "#define MC_VALID 8\n");
199
200 (void) fprintf (out_file, "%s",
201 "\n"
202 "/* initilization flag */\n"
203 "extern int __m_endwin;\n"
204 "\n"
205 "/* Prototypes */\n"
206 "void process_menu (int num);\n"
207 "void __menu_initerror (void);\n"
208 );
209
210 if (do_dynamic)
211 (void) fprintf (out_file, "%s",
212 "int new_menu (char * title, menu_ent * opts, "
213 "int numopts, \n"
214 "\tint x, int y, int h, int w, int mopt,\n"
215 "\tvoid (*post_act)(void), void (*exit_act), "
216 "char * help);\n"
217 "void free_menu (int menu_no);\n"
218 );
219
220 (void) fprintf (out_file, "\n/* Menu names */\n");
221 for (i=0; i<menu_no; i++) {
222 (void) fprintf (out_file, "#define MENU_%s\t%d\n",
223 menus[i]->id, i);
224 }
225
226 if (do_dynamic)
227 (void) fprintf (out_file, "\n#define DYN_MENU_START\t%d",
228 menu_no);
229
230 (void) fprintf (out_file, "\n#define MAX_STRLEN %d\n"
231 "#endif\n", max_strlen);
232 fclose (out_file);
233
234 /* Now the C file */
235 out_file = fopen (cname, "w");
236 if (out_file == NULL) {
237 (void) fprintf (stderr, "%s: could not open %s.\n",
238 prog_name, cname);
239 exit (1);
240 }
241
242 /* initial code */
243 fprintf (out_file, "#include \"%s\"\n\n", hname);
244 fprintf (out_file, "%s\n\n", initcode);
245
246 /* data definitions */
247
248 /* func defs */
249 for (i=0; i<menu_no; i++) {
250 if (strlen(menus[i]->info->postact.code)) {
251 (void) fprintf (out_file,
252 "void menu_%d_postact(void);\n"
253 "void menu_%d_postact(void)\n{", i, i);
254 if (menus[i]->info->postact.endwin)
255 (void) fprintf (out_file, "\tendwin();\n"
256 "\t__m_endwin = 1;\n");
257 (void) fprintf (out_file,
258 "\t%s\n}\n",
259 menus[i]->info->postact.code);
260 }
261 if (strlen(menus[i]->info->exitact.code)) {
262 (void) fprintf (out_file,
263 "void menu_%d_exitact(void);\n"
264 "void menu_%d_exitact(void)\n{", i, i);
265 if (menus[i]->info->exitact.endwin)
266 (void) fprintf (out_file, "\tendwin();\n"
267 "\t__m_endwin = 1;\n");
268 (void) fprintf (out_file, "\t%s\n}\n\n",
269 menus[i]->info->exitact.code);
270 }
271 j = 0;
272 toptn = menus[i]->info->optns;
273 while (toptn != NULL) {
274 if (strlen(toptn->optact.code)) {
275 (void) fprintf (out_file,
276 "void opt_act_%d_%d(void);\n"
277 "void opt_act_%d_%d(void)\n"
278 "{\t%s\n}\n\n",
279 i, j, i, j, toptn->optact.code);
280 }
281 j++;
282 toptn = toptn->next;
283 }
284
285 }
286
287 /* optentX */
288 for (i=0; i<menu_no; i++) {
289 if (menus[i]->info->numopt > 53) {
290 (void) fprintf (stderr, "Menu %s has "
291 "too many options.\n",
292 menus[i]->info->title);
293 exit (1);
294 }
295 toptn = menus[i]->info->optns;
296 j = 0;
297 (void) fprintf (out_file,
298 "static menu_ent optent%d[] = {\n", i);
299 while (toptn != NULL) {
300 (void) fprintf (out_file, "\t{\"%s,%d,%d,",
301 toptn->name+1, toptn->menu,
302 (toptn->issub ? OPT_SUB : 0)
303 +(toptn->doexit ? OPT_EXIT : 0)
304 +(toptn->optact.endwin ? OPT_ENDWIN : 0));
305 if (strlen(toptn->optact.code))
306 (void) fprintf (out_file, "opt_act_%d_%d}",
307 i, j);
308 else
309 (void) fprintf (out_file, "NULL}");
310 (void) fprintf (out_file, "%s\n",
311 (toptn->next ? "," : ""));
312 j++;
313 toptn = toptn->next;
314 }
315 (void) fprintf (out_file, "\t};\n\n");
316
317 }
318
319
320 /* menus */
321 (void) fprintf (out_file, "static struct menudesc menu_def[] = {\n");
322 for (i=0; i<menu_no; i++) {
323 (void) fprintf (out_file,
324 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,",
325 menus[i]->info->title, menus[i]->info->y,
326 menus[i]->info->x, menus[i]->info->h,
327 menus[i]->info->w, menus[i]->info->mopt,
328 menus[i]->info->numopt, i);
329 if (menus[i]->info->helpstr == NULL)
330 (void) fprintf (out_file, "NULL");
331 else {
332 tmpstr = menus[i]->info->helpstr;
333 /* Skip an initial newline. */
334 if (*tmpstr == '\n')
335 tmpstr++;
336 (void) fprintf (out_file, "\n\"");
337 while (*tmpstr)
338 if (*tmpstr != '\n')
339 fputc (*tmpstr++, out_file);
340 else {
341 (void) fprintf (out_file, "\\n\\\n");
342 tmpstr++;
343 }
344 (void) fprintf (out_file, "\"");
345 }
346 if (strlen(menus[i]->info->postact.code))
347 (void) fprintf (out_file, ",menu_%d_postact", i);
348 else
349 (void) fprintf (out_file, ",NULL");
350 if (strlen(menus[i]->info->exitact.code))
351 (void) fprintf (out_file, ",menu_%d_exitact", i);
352 else
353 (void) fprintf (out_file, ",NULL");
354
355 (void) fprintf (out_file, "},\n");
356
357 }
358 (void) fprintf (out_file, "{NULL}};\n\n");
359
360 /* __menu_initerror: initscr failed. */
361 (void) fprintf (out_file,
362 "/* __menu_initerror: initscr failed. */\n"
363 "void __menu_initerror (void) {\n");
364 if (error_act.code == NULL) {
365 (void) fprintf (out_file,
366 "\t(void) fprintf (stderr, "
367 "\"Could not initialize curses\\n\");\n"
368 "\texit(1);\n"
369 "}\n");
370 } else {
371 if (error_act.endwin)
372 (void) fprintf (out_file, "\tendwin();\n");
373 (void) fprintf (out_file, "%s\n}\n", error_act.code);
374 }
375
376 /* Copy menu_sys.def file. */
377 while ((ch = fgetc(sys_file)) != '\014') /* Control-L */
378 fputc(ch, out_file);
379
380 if (do_dynamic) {
381 while ((ch = fgetc(sys_file)) != '\n')
382 /* skip it */;
383 while ((ch = fgetc(sys_file)) != EOF)
384 fputc(ch, out_file);
385 }
386
387 fclose (out_file);
388 fclose (sys_file);
389 }
390