xref: /csrg-svn/bin/sh/miscbltin.c (revision 66823)
147129Sbostic /*-
260698Sbostic  * Copyright (c) 1991, 1993
360698Sbostic  *	The Regents of the University of California.  All rights reserved.
447129Sbostic  *
547129Sbostic  * This code is derived from software contributed to Berkeley by
647129Sbostic  * Kenneth Almquist.
747129Sbostic  *
847129Sbostic  * %sccs.include.redist.c%
947129Sbostic  */
1047129Sbostic 
1147129Sbostic #ifndef lint
12*66823Sbostic static char sccsid[] = "@(#)miscbltin.c	8.2 (Berkeley) 04/16/94";
1347129Sbostic #endif /* not lint */
1447129Sbostic 
1547129Sbostic /*
1647129Sbostic  * Miscelaneous builtins.
1747129Sbostic  */
1847129Sbostic 
1947129Sbostic #include "shell.h"
2047129Sbostic #include "options.h"
2147129Sbostic #include "var.h"
2247129Sbostic #include "output.h"
2347129Sbostic #include "memalloc.h"
2447129Sbostic #include "error.h"
2547129Sbostic #include "mystring.h"
2647129Sbostic 
2747129Sbostic #undef eflag
2847129Sbostic 
2947129Sbostic extern char **argptr;		/* argument list for builtin command */
3047129Sbostic 
3147129Sbostic 
3247129Sbostic /*
3347129Sbostic  * The read builtin.  The -e option causes backslashes to escape the
3447129Sbostic  * following character.
3547129Sbostic  *
3647129Sbostic  * This uses unbuffered input, which may be avoidable in some cases.
3747129Sbostic  */
3847129Sbostic 
3947129Sbostic readcmd(argc, argv)  char **argv; {
4047129Sbostic 	char **ap;
4147129Sbostic 	int backslash;
4247129Sbostic 	char c;
4347129Sbostic 	int eflag;
4447129Sbostic 	char *prompt;
4547129Sbostic 	char *ifs;
4647129Sbostic 	char *p;
4747129Sbostic 	int startword;
4847129Sbostic 	int status;
4947129Sbostic 	int i;
5047129Sbostic 
5147129Sbostic 	eflag = 0;
5247129Sbostic 	prompt = NULL;
5347297Smarc 	while ((i = nextopt("ep:")) != '\0') {
5447129Sbostic 		if (i == 'p')
5547129Sbostic 			prompt = optarg;
5647129Sbostic 		else
5747129Sbostic 			eflag = 1;
5847129Sbostic 	}
5947129Sbostic 	if (prompt && isatty(0)) {
6047129Sbostic 		out2str(prompt);
6147129Sbostic 		flushall();
6247129Sbostic 	}
63*66823Sbostic 	if (*(ap = argptr) == NULL)
6447129Sbostic 		error("arg count");
6547129Sbostic 	if ((ifs = bltinlookup("IFS", 1)) == NULL)
6647129Sbostic 		ifs = nullstr;
6747129Sbostic 	status = 0;
6847129Sbostic 	startword = 1;
6947129Sbostic 	backslash = 0;
7047129Sbostic 	STARTSTACKSTR(p);
7147129Sbostic 	for (;;) {
7247129Sbostic 		if (read(0, &c, 1) != 1) {
7347129Sbostic 			status = 1;
7447129Sbostic 			break;
7547129Sbostic 		}
7647129Sbostic 		if (c == '\0')
7747129Sbostic 			continue;
7847129Sbostic 		if (backslash) {
7947129Sbostic 			backslash = 0;
8047129Sbostic 			if (c != '\n')
8147129Sbostic 				STPUTC(c, p);
8247129Sbostic 			continue;
8347129Sbostic 		}
8447129Sbostic 		if (eflag && c == '\\') {
8547129Sbostic 			backslash++;
8647129Sbostic 			continue;
8747129Sbostic 		}
8847129Sbostic 		if (c == '\n')
8947129Sbostic 			break;
9047129Sbostic 		if (startword && *ifs == ' ' && strchr(ifs, c)) {
9147129Sbostic 			continue;
9247129Sbostic 		}
9347129Sbostic 		startword = 0;
9447129Sbostic 		if (backslash && c == '\\') {
9547129Sbostic 			if (read(0, &c, 1) != 1) {
9647129Sbostic 				status = 1;
9747129Sbostic 				break;
9847129Sbostic 			}
9947129Sbostic 			STPUTC(c, p);
10047129Sbostic 		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
10147129Sbostic 			STACKSTRNUL(p);
10247129Sbostic 			setvar(*ap, stackblock(), 0);
10347129Sbostic 			ap++;
10447129Sbostic 			startword = 1;
10547129Sbostic 			STARTSTACKSTR(p);
10647129Sbostic 		} else {
10747129Sbostic 			STPUTC(c, p);
10847129Sbostic 		}
10947129Sbostic 	}
11047129Sbostic 	STACKSTRNUL(p);
11147129Sbostic 	setvar(*ap, stackblock(), 0);
11247129Sbostic 	while (*++ap != NULL)
11347129Sbostic 		setvar(*ap, nullstr, 0);
11447129Sbostic 	return status;
11547129Sbostic }
11647129Sbostic 
11747129Sbostic 
11847129Sbostic 
11947129Sbostic umaskcmd(argc, argv)  char **argv; {
12047129Sbostic 	int mask;
12147129Sbostic 	char *p;
12247129Sbostic 	int i;
12347129Sbostic 
12447129Sbostic 	if ((p = argv[1]) == NULL) {
12547129Sbostic 		INTOFF;
12647129Sbostic 		mask = umask(0);
12747129Sbostic 		umask(mask);
12847129Sbostic 		INTON;
12947129Sbostic 		out1fmt("%.4o\n", mask);	/* %#o might be better */
13047129Sbostic 	} else {
13147129Sbostic 		mask = 0;
13247129Sbostic 		do {
13347129Sbostic 			if ((unsigned)(i = *p - '0') >= 8)
13447129Sbostic 				error("Illegal number: %s", argv[1]);
13547129Sbostic 			mask = (mask << 3) + i;
13647129Sbostic 		} while (*++p != '\0');
13747129Sbostic 		umask(mask);
13847129Sbostic 	}
13947129Sbostic 	return 0;
14047129Sbostic }
141