Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.48
      1 /*	$NetBSD: mdb.c,v 1.48 2019/01/04 15:27:19 martin 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. The name of Piermont Information Systems Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
     22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
     25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     31  * THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 /* mdb.c - menu database manipulation */
     36 
     37 #if HAVE_NBTOOL_CONFIG_H
     38 #include "nbtool_config.h"
     39 #endif
     40 
     41 #include <sys/cdefs.h>
     42 
     43 #if defined(__RCSID) && !defined(lint)
     44 __RCSID("$NetBSD: mdb.c,v 1.48 2019/01/04 15:27:19 martin 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 void
     97 check_defined(void)
     98 {
     99 	int i;
    100 
    101 	for (i = 0; i < menu_no; i++)
    102 		if (!menus[i]->info)
    103 			yyerror ("Menu '%s' undefined.", menus[i]->id);
    104 }
    105 
    106 
    107 /* Write out the menu file. */
    108 void
    109 write_menu_file(char *initcode)
    110 {
    111 	FILE *out_file, *sys_file;
    112 	int i, j;
    113 	char hname[1024], cname[1024], sname[1024];
    114 	char *sys_prefix, *tmpstr;
    115 	int name_is_code, nlen, ch;
    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 = _PATH_DEFSYSPREFIX;
    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_msgxlat)
    161 		(void)fprintf(out_file, "#define MSG_XLAT(x) msg_string(x)\n");
    162 	else
    163 		(void)fprintf(out_file, "#define MSG_XLAT(x) (x)\n");
    164 	if (do_dynamic)
    165 		(void)fprintf(out_file, "#define DYNAMIC_MENUS\n");
    166 	if (do_expands)
    167 		(void)fprintf(out_file, "#define MENU_EXPANDS\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 		"#ifdef	MENU_EXPANDS\n"
    177 		"	const char	*opt_exp_name;\n"
    178 		"#endif\n"
    179 		"	int		opt_menu;\n"
    180 		"	int		opt_flags;\n"
    181 		"	int		(*opt_action)(menudesc *, void *);\n"
    182 		"};\n\n"
    183 		"#define OPT_SUB	1\n"
    184 		"#define OPT_ENDWIN	2\n"
    185 		"#define OPT_EXIT	4\n"
    186 		"#define OPT_IGNORE	8\n"
    187 		"#define OPT_NOSHORT	16\n"
    188 		"#define OPT_NOMENU	-1\n\n"
    189 		"struct menudesc {\n"
    190 		"	const char	*title;\n"
    191 		"	int		y, x;\n"
    192 		"	int		h, w;\n"
    193 		"	int		mopt;\n"
    194 		"	int		numopts;\n"
    195 		"	int		cursel;\n"
    196 		"	int		topline;\n"
    197 		"	menu_ent	*opts;\n"
    198 		"	WINDOW		*mw;\n"
    199 		"	WINDOW		*sv_mw;\n"
    200 		"	const char	*helpstr;\n"
    201 		"	const char	*exitstr;\n");
    202 	if (do_expands)
    203 		(void)fprintf(out_file,
    204 			"	void		(*expand_act)(menudesc *, "
    205 			    "void *);\n");
    206 	(void)fprintf(out_file,
    207 		"	void		(*post_act)(menudesc *, void *);\n"
    208 		"	void		(*exit_act)(menudesc *, void *);\n"
    209 		"	void		(*draw_line)(menudesc *, int, void *);\n"
    210 		"};\n"
    211 		"\n"
    212 		"/* defines for mopt field. */\n"
    213 #define STR(x) #x
    214 #define MC_OPT(x) "#define " #x " " STR(x) "\n"
    215 		MC_OPT(MC_NOEXITOPT)
    216 		MC_OPT(MC_NOBOX)
    217 		MC_OPT(MC_SCROLL)
    218 		MC_OPT(MC_NOSHORTCUT)
    219 		MC_OPT(MC_NOCLEAR)
    220 		MC_OPT(MC_DFLTEXIT)
    221 		MC_OPT(MC_ALWAYS_SCROLL)
    222 		MC_OPT(MC_SUBMENU)
    223 		MC_OPT(MC_VALID)
    224 #undef MC_OPT
    225 #undef STR
    226 	);
    227 
    228 	(void)fprintf(out_file, "%s",
    229 		"\n"
    230 		"/* Prototypes */\n"
    231 		"int menu_init(void);\n"
    232 		"void process_menu(int, void *);\n"
    233 		"__dead void __menu_initerror(void);\n"
    234 		);
    235 
    236 	if (do_dynamic)
    237 		(void)fprintf(out_file, "%s",
    238 			"int new_menu(const char *, menu_ent *, int, \n"
    239 			    "\tint, int, int, int, int,\n"
    240 			    "\tvoid (*)(menudesc *, void *), "
    241 			    "void (*)(menudesc *, int, void *),\n"
    242 			    "\tvoid (*)(menudesc *, void *), "
    243 			    "const char *, const char *);\n"
    244 			"void free_menu(int);\n"
    245 			"void set_menu_numopts(int, int);\n"
    246 			);
    247 
    248 	(void)fprintf(out_file, "\n/* Menu names */\n");
    249 	for (i = 0; i < menu_no; i++)
    250 		(void)fprintf(out_file, "#define MENU_%s\t%d\n",
    251 		    menus[i]->id, i);
    252 
    253 	if (do_dynamic)
    254 		(void)fprintf(out_file, "\n#define DYN_MENU_START\t%d",
    255 		    menu_no);
    256 
    257 	(void)fprintf (out_file, "\n#define MAX_STRLEN %d\n"
    258 	    "#endif\n", max_strlen);
    259 	fclose(out_file);
    260 
    261 	/* Now the C file */
    262 	out_file = fopen(cname, "w");
    263 	if (out_file == NULL) {
    264 		(void)fprintf(stderr, "%s: could not open %s.\n",
    265 		    prog_name, cname);
    266 		exit(1);
    267 	}
    268 
    269 	/* initial code */
    270 	fprintf(out_file, "#include \"%s\"\n\n", hname);
    271 	fprintf(out_file, "%s\n\n", initcode);
    272 
    273 	/* data definitions */
    274 
    275 	/* func defs */
    276 	for (i = 0; i < menu_no; i++) {
    277 		if (do_expands && strlen(menus[i]->info->expact.code)) {
    278 			(void)fprintf(out_file, "/*ARGSUSED*/\n"
    279 			    "static void menu_%d_expact(menudesc *menu, void *arg)\n{\n", i);
    280 			if (menus[i]->info->expact.endwin)
    281 				(void)fprintf(out_file, "\tendwin();\n");
    282 			(void)fprintf(out_file, "\t%s\n}\n\n",
    283 			    menus[i]->info->expact.code);
    284 		}
    285 		if (strlen(menus[i]->info->postact.code)) {
    286 			(void)fprintf(out_file, "/*ARGSUSED*/\n"
    287 			    "static void menu_%d_postact(menudesc *menu, void *arg)\n{\n", i);
    288 			if (menus[i]->info->postact.endwin)
    289 				(void)fprintf(out_file, "\tendwin();\n");
    290 			(void)fprintf(out_file, "\t%s\n}\n\n",
    291 			    menus[i]->info->postact.code);
    292 		}
    293 		if (strlen(menus[i]->info->exitact.code)) {
    294 			(void)fprintf(out_file, "/*ARGSUSED*/\n"
    295 			    "static void menu_%d_exitact(menudesc *menu, void *arg)\n{\n", i);
    296 			if (menus[i]->info->exitact.endwin)
    297 				(void)fprintf(out_file, "\tendwin();\n");
    298 			(void)fprintf(out_file, "\t%s\n}\n\n",
    299 			    menus[i]->info->exitact.code);
    300 		}
    301 		j = 0;
    302 		toptn = menus[i]->info->optns;
    303 		for (; toptn != NULL; j++, toptn = toptn->next) {
    304 			if (strlen(toptn->optact.code) == 0)
    305 				continue;
    306 
    307 			(void)fprintf(out_file, "/*ARGSUSED*/\n"
    308 			    "static int opt_act_%d_%d(menudesc *m, void *arg)\n"
    309 			    "{\n\t%s\n\treturn %s;\n}\n\n",
    310 			    i, j, toptn->optact.code,
    311 			    (toptn->doexit ? "1" : "0"));
    312 		}
    313 
    314 	}
    315 
    316 	/* optentX */
    317 	for (i = 0; i < menu_no; i++) {
    318 		if (menus[i]->info->numopt > 53) {
    319 			(void)fprintf(stderr, "Menu %s has "
    320 				"too many options.\n",
    321 				menus[i]->info->title);
    322 			exit(1);
    323 		}
    324 		(void)fprintf(out_file, "static menu_ent optent%d[] = {\n", i);
    325 		name_is_code = 0;
    326 		for (j = 0, toptn = menus[i]->info->optns; toptn;
    327 		    toptn = toptn->next, j++) {
    328 			name_is_code += toptn->name_is_code;
    329 			(void)fprintf(out_file, "\t{ .opt_name = %s, "
    330 				".opt_menu=%d, .opt_flags=%d, .opt_action=",
    331 				toptn->name_is_code ? "0" : toptn->name,
    332 				toptn->menu,
    333 				(toptn->issub ? OPT_SUB : 0)
    334 				+(toptn->doexit ? OPT_EXIT : 0)
    335 				+(toptn->optact.endwin ? OPT_ENDWIN : 0));
    336 			if (strlen(toptn->optact.code))
    337 				(void)fprintf(out_file, "opt_act_%d_%d}", i, j);
    338 			else
    339 				(void)fprintf(out_file, "NULL}");
    340 			(void)fprintf(out_file, "%s\n",
    341 				(toptn->next ? "," : ""));
    342 		}
    343 		(void)fprintf(out_file, "\t};\n\n");
    344 
    345 		if (name_is_code) {
    346 			menus[i]->info->name_is_code = 1;
    347 			fprintf(out_file, "static void menu_%d_legend("
    348 			    "menudesc *menu, int opt, void *arg)\n{\n"
    349 			    "\tswitch (opt) {\n", i);
    350 			for (j = 0, toptn = menus[i]->info->optns; toptn;
    351 			    toptn = toptn->next, j++) {
    352 				if (!toptn->name_is_code)
    353 					continue;
    354 				fprintf(out_file, "\tcase %d:\n\t\t{%s};\n"
    355 				    "\t\tbreak;\n", j, toptn->name);
    356 			}
    357 			fprintf(out_file, "\t}\n}\n\n");
    358 		}
    359 	}
    360 
    361 
    362 	/* menus */
    363 	if (!do_dynamic)
    364 		(void)fprintf(out_file, "static int num_menus = %d;\n",
    365 		    menu_no);
    366 
    367 	(void)fprintf(out_file, "static struct menudesc menu_def[] = {\n");
    368 	for (i = 0; i < menu_no; i++) {
    369 		(void)fprintf(out_file,
    370 			"\t{%s,%d,%d,%d,%d,%d,%d,0,0,optent%d,NULL,NULL,",
    371 			menus[i]->info->title, 	menus[i]->info->y,
    372 			menus[i]->info->x, menus[i]->info->h,
    373 			menus[i]->info->w, menus[i]->info->mopt,
    374 			menus[i]->info->numopt, i);
    375 		if (menus[i]->info->helpstr == NULL)
    376 			(void)fprintf(out_file, "NULL");
    377 		else {
    378 			tmpstr = menus[i]->info->helpstr;
    379 			if (*tmpstr != '"')
    380 				(void)fprintf(out_file, "%s", tmpstr);
    381 			else {
    382 				/* Skip an initial newline. */
    383 				if (tmpstr[1] == '\n')
    384 					*++tmpstr = '"';
    385 				(void)fprintf(out_file, "\n");
    386 				while (*tmpstr) {
    387 					if (*tmpstr != '\n') {
    388 						fputc(*tmpstr++, out_file);
    389 						continue;
    390 					}
    391 					(void)fprintf(out_file, "\\n\"\n\"");
    392 					tmpstr++;
    393 				}
    394 			}
    395 		}
    396 		(void)fprintf(out_file, ",");
    397 		if (menus[i]->info->mopt & MC_NOEXITOPT)
    398 			(void) fprintf (out_file, "NULL");
    399 		else if (menus[i]->info->exitstr != NULL)
    400 			(void)fprintf(out_file, "%s", menus[i]->info->exitstr);
    401 		else
    402 			(void)fprintf(out_file, "\"Exit\"");
    403 		if (do_expands) {
    404 			if (strlen(menus[i]->info->expact.code))
    405 				(void)fprintf(out_file, ",menu_%d_expact", i);
    406 			else
    407 				(void)fprintf(out_file, ",NULL");
    408 		}
    409 		if (strlen(menus[i]->info->postact.code))
    410 			(void)fprintf(out_file, ",menu_%d_postact", i);
    411 		else
    412 			(void)fprintf(out_file, ",NULL");
    413 		if (strlen(menus[i]->info->exitact.code))
    414 			(void)fprintf(out_file, ",menu_%d_exitact", i);
    415 		else
    416 			(void)fprintf(out_file, ",NULL");
    417 		if (menus[i]->info->name_is_code)
    418 			(void)fprintf(out_file, ",menu_%d_legend", i);
    419 		else
    420 			(void)fprintf(out_file, ",NULL");
    421 
    422 		(void)fprintf(out_file, "},\n");
    423 
    424 	}
    425 	(void)fprintf(out_file, "{NULL, 0, 0, 0, 0, 0, 0, 0, 0, "
    426 		"NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL");
    427 	if (do_expands)
    428 		(void)fprintf(out_file, ", NULL");
    429 	(void)fprintf(out_file, "}};\n\n");
    430 
    431 	/* __menu_initerror: initscr failed. */
    432 	(void)fprintf(out_file,
    433 		"/* __menu_initerror: initscr failed. */\n"
    434 		"void __menu_initerror (void) {\n");
    435 	if (error_act.code == NULL) {
    436 		(void)fprintf(out_file,
    437 			"\t(void) fprintf (stderr, "
    438 			"\"%%s: Could not initialize curses\\n\", "
    439 			"getprogname());\n"
    440 			"\texit(1);\n"
    441 			"}\n");
    442 	} else {
    443 		if (error_act.endwin)
    444 			(void)fprintf(out_file, "\tendwin();\n");
    445 		(void)fprintf(out_file, "%s\n}\n", error_act.code);
    446 	}
    447 
    448 	/* Copy menu_sys.def file. */
    449 	while ((ch = fgetc(sys_file)) != '\014')  /* Control-L */
    450 		fputc(ch, out_file);
    451 
    452 	if (do_dynamic) {
    453 		while ((ch = fgetc(sys_file)) != '\n')
    454 			/* skip it */;
    455 		while ((ch = fgetc(sys_file)) != EOF)
    456 			fputc(ch, out_file);
    457 	}
    458 	fclose(out_file);
    459 	fclose(sys_file);
    460 }
    461