1 /* $NetBSD: mkstr.c,v 1.14 2009/04/13 00:41:28 lukem Exp $ */
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)mkstr.c 8.1 (Berkeley) 6/6/93";
42 __RCSID("$NetBSD: mkstr.c,v 1.14 2009/04/13 00:41:28 lukem Exp $");
50 #define ungetchar(c) ungetc(c, stdin)
53 * mkstr - create a string error message file by massaging C source
55 * Bill Joy UCB August 1977
57 * Modified March 1978 to hash old messages to be able to recompile
58 * without addding messages to the message file (usually)
60 * Based on an earlier program conceived by Bill Joy and Chuck Haley
62 * Program to create a string error message file
63 * from a group of C programs. Arguments are the name
64 * of the file where the strings are to be placed, the
65 * prefix of the new files where the processed source text
66 * is to be placed, and the files to be processed.
68 * The program looks for 'error("' in the source stream.
69 * Whenever it finds this, the following characters from the '"'
70 * to a '"' are replaced by 'seekpt' where seekpt is a
71 * pointer into the error message file.
72 * If the '(' is not immediately followed by a '"' no change occurs.
74 * The optional '-' causes strings to be added at the end of the
75 * existing error message file for recompilation of single routines.
79 FILE *mesgread
, *mesgwrite
;
81 const char usagestr
[] = "usage: %s [ - ] mesgfile prefix file ...\n";
85 int main(int, char **);
86 int match(const char *);
89 long hashit(const char *, char, long);
91 int fgetNUL(char *, int, FILE *);
94 main(int argc
, char *argv
[])
98 argc
--, progname
= *argv
++;
99 if (argc
> 1 && argv
[0][0] == '-')
100 addon
++, argc
--, argv
++;
102 fprintf(stderr
, usagestr
, progname
), exit(1);
103 mesgwrite
= fopen(argv
[0], addon
? "a" : "w");
104 if (mesgwrite
== NULL
)
105 perror(argv
[0]), exit(1);
106 mesgread
= fopen(argv
[0], "r");
107 if (mesgread
== NULL
)
108 perror(argv
[0]), exit(1);
111 strlcpy(name
, argv
[0], sizeof(name
));
112 np
= name
+ strlen(name
);
115 strlcpy(np
, argv
[0], sizeof(name
) - (np
- name
));
116 if (freopen(name
, "w", stdout
) == NULL
)
117 perror(name
), exit(1);
118 if (freopen(argv
[0], "r", stdin
) == NULL
)
119 perror(argv
[0]), exit(1);
139 if (match("error(")) {
151 match(const char *ocp
)
156 for (cp
= ocp
+ 1; *cp
; cp
++) {
217 c
<<= 7, c
+= ch
- '0';
221 c
<<= 3, c
+= ch
- '0', ch
= -1;
229 printf("%ld", hashit(buf
, 1, 0));
236 return (c
>= '0' && c
<= '7');
246 while (fgetNUL(buf
, sizeof buf
, mesgread
) != 0) {
247 hashit(buf
, 0, mesgpt
);
248 mesgpt
+= strlen(buf
) + 2;
261 hashit(const char *str
, char really
, long fakept
)
270 hp
= NULL
; /* XXX gcc */
275 hashval
= (hashval
<< 1) + *cp
++;
276 i
= hashval
% NBUCKETS
;
280 for (hp
= bucket
[i
]; hp
!= 0; hp
= hp
->hnext
)
281 if (hp
->hval
== hashval
) {
282 fseek(mesgread
, hp
->hpt
, 0);
283 fgetNUL(buf
, sizeof buf
, mesgread
);
285 fprintf(stderr, "Got (from %ld) %s\n", hp->hpt, buf);
287 if (strcmp(buf
, str
) == 0)
290 if (!really
|| hp
== 0) {
291 hp
= (struct hash
*) calloc(1, sizeof *hp
);
292 hp
->hnext
= bucket
[i
];
294 hp
->hpt
= really
? ftell(mesgwrite
) : fakept
;
296 fwrite(str
, sizeof (char), strlen(str
) + 1, mesgwrite
);
297 fwrite("\n", sizeof (char), 1, mesgwrite
);
302 fprintf(stderr, "%s hashed to %ld at %ld\n", str, hp->hval, hp->hpt);
307 #include <sys/types.h>
308 #include <sys/stat.h>
311 fgetNUL(char *obuf
, int rmdr
, FILE *file
)
316 while (--rmdr
> 0 && (c
= getc(file
)) != 0 && c
!= EOF
)
320 return ((feof(file
) || ferror(file
)) ? 0 : 1);