[BACK]Return to mkstr.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mkstr

Annotation of src/usr.bin/mkstr/mkstr.c, Revision 1.1

1.1     ! deraadt     1: /*     $NetBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls Exp $    */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1980, 1993
        !             5:  *     The Regents of the University of California.  All rights reserved.
        !             6:  *
        !             7:  * Redistribution and use in source and binary forms, with or without
        !             8:  * modification, are permitted provided that the following conditions
        !             9:  * are met:
        !            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. All advertising materials mentioning features or use of this software
        !            16:  *    must display the following acknowledgement:
        !            17:  *     This product includes software developed by the University of
        !            18:  *     California, Berkeley and its contributors.
        !            19:  * 4. Neither the name of the University nor the names of its contributors
        !            20:  *    may be used to endorse or promote products derived from this software
        !            21:  *    without specific prior written permission.
        !            22:  *
        !            23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            33:  * SUCH DAMAGE.
        !            34:  */
        !            35:
        !            36: #ifndef lint
        !            37: static char copyright[] =
        !            38: "@(#) Copyright (c) 1980, 1993\n\
        !            39:        The Regents of the University of California.  All rights reserved.\n";
        !            40: #endif /* not lint */
        !            41:
        !            42: #ifndef lint
        !            43: #if 0
        !            44: static char sccsid[] = "@(#)mkstr.c    8.1 (Berkeley) 6/6/93";
        !            45: #else
        !            46: static char rcsid[] = "$NetBSD: mkstr.c,v 1.4 1995/09/28 06:22:20 tls Exp $";
        !            47: #endif
        !            48: #endif /* not lint */
        !            49:
        !            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:
        !            83: FILE   *mesgread, *mesgwrite;
        !            84: char   *progname;
        !            85: char   usagestr[] =    "usage: %s [ - ] mesgfile prefix file ...\n";
        !            86: char   name[100], *np;
        !            87:
        !            88: main(argc, argv)
        !            89:        int argc;
        !            90:        char *argv[];
        !            91: {
        !            92:        char addon = 0;
        !            93:
        !            94:        argc--, progname = *argv++;
        !            95:        if (argc > 1 && argv[0][0] == '-')
        !            96:                addon++, argc--, argv++;
        !            97:        if (argc < 3)
        !            98:                fprintf(stderr, usagestr, progname), exit(1);
        !            99:        mesgwrite = fopen(argv[0], addon ? "a" : "w");
        !           100:        if (mesgwrite == NULL)
        !           101:                perror(argv[0]), exit(1);
        !           102:        mesgread = fopen(argv[0], "r");
        !           103:        if (mesgread == NULL)
        !           104:                perror(argv[0]), exit(1);
        !           105:        inithash();
        !           106:        argc--, argv++;
        !           107:        strcpy(name, argv[0]);
        !           108:        np = name + strlen(name);
        !           109:        argc--, argv++;
        !           110:        do {
        !           111:                strcpy(np, argv[0]);
        !           112:                if (freopen(name, "w", stdout) == NULL)
        !           113:                        perror(name), exit(1);
        !           114:                if (freopen(argv[0], "r", stdin) == NULL)
        !           115:                        perror(argv[0]), exit(1);
        !           116:                process();
        !           117:                argc--, argv++;
        !           118:        } while (argc > 0);
        !           119:        exit(0);
        !           120: }
        !           121:
        !           122: process()
        !           123: {
        !           124:        register char *cp;
        !           125:        register c;
        !           126:
        !           127:        for (;;) {
        !           128:                c = getchar();
        !           129:                if (c == EOF)
        !           130:                        return;
        !           131:                if (c != 'e') {
        !           132:                        putchar(c);
        !           133:                        continue;
        !           134:                }
        !           135:                if (match("error(")) {
        !           136:                        printf("error(");
        !           137:                        c = getchar();
        !           138:                        if (c != '"')
        !           139:                                putchar(c);
        !           140:                        else
        !           141:                                copystr();
        !           142:                }
        !           143:        }
        !           144: }
        !           145:
        !           146: match(ocp)
        !           147:        char *ocp;
        !           148: {
        !           149:        register char *cp;
        !           150:        register c;
        !           151:
        !           152:        for (cp = ocp + 1; *cp; cp++) {
        !           153:                c = getchar();
        !           154:                if (c != *cp) {
        !           155:                        while (ocp < cp)
        !           156:                                putchar(*ocp++);
        !           157:                        ungetchar(c);
        !           158:                        return (0);
        !           159:                }
        !           160:        }
        !           161:        return (1);
        !           162: }
        !           163:
        !           164: copystr()
        !           165: {
        !           166:        register c, ch;
        !           167:        char buf[512];
        !           168:        register char *cp = buf;
        !           169:
        !           170:        for (;;) {
        !           171:                c = getchar();
        !           172:                if (c == EOF)
        !           173:                        break;
        !           174:                switch (c) {
        !           175:
        !           176:                case '"':
        !           177:                        *cp++ = 0;
        !           178:                        goto out;
        !           179:                case '\\':
        !           180:                        c = getchar();
        !           181:                        switch (c) {
        !           182:
        !           183:                        case 'b':
        !           184:                                c = '\b';
        !           185:                                break;
        !           186:                        case 't':
        !           187:                                c = '\t';
        !           188:                                break;
        !           189:                        case 'r':
        !           190:                                c = '\r';
        !           191:                                break;
        !           192:                        case 'n':
        !           193:                                c = '\n';
        !           194:                                break;
        !           195:                        case '\n':
        !           196:                                continue;
        !           197:                        case 'f':
        !           198:                                c = '\f';
        !           199:                                break;
        !           200:                        case '0':
        !           201:                                c = 0;
        !           202:                                break;
        !           203:                        case '\\':
        !           204:                                break;
        !           205:                        default:
        !           206:                                if (!octdigit(c))
        !           207:                                        break;
        !           208:                                c -= '0';
        !           209:                                ch = getchar();
        !           210:                                if (!octdigit(ch))
        !           211:                                        break;
        !           212:                                c <<= 7, c += ch - '0';
        !           213:                                ch = getchar();
        !           214:                                if (!octdigit(ch))
        !           215:                                        break;
        !           216:                                c <<= 3, c+= ch - '0', ch = -1;
        !           217:                                break;
        !           218:                        }
        !           219:                }
        !           220:                *cp++ = c;
        !           221:        }
        !           222: out:
        !           223:        *cp = 0;
        !           224:        printf("%d", hashit(buf, 1, NULL));
        !           225: }
        !           226:
        !           227: octdigit(c)
        !           228:        char c;
        !           229: {
        !           230:
        !           231:        return (c >= '0' && c <= '7');
        !           232: }
        !           233:
        !           234: inithash()
        !           235: {
        !           236:        char buf[512];
        !           237:        int mesgpt = 0;
        !           238:
        !           239:        rewind(mesgread);
        !           240:        while (fgetNUL(buf, sizeof buf, mesgread) != NULL) {
        !           241:                hashit(buf, 0, mesgpt);
        !           242:                mesgpt += strlen(buf) + 2;
        !           243:        }
        !           244: }
        !           245:
        !           246: #define        NBUCKETS        511
        !           247:
        !           248: struct hash {
        !           249:        long    hval;
        !           250:        unsigned hpt;
        !           251:        struct  hash *hnext;
        !           252: } *bucket[NBUCKETS];
        !           253:
        !           254: hashit(str, really, fakept)
        !           255:        char *str;
        !           256:        char really;
        !           257:        unsigned fakept;
        !           258: {
        !           259:        int i;
        !           260:        register struct hash *hp;
        !           261:        char buf[512];
        !           262:        long hashval = 0;
        !           263:        register char *cp;
        !           264:
        !           265:        if (really)
        !           266:                fflush(mesgwrite);
        !           267:        for (cp = str; *cp;)
        !           268:                hashval = (hashval << 1) + *cp++;
        !           269:        i = hashval % NBUCKETS;
        !           270:        if (i < 0)
        !           271:                i += NBUCKETS;
        !           272:        if (really != 0)
        !           273:                for (hp = bucket[i]; hp != 0; hp = hp->hnext)
        !           274:                if (hp->hval == hashval) {
        !           275:                        fseek(mesgread, (long) hp->hpt, 0);
        !           276:                        fgetNUL(buf, sizeof buf, mesgread);
        !           277: /*
        !           278:                        fprintf(stderr, "Got (from %d) %s\n", hp->hpt, buf);
        !           279: */
        !           280:                        if (strcmp(buf, str) == 0)
        !           281:                                break;
        !           282:                }
        !           283:        if (!really || hp == 0) {
        !           284:                hp = (struct hash *) calloc(1, sizeof *hp);
        !           285:                hp->hnext = bucket[i];
        !           286:                hp->hval = hashval;
        !           287:                hp->hpt = really ? ftell(mesgwrite) : fakept;
        !           288:                if (really) {
        !           289:                        fwrite(str, sizeof (char), strlen(str) + 1, mesgwrite);
        !           290:                        fwrite("\n", sizeof (char), 1, mesgwrite);
        !           291:                }
        !           292:                bucket[i] = hp;
        !           293:        }
        !           294: /*
        !           295:        fprintf(stderr, "%s hashed to %ld at %d\n", str, hp->hval, hp->hpt);
        !           296: */
        !           297:        return (hp->hpt);
        !           298: }
        !           299:
        !           300: #include <sys/types.h>
        !           301: #include <sys/stat.h>
        !           302:
        !           303: fgetNUL(obuf, rmdr, file)
        !           304:        char *obuf;
        !           305:        register int rmdr;
        !           306:        FILE *file;
        !           307: {
        !           308:        register c;
        !           309:        register char *buf = obuf;
        !           310:
        !           311:        while (--rmdr > 0 && (c = getc(file)) != 0 && c != EOF)
        !           312:                *buf++ = c;
        !           313:        *buf++ = 0;
        !           314:        getc(file);
        !           315:        return ((feof(file) || ferror(file)) ? NULL : 1);
        !           316: }