[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.40

1.40    ! espie       1: /*     $OpenBSD: misc.c,v 1.39 2008/08/16 12:23:50 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.
1.29      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    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: #include <sys/types.h>
                     37: #include <errno.h>
                     38: #include <unistd.h>
1.30      espie      39: #include <stdarg.h>
1.1       deraadt    40: #include <stdio.h>
                     41: #include <stdlib.h>
1.7       espie      42: #include <stddef.h>
1.1       deraadt    43: #include <string.h>
1.7       espie      44: #include <err.h>
1.1       deraadt    45: #include "mdef.h"
                     46: #include "stdd.h"
                     47: #include "extern.h"
                     48: #include "pathnames.h"
                     49:
1.9       espie      50:
                     51: char *ep;              /* first free char in strspace */
                     52: static char *strspace; /* string space for evaluation */
1.24      espie      53: char *endest;          /* end of string space         */
1.9       espie      54: static size_t strsize = STRSPMAX;
                     55: static size_t bufsize = BUFSIZE;
                     56:
1.40    ! espie      57: unsigned char *buf;                    /* push-back buffer            */
        !            58: unsigned char *bufbase;                        /* the base for current ilevel */
        !            59: unsigned char *bbase[MAXINP];          /* the base for each ilevel    */
        !            60: unsigned char *bp;                     /* first available character   */
        !            61: unsigned char *endpbb;                 /* end of push-back buffer     */
1.9       espie      62:
                     63:
1.1       deraadt    64: /*
                     65:  * find the index of second str in the first str.
                     66:  */
1.7       espie      67: ptrdiff_t
1.27      espie      68: indx(const char *s1, const char *s2)
1.1       deraadt    69: {
1.12      espie      70:        char *t;
1.7       espie      71:
1.12      espie      72:        t = strstr(s1, s2);
                     73:        if (t == NULL)
                     74:                return (-1);
1.7       espie      75:        else
1.12      espie      76:                return (t - s1);
1.1       deraadt    77: }
                     78: /*
1.33      espie      79:  *  pushback - push character back onto input
1.1       deraadt    80:  */
                     81: void
1.33      espie      82: pushback(int c)
1.1       deraadt    83: {
1.17      espie      84:        if (c == EOF)
                     85:                return;
1.9       espie      86:        if (bp >= endpbb)
                     87:                enlarge_bufspace();
                     88:        *bp++ = c;
1.1       deraadt    89: }
                     90:
                     91: /*
                     92:  *  pbstr - push string back onto input
1.33      espie      93:  *          pushback is replicated to improve
1.1       deraadt    94:  *          performance.
                     95:  */
                     96: void
1.27      espie      97: pbstr(const char *s)
1.1       deraadt    98: {
1.9       espie      99:        size_t n;
1.1       deraadt   100:
1.9       espie     101:        n = strlen(s);
1.12      espie     102:        while (endpbb - bp <= n)
1.9       espie     103:                enlarge_bufspace();
                    104:        while (n > 0)
                    105:                *bp++ = s[--n];
1.1       deraadt   106: }
                    107:
                    108: /*
                    109:  *  pbnum - convert number to string, push back on input.
                    110:  */
                    111: void
1.27      espie     112: pbnum(int n)
1.1       deraadt   113: {
1.31      espie     114:        pbnumbase(n, 10, 0);
                    115: }
                    116:
                    117: void
                    118: pbnumbase(int n, int base, int d)
                    119: {
                    120:        static char digits[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
1.12      espie     121:        int num;
1.31      espie     122:        int printed = 0;
                    123:
                    124:        if (base > 36)
1.35      espie     125:                m4errx(1, "base %d > 36: not supported.", base);
1.31      espie     126:
                    127:        if (base < 2)
1.35      espie     128:                m4errx(1, "bad base %d for conversion.", base);
1.1       deraadt   129:
                    130:        num = (n < 0) ? -n : n;
                    131:        do {
1.33      espie     132:                pushback(digits[num % base]);
1.31      espie     133:                printed++;
1.1       deraadt   134:        }
1.31      espie     135:        while ((num /= base) > 0);
                    136:
                    137:        if (n < 0)
                    138:                printed++;
                    139:        while (printed++ < d)
1.33      espie     140:                pushback('0');
1.1       deraadt   141:
                    142:        if (n < 0)
1.33      espie     143:                pushback('-');
1.1       deraadt   144: }
                    145:
1.18      espie     146: /*
                    147:  *  pbunsigned - convert unsigned long to string, push back on input.
                    148:  */
                    149: void
1.27      espie     150: pbunsigned(unsigned long n)
1.18      espie     151: {
                    152:        do {
1.33      espie     153:                pushback(n % 10 + '0');
1.18      espie     154:        }
                    155:        while ((n /= 10) > 0);
                    156: }
1.9       espie     157:
                    158: void
                    159: initspaces()
                    160: {
                    161:        int i;
                    162:
1.30      espie     163:        strspace = xalloc(strsize+1, NULL);
1.9       espie     164:        ep = strspace;
                    165:        endest = strspace+strsize;
1.40    ! espie     166:        buf = (unsigned char *)xalloc(bufsize, NULL);
1.9       espie     167:        bufbase = buf;
                    168:        bp = buf;
                    169:        endpbb = buf + bufsize;
                    170:        for (i = 0; i < MAXINP; i++)
                    171:                bbase[i] = buf;
                    172: }
                    173:
1.24      espie     174: void
1.17      espie     175: enlarge_strspace()
1.9       espie     176: {
                    177:        char *newstrspace;
1.19      espie     178:        int i;
1.9       espie     179:
                    180:        strsize *= 2;
                    181:        newstrspace = malloc(strsize + 1);
                    182:        if (!newstrspace)
                    183:                errx(1, "string space overflow");
                    184:        memcpy(newstrspace, strspace, strsize/2);
1.19      espie     185:        for (i = 0; i <= sp; i++)
                    186:                if (sstack[i])
                    187:                        mstack[i].sstr = (mstack[i].sstr - strspace)
                    188:                            + newstrspace;
1.9       espie     189:        ep = (ep-strspace) + newstrspace;
1.19      espie     190:        free(strspace);
1.9       espie     191:        strspace = newstrspace;
                    192:        endest = strspace + strsize;
                    193: }
                    194:
1.24      espie     195: void
1.17      espie     196: enlarge_bufspace()
1.9       espie     197: {
1.40    ! espie     198:        unsigned char *newbuf;
1.9       espie     199:        int i;
                    200:
1.30      espie     201:        bufsize += bufsize/2;
                    202:        newbuf = xrealloc(buf, bufsize, "too many characters pushed back");
1.9       espie     203:        for (i = 0; i < MAXINP; i++)
                    204:                bbase[i] = (bbase[i]-buf)+newbuf;
                    205:        bp = (bp-buf)+newbuf;
                    206:        bufbase = (bufbase-buf)+newbuf;
                    207:        buf = newbuf;
1.10      espie     208:        endpbb = buf+bufsize;
1.9       espie     209: }
                    210:
1.1       deraadt   211: /*
                    212:  *  chrsave - put single char on string space
                    213:  */
                    214: void
1.27      espie     215: chrsave(int c)
1.1       deraadt   216: {
1.9       espie     217:        if (ep >= endest)
                    218:                enlarge_strspace();
                    219:        *ep++ = c;
1.1       deraadt   220: }
                    221:
                    222: /*
                    223:  * read in a diversion file, and dispose it.
                    224:  */
                    225: void
1.27      espie     226: getdiv(int n)
1.1       deraadt   227: {
1.12      espie     228:        int c;
1.1       deraadt   229:
                    230:        if (active == outfile[n])
1.35      espie     231:                m4errx(1, "undivert: diversion still active.");
1.8       espie     232:        rewind(outfile[n]);
                    233:        while ((c = getc(outfile[n])) != EOF)
                    234:                putc(c, active);
1.1       deraadt   235:        (void) fclose(outfile[n]);
1.13      espie     236:        outfile[n] = NULL;
1.1       deraadt   237: }
                    238:
                    239: void
1.27      espie     240: onintr(int signo)
1.1       deraadt   241: {
1.21      espie     242: #define intrmessage    "m4: interrupted.\n"
1.26      deraadt   243:        write(STDERR_FILENO, intrmessage, sizeof(intrmessage)-1);
1.21      espie     244:        _exit(1);
1.1       deraadt   245: }
                    246:
                    247: /*
                    248:  * killdiv - get rid of the diversion files
                    249:  */
                    250: void
                    251: killdiv()
                    252: {
1.12      espie     253:        int n;
1.1       deraadt   254:
1.20      espie     255:        for (n = 0; n < maxout; n++)
1.1       deraadt   256:                if (outfile[n] != NULL) {
                    257:                        (void) fclose(outfile[n]);
                    258:                }
1.35      espie     259: }
                    260:
                    261: extern char *__progname;
                    262:
                    263: void
                    264: m4errx(int eval, const char *fmt, ...)
                    265: {
                    266:        fprintf(stderr, "%s: ", __progname);
                    267:        fprintf(stderr, "%s at line %lu: ", CURRENT_NAME, CURRENT_LINE);
1.37      ray       268:        if (fmt != NULL) {
                    269:                va_list ap;
                    270:
                    271:                va_start(ap, fmt);
1.35      espie     272:                vfprintf(stderr, fmt, ap);
1.37      ray       273:                va_end(ap);
                    274:        }
1.35      espie     275:        fprintf(stderr, "\n");
                    276:        exit(eval);
1.20      espie     277: }
                    278:
                    279: /*
                    280:  * resizedivs: allocate more diversion files */
                    281: void
1.27      espie     282: resizedivs(int n)
1.20      espie     283: {
                    284:        int i;
                    285:
1.30      espie     286:        outfile = (FILE **)xrealloc(outfile, sizeof(FILE *) * n,
                    287:            "too many diverts %d", n);
1.20      espie     288:        for (i = maxout; i < n; i++)
                    289:                outfile[i] = NULL;
                    290:        maxout = n;
1.1       deraadt   291: }
                    292:
1.18      espie     293: void *
1.30      espie     294: xalloc(size_t n, const char *fmt, ...)
1.1       deraadt   295: {
1.30      espie     296:        void *p = malloc(n);
1.1       deraadt   297:
1.30      espie     298:        if (p == NULL) {
                    299:                if (fmt == NULL)
                    300:                        err(1, "malloc");
                    301:                else {
                    302:                        va_list va;
                    303:
                    304:                        va_start(va, fmt);
                    305:                        verr(1, fmt, va);
                    306:                        va_end(va);
                    307:                }
                    308:        }
                    309:        return p;
                    310: }
                    311:
                    312: void *
                    313: xrealloc(void *old, size_t n, const char *fmt, ...)
                    314: {
                    315:        char *p = realloc(old, n);
                    316:
                    317:        if (p == NULL) {
                    318:                free(old);
                    319:                if (fmt == NULL)
                    320:                        err(1, "realloc");
                    321:                else {
                    322:                        va_list va;
                    323:
                    324:                        va_start(va, fmt);
                    325:                        verr(1, fmt, va);
                    326:                        va_end(va);
                    327:                }
                    328:        }
1.1       deraadt   329:        return p;
                    330: }
                    331:
                    332: char *
1.27      espie     333: xstrdup(const char *s)
1.1       deraadt   334: {
1.12      espie     335:        char *p = strdup(s);
1.1       deraadt   336:        if (p == NULL)
1.7       espie     337:                err(1, "strdup");
1.1       deraadt   338:        return p;
                    339: }
                    340:
                    341: void
                    342: usage()
                    343: {
1.36      jmc       344:        fprintf(stderr, "usage: m4 [-gs] [-Dname[=value]] [-d flags] "
                    345:                        "[-I dirname] [-o filename]\n"
                    346:                        "\t[-t macro] [-Uname] [file ...]\n");
1.1       deraadt   347:        exit(1);
                    348: }
                    349:
1.15      espie     350: int
1.27      espie     351: obtain_char(struct input_file *f)
1.15      espie     352: {
1.17      espie     353:        if (f->c == EOF)
                    354:                return EOF;
1.33      espie     355:
                    356:        f->c = fgetc(f->file);
                    357:        if (f->c == '\n')
1.15      espie     358:                f->lineno++;
                    359:
                    360:        return f->c;
                    361: }
                    362:
                    363: void
1.27      espie     364: set_input(struct input_file *f, FILE *real, const char *name)
1.15      espie     365: {
                    366:        f->file = real;
                    367:        f->lineno = 1;
                    368:        f->c = 0;
                    369:        f->name = xstrdup(name);
1.28      espie     370:        emit_synchline();
                    371: }
                    372:
                    373: void
                    374: do_emit_synchline()
                    375: {
                    376:        fprintf(active, "#line %lu \"%s\"\n",
                    377:            infile[ilevel].lineno, infile[ilevel].name);
                    378:        infile[ilevel].synch_lineno = infile[ilevel].lineno;
1.15      espie     379: }
                    380:
                    381: void
1.27      espie     382: release_input(struct input_file *f)
1.15      espie     383: {
                    384:        if (f->file != stdin)
                    385:            fclose(f->file);
1.17      espie     386:        f->c = EOF;
1.16      espie     387:        /*
                    388:         * XXX can't free filename, as there might still be
                    389:         * error information pointing to it.
                    390:         */
1.18      espie     391: }
                    392:
                    393: void
1.27      espie     394: doprintlineno(struct input_file *f)
1.18      espie     395: {
                    396:        pbunsigned(f->lineno);
                    397: }
                    398:
                    399: void
1.27      espie     400: doprintfilename(struct input_file *f)
1.18      espie     401: {
1.25      espie     402:        pbstr(rquote);
1.18      espie     403:        pbstr(f->name);
1.25      espie     404:        pbstr(lquote);
1.22      espie     405: }
                    406:
                    407: /*
                    408:  * buffer_mark/dump_buffer: allows one to save a mark in a buffer,
                    409:  * and later dump everything that was added since then to a file.
                    410:  */
                    411: size_t
                    412: buffer_mark()
                    413: {
                    414:        return bp - buf;
                    415: }
                    416:
                    417:
                    418: void
1.27      espie     419: dump_buffer(FILE *f, size_t m)
1.22      espie     420: {
1.40    ! espie     421:        unsigned char *s;
1.22      espie     422:
1.23      espie     423:        for (s = bp; s-buf > m;)
1.22      espie     424:                fputc(*--s, f);
1.15      espie     425: }