xref: /csrg-svn/bin/csh/misc.c (revision 16678)
1*16678Ssam static	char *sccsid = "@(#)misc.c 4.2 07/06/84";
21300Sbill 
31300Sbill #include "sh.h"
41300Sbill 
51300Sbill /*
61300Sbill  * C Shell
71300Sbill  */
81300Sbill 
91300Sbill letter(c)
101300Sbill 	register char c;
111300Sbill {
121300Sbill 
131300Sbill 	return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_');
141300Sbill }
151300Sbill 
161300Sbill digit(c)
171300Sbill 	register char c;
181300Sbill {
191300Sbill 
201300Sbill 	return (c >= '0' && c <= '9');
211300Sbill }
221300Sbill 
231300Sbill alnum(c)
241300Sbill 	register char c;
251300Sbill {
261300Sbill 	return (letter(c) || digit(c));
271300Sbill }
281300Sbill 
291300Sbill any(c, s)
301300Sbill 	register int c;
311300Sbill 	register char *s;
321300Sbill {
331300Sbill 
341300Sbill 	while (*s)
351300Sbill 		if (*s++ == c)
361300Sbill 			return(1);
371300Sbill 	return(0);
381300Sbill }
391300Sbill 
401300Sbill char *
411300Sbill calloc(i, j)
421300Sbill 	register unsigned i;
431300Sbill 	unsigned j;
441300Sbill {
451300Sbill 	register char *cp, *dp;
461300Sbill #ifdef debug
471300Sbill 	static char *av[2] = {0, 0};
481300Sbill #endif
491300Sbill 
501300Sbill 	i *= j;
511300Sbill 	cp = (char *) malloc(i);
521300Sbill 	if (cp == 0) {
531300Sbill 		child++;
541300Sbill #ifndef debug
551300Sbill 		error("Out of memory");
561300Sbill #else
571300Sbill 		showall(av);
581300Sbill 		printf("i=%d, j=%d: ", i/j, j);
591300Sbill 		printf("Out of memory\n");
601300Sbill 		chdir("/usr/bill/cshcore");
611300Sbill 		abort();
621300Sbill #endif
631300Sbill 	}
641300Sbill 	dp = cp;
651300Sbill 	if (i != 0)
661300Sbill 		do
671300Sbill 			*dp++ = 0;
681300Sbill 		while (--i);
691300Sbill 	return (cp);
701300Sbill }
711300Sbill 
721300Sbill cfree(p)
731300Sbill 	char *p;
741300Sbill {
751300Sbill 
761300Sbill 	free(p);
771300Sbill }
781300Sbill 
791300Sbill char **
801300Sbill blkend(up)
811300Sbill 	register char **up;
821300Sbill {
831300Sbill 
841300Sbill 	while (*up)
851300Sbill 		up++;
861300Sbill 	return (up);
871300Sbill }
881300Sbill 
891300Sbill blkpr(av)
901300Sbill 	register char **av;
911300Sbill {
921300Sbill 
931300Sbill 	for (; *av; av++) {
941300Sbill 		printf("%s", *av);
951300Sbill 		if (av[1])
961300Sbill 			printf(" ");
971300Sbill 	}
981300Sbill }
991300Sbill 
1001300Sbill blklen(av)
1011300Sbill 	register char **av;
1021300Sbill {
1031300Sbill 	register int i = 0;
1041300Sbill 
1051300Sbill 	while (*av++)
1061300Sbill 		i++;
1071300Sbill 	return (i);
1081300Sbill }
1091300Sbill 
1101300Sbill char **
1111300Sbill blkcpy(oav, bv)
1121300Sbill 	char **oav;
1131300Sbill 	register char **bv;
1141300Sbill {
1151300Sbill 	register char **av = oav;
1161300Sbill 
1171300Sbill 	while (*av++ = *bv++)
1181300Sbill 		continue;
1191300Sbill 	return (oav);
1201300Sbill }
1211300Sbill 
1221300Sbill char **
1231300Sbill blkcat(up, vp)
1241300Sbill 	char **up, **vp;
1251300Sbill {
1261300Sbill 
1271300Sbill 	blkcpy(blkend(up), vp);
1281300Sbill 	return (up);
1291300Sbill }
1301300Sbill 
1311300Sbill blkfree(av0)
1321300Sbill 	char **av0;
1331300Sbill {
1341300Sbill 	register char **av = av0;
1351300Sbill 
1361300Sbill 	while (*av)
1371300Sbill 		xfree(*av++);
1381300Sbill 	xfree((char *)av0);
1391300Sbill }
1401300Sbill 
1411300Sbill char **
1421300Sbill saveblk(v)
1431300Sbill 	register char **v;
1441300Sbill {
1451300Sbill 	register int len = blklen(v) + 1;
1461300Sbill 	register char **newv = (char **) calloc(len, sizeof (char **));
1471300Sbill 	char **onewv = newv;
1481300Sbill 
1491300Sbill 	while (*v)
1501300Sbill 		*newv++ = savestr(*v++);
1511300Sbill 	return (onewv);
1521300Sbill }
1531300Sbill 
1541300Sbill char *
1551300Sbill strspl(cp, dp)
1561300Sbill 	register char *cp, *dp;
1571300Sbill {
1581300Sbill 	register char *ep = calloc(1, strlen(cp) + strlen(dp) + 1);
1591300Sbill 
1601300Sbill 	strcpy(ep, cp);
1611300Sbill 	strcat(ep, dp);
1621300Sbill 	return (ep);
1631300Sbill }
1641300Sbill 
1651300Sbill char **
1661300Sbill blkspl(up, vp)
1671300Sbill 	register char **up, **vp;
1681300Sbill {
1691300Sbill 	register char **wp = (char **) calloc(blklen(up) + blklen(vp) + 1, sizeof (char **));
1701300Sbill 
1711300Sbill 	blkcpy(wp, up);
1721300Sbill 	return (blkcat(wp, vp));
1731300Sbill }
1741300Sbill 
1751300Sbill lastchr(cp)
1761300Sbill 	register char *cp;
1771300Sbill {
1781300Sbill 
1791300Sbill 	if (!*cp)
1801300Sbill 		return (0);
1811300Sbill 	while (cp[1])
1821300Sbill 		cp++;
1831300Sbill 	return (*cp);
1841300Sbill }
1851300Sbill 
1861300Sbill /*
1871300Sbill  * This routine is called after an error to close up
1881300Sbill  * any units which may have been left open accidentally.
1891300Sbill  */
1901300Sbill closem()
1911300Sbill {
1921300Sbill 	register int f;
1931300Sbill 
1941300Sbill 	for (f = 0; f < NOFILE; f++)
1951300Sbill 		if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
1961300Sbill 		    f != FSHTTY)
1971300Sbill 			close(f);
1981300Sbill }
1991300Sbill 
2001300Sbill /*
2011300Sbill  * Close files before executing a file.
2021300Sbill  * We could be MUCH more intelligent, since (on a version 7 system)
2031300Sbill  * we need only close files here during a source, the other
2041300Sbill  * shell fd's being in units 16-19 which are closed automatically!
2051300Sbill  */
2061300Sbill closech()
2071300Sbill {
2081300Sbill 	register int f;
2091300Sbill 
2101300Sbill 	if (didcch)
2111300Sbill 		return;
2121300Sbill 	didcch = 1;
2131300Sbill 	SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;
2141300Sbill 	for (f = 3; f < NOFILE; f++)
2151300Sbill 		close(f);
2161300Sbill }
2171300Sbill 
2181300Sbill donefds()
2191300Sbill {
2201300Sbill 
2211300Sbill 	close(0), close(1), close(2);
2221300Sbill 	didfds = 0;
2231300Sbill }
2241300Sbill 
2251300Sbill /*
2261300Sbill  * Move descriptor i to j.
2271300Sbill  * If j is -1 then we just want to get i to a safe place,
2281300Sbill  * i.e. to a unit > 2.  This also happens in dcopy.
2291300Sbill  */
2301300Sbill dmove(i, j)
2311300Sbill 	register int i, j;
2321300Sbill {
2331300Sbill 
2341300Sbill 	if (i == j || i < 0)
2351300Sbill 		return (i);
2361300Sbill #ifdef V7
2371300Sbill 	if (j >= 0) {
2381300Sbill 		dup2(i, j);
2391300Sbill 		return (j);
2401300Sbill 	} else
2411300Sbill #endif
2421300Sbill 		j = dcopy(i, j);
2431300Sbill 	if (j != i)
2441300Sbill 		close(i);
2451300Sbill 	return (j);
2461300Sbill }
2471300Sbill 
2481300Sbill dcopy(i, j)
2491300Sbill 	register int i, j;
2501300Sbill {
2511300Sbill 
2521300Sbill 	if (i == j || i < 0 || j < 0 && i > 2)
2531300Sbill 		return (i);
2541300Sbill #ifdef V7
2551300Sbill 	if (j >= 0) {
2561300Sbill 		dup2(i, j);
2571300Sbill 		return (j);
2581300Sbill 	}
2591300Sbill #endif
2601300Sbill 	close(j);
2611300Sbill 	return (renum(i, j));
2621300Sbill }
2631300Sbill 
2641300Sbill renum(i, j)
2651300Sbill 	register int i, j;
2661300Sbill {
2671300Sbill 	register int k = dup(i);
2681300Sbill 
2691300Sbill 	if (k < 0)
2701300Sbill 		return (-1);
2711300Sbill 	if (j == -1 && k > 2)
2721300Sbill 		return (k);
2731300Sbill 	if (k != j) {
2741300Sbill 		j = renum(k, j);
2751300Sbill 		close(k);
2761300Sbill 		return (j);
2771300Sbill 	}
2781300Sbill 	return (k);
2791300Sbill }
2801300Sbill 
281*16678Ssam #ifndef copy
2821300Sbill copy(to, from, size)
2831300Sbill 	register char *to, *from;
2841300Sbill 	register int size;
2851300Sbill {
2861300Sbill 
2871300Sbill 	if (size)
2881300Sbill 		do
2891300Sbill 			*to++ = *from++;
2901300Sbill 		while (--size != 0);
2911300Sbill }
292*16678Ssam #endif
2931300Sbill 
2941300Sbill /*
2951300Sbill  * Left shift a command argument list, discarding
2961300Sbill  * the first c arguments.  Used in "shift" commands
2971300Sbill  * as well as by commands like "repeat".
2981300Sbill  */
2991300Sbill lshift(v, c)
3001300Sbill 	register char **v;
3011300Sbill 	register int c;
3021300Sbill {
3031300Sbill 	register char **u = v;
3041300Sbill 
3051300Sbill 	while (*u && --c >= 0)
3061300Sbill 		xfree(*u++);
3071300Sbill 	blkcpy(v, u);
3081300Sbill }
3091300Sbill 
3101300Sbill number(cp)
3111300Sbill 	char *cp;
3121300Sbill {
3131300Sbill 
3141300Sbill 	if (*cp == '-') {
3151300Sbill 		cp++;
3161300Sbill 		if (!digit(*cp++))
3171300Sbill 			return (0);
3181300Sbill 	}
3191300Sbill 	while (*cp && digit(*cp))
3201300Sbill 		cp++;
3211300Sbill 	return (*cp == 0);
3221300Sbill }
3231300Sbill 
3241300Sbill char **
3251300Sbill copyblk(v)
3261300Sbill 	register char **v;
3271300Sbill {
3281300Sbill 	register char **nv = (char **) calloc(blklen(v) + 1, sizeof (char **));
3291300Sbill 
3301300Sbill 	return (blkcpy(nv, v));
3311300Sbill }
3321300Sbill 
3331300Sbill char *
3341300Sbill strend(cp)
3351300Sbill 	register char *cp;
3361300Sbill {
3371300Sbill 
3381300Sbill 	while (*cp)
3391300Sbill 		cp++;
3401300Sbill 	return (cp);
3411300Sbill }
3421300Sbill 
3431300Sbill char *
3441300Sbill strip(cp)
3451300Sbill 	char *cp;
3461300Sbill {
3471300Sbill 	register char *dp = cp;
3481300Sbill 
3491300Sbill 	while (*dp++ &= TRIM)
3501300Sbill 		continue;
3511300Sbill 	return (cp);
3521300Sbill }
3531300Sbill 
3541300Sbill udvar(name)
3551300Sbill 	char *name;
3561300Sbill {
3571300Sbill 
3581300Sbill 	setname(name);
3591300Sbill 	bferr("Undefined variable");
3601300Sbill }
3611300Sbill 
3621300Sbill prefix(sub, str)
3631300Sbill 	register char *sub, *str;
3641300Sbill {
3651300Sbill 
3661300Sbill 	for (;;) {
3671300Sbill 		if (*sub == 0)
3681300Sbill 			return (1);
3691300Sbill 		if (*str == 0)
3701300Sbill 			return (0);
3711300Sbill 		if (*sub++ != *str++)
3721300Sbill 			return (0);
3731300Sbill 	}
3741300Sbill }
375