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