1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/cdefs.h> 47 __FBSDID("$FreeBSD$"); 48 49 #include <err.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #define ungetchar(c) ungetc(c, stdin) 55 56 /* 57 * mkstr - create a string error message file by massaging C source 58 * 59 * Bill Joy UCB August 1977 60 * 61 * Modified March 1978 to hash old messages to be able to recompile 62 * without addding messages to the message file (usually) 63 * 64 * Based on an earlier program conceived by Bill Joy and Chuck Haley 65 * 66 * Program to create a string error message file 67 * from a group of C programs. Arguments are the name 68 * of the file where the strings are to be placed, the 69 * prefix of the new files where the processed source text 70 * is to be placed, and the files to be processed. 71 * 72 * The program looks for 'error("' in the source stream. 73 * Whenever it finds this, the following characters from the '"' 74 * to a '"' are replaced by 'seekpt' where seekpt is a 75 * pointer into the error message file. 76 * If the '(' is not immediately followed by a '"' no change occurs. 77 * 78 * The optional '-' causes strings to be added at the end of the 79 * existing error message file for recompilation of single routines. 80 */ 81 82 FILE *mesgread, *mesgwrite; 83 char name[100], *np; 84 85 void copystr(void); 86 int fgetNUL(char *, int, FILE *); 87 unsigned hashit(char *, int, unsigned); 88 void inithash(void); 89 int match(const char *); 90 int octdigit(char); 91 void process(void); 92 void usage(void); 93 94 int 95 main(int argc, char *argv[]) 96 { 97 char addon = 0; 98 99 argc--, argv++; 100 if (argc > 1 && argv[0][0] == '-') 101 addon++, argc--, argv++; 102 if (argc < 3) 103 usage(); 104 mesgwrite = fopen(argv[0], addon ? "a" : "w"); 105 if (mesgwrite == NULL) 106 err(1, "%s", argv[0]); 107 mesgread = fopen(argv[0], "r"); 108 if (mesgread == NULL) 109 err(1, "%s", argv[0]); 110 inithash(); 111 argc--, argv++; 112 strcpy(name, argv[0]); 113 np = name + strlen(name); 114 argc--, argv++; 115 do { 116 strcpy(np, argv[0]); 117 if (freopen(name, "w", stdout) == NULL) 118 err(1, "%s", name); 119 if (freopen(argv[0], "r", stdin) == NULL) 120 err(1, "%s", argv[0]); 121 process(); 122 argc--, argv++; 123 } while (argc > 0); 124 exit(0); 125 } 126 127 void 128 usage(void) 129 { 130 fprintf(stderr, "usage: mkstr [-] mesgfile prefix file ...\n"); 131 exit(1); 132 } 133 134 void 135 process(void) 136 { 137 int c; 138 139 for (;;) { 140 c = getchar(); 141 if (c == EOF) 142 return; 143 if (c != 'e') { 144 putchar(c); 145 continue; 146 } 147 if (match("error(")) { 148 printf("error("); 149 c = getchar(); 150 if (c != '"') 151 putchar(c); 152 else 153 copystr(); 154 } 155 } 156 } 157 158 int 159 match(const char *ocp) 160 { 161 const char *cp; 162 int c; 163 164 for (cp = ocp + 1; *cp; cp++) { 165 c = getchar(); 166 if (c != *cp) { 167 while (ocp < cp) 168 putchar(*ocp++); 169 ungetchar(c); 170 return (0); 171 } 172 } 173 return (1); 174 } 175 176 void 177 copystr(void) 178 { 179 int c, ch; 180 char buf[512]; 181 char *cp = buf; 182 183 for (;;) { 184 c = getchar(); 185 if (c == EOF) 186 break; 187 switch (c) { 188 189 case '"': 190 *cp++ = 0; 191 goto out; 192 case '\\': 193 c = getchar(); 194 switch (c) { 195 196 case 'b': 197 c = '\b'; 198 break; 199 case 't': 200 c = '\t'; 201 break; 202 case 'r': 203 c = '\r'; 204 break; 205 case 'n': 206 c = '\n'; 207 break; 208 case '\n': 209 continue; 210 case 'f': 211 c = '\f'; 212 break; 213 case '0': 214 c = 0; 215 break; 216 case '\\': 217 break; 218 default: 219 if (!octdigit(c)) 220 break; 221 c -= '0'; 222 ch = getchar(); 223 if (!octdigit(ch)) 224 break; 225 c <<= 7, c += ch - '0'; 226 ch = getchar(); 227 if (!octdigit(ch)) 228 break; 229 c <<= 3, c+= ch - '0', ch = -1; 230 break; 231 } 232 } 233 *cp++ = c; 234 } 235 out: 236 *cp = 0; 237 printf("%d", hashit(buf, 1, NULL)); 238 } 239 240 int 241 octdigit(char c) 242 { 243 244 return (c >= '0' && c <= '7'); 245 } 246 247 void 248 inithash(void) 249 { 250 char buf[512]; 251 int mesgpt = 0; 252 253 rewind(mesgread); 254 while (fgetNUL(buf, sizeof buf, mesgread) != 0) { 255 hashit(buf, 0, mesgpt); 256 mesgpt += strlen(buf) + 2; 257 } 258 } 259 260 #define NBUCKETS 511 261 262 struct hash { 263 long hval; 264 unsigned hpt; 265 struct hash *hnext; 266 } *bucket[NBUCKETS]; 267 268 unsigned 269 hashit(char *str, int really, unsigned fakept) 270 { 271 int i; 272 struct hash *hp; 273 char buf[512]; 274 long hashval = 0; 275 char *cp; 276 277 if (really) 278 fflush(mesgwrite); 279 for (cp = str; *cp;) 280 hashval = (hashval << 1) + *cp++; 281 i = hashval % NBUCKETS; 282 if (i < 0) 283 i += NBUCKETS; 284 if (really != 0) 285 for (hp = bucket[i]; hp != 0; hp = hp->hnext) 286 if (hp->hval == hashval) { 287 fseek(mesgread, (long) hp->hpt, 0); 288 fgetNUL(buf, sizeof buf, mesgread); 289 /* 290 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf); 291 */ 292 if (strcmp(buf, str) == 0) 293 break; 294 } 295 if (!really || hp == 0) { 296 hp = (struct hash *) calloc(1, sizeof *hp); 297 if (hp == NULL) 298 err(1, NULL); 299 hp->hnext = bucket[i]; 300 hp->hval = hashval; 301 hp->hpt = really ? ftell(mesgwrite) : fakept; 302 if (really) { 303 fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite); 304 fwrite("\n", sizeof (char), 1, mesgwrite); 305 } 306 bucket[i] = hp; 307 } 308 /* 309 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt); 310 */ 311 return (hp->hpt); 312 } 313 314 int 315 fgetNUL(char *obuf, int rmdr, FILE *file) 316 { 317 int c; 318 char *buf = obuf; 319 320 while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF) 321 *buf++ = c; 322 *buf++ = 0; 323 getc(file); 324 return ((feof(file) || ferror(file)) ? 0 : 1); 325 } 326