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