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