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