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