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