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