xref: /netbsd-src/usr.bin/msgc/msgdb.c (revision 0e1db6443345f97b2e756c31d30ae66afe804cbc)
1*0e1db644Schristos /*	$NetBSD: msgdb.c,v 1.24 2019/06/20 00:45:18 christos Exp $	*/
2584c2298Sphil 
3584c2298Sphil /*
4584c2298Sphil  * Copyright 1997 Piermont Information Systems Inc.
5584c2298Sphil  * All rights reserved.
6584c2298Sphil  *
7584c2298Sphil  * Written by Philip A. Nelson for Piermont Information Systems Inc.
8584c2298Sphil  *
9584c2298Sphil  * Redistribution and use in source and binary forms, with or without
10584c2298Sphil  * modification, are permitted provided that the following conditions
11584c2298Sphil  * are met:
12584c2298Sphil  * 1. Redistributions of source code must retain the above copyright
13584c2298Sphil  *    notice, this list of conditions and the following disclaimer.
14584c2298Sphil  * 2. Redistributions in binary form must reproduce the above copyright
15584c2298Sphil  *    notice, this list of conditions and the following disclaimer in the
16584c2298Sphil  *    documentation and/or other materials provided with the distribution.
1740c86e8bSmbalmer  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18584c2298Sphil  *    or promote products derived from this software without specific prior
19584c2298Sphil  *    written permission.
20584c2298Sphil  *
21584c2298Sphil  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22584c2298Sphil  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23584c2298Sphil  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24584c2298Sphil  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25584c2298Sphil  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26584c2298Sphil  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27584c2298Sphil  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28584c2298Sphil  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29584c2298Sphil  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30584c2298Sphil  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31584c2298Sphil  * THE POSSIBILITY OF SUCH DAMAGE.
32584c2298Sphil  *
33584c2298Sphil  */
34584c2298Sphil 
35584c2298Sphil /* mdb.c - message database manipulation */
36584c2298Sphil 
37b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
38b2f78261Sjmc #include "nbtool_config.h"
39b2f78261Sjmc #endif
40b2f78261Sjmc 
41a8d6388eSagc #include <sys/cdefs.h>
42a8d6388eSagc 
43abcf838dSlukem #if defined(__RCSID) && !defined(lint)
44*0e1db644Schristos __RCSID("$NetBSD: msgdb.c,v 1.24 2019/06/20 00:45:18 christos Exp $");
45a8d6388eSagc #endif
46a8d6388eSagc 
47a8d6388eSagc 
48584c2298Sphil #include <stdio.h>
49584c2298Sphil #include <stdlib.h>
505e13aa5aSenami #include <string.h>
515e13aa5aSenami 
52584c2298Sphil #include "defs.h"
5392fa2601Sbjh21 #include "pathnames.h"
54584c2298Sphil 
55584c2298Sphil static struct id_rec *head = NULL, *tail = NULL;
5662a063f9Sdsl static int msg_no = 1;
57584c2298Sphil 
define_msg(char * name,char * value)58584c2298Sphil void define_msg (char *name, char *value)
59584c2298Sphil {
60584c2298Sphil 	struct id_rec *tmp = (struct id_rec *)malloc(sizeof(struct id_rec));
61584c2298Sphil 
62584c2298Sphil 	if (find_id (root, name))
63584c2298Sphil 		yyerror ("%s is defined twice", name);
64584c2298Sphil 
65584c2298Sphil 	tmp->id     = name;
66584c2298Sphil 	tmp->msg    = value;
67584c2298Sphil 	tmp->msg_no = msg_no++;
68584c2298Sphil 	tmp->next   = NULL;
69584c2298Sphil 	if (tail == NULL)
70584c2298Sphil 		head = tail = tmp;
71584c2298Sphil 	else {
72584c2298Sphil 		tail->next = tmp;
73584c2298Sphil 		tail = tmp;
74584c2298Sphil 	}
75584c2298Sphil 
76584c2298Sphil 	insert_id (&root, tmp);
77584c2298Sphil }
78584c2298Sphil 
write_str(FILE * f,char * str)79584c2298Sphil static void write_str (FILE *f, char *str)
80584c2298Sphil {
81584c2298Sphil 	(void)fprintf (f, "\"");
82584c2298Sphil 	while (*str) {
83584c2298Sphil 		if (*str == '\n')
84584c2298Sphil 			(void) fprintf (f, "\\n\"\n\""), str++;
85584c2298Sphil 		else if (*str == '"')
86584c2298Sphil 			(void) fprintf (f, "\\\""), str++;
87584c2298Sphil 		else
88584c2298Sphil 			(void) fprintf (f, "%c", *str++);
89584c2298Sphil 	}
90584c2298Sphil 	(void)fprintf (f, "\",");
91584c2298Sphil }
92584c2298Sphil 
93584c2298Sphil /* Write out the msg files. */
94584c2298Sphil void
write_msg_file()95584c2298Sphil write_msg_file ()
96584c2298Sphil {
97584c2298Sphil 	FILE *out_file;
98584c2298Sphil 	FILE *sys_file;
99584c2298Sphil 	char hname[1024];
100584c2298Sphil 	char cname[1024];
101584c2298Sphil 	char sname[1024];
102584c2298Sphil 	char *sys_prefix;
103584c2298Sphil 
104584c2298Sphil 	int nlen;
105584c2298Sphil 	int ch;
106584c2298Sphil 
107584c2298Sphil 	struct id_rec *t;
108584c2298Sphil 
109584c2298Sphil 	/* Generate file names */
110584c2298Sphil 	snprintf (hname, 1024, "%s.h", out_name);
111584c2298Sphil 	nlen = strlen(hname);
112584c2298Sphil 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
113584c2298Sphil 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
114584c2298Sphil 				prog_name, out_name);
115584c2298Sphil 		exit(1);
116584c2298Sphil 	}
117584c2298Sphil 	snprintf (cname, 1024, "%s.c", out_name);
118584c2298Sphil 
119584c2298Sphil 	/* Open the msg_sys file first. */
120584c2298Sphil 	sys_prefix = getenv ("MSGDEF");
121584c2298Sphil 	if (sys_prefix == NULL)
12292fa2601Sbjh21 		sys_prefix = _PATH_DEFSYSPREFIX;
123584c2298Sphil 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
124584c2298Sphil 	sys_file = fopen (sname, "r");
125584c2298Sphil 	if (sys_file == NULL) {
126584c2298Sphil 		(void) fprintf (stderr, "%s: could not open %s.\n",
127584c2298Sphil 				prog_name, sname);
128584c2298Sphil 		exit (1);
129584c2298Sphil 	}
130584c2298Sphil 
131584c2298Sphil 	/* Output the .h file first. */
132584c2298Sphil 	out_file = fopen (hname, "w");
133584c2298Sphil 	if (out_file == NULL) {
134584c2298Sphil 		(void) fprintf (stderr, "%s: could not open %s.\n",
135584c2298Sphil 				prog_name, hname);
136584c2298Sphil 		exit (1);
137584c2298Sphil 	}
138584c2298Sphil 
139584c2298Sphil 	/* Write it */
140584c2298Sphil 	(void) fprintf (out_file, "%s",
141584c2298Sphil 		"/* msg system definitions. */\n"
142584c2298Sphil 		"\n"
143584c2298Sphil 		"#ifndef MSG_DEFS_H\n"
144584c2298Sphil 		"#define MSG_DEFS_H\n"
145584c2298Sphil 		"#include <stdio.h>\n"
1464b1c7617Scgd 		"#include <stdlib.h>\n"
147e2724ab4Sdsl 		"#include <unistd.h>\n"
148e2724ab4Sdsl 		"#include <fcntl.h>\n"
149584c2298Sphil 		"#include <string.h>\n"
150584c2298Sphil 		"#include <ctype.h>\n"
151584c2298Sphil 		"#include <stdarg.h>\n"
152c3e2d898Schristos 		"#include <stdint.h>\n"
153584c2298Sphil 		"#include <curses.h>\n"
154e2724ab4Sdsl 		"#include <sys/mman.h>\n"
155584c2298Sphil 		"\n"
156a70b70f9Scgd 		"typedef const char *msg;\n"
157a70b70f9Scgd 		"\n"
158584c2298Sphil 		"/* Prototypes */\n"
15962a063f9Sdsl 		"WINDOW *msg_window(WINDOW *window);\n"
160a70b70f9Scgd 		"const char *msg_string(msg msg_no);\n"
161e2724ab4Sdsl 		"int msg_file(const char *);\n"
162584c2298Sphil 		"void msg_clear(void);\n"
163584c2298Sphil 		"void msg_standout(void);\n"
164584c2298Sphil 		"void msg_standend(void);\n"
165d13b2dd8Smbalmer 		"void msg_printf(const char *fmt, ...) __printflike(1, 2);\n"
166*0e1db644Schristos 		"void msg_display(msg msg_no);\n"
167*0e1db644Schristos 		"void msg_fmt_display(msg msg_no, const char *fmt, ...)"
168*0e1db644Schristos 			" __printflike(2, 3);\n"
169*0e1db644Schristos 
170*0e1db644Schristos 		"void msg_display_add(msg msg_no);\n"
171*0e1db644Schristos 		"void msg_fmt_display_add(msg msg_no, const char *fmt, ...);\n"
172*0e1db644Schristos 
173a70b70f9Scgd 		"void msg_prompt(msg msg_no, const char *def,"
174*0e1db644Schristos 			" char *val, size_t max_chars);\n"
175*0e1db644Schristos 		"void msg_fmt_prompt(msg msg_no, const char *def,"
176*0e1db644Schristos 			" char *val, size_t max_chars, const char *fmt, ...)"
177*0e1db644Schristos 			" __printflike(5, 6);\n"
178*0e1db644Schristos 
179a70b70f9Scgd 		"void msg_prompt_add(msg msg_no, const char *def,"
180*0e1db644Schristos 			" char *val, size_t max_chars);\n"
181*0e1db644Schristos 		"void msg_fmt_prompt_add(msg msg_no, const char *def,"
182*0e1db644Schristos 			" char *val, size_t max_chars, const char *fmt, ...)"
183*0e1db644Schristos 			" __printflike(5, 6);\n"
184*0e1db644Schristos 
185a70b70f9Scgd 		"void msg_prompt_noecho(msg msg_no, const char *def,"
186*0e1db644Schristos 			" char *val, size_t max_chars);\n"
187*0e1db644Schristos 		"void msg_fmt_prompt_noecho(msg msg_no, const char *def,"
188*0e1db644Schristos 			" char *val, size_t max_chars, const char *fmt, ...)"
189*0e1db644Schristos 			" __printflike(5, 6);\n"
190*0e1db644Schristos 
191*0e1db644Schristos 		"void msg_prompt_win(msg msg_no, int x, int y, int w,"
192*0e1db644Schristos 			" int h, const char *def, char *val,"
193*0e1db644Schristos 			" size_t max_chars);\n"
194*0e1db644Schristos 		"void msg_fmt_prompt_win(msg msg_no, int x, int y, int w,"
195*0e1db644Schristos 			" int h, const char *def, char *val, size_t max_chars,"
196*0e1db644Schristos 			" const char *fmt, ...)"
197*0e1db644Schristos 			" __printflike(9, 10);\n"
198*0e1db644Schristos 
199*0e1db644Schristos 		"void msg_table_add(msg msg_no);"
200*0e1db644Schristos 		"void msg_fmt_table_add(msg msg_no, const char *fmt, ...)"
201*0e1db644Schristos 			" __printflike(2, 3);\n"
202*0e1db644Schristos 
203556e606cSdsl 		"int msg_row(void);\n"
204584c2298Sphil 		"\n"
205584c2298Sphil 		"/* Message names */\n"
206584c2298Sphil 	      );
207a70b70f9Scgd 	(void) fprintf (out_file, "#define MSG_NONE\tNULL\n");
208584c2298Sphil 	for (t=head; t != NULL; t = t->next) {
209a70b70f9Scgd 		(void) fprintf (out_file, "#define MSG_%s\t((msg)(long)%d)\n",
210584c2298Sphil 				t->id, t->msg_no);
211584c2298Sphil 	}
21282ecc5c4Scgd 	(void) fprintf (out_file, "\n#endif\n");
213584c2298Sphil 
214584c2298Sphil 	fclose (out_file);
215584c2298Sphil 
216584c2298Sphil 	/* Now the C file */
217584c2298Sphil 	out_file = fopen (cname, "w");
218584c2298Sphil 	if (out_file == NULL) {
219584c2298Sphil 		(void) fprintf (stderr, "%s: could not open %s.\n",
220584c2298Sphil 				prog_name, cname);
221584c2298Sphil 		exit (1);
222584c2298Sphil 	}
223584c2298Sphil 
224584c2298Sphil 	/* hfile include ... */
225584c2298Sphil 	(void)fprintf (out_file, "#include \"%s\"\n", hname);
226584c2298Sphil 
227584c2298Sphil 	/* msg_list */
22862a063f9Sdsl 	(void)fprintf (out_file, "const char *msg_list[] = {\nNULL,\n");
229584c2298Sphil 	for (t=head ; t != NULL; t = t->next)
230584c2298Sphil 		write_str (out_file, t->msg);
231584c2298Sphil 	(void)fprintf (out_file, "NULL};\n");
232584c2298Sphil 
233584c2298Sphil 	/* sys file out! */
234584c2298Sphil 	while ((ch = fgetc(sys_file)) != EOF)
235584c2298Sphil 		fputc(ch, out_file);
236584c2298Sphil 
237584c2298Sphil 	fclose (out_file);
238584c2298Sphil 	fclose (sys_file);
239584c2298Sphil }
240