xref: /netbsd-src/usr.bin/menuc/mdb.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: mdb.c,v 1.12 1998/07/23 17:56:00 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 		"	int	(*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 					"int opt_act_%d_%d(void);\n"
277 					"int opt_act_%d_%d(void)\n"
278 					"{\t%s\n\treturn %s;\n}\n\n",
279 					i, j, i, j, toptn->optact.code,
280 					(toptn->doexit ? "1" : "0"));
281 
282 
283 			}
284 			j++;
285 			toptn = toptn->next;
286 		}
287 
288 	}
289 
290 	/* optentX */
291 	for (i=0; i<menu_no; i++) {
292 		if (menus[i]->info->numopt > 53) {
293 			(void) fprintf (stderr, "Menu %s has "
294 				"too many options.\n",
295 				menus[i]->info->title);
296 			exit (1);
297 		}
298 		toptn = menus[i]->info->optns;
299 		j = 0;
300 		(void) fprintf (out_file,
301 				"static menu_ent optent%d[] = {\n", i);
302 		while (toptn != NULL) {
303 			(void) fprintf (out_file, "\t{\"%s,%d,%d,",
304 				toptn->name+1, toptn->menu,
305 				(toptn->issub ? OPT_SUB : 0)
306 				+(toptn->doexit ? OPT_EXIT : 0)
307 				+(toptn->optact.endwin ? OPT_ENDWIN : 0));
308 			if (strlen(toptn->optact.code))
309 				(void) fprintf (out_file, "opt_act_%d_%d}",
310 					i, j);
311 			else
312 				(void) fprintf (out_file, "NULL}");
313 			(void) fprintf (out_file, "%s\n",
314 				(toptn->next ? "," : ""));
315 			j++;
316 			toptn = toptn->next;
317 		}
318 		(void) fprintf (out_file, "\t};\n\n");
319 
320 	}
321 
322 
323 	/* menus */
324 	(void) fprintf (out_file, "static struct menudesc menu_def[] = {\n");
325 	for (i=0; i<menu_no; i++) {
326 		(void) fprintf (out_file,
327 			"\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,",
328 			menus[i]->info->title, 	menus[i]->info->y,
329 			menus[i]->info->x, menus[i]->info->h,
330 			menus[i]->info->w, menus[i]->info->mopt,
331 			menus[i]->info->numopt, i);
332 		if (menus[i]->info->helpstr == NULL)
333 			(void) fprintf (out_file, "NULL");
334 		else {
335 			tmpstr = menus[i]->info->helpstr;
336 			/* Skip an initial newline. */
337 			if (*tmpstr == '\n')
338 				tmpstr++;
339 			(void) fprintf (out_file, "\n\"");
340 			while (*tmpstr)
341 				if (*tmpstr != '\n')
342 				  fputc (*tmpstr++, out_file);
343 				else {
344 					(void) fprintf (out_file, "\\n\\\n");
345 					tmpstr++;
346 				}
347 			(void) fprintf (out_file, "\"");
348 		}
349 		if (strlen(menus[i]->info->postact.code))
350 			(void) fprintf (out_file, ",menu_%d_postact", i);
351 		else
352 			(void) fprintf (out_file, ",NULL");
353 		if (strlen(menus[i]->info->exitact.code))
354 			(void) fprintf (out_file, ",menu_%d_exitact", i);
355 		else
356 			(void) fprintf (out_file, ",NULL");
357 
358 		(void) fprintf (out_file, "},\n");
359 
360 	}
361 	(void) fprintf (out_file, "{NULL}};\n\n");
362 
363 	/* __menu_initerror: initscr failed. */
364 	(void) fprintf (out_file,
365 		"/* __menu_initerror: initscr failed. */\n"
366 		"void __menu_initerror (void) {\n");
367 	if (error_act.code == NULL) {
368 		(void) fprintf (out_file,
369 			"\t(void) fprintf (stderr, "
370 				"\"Could not initialize curses\\n\");\n"
371 			"\texit(1);\n"
372 			"}\n");
373 	} else {
374 		if (error_act.endwin)
375 			(void) fprintf (out_file, "\tendwin();\n");
376 		(void) fprintf (out_file, "%s\n}\n", error_act.code);
377 	}
378 
379 	/* Copy menu_sys.def file. */
380 	while ((ch = fgetc(sys_file)) != '\014')  /* Control-L */
381 		fputc(ch, out_file);
382 
383 	if (do_dynamic) {
384 		while ((ch = fgetc(sys_file)) != '\n')
385 			/* skip it */;
386 		while ((ch = fgetc(sys_file)) != EOF)
387 			fputc(ch, out_file);
388 	}
389 
390 	fclose (out_file);
391 	fclose (sys_file);
392 }
393