17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
2178b6ed60Scraigm */
2278b6ed60Scraigm
2378b6ed60Scraigm /*
24f22acdffSgbrunett * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate * mkstr - create a string error message file by massaging C source
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * Bill Joy UCB August 1977
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate * Modified March 1978 to hash old messages to be able to recompile
347c478bd9Sstevel@tonic-gate * without addding messages to the message file (usually)
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate * Based on an earlier program conceived by Bill Joy and Chuck Haley
377c478bd9Sstevel@tonic-gate *
387c478bd9Sstevel@tonic-gate * Program to create a string error message file
397c478bd9Sstevel@tonic-gate * from a group of C programs. Arguments are the name
407c478bd9Sstevel@tonic-gate * of the file where the strings are to be placed, the
417c478bd9Sstevel@tonic-gate * prefix of the new files where the processed source text
427c478bd9Sstevel@tonic-gate * is to be placed, and the files to be processed.
437c478bd9Sstevel@tonic-gate *
447c478bd9Sstevel@tonic-gate * The program looks for 'error("' in the source stream.
457c478bd9Sstevel@tonic-gate * Whenever it finds this, the following characters from the '"'
467c478bd9Sstevel@tonic-gate * to a '"' are replaced by 'seekpt' where seekpt is a
477c478bd9Sstevel@tonic-gate * pointer into the error message file.
487c478bd9Sstevel@tonic-gate * If the '(' is not immediately followed by a '"' no change occurs.
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * The optional '-' causes strings to be added at the end of the
517c478bd9Sstevel@tonic-gate * existing error message file for recompilation of single routines.
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate
5478b6ed60Scraigm #include <stdio.h>
55f22acdffSgbrunett #include <stdlib.h>
56f22acdffSgbrunett #include <string.h>
5778b6ed60Scraigm #include <locale.h>
58f22acdffSgbrunett #include <sys/param.h>
5978b6ed60Scraigm
6078b6ed60Scraigm #define ungetchar(c) ungetc(c, stdin)
6178b6ed60Scraigm
62f22acdffSgbrunett #define NBUCKETS 511
63f22acdffSgbrunett
64f22acdffSgbrunett static char usagestr[] = "usage: %s [ - ] mesgfile prefix file ...\n";
657c478bd9Sstevel@tonic-gate
66f22acdffSgbrunett static FILE *mesgread, *mesgwrite;
677c478bd9Sstevel@tonic-gate
68f22acdffSgbrunett static void process(void);
69f22acdffSgbrunett static int match(char *ocp);
70f22acdffSgbrunett static void copystr(void);
71f22acdffSgbrunett static int octdigit(char c);
72f22acdffSgbrunett static void inithash(void);
73f22acdffSgbrunett static int hashit(char *str, char really, unsigned int fakept);
74f22acdffSgbrunett static int fgetNUL(char *obuf, int rmdr, FILE *file);
7578b6ed60Scraigm
7678b6ed60Scraigm int
main(int argc,char * argv[])7778b6ed60Scraigm main(int argc, char *argv[])
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate char addon = 0;
80f22acdffSgbrunett char *progname, *np, name[MAXPATHLEN];
81f22acdffSgbrunett size_t size = 0;
82f22acdffSgbrunett size_t len;
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
87f22acdffSgbrunett #define TEXT_DOMAIN "SYS_TEST"
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate argc--, progname = *argv++;
927c478bd9Sstevel@tonic-gate if (argc > 1 && argv[0][0] == '-')
937c478bd9Sstevel@tonic-gate addon++, argc--, argv++;
947c478bd9Sstevel@tonic-gate if (argc < 3)
95f22acdffSgbrunett (void) fprintf(stderr, gettext(usagestr), progname), exit(1);
967c478bd9Sstevel@tonic-gate mesgwrite = fopen(argv[0], addon ? "a" : "w");
977c478bd9Sstevel@tonic-gate if (mesgwrite == NULL)
987c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1);
997c478bd9Sstevel@tonic-gate mesgread = fopen(argv[0], "r");
1007c478bd9Sstevel@tonic-gate if (mesgread == NULL)
1017c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1);
1027c478bd9Sstevel@tonic-gate inithash();
1037c478bd9Sstevel@tonic-gate argc--, argv++;
104f22acdffSgbrunett
105f22acdffSgbrunett if (strlcpy(name, argv[0], sizeof (name)) >= sizeof (name)) {
106f22acdffSgbrunett (void) fprintf(stderr, gettext("%s: %s: string too long"),
107f22acdffSgbrunett progname, argv[0]);
108f22acdffSgbrunett exit(1);
109f22acdffSgbrunett }
110f22acdffSgbrunett
1117c478bd9Sstevel@tonic-gate np = name + strlen(name);
112f22acdffSgbrunett
113f22acdffSgbrunett len = strlen(name);
114f22acdffSgbrunett np = name + len;
115f22acdffSgbrunett size = sizeof (name) - len;
1167c478bd9Sstevel@tonic-gate argc--, argv++;
1177c478bd9Sstevel@tonic-gate do {
118f22acdffSgbrunett if (strlcpy(np, argv[0], size) >= size) {
119f22acdffSgbrunett (void) fprintf(stderr,
120f22acdffSgbrunett gettext("%s: %s: string too long"),
121f22acdffSgbrunett progname, argv[0]);
122f22acdffSgbrunett exit(1);
123f22acdffSgbrunett }
1247c478bd9Sstevel@tonic-gate if (freopen(name, "w", stdout) == NULL)
1257c478bd9Sstevel@tonic-gate perror(name), exit(1);
1267c478bd9Sstevel@tonic-gate if (freopen(argv[0], "r", stdin) == NULL)
1277c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1);
1287c478bd9Sstevel@tonic-gate process();
1297c478bd9Sstevel@tonic-gate argc--, argv++;
1307c478bd9Sstevel@tonic-gate } while (argc > 0);
131f22acdffSgbrunett
13278b6ed60Scraigm return (0);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate
135f22acdffSgbrunett static void
process(void)13678b6ed60Scraigm process(void)
1377c478bd9Sstevel@tonic-gate {
13878b6ed60Scraigm int c;
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate for (;;) {
1417c478bd9Sstevel@tonic-gate c = getchar();
1427c478bd9Sstevel@tonic-gate if (c == EOF)
1437c478bd9Sstevel@tonic-gate return;
1447c478bd9Sstevel@tonic-gate if (c != 'e') {
145f22acdffSgbrunett (void) putchar(c);
1467c478bd9Sstevel@tonic-gate continue;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate if (match("error(")) {
149f22acdffSgbrunett (void) printf(gettext("error("));
1507c478bd9Sstevel@tonic-gate c = getchar();
1517c478bd9Sstevel@tonic-gate if (c != '"')
152f22acdffSgbrunett (void) putchar(c);
1537c478bd9Sstevel@tonic-gate else
1547c478bd9Sstevel@tonic-gate copystr();
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
159f22acdffSgbrunett static int
match(char * ocp)16078b6ed60Scraigm match(char *ocp)
1617c478bd9Sstevel@tonic-gate {
16278b6ed60Scraigm char *cp;
16378b6ed60Scraigm int c;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate for (cp = ocp + 1; *cp; cp++) {
1667c478bd9Sstevel@tonic-gate c = getchar();
1677c478bd9Sstevel@tonic-gate if (c != *cp) {
1687c478bd9Sstevel@tonic-gate while (ocp < cp)
169f22acdffSgbrunett (void) putchar(*ocp++);
170f22acdffSgbrunett (void) ungetchar(c);
1717c478bd9Sstevel@tonic-gate return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate return (1);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
177f22acdffSgbrunett static void
copystr(void)17878b6ed60Scraigm copystr(void)
1797c478bd9Sstevel@tonic-gate {
18078b6ed60Scraigm int c, ch;
1817c478bd9Sstevel@tonic-gate char buf[512];
18278b6ed60Scraigm char *cp = buf;
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate for (;;) {
1857c478bd9Sstevel@tonic-gate c = getchar();
1867c478bd9Sstevel@tonic-gate if (c == EOF)
1877c478bd9Sstevel@tonic-gate break;
1887c478bd9Sstevel@tonic-gate switch (c) {
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate case '"':
1917c478bd9Sstevel@tonic-gate *cp++ = 0;
1927c478bd9Sstevel@tonic-gate goto out;
1937c478bd9Sstevel@tonic-gate case '\\':
1947c478bd9Sstevel@tonic-gate c = getchar();
1957c478bd9Sstevel@tonic-gate switch (c) {
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate case 'b':
1987c478bd9Sstevel@tonic-gate c = '\b';
1997c478bd9Sstevel@tonic-gate break;
2007c478bd9Sstevel@tonic-gate case 't':
2017c478bd9Sstevel@tonic-gate c = '\t';
2027c478bd9Sstevel@tonic-gate break;
2037c478bd9Sstevel@tonic-gate case 'r':
2047c478bd9Sstevel@tonic-gate c = '\r';
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate case 'n':
2077c478bd9Sstevel@tonic-gate c = '\n';
2087c478bd9Sstevel@tonic-gate break;
2097c478bd9Sstevel@tonic-gate case '\n':
2107c478bd9Sstevel@tonic-gate continue;
2117c478bd9Sstevel@tonic-gate case 'f':
2127c478bd9Sstevel@tonic-gate c = '\f';
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate case '0':
2157c478bd9Sstevel@tonic-gate c = 0;
2167c478bd9Sstevel@tonic-gate break;
2177c478bd9Sstevel@tonic-gate case '\\':
2187c478bd9Sstevel@tonic-gate break;
2197c478bd9Sstevel@tonic-gate default:
2207c478bd9Sstevel@tonic-gate if (!octdigit(c))
2217c478bd9Sstevel@tonic-gate break;
2227c478bd9Sstevel@tonic-gate c -= '0';
2237c478bd9Sstevel@tonic-gate ch = getchar();
2247c478bd9Sstevel@tonic-gate if (!octdigit(ch))
2257c478bd9Sstevel@tonic-gate break;
2267c478bd9Sstevel@tonic-gate c <<= 7, c += ch - '0';
2277c478bd9Sstevel@tonic-gate ch = getchar();
2287c478bd9Sstevel@tonic-gate if (!octdigit(ch))
2297c478bd9Sstevel@tonic-gate break;
230f22acdffSgbrunett c <<= 3, c += ch - '0', ch = -1;
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate *cp++ = c;
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate out:
2377c478bd9Sstevel@tonic-gate *cp = 0;
238*332e84f5SToomas Soome (void) printf("%d", hashit(buf, 1, 0));
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate
241f22acdffSgbrunett static int
octdigit(char c)24278b6ed60Scraigm octdigit(char c)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate return (c >= '0' && c <= '7');
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
248f22acdffSgbrunett static void
inithash(void)24978b6ed60Scraigm inithash(void)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate char buf[512];
2527c478bd9Sstevel@tonic-gate int mesgpt = 0;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate rewind(mesgread);
255*332e84f5SToomas Soome while (fgetNUL(buf, sizeof (buf), mesgread) != 0) {
256f22acdffSgbrunett (void) hashit(buf, 0, mesgpt);
2577c478bd9Sstevel@tonic-gate mesgpt += strlen(buf) + 2;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate
261f22acdffSgbrunett static struct hash {
2627c478bd9Sstevel@tonic-gate long hval;
26378b6ed60Scraigm unsigned int hpt;
2647c478bd9Sstevel@tonic-gate struct hash *hnext;
2657c478bd9Sstevel@tonic-gate } *bucket[NBUCKETS];
2667c478bd9Sstevel@tonic-gate
267f22acdffSgbrunett static int
hashit(char * str,char really,unsigned int fakept)26878b6ed60Scraigm hashit(char *str, char really, unsigned int fakept)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate int i;
27178b6ed60Scraigm struct hash *hp;
2727c478bd9Sstevel@tonic-gate char buf[512];
2737c478bd9Sstevel@tonic-gate long hashval = 0;
27478b6ed60Scraigm char *cp;
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate if (really)
277f22acdffSgbrunett (void) fflush(mesgwrite);
278f22acdffSgbrunett for (cp = str; *cp; )
2797c478bd9Sstevel@tonic-gate hashval = (hashval << 1) + *cp++;
2807c478bd9Sstevel@tonic-gate i = hashval % NBUCKETS;
2817c478bd9Sstevel@tonic-gate if (i < 0)
2827c478bd9Sstevel@tonic-gate i += NBUCKETS;
2837c478bd9Sstevel@tonic-gate if (really != 0)
2847c478bd9Sstevel@tonic-gate for (hp = bucket[i]; hp != 0; hp = hp->hnext)
2857c478bd9Sstevel@tonic-gate if (hp->hval == hashval) {
286f22acdffSgbrunett (void) fseek(mesgread, (long)hp->hpt, 0);
287f22acdffSgbrunett (void) fgetNUL(buf, sizeof (buf), mesgread);
2887c478bd9Sstevel@tonic-gate if (strcmp(buf, str) == 0)
2897c478bd9Sstevel@tonic-gate break;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate if (!really || hp == 0) {
292f22acdffSgbrunett hp = (struct hash *)calloc(1, sizeof (*hp));
2937c478bd9Sstevel@tonic-gate hp->hnext = bucket[i];
2947c478bd9Sstevel@tonic-gate hp->hval = hashval;
2957c478bd9Sstevel@tonic-gate hp->hpt = really ? ftell(mesgwrite) : fakept;
2967c478bd9Sstevel@tonic-gate if (really) {
297f22acdffSgbrunett (void) fwrite(str, sizeof (char), strlen(str) + 1,
298f22acdffSgbrunett mesgwrite);
299f22acdffSgbrunett (void) fwrite("\n", sizeof (char), 1, mesgwrite);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate bucket[i] = hp;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate return (hp->hpt);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate
306f22acdffSgbrunett static int
fgetNUL(char * obuf,int rmdr,FILE * file)30778b6ed60Scraigm fgetNUL(char *obuf, int rmdr, FILE *file)
3087c478bd9Sstevel@tonic-gate {
30978b6ed60Scraigm int c;
31078b6ed60Scraigm char *buf = obuf;
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
3137c478bd9Sstevel@tonic-gate *buf++ = c;
3147c478bd9Sstevel@tonic-gate *buf++ = 0;
315f22acdffSgbrunett (void) getc(file);
316*332e84f5SToomas Soome return ((feof(file) || ferror(file)) ? 0 : 1);
3177c478bd9Sstevel@tonic-gate }
318