xref: /csrg-svn/bin/sh/miscbltin.c (revision 47129)
1*47129Sbostic /*-
2*47129Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*47129Sbostic  * All rights reserved.
4*47129Sbostic  *
5*47129Sbostic  * This code is derived from software contributed to Berkeley by
6*47129Sbostic  * Kenneth Almquist.
7*47129Sbostic  *
8*47129Sbostic  * %sccs.include.redist.c%
9*47129Sbostic  */
10*47129Sbostic 
11*47129Sbostic #ifndef lint
12*47129Sbostic static char sccsid[] = "@(#)miscbltin.c	5.1 (Berkeley) 03/07/91";
13*47129Sbostic #endif /* not lint */
14*47129Sbostic 
15*47129Sbostic /*
16*47129Sbostic  * Miscelaneous builtins.
17*47129Sbostic  */
18*47129Sbostic 
19*47129Sbostic #include "shell.h"
20*47129Sbostic #include "options.h"
21*47129Sbostic #include "var.h"
22*47129Sbostic #include "output.h"
23*47129Sbostic #include "memalloc.h"
24*47129Sbostic #include "error.h"
25*47129Sbostic #include "mystring.h"
26*47129Sbostic 
27*47129Sbostic #undef eflag
28*47129Sbostic 
29*47129Sbostic extern char **argptr;		/* argument list for builtin command */
30*47129Sbostic 
31*47129Sbostic 
32*47129Sbostic /*
33*47129Sbostic  * The read builtin.  The -e option causes backslashes to escape the
34*47129Sbostic  * following character.
35*47129Sbostic  *
36*47129Sbostic  * This uses unbuffered input, which may be avoidable in some cases.
37*47129Sbostic  */
38*47129Sbostic 
39*47129Sbostic readcmd(argc, argv)  char **argv; {
40*47129Sbostic 	char **ap;
41*47129Sbostic 	int backslash;
42*47129Sbostic 	char c;
43*47129Sbostic 	int eflag;
44*47129Sbostic 	char *prompt;
45*47129Sbostic 	char *ifs;
46*47129Sbostic 	char *p;
47*47129Sbostic 	int startword;
48*47129Sbostic 	int status;
49*47129Sbostic 	int i;
50*47129Sbostic 
51*47129Sbostic 	eflag = 0;
52*47129Sbostic 	prompt = NULL;
53*47129Sbostic 	while ((i = nextopt("ep:")) >= 0) {
54*47129Sbostic 		if (i == 'p')
55*47129Sbostic 			prompt = optarg;
56*47129Sbostic 		else
57*47129Sbostic 			eflag = 1;
58*47129Sbostic 	}
59*47129Sbostic 	if (prompt && isatty(0)) {
60*47129Sbostic 		out2str(prompt);
61*47129Sbostic 		flushall();
62*47129Sbostic 	}
63*47129Sbostic 	if ((ap = argptr) == NULL)
64*47129Sbostic 		error("arg count");
65*47129Sbostic 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
66*47129Sbostic 		ifs = nullstr;
67*47129Sbostic 	status = 0;
68*47129Sbostic 	startword = 1;
69*47129Sbostic 	backslash = 0;
70*47129Sbostic 	STARTSTACKSTR(p);
71*47129Sbostic 	for (;;) {
72*47129Sbostic 		if (read(0, &c, 1) != 1) {
73*47129Sbostic 			status = 1;
74*47129Sbostic 			break;
75*47129Sbostic 		}
76*47129Sbostic 		if (c == '\0')
77*47129Sbostic 			continue;
78*47129Sbostic 		if (backslash) {
79*47129Sbostic 			backslash = 0;
80*47129Sbostic 			if (c != '\n')
81*47129Sbostic 				STPUTC(c, p);
82*47129Sbostic 			continue;
83*47129Sbostic 		}
84*47129Sbostic 		if (eflag && c == '\\') {
85*47129Sbostic 			backslash++;
86*47129Sbostic 			continue;
87*47129Sbostic 		}
88*47129Sbostic 		if (c == '\n')
89*47129Sbostic 			break;
90*47129Sbostic 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
91*47129Sbostic 			continue;
92*47129Sbostic 		}
93*47129Sbostic 		startword = 0;
94*47129Sbostic 		if (backslash && c == '\\') {
95*47129Sbostic 			if (read(0, &c, 1) != 1) {
96*47129Sbostic 				status = 1;
97*47129Sbostic 				break;
98*47129Sbostic 			}
99*47129Sbostic 			STPUTC(c, p);
100*47129Sbostic 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
101*47129Sbostic 			STACKSTRNUL(p);
102*47129Sbostic 			setvar(*ap, stackblock(), 0);
103*47129Sbostic 			ap++;
104*47129Sbostic 			startword = 1;
105*47129Sbostic 			STARTSTACKSTR(p);
106*47129Sbostic 		} else {
107*47129Sbostic 			STPUTC(c, p);
108*47129Sbostic 		}
109*47129Sbostic 	}
110*47129Sbostic 	STACKSTRNUL(p);
111*47129Sbostic 	setvar(*ap, stackblock(), 0);
112*47129Sbostic 	while (*++ap != NULL)
113*47129Sbostic 		setvar(*ap, nullstr, 0);
114*47129Sbostic 	return status;
115*47129Sbostic }
116*47129Sbostic 
117*47129Sbostic 
118*47129Sbostic 
119*47129Sbostic umaskcmd(argc, argv)  char **argv; {
120*47129Sbostic 	int mask;
121*47129Sbostic 	char *p;
122*47129Sbostic 	int i;
123*47129Sbostic 
124*47129Sbostic 	if ((p = argv[1]) == NULL) {
125*47129Sbostic 		INTOFF;
126*47129Sbostic 		mask = umask(0);
127*47129Sbostic 		umask(mask);
128*47129Sbostic 		INTON;
129*47129Sbostic 		out1fmt("%.4o\n", mask);	/* %#o might be better */
130*47129Sbostic 	} else {
131*47129Sbostic 		mask = 0;
132*47129Sbostic 		do {
133*47129Sbostic 			if ((unsigned)(i = *p - '0') >= 8)
134*47129Sbostic 				error("Illegal number: %s", argv[1]);
135*47129Sbostic 			mask = (mask << 3) + i;
136*47129Sbostic 		} while (*++p != '\0');
137*47129Sbostic 		umask(mask);
138*47129Sbostic 	}
139*47129Sbostic 	return 0;
140*47129Sbostic }
141