xref: /csrg-svn/bin/csh/misc.c (revision 1300)
1*1300Sbill static	char *sccsid = "@(#)misc.c 4.1 10/09/80";
2*1300Sbill 
3*1300Sbill #include "sh.h"
4*1300Sbill 
5*1300Sbill /*
6*1300Sbill  * C Shell
7*1300Sbill  */
8*1300Sbill 
9*1300Sbill letter(c)
10*1300Sbill 	register char c;
11*1300Sbill {
12*1300Sbill 
13*1300Sbill 	return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_');
14*1300Sbill }
15*1300Sbill 
16*1300Sbill digit(c)
17*1300Sbill 	register char c;
18*1300Sbill {
19*1300Sbill 
20*1300Sbill 	return (c >= '0' && c <= '9');
21*1300Sbill }
22*1300Sbill 
23*1300Sbill alnum(c)
24*1300Sbill 	register char c;
25*1300Sbill {
26*1300Sbill 	return (letter(c) || digit(c));
27*1300Sbill }
28*1300Sbill 
29*1300Sbill any(c, s)
30*1300Sbill 	register int c;
31*1300Sbill 	register char *s;
32*1300Sbill {
33*1300Sbill 
34*1300Sbill 	while (*s)
35*1300Sbill 		if (*s++ == c)
36*1300Sbill 			return(1);
37*1300Sbill 	return(0);
38*1300Sbill }
39*1300Sbill 
40*1300Sbill char *
41*1300Sbill calloc(i, j)
42*1300Sbill 	register unsigned i;
43*1300Sbill 	unsigned j;
44*1300Sbill {
45*1300Sbill 	register char *cp, *dp;
46*1300Sbill #ifdef debug
47*1300Sbill 	static char *av[2] = {0, 0};
48*1300Sbill #endif
49*1300Sbill 
50*1300Sbill 	i *= j;
51*1300Sbill 	cp = (char *) malloc(i);
52*1300Sbill 	if (cp == 0) {
53*1300Sbill 		child++;
54*1300Sbill #ifndef debug
55*1300Sbill 		error("Out of memory");
56*1300Sbill #else
57*1300Sbill 		showall(av);
58*1300Sbill 		printf("i=%d, j=%d: ", i/j, j);
59*1300Sbill 		printf("Out of memory\n");
60*1300Sbill 		chdir("/usr/bill/cshcore");
61*1300Sbill 		abort();
62*1300Sbill #endif
63*1300Sbill 	}
64*1300Sbill 	dp = cp;
65*1300Sbill 	if (i != 0)
66*1300Sbill 		do
67*1300Sbill 			*dp++ = 0;
68*1300Sbill 		while (--i);
69*1300Sbill 	return (cp);
70*1300Sbill }
71*1300Sbill 
72*1300Sbill cfree(p)
73*1300Sbill 	char *p;
74*1300Sbill {
75*1300Sbill 
76*1300Sbill 	free(p);
77*1300Sbill }
78*1300Sbill 
79*1300Sbill char **
80*1300Sbill blkend(up)
81*1300Sbill 	register char **up;
82*1300Sbill {
83*1300Sbill 
84*1300Sbill 	while (*up)
85*1300Sbill 		up++;
86*1300Sbill 	return (up);
87*1300Sbill }
88*1300Sbill 
89*1300Sbill blkpr(av)
90*1300Sbill 	register char **av;
91*1300Sbill {
92*1300Sbill 
93*1300Sbill 	for (; *av; av++) {
94*1300Sbill 		printf("%s", *av);
95*1300Sbill 		if (av[1])
96*1300Sbill 			printf(" ");
97*1300Sbill 	}
98*1300Sbill }
99*1300Sbill 
100*1300Sbill blklen(av)
101*1300Sbill 	register char **av;
102*1300Sbill {
103*1300Sbill 	register int i = 0;
104*1300Sbill 
105*1300Sbill 	while (*av++)
106*1300Sbill 		i++;
107*1300Sbill 	return (i);
108*1300Sbill }
109*1300Sbill 
110*1300Sbill char **
111*1300Sbill blkcpy(oav, bv)
112*1300Sbill 	char **oav;
113*1300Sbill 	register char **bv;
114*1300Sbill {
115*1300Sbill 	register char **av = oav;
116*1300Sbill 
117*1300Sbill 	while (*av++ = *bv++)
118*1300Sbill 		continue;
119*1300Sbill 	return (oav);
120*1300Sbill }
121*1300Sbill 
122*1300Sbill char **
123*1300Sbill blkcat(up, vp)
124*1300Sbill 	char **up, **vp;
125*1300Sbill {
126*1300Sbill 
127*1300Sbill 	blkcpy(blkend(up), vp);
128*1300Sbill 	return (up);
129*1300Sbill }
130*1300Sbill 
131*1300Sbill blkfree(av0)
132*1300Sbill 	char **av0;
133*1300Sbill {
134*1300Sbill 	register char **av = av0;
135*1300Sbill 
136*1300Sbill 	while (*av)
137*1300Sbill 		xfree(*av++);
138*1300Sbill 	xfree((char *)av0);
139*1300Sbill }
140*1300Sbill 
141*1300Sbill char **
142*1300Sbill saveblk(v)
143*1300Sbill 	register char **v;
144*1300Sbill {
145*1300Sbill 	register int len = blklen(v) + 1;
146*1300Sbill 	register char **newv = (char **) calloc(len, sizeof (char **));
147*1300Sbill 	char **onewv = newv;
148*1300Sbill 
149*1300Sbill 	while (*v)
150*1300Sbill 		*newv++ = savestr(*v++);
151*1300Sbill 	return (onewv);
152*1300Sbill }
153*1300Sbill 
154*1300Sbill char *
155*1300Sbill strspl(cp, dp)
156*1300Sbill 	register char *cp, *dp;
157*1300Sbill {
158*1300Sbill 	register char *ep = calloc(1, strlen(cp) + strlen(dp) + 1);
159*1300Sbill 
160*1300Sbill 	strcpy(ep, cp);
161*1300Sbill 	strcat(ep, dp);
162*1300Sbill 	return (ep);
163*1300Sbill }
164*1300Sbill 
165*1300Sbill char **
166*1300Sbill blkspl(up, vp)
167*1300Sbill 	register char **up, **vp;
168*1300Sbill {
169*1300Sbill 	register char **wp = (char **) calloc(blklen(up) + blklen(vp) + 1, sizeof (char **));
170*1300Sbill 
171*1300Sbill 	blkcpy(wp, up);
172*1300Sbill 	return (blkcat(wp, vp));
173*1300Sbill }
174*1300Sbill 
175*1300Sbill lastchr(cp)
176*1300Sbill 	register char *cp;
177*1300Sbill {
178*1300Sbill 
179*1300Sbill 	if (!*cp)
180*1300Sbill 		return (0);
181*1300Sbill 	while (cp[1])
182*1300Sbill 		cp++;
183*1300Sbill 	return (*cp);
184*1300Sbill }
185*1300Sbill 
186*1300Sbill /*
187*1300Sbill  * This routine is called after an error to close up
188*1300Sbill  * any units which may have been left open accidentally.
189*1300Sbill  */
190*1300Sbill closem()
191*1300Sbill {
192*1300Sbill 	register int f;
193*1300Sbill 
194*1300Sbill 	for (f = 0; f < NOFILE; f++)
195*1300Sbill 		if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
196*1300Sbill 		    f != FSHTTY)
197*1300Sbill 			close(f);
198*1300Sbill }
199*1300Sbill 
200*1300Sbill /*
201*1300Sbill  * Close files before executing a file.
202*1300Sbill  * We could be MUCH more intelligent, since (on a version 7 system)
203*1300Sbill  * we need only close files here during a source, the other
204*1300Sbill  * shell fd's being in units 16-19 which are closed automatically!
205*1300Sbill  */
206*1300Sbill closech()
207*1300Sbill {
208*1300Sbill 	register int f;
209*1300Sbill 
210*1300Sbill 	if (didcch)
211*1300Sbill 		return;
212*1300Sbill 	didcch = 1;
213*1300Sbill 	SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;
214*1300Sbill 	for (f = 3; f < NOFILE; f++)
215*1300Sbill 		close(f);
216*1300Sbill }
217*1300Sbill 
218*1300Sbill donefds()
219*1300Sbill {
220*1300Sbill 
221*1300Sbill 	close(0), close(1), close(2);
222*1300Sbill 	didfds = 0;
223*1300Sbill }
224*1300Sbill 
225*1300Sbill /*
226*1300Sbill  * Move descriptor i to j.
227*1300Sbill  * If j is -1 then we just want to get i to a safe place,
228*1300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
229*1300Sbill  */
230*1300Sbill dmove(i, j)
231*1300Sbill 	register int i, j;
232*1300Sbill {
233*1300Sbill 
234*1300Sbill 	if (i == j || i < 0)
235*1300Sbill 		return (i);
236*1300Sbill #ifdef V7
237*1300Sbill 	if (j >= 0) {
238*1300Sbill 		dup2(i, j);
239*1300Sbill 		return (j);
240*1300Sbill 	} else
241*1300Sbill #endif
242*1300Sbill 		j = dcopy(i, j);
243*1300Sbill 	if (j != i)
244*1300Sbill 		close(i);
245*1300Sbill 	return (j);
246*1300Sbill }
247*1300Sbill 
248*1300Sbill dcopy(i, j)
249*1300Sbill 	register int i, j;
250*1300Sbill {
251*1300Sbill 
252*1300Sbill 	if (i == j || i < 0 || j < 0 && i > 2)
253*1300Sbill 		return (i);
254*1300Sbill #ifdef V7
255*1300Sbill 	if (j >= 0) {
256*1300Sbill 		dup2(i, j);
257*1300Sbill 		return (j);
258*1300Sbill 	}
259*1300Sbill #endif
260*1300Sbill 	close(j);
261*1300Sbill 	return (renum(i, j));
262*1300Sbill }
263*1300Sbill 
264*1300Sbill renum(i, j)
265*1300Sbill 	register int i, j;
266*1300Sbill {
267*1300Sbill 	register int k = dup(i);
268*1300Sbill 
269*1300Sbill 	if (k < 0)
270*1300Sbill 		return (-1);
271*1300Sbill 	if (j == -1 && k > 2)
272*1300Sbill 		return (k);
273*1300Sbill 	if (k != j) {
274*1300Sbill 		j = renum(k, j);
275*1300Sbill 		close(k);
276*1300Sbill 		return (j);
277*1300Sbill 	}
278*1300Sbill 	return (k);
279*1300Sbill }
280*1300Sbill 
281*1300Sbill copy(to, from, size)
282*1300Sbill 	register char *to, *from;
283*1300Sbill 	register int size;
284*1300Sbill {
285*1300Sbill 
286*1300Sbill 	if (size)
287*1300Sbill 		do
288*1300Sbill 			*to++ = *from++;
289*1300Sbill 		while (--size != 0);
290*1300Sbill }
291*1300Sbill 
292*1300Sbill /*
293*1300Sbill  * Left shift a command argument list, discarding
294*1300Sbill  * the first c arguments.  Used in "shift" commands
295*1300Sbill  * as well as by commands like "repeat".
296*1300Sbill  */
297*1300Sbill lshift(v, c)
298*1300Sbill 	register char **v;
299*1300Sbill 	register int c;
300*1300Sbill {
301*1300Sbill 	register char **u = v;
302*1300Sbill 
303*1300Sbill 	while (*u && --c >= 0)
304*1300Sbill 		xfree(*u++);
305*1300Sbill 	blkcpy(v, u);
306*1300Sbill }
307*1300Sbill 
308*1300Sbill number(cp)
309*1300Sbill 	char *cp;
310*1300Sbill {
311*1300Sbill 
312*1300Sbill 	if (*cp == '-') {
313*1300Sbill 		cp++;
314*1300Sbill 		if (!digit(*cp++))
315*1300Sbill 			return (0);
316*1300Sbill 	}
317*1300Sbill 	while (*cp && digit(*cp))
318*1300Sbill 		cp++;
319*1300Sbill 	return (*cp == 0);
320*1300Sbill }
321*1300Sbill 
322*1300Sbill char **
323*1300Sbill copyblk(v)
324*1300Sbill 	register char **v;
325*1300Sbill {
326*1300Sbill 	register char **nv = (char **) calloc(blklen(v) + 1, sizeof (char **));
327*1300Sbill 
328*1300Sbill 	return (blkcpy(nv, v));
329*1300Sbill }
330*1300Sbill 
331*1300Sbill char *
332*1300Sbill strend(cp)
333*1300Sbill 	register char *cp;
334*1300Sbill {
335*1300Sbill 
336*1300Sbill 	while (*cp)
337*1300Sbill 		cp++;
338*1300Sbill 	return (cp);
339*1300Sbill }
340*1300Sbill 
341*1300Sbill char *
342*1300Sbill strip(cp)
343*1300Sbill 	char *cp;
344*1300Sbill {
345*1300Sbill 	register char *dp = cp;
346*1300Sbill 
347*1300Sbill 	while (*dp++ &= TRIM)
348*1300Sbill 		continue;
349*1300Sbill 	return (cp);
350*1300Sbill }
351*1300Sbill 
352*1300Sbill udvar(name)
353*1300Sbill 	char *name;
354*1300Sbill {
355*1300Sbill 
356*1300Sbill 	setname(name);
357*1300Sbill 	bferr("Undefined variable");
358*1300Sbill }
359*1300Sbill 
360*1300Sbill prefix(sub, str)
361*1300Sbill 	register char *sub, *str;
362*1300Sbill {
363*1300Sbill 
364*1300Sbill 	for (;;) {
365*1300Sbill 		if (*sub == 0)
366*1300Sbill 			return (1);
367*1300Sbill 		if (*str == 0)
368*1300Sbill 			return (0);
369*1300Sbill 		if (*sub++ != *str++)
370*1300Sbill 			return (0);
371*1300Sbill 	}
372*1300Sbill }
373