xref: /csrg-svn/bin/csh/misc.c (revision 49992)
147823Sbostic /*-
247823Sbostic  * Copyright (c) 1980, 1991 The Regents of the University of California.
347823Sbostic  * All rights reserved.
447823Sbostic  *
547823Sbostic  * %sccs.include.redist.c%
621938Sdist  */
721938Sdist 
817511Sedward #ifndef lint
9*49992Sbostic static char sccsid[] = "@(#)misc.c	5.7 (Berkeley) 06/04/91";
1047823Sbostic #endif /* not lint */
111300Sbill 
121300Sbill #include "sh.h"
131300Sbill 
14*49992Sbostic static int renum();
151300Sbill 
16*49992Sbostic int
17*49992Sbostic any(s, c)
18*49992Sbostic     register char *s;
19*49992Sbostic     register int c;
2017511Sedward {
21*49992Sbostic     if (!s)
22*49992Sbostic 	return (0);		/* Check for nil pointer */
23*49992Sbostic     while (*s)
24*49992Sbostic 	if (*s++ == c)
25*49992Sbostic 	    return (1);
26*49992Sbostic     return (0);
2717511Sedward }
2817511Sedward 
29*49992Sbostic void
30*49992Sbostic setzero(cp, i)
31*49992Sbostic     char   *cp;
32*49992Sbostic     int     i;
3317511Sedward {
34*49992Sbostic     if (i != 0)
35*49992Sbostic 	do
36*49992Sbostic 	    *cp++ = 0;
37*49992Sbostic 	while (--i);
3817511Sedward }
3917511Sedward 
40*49992Sbostic char   *
41*49992Sbostic strsave(s)
42*49992Sbostic     register char *s;
4317511Sedward {
44*49992Sbostic     char   *n;
45*49992Sbostic     register char *p;
4617511Sedward 
47*49992Sbostic     if (s == 0)
48*49992Sbostic 	s = "";
49*49992Sbostic     for (p = s; *p++;);
50*49992Sbostic     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
51*49992Sbostic     while (*p++ = *s++);
52*49992Sbostic     return (n);
5317511Sedward }
5417511Sedward 
55*49992Sbostic Char  **
56*49992Sbostic blkend(up)
57*49992Sbostic     register Char **up;
581300Sbill {
591300Sbill 
60*49992Sbostic     while (*up)
61*49992Sbostic 	up++;
62*49992Sbostic     return (up);
631300Sbill }
641300Sbill 
6517511Sedward 
66*49992Sbostic void
671300Sbill blkpr(av)
68*49992Sbostic     register Char **av;
691300Sbill {
701300Sbill 
71*49992Sbostic     for (; *av; av++) {
72*49992Sbostic 	xprintf("%s", short2str(*av));
73*49992Sbostic 	if (av[1])
74*49992Sbostic 	    xprintf(" ");
75*49992Sbostic     }
761300Sbill }
771300Sbill 
78*49992Sbostic int
791300Sbill blklen(av)
80*49992Sbostic     register Char **av;
811300Sbill {
82*49992Sbostic     register int i = 0;
831300Sbill 
84*49992Sbostic     while (*av++)
85*49992Sbostic 	i++;
86*49992Sbostic     return (i);
871300Sbill }
881300Sbill 
89*49992Sbostic Char  **
901300Sbill blkcpy(oav, bv)
91*49992Sbostic     Char  **oav;
92*49992Sbostic     register Char **bv;
931300Sbill {
94*49992Sbostic     register Char **av = oav;
951300Sbill 
96*49992Sbostic     while (*av++ = *bv++)
97*49992Sbostic 	continue;
98*49992Sbostic     return (oav);
991300Sbill }
1001300Sbill 
101*49992Sbostic Char  **
1021300Sbill blkcat(up, vp)
103*49992Sbostic     Char  **up, **vp;
1041300Sbill {
1051300Sbill 
106*49992Sbostic     (void) blkcpy(blkend(up), vp);
107*49992Sbostic     return (up);
1081300Sbill }
1091300Sbill 
110*49992Sbostic void
1111300Sbill blkfree(av0)
112*49992Sbostic     Char  **av0;
1131300Sbill {
114*49992Sbostic     register Char **av = av0;
1151300Sbill 
116*49992Sbostic     if (!av0)
117*49992Sbostic 	return;
118*49992Sbostic     for (; *av; av++)
119*49992Sbostic 	xfree((ptr_t) * av);
120*49992Sbostic     xfree((ptr_t) av0);
1211300Sbill }
1221300Sbill 
123*49992Sbostic Char  **
1241300Sbill saveblk(v)
125*49992Sbostic     register Char **v;
1261300Sbill {
127*49992Sbostic     register Char **newv =
128*49992Sbostic     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
129*49992Sbostic     Char  **onewv = newv;
1301300Sbill 
131*49992Sbostic     while (*v)
132*49992Sbostic 	*newv++ = Strsave(*v++);
133*49992Sbostic     return (onewv);
1341300Sbill }
1351300Sbill 
136*49992Sbostic #ifdef notdef
137*49992Sbostic char   *
138*49992Sbostic strstr(s, t)
139*49992Sbostic     register char *s, *t;
140*49992Sbostic {
141*49992Sbostic     do {
142*49992Sbostic 	register char *ss = s;
143*49992Sbostic 	register char *tt = t;
144*49992Sbostic 
145*49992Sbostic 	do
146*49992Sbostic 	    if (*tt == '\0')
147*49992Sbostic 		return (s);
148*49992Sbostic 	while (*ss++ == *tt++);
149*49992Sbostic     } while (*s++ != '\0');
150*49992Sbostic     return ((char *) 0);
151*49992Sbostic }
152*49992Sbostic 
153*49992Sbostic #endif				/* notdef */
154*49992Sbostic 
155*49992Sbostic #ifndef SHORT_STRINGS
156*49992Sbostic char   *
1571300Sbill strspl(cp, dp)
158*49992Sbostic     char   *cp, *dp;
1591300Sbill {
160*49992Sbostic     char   *ep;
161*49992Sbostic     register char *p, *q;
1621300Sbill 
163*49992Sbostic     if (!cp)
164*49992Sbostic 	cp = "";
165*49992Sbostic     if (!dp)
166*49992Sbostic 	dp = "";
167*49992Sbostic     for (p = cp; *p++;);
168*49992Sbostic     for (q = dp; *q++;);
169*49992Sbostic     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
170*49992Sbostic     for (p = ep, q = cp; *p++ = *q++;);
171*49992Sbostic     for (p--, q = dp; *p++ = *q++;);
172*49992Sbostic     return (ep);
1731300Sbill }
1741300Sbill 
175*49992Sbostic #endif
176*49992Sbostic 
177*49992Sbostic Char  **
1781300Sbill blkspl(up, vp)
179*49992Sbostic     register Char **up, **vp;
1801300Sbill {
181*49992Sbostic     register Char **wp =
182*49992Sbostic     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
183*49992Sbostic 		      sizeof(Char **));
1841300Sbill 
185*49992Sbostic     (void) blkcpy(wp, up);
186*49992Sbostic     return (blkcat(wp, vp));
1871300Sbill }
1881300Sbill 
189*49992Sbostic Char
1901300Sbill lastchr(cp)
191*49992Sbostic     register Char *cp;
1921300Sbill {
1931300Sbill 
194*49992Sbostic     if (!cp)
195*49992Sbostic 	return (0);
196*49992Sbostic     if (!*cp)
197*49992Sbostic 	return (0);
198*49992Sbostic     while (cp[1])
199*49992Sbostic 	cp++;
200*49992Sbostic     return (*cp);
2011300Sbill }
2021300Sbill 
2031300Sbill /*
2041300Sbill  * This routine is called after an error to close up
2051300Sbill  * any units which may have been left open accidentally.
2061300Sbill  */
207*49992Sbostic void
2081300Sbill closem()
2091300Sbill {
210*49992Sbostic     register int f;
2111300Sbill 
212*49992Sbostic     for (f = 0; f < NOFILE; f++)
213*49992Sbostic 	if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
214*49992Sbostic 	    f != FSHTTY)
215*49992Sbostic 	    (void) close(f);
2161300Sbill }
2171300Sbill 
218*49992Sbostic void
2191300Sbill donefds()
2201300Sbill {
2211300Sbill 
222*49992Sbostic     (void) close(0);
223*49992Sbostic     (void) close(1);
224*49992Sbostic     (void) close(2);
225*49992Sbostic     didfds = 0;
2261300Sbill }
2271300Sbill 
2281300Sbill /*
2291300Sbill  * Move descriptor i to j.
2301300Sbill  * If j is -1 then we just want to get i to a safe place,
2311300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
2321300Sbill  */
233*49992Sbostic int
2341300Sbill dmove(i, j)
235*49992Sbostic     register int i, j;
2361300Sbill {
2371300Sbill 
238*49992Sbostic     if (i == j || i < 0)
239*49992Sbostic 	return (i);
240*49992Sbostic     if (j >= 0) {
241*49992Sbostic 	(void) dup2(i, j);
2421300Sbill 	return (j);
243*49992Sbostic     }
244*49992Sbostic     j = dcopy(i, j);
245*49992Sbostic     if (j != i)
246*49992Sbostic 	(void) close(i);
247*49992Sbostic     return (j);
2481300Sbill }
2491300Sbill 
250*49992Sbostic int
2511300Sbill dcopy(i, j)
252*49992Sbostic     register int i, j;
2531300Sbill {
2541300Sbill 
255*49992Sbostic     if (i == j || i < 0 || j < 0 && i > 2)
256*49992Sbostic 	return (i);
257*49992Sbostic     if (j >= 0) {
258*49992Sbostic 	(void) dup2(i, j);
259*49992Sbostic 	return (j);
260*49992Sbostic     }
261*49992Sbostic     (void) close(j);
262*49992Sbostic     return (renum(i, j));
2631300Sbill }
2641300Sbill 
265*49992Sbostic static int
2661300Sbill renum(i, j)
267*49992Sbostic     register int i, j;
2681300Sbill {
269*49992Sbostic     register int k = dup(i);
2701300Sbill 
271*49992Sbostic     if (k < 0)
272*49992Sbostic 	return (-1);
273*49992Sbostic     if (j == -1 && k > 2)
2741300Sbill 	return (k);
275*49992Sbostic     if (k != j) {
276*49992Sbostic 	j = renum(k, j);
277*49992Sbostic 	(void) close(k);
278*49992Sbostic 	return (j);
279*49992Sbostic     }
280*49992Sbostic     return (k);
2811300Sbill }
2821300Sbill 
2831300Sbill /*
2841300Sbill  * Left shift a command argument list, discarding
2851300Sbill  * the first c arguments.  Used in "shift" commands
2861300Sbill  * as well as by commands like "repeat".
2871300Sbill  */
288*49992Sbostic void
2891300Sbill lshift(v, c)
290*49992Sbostic     register Char **v;
291*49992Sbostic     register int c;
2921300Sbill {
293*49992Sbostic     register Char **u = v;
2941300Sbill 
295*49992Sbostic     while (*u && --c >= 0)
296*49992Sbostic 	xfree((ptr_t) * u++);
297*49992Sbostic     (void) blkcpy(v, u);
2981300Sbill }
2991300Sbill 
300*49992Sbostic int
3011300Sbill number(cp)
302*49992Sbostic     Char   *cp;
3031300Sbill {
304*49992Sbostic     if (!cp)
305*49992Sbostic 	return(0);
306*49992Sbostic     if (*cp == '-') {
307*49992Sbostic 	cp++;
308*49992Sbostic 	if (!Isdigit(*cp))
309*49992Sbostic 	    return (0);
310*49992Sbostic 	cp++;
311*49992Sbostic     }
312*49992Sbostic     while (*cp && Isdigit(*cp))
313*49992Sbostic 	cp++;
314*49992Sbostic     return (*cp == 0);
3151300Sbill }
3161300Sbill 
317*49992Sbostic Char  **
3181300Sbill copyblk(v)
319*49992Sbostic     register Char **v;
3201300Sbill {
321*49992Sbostic     Char  **nv = (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
3221300Sbill 
323*49992Sbostic     return (blkcpy(nv, v));
3241300Sbill }
3251300Sbill 
326*49992Sbostic #ifndef SHORT_STRINGS
327*49992Sbostic char   *
3281300Sbill strend(cp)
329*49992Sbostic     register char *cp;
3301300Sbill {
331*49992Sbostic     if (!cp)
3321300Sbill 	return (cp);
333*49992Sbostic     while (*cp)
334*49992Sbostic 	cp++;
335*49992Sbostic     return (cp);
3361300Sbill }
3371300Sbill 
338*49992Sbostic #endif				/* SHORT_STRINGS */
339*49992Sbostic 
340*49992Sbostic Char   *
3411300Sbill strip(cp)
342*49992Sbostic     Char   *cp;
3431300Sbill {
344*49992Sbostic     register Char *dp = cp;
3451300Sbill 
346*49992Sbostic     if (!cp)
3471300Sbill 	return (cp);
348*49992Sbostic     while (*dp++ &= TRIM)
349*49992Sbostic 	continue;
350*49992Sbostic     return (cp);
3511300Sbill }
3521300Sbill 
353*49992Sbostic void
3541300Sbill udvar(name)
355*49992Sbostic     Char   *name;
3561300Sbill {
3571300Sbill 
358*49992Sbostic     setname(short2str(name));
359*49992Sbostic     stderror(ERR_NAME | ERR_UNDVAR);
3601300Sbill }
3611300Sbill 
362*49992Sbostic int
3631300Sbill prefix(sub, str)
364*49992Sbostic     register Char *sub, *str;
3651300Sbill {
3661300Sbill 
367*49992Sbostic     for (;;) {
368*49992Sbostic 	if (*sub == 0)
369*49992Sbostic 	    return (1);
370*49992Sbostic 	if (*str == 0)
371*49992Sbostic 	    return (0);
372*49992Sbostic 	if (*sub++ != *str++)
373*49992Sbostic 	    return (0);
374*49992Sbostic     }
3751300Sbill }
376