mdb.c revision 1.8 1 /* $NetBSD: mdb.c,v 1.8 1998/06/29 08:46:37 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 "typedef\n"
156 "struct menudesc {\n"
157 " char *title;\n"
158 " int y, x;\n"
159 " int h, w;\n"
160 " int mopt;\n"
161 " int numopts;\n"
162 " int cursel;\n"
163 " int topline;\n"
164 " char **opts;\n"
165 " WINDOW *mw;\n"
166 " char *helpstr;\n"
167 "} menudesc ;\n"
168 "\n"
169 "/* defines for mopt field. */\n"
170 "#define NOEXITOPT 1\n"
171 "#define NOBOX 2\n"
172 "#define SCROLL 4\n"
173 "\n"
174 "/* initilization flag */\n"
175 "extern int __m_endwin;\n"
176 "\n"
177 "/* Prototypes */\n"
178 "void process_menu (int num);\n"
179 "void __menu_initerror (void);\n"
180 "\n"
181 "/* Menu names */\n"
182 );
183 for (i=0; i<menu_no; i++) {
184 (void) fprintf (out_file, "#define MENU_%s\t%d\n",
185 menus[i]->id, i);
186 }
187 (void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
188 (void) fprintf (out_file, "#endif\n");
189
190 fclose (out_file);
191
192 /* Now the C file */
193 out_file = fopen (cname, "w");
194 if (out_file == NULL) {
195 (void) fprintf (stderr, "%s: could not open %s.\n",
196 prog_name, cname);
197 exit (1);
198 }
199
200 /* initial code */
201 fprintf (out_file, "#include \"%s\"\n\n", hname);
202 fprintf (out_file, "%s\n\n", initcode);
203
204 /* data definitions */
205
206 /* optstrX */
207 for (i=0; i<menu_no; i++) {
208 (void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
209 toptn = menus[i]->info->optns;
210 opt_ch = 'a';
211 while (toptn != NULL) {
212 if (opt_ch > 'Z' && opt_ch < 'a') {
213 (void) fprintf (stderr, "Menu %s has "
214 "too many options.\n",
215 menus[i]->info->title);
216 exit (1);
217 }
218 (void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch,
219 toptn->name+1);
220 toptn = toptn->next;
221 if (opt_ch == 'z')
222 opt_ch = 'A';
223 else if (opt_ch == 'w')
224 opt_ch = 'y';
225 else
226 opt_ch++;
227 }
228 (void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
229 }
230
231 /* menus */
232 (void) fprintf (out_file, "static struct menudesc menus[] = {\n");
233 for (i=0; i<menu_no; i++) {
234 (void) fprintf (out_file,
235 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optstr%d,NULL,",
236 menus[i]->info->title, menus[i]->info->y,
237 menus[i]->info->x, menus[i]->info->h,
238 menus[i]->info->w, menus[i]->info->mopt,
239 menus[i]->info->numopt, i);
240 if (menus[i]->info->helpstr == NULL)
241 (void) fprintf (out_file, "NULL},\n");
242 else {
243 tmpstr = menus[i]->info->helpstr;
244 /* Skip an initial newline. */
245 if (*tmpstr == '\n')
246 tmpstr++;
247 (void) fprintf (out_file, "\n\"");
248 while (*tmpstr)
249 if (*tmpstr != '\n')
250 fputc (*tmpstr++, out_file);
251 else {
252 (void) fprintf (out_file, "\\n\\\n");
253 tmpstr++;
254 }
255 (void) fprintf (out_file, "\"},\n");
256 }
257 }
258 (void) fprintf (out_file, "{NULL}};\n\n");
259
260 /* num_menus */
261 (void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
262
263 /* action code */
264 (void) fprintf (out_file, "%s",
265 "static int process_item (int *menu_no, int sel)\n"
266 "{\n"
267 "\tint retval = FALSE;\n"
268 "\n"
269 "\tswitch (*menu_no) {\n"
270 );
271 for (i=0; i<menu_no; i++) {
272 (void) fprintf (out_file, "\tcase MENU_%s:\n",
273 menus[i]->id);
274 (void) fprintf (out_file, "\t\tswitch (sel) {\n"
275 "\t\tcase -2:\n");
276 if (menus[i]->info->postact.endwin)
277 (void) fprintf (out_file, "\t\t\tendwin();\n"
278 "\t\t__m_endwin = 1;\n");
279 if (strlen(menus[i]->info->postact.code))
280 (void) fprintf (out_file, "\t\t\t{%s}\n",
281 menus[i]->info->postact.code);
282 (void) fprintf (out_file, "\t\t\tbreak;\n");
283 (void) fprintf (out_file, "\t\tcase -1:\n");
284 if (menus[i]->info->exitact.endwin)
285 (void) fprintf (out_file, "\t\t\tendwin();\n"
286 "\t\t__m_endwin = 1;\n");
287 if (strlen(menus[i]->info->exitact.code))
288 (void) fprintf (out_file, "\t\t\t{%s}\n",
289 menus[i]->info->exitact.code);
290 (void) fprintf (out_file, "\t\t\tbreak;\n");
291 j = 0;
292 toptn = menus[i]->info->optns;
293 while (toptn != NULL) {
294 (void) fprintf (out_file, "\t\tcase %d:\n", j++);
295 if (toptn->optact.endwin)
296 (void) fprintf (out_file, "\t\t\tendwin();\n"
297 "\t\t__m_endwin = 1;\n");
298 if (strlen(toptn->optact.code))
299 (void) fprintf (out_file, "\t\t\t{%s}\n",
300 toptn->optact.code);
301 if (toptn->menu >= 0)
302 if (toptn->issub)
303 (void) fprintf (out_file,
304 "\t\t\tprocess_menu(%d);\n",
305 toptn->menu);
306 else
307 (void) fprintf (out_file,
308 "\t\t\t*menu_no = %d;\n",
309 toptn->menu);
310 if (toptn->doexit)
311 (void) fprintf (out_file,
312 "\t\t\tretval = TRUE;\n");
313 (void) fprintf (out_file, "\t\t\tbreak;\n");
314 toptn = toptn->next;
315 }
316
317 (void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
318 }
319 (void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
320
321 while ((ch = fgetc(sys_file)) != EOF)
322 fputc(ch, out_file);
323
324 /* __menu_initerror: initscr failed. */
325 (void) fprintf (out_file,
326 "/* __menu_initerror: initscr failed. */\n"
327 "void __menu_initerror (void) {\n");
328 if (error_act.code == NULL) {
329 (void) fprintf (out_file,
330 "\t(void) fprintf (stderr, "
331 "\"Could not initialize curses\\n\");\n"
332 "\texit(1);\n"
333 "}\n");
334 } else {
335 if (error_act.endwin)
336 (void) fprintf (out_file, "\tendwin();\n");
337 (void) fprintf (out_file, "%s\n}\n", error_act.code);
338 }
339
340 fclose (out_file);
341 fclose (sys_file);
342 }
343