1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate * 22*7c478bd9Sstevel@tonic-gate * Copyright 1992 Sun Microsystems, Inc. All rights reserved. 23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*7c478bd9Sstevel@tonic-gate */ 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*7c478bd9Sstevel@tonic-gate /* from UCB 4.1 10/01/80 */ 28*7c478bd9Sstevel@tonic-gate #include <stdio.h> 29*7c478bd9Sstevel@tonic-gate #include <locale.h> 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #define ungetchar(c) ungetc(c, stdin) 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate long ftell(); 34*7c478bd9Sstevel@tonic-gate char *calloc(); 35*7c478bd9Sstevel@tonic-gate /* 36*7c478bd9Sstevel@tonic-gate * mkstr - create a string error message file by massaging C source 37*7c478bd9Sstevel@tonic-gate * 38*7c478bd9Sstevel@tonic-gate * Bill Joy UCB August 1977 39*7c478bd9Sstevel@tonic-gate * 40*7c478bd9Sstevel@tonic-gate * Modified March 1978 to hash old messages to be able to recompile 41*7c478bd9Sstevel@tonic-gate * without addding messages to the message file (usually) 42*7c478bd9Sstevel@tonic-gate * 43*7c478bd9Sstevel@tonic-gate * Based on an earlier program conceived by Bill Joy and Chuck Haley 44*7c478bd9Sstevel@tonic-gate * 45*7c478bd9Sstevel@tonic-gate * Program to create a string error message file 46*7c478bd9Sstevel@tonic-gate * from a group of C programs. Arguments are the name 47*7c478bd9Sstevel@tonic-gate * of the file where the strings are to be placed, the 48*7c478bd9Sstevel@tonic-gate * prefix of the new files where the processed source text 49*7c478bd9Sstevel@tonic-gate * is to be placed, and the files to be processed. 50*7c478bd9Sstevel@tonic-gate * 51*7c478bd9Sstevel@tonic-gate * The program looks for 'error("' in the source stream. 52*7c478bd9Sstevel@tonic-gate * Whenever it finds this, the following characters from the '"' 53*7c478bd9Sstevel@tonic-gate * to a '"' are replaced by 'seekpt' where seekpt is a 54*7c478bd9Sstevel@tonic-gate * pointer into the error message file. 55*7c478bd9Sstevel@tonic-gate * If the '(' is not immediately followed by a '"' no change occurs. 56*7c478bd9Sstevel@tonic-gate * 57*7c478bd9Sstevel@tonic-gate * The optional '-' causes strings to be added at the end of the 58*7c478bd9Sstevel@tonic-gate * existing error message file for recompilation of single routines. 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate FILE *mesgread, *mesgwrite; 63*7c478bd9Sstevel@tonic-gate char *progname; 64*7c478bd9Sstevel@tonic-gate char usagestr[] = "usage: %s [ - ] mesgfile prefix file ...\n"; 65*7c478bd9Sstevel@tonic-gate char name[100], *np; 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate main(argc, argv) 68*7c478bd9Sstevel@tonic-gate int argc; 69*7c478bd9Sstevel@tonic-gate char *argv[]; 70*7c478bd9Sstevel@tonic-gate { 71*7c478bd9Sstevel@tonic-gate char addon = 0; 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 76*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 77*7c478bd9Sstevel@tonic-gate #endif 78*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate argc--, progname = *argv++; 81*7c478bd9Sstevel@tonic-gate if (argc > 1 && argv[0][0] == '-') 82*7c478bd9Sstevel@tonic-gate addon++, argc--, argv++; 83*7c478bd9Sstevel@tonic-gate if (argc < 3) 84*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext(usagestr), progname), exit(1); 85*7c478bd9Sstevel@tonic-gate mesgwrite = fopen(argv[0], addon ? "a" : "w"); 86*7c478bd9Sstevel@tonic-gate if (mesgwrite == NULL) 87*7c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1); 88*7c478bd9Sstevel@tonic-gate mesgread = fopen(argv[0], "r"); 89*7c478bd9Sstevel@tonic-gate if (mesgread == NULL) 90*7c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1); 91*7c478bd9Sstevel@tonic-gate inithash(); 92*7c478bd9Sstevel@tonic-gate argc--, argv++; 93*7c478bd9Sstevel@tonic-gate strcpy(name, argv[0]); 94*7c478bd9Sstevel@tonic-gate np = name + strlen(name); 95*7c478bd9Sstevel@tonic-gate argc--, argv++; 96*7c478bd9Sstevel@tonic-gate do { 97*7c478bd9Sstevel@tonic-gate strcpy(np, argv[0]); 98*7c478bd9Sstevel@tonic-gate if (freopen(name, "w", stdout) == NULL) 99*7c478bd9Sstevel@tonic-gate perror(name), exit(1); 100*7c478bd9Sstevel@tonic-gate if (freopen(argv[0], "r", stdin) == NULL) 101*7c478bd9Sstevel@tonic-gate perror(argv[0]), exit(1); 102*7c478bd9Sstevel@tonic-gate process(); 103*7c478bd9Sstevel@tonic-gate argc--, argv++; 104*7c478bd9Sstevel@tonic-gate } while (argc > 0); 105*7c478bd9Sstevel@tonic-gate exit(0); 106*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate process() 110*7c478bd9Sstevel@tonic-gate { 111*7c478bd9Sstevel@tonic-gate register char *cp; 112*7c478bd9Sstevel@tonic-gate register c; 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate for (;;) { 115*7c478bd9Sstevel@tonic-gate c = getchar(); 116*7c478bd9Sstevel@tonic-gate if (c == EOF) 117*7c478bd9Sstevel@tonic-gate return; 118*7c478bd9Sstevel@tonic-gate if (c != 'e') { 119*7c478bd9Sstevel@tonic-gate putchar(c); 120*7c478bd9Sstevel@tonic-gate continue; 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate if (match("error(")) { 123*7c478bd9Sstevel@tonic-gate printf(gettext("error(")); 124*7c478bd9Sstevel@tonic-gate c = getchar(); 125*7c478bd9Sstevel@tonic-gate if (c != '"') 126*7c478bd9Sstevel@tonic-gate putchar(c); 127*7c478bd9Sstevel@tonic-gate else 128*7c478bd9Sstevel@tonic-gate copystr(); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate match(ocp) 134*7c478bd9Sstevel@tonic-gate char *ocp; 135*7c478bd9Sstevel@tonic-gate { 136*7c478bd9Sstevel@tonic-gate register char *cp; 137*7c478bd9Sstevel@tonic-gate register c; 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate for (cp = ocp + 1; *cp; cp++) { 140*7c478bd9Sstevel@tonic-gate c = getchar(); 141*7c478bd9Sstevel@tonic-gate if (c != *cp) { 142*7c478bd9Sstevel@tonic-gate while (ocp < cp) 143*7c478bd9Sstevel@tonic-gate putchar(*ocp++); 144*7c478bd9Sstevel@tonic-gate ungetchar(c); 145*7c478bd9Sstevel@tonic-gate return (0); 146*7c478bd9Sstevel@tonic-gate } 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate return (1); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate 151*7c478bd9Sstevel@tonic-gate copystr() 152*7c478bd9Sstevel@tonic-gate { 153*7c478bd9Sstevel@tonic-gate register c, ch; 154*7c478bd9Sstevel@tonic-gate char buf[512]; 155*7c478bd9Sstevel@tonic-gate register char *cp = buf; 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate for (;;) { 158*7c478bd9Sstevel@tonic-gate c = getchar(); 159*7c478bd9Sstevel@tonic-gate if (c == EOF) 160*7c478bd9Sstevel@tonic-gate break; 161*7c478bd9Sstevel@tonic-gate switch (c) { 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate case '"': 164*7c478bd9Sstevel@tonic-gate *cp++ = 0; 165*7c478bd9Sstevel@tonic-gate goto out; 166*7c478bd9Sstevel@tonic-gate case '\\': 167*7c478bd9Sstevel@tonic-gate c = getchar(); 168*7c478bd9Sstevel@tonic-gate switch (c) { 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate case 'b': 171*7c478bd9Sstevel@tonic-gate c = '\b'; 172*7c478bd9Sstevel@tonic-gate break; 173*7c478bd9Sstevel@tonic-gate case 't': 174*7c478bd9Sstevel@tonic-gate c = '\t'; 175*7c478bd9Sstevel@tonic-gate break; 176*7c478bd9Sstevel@tonic-gate case 'r': 177*7c478bd9Sstevel@tonic-gate c = '\r'; 178*7c478bd9Sstevel@tonic-gate break; 179*7c478bd9Sstevel@tonic-gate case 'n': 180*7c478bd9Sstevel@tonic-gate c = '\n'; 181*7c478bd9Sstevel@tonic-gate break; 182*7c478bd9Sstevel@tonic-gate case '\n': 183*7c478bd9Sstevel@tonic-gate continue; 184*7c478bd9Sstevel@tonic-gate case 'f': 185*7c478bd9Sstevel@tonic-gate c = '\f'; 186*7c478bd9Sstevel@tonic-gate break; 187*7c478bd9Sstevel@tonic-gate case '0': 188*7c478bd9Sstevel@tonic-gate c = 0; 189*7c478bd9Sstevel@tonic-gate break; 190*7c478bd9Sstevel@tonic-gate case '\\': 191*7c478bd9Sstevel@tonic-gate break; 192*7c478bd9Sstevel@tonic-gate default: 193*7c478bd9Sstevel@tonic-gate if (!octdigit(c)) 194*7c478bd9Sstevel@tonic-gate break; 195*7c478bd9Sstevel@tonic-gate c -= '0'; 196*7c478bd9Sstevel@tonic-gate ch = getchar(); 197*7c478bd9Sstevel@tonic-gate if (!octdigit(ch)) 198*7c478bd9Sstevel@tonic-gate break; 199*7c478bd9Sstevel@tonic-gate c <<= 7, c += ch - '0'; 200*7c478bd9Sstevel@tonic-gate ch = getchar(); 201*7c478bd9Sstevel@tonic-gate if (!octdigit(ch)) 202*7c478bd9Sstevel@tonic-gate break; 203*7c478bd9Sstevel@tonic-gate c <<= 3, c+= ch - '0', ch = -1; 204*7c478bd9Sstevel@tonic-gate break; 205*7c478bd9Sstevel@tonic-gate } 206*7c478bd9Sstevel@tonic-gate } 207*7c478bd9Sstevel@tonic-gate *cp++ = c; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate out: 210*7c478bd9Sstevel@tonic-gate *cp = 0; 211*7c478bd9Sstevel@tonic-gate printf("%d", hashit(buf, 1, NULL)); 212*7c478bd9Sstevel@tonic-gate } 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate octdigit(c) 215*7c478bd9Sstevel@tonic-gate char c; 216*7c478bd9Sstevel@tonic-gate { 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate return (c >= '0' && c <= '7'); 219*7c478bd9Sstevel@tonic-gate } 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate inithash() 222*7c478bd9Sstevel@tonic-gate { 223*7c478bd9Sstevel@tonic-gate char buf[512]; 224*7c478bd9Sstevel@tonic-gate int mesgpt = 0; 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate rewind(mesgread); 227*7c478bd9Sstevel@tonic-gate while (fgetNUL(buf, sizeof buf, mesgread) != NULL) { 228*7c478bd9Sstevel@tonic-gate hashit(buf, 0, mesgpt); 229*7c478bd9Sstevel@tonic-gate mesgpt += strlen(buf) + 2; 230*7c478bd9Sstevel@tonic-gate } 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate #define NBUCKETS 511 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate struct hash { 236*7c478bd9Sstevel@tonic-gate long hval; 237*7c478bd9Sstevel@tonic-gate unsigned hpt; 238*7c478bd9Sstevel@tonic-gate struct hash *hnext; 239*7c478bd9Sstevel@tonic-gate } *bucket[NBUCKETS]; 240*7c478bd9Sstevel@tonic-gate 241*7c478bd9Sstevel@tonic-gate hashit(str, really, fakept) 242*7c478bd9Sstevel@tonic-gate char *str; 243*7c478bd9Sstevel@tonic-gate char really; 244*7c478bd9Sstevel@tonic-gate unsigned fakept; 245*7c478bd9Sstevel@tonic-gate { 246*7c478bd9Sstevel@tonic-gate int i; 247*7c478bd9Sstevel@tonic-gate register struct hash *hp; 248*7c478bd9Sstevel@tonic-gate char buf[512]; 249*7c478bd9Sstevel@tonic-gate long hashval = 0; 250*7c478bd9Sstevel@tonic-gate register char *cp; 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate if (really) 253*7c478bd9Sstevel@tonic-gate fflush(mesgwrite); 254*7c478bd9Sstevel@tonic-gate for (cp = str; *cp;) 255*7c478bd9Sstevel@tonic-gate hashval = (hashval << 1) + *cp++; 256*7c478bd9Sstevel@tonic-gate i = hashval % NBUCKETS; 257*7c478bd9Sstevel@tonic-gate if (i < 0) 258*7c478bd9Sstevel@tonic-gate i += NBUCKETS; 259*7c478bd9Sstevel@tonic-gate if (really != 0) 260*7c478bd9Sstevel@tonic-gate for (hp = bucket[i]; hp != 0; hp = hp->hnext) 261*7c478bd9Sstevel@tonic-gate if (hp->hval == hashval) { 262*7c478bd9Sstevel@tonic-gate fseek(mesgread, (long) hp->hpt, 0); 263*7c478bd9Sstevel@tonic-gate fgetNUL(buf, sizeof buf, mesgread); 264*7c478bd9Sstevel@tonic-gate /* 265*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("Got (from %d) %s\n"), hp->hpt, buf); 266*7c478bd9Sstevel@tonic-gate */ 267*7c478bd9Sstevel@tonic-gate if (strcmp(buf, str) == 0) 268*7c478bd9Sstevel@tonic-gate break; 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate if (!really || hp == 0) { 271*7c478bd9Sstevel@tonic-gate hp = (struct hash *) calloc(1, sizeof *hp); 272*7c478bd9Sstevel@tonic-gate hp->hnext = bucket[i]; 273*7c478bd9Sstevel@tonic-gate hp->hval = hashval; 274*7c478bd9Sstevel@tonic-gate hp->hpt = really ? ftell(mesgwrite) : fakept; 275*7c478bd9Sstevel@tonic-gate if (really) { 276*7c478bd9Sstevel@tonic-gate fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite); 277*7c478bd9Sstevel@tonic-gate fwrite("\n", sizeof (char), 1, mesgwrite); 278*7c478bd9Sstevel@tonic-gate } 279*7c478bd9Sstevel@tonic-gate bucket[i] = hp; 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate /* 282*7c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("%s hashed to %ld at %d\n"), str, hp->hval, hp->hpt); 283*7c478bd9Sstevel@tonic-gate */ 284*7c478bd9Sstevel@tonic-gate return (hp->hpt); 285*7c478bd9Sstevel@tonic-gate } 286*7c478bd9Sstevel@tonic-gate 287*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 288*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate fgetNUL(obuf, rmdr, file) 291*7c478bd9Sstevel@tonic-gate char *obuf; 292*7c478bd9Sstevel@tonic-gate register int rmdr; 293*7c478bd9Sstevel@tonic-gate FILE *file; 294*7c478bd9Sstevel@tonic-gate { 295*7c478bd9Sstevel@tonic-gate register c; 296*7c478bd9Sstevel@tonic-gate register char *buf = obuf; 297*7c478bd9Sstevel@tonic-gate 298*7c478bd9Sstevel@tonic-gate while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF) 299*7c478bd9Sstevel@tonic-gate *buf++ = c; 300*7c478bd9Sstevel@tonic-gate *buf++ = 0; 301*7c478bd9Sstevel@tonic-gate getc(file); 302*7c478bd9Sstevel@tonic-gate return ((feof(file) || ferror(file)) ? NULL : 1); 303*7c478bd9Sstevel@tonic-gate } 304