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

1.19    ! espie       1: /*     $OpenBSD: misc.c,v 1.18 2000/03/11 15:54:44 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.19    ! espie      44: static char rcsid[] = "$OpenBSD: misc.c,v 1.18 2000/03/11 15:54:44 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.7       espie     246:        errx(1, "interrupted.");
1.1       deraadt   247: }
                    248:
                    249: /*
                    250:  * killdiv - get rid of the diversion files
                    251:  */
                    252: void
                    253: killdiv()
                    254: {
1.12      espie     255:        int n;
1.1       deraadt   256:
                    257:        for (n = 0; n < MAXOUT; n++)
                    258:                if (outfile[n] != NULL) {
                    259:                        (void) fclose(outfile[n]);
                    260:                }
                    261: }
                    262:
1.18      espie     263: void *
1.1       deraadt   264: xalloc(n)
1.14      espie     265:        size_t n;
1.1       deraadt   266: {
1.12      espie     267:        char *p = malloc(n);
1.1       deraadt   268:
                    269:        if (p == NULL)
1.7       espie     270:                err(1, "malloc");
1.1       deraadt   271:        return p;
                    272: }
                    273:
                    274: char *
                    275: xstrdup(s)
1.12      espie     276:        const char *s;
1.1       deraadt   277: {
1.12      espie     278:        char *p = strdup(s);
1.1       deraadt   279:        if (p == NULL)
1.7       espie     280:                err(1, "strdup");
1.1       deraadt   281:        return p;
                    282: }
                    283:
                    284: void
                    285: usage()
                    286: {
1.11      espie     287:        fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname] [-I dirname...]\n");
1.1       deraadt   288:        exit(1);
                    289: }
                    290:
1.15      espie     291: int
                    292: obtain_char(f)
                    293:        struct input_file *f;
                    294: {
1.17      espie     295:        if (f->c == EOF)
                    296:                return EOF;
                    297:        else if (f->c == '\n')
1.15      espie     298:                f->lineno++;
                    299:
                    300:        f->c = fgetc(f->file);
                    301:        return f->c;
                    302: }
                    303:
                    304: void
                    305: set_input(f, real, name)
                    306:        struct input_file *f;
                    307:        FILE *real;
                    308:        const char *name;
                    309: {
                    310:        f->file = real;
                    311:        f->lineno = 1;
                    312:        f->c = 0;
                    313:        f->name = xstrdup(name);
                    314: }
                    315:
                    316: void
                    317: release_input(f)
                    318:        struct input_file *f;
                    319: {
                    320:        if (f->file != stdin)
                    321:            fclose(f->file);
1.17      espie     322:        f->c = EOF;
1.16      espie     323:        /*
                    324:         * XXX can't free filename, as there might still be
                    325:         * error information pointing to it.
                    326:         */
1.18      espie     327: }
                    328:
                    329: void
                    330: doprintlineno(f)
                    331:        struct input_file *f;
                    332: {
                    333:        pbunsigned(f->lineno);
                    334: }
                    335:
                    336: void
                    337: doprintfilename(f)
                    338:        struct input_file *f;
                    339: {
                    340:        pbstr(f->name);
1.15      espie     341: }