2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)mkstr.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/mkstr/mkstr.c,v 1.4.2.1 2002/11/16 01:07:42 tjr Exp $
40 #define ungetchar(c) ungetc(c, stdin)
43 * mkstr - create a string error message file by massaging C source
45 * Bill Joy UCB August 1977
47 * Modified March 1978 to hash old messages to be able to recompile
48 * without addding messages to the message file (usually)
50 * Based on an earlier program conceived by Bill Joy and Chuck Haley
52 * Program to create a string error message file
53 * from a group of C programs. Arguments are the name
54 * of the file where the strings are to be placed, the
55 * prefix of the new files where the processed source text
56 * is to be placed, and the files to be processed.
58 * The program looks for 'error("' in the source stream.
59 * Whenever it finds this, the following characters from the '"'
60 * to a '"' are replaced by 'seekpt' where seekpt is a
61 * pointer into the error message file.
62 * If the '(' is not immediately followed by a '"' no change occurs.
64 * The optional '-' causes strings to be added at the end of the
65 * existing error message file for recompilation of single routines.
68 FILE *mesgread
, *mesgwrite
;
72 int fgetNUL(char *, int, FILE *);
73 unsigned hashit(char *, int, unsigned);
75 int match(const char *);
81 main(int argc
, char *argv
[])
87 if (argc
> 1 && argv
[0][0] == '-')
88 addon
++, argc
--, argv
++;
91 mesgwrite
= fopen(argv
[0], addon
? "a" : "w");
92 if (mesgwrite
== NULL
)
93 err(1, "%s", argv
[0]);
94 mesgread
= fopen(argv
[0], "r");
96 err(1, "%s", argv
[0]);
99 namelen
= strlcpy(name
, argv
[0], sizeof(name
));
100 if (namelen
>= sizeof(name
)) {
101 errno
= ENAMETOOLONG
;
102 err(1, "%s", argv
[0]);
107 if (strlcpy(np
, argv
[0], sizeof(name
) - namelen
) >=
108 sizeof(name
) - namelen
) {
109 errno
= ENAMETOOLONG
;
110 err(1, "%s%s", name
, argv
[0]);
112 if (freopen(name
, "w", stdout
) == NULL
)
114 if (freopen(argv
[0], "r", stdin
) == NULL
)
115 err(1, "%s", argv
[0]);
125 fprintf(stderr
, "usage: mkstr [-] mesgfile prefix file ...\n");
142 if (match("error(")) {
154 match(const char *ocp
)
159 for (cp
= ocp
+ 1; *cp
; cp
++) {
179 if (cp
== buf
+ sizeof(buf
) - 2)
180 errx(1, "message too long");
222 c
<<= 7, c
+= ch
- '0';
226 c
<<= 3, c
+= ch
- '0', ch
= -1;
234 printf("%d", hashit(buf
, 1, 0));
241 return (c
>= '0' && c
<= '7');
251 while (fgetNUL(buf
, sizeof buf
, mesgread
) != 0) {
252 hashit(buf
, 0, mesgpt
);
253 mesgpt
+= strlen(buf
) + 2;
266 hashit(char *str
, int really
, unsigned fakept
)
277 hashval
= (hashval
<< 1) + *cp
++;
278 i
= hashval
% NBUCKETS
;
282 for (hp
= bucket
[i
]; hp
!= NULL
; hp
= hp
->hnext
)
283 if (hp
->hval
== hashval
) {
284 fseek(mesgread
, (long) hp
->hpt
, 0);
285 fgetNUL(buf
, sizeof buf
, mesgread
);
287 fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
289 if (strcmp(buf
, str
) == 0)
292 if (!really
|| hp
== NULL
) {
293 hp
= (struct hash
*) calloc(1, sizeof *hp
);
296 hp
->hnext
= bucket
[i
];
298 hp
->hpt
= really
? ftell(mesgwrite
) : fakept
;
300 fwrite(str
, sizeof (char), strlen(str
) + 1, mesgwrite
);
301 fwrite("\n", sizeof (char), 1, mesgwrite
);
306 fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
312 fgetNUL(char *obuf
, int rmdr
, FILE *file
)
317 while (--rmdr
> 0 && (c
= getc(file
)) != 0 && c
!= EOF
)
321 return ((feof(file
) || ferror(file
)) ? 0 : 1);