Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.5
      1 /*	$NetBSD: mdb.c,v 1.5 1998/06/24 06:46:23 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 		"	char   **opts;\n"
    164 		"	WINDOW *mw;\n"
    165 		"	char   *helpstr;\n"
    166 		"} menudesc ;\n"
    167 		"\n"
    168 		"/* defines for mopt field. */\n"
    169 		"#define NOEXITOPT 1\n"
    170 		"#define NOBOX 2\n"
    171 		"\n"
    172 		"/* initilization flag */\n"
    173 		"extern int __m_endwin;\n"
    174 		"\n"
    175 		"/* Prototypes */\n"
    176 		"void process_menu (int num);\n"
    177 		"void __menu_initerror (void);\n"
    178 		"\n"
    179 		"/* Menu names */\n"
    180 	      );
    181 	for (i=0; i<menu_no; i++) {
    182 		(void) fprintf (out_file, "#define MENU_%s\t%d\n",
    183 				menus[i]->id, i);
    184 	}
    185 	(void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
    186 	(void) fprintf (out_file, "#endif\n");
    187 
    188 	fclose (out_file);
    189 
    190 	/* Now the C file */
    191 	out_file = fopen (cname, "w");
    192 	if (out_file == NULL) {
    193 		(void) fprintf (stderr, "%s: could not open %s.\n",
    194 				prog_name, cname);
    195 		exit (1);
    196 	}
    197 
    198 	/* initial code */
    199 	fprintf (out_file, "#include \"%s\"\n\n", hname);
    200 	fprintf (out_file, "%s\n\n", initcode);
    201 
    202 	/* data definitions */
    203 
    204 	/* optstrX */
    205 	for (i=0; i<menu_no; i++) {
    206 		(void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
    207 		toptn = menus[i]->info->optns;
    208 		opt_ch = 'a';
    209 		while (toptn != NULL) {
    210 			(void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch++,
    211 					toptn->name+1);
    212 			toptn = toptn->next;
    213 		}
    214 		(void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
    215 	}
    216 
    217 	/* menus */
    218 	(void) fprintf (out_file, "static struct menudesc menus[] = {\n");
    219 	for (i=0; i<menu_no; i++) {
    220 		(void) fprintf (out_file,
    221 			"\t{%s,%d,%d,%d,%d,%d,%d,0,optstr%d,NULL,",
    222 			menus[i]->info->title, 	menus[i]->info->y,
    223 			menus[i]->info->x, menus[i]->info->h,
    224 			menus[i]->info->w, menus[i]->info->mopt,
    225 			menus[i]->info->numopt, i);
    226 		if (menus[i]->info->helpstr == NULL)
    227 			(void) fprintf (out_file, "NULL},\n");
    228 		else {
    229 			tmpstr = menus[i]->info->helpstr;
    230 			/* Skip an initial newline. */
    231 			if (*tmpstr == '\n')
    232 				tmpstr++;
    233 			(void) fprintf (out_file, "\n\"");
    234 			while (*tmpstr)
    235 				if (*tmpstr != '\n')
    236 				  fputc (*tmpstr++, out_file);
    237 				else {
    238 					(void) fprintf (out_file, "\\n\\\n");
    239 					tmpstr++;
    240 				}
    241 			(void) fprintf (out_file, "\"},\n");
    242 		}
    243 	}
    244 	(void) fprintf (out_file, "{NULL}};\n\n");
    245 
    246 	/* num_menus */
    247 	(void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
    248 
    249 	/* action code */
    250 	(void) fprintf (out_file, "%s",
    251 		"static int process_item (int *menu_no, int sel)\n"
    252 		"{\n"
    253 		"\tint retval = FALSE;\n"
    254 		"\n"
    255 		"\tswitch (*menu_no) {\n"
    256 	);
    257 	for (i=0; i<menu_no; i++) {
    258 		(void) fprintf (out_file, "\tcase MENU_%s:\n",
    259 				menus[i]->id);
    260 		(void) fprintf (out_file, "\t\tswitch (sel) {\n"
    261 				"\t\tcase -2:\n");
    262 		if (menus[i]->info->postact.endwin)
    263 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    264 					"\t\t__m_endwin = 1;\n");
    265 		if (strlen(menus[i]->info->postact.code))
    266 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    267 					menus[i]->info->postact.code);
    268 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    269 		(void) fprintf (out_file, "\t\tcase -1:\n");
    270 		if (menus[i]->info->exitact.endwin)
    271 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    272 					"\t\t__m_endwin = 1;\n");
    273 		if (strlen(menus[i]->info->exitact.code))
    274 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    275 					menus[i]->info->exitact.code);
    276 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    277 		j = 0;
    278 		toptn = menus[i]->info->optns;
    279 		while (toptn != NULL) {
    280 			(void) fprintf (out_file, "\t\tcase %d:\n", j++);
    281 			if (toptn->optact.endwin)
    282 				(void) fprintf (out_file, "\t\t\tendwin();\n"
    283 						"\t\t__m_endwin = 1;\n");
    284 			if (strlen(toptn->optact.code))
    285 				(void) fprintf (out_file, "\t\t\t{%s}\n",
    286 						toptn->optact.code);
    287 			if (toptn->menu >= 0)
    288 				if (toptn->issub)
    289 					(void) fprintf (out_file,
    290 						"\t\t\tprocess_menu(%d);\n",
    291 						toptn->menu);
    292 				else
    293 					(void) fprintf (out_file,
    294 						"\t\t\t*menu_no = %d;\n",
    295 						toptn->menu);
    296 			if (toptn->doexit)
    297 				(void) fprintf (out_file,
    298 						"\t\t\tretval = TRUE;\n");
    299 			(void) fprintf (out_file, "\t\t\tbreak;\n");
    300 			toptn = toptn->next;
    301 		}
    302 
    303 		(void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
    304 	}
    305 	(void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
    306 
    307 	while ((ch = fgetc(sys_file)) != EOF)
    308 		fputc(ch, out_file);
    309 
    310 	/* __menu_initerror: initscr failed. */
    311 	(void) fprintf (out_file,
    312 		"/* __menu_initerror: initscr failed. */\n"
    313 		"void __menu_initerror (void) {\n");
    314 	if (error_act.code == NULL) {
    315 		(void) fprintf (out_file,
    316 			"\t(void) fprintf (stderr, "
    317 				"\"Could not initialize curses\\n\");\n"
    318 			"\texit(1);\n"
    319 			"}\n");
    320 	} else {
    321 		if (error_act.endwin)
    322 			(void) fprintf (out_file, "\tendwin();\n");
    323 		(void) fprintf (out_file, "%s\n}\n", error_act.code);
    324 	}
    325 
    326 	fclose (out_file);
    327 	fclose (sys_file);
    328 }
    329