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 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; 40 #endif 41 static const char rcsid[] = 42 "$FreeBSD$"; 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 #include <err.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include "mdef.h" 52 #include "stdd.h" 53 #include "extern.h" 54 #include "pathnames.h" 55 56 /* 57 * find the index of second str in the first str. 58 */ 59 int 60 indx(s1, s2) 61 char *s1; 62 char *s2; 63 { 64 register char *t; 65 register char *p; 66 register char *m; 67 68 for (p = s1; *p; p++) { 69 for (t = p, m = s2; *m && *m == *t; m++, t++); 70 if (!*m) 71 return (p - s1); 72 } 73 return (-1); 74 } 75 /* 76 * putback - push character back onto input 77 */ 78 void 79 putback(c) 80 int c; 81 { 82 if (c == EOF) 83 c = 0; 84 else if (c == 0) 85 return; 86 if (bp < endpbb) 87 *bp++ = c; 88 else 89 errx(1, "too many characters pushed back"); 90 } 91 92 /* 93 * pbstr - push string back onto input 94 * putback is replicated to improve 95 * performance. 96 */ 97 void 98 pbstr(s) 99 register unsigned char *s; 100 { 101 register unsigned char *es; 102 register unsigned char *zp; 103 104 es = s; 105 zp = bp; 106 107 while (*es) 108 es++; 109 es--; 110 while (es >= s) 111 if (zp < endpbb) 112 *zp++ = *es--; 113 if ((bp = zp) == endpbb) 114 errx(1, "too many characters pushed back"); 115 } 116 117 /* 118 * pbnum - convert number to string, push back on input. 119 */ 120 void 121 pbnum(n) 122 int n; 123 { 124 register int num; 125 126 num = (n < 0) ? -n : n; 127 do { 128 putback(num % 10 + '0'); 129 } 130 while ((num /= 10) > 0); 131 132 if (n < 0) 133 putback('-'); 134 } 135 136 /* 137 * chrsave - put single char on string space 138 */ 139 void 140 chrsave(c) 141 char c; 142 { 143 if (ep < endest) 144 *ep++ = c; 145 else 146 errx(1, "string space overflow"); 147 } 148 149 /* 150 * read in a diversion file, and dispose it. 151 */ 152 void 153 getdiv(n) 154 int n; 155 { 156 register int c; 157 register FILE *dfil; 158 159 if (active == outfile[n]) 160 errx(1, "undivert: diversion still active"); 161 (void) fclose(outfile[n]); 162 outfile[n] = NULL; 163 m4temp[UNIQUE] = n + '0'; 164 if ((dfil = fopen(m4temp, "r")) == NULL) 165 errx(1, "%s: cannot undivert", m4temp); 166 else 167 while ((c = getc(dfil)) != EOF) 168 putc(c, active); 169 (void) fclose(dfil); 170 171 #ifdef vms 172 if (remove(m4temp)) 173 #else 174 if (unlink(m4temp) == -1) 175 #endif 176 errx(1, "%s: cannot unlink", m4temp); 177 } 178 179 void 180 onintr(signo) 181 int signo; 182 { 183 errx(1, "interrupted"); 184 } 185 186 /* 187 * killdiv - get rid of the diversion files 188 */ 189 void 190 killdiv() 191 { 192 register int n; 193 194 for (n = 0; n < MAXOUT; n++) 195 if (outfile[n] != NULL) { 196 (void) fclose(outfile[n]); 197 m4temp[UNIQUE] = n + '0'; 198 #ifdef vms 199 (void) remove(m4temp); 200 #else 201 (void) unlink(m4temp); 202 #endif 203 } 204 } 205 206 void 207 cleanup(n) 208 int n; 209 { 210 if (outfile[0] != NULL) { 211 (void) fclose(outfile[0]); 212 outfile[0] = NULL; 213 m4temp[UNIQUE] = '0'; 214 (void) remove(m4temp); 215 } 216 (void) remove(m4dir); 217 } 218 219 void 220 usage() 221 { 222 fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n"); 223 exit(1); 224 } 225