source: trunk/minix/commands/m4/misc.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 4.9 KB
Line 
1/*
2 * misc.c
3 * Facility: m4 macro processor
4 * by: oz
5 */
6
7#include "mdef.h"
8#include "extr.h"
9
10/*
11 * indx - find the index of second str in the
12 * first str.
13 */
14int indx(s1, s2)
15char *s1;
16char *s2;
17{
18 register char *t;
19 register char *p;
20 register char *m;
21
22 for (p = s1; *p; p++) {
23 for (t = p, m = s2; *m && *m == *t; m++, t++)
24 ;
25 if (!*m)
26 return(p - s1);
27 }
28 return (-1);
29}
30
31/*
32 * putback - push character back onto input
33 *
34 */
35void putback (c)
36char c;
37{
38 if (bp < endpbb)
39 *bp++ = c;
40 else
41 error("m4: too many characters pushed back");
42}
43
44/*
45 * pbstr - push string back onto input
46 * putback is replicated to improve
47 * performance.
48 *
49 */
50void pbstr(s)
51register char *s;
52{
53 register char *es;
54 register char *zp;
55
56 es = s;
57 zp = bp;
58
59 while (*es)
60 es++;
61 es--;
62 while (es >= s)
63 if (zp < endpbb)
64 *zp++ = *es--;
65 if ((bp = zp) == endpbb)
66 error("m4: too many characters pushed back");
67}
68
69/*
70 * pbnum - convert number to string, push back on input.
71 *
72 */
73void pbnum (n)
74int n;
75{
76 register int num;
77
78 num = (n < 0) ? -n : n;
79 do {
80 putback(num % 10 + '0');
81 }
82 while ((num /= 10) > 0);
83
84 if (n < 0) putback('-');
85}
86
87/*
88 * chrsave - put single char on string space
89 *
90 */
91void chrsave (c)
92char c;
93{
94/*** if (sp < 0)
95 putc(c, active);
96 else ***/ if (ep < endest)
97 *ep++ = c;
98 else
99 error("m4: string space overflow");
100}
101
102/*
103 * getdiv - read in a diversion file, and
104 * trash it.
105 */
106void getdiv(ind)
107int ind;
108{
109 register int c;
110 register FILE *dfil;
111
112 if (active == outfile[ind])
113 error("m4: undivert: diversion still active.");
114 (void) fclose(outfile[ind]);
115 outfile[ind] = NULL;
116 m4temp[UNIQUE] = ind + '0';
117 if ((dfil = fopen(m4temp, "r")) == NULL)
118 error("m4: cannot undivert.");
119 else
120 while((c = getc(dfil)) != EOF)
121 putc(c, active);
122 (void) fclose(dfil);
123
124#if vms
125 if (remove(m4temp))
126#else
127 if (unlink(m4temp) == -1)
128#endif
129 error("m4: cannot unlink.");
130}
131
132/*
133 * Very fatal error. Close all files
134 * and die hard.
135 */
136void error(s)
137char *s;
138{
139 killdiv();
140 fprintf(stderr,"%s\n",s);
141 exit(1);
142}
143
144/*
145 * Interrupt handling
146 */
147static char *msg = "\ninterrupted.";
148
149void onintr(s)
150int s; /* ANSI requires the parameter */
151{
152 error(msg);
153}
154
155/*
156 * killdiv - get rid of the diversion files
157 *
158 */
159void killdiv() {
160 register int n;
161
162 for (n = 0; n < MAXOUT; n++)
163 if (outfile[n] != NULL) {
164 (void) fclose (outfile[n]);
165 m4temp[UNIQUE] = n + '0';
166#if vms
167 (void) remove (m4temp);
168#else
169 (void) unlink (m4temp);
170#endif
171 }
172}
173
174/*
175 * save a string somewhere..
176 *
177 */
178char *strsave(s)
179char *s;
180{
181 register int n;
182 char *p;
183
184 n = strlen(s)+1;
185 p = (char *) malloc(n);
186 if (p != NULL) (void) memcpy(p, s, n);
187 return (p);
188}
189
190void usage() {
191 fprintf(stderr, "Usage: m4 [-Dname[=val]] [-Uname]\n");
192 exit(1);
193}
194
195#ifdef GETOPT
196/*
197 * H. Spencer getopt - get option letter from argv
198 *
199 *
200#include <stdio.h>
201 *
202 */
203
204char *optarg; /* Global argument pointer. */
205int optind = 0; /* Global argv index. */
206
207static char *scan = NULL; /* Private scan pointer. */
208
209int
210getopt(argc, argv, optstring)
211int argc;
212char *argv[];
213char *optstring;
214{
215 register char c;
216 register char *place;
217
218 optarg = NULL;
219
220 if (scan == NULL || *scan == '\0') {
221 if (optind == 0)
222 optind++;
223
224 if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
225 return(EOF);
226 if (strcmp(argv[optind], "--")==0) {
227 optind++;
228 return(EOF);
229 }
230
231 scan = argv[optind]+1;
232 optind++;
233 }
234
235 c = *scan++;
236 place = index(optstring, c);
237
238 if (place == NULL || c == ':') {
239 fprintf(stderr, "%s: unknown option -%c\n", argv[0], c);
240 return('?');
241 }
242
243 place++;
244 if (*place == ':') {
245 if (*scan != '\0') {
246 optarg = scan;
247 scan = NULL;
248 } else {
249 optarg = argv[optind];
250 optind++;
251 }
252 }
253
254 return(c);
255}
256
257#endif
258
259#ifdef DUFFCP
260/*
261 * This code uses Duff's Device (tm Tom Duff)
262 * to unroll the copying loop:
263 * while (count-- > 0)
264 * *to++ = *from++;
265 */
266
267#define COPYBYTE *to++ = *from++
268
269void memcpy(to, from, count)
270register char *from, *to;
271register int count;
272{
273 if (count > 0) {
274 register int loops = (count+8-1) >> 3; /* div 8 round up */
275
276 switch (count&(8-1)) { /* mod 8 */
277 case 0: do {
278 COPYBYTE;
279 case 7: COPYBYTE;
280 case 6: COPYBYTE;
281 case 5: COPYBYTE;
282 case 4: COPYBYTE;
283 case 3: COPYBYTE;
284 case 2: COPYBYTE;
285 case 1: COPYBYTE;
286 } while (--loops > 0);
287 }
288
289 }
290}
291
292#endif
Note: See TracBrowser for help on using the repository browser.