xref: /netbsd-src/usr.bin/msgc/msgdb.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: msgdb.c,v 1.2 1997/10/03 16:37:25 enami 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 - message database manipulation */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #include "defs.h"
46 
47 static struct id_rec *head = NULL, *tail = NULL;
48 static int msg_no = 0;
49 static int maxstr = 1;
50 
51 void define_msg (char *name, char *value)
52 {
53 	int vlen = strlen(value);
54 	struct id_rec *tmp = (struct id_rec *)malloc(sizeof(struct id_rec));
55 
56 	if (find_id (root, name))
57 		yyerror ("%s is defined twice", name);
58 
59 	tmp->id     = name;
60 	tmp->msg    = value;
61 	tmp->msg_no = msg_no++;
62 	tmp->next   = NULL;
63 	if (tail == NULL)
64 		head = tail = tmp;
65 	else {
66 		tail->next = tmp;
67 		tail = tmp;
68 	}
69 
70 	insert_id (&root, tmp);
71 
72 	if (vlen > maxstr)
73 		maxstr = vlen;
74 }
75 
76 static void write_str (FILE *f, char *str)
77 {
78 	(void)fprintf (f, "\"");
79 	while (*str) {
80 		if (*str == '\n')
81 			(void) fprintf (f, "\\n\"\n\""), str++;
82 		else if (*str == '"')
83 			(void) fprintf (f, "\\\""), str++;
84 		else
85 			(void) fprintf (f, "%c", *str++);
86 	}
87 	(void)fprintf (f, "\",");
88 }
89 
90 /* Write out the msg files. */
91 void
92 write_msg_file ()
93 {
94 	FILE *out_file;
95 	FILE *sys_file;
96 	char hname[1024];
97 	char cname[1024];
98 	char sname[1024];
99 	char *sys_prefix;
100 
101 	int nlen;
102 	int ch;
103 
104 	struct id_rec *t;
105 
106 	/* Generate file names */
107 	snprintf (hname, 1024, "%s.h", out_name);
108 	nlen = strlen(hname);
109 	if (hname[nlen-2] != '.' || hname[nlen-1] != 'h') {
110 		(void) fprintf (stderr, "%s: name `%s` too long.\n",
111 				prog_name, out_name);
112 		exit(1);
113 	}
114 	snprintf (cname, 1024, "%s.c", out_name);
115 
116 	/* Open the msg_sys file first. */
117 	sys_prefix = getenv ("MSGDEF");
118 	if (sys_prefix == NULL)
119 		sys_prefix = "/usr/share/misc";
120 	snprintf (sname, 1024, "%s/%s", sys_prefix, sys_name);
121 	sys_file = fopen (sname, "r");
122 	if (sys_file == NULL) {
123 		(void) fprintf (stderr, "%s: could not open %s.\n",
124 				prog_name, sname);
125 		exit (1);
126 	}
127 
128 	/* Output the .h file first. */
129 	out_file = fopen (hname, "w");
130 	if (out_file == NULL) {
131 		(void) fprintf (stderr, "%s: could not open %s.\n",
132 				prog_name, hname);
133 		exit (1);
134 	}
135 
136 	/* Write it */
137 	(void) fprintf (out_file, "%s",
138 		"/* msg system definitions. */\n"
139 		"\n"
140 		"#ifndef MSG_DEFS_H\n"
141 		"#define MSG_DEFS_H\n"
142 		"#include <stdio.h>\n"
143 		"#include <string.h>\n"
144 		"#include <ctype.h>\n"
145 		"#include <stdarg.h>\n"
146 		"#include <curses.h>\n"
147 		"\n"
148 		"/* Prototypes */\n"
149 		"void beep(void);\n"
150 		"void msg_window(WINDOW *window);\n"
151 		"char *msg_string (int msg_no);\n"
152 		"void msg_clear(void);\n"
153 		"void msg_standout(void);\n"
154 		"void msg_standend(void);\n"
155 		"void msg_display(int msg_no,...);\n"
156 		"void msg_display_add(int msg_no,...);\n"
157 		"int  msg_vprintf (char *fmt, va_list ap);\n"
158 		"int  msg_printf (char *fmt, ...);\n"
159 		"int  msg_printf_add (char *fmt, ...);\n"
160 		"void msg_prompt_str (char *msg, char *def, char *val,"
161 			" int max_chars, ...);\n"
162 		"void msg_prompt (int msg_no, char *def, char *val,"
163 			" int max_chars, ...);\n"
164 		"void msg_prompt_addstr (char *msg, char *def, char *val,"
165 			"int max_chars, ...);\n"
166 		"void msg_prompt_add (int msg_no, char *def, char *val,"
167 			" int max_chars, ...);\n"
168 		"void msg_echo (void);\n"
169 		"void msg_noecho (void);\n"
170 		"\n"
171 		"/* Message names */\n"
172 	      );
173 	for (t=head; t != NULL; t = t->next) {
174 		(void) fprintf (out_file, "#define MSG_%s\t%d\n",
175 				t->id, t->msg_no);
176 	}
177 	(void) fprintf (out_file, "#define MAXSTR %d\n\n#endif\n", 2*maxstr);
178 
179 	fclose (out_file);
180 
181 	/* Now the C file */
182 	out_file = fopen (cname, "w");
183 	if (out_file == NULL) {
184 		(void) fprintf (stderr, "%s: could not open %s.\n",
185 				prog_name, cname);
186 		exit (1);
187 	}
188 
189 	/* hfile include ... */
190 	(void)fprintf (out_file, "#include \"%s\"\n", hname);
191 
192 	/* msg_list */
193 	(void)fprintf (out_file, "char *msg_list[] = {\n");
194 	for (t=head ; t != NULL; t = t->next)
195 		write_str (out_file, t->msg);
196 	(void)fprintf (out_file, "NULL};\n");
197 
198 	/* sys file out! */
199 	while ((ch = fgetc(sys_file)) != EOF)
200 		fputc(ch, out_file);
201 
202 	fclose (out_file);
203 	fclose (sys_file);
204 }
205