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

Annotation of src/usr.bin/error/subr.c, Revision 1.4

1.4     ! deraadt     1: /*     $OpenBSD: subr.c,v 1.3 1997/06/17 20:56:40 kstailey Exp $       */
1.1       deraadt     2: /*     $NetBSD: subr.c,v 1.4 1995/09/10 15:55:15 christos Exp $        */
                      3:
                      4: /*
                      5:  * Copyright (c) 1980, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)subr.c     8.1 (Berkeley) 6/6/93";
                     40: #endif
1.4     ! deraadt    41: static char rcsid[] = "$OpenBSD: subr.c,v 1.3 1997/06/17 20:56:40 kstailey Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #include <stdio.h>
                     45: #include <ctype.h>
                     46: #include <stdlib.h>
                     47: #include <string.h>
                     48: #include "error.h"
                     49: /*
                     50:  *     Arrayify a list of rules
                     51:  */
1.4     ! deraadt    52: void
1.1       deraadt    53: arrayify(e_length, e_array, header)
                     54:        int     *e_length;
                     55:        Eptr    **e_array;
                     56:        Eptr    header;
                     57: {
                     58:        reg     Eptr    errorp;
                     59:        reg     Eptr    *array;
                     60:        reg     int     listlength;
                     61:        reg     int     listindex;
                     62:
                     63:        for (errorp = header, listlength = 0;
                     64:             errorp; errorp = errorp->error_next, listlength++)
                     65:                continue;
                     66:        array = (Eptr*)Calloc(listlength+1, sizeof (Eptr));
                     67:        for(listindex = 0, errorp = header;
                     68:            listindex < listlength;
                     69:            listindex++, errorp = errorp->error_next){
                     70:                array[listindex] = errorp;
                     71:                errorp->error_position = listindex;
                     72:        }
                     73:        array[listindex] = (Eptr)0;
                     74:        *e_length = listlength;
                     75:        *e_array = array;
                     76: }
                     77:
                     78: /*VARARGS1*/
1.4     ! deraadt    79: void
1.1       deraadt    80: error(msg, a1, a2, a3)
                     81:        char    *msg;
                     82: {
                     83:        fprintf(stderr, "Error: ");
                     84:        fprintf(stderr, msg, a1, a2, a3);
                     85:        fprintf(stderr, "\n");
                     86:        fflush(stdout);
                     87:        fflush(stderr);
                     88:        exit(6);
                     89: }
