mdb.c revision 1.6 1 /* $NetBSD: mdb.c,v 1.6 1998/06/25 09:58:57 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 (void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch++,
213 toptn->name+1);
214 toptn = toptn->next;
215 }
216 (void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
217 }
218
219 /* menus */
220 (void) fprintf (out_file, "static struct menudesc menus[] = {\n");
221 for (i=0; i<menu_no; i++) {
222 (void) fprintf (out_file,
223 "\t{%s,%d,%d,%d,%d,%d,%d,0,0,optstr%d,NULL,",
224 menus[i]->info->title, menus[i]->info->y,
225 menus[i]->info->x, menus[i]->info->h,
226 menus[i]->info->w, menus[i]->info->mopt,
227 menus[i]->info->numopt, i);
228 if (menus[i]->info->helpstr == NULL)
229 (void) fprintf (out_file, "NULL},\n");
230 else {
231 tmpstr = menus[i]->info->helpstr;
232 /* Skip an initial newline. */
233 if (*tmpstr == '\n')
234 tmpstr++;
235 (void) fprintf (out_file, "\n\"");
236 while (*tmpstr)
237 if (*tmpstr != '\n')
238 fputc (*tmpstr++, out_file);
239 else {
240 (void) fprintf (out_file, "\\n\\\n");
241 tmpstr++;
242 }
243 (void) fprintf (out_file, "\"},\n");
244 }
245 }
246 (void) fprintf (out_file, "{NULL}};\n\n");
247
248 /* num_menus */
249 (void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
250
251 /* action code */
252 (void) fprintf (out_file, "%s",
253 "static int process_item (int *menu_no, int sel)\n"
254 "{\n"
255 "\tint retval = FALSE;\n"
256 "\n"
257 "\tswitch (*menu_no) {\n"
258 );
259 for (i=0; i<menu_no; i++) {
260 (void) fprintf (out_file, "\tcase MENU_%s:\n",
261 menus[i]->id);
262 (void) fprintf (out_file, "\t\tswitch (sel) {\n"
263 "\t\tcase -2:\n");
264 if (menus[i]->info->postact.endwin)
265 (void) fprintf (out_file, "\t\t\tendwin();\n"
266 "\t\t__m_endwin = 1;\n");
267 if (strlen(menus[i]->info->postact.code))
268 (void) fprintf (out_file, "\t\t\t{%s}\n",
269 menus[i]->info->postact.code);
270 (void) fprintf (out_file, "\t\t\tbreak;\n");
271 (void) fprintf (out_file, "\t\tcase -1:\n");
272 if (menus[i]->info->exitact.endwin)
273 (void) fprintf (out_file, "\t\t\tendwin();\n"
274 "\t\t__m_endwin = 1;\n");
275 if (strlen(menus[i]->info->exitact.code))
276 (void) fprintf (out_file, "\t\t\t{%s}\n",
277 menus[i]->info->exitact.code);
278 (void) fprintf (out_file, "\t\t\tbreak;\n");
279 j = 0;
280 toptn = menus[i]->info->optns;
281 while (toptn != NULL) {
282 (void) fprintf (out_file, "\t\tcase %d:\n", j++);
283 if (toptn->optact.endwin)
284 (void) fprintf (out_file, "\t\t\tendwin();\n"
285 "\t\t__m_endwin = 1;\n");
286 if (strlen(toptn->optact.code))
287 (void) fprintf (out_file, "\t\t\t{%s}\n",
288 toptn->optact.code);
289 if (toptn->menu >= 0)
290 if (toptn->issub)
291 (void) fprintf (out_file,
292 "\t\t\tprocess_menu(%d);\n",
293 toptn->menu);
294 else
295 (void) fprintf (out_file,
296 "\t\t\t*menu_no = %d;\n",
297 toptn->menu);
298 if (toptn->doexit)
299 (void) fprintf (out_file,
300 "\t\t\tretval = TRUE;\n");
301 (void) fprintf (out_file, "\t\t\tbreak;\n");
302 toptn = toptn->next;
303 }
304
305 (void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
306 }
307 (void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
308
309 while ((ch = fgetc(sys_file)) != EOF)
310 fputc(ch, out_file);
311
312 /* __menu_initerror: initscr failed. */
313 (void) fprintf (out_file,
314 "/* __menu_initerror: initscr failed. */\n"
315 "void __menu_initerror (void) {\n");
316 if (error_act.code == NULL) {
317 (void) fprintf (out_file,
318 "\t(void) fprintf (stderr, "
319 "\"Could not initialize curses\\n\");\n"
320 "\texit(1);\n"
321 "}\n");
322 } else {
323 if (error_act.endwin)
324 (void) fprintf (out_file, "\tendwin();\n");
325 (void) fprintf (out_file, "%s\n}\n", error_act.code);
326 }
327
328 fclose (out_file);
329 fclose (sys_file);
330 }
331