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