1.4     ! deraadt    90:
1.1       deraadt    91: /*ARGSUSED*/
                     92: char *Calloc(nelements, size)
                     93:        int     nelements;
                     94:        int     size;
                     95: {
                     96:        char    *back;
1.3       kstailey   97:        if ( (back = (char *)calloc(nelements, size)) == NULL) {
1.1       deraadt    98:                error("Ran out of memory.\n");
                     99:                exit(1);
                    100:        }
                    101:        return(back);
                    102: }
                    103:
                    104: char *strsave(instring)
                    105:        char    *instring;
                    106: {
                    107:        char    *outstring;
                    108:        (void)strcpy(outstring = (char *)Calloc(1, strlen(instring) + 1),
                    109:                instring);
                    110:        return(outstring);
                    111: }
                    112: /*
                    113:  *     find the position of a given character in a string
                    114:  *             (one based)
                    115:  */
                    116: int position(string, ch)
                    117:        reg     char    *string;
                    118:        reg     char    ch;
                    119: {
                    120:        reg     int     i;
                    121:        if (string)
                    122:        for (i=1; *string; string++, i++){
                    123:                if (*string == ch)
                    124:                        return(i);
                    125:        }
                    126:        return(-1);
                    127: }
                    128: /*
                    129:  *     clobber the first occurance of ch in string by the new character
                    130:  */
                    131: char *substitute(string, chold, chnew)
                    132:        char    *string;
                    133:        char    chold, chnew;
                    134: {
                    135:        reg     char    *cp = string;
                    136:
                    137:        if (cp)
                    138:        while (*cp){
                    139:                if (*cp == chold){
                    140:                        *cp = chnew;
                    141:                        break;
                    142:                }
                    143:                cp++;
                    144:        }
                    145:        return(string);
                    146: }
                    147:
                    148: char lastchar(string)
                    149:        char    *string;
                    150: {
                    151:        int     length;
                    152:        if (string == 0) return('\0');
                    153:        length = strlen(string);
                    154:        if (length >= 1)
                    155:                return(string[length-1]);
                    156:        else
                    157:                return('\0');
                    158: }
                    159:
                    160: char firstchar(string)
                    161:        char    *string;
                    162: {
                    163:        if (string)
                    164:                return(string[0]);
                    165:        else
                    166:                return('\0');
                    167: }
                    168:
                    169: char   next_lastchar(string)
                    170:        char    *string;
                    171: {
                    172:        int     length;
                    173:        if (string == 0) return('\0');
                    174:        length = strlen(string);
                    175:        if (length >= 2)
                    176:                return(string[length - 2]);
                    177:        else
                    178:                return('\0');
                    179: }
                    180:
                    181: clob_last(string, newstuff)
                    182:        char    *string, newstuff;
                    183: {
                    184:        int     length = 0;
                    185:        if (string)
                    186:                length = strlen(string);
                    187:        if (length >= 1)
                    188:                string[length - 1] = newstuff;
                    189: }
                    190:
                    191: /*
                    192:  *     parse a string that is the result of a format %s(%d)
                    193:  *     return TRUE if this is of the proper format
                    194:  */
                    195: boolean persperdexplode(string, r_perd, r_pers)
                    196:        char    *string;
                    197:        char    **r_perd, **r_pers;
                    198: {
                    199:        reg     char    *cp;
                    200:                int     length = 0;
                    201:
                    202:        if (string)
                    203:                length = strlen(string);
                    204:        if (   (length >= 4)
                    205:            && (string[length - 1] == ')' ) ){
                    206:                for (cp = &string[length - 2];
                    207:                     (isdigit(*cp)) && (*cp != '(');
                    208:                     --cp)
                    209:                        continue;
                    210:                if (*cp == '('){
                    211:                        string[length - 1] = '\0';      /* clobber the ) */
                    212:                        *r_perd = strsave(cp+1);
                    213:                        string[length - 1] = ')';
                    214:                        *cp = '\0';                     /* clobber the ( */
                    215:                        *r_pers = strsave(string);
                    216:                        *cp = '(';
                    217:                        return(TRUE);
                    218:                }
                    219:        }
                    220:        return(FALSE);
                    221: }
                    222: /*
                    223:  *     parse a quoted string that is the result of a format \"%s\"(%d)
                    224:  *     return TRUE if this is of the proper format
                    225:  */
                    226: boolean qpersperdexplode(string, r_perd, r_pers)
                    227:        char    *string;
                    228:        char    **r_perd, **r_pers;
                    229: {
                    230:        reg     char    *cp;
                    231:                int     length = 0;
                    232:
                    233:        if (string)
                    234:                length = strlen(string);
                    235:        if (   (length >= 4)
                    236:            && (string[length - 1] == ')' ) ){
                    237:                for (cp = &string[length - 2];
                    238:                     (isdigit(*cp)) && (*cp != '(');
                    239:                     --cp)
                    240:                        continue;
                    241:                if (*cp == '(' && *(cp - 1) == '"'){
                    242:                        string[length - 1] = '\0';
                    243:                        *r_perd = strsave(cp+1);
                    244:                        string[length - 1] = ')';
                    245:                        *(cp - 1) = '\0';               /* clobber the " */
                    246:                        *r_pers = strsave(string + 1);
                    247:                        *(cp - 1) = '"';
                    248:                        return(TRUE);
                    249:                }
                    250:        }
                    251:        return(FALSE);
                    252: }
                    253:
                    254: static char    cincomment[] = CINCOMMENT;
                    255: static char    coutcomment[] = COUTCOMMENT;
                    256: static char    fincomment[] = FINCOMMENT;
                    257: static char    foutcomment[] = FOUTCOMMENT;
                    258: static char    newline[] = NEWLINE;
                    259: static char    piincomment[] = PIINCOMMENT;
                    260: static char    pioutcomment[] = PIOUTCOMMENT;
                    261: static char    lispincomment[] = LISPINCOMMENT;
                    262: static char    riincomment[] = RIINCOMMENT;
                    263: static char    rioutcomment[] = RIOUTCOMMENT;
                    264: static char    troffincomment[] = TROFFINCOMMENT;
                    265: static char    troffoutcomment[] = TROFFOUTCOMMENT;
                    266: static char    mod2incomment[] = MOD2INCOMMENT;
                    267: static char    mod2outcomment[] = MOD2OUTCOMMENT;
                    268:
                    269: struct lang_desc lang_table[] = {
                    270:        /*INUNKNOWN     0*/     "unknown", cincomment,  coutcomment,
                    271:        /*INCPP         1*/     "cpp",  cincomment,    coutcomment,
                    272:        /*INCC          2*/     "cc",   cincomment,    coutcomment,
                    273:        /*INAS          3*/     "as",   ASINCOMMENT,   newline,
                    274:        /*INLD          4*/     "ld",   cincomment,    coutcomment,
                    275:        /*INLINT        5*/     "lint", cincomment,    coutcomment,
                    276:        /*INF77         6*/     "f77",  fincomment,    foutcomment,
                    277:        /*INPI          7*/     "pi",   piincomment,   pioutcomment,
                    278:        /*INPC          8*/     "pc",   piincomment,   pioutcomment,
                    279:        /*INFRANZ       9*/     "franz",lispincomment, newline,
                    280:        /*INLISP        10*/    "lisp", lispincomment, newline,
                    281:        /*INVAXIMA      11*/    "vaxima",lispincomment,newline,
                    282:        /*INRATFOR      12*/    "ratfor",fincomment,   foutcomment,
                    283:        /*INLEX         13*/    "lex",  cincomment,    coutcomment,
                    284:        /*INYACC        14*/    "yacc", cincomment,    coutcomment,
                    285:        /*INAPL         15*/    "apl",  ".lm",         newline,
                    286:        /*INMAKE        16*/    "make", ASINCOMMENT,   newline,
                    287:        /*INRI          17*/    "ri",   riincomment,   rioutcomment,
                    288:        /*INTROFF       18*/    "troff",troffincomment,troffoutcomment,
                    289:        /*INMOD2        19*/    "mod2", mod2incomment, mod2outcomment,
                    290:                                0,      0,           0
                    291: };
                    292:
                    293: printerrors(look_at_subclass, errorc, errorv)
                    294:        boolean look_at_subclass;
                    295:        int     errorc;
                    296:        Eptr    errorv[];
                    297: {
                    298:        reg     int     i;
                    299:        reg     Eptr    errorp;
                    300:
                    301:        for (errorp = errorv[i = 0]; i < errorc; errorp = errorv[++i]){
                    302:                if (errorp->error_e_class == C_IGNORE)
                    303:                        continue;
                    304:                if (look_at_subclass && errorp->error_s_class == C_DUPL)
                    305:                        continue;
                    306:                printf("Error %d, (%s error) [%s], text = \"",
                    307:                        i,
                    308:                        class_table[errorp->error_e_class],
                    309:                        lang_table[errorp->error_language].lang_name);
                    310:                wordvprint(stdout,errorp->error_lgtext,errorp->error_text);
                    311:                printf("\"\n");
                    312:        }
                    313: }
                    314:
                    315: wordvprint(fyle, wordc, wordv)
                    316:        FILE    *fyle;
                    317:        int     wordc;
                    318:        char    *wordv[];
                    319: {
                    320:        int     i;
                    321:        char *sep = "";
                    322:
                    323:        for(i = 0; i < wordc; i++)
                    324:                if (wordv[i]) {
                    325:                        fprintf(fyle, "%s%s",sep,wordv[i]);
                    326:                        sep = " ";
                    327:                }
                    328: }
                    329:
                    330: /*
                    331:  *     Given a string, parse it into a number of words, and build
                    332:  *     a wordc wordv combination pointing into it.
                    333:  */
                    334: wordvbuild(string, r_wordc, r_wordv)
                    335:        char    *string;
                    336:        int     *r_wordc;
                    337:        char    ***r_wordv;
                    338: {
                    339:        reg     char    *cp;
                    340:                char    **wordv;
                    341:                int     wordcount;
                    342:                int     wordindex;
                    343:
                    344:        for (wordcount = 0, cp = string; *cp; wordcount++){
                    345:                while (*cp  && isspace(*cp))
                    346:                        cp++;
                    347:                if (*cp == 0)
                    348:                        break;
                    349:                while (!isspace(*cp))
                    350:                        cp++;
                    351:        }
                    352:        wordv = (char **)Calloc(wordcount + 1, sizeof (char *));
                    353:        for (cp=string,wordindex=0; wordcount; wordindex++,--wordcount){
                    354:                while (*cp && isspace(*cp))
                    355:                        cp++;
                    356:                if (*cp == 0)
                    357:                        break;
                    358:                wordv[wordindex] = cp;
                    359:                while(!isspace(*cp))
                    360:                        cp++;
                    361:                *cp++ = '\0';
                    362:        }
                    363:        if (wordcount != 0)
                    364:                error("Initial miscount of the number of words in a line\n");
                    365:        wordv[wordindex] = (char *)0;
                    366: #ifdef FULLDEBUG
                    367:        for (wordcount = 0; wordcount < wordindex; wordcount++)
                    368:                printf("Word %d = \"%s\"\n", wordcount, wordv[wordcount]);
                    369:        printf("\n");
                    370: #endif
                    371:        *r_wordc = wordindex;
                    372:        *r_wordv = wordv;
                    373: }
                    374: /*
                    375:  *     Compare two 0 based wordvectors
                    376:  */
                    377: int wordvcmp(wordv1, wordc, wordv2)
                    378:        char    **wordv1;
                    379:        int     wordc;
                    380:        char    **wordv2;
                    381: {
                    382:        reg     int i;
                    383:                int     back;
                    384:        for (i = 0; i < wordc; i++){
                    385:                if (wordv1[i] == 0 || wordv2[i] == 0)
                    386:                                return(-1);
                    387:                if (back = strcmp(wordv1[i], wordv2[i])){
                    388:                        return(back);
                    389:                }
                    390:        }
                    391:        return(0);      /* they are equal */
                    392: }
                    393:
                    394: /*
                    395:  *     splice a 0 basedword vector onto the tail of a
                    396:  *     new wordv, allowing the first emptyhead slots to be empty
                    397:  */
                    398: char   **wordvsplice(emptyhead, wordc, wordv)
                    399:        int     emptyhead;
                    400:        int     wordc;
                    401:        char    **wordv;
                    402: {
                    403:        reg     char    **nwordv;
                    404:                int     nwordc = emptyhead + wordc;
                    405:        reg     int     i;
                    406:
                    407:        nwordv = (char **)Calloc(nwordc, sizeof (char *));
                    408:        for (i = 0; i < emptyhead; i++)
                    409:                nwordv[i] = 0;
                    410:        for(i = emptyhead; i < nwordc; i++){
                    411:                nwordv[i] = wordv[i-emptyhead];
                    412:        }
                    413:        return(nwordv);
                    414: }
                    415: /*
                    416:  *     plural'ize and verb forms
                    417:  */
                    418: static char    *S = "s";
                    419: static char    *N = "";
                    420: char *plural(n)
                    421:        int     n;
                    422: {
                    423:        return( n > 1 ? S : N);
                    424: }
                    425: char *verbform(n)
                    426:        int     n;
                    427: {
                    428:        return( n > 1 ? N : S);
                    429: }
                    430: