msgdb.c revision 1.4 1 /* $NetBSD: msgdb.c,v 1.4 1999/06/19 19:25:10 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 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 <stdlib.h>\n"
144 "#include <string.h>\n"
145 "#include <ctype.h>\n"
146 "#include <stdarg.h>\n"
147 "#include <curses.h>\n"
148 "\n"
149 "/* Prototypes */\n"
150 "void msg_beep(void);\n"
151 "void msg_window(WINDOW *window);\n"
152 "char *msg_string (int msg_no);\n"
153 "void msg_clear(void);\n"
154 "void msg_standout(void);\n"
155 "void msg_standend(void);\n"
156 "void msg_display(int msg_no,...);\n"
157 "void msg_display_add(int msg_no,...);\n"
158 "int msg_vprintf (char *fmt, va_list ap);\n"
159 "int msg_printf (char *fmt, ...);\n"
160 "int msg_printf_add (char *fmt, ...);\n"
161 "void msg_prompt_str (char *msg, char *def, char *val,"
162 " int max_chars, ...);\n"
163 "void msg_prompt (int msg_no, char *def, char *val,"
164 " int max_chars, ...);\n"
165 "void msg_prompt_addstr (char *msg, char *def, char *val,"
166 "int max_chars, ...);\n"
167 "void msg_prompt_add (int msg_no, char *def, char *val,"
168 " int max_chars, ...);\n"
169 "void msg_echo (void);\n"
170 "void msg_noecho (void);\n"
171 "\n"
172 "/* Message names */\n"
173 );
174 for (t=head; t != NULL; t = t->next) {
175 (void) fprintf (out_file, "#define MSG_%s\t%d\n",
176 t->id, t->msg_no);
177 }
178 (void) fprintf (out_file, "#define MAXSTR %d\n\n#endif\n", 2*maxstr);
179
180 fclose (out_file);
181
182 /* Now the C file */
183 out_file = fopen (cname, "w");
184 if (out_file == NULL) {
185 (void) fprintf (stderr, "%s: could not open %s.\n",
186 prog_name, cname);
187 exit (1);
188 }
189
190 /* hfile include ... */
191 (void)fprintf (out_file, "#include \"%s\"\n", hname);
192
193 /* msg_list */
194 (void)fprintf (out_file, "char *msg_list[] = {\n");
195 for (t=head ; t != NULL; t = t->next)
196 write_str (out_file, t->msg);
197 (void)fprintf (out_file, "NULL};\n");
198
199 /* sys file out! */
200 while ((ch = fgetc(sys_file)) != EOF)
201 fputc(ch, out_file);
202
203 fclose (out_file);
204 fclose (sys_file);
205 }
206