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