# 2.11BSD#431 - /usr/src/usr.bin/m4/misc.c

##### Functions

    basename          X
    chrsave           X
    getdiv            X
    indx              X
    onintr            X
    oops              X
    pbnum             X
    pbstr             X
    putback           X
    usage             X
    xalloc            X
    xstrdup           X

       1: /*
       2:  * Copyright (c) 1989, 1993
       3:  *	The Regents of the University of California.  All rights reserved.
       4:  *
       5:  * This code is derived from software contributed to Berkeley by
       6:  * Ozan Yigit at York University.
       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: #if !defined(lint) && defined(DOSCCS)
      38: static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 6/6/93";
      39: #endif
      40:
      41: #include <sys/types.h>
      42: #include <errno.h>
      43: #include <stdio.h>
      44: #include <string.h>
      45: #include "mdef.h"
      46: #include "stdd.h"
      47: #include "extern.h"
      48: #include "pathnames.h"
      49:
      50: /*
      51:  * find the index of second str in the first str.
      52:  */
      53: int
      54: indx(s1, s2)
      55: char *s1;
      56: char *s2;
      57: {
      58:     register char *t;
      59:     register char *p;
      60:     register char *m;
      61:
      62:     for (p = s1; *p; p++) {
      63:         for (t = p, m = s2; *m && *m == *t; m++, t++);
      64:         if (!*m)
      65:             return (p - s1);
      66:     }
      67:     return (-1);
      68: }
      69: /*
      70:  *  putback - push character back onto input
      71:  */
      72: void
      73: putback(c)
      74: char c;
      75: {
      76:     if (bp < endpbb)
      77:         *bp++ = c;
      78:     else
      79:         oops("too many characters pushed back");
      80: }
      81:
      82: /*
      83:  *  pbstr - push string back onto input
      84:  *          putback is replicated to improve
      85:  *          performance.
      86:  */
      87: void
      88: pbstr(s)
      89: register char *s;
      90: {
      91:     register char *es;
      92:     register char *zp;
      93:
      94:     es = s;
      95:     zp = bp;
      96:
      97:     while (*es)
      98:         es++;
      99:     es--;
     100:     while (es >= s)
     101:         if (zp < endpbb)
     102:             *zp++ = *es--;
     103:     if ((bp = zp) == endpbb)
     104:         oops("too many characters pushed back");
     105: }
     106:
     107: /*
     108:  *  pbnum - convert number to string, push back on input.
     109:  */
     110: void
     111: pbnum(n)
     112: int n;
     113: {
     114:     register int num;
     115:
     116:     num = (n < 0) ? -n : n;
     117:     do {
     118:         putback(num % 10 + '0');
     119:     }
     120:     while ((num /= 10) > 0);
     121:
     122:     if (n < 0)
     123:         putback('-');
     124: }
     125:
     126: /*
     127:  *  chrsave - put single char on string space
     128:  */
     129: void
     130: chrsave(c)
     131: char c;
     132: {
     133:     if (ep < endest)
     134:         *ep++ = c;
     135:     else
     136:         oops("string space overflow");
     137: }
     138:
     139: /*
     140:  * read in a diversion file, and dispose it.
     141:  */
     142: void
     143: getdiv(n)
     144: int n;
     145: {
     146:     register int c;
     147:     register FILE *dfil;
     148:
     149:     if (active == outfile[n])
     150:         oops("%s: diversion still active.", "undivert");
     151:     (void) fclose(outfile[n]);
     152:     outfile[n] = NULL;
     153:     m4temp[UNIQUE] = n + '0';
     154:     if ((dfil = fopen(m4temp, "r")) == NULL)
     155:         oops("%s: cannot undivert.", m4temp);
     156:     else
     157:         while ((c = getc(dfil)) != EOF)
     158:             putc(c, active);
     159:     (void) fclose(dfil);
     160:
     161: #ifdef vms
     162:     if (remove(m4temp))
     163: #else
     164:     if (unlink(m4temp) == -1)
     165: #endif
     166:         oops("%s: cannot unlink.", m4temp);
     167: }
     168:
     169: int
     170: onintr()
     171: {
     172:     oops("interrupted.");
     173: }
     174:
     175: char *
     176: xalloc(n)
     177: unsigned n;
     178: {
     179:     register char *p = malloc(n);
     180:
     181:     if (p == NULL)
     182:         oops("malloc: %s", strerror(errno));
     183:     return p;
     184: }
     185:
     186: char *
     187: xstrdup(s)
     188: char *s;
     189: {
     190:     register char *p = strdup(s);
     191:     if (p == NULL)
     192:         oops("strdup: %s", strerror(errno));
     193:     return p;
     194: }
     195:
     196: char *
     197: basename(s)
     198: register char *s;
     199: {
     200:     register char *p;
     201:     extern char *strrchr();
     202:
     203:     if ((p = strrchr(s, '/')) == NULL)
     204:         return s;
     205:
     206:     return ++p;
     207: }
     208:
     209: void
     210: usage()
     211: {
     212:     fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
     213:     exit(1);
     214: }
     215:
     216: #if __STDC__
     217: #include <stdarg.h>
     218: #else
     219: #include <varargs.h>
     220: #endif
     221:
     222: void
     223: #if __STDC__
     224: oops(const char *fmt, ...)
     225: #else
     226: /* VARARGS1 */
     227: oops(fmt, va_alist)
     228:     char *fmt;
     229:     va_dcl
     230: #endif
     231: {
     232:     va_list ap;
     233: #if __STDC__
     234:     va_start(ap, fmt);
     235: #else
     236:     va_start(ap);
     237: #endif
     238:     (void)fprintf(stderr, "%s: ", progname);
     239:     (void)vfprintf(stderr, fmt, ap);
     240:     va_end(ap);
     241:     (void)fprintf(stderr, "\n");
     242:     exit(1);
     243:     /* NOTREACHED */
     244: }

#### Defined functions

basename defined in line 196; never used
chrsave defined in line 129; never used
getdiv defined in line 142; never used
indx defined in line 53; never used
onintr defined in line 169; never used
oops defined in line 222; used 9 times

- in line 79, 104, 136, 150-155(2), 166, 172, 182, 192

pbnum defined in line 110; never used
pbstr defined in line 87; never used
putback defined in line 72; used 2 times

- in line 118-123(2)

usage defined in line 209; never used
xalloc defined in line 175; never used
xstrdup defined in line 186; never used

#### Defined variables

sccsid defined in line 38; never used

Last modified: 1994-04-06 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1473 |