Home | History | Annotate | Line # | Download | only in m4
misc.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1989 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Ozan Yigit.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd #ifndef lint
     38  1.1  cgd static char sccsid[] = "@(#)misc.c	5.6 (Berkeley) 2/26/91";
     39  1.1  cgd #endif /* not lint */
     40  1.1  cgd 
     41  1.1  cgd /*
     42  1.1  cgd  * misc.c
     43  1.1  cgd  * Facility: m4 macro processor
     44  1.1  cgd  * by: oz
     45  1.1  cgd  */
     46  1.1  cgd 
     47  1.1  cgd #include <unistd.h>
     48  1.1  cgd #include <stdio.h>
     49  1.1  cgd #include <stdlib.h>
     50  1.1  cgd #include <string.h>
     51  1.1  cgd #include "mdef.h"
     52  1.1  cgd #include "extr.h"
     53  1.1  cgd #include "pathnames.h"
     54  1.1  cgd 
     55  1.1  cgd /*
     56  1.1  cgd  * indx - find the index of second str in the
     57  1.1  cgd  *        first str.
     58  1.1  cgd  */
     59  1.1  cgd indx(s1, s2)
     60  1.1  cgd char *s1;
     61  1.1  cgd char *s2;
     62  1.1  cgd {
     63  1.1  cgd         register char *t;
     64  1.1  cgd         register char *p;
     65  1.1  cgd         register char *m;
     66  1.1  cgd 
     67  1.1  cgd         for (p = s1; *p; p++) {
     68  1.1  cgd                 for (t = p, m = s2; *m && *m == *t; m++, t++)
     69  1.1  cgd                         ;
     70  1.1  cgd                 if (!*m)
     71  1.1  cgd                         return(p - s1);
     72  1.1  cgd         }
     73  1.1  cgd         return (-1);
     74  1.1  cgd }
     75  1.1  cgd 
     76  1.1  cgd /*
     77  1.1  cgd  *  putback - push character back onto input
     78  1.1  cgd  *
     79  1.1  cgd  */
     80  1.1  cgd putback (c)
     81  1.1  cgd char c;
     82  1.1  cgd {
     83  1.1  cgd         if (bp < endpbb)
     84  1.1  cgd                 *bp++ = c;
     85  1.1  cgd         else
     86  1.1  cgd                 error("m4: too many characters pushed back");
     87  1.1  cgd }
     88  1.1  cgd 
     89  1.1  cgd /*
     90  1.1  cgd  *  pbstr - push string back onto input
     91  1.1  cgd  *          putback is replicated to improve
     92  1.1  cgd  *          performance.
     93  1.1  cgd  *
     94  1.1  cgd  */
     95  1.1  cgd pbstr(s)
     96  1.1  cgd register char *s;
     97  1.1  cgd {
     98  1.1  cgd         register char *es;
     99  1.1  cgd 	register char *zp;
    100  1.1  cgd 
    101  1.1  cgd 	es = s;
    102  1.1  cgd 	zp = bp;
    103  1.1  cgd 
    104  1.1  cgd         while (*es)
    105  1.1  cgd                 es++;
    106  1.1  cgd         es--;
    107  1.1  cgd         while (es >= s)
    108  1.1  cgd                 if (zp < endpbb)
    109  1.1  cgd                         *zp++ = *es--;
    110  1.1  cgd         if ((bp = zp) == endpbb)
    111  1.1  cgd                 error("m4: too many characters pushed back");
    112  1.1  cgd }
    113  1.1  cgd 
    114  1.1  cgd /*
    115  1.1  cgd  *  pbnum - convert number to string, push back on input.
    116  1.1  cgd  *
    117  1.1  cgd  */
    118  1.1  cgd pbnum (n)
    119  1.1  cgd int n;
    120  1.1  cgd {
    121  1.1  cgd         register int num;
    122  1.1  cgd 
    123  1.1  cgd         num = (n < 0) ? -n : n;
    124  1.1  cgd         do {
    125  1.1  cgd                 putback(num % 10 + '0');
    126  1.1  cgd         }
    127  1.1  cgd         while ((num /= 10) > 0);
    128  1.1  cgd 
    129  1.1  cgd         if (n < 0) putback('-');
    130  1.1  cgd }
    131  1.1  cgd 
    132  1.1  cgd /*
    133  1.1  cgd  *  chrsave - put single char on string space
    134  1.1  cgd  *
    135  1.1  cgd  */
    136  1.1  cgd chrsave (c)
    137  1.1  cgd char c;
    138  1.1  cgd {
    139  1.1  cgd /***        if (sp < 0)
    140  1.1  cgd                 putc(c, active);
    141  1.1  cgd         else ***/ if (ep < endest)
    142  1.1  cgd                 *ep++ = c;
    143  1.1  cgd         else
    144  1.1  cgd                 error("m4: string space overflow");
    145  1.1  cgd }
    146  1.1  cgd 
    147  1.1  cgd /*
    148  1.1  cgd  * getdiv - read in a diversion file, and
    149  1.1  cgd  *          trash it.
    150  1.1  cgd  */
    151  1.1  cgd getdiv(ind) {
    152  1.1  cgd         register int c;
    153  1.1  cgd         register FILE *dfil;
    154  1.1  cgd 
    155  1.1  cgd         if (active == outfile[ind])
    156  1.1  cgd                 error("m4: undivert: diversion still active.");
    157  1.1  cgd         (void) fclose(outfile[ind]);
    158  1.1  cgd         outfile[ind] = NULL;
    159  1.1  cgd         m4temp[UNIQUE] = ind + '0';
    160  1.1  cgd         if ((dfil = fopen(m4temp, "r")) == NULL)
    161  1.1  cgd                 error("m4: cannot undivert.");
    162  1.1  cgd         else
    163  1.1  cgd                 while((c = getc(dfil)) != EOF)
    164  1.1  cgd                         putc(c, active);
    165  1.1  cgd         (void) fclose(dfil);
    166  1.1  cgd 
    167  1.1  cgd 	if (unlink(m4temp) == -1)
    168  1.1  cgd                 error("m4: cannot unlink.");
    169  1.1  cgd }
    170  1.1  cgd 
    171  1.1  cgd /*
    172  1.1  cgd  * Very fatal error. Close all files
    173  1.1  cgd  * and die hard.
    174  1.1  cgd  */
    175  1.1  cgd error(s)
    176  1.1  cgd char *s;
    177  1.1  cgd {
    178  1.1  cgd         killdiv();
    179  1.1  cgd         fprintf(stderr,"%s\n",s);
    180  1.1  cgd         exit(1);
    181  1.1  cgd }
    182  1.1  cgd 
    183  1.1  cgd /*
    184  1.1  cgd  * Interrupt handling
    185  1.1  cgd  */
    186  1.1  cgd static char *msg = "\ninterrupted.";
    187  1.1  cgd 
    188  1.1  cgd void
    189  1.1  cgd onintr() {
    190  1.1  cgd         error(msg);
    191  1.1  cgd }
    192  1.1  cgd 
    193  1.1  cgd /*
    194  1.1  cgd  * killdiv - get rid of the diversion files
    195  1.1  cgd  *
    196  1.1  cgd  */
    197  1.1  cgd killdiv() {
    198  1.1  cgd         register int n;
    199  1.1  cgd 
    200  1.1  cgd         for (n = 0; n < MAXOUT; n++)
    201  1.1  cgd                 if (outfile[n] != NULL) {
    202  1.1  cgd                         (void) fclose (outfile[n]);
    203  1.1  cgd                         m4temp[UNIQUE] = n + '0';
    204  1.1  cgd                         (void) unlink (m4temp);
    205  1.1  cgd                 }
    206  1.1  cgd }
    207  1.1  cgd 
    208  1.1  cgd usage() {
    209  1.1  cgd         fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n");
    210  1.1  cgd         exit(1);
    211  1.1  cgd }
    212