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

1.7       espie       1: /*     $OpenBSD: misc.c,v 1.6 1997/12/10 20:24:17 deraadt 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.7       espie      44: static char rcsid[] = "$OpenBSD: misc.c,v 1.6 1997/12/10 20:24:17 deraadt 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:
                     61: /*
                     62:  * find the index of second str in the first str.
                     63:  */
1.7       espie      64: ptrdiff_t
1.1       deraadt    65: indx(s1, s2)
1.7       espie      66: const char *s1;
                     67: const char *s2;
1.1       deraadt    68: {
1.7       espie      69:        char *r;
                     70:
                     71:        r = strstr(s1, s2);
                     72:        if (r)
                     73:                return (r - s1);
                     74:        else
                     75:                return (-1);
1.1       deraadt    76: }
                     77: /*
                     78:  *  putback - push character back onto input
                     79:  */
                     80: void
                     81: putback(c)
1.6       deraadt    82: pbent c;
1.1       deraadt    83: {
                     84:        if (bp < endpbb)
                     85:                *bp++ = c;
                     86:        else
1.7       espie      87:                errx(1, "too many characters pushed back");
1.1       deraadt    88: }
                     89:
                     90: /*
                     91:  *  pbstr - push string back onto input
                     92:  *          putback is replicated to improve
                     93:  *          performance.
                     94:  */
                     95: void
                     96: pbstr(s)
                     97: register char *s;
                     98: {
                     99:        register char *es;
1.6       deraadt   100:        pbent *zp;
1.1       deraadt   101:
                    102:        es = s;
                    103:        zp = bp;
                    104:
                    105:        while (*es)
                    106:                es++;
                    107:        es--;
                    108:        while (es >= s)
                    109:                if (zp < endpbb)
                    110:                        *zp++ = *es--;
                    111:        if ((bp = zp) == endpbb)
1.7       espie     112:                errx(1, "too many characters pushed back");
1.1       deraadt   113: }
                    114:
                    115: /*
                    116:  *  pbnum - convert number to string, push back on input.
                    117:  */
                    118: void
                    119: pbnum(n)
                    120: int n;
                    121: {
                    122:        register int num;
                    123:
                    124:        num = (n < 0) ? -n : n;
                    125:        do {
                    126:                putback(num % 10 + '0');
                    127:        }
                    128:        while ((num /= 10) > 0);
                    129:
                    130:        if (n < 0)
                    131:                putback('-');
                    132: }
                    133:
                    134: /*
                    135:  *  chrsave - put single char on string space
                    136:  */
                    137: void
                    138: chrsave(c)
                    139: char c;
                    140: {
                    141:        if (ep < endest)
                    142:                *ep++ = c;
                    143:        else
1.7       espie     144:                errx(1, "string space overflow");
1.1       deraadt   145: }
                    146:
                    147: /*
                    148:  * read in a diversion file, and dispose it.
                    149:  */
                    150: void
                    151: getdiv(n)
                    152: int n;
                    153: {
                    154:        register int c;
                    155:
                    156:        if (active == outfile[n])
1.7       espie     157:                errx(1, "undivert: diversion still active");
1.8     ! espie     158:        rewind(outfile[n]);
        !           159:        while ((c = getc(outfile[n])) != EOF)
        !           160:                putc(c, active);
1.1       deraadt   161:        (void) fclose(outfile[n]);
                    162: }
                    163:
                    164: void
                    165: onintr(signo)
                    166:        int signo;
                    167: {
1.7       espie     168:        errx(1, "interrupted.");
1.1       deraadt   169: }
                    170:
                    171: /*
                    172:  * killdiv - get rid of the diversion files
                    173:  */
                    174: void
                    175: killdiv()
                    176: {
                    177:        register int n;
                    178:
                    179:        for (n = 0; n < MAXOUT; n++)
                    180:                if (outfile[n] != NULL) {
                    181:                        (void) fclose(outfile[n]);
                    182:                }
                    183: }
                    184:
                    185: char *
                    186: xalloc(n)
                    187: unsigned long n;
                    188: {
                    189:        register char *p = malloc(n);
                    190:
                    191:        if (p == NULL)
1.7       espie     192:                err(1, "malloc");
1.1       deraadt   193:        return p;
                    194: }
                    195:
                    196: char *
                    197: xstrdup(s)
                    198: const char *s;
                    199: {
                    200:        register char *p = strdup(s);
                    201:        if (p == NULL)
1.7       espie     202:                err(1, "strdup");
1.1       deraadt   203:        return p;
                    204: }
                    205:
                    206: void
                    207: usage()
                    208: {
                    209:        fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
                    210:        exit(1);
                    211: }
                    212: