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