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

Annotation of src/usr.bin/m4/misc.c, Revision 1.21

1.21    ! espie       1: /*     $OpenBSD: misc.c,v 1.20 2000/07/27 17:44:33 espie Exp $ */
1.1       deraadt     2: /*     $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $     */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Ozan Yigit at York University.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)misc.c     8.1 (Berkeley) 6/6/93";
                     43: #else
1.21    ! espie      44: static char rcsid[] = "$OpenBSD: misc.c,v 1.20 2000/07/27 17:44:33 espie Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
                     48: #include <sys/types.h>
                     49: #include <errno.h>
                     50: #include <unistd.h>
                     51: #include <stdio.h>
                     52: #include <stdlib.h>
1.7       espie      53: #include <stddef.h>
1.1       deraadt    54: #include <string.h>
1.7       espie      55: #include <err.h>
1.1       deraadt    56: #include "mdef.h"
                     57: #include "stdd.h"
                     58: #include "extern.h"
                     59: #include "pathnames.h"
                     60:
1.9       espie      61:
                     62: char *ep;              /* first free char in strspace */
                     63: static char *strspace; /* string space for evaluation */
                     64: static char *endest;   /* end of string space         */
                     65: static size_t strsize = STRSPMAX;
                     66: static size_t bufsize = BUFSIZE;
                     67:
1.17      espie      68: char *buf;                     /* push-back buffer            */
                     69: char *bufbase;                 /* the base for current ilevel */
                     70: char *bbase[MAXINP];           /* the base for each ilevel    */
                     71: char *bp;                      /* first available character   */
                     72: static char *endpbb;                   /* end of push-back buffer     */
1.9       espie      73:
                     74:
1.14      espie      75: static void enlarge_bufspace __P((void));
                     76: static void enlarge_strspace __P((void));
1.1       deraadt    77: /*
                     78:  * find the index of second str in the first str.
                     79:  */
1.7       espie      80: ptrdiff_t
1.1       deraadt    81: indx(s1, s2)
1.12      espie      82:        const char *s1;
                     83:        const char *s2;
1.1       deraadt    84: {
1.12      espie      85:        char *t;
1.7       espie      86:
1.12      espie      87:        t = strstr(s1, s2);
                     88:        if (t == NULL)
                     89:                return (-1);
1.7       espie      90:        else
1.12      espie      91:                return (t - s1);
1.1       deraadt    92: }
                     93: /*
                     94:  *  putback - push character back onto input
                     95:  */
                     96: void
                     97: putback(c)
1.17      espie      98:        int c;
1.1       deraadt    99: {
1.17      espie     100:        if (c == EOF)
                    101:                return;
1.9       espie     102:        if (bp >= endpbb)
                    103:                enlarge_bufspace();
                    104:        *bp++ = c;
1.1       deraadt   105: }
                    106:
                    107: /*
                    108:  *  pbstr - push string back onto input
                    109:  *          putback is replicated to improve
                    110:  *          performance.
                    111:  */
                    112: void
                    113: pbstr(s)
1.14      espie     114:        const char *s;
1.1       deraadt   115: {
1.9       espie     116:        size_t n;
1.1       deraadt   117:
1.9       espie     118:        n = strlen(s);
1.12      espie     119:        while (endpbb - bp <= n)
1.9       espie     120:                enlarge_bufspace();
                    121:        while (n > 0)
                    122:                *bp++ = s[--n];
1.1       deraadt   123: }
                    124:
                    125: /*
                    126:  *  pbnum - convert number to string, push back on input.
                    127:  */
                    128: void
                    129: pbnum(n)
1.12      espie     130:        int n;
1.1       deraadt   131: {
1.12      espie     132:        int num;
1.1       deraadt   133:
                    134:        num = (n < 0) ? -n : n;
                    135:        do {
                    136:                putback(num % 10 + '0');
                    137:        }
                    138:        while ((num /= 10) > 0);
                    139:
                    140:        if (n < 0)
                    141:                putback('-');
                    142: }
                    143:
1.18      espie     144: /*
                    145:  *  pbunsigned - convert unsigned long to string, push back on input.
                    146:  */
                    147: void
                    148: pbunsigned(n)
                    149:        unsigned long n;
                    150: {
                    151:        do {
                    152:                putback(n % 10 + '0');
                    153:        }
                    154:        while ((n /= 10) > 0);
                    155: }
1.9       espie     156:
                    157: void
                    158: initspaces()
                    159: {
                    160:        int i;
                    161:
                    162:        strspace = xalloc(strsize+1);
                    163:        ep = strspace;
                    164:        endest = strspace+strsize;
1.17      espie     165:        buf = (char *)xalloc(bufsize);
1.9       espie     166:        bufbase = buf;
                    167:        bp = buf;
                    168:        endpbb = buf + bufsize;
                    169:        for (i = 0; i < MAXINP; i++)
                    170:                bbase[i] = buf;
                    171: }
                    172:
1.17      espie     173: static void
                    174: enlarge_strspace()
1.9       espie     175: {
                    176:        char *newstrspace;
1.19      espie     177:        int i;
1.9       espie     178:
                    179:        strsize *= 2;
                    180:        newstrspace = malloc(strsize + 1);
                    181:        if (!newstrspace)
                    182:                errx(1, "string space overflow");
                    183:        memcpy(newstrspace, strspace, strsize/2);
1.19      espie     184:        for (i = 0; i <= sp; i++)
                    185:                if (sstack[i])
                    186:                        mstack[i].sstr = (mstack[i].sstr - strspace)
                    187:                            + newstrspace;
1.9       espie     188:        ep = (ep-strspace) + newstrspace;
1.19      espie     189:        free(strspace);
1.9       espie     190:        strspace = newstrspace;
                    191:        endest = strspace + strsize;
                    192: }
                    193:
1.19      espie     194: static void
1.17      espie     195: enlarge_bufspace()
1.9       espie     196: {
1.17      espie     197:        char *newbuf;
1.9       espie     198:        int i;
                    199:
                    200:        bufsize *= 2;
1.17      espie     201:        newbuf = realloc(buf, bufsize);
1.9       espie     202:        if (!newbuf)
                    203:                errx(1, "too many characters pushed back");
                    204:        for (i = 0; i < MAXINP; i++)
                    205:                bbase[i] = (bbase[i]-buf)+newbuf;
                    206:        bp = (bp-buf)+newbuf;
                    207:        bufbase = (bufbase-buf)+newbuf;
                    208:        buf = newbuf;
1.10      espie     209:        endpbb = buf+bufsize;
1.9       espie     210: }
                    211:
1.1       deraadt   212: /*
                    213:  *  chrsave - put single char on string space
                    214:  */
                    215: void
                    216: chrsave(c)
1.14      espie     217:        int c;
1.1       deraadt   218: {
1.9       espie     219:        if (ep >= endest)
                    220:                enlarge_strspace();
                    221:        *ep++ = c;
1.1       deraadt   222: }
                    223:
                    224: /*
                    225:  * read in a diversion file, and dispose it.
                    226:  */
                    227: void
                    228: getdiv(n)
1.12      espie     229:        int n;
1.1       deraadt   230: {
1.12      espie     231:        int c;
1.1       deraadt   232:
                    233:        if (active == outfile[n])
1.7       espie     234:                errx(1, "undivert: diversion still active");
1.8       espie     235:        rewind(outfile[n]);
                    236:        while ((c = getc(outfile[n])) != EOF)
                    237:                putc(c, active);
1.1       deraadt   238:        (void) fclose(outfile[n]);
1.13      espie     239:        outfile[n] = NULL;
1.1       deraadt   240: }
                    241:
                    242: void
                    243: onintr(signo)
                    244:        int signo;
                    245: {
1.21    ! espie     246: #define intrmessage    "m4: interrupted.\n"
        !           247:        write(STDERR_FILENO, intrmessage, sizeof(intrmessage));
        !           248:        _exit(1);
1.1       deraadt   249: }
                    250:
                    251: /*
                    252:  * killdiv - get rid of the diversion files
                    253:  */
                    254: void
                    255: killdiv()
                    256: {
1.12      espie     257:        int n;
1.1       deraadt   258:
1.20      espie     259:        for (n = 0; n < maxout; n++)
1.1       deraadt   260:                if (outfile[n] != NULL) {
                    261:                        (void) fclose(outfile[n]);
                    262:                }
1.20      espie     263: }
                    264:
                    265: /*
                    266:  * resizedivs: allocate more diversion files */
                    267: void
                    268: resizedivs(n)
                    269:        int n;
                    270: {
                    271:        int i;
                    272:
                    273:        outfile = (FILE **)realloc(outfile, sizeof(FILE *) * n);
                    274:        if (outfile == NULL)
                    275:                    errx(1, "too many diverts %d", n);
                    276:        for (i = maxout; i < n; i++)
                    277:                outfile[i] = NULL;
                    278:        maxout = n;
1.1       deraadt   279: }
                    280:
1.18      espie     281: void *
1.1       deraadt   282: xalloc(n)
1.14      espie     283:        size_t n;
1.1       deraadt   284: {
1.12      espie     285:        char *p = malloc(n);
1.1       deraadt   286:
                    287:        if (p == NULL)
1.7       espie     288:                err(1, "malloc");
1.1       deraadt   289:        return p;
                    290: }
                    291:
                    292: char *
                    293: xstrdup(s)
1.12      espie     294:        const char *s;
1.1       deraadt   295: {
1.12      espie     296:        char *p = strdup(s);
1.1       deraadt   297:        if (p == NULL)
1.7       espie     298:                err(1, "strdup");
1.1       deraadt   299:        return p;
                    300: }
                    301:
                    302: void
                    303: usage()
                    304: {
1.11      espie     305:        fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
1.1       deraadt   306:        exit(1);
                    307: }
                    308:
1.15      espie     309: int
                    310: obtain_char(f)
                    311:        struct input_file *f;
                    312: {
1.17      espie     313:        if (f->c == EOF)
                    314:                return EOF;
                    315:        else if (f->c == '\n')
1.15      espie     316:                f->lineno++;
                    317:
                    318:        f->c = fgetc(f->file);
                    319:        return f->c;
                    320: }
                    321:
                    322: void
                    323: set_input(f, real, name)
                    324:        struct input_file *f;
                    325:        FILE *real;
                    326:        const char *name;
                    327: {
                    328:        f->file = real;
                    329:        f->lineno = 1;
                    330:        f->c = 0;
                    331:        f->name = xstrdup(name);
                    332: }
                    333:
                    334: void
                    335: release_input(f)
                    336:        struct input_file *f;
                    337: {
                    338:        if (f->file != stdin)
                    339:            fclose(f->file);
1.17      espie     340:        f->c = EOF;
1.16      espie     341:        /*
                    342:         * XXX can't free filename, as there might still be
                    343:         * error information pointing to it.
                    344:         */
1.18      espie     345: }
                    346:
                    347: void
                    348: doprintlineno(f)
                    349:        struct input_file *f;
                    350: {
                    351:        pbunsigned(f->lineno);
                    352: }
                    353:
                    354: void
                    355: doprintfilename(f)
                    356:        struct input_file *f;
                    357: {
                    358:        pbstr(f->name);
1.15      espie     359: }