mdb.c revision 1.9 1 /* $NetBSD: mdb.c,v 1.9 1998/07/01 07:46:02 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 500
49 static int menu_no = 0;
50 static id_rec *menus[MAX];
51
52
53 /* get_menu returns a pointer to a newly created id_rec or an old one. */
54
55 id_rec *
56 get_menu (char *name)
57 {
58 id_rec *temp;
59
60 temp = find_id (root, name);
61
62 if (temp == NULL) {
63 if (menu_no < MAX) {
64 temp = (id_rec *) malloc (sizeof(id_rec));
65 temp->id = strdup(name);
66 temp->info = NULL;
67 temp->menu_no = menu_no;
68 menus[menu_no++] = temp;
69 insert_id (&root, temp);
70 } else {
71 (void) fprintf (stderr, "Too many menus. "
72 "Increase MAX.\n");
73 exit(1);
74 }
75 }
76
77 return temp;
78 }
79
80
81 /* Verify that all menus are defined. */
82
83 void
84 check_defined (void)
85 {
86 int i;
87
88 for (i=0; i<menu_no; i++)
89 if (!menus[i]->info)
90 yyerror ("Menu '%s' undefined.", menus[i]->id);
91 }
92
93
94 /* Write out the menu file. */
95 void
96 write_menu_file (char *initcode)
97 {
98 FILE *out_file;
99 FILE *sys_file;
100 int i, j;
101 char hname[1024];
102 char cname[1024];
103 char sname[1024];
104 char *sys_prefix;
105 char *tmpstr;
106
107 int nlen;
108
109 char opt_ch;
110 int ch;
111
112 optn_info *toptn;
113
114 /* Generate file names */
115 snprintf (hname, 1024, "%s.h", out_name);
116 nlen = strlen(hname);
117 if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
118 (void) fprintf (stderr, "%s: name `%s` too long.\n",
119 prog_name, out_name);
120 exit(1);
121 }
122 snprintf (cname, 1024, "%s.c", out_name);
123
124 /* Open the menu_sys file first. */
125 sys_prefix = getenv ("MENUDEF");
126 if (sys_prefix == NULL)
127 sys_prefix = "/usr/share/misc";
128 snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
129 sys_file = fopen (sname, "r");
130 if (sys_file == NULL) {
131 (void) fprintf (stderr, "%s: could not open %s.\n",
132 prog_name, sname);
133 exit (1);
134 }
135
136 /* Output the .h file first. */
137 out_file = fopen (hname, "w");
138 if (out_file == NULL) {
139 (void) fprintf (stderr, "%s: could not open %s.\n",
140 prog_name, hname);
141 exit (1);
142 }
143
144 /* Write it */
145 (void) fprintf (out_file, "%s",
146 "/* menu system definitions. */\n"
147 "\n"
148 "#ifndef MENU_DEFS_H\n"
149 "#define MENU_DEFS_H\n"
150 "#include <stdlib.h>\n"
151 "#include <string.h>\n"
152 "#include <ctype.h>\n"
153 "#include <curses.h>\n"
154 "\n");
155
156 if (do_dynamic)
157 (void) fprintf (out_file,
158 "\n#define DYNAMIC_MENUS\n\n"
159 "typedef\n"
160 "struct dyn_menu_ent {\n"
161 " char *optname;\n"
162 " int nextmenu;\n"
163 " int submenu;\n"
164 " void (*action)(void);\n"
165 "} dyn_menu_ent ;\n");
166
167 (void) fprintf (out_file, "%s",
168 "typedef\n"
169 "struct menudesc {\n"
170 " char *title;\n"
171 " int y, x;\n"
172 " int h, w;\n"
173 " int mopt;\n"
174 " int numopts;\n"
175 " int cursel;\n"
176 " int topline;\n"
177 " char **opts;\n"
178 " WINDOW *mw;\n"
179 " char *helpstr;\n");
180
181 if (do_dynamic)
182 (void) fprintf (out_file,
183 " dyn_menu_ent *dyn_opts;\n"
184 " void (*post_act)(void);\n"
185 " void (*exit_act)(void);\n");
186
187 (void) fprintf (out_file, "%s",
188 "} menudesc ;\n"
189 "\n"
190 "/* defines for mopt field. */\n"
191 "#define NOEXITOPT 1\n"
192 "#define NOBOX 2\n"
193 "#define SCROLL 4\n");
194
195 if (do_dynamic)
196 (void) fprintf (out_file,
197 "#define DYNAMIC 8\n");
198
199 (void) fprintf (out_file, "%s",
200 "\n"
201 "/* initilization flag */\n"
202 "extern int __m_endwin;\n"
203 "\n"
204 "/* Prototypes */\n"
205 "void process_menu (int num);\n"
206 "void __menu_initerror (void);\n"
207 "\n"
208 "/* Menu names */\n"
209 );
210 for (i=0; i<menu_no; i++) {
211 (void) fprintf (out_file, "#define MENU_%s\t%d\n",
212 menus[i]->id, i);
213 }
214 (void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
215
216 (void) fprintf (out_file, "#endif\n");
217 fclose (out_file);
218
219 /* Now the C file */
220 out_file = fopen (cname, "w");
221 if (out_file == NULL) {
222 (void) fprintf (stderr, "%s: could not open %s.\n",
223 prog_name, cname);
224 exit (1);
225 }
226
227 /* initial code */
228 fprintf (out_file, "#include \"%s\"\n\n", hname);
229 fprintf (out_file, "%s\n\n", initcode);
230
231 /* data definitions */
232
233 /* optstrX */
234 for (i=0; i<menu_no; i++) {
235 (void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
236 toptn = menus[i]->info->optns;
237 opt_ch = 'a';
238 while (toptn != NULL) {
239 if (opt_ch > 'Z' && opt_ch < 'a') {
240 (void) fprintf (stderr, "Menu %s has "
241 "too many options.\n",
242 menus[i]->info->title);
243 exit (1);
244 }
245 (void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch,
246 toptn->name+1);
247 toptn = toptn->next;
248 if (opt_ch == 'z')
249 opt_ch = 'A';
250 else if (opt_ch == 'w')
251 opt_ch = 'y';
252 else
253 opt_ch++;
254 }
255 (void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
256 }
257
258 /* menus */
259 (void) fprintf (out_file, "static struct menudesc menus[] = {\n");
260 for (i=0; i<menu_no; i++) {
261 (void) fprintf (out_file,
262 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optstr%d,NULL,",
263 menus[i]->info->title, menus[i]->info->y,
264 menus[i]->info->x, menus[i]->info->h,
265 menus[i]->info->w, menus[i]->info->mopt,
266 menus[i]->info->numopt, i);
267 if (menus[i]->info->helpstr == NULL)
268 (void) fprintf (out_file, "NULL");
269 else {
270 tmpstr = menus[i]->info->helpstr;
271 /* Skip an initial newline. */
272 if (*tmpstr == '\n')
273 tmpstr++;
274 (void) fprintf (out_file, "\n\"");
275 while (*tmpstr)
276 if (*tmpstr != '\n')
277 fputc (*tmpstr++, out_file);
278 else {
279 (void) fprintf (out_file, "\\n\\\n");
280 tmpstr++;
281 }
282 (void) fprintf (out_file, "\"");
283 }
284 if (do_dynamic)
285 (void) fprintf (out_file, ",NULL,NULL,NULL},\n");
286 else
287 (void) fprintf (out_file, "},\n");
288
289 }
290 (void) fprintf (out_file, "{NULL}};\n\n");
291
292 /* num_menus */
293 (void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
294
295 /* action code */
296 (void) fprintf (out_file, "%s",
297 "static int process_item (int *menu_no, int sel)\n"
298 "{\n"
299 "\tint retval = FALSE;\n"
300 "\n"
301 "\tswitch (*menu_no) {\n"
302 );
303 for (i=0; i<menu_no; i++) {
304 (void) fprintf (out_file, "\tcase MENU_%s:\n",
305 menus[i]->id);
306 (void) fprintf (out_file, "\t\tswitch (sel) {\n"
307 "\t\tcase -2:\n");
308 if (menus[i]->info->postact.endwin)
309 (void) fprintf (out_file, "\t\t\tendwin();\n"
310 "\t\t__m_endwin = 1;\n");
311 if (strlen(menus[i]->info->postact.code))
312 (void) fprintf (out_file, "\t\t\t{%s}\n",
313 menus[i]->info->postact.code);
314 (void) fprintf (out_file, "\t\t\tbreak;\n");
315 (void) fprintf (out_file, "\t\tcase -1:\n");
316 if (menus[i]->info->exitact.endwin)
317 (void) fprintf (out_file, "\t\t\tendwin();\n"
318 "\t\t__m_endwin = 1;\n");
319 if (strlen(menus[i]->info->exitact.code))
320 (void) fprintf (out_file, "\t\t\t{%s}\n",
321 menus[i]->info->exitact.code);
322 (void) fprintf (out_file, "\t\t\tbreak;\n");
323 j = 0;
324 toptn = menus[i]->info->optns;
325 while (toptn != NULL) {
326 (void) fprintf (out_file, "\t\tcase %d:\n", j++);
327 if (toptn->optact.endwin)
328 (void) fprintf (out_file, "\t\t\tendwin();\n"
329 "\t\t__m_endwin = 1;\n");
330 if (strlen(toptn->optact.code))
331 (void) fprintf (out_file, "\t\t\t{%s}\n",
332 toptn->optact.code);
333 if (toptn->menu >= 0)
334 if (toptn->issub)
335 (void) fprintf (out_file,
336 "\t\t\tprocess_menu(%d);\n",
337 toptn->menu);
338 else
339 (void) fprintf (out_file,
340 "\t\t\t*menu_no = %d;\n",
341 toptn->menu);
342 if (toptn->doexit)
343 (void) fprintf (out_file,
344 "\t\t\tretval = TRUE;\n");
345 (void) fprintf (out_file, "\t\t\tbreak;\n");
346 toptn = toptn->next;
347 }
348
349 (void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
350 }
351 (void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
352
353 while ((ch = fgetc(sys_file)) != EOF)
354 fputc(ch, out_file);
355
356 /* __menu_initerror: initscr failed. */
357 (void) fprintf (out_file,
358 "/* __menu_initerror: initscr failed. */\n"
359 "void __menu_initerror (void) {\n");
360 if (error_act.code == NULL) {
361 (void) fprintf (out_file,
362 "\t(void) fprintf (stderr, "
363 "\"Could not initialize curses\\n\");\n"
364 "\texit(1);\n"
365 "}\n");
366 } else {
367 if (error_act.endwin)
368 (void) fprintf (out_file, "\tendwin();\n");
369 (void) fprintf (out_file, "%s\n}\n", error_act.code);
370 }
371
372 fclose (out_file);
373 fclose (sys_file);
374 }
375