Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.7
      1 /*	$NetBSD: mdb.c,v 1.7 1998/06/25 19:57:10 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 500
     49 static int menu_no = 0;
     50 static id_rec *menus[MAX];
     51 
     52 
     53 /* get_menu returns a pointer to a newly created id_rec or an old one. */
     54 
     55 id_rec *
     56 get_menu (char *name)
     57 {
     58 	id_rec *temp;
     59 
     60 	temp = find_id (root, name);
     61 
     62 	if (temp == NULL) {
     63 		if (menu_no < MAX) {
     64 			temp = (id_rec *) malloc (sizeof(id_rec));
     65 			temp->id = strdup(name);
     66 			temp->info = NULL;
     67 			temp->menu_no = menu_no;
     68 			menus[menu_no++] = temp;
     69 			insert_id (&root, temp);
     70 		} else {
     71 			(void) fprintf (stderr, "Too many menus.  "
     72 					"Increase MAX.\n");
     73 			exit(1);
     74 		}
     75 	}
     76 
     77 	return temp;
     78 }
     79 
     80 
     81 /* Verify that all menus are defined. */
     82 
     83 void
     84 check_defined (void)
     85 {
     86 	int i;
     87 
     88 	for (i=0; i<menu_no; i++)
     89 		if (!menus[i]->info)
     90 			yyerror ("Menu '%s' undefined.", menus[i]->id);
     91 }
     92 
     93 
     94 /* Write out the menu file. */
     95 void
     96 write_menu_file (char *initcode)
     97 {
     98 	FILE *out_file;
     99 	FILE *sys_file;
    100 	int i, j;
    101 	char hname[1024];
    102 	char cname[1024];
    103 	char sname[1024];
    104 	char *sys_prefix;
    105 	char *tmpstr;
    106 
    107 	int nlen;
    108 
    109 	char opt_ch;
    110 	int ch;
    111 
    112 	optn_info *toptn;
    113 
    114 	/* Generate file names */
    115 	snprintf (hname, 1024, "%s.h", out_name);
    116 	nlen = strlen(hname);
    117 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
    118 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
    119 				prog_name, out_name);
    120 		exit(1);
    121 	}
    122 	snprintf (cname, 1024, "%s.c", out_name);
    123 
    124 	/* Open the menu_sys file first. */
    125 	sys_prefix = getenv ("MENUDEF");
    126 	if (sys_prefix == NULL)
    127 		sys_prefix = "/usr/share/misc";
    128 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
    129 	sys_file = fopen (sname, "r");
    130 	if (sys_file == NULL) {
    131 		(void) fprintf (stderr, "%s: could not open %s.\n",
    132 				prog_name, sname);
    133 		exit (1);
    134 	}
    135 
    136 	/* Output the .h file first. */
    137 	out_file = fopen (hname, "w");
    138 	if (out_file == NULL) {
    139 		(void) fprintf (stderr, "%s: could not open %s.\n",
    140 				prog_name, hname);
    141 		exit (1);
    142 	}
    143 
    144 	/* Write it */
    145 	(void) fprintf (out_file, "%s",
    146 		"/* menu system definitions. */\n"
    147 		"\n"
    148 		"#ifndef MENU_DEFS_H\n"
    149 		"#define MENU_DEFS_H\n"
    150 		"#include <stdlib.h>\n"
    151 		"#include <string.h>\n"
    152 		"#include <ctype.h>\n"
    153 		"#include <curses.h>\n"
    154 		"\n"
    155 		"typedef\n"
    156 		"struct menudesc {\n"
    157 		"	char   *title;\n"
    158 		"	int     y, x;\n"
    159 		"	int	h, w;\n"
    160 		"	int	mopt;\n"
    161 		"	int     numopts;\n"
    162 		"	int	cursel;\n"
    163 		"	int	topline;\n"
    164 		"	char   **opts;\n"
    165 		"	WINDOW *mw;\n"
    166 		"	char   *helpstr;\n"
    167 		"} menudesc ;\n"
    168 		"\n"
    169 		"/* defines for mopt field. */\n"
    170 		"#define NOEXITOPT 1\n"
    171 		"#define NOBOX 2\n"
    172 		"#define SCROLL 4\n"
    173 		"\n"
    174 		"/* initilization flag */\n"
    175 		"extern int __m_endwin;\n"
    176 		"\n"
    177 		"/* Prototypes */\n"
    178 		"void process_menu (int num);\n"
    179 		"void __menu_initerror (void);\n"
    180 		"\n"
    181 		"/* Menu names */\n"
    182 	      );
    183 	for (i=0; i<menu_no; i++) {
    184 		(void) fprintf (out_file, "#define MENU_%s\t%d\n",
    185 				menus[i]->id, i);
    186 	}
    187 	(void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
    188 	(void) fprintf (out_file, "#endif\n");
    189 
    190 	fclose (out_file);
    191 
    192 	/* Now the C file */
    193 	out_file = fopen (cname, "w");
    194 	if (out_file == NULL) {
    195 		(void) fprintf (stderr, "%s: could not open %s.\n",
    196 				prog_name, cname);
    197 		exit (1);
    198 	}
    199 
    200 	/* initial code */
    201 	fprintf (out_file, "#include \"%s\"\n\n", hname);
    202 	fprintf (out_file, "%s\n\n", initcode);
    203 
    204 	/* data definitions */
    205 
    206 	/* optstrX */
    207 	for (i=0; i<menu_no; i++) {
    208 		(void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
    209 		toptn = menus[i]->info->optns;
    210 		opt_ch = 'a';
    211 		while (toptn != NULL) {
    212 			(void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch,
    213 					toptn->name+1);
    214 			toptn = toptn->next;
    215 			if (opt_ch == 'z')
    216 				opt_ch = 'A';
    217 			else if (opt_ch == 'x')
    218 				opt_ch = 'y';
    219 			else if (opt_ch == 'Z') {
    220 				(void) fprintf (stderr, "Menu %s has "
    221 					"too many options.\n",
    222 					menus[i]->info->title);
    223 				exit (1);
    224 			} else
    225 				opt_ch++;
    226 		}
    227 		(void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
    228 	}
    229 
    230 	/* menus */
    231 	(void) fprintf (out_file, "static struct menudesc menus[] = {\n");
    232 	for (i=0; i<menu_no; i++) {
    233 		(void) fprintf (out_file,
    234 			"\t{%s,%d,%d,%d,%d,%d,%d,0,0,optstr%d,NULL,",
    235 			menus[i]->info->title, 	menus[i]->info->y,
    236 			menus[i]->info->x, menus[i]->info->h,
    237 			menus[i]->info->w, menus[i]->info->mopt,
    238 			menus[i]->info->numopt, i);
    239 		if (menus[i]->info->helpstr == NULL)
    240 			(void) fprintf (out_file, "NULL},\n");
    241 		else {
    242 			tmpstr = menus[i]->info->helpstr;
    243 			/* Skip an initial newline. */
    244 			if (*tmpstr == '\n')
    245 				tmpstr++;
    246 			(void) fprintf (out_file, "\n\"");
    247 			while (*tmpstr)
    248 				if (*tmpstr != '\n')
    249 				  fputc (*tmpstr++, out_file);
    250 				else {
    251 					(void) fprintf (out_file, "\\n\\\n");
    252 					tmpstr++;
    253 				}
    254 			(void) fprintf (out_file, "\"},\n");
    255 		}
    256 	}
    257 	(void) fprintf (out_file, "{NULL}};\n\n");
    258 
    259 	/* num_menus */
    260 	(void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
    261 
    262 	/* action code */
    263 	(void) fprintf (out_file, "%s",
    264 		"static int process_item (int *menu_no, int sel)\n"
    265 		"{\n"
    266 		"\tint retval = FALSE;\n"
    267 		"\n"
    268 		"\tswitch (*menu_no) {\n"
    269 	);
    270 	for (i=0; i<menu_no; i++) {
    271 		(void) fprintf (out_file, "\tcase MENU_%s:\n",
    272 				menus[i]->id);
    273 		(void) fprintf (out_file, "\t\tswitch (sel) {\n"
    274 				"\t\tcase -2:\n");
    275 		if (menus[i]->info->postact.endwin)
    276 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    277 					"\t\t__m_endwin = 1;\n");
    278 		if (strlen(menus[i]->info->postact.code))
    279 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    280 					menus[i]->info->postact.code);
    281 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    282 		(void) fprintf (out_file, "\t\tcase -1:\n");
    283 		if (menus[i]->info->exitact.endwin)
    284 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    285 					"\t\t__m_endwin = 1;\n");
    286 		if (strlen(menus[i]->info->exitact.code))
    287 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    288 					menus[i]->info->exitact.code);
    289 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    290 		j = 0;
    291 		toptn = menus[i]->info->optns;
    292 		while (toptn != NULL) {
    293 			(void) fprintf (out_file, "\t\tcase %d:\n", j++);
    294 			if (toptn->optact.endwin)
    295 				(void) fprintf (out_file, "\t\t\tendwin();\n"
    296 						"\t\t__m_endwin = 1;\n");
    297 			if (strlen(toptn->optact.code))
    298 				(void) fprintf (out_file, "\t\t\t{%s}\n",
    299 						toptn->optact.code);
    300 			if (toptn->menu >= 0)
    301 				if (toptn->issub)
    302 					(void) fprintf (out_file,
    303 						"\t\t\tprocess_menu(%d);\n",
    304 						toptn->menu);
    305 				else
    306 					(void) fprintf (out_file,
    307 						"\t\t\t*menu_no = %d;\n",
    308 						toptn->menu);
    309 			if (toptn->doexit)
    310 				(void) fprintf (out_file,
    311 						"\t\t\tretval = TRUE;\n");
    312 			(void) fprintf (out_file, "\t\t\tbreak;\n");
    313 			toptn = toptn->next;
    314 		}
    315 
    316 		(void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
    317 	}
    318 	(void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
    319 
    320 	while ((ch = fgetc(sys_file)) != EOF)
    321 		fputc(ch, out_file);
    322 
    323 	/* __menu_initerror: initscr failed. */
    324 	(void) fprintf (out_file,
    325 		"/* __menu_initerror: initscr failed. */\n"
    326 		"void __menu_initerror (void) {\n");
    327 	if (error_act.code == NULL) {
    328 		(void) fprintf (out_file,
    329 			"\t(void) fprintf (stderr, "
    330 				"\"Could not initialize curses\\n\");\n"
    331 			"\texit(1);\n"
    332 			"}\n");
    333 	} else {
    334 		if (error_act.endwin)
    335 			(void) fprintf (out_file, "\tendwin();\n");
    336 		(void) fprintf (out_file, "%s\n}\n", error_act.code);
    337 	}
    338 
    339 	fclose (out_file);
    340 	fclose (sys_file);
    341 }
    342