Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.33
      1 /*	$NetBSD: mdb.c,v 1.33 2003/06/10 17:19:04 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_msgxlat)
    163 		(void)fprintf(out_file, "#define MSG_XLAT(x) msg_string(x)\n");
    164 	else
    165 		(void)fprintf(out_file, "#define MSG_XLAT(x) (x)\n");
    166 	if (do_dynamic)
    167 		(void)fprintf(out_file, "#define DYNAMIC_MENUS\n");
    168 	if (do_dynamic || do_msgxlat)
    169 		(void)fprintf(out_file, "\n");
    170 
    171 	(void) fprintf (out_file,
    172 		"typedef struct menudesc menudesc;\n"
    173 		"typedef struct menu_ent menu_ent;\n"
    174 		"struct menu_ent {\n"
    175 		"	const char	*opt_name;\n"
    176 		"	int		opt_menu;\n"
    177 		"	int		opt_flags;\n"
    178 		"	int		(*opt_action)(menudesc *, menu_ent *, void *);\n"
    179 		"};\n\n"
    180 		"#define OPT_SUB	1\n"
    181 		"#define OPT_ENDWIN	2\n"
    182 		"#define OPT_EXIT	4\n"
    183 		"#define OPT_NOMENU	-1\n\n"
    184 		"struct menudesc {\n"
    185 		"	const char	*title;\n"
    186 		"	int		y, x;\n"
    187 		"	int		h, w;\n"
    188 		"	int		mopt;\n"
    189 		"	int		numopts;\n"
    190 		"	int		cursel;\n"
    191 		"	int		topline;\n"
    192 		"	menu_ent	*opts;\n"
    193 		"	WINDOW		*mw;\n"
    194 		"	const char	*helpstr;\n"
    195 		"	const char	*exitstr;\n"
    196 		"	void		(*post_act)(menudesc *, void *);\n"
    197 		"	void		(*exit_act)(menudesc *, void *);\n"
    198 		"};\n"
    199 		"\n"
    200 		"/* defines for mopt field. */\n"
    201 		"#define MC_NOEXITOPT 1\n"
    202 		"#define MC_NOBOX 2\n"
    203 		"#define MC_SCROLL 4\n"
    204 		"#define MC_NOSHORTCUT 8\n"
    205 		"#define MC_NOCLEAR 16\n"
    206 		"#define MC_DFLTEXIT 32\n"
    207 		"#define MC_VALID 256\n"
    208 		);
    209 
    210 	(void) fprintf (out_file, "%s",
    211 		"\n"
    212 		"/* initilization flag */\n"
    213 		"extern int __m_endwin;\n"
    214 		"\n"
    215 		"/* Prototypes */\n"
    216 		"int menu_init (void);\n"
    217 		"void process_menu (int num, void *arg);\n"
    218 		"void __menu_initerror (void);\n"
    219 		);
    220 
    221 	if (do_dynamic)
    222 		(void) fprintf (out_file, "%s",
    223 			"int new_menu(const char *title, menu_ent *opts, "
    224 				"int numopts, \n"
    225 				"\tint x, int y, int h, int w, int mopt,\n"
    226 				"\tvoid (*post_act)(menudesc *, void *), "
    227 				"void (*exit_act)(menudesc *, void *), "
    228 				"const char *help, const char *exit);\n"
    229 			"void free_menu(int menu_no);\n"
    230 			"void set_menu_numopts(int, int);\n"
    231 			);
    232 
    233 	(void) fprintf (out_file, "\n/* Menu names */\n");
    234 	for (i=0; i<menu_no; i++) {
    235 		(void) fprintf (out_file, "#define MENU_%s\t%d\n",
    236 				menus[i]->id, i);
    237 	}
    238 
    239 	if (do_dynamic)
    240 		(void) fprintf (out_file, "\n#define DYN_MENU_START\t%d",
    241 				menu_no);
    242 
    243 	(void) fprintf (out_file, "\n#define MAX_STRLEN %d\n"
    244 			"#endif\n", max_strlen);
    245 	fclose (out_file);
    246 
    247 	/* Now the C file */
    248 	out_file = fopen (cname, "w");
    249 	if (out_file == NULL) {
    250 		(void) fprintf (stderr, "%s: could not open %s.\n",
    251 				prog_name, cname);
    252 		exit (1);
    253 	}
    254 
    255 	/* initial code */
    256 	fprintf (out_file, "#include \"%s\"\n\n", hname);
    257 	fprintf (out_file, "%s\n\n", initcode);
    258 
    259 	/* data definitions */
    260 
    261 	/* func defs */
    262 	for (i=0; i<menu_no; i++) {
    263 		if (strlen(menus[i]->info->postact.code)) {
    264 			(void) fprintf (out_file,
    265 				"/*ARGSUSED*/\n"
    266 				"static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i);
    267 			if (menus[i]->info->postact.endwin)
    268 				(void) fprintf (out_file, "\tendwin();\n"
    269 					"\t__m_endwin = 1;\n");
    270 			(void) fprintf (out_file,
    271 					"\t%s\n}\n\n",
    272 					menus[i]->info->postact.code);
    273 		}
    274 		if (strlen(menus[i]->info->exitact.code)) {
    275 			(void) fprintf (out_file,
    276 				"/*ARGSUSED*/\n"
    277 				"static void menu_%d_exitact(menudesc *menu, void *arg)\n{\n", i);
    278 			if (menus[i]->info->exitact.endwin)
    279 				(void) fprintf (out_file, "\tendwin();\n"
    280 					"\t__m_endwin = 1;\n");
    281 			(void) fprintf (out_file, "\t%s\n}\n\n",
    282 					menus[i]->info->exitact.code);
    283 		}
    284 		j = 0;
    285 		toptn = menus[i]->info->optns;
    286 		for (;toptn != NULL; j++, toptn = toptn->next) {
    287 			if (strlen(toptn->optact.code) == 0)
    288 				continue;
    289 
    290 			(void) fprintf (out_file,
    291 				"/*ARGSUSED*/\n"
    292 				"static int opt_act_%d_%d(menudesc *m, menu_ent *opt, void *arg)\n"
    293 				"{\n\t%s\n\treturn %s;\n}\n\n",
    294 				i, j, toptn->optact.code,
    295 				(toptn->doexit ? "1" : "0"));
    296 		}
    297 
    298 	}
    299 
    300 	/* optentX */
    301 	for (i=0; i<menu_no; i++) {
    302 		if (menus[i]->info->numopt > 53) {
    303 			(void) fprintf (stderr, "Menu %s has "
    304 				"too many options.\n",
    305 				menus[i]->info->title);
    306 			exit (1);
    307 		}
    308 		toptn = menus[i]->info->optns;
    309 		j = 0;
    310 		(void) fprintf (out_file,
    311 				"static menu_ent optent%d[] = {\n", i);
    312 		while (toptn != NULL) {
    313 			(void) fprintf (out_file, "\t{%s,%d,%d,",
    314 				toptn->name, toptn->menu,
    315 				(toptn->issub ? OPT_SUB : 0)
    316 				+(toptn->doexit ? OPT_EXIT : 0)
    317 				+(toptn->optact.endwin ? OPT_ENDWIN : 0));
    318 			if (strlen(toptn->optact.code))
    319 				(void) fprintf (out_file, "opt_act_%d_%d}",
    320 					i, j);
    321 			else
    322 				(void) fprintf (out_file, "NULL}");
    323 			(void) fprintf (out_file, "%s\n",
    324 				(toptn->next ? "," : ""));
    325 			j++;
    326 			toptn = toptn->next;
    327 		}
    328 		(void) fprintf (out_file, "\t};\n\n");
    329 
    330 	}
    331 
    332 
    333 	/* menus */
    334 	(void) fprintf (out_file, "static struct menudesc menu_def[] = {\n");
    335 	for (i=0; i<menu_no; i++) {
    336 		(void) fprintf (out_file,
    337 			"\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,",
    338 			menus[i]->info->title, 	menus[i]->info->y,
    339 			menus[i]->info->x, menus[i]->info->h,
    340 			menus[i]->info->w, menus[i]->info->mopt,
    341 			menus[i]->info->numopt, i);
    342 		if (menus[i]->info->helpstr == NULL)
    343 			(void) fprintf (out_file, "NULL");
    344 		else {
    345 			tmpstr = menus[i]->info->helpstr;
    346 			/* Skip an initial newline. */
    347 			if (*tmpstr == '\n')
    348 				tmpstr++;
    349 			(void) fprintf (out_file, "\n\"");
    350 			while (*tmpstr)
    351 				if (*tmpstr != '\n')
    352 					fputc (*tmpstr++, out_file);
    353 				else {
    354 					(void) fprintf (out_file, "\\n\\\n");
    355 					tmpstr++;
    356 				}
    357 			(void) fprintf (out_file, "\"");
    358 		}
    359 		(void) fprintf (out_file, ",");
    360 		if (menus[i]->info->mopt & NOEXITOPT)
    361 			(void) fprintf (out_file, "NULL");
    362 		else if (menus[i]->info->exitstr != NULL)
    363 			(void) fprintf (out_file, "%s",
    364 				menus[i]->info->exitstr);
    365 		else
    366 			(void) fprintf (out_file, "\"Exit\"");
    367 		if (strlen(menus[i]->info->postact.code))
    368 			(void) fprintf (out_file, ",menu_%d_postact", i);
    369 		else
    370 			(void) fprintf (out_file, ",NULL");
    371 		if (strlen(menus[i]->info->exitact.code))
    372 			(void) fprintf (out_file, ",menu_%d_exitact", i);
    373 		else
    374 			(void) fprintf (out_file, ",NULL");
    375 
    376 		(void) fprintf (out_file, "},\n");
    377 
    378 	}
    379 	(void) fprintf (out_file, "{NULL}};\n\n");
    380 
    381 	/* __menu_initerror: initscr failed. */
    382 	(void) fprintf (out_file,
    383 		"/* __menu_initerror: initscr failed. */\n"
    384 		"void __menu_initerror (void) {\n");
    385 	if (error_act.code == NULL) {
    386 		(void) fprintf (out_file,
    387 			"\t(void) fprintf (stderr, "
    388 			"\"%%s: Could not initialize curses\\n\", "
    389 			"getprogname());\n"
    390 			"\texit(1);\n"
    391 			"}\n");
    392 	} else {
    393 		if (error_act.endwin)
    394 			(void) fprintf (out_file, "\tendwin();\n");
    395 		(void) fprintf (out_file, "%s\n}\n", error_act.code);
    396 	}
    397 
    398 	/* Copy menu_sys.def file. */
    399 	while ((ch = fgetc(sys_file)) != '\014')  /* Control-L */
    400 		fputc(ch, out_file);
    401 
    402 	if (do_dynamic) {
    403 		while ((ch = fgetc(sys_file)) != '\n')
    404 			/* skip it */;
    405 		while ((ch = fgetc(sys_file)) != EOF)
    406 			fputc(ch, out_file);
    407 	}
    408 
    409 	fclose (out_file);
    410 	fclose (sys_file);
    411 }
    412