xref: /minix3/usr.bin/msgc/msgdb.c (revision d44a5ed1c177718aebe860f2bd736ea12fb19804)
1*d44a5ed1SThomas Cort /*	$NetBSD: msgdb.c,v 1.23 2012/03/06 16:26:01 mbalmer Exp $	*/
2*d44a5ed1SThomas Cort 
3*d44a5ed1SThomas Cort /*
4*d44a5ed1SThomas Cort  * Copyright 1997 Piermont Information Systems Inc.
5*d44a5ed1SThomas Cort  * All rights reserved.
6*d44a5ed1SThomas Cort  *
7*d44a5ed1SThomas Cort  * Written by Philip A. Nelson for Piermont Information Systems Inc.
8*d44a5ed1SThomas Cort  *
9*d44a5ed1SThomas Cort  * Redistribution and use in source and binary forms, with or without
10*d44a5ed1SThomas Cort  * modification, are permitted provided that the following conditions
11*d44a5ed1SThomas Cort  * are met:
12*d44a5ed1SThomas Cort  * 1. Redistributions of source code must retain the above copyright
13*d44a5ed1SThomas Cort  *    notice, this list of conditions and the following disclaimer.
14*d44a5ed1SThomas Cort  * 2. Redistributions in binary form must reproduce the above copyright
15*d44a5ed1SThomas Cort  *    notice, this list of conditions and the following disclaimer in the
16*d44a5ed1SThomas Cort  *    documentation and/or other materials provided with the distribution.
17*d44a5ed1SThomas Cort  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18*d44a5ed1SThomas Cort  *    or promote products derived from this software without specific prior
19*d44a5ed1SThomas Cort  *    written permission.
20*d44a5ed1SThomas Cort  *
21*d44a5ed1SThomas Cort  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22*d44a5ed1SThomas Cort  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*d44a5ed1SThomas Cort  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*d44a5ed1SThomas Cort  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25*d44a5ed1SThomas Cort  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*d44a5ed1SThomas Cort  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*d44a5ed1SThomas Cort  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*d44a5ed1SThomas Cort  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*d44a5ed1SThomas Cort  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*d44a5ed1SThomas Cort  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31*d44a5ed1SThomas Cort  * THE POSSIBILITY OF SUCH DAMAGE.
32*d44a5ed1SThomas Cort  *
33*d44a5ed1SThomas Cort  */
34*d44a5ed1SThomas Cort 
35*d44a5ed1SThomas Cort /* mdb.c - message database manipulation */
36*d44a5ed1SThomas Cort 
37*d44a5ed1SThomas Cort #if HAVE_NBTOOL_CONFIG_H
38*d44a5ed1SThomas Cort #include "nbtool_config.h"
39*d44a5ed1SThomas Cort #endif
40*d44a5ed1SThomas Cort 
41*d44a5ed1SThomas Cort #include <sys/cdefs.h>
42*d44a5ed1SThomas Cort 
43*d44a5ed1SThomas Cort #if defined(__RCSID) && !defined(lint)
44*d44a5ed1SThomas Cort __RCSID("$NetBSD: msgdb.c,v 1.23 2012/03/06 16:26:01 mbalmer Exp $");
45*d44a5ed1SThomas Cort #endif
46*d44a5ed1SThomas Cort 
47*d44a5ed1SThomas Cort 
48*d44a5ed1SThomas Cort #include <stdio.h>
49*d44a5ed1SThomas Cort #include <stdlib.h>
50*d44a5ed1SThomas Cort #include <string.h>
51*d44a5ed1SThomas Cort 
52*d44a5ed1SThomas Cort #include "defs.h"
53*d44a5ed1SThomas Cort #include "pathnames.h"
54*d44a5ed1SThomas Cort 
55*d44a5ed1SThomas Cort static struct id_rec *head = NULL, *tail = NULL;
56*d44a5ed1SThomas Cort static int msg_no = 1;
57*d44a5ed1SThomas Cort 
define_msg(char * name,char * value)58*d44a5ed1SThomas Cort void define_msg (char *name, char *value)
59*d44a5ed1SThomas Cort {
60*d44a5ed1SThomas Cort 	struct id_rec *tmp = (struct id_rec *)malloc(sizeof(struct id_rec));
61*d44a5ed1SThomas Cort 
62*d44a5ed1SThomas Cort 	if (find_id (root, name))
63*d44a5ed1SThomas Cort 		yyerror ("%s is defined twice", name);
64*d44a5ed1SThomas Cort 
65*d44a5ed1SThomas Cort 	tmp->id     = name;
66*d44a5ed1SThomas Cort 	tmp->msg    = value;
67*d44a5ed1SThomas Cort 	tmp->msg_no = msg_no++;
68*d44a5ed1SThomas Cort 	tmp->next   = NULL;
69*d44a5ed1SThomas Cort 	if (tail == NULL)
70*d44a5ed1SThomas Cort 		head = tail = tmp;
71*d44a5ed1SThomas Cort 	else {
72*d44a5ed1SThomas Cort 		tail->next = tmp;
73*d44a5ed1SThomas Cort 		tail = tmp;
74*d44a5ed1SThomas Cort 	}
75*d44a5ed1SThomas Cort 
76*d44a5ed1SThomas Cort 	insert_id (&root, tmp);
77*d44a5ed1SThomas Cort }
78*d44a5ed1SThomas Cort 
write_str(FILE * f,char * str)79*d44a5ed1SThomas Cort static void write_str (FILE *f, char *str)
80*d44a5ed1SThomas Cort {
81*d44a5ed1SThomas Cort 	(void)fprintf (f, "\"");
82*d44a5ed1SThomas Cort 	while (*str) {
83*d44a5ed1SThomas Cort 		if (*str == '\n')
84*d44a5ed1SThomas Cort 			(void) fprintf (f, "\\n\"\n\""), str++;
85*d44a5ed1SThomas Cort 		else if (*str == '"')
86*d44a5ed1SThomas Cort 			(void) fprintf (f, "\\\""), str++;
87*d44a5ed1SThomas Cort 		else
88*d44a5ed1SThomas Cort 			(void) fprintf (f, "%c", *str++);
89*d44a5ed1SThomas Cort 	}
90*d44a5ed1SThomas Cort 	(void)fprintf (f, "\",");
91*d44a5ed1SThomas Cort }
92*d44a5ed1SThomas Cort 
93*d44a5ed1SThomas Cort /* Write out the msg files. */
94*d44a5ed1SThomas Cort void
write_msg_file()95*d44a5ed1SThomas Cort write_msg_file ()
96*d44a5ed1SThomas Cort {
97*d44a5ed1SThomas Cort 	FILE *out_file;
98*d44a5ed1SThomas Cort 	FILE *sys_file;
99*d44a5ed1SThomas Cort 	char hname[1024];
100*d44a5ed1SThomas Cort 	char cname[1024];
101*d44a5ed1SThomas Cort 	char sname[1024];
102*d44a5ed1SThomas Cort 	char *sys_prefix;
103*d44a5ed1SThomas Cort 
104*d44a5ed1SThomas Cort 	int nlen;
105*d44a5ed1SThomas Cort 	int ch;
106*d44a5ed1SThomas Cort 
107*d44a5ed1SThomas Cort 	struct id_rec *t;
108*d44a5ed1SThomas Cort 
109*d44a5ed1SThomas Cort 	/* Generate file names */
110*d44a5ed1SThomas Cort 	snprintf (hname, 1024, "%s.h", out_name);
111*d44a5ed1SThomas Cort 	nlen = strlen(hname);
112*d44a5ed1SThomas Cort 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
113*d44a5ed1SThomas Cort 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
114*d44a5ed1SThomas Cort 				prog_name, out_name);
115*d44a5ed1SThomas Cort 		exit(1);
116*d44a5ed1SThomas Cort 	}
117*d44a5ed1SThomas Cort 	snprintf (cname, 1024, "%s.c", out_name);
118*d44a5ed1SThomas Cort 
119*d44a5ed1SThomas Cort 	/* Open the msg_sys file first. */
120*d44a5ed1SThomas Cort 	sys_prefix = getenv ("MSGDEF");
121*d44a5ed1SThomas Cort 	if (sys_prefix == NULL)
122*d44a5ed1SThomas Cort 		sys_prefix = _PATH_DEFSYSPREFIX;
123*d44a5ed1SThomas Cort 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
124*d44a5ed1SThomas Cort 	sys_file = fopen (sname, "r");
125*d44a5ed1SThomas Cort 	if (sys_file == NULL) {
126*d44a5ed1SThomas Cort 		(void) fprintf (stderr, "%s: could not open %s.\n",
127*d44a5ed1SThomas Cort 				prog_name, sname);
128*d44a5ed1SThomas Cort 		exit (1);
129*d44a5ed1SThomas Cort 	}
130*d44a5ed1SThomas Cort 
131*d44a5ed1SThomas Cort 	/* Output the .h file first. */
132*d44a5ed1SThomas Cort 	out_file = fopen (hname, "w");
133*d44a5ed1SThomas Cort 	if (out_file == NULL) {
134*d44a5ed1SThomas Cort 		(void) fprintf (stderr, "%s: could not open %s.\n",
135*d44a5ed1SThomas Cort 				prog_name, hname);
136*d44a5ed1SThomas Cort 		exit (1);
137*d44a5ed1SThomas Cort 	}
138*d44a5ed1SThomas Cort 
139*d44a5ed1SThomas Cort 	/* Write it */
140*d44a5ed1SThomas Cort 	(void) fprintf (out_file, "%s",
141*d44a5ed1SThomas Cort 		"/* msg system definitions. */\n"
142*d44a5ed1SThomas Cort 		"\n"
143*d44a5ed1SThomas Cort 		"#ifndef MSG_DEFS_H\n"
144*d44a5ed1SThomas Cort 		"#define MSG_DEFS_H\n"
145*d44a5ed1SThomas Cort 		"#include <stdio.h>\n"
146*d44a5ed1SThomas Cort 		"#include <stdlib.h>\n"
147*d44a5ed1SThomas Cort 		"#include <unistd.h>\n"
148*d44a5ed1SThomas Cort 		"#include <fcntl.h>\n"
149*d44a5ed1SThomas Cort 		"#include <string.h>\n"
150*d44a5ed1SThomas Cort 		"#include <ctype.h>\n"
151*d44a5ed1SThomas Cort 		"#include <stdarg.h>\n"
152*d44a5ed1SThomas Cort 		"#include <stdint.h>\n"
153*d44a5ed1SThomas Cort 		"#include <curses.h>\n"
154*d44a5ed1SThomas Cort 		"#include <sys/mman.h>\n"
155*d44a5ed1SThomas Cort 		"\n"
156*d44a5ed1SThomas Cort 		"typedef const char *msg;\n"
157*d44a5ed1SThomas Cort 		"\n"
158*d44a5ed1SThomas Cort 		"/* Prototypes */\n"
159*d44a5ed1SThomas Cort 		"WINDOW *msg_window(WINDOW *window);\n"
160*d44a5ed1SThomas Cort 		"const char *msg_string(msg msg_no);\n"
161*d44a5ed1SThomas Cort 		"int msg_file(const char *);\n"
162*d44a5ed1SThomas Cort 		"void msg_clear(void);\n"
163*d44a5ed1SThomas Cort 		"void msg_standout(void);\n"
164*d44a5ed1SThomas Cort 		"void msg_standend(void);\n"
165*d44a5ed1SThomas Cort 		"void msg_display(msg msg_no,...);\n"
166*d44a5ed1SThomas Cort 		"void msg_display_add(msg msg_no,...);\n"
167*d44a5ed1SThomas Cort 		"void msg_printf(const char *fmt, ...) __printflike(1, 2);\n"
168*d44a5ed1SThomas Cort 		"void msg_prompt (msg msg_no, const char *def,"
169*d44a5ed1SThomas Cort 			" char *val, size_t max_chars, ...);\n"
170*d44a5ed1SThomas Cort 		"void msg_prompt_add (msg msg_no, const char *def,"
171*d44a5ed1SThomas Cort 			" char *val, size_t max_chars, ...);\n"
172*d44a5ed1SThomas Cort 		"void msg_prompt_noecho (msg msg_no, const char *def,"
173*d44a5ed1SThomas Cort 			" char *val, size_t max_chars, ...);\n"
174*d44a5ed1SThomas Cort 		"void msg_prompt_win (msg, int, int, int, int,"
175*d44a5ed1SThomas Cort 			" const char *, char *, size_t, ...);\n"
176*d44a5ed1SThomas Cort 		"void msg_table_add(msg msg_no,...);\n"
177*d44a5ed1SThomas Cort 		"int msg_row(void);\n"
178*d44a5ed1SThomas Cort 		"\n"
179*d44a5ed1SThomas Cort 		"/* Message names */\n"
180*d44a5ed1SThomas Cort 	      );
181*d44a5ed1SThomas Cort 	(void) fprintf (out_file, "#define MSG_NONE\tNULL\n");
182*d44a5ed1SThomas Cort 	for (t=head; t != NULL; t = t->next) {
183*d44a5ed1SThomas Cort 		(void) fprintf (out_file, "#define MSG_%s\t((msg)(long)%d)\n",
184*d44a5ed1SThomas Cort 				t->id, t->msg_no);
185*d44a5ed1SThomas Cort 	}
186*d44a5ed1SThomas Cort 	(void) fprintf (out_file, "\n#endif\n");
187*d44a5ed1SThomas Cort 
188*d44a5ed1SThomas Cort 	fclose (out_file);
189*d44a5ed1SThomas Cort 
190*d44a5ed1SThomas Cort 	/* Now the C file */
191*d44a5ed1SThomas Cort 	out_file = fopen (cname, "w");
192*d44a5ed1SThomas Cort 	if (out_file == NULL) {
193*d44a5ed1SThomas Cort 		(void) fprintf (stderr, "%s: could not open %s.\n",
194*d44a5ed1SThomas Cort 				prog_name, cname);
195*d44a5ed1SThomas Cort 		exit (1);
196*d44a5ed1SThomas Cort 	}
197*d44a5ed1SThomas Cort 
198*d44a5ed1SThomas Cort 	/* hfile include ... */
199*d44a5ed1SThomas Cort 	(void)fprintf (out_file, "#include \"%s\"\n", hname);
200*d44a5ed1SThomas Cort 
201*d44a5ed1SThomas Cort 	/* msg_list */
202*d44a5ed1SThomas Cort 	(void)fprintf (out_file, "const char *msg_list[] = {\nNULL,\n");
203*d44a5ed1SThomas Cort 	for (t=head ; t != NULL; t = t->next)
204*d44a5ed1SThomas Cort 		write_str (out_file, t->msg);
205*d44a5ed1SThomas Cort 	(void)fprintf (out_file, "NULL};\n");
206*d44a5ed1SThomas Cort 
207*d44a5ed1SThomas Cort 	/* sys file out! */
208*d44a5ed1SThomas Cort 	while ((ch = fgetc(sys_file)) != EOF)
209*d44a5ed1SThomas Cort 		fputc(ch, out_file);
210*d44a5ed1SThomas Cort 
211*d44a5ed1SThomas Cort 	fclose (out_file);
212*d44a5ed1SThomas Cort 	fclose (sys_file);
213*d44a5ed1SThomas Cort }
214