Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.2
      1 /*	$NetBSD: mdb.c,v 1.2 1997/11/09 20:59:14 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 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 
    106 	int nlen;
    107 
    108 	char opt_ch;
    109 	char ch;
    110 
    111 	optn_info *toptn;
    112 
    113 	/* Generate file names */
    114 	snprintf (hname, 1024, "%s.h", out_name);
    115 	nlen = strlen(hname);
    116 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
    117 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
    118 				prog_name, out_name);
    119 		exit(1);
    120 	}
    121 	snprintf (cname, 1024, "%s.c", out_name);
    122 
    123 	/* Open the menu_sys file first. */
    124 	sys_prefix = getenv ("MENUDEF");
    125 	if (sys_prefix == NULL)
    126 		sys_prefix = "/usr/share/misc";
    127 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
    128 	sys_file = fopen (sname, "r");
    129 	if (sys_file == NULL) {
    130 		(void) fprintf (stderr, "%s: could not open %s.\n",
    131 				prog_name, sname);
    132 		exit (1);
    133 	}
    134 
    135 	/* Output the .h file first. */
    136 	out_file = fopen (hname, "w");
    137 	if (out_file == NULL) {
    138 		(void) fprintf (stderr, "%s: could not open %s.\n",
    139 				prog_name, hname);
    140 		exit (1);
    141 	}
    142 
    143 	/* Write it */
    144 	(void) fprintf (out_file, "%s",
    145 		"/* menu system definitions. */\n"
    146 		"\n"
    147 		"#ifndef MENU_DEFS_H\n"
    148 		"#define MENU_DEFS_H\n"
    149 		"#include <stdlib.h>\n"
    150 		"#include <string.h>\n"
    151 		"#include <ctype.h>\n"
    152 		"#include <curses.h>\n"
    153 		"\n"
    154 		"typedef\n"
    155 		"struct menudesc {\n"
    156 		"	char   *title;\n"
    157 		"	int     y, x;\n"
    158 		"	int	h, w;\n"
    159 		"	int	mopt;\n"
    160 		"	int     numopts;\n"
    161 		"	int	cursel;\n"
    162 		"	char   **opts;\n"
    163 		"	WINDOW *mw;\n"
    164 		"} menudesc ;\n"
    165 		"\n"
    166 		"/* defines for mopt field. */\n"
    167 		"#define NOEXITOPT 1\n"
    168 		"#define NOBOX 2\n"
    169 		"\n"
    170 		"/* initilization flag */\n"
    171 		"extern int __m_endwin;\n"
    172 		"\n"
    173 		"/* Prototypes */\n"
    174 		"void process_menu (int num);\n"
    175 		"void __menu_initerror (void);\n"
    176 		"\n"
    177 		"/* Menu names */\n"
    178 	      );
    179 	for (i=0; i<menu_no; i++) {
    180 		(void) fprintf (out_file, "#define MENU_%s\t%d\n",
    181 				menus[i]->id, i);
    182 	}
    183 	(void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
    184 	(void) fprintf (out_file, "#endif\n");
    185 
    186 	fclose (out_file);
    187 
    188 	/* Now the C file */
    189 	out_file = fopen (cname, "w");
    190 	if (out_file == NULL) {
    191 		(void) fprintf (stderr, "%s: could not open %s.\n",
    192 				prog_name, cname);
    193 		exit (1);
    194 	}
    195 
    196 	/* initial code */
    197 	fprintf (out_file, "#include \"%s\"\n\n", hname);
    198 	fprintf (out_file, "%s\n\n", initcode);
    199 
    200 	/* data definitions */
    201 
    202 	/* optstrX */
    203 	for (i=0; i<menu_no; i++) {
    204 		(void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
    205 		toptn = menus[i]->info->optns;
    206 		opt_ch = 'a';
    207 		while (toptn != NULL) {
    208 			(void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch++,
    209 					toptn->name+1);
    210 			toptn = toptn->next;
    211 		}
    212 		(void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
    213 	}
    214 
    215 	/* menus */
    216 	(void) fprintf (out_file, "static struct menudesc menus[] = {\n");
    217 	for (i=0; i<menu_no; i++)
    218 		(void) fprintf (out_file,
    219 			"\t{%s,%d,%d,%d,%d,%d,%d,0,optstr%d,NULL},\n",
    220 			menus[i]->info->title, 	menus[i]->info->y,
    221 			menus[i]->info->x, menus[i]->info->h,
    222 			menus[i]->info->w, menus[i]->info->mopt,
    223 			menus[i]->info->numopt, i);
    224 	(void) fprintf (out_file, "{NULL}};\n\n");
    225 
    226 	/* num_menus */
    227 	(void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
    228 
    229 	/* action code */
    230 	(void) fprintf (out_file, "%s",
    231 		"static int process_item (int *menu_no, int sel)\n"
    232 		"{\n"
    233 		"\tint retval = FALSE;\n"
    234 		"\n"
    235 		"\tswitch (*menu_no) {\n"
    236 	);
    237 	for (i=0; i<menu_no; i++) {
    238 		(void) fprintf (out_file, "\tcase MENU_%s:\n",
    239 				menus[i]->id);
    240 		(void) fprintf (out_file, "\t\tswitch (sel) {\n"
    241 				"\t\tcase -2:\n");
    242 		if (menus[i]->info->postact.endwin)
    243 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    244 					"\t\t__m_endwin = 1;\n");
    245 		if (strlen(menus[i]->info->postact.code))
    246 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    247 					menus[i]->info->postact.code);
    248 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    249 		(void) fprintf (out_file, "\t\tcase -1:\n");
    250 		if (menus[i]->info->exitact.endwin)
    251 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    252 					"\t\t__m_endwin = 1;\n");
    253 		if (strlen(menus[i]->info->exitact.code))
    254 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    255 					menus[i]->info->exitact.code);
    256 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    257 		j = 0;
    258 		toptn = menus[i]->info->optns;
    259 		while (toptn != NULL) {
    260 			(void) fprintf (out_file, "\t\tcase %d:\n", j++);
    261 			if (toptn->optact.endwin)
    262 				(void) fprintf (out_file, "\t\t\tendwin();\n"
    263 						"\t\t__m_endwin = 1;\n");
    264 			if (strlen(toptn->optact.code))
    265 				(void) fprintf (out_file, "\t\t\t{%s}\n",
    266 						toptn->optact.code);
    267 			if (toptn->menu >= 0)
    268 				if (toptn->issub)
    269 					(void) fprintf (out_file,
    270 						"\t\t\tprocess_menu(%d);\n",
    271 						toptn->menu);
    272 				else
    273 					(void) fprintf (out_file,
    274 						"\t\t\t*menu_no = %d;\n",
    275 						toptn->menu);
    276 			if (toptn->doexit)
    277 				(void) fprintf (out_file,
    278 						"\t\t\tretval = TRUE;\n");
    279 			(void) fprintf (out_file, "\t\t\tbreak;\n");
    280 			toptn = toptn->next;
    281 		}
    282 
    283 		(void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
    284 	}
    285 	(void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
    286 
    287 	while ((ch = fgetc(sys_file)) != EOF)
    288 		fputc(ch, out_file);
    289 
    290 	/* __menu_initerror: initscr failed. */
    291 	(void) fprintf (out_file,
    292 		"/* __menu_initerror: initscr failed. */\n"
    293 		"void __menu_initerror (void) {\n");
    294 	if (error_act.code == NULL) {
    295 		(void) fprintf (out_file,
    296 			"\t(void) fprintf (stderr, "
    297 				"\"Could not initialize curses\\n\");\n"
    298 			"\texit(1);\n"
    299 			"}\n");
    300 	} else {
    301 		if (error_act.endwin)
    302 			(void) fprintf (out_file, "\tendwin();\n");
    303 		(void) fprintf (out_file, "%s\n}\n", error_act.code);
    304 	}
    305 
    306 	fclose (out_file);
    307 	fclose (sys_file);
    308 }
    309