Home | History | Annotate | Line # | Download | only in menuc
mdb.c revision 1.1
      1 /*	$NetBSD: mdb.c,v 1.1 1997/09/26 17:54:09 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 		"\n"
    176 		"/* Menu names */\n"
    177 	      );
    178 	for (i=0; i<menu_no; i++) {
    179 		(void) fprintf (out_file, "#define MENU_%s\t%d\n",
    180 				menus[i]->id, i);
    181 	}
    182 	(void) fprintf (out_file, "\n#define MAX_STRLEN %d\n", max_strlen);
    183 	(void) fprintf (out_file, "#endif\n");
    184 
    185 	fclose (out_file);
    186 
    187 	/* Now the C file */
    188 	out_file = fopen (cname, "w");
    189 	if (out_file == NULL) {
    190 		(void) fprintf (stderr, "%s: could not open %s.\n",
    191 				prog_name, cname);
    192 		exit (1);
    193 	}
    194 
    195 	/* initial code */
    196 	fprintf (out_file, "#include \"%s\"\n\n", hname);
    197 	fprintf (out_file, "%s\n\n", initcode);
    198 
    199 	/* data definitions */
    200 
    201 	/* optstrX */
    202 	for (i=0; i<menu_no; i++) {
    203 		(void) fprintf (out_file, "static char *optstr%d[] = {\n", i);
    204 		toptn = menus[i]->info->optns;
    205 		opt_ch = 'a';
    206 		while (toptn != NULL) {
    207 			(void) fprintf (out_file, "\t\"%c: %s,\n", opt_ch++,
    208 					toptn->name+1);
    209 			toptn = toptn->next;
    210 		}
    211 		(void) fprintf (out_file, "\t(char *)NULL\n};\n\n");
    212 	}
    213 
    214 	/* menus */
    215 	(void) fprintf (out_file, "static struct menudesc menus[] = {\n");
    216 	for (i=0; i<menu_no; i++)
    217 		(void) fprintf (out_file,
    218 			"\t{%s,%d,%d,%d,%d,%d,%d,0,optstr%d,NULL},\n",
    219 			menus[i]->info->title, 	menus[i]->info->y,
    220 			menus[i]->info->x, menus[i]->info->h,
    221 			menus[i]->info->w, menus[i]->info->mopt,
    222 			menus[i]->info->numopt, i);
    223 	(void) fprintf (out_file, "{NULL}};\n\n");
    224 
    225 	/* num_menus */
    226 	(void) fprintf (out_file, "int num_menus = %d;\n\n", menu_no);
    227 
    228 	/* action code */
    229 	(void) fprintf (out_file, "%s",
    230 		"static int process_item (int *menu_no, int sel)\n"
    231 		"{\n"
    232 		"\tint retval = FALSE;\n"
    233 		"\n"
    234 		"\tswitch (*menu_no) {\n"
    235 	);
    236 	for (i=0; i<menu_no; i++) {
    237 		(void) fprintf (out_file, "\tcase MENU_%s:\n",
    238 				menus[i]->id);
    239 		(void) fprintf (out_file, "\t\tswitch (sel) {\n"
    240 				"\t\tcase -2:\n");
    241 		if (menus[i]->info->postact.endwin)
    242 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    243 					"\t\t__m_endwin = 1;\n");
    244 		if (strlen(menus[i]->info->postact.code))
    245 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    246 					menus[i]->info->postact.code);
    247 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    248 		(void) fprintf (out_file, "\t\tcase -1:\n");
    249 		if (menus[i]->info->exitact.endwin)
    250 			(void) fprintf (out_file, "\t\t\tendwin();\n"
    251 					"\t\t__m_endwin = 1;\n");
    252 		if (strlen(menus[i]->info->exitact.code))
    253 			(void) fprintf (out_file, "\t\t\t{%s}\n",
    254 					menus[i]->info->exitact.code);
    255 		(void) fprintf (out_file, "\t\t\tbreak;\n");
    256 		j = 0;
    257 		toptn = menus[i]->info->optns;
    258 		while (toptn != NULL) {
    259 			(void) fprintf (out_file, "\t\tcase %d:\n", j++);
    260 			if (toptn->optact.endwin)
    261 				(void) fprintf (out_file, "\t\t\tendwin();\n"
    262 						"\t\t__m_endwin = 1;\n");
    263 			if (strlen(toptn->optact.code))
    264 				(void) fprintf (out_file, "\t\t\t{%s}\n",
    265 						toptn->optact.code);
    266 			if (toptn->menu >= 0)
    267 				if (toptn->issub)
    268 					(void) fprintf (out_file,
    269 						"\t\t\tprocess_menu(%d);\n",
    270 						toptn->menu);
    271 				else
    272 					(void) fprintf (out_file,
    273 						"\t\t\t*menu_no = %d;\n",
    274 						toptn->menu);
    275 			if (toptn->doexit)
    276 				(void) fprintf (out_file,
    277 						"\t\t\tretval = TRUE;\n");
    278 			(void) fprintf (out_file, "\t\t\tbreak;\n");
    279 			toptn = toptn->next;
    280 		}
    281 
    282 		(void) fprintf (out_file, "\t\t}\n\t\tbreak;\n");
    283 	}
    284 	(void) fprintf (out_file, "\t}\n\t return retval;\n}\n\n");
    285 
    286 	while ((ch = fgetc(sys_file)) != EOF)
    287 		fputc(ch, out_file);
    288 
    289 	fclose (out_file);
    290 	fclose (sys_file);
    291 }
    292