1 static char *sccsid = "@(#)misc.c 4.6 (Berkeley) 93/03/23";
2 #include "defs"
3 #include <stdlib.h>
4
5 FSTATIC struct nameblock *hashtab[HASHSIZE];
6 FSTATIC int nhashed = 0;
7
8
9 /* simple linear hash. hash function is sum of
10 characters mod hash table size.
11 */
hashloc(s)12 hashloc(s)
13 char *s;
14 {
15 register int i;
16 register int hashval;
17 register char *t;
18
19 hashval = 0;
20
21 for(t=s; *t!='\0' ; ++t)
22 hashval += *t;
23
24 hashval %= HASHSIZE;
25
26 for(i=hashval;
27 hashtab[i]!=0 && unequal(s,hashtab[i]->namep);
28 i = (i+1)%HASHSIZE ) ;
29
30 return(i);
31 }
32
33
srchname(s)34 struct nameblock *srchname(s)
35 char *s;
36 {
37 return( hashtab[hashloc(s)] );
38 }
39
40
41
makename(s)42 struct nameblock *makename(s)
43 char *s;
44 {
45 /* make a fresh copy of the string s */
46
47 char *copys();
48 register struct nameblock *p;
49
50 if(nhashed++ > HASHSIZE-3)
51 fatal("Hash table overflow");
52
53 p = ALLOC(nameblock);
54 p->nxtnameblock = firstname;
55 p->namep = copys(s);
56 p->linep = 0;
57 p->done = 0;
58 p->septype = 0;
59 p->modtime = 0;
60
61 firstname = p;
62 if(mainname == NULL)
63 if(s[0]!='.' || hasslash(s) )
64 mainname = p;
65
66 hashtab[hashloc(s)] = p;
67
68 return(p);
69 }
70
71
72
hasslash(s)73 hasslash(s)
74 char *s;
75 {
76 for( ; *s ; ++s)
77 if(*s == '/')
78 return(YES);
79 return(NO);
80 }
81
82
83
copys(s)84 char *copys(s)
85 register char *s;
86 {
87 register char *t, *t0;
88
89 if( (t = t0 = calloc( strlen(s)+1 , sizeof(char)) ) == NULL)
90 fatal("out of memory");
91 while(*t++ = *s++)
92 ;
93 return(t0);
94 }
95
96
97
concat(a,b,c)98 char *concat(a,b,c) /* c = concatenation of a and b */
99 register char *a,*b;
100 char *c;
101 {
102 register char *t;
103 t = c;
104
105 while(*t = *a++) t++;
106 while(*t++ = *b++);
107 return(c);
108 }
109
110
111
suffix(a,b,p)112 suffix(a,b,p) /* is b the suffix of a? if so, set p = prefix */
113 register char *a,*b,*p;
114 {
115 char *a0,*b0;
116 a0 = a;
117 b0 = b;
118
119 while(*a++);
120 while(*b++);
121
122 if( (a-a0) < (b-b0) ) return(0);
123
124 while(b>b0)
125 if(*--a != *--b) return(0);
126
127 while(a0<a) *p++ = *a0++;
128 *p = '\0';
129
130 return(1);
131 }
132
133
134
135
136
137
ckalloc(n)138 int *ckalloc(n)
139 register int n;
140 {
141 register int *p;
142
143 if( p = (int *) calloc(1,n) )
144 return(p);
145
146 fatal("out of memory");
147 /* NOTREACHED */
148 }
149
150 /* copy string a into b, substituting for arguments */
subst(a,b)151 char *subst(a,b)
152 register char *a,*b;
153 {
154 static depth = 0;
155 register char *s;
156 char vname[BUFSIZ];
157 struct varblock *varptr(), *vbp;
158 char closer;
159
160 if(++depth > 100)
161 fatal("infinitely recursive macro?");
162 if(a!=0) while(*a)
163 {
164 if(*a != '$') *b++ = *a++;
165 else if(*++a=='\0' || *a=='$')
166 *b++ = *a++;
167 else {
168 s = vname;
169 if( *a=='(' || *a=='{' )
170 {
171 closer = ( *a=='(' ? ')' : '}');
172 ++a;
173 while(*a == ' ') ++a;
174 while(*a!=' ' && *a!=closer && *a!='\0') *s++ = *a++;
175 while(*a!=closer && *a!='\0') ++a;
176 if(*a == closer) ++a;
177 }
178 else *s++ = *a++;
179
180 *s = '\0';
181 if( (vbp = varptr(vname)) ->varval != 0)
182 {
183 b = subst(vbp->varval, b);
184 vbp->used = YES;
185 }
186 }
187 }
188
189 *b = '\0';
190 --depth;
191 return(b);
192 }
193
194
setvar(v,s)195 setvar(v,s)
196 char *v, *s;
197 {
198 struct varblock *varptr(), *p;
199
200 p = varptr(v);
201 if(p->noreset == 0)
202 {
203 p->varval = s;
204 p->noreset = inarglist;
205 if(p->used && unequal(v,"@") && unequal(v,"*")
206 && unequal(v,"<") && unequal(v,"?") )
207 fprintf(stderr, "Warning: %s changed after being used\n",v);
208 }
209 }
210
211
eqsign(a)212 eqsign(a) /*look for arguments with equal signs but not colons */
213 char *a;
214 {
215 register char *s, *t, c;
216
217 while(*a == ' ') ++a;
218 for(s=a ; *s!='\0' && *s!=':' ; ++s)
219 if(*s == '=')
220 {
221 for(t = a ; *t!='=' && *t!=' ' && *t!='\t' ; ++t );
222 c = *t;
223 *t = '\0';
224
225 for(++s; *s==' ' || *s=='\t' ; ++s);
226 setvar(a, copys(s));
227 *t = c;
228 return(YES);
229 }
230
231 return(NO);
232 }
233
234
varptr(v)235 struct varblock *varptr(v)
236 char *v;
237 {
238 register struct varblock *vp;
239
240 for(vp = firstvar; vp ; vp = vp->nxtvarblock)
241 if(! unequal(v , vp->varname))
242 return(vp);
243
244 vp = ALLOC(varblock);
245 vp->nxtvarblock = firstvar;
246 firstvar = vp;
247 vp->varname = copys(v);
248 vp->varval = 0;
249 return(vp);
250 }
251
252
fatal1(s,t)253 fatal1(s, t)
254 char *s, *t;
255 {
256 char buf[BUFSIZ];
257 (void)sprintf(buf, s, t);
258 fatal(buf);
259 }
260
261
262
fatal(s)263 fatal(s)
264 char *s;
265 {
266 if(s) fprintf(stderr, "Make: %s. Stop.\n", s);
267 else fprintf(stderr, "\nStop.\n");
268 #ifdef unix
269 exit(1);
270 #endif
271 #ifdef gcos
272 exit(0);
273 #endif
274 }
275
276
277
yyerror(s)278 yyerror(s)
279 char *s;
280 {
281 char buf[50];
282 extern int yylineno;
283
284 (void)sprintf(buf, "line %d: %s", yylineno, s);
285 fatal(buf);
286 }
287
288
289
appendq(head,tail)290 struct chain *appendq(head, tail)
291 struct chain *head;
292 char *tail;
293 {
294 register struct chain *p, *q;
295
296 p = ALLOC(chain);
297 p->datap = tail;
298
299 if(head)
300 {
301 for(q = head ; q->nextp ; q = q->nextp)
302 ;
303 q->nextp = p;
304 return(head);
305 }
306 else
307 return(p);
308 }
309
310
311
312
313
mkqlist(p)314 char *mkqlist(p)
315 struct chain *p;
316 {
317 register char *qbufp, *s;
318 static char qbuf[QBUFMAX];
319
320 if(p == NULL)
321 {
322 qbuf[0] = '\0';
323 return;
324 }
325
326 qbufp = qbuf;
327
328 for( ; p ; p = p->nextp)
329 {
330 s = p->datap;
331 if(qbufp+strlen(s) > &qbuf[QBUFMAX-3])
332 {
333 fprintf(stderr, "$? list too long\n");
334 break;
335 }
336 while (*s)
337 *qbufp++ = *s++;
338 *qbufp++ = ' ';
339 }
340 *--qbufp = '\0';
341 return(qbuf);
342 }
343