Annotation of 43BSDReno/pgrm/m4/misc.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.