miscbltin.c revision 1556
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
351556Srgrimes */
361556Srgrimes
371556Srgrimes#ifndef lint
381556Srgrimesstatic char sccsid[] = "@(#)miscbltin.c	8.2 (Berkeley) 4/16/94";
391556Srgrimes#endif /* not lint */
401556Srgrimes
411556Srgrimes/*
421556Srgrimes * Miscelaneous builtins.
431556Srgrimes */
441556Srgrimes
451556Srgrimes#include "shell.h"
461556Srgrimes#include "options.h"
471556Srgrimes#include "var.h"
481556Srgrimes#include "output.h"
491556Srgrimes#include "memalloc.h"
501556Srgrimes#include "error.h"
511556Srgrimes#include "mystring.h"
521556Srgrimes
531556Srgrimes#undef eflag
541556Srgrimes
551556Srgrimesextern char **argptr;		/* argument list for builtin command */
561556Srgrimes
571556Srgrimes
581556Srgrimes/*
591556Srgrimes * The read builtin.  The -e option causes backslashes to escape the
601556Srgrimes * following character.
611556Srgrimes *
621556Srgrimes * This uses unbuffered input, which may be avoidable in some cases.
631556Srgrimes */
641556Srgrimes
651556Srgrimesreadcmd(argc, argv)  char **argv; {
661556Srgrimes	char **ap;
671556Srgrimes	int backslash;
681556Srgrimes	char c;
691556Srgrimes	int eflag;
701556Srgrimes	char *prompt;
711556Srgrimes	char *ifs;
721556Srgrimes	char *p;
731556Srgrimes	int startword;
741556Srgrimes	int status;
751556Srgrimes	int i;
761556Srgrimes
771556Srgrimes	eflag = 0;
781556Srgrimes	prompt = NULL;
791556Srgrimes	while ((i = nextopt("ep:")) != '\0') {
801556Srgrimes		if (i == 'p')
811556Srgrimes			prompt = optarg;
821556Srgrimes		else
831556Srgrimes			eflag = 1;
841556Srgrimes	}
851556Srgrimes	if (prompt && isatty(0)) {
861556Srgrimes		out2str(prompt);
871556Srgrimes		flushall();
881556Srgrimes	}
891556Srgrimes	if (*(ap = argptr) == NULL)
901556Srgrimes		error("arg count");
911556Srgrimes	if ((ifs = bltinlookup("IFS", 1)) == NULL)
921556Srgrimes		ifs = nullstr;
931556Srgrimes	status = 0;
941556Srgrimes	startword = 1;
951556Srgrimes	backslash = 0;
961556Srgrimes	STARTSTACKSTR(p);
971556Srgrimes	for (;;) {
981556Srgrimes		if (read(0, &c, 1) != 1) {
991556Srgrimes			status = 1;
1001556Srgrimes			break;
1011556Srgrimes		}
1021556Srgrimes		if (c == '\0')
1031556Srgrimes			continue;
1041556Srgrimes		if (backslash) {
1051556Srgrimes			backslash = 0;
1061556Srgrimes			if (c != '\n')
1071556Srgrimes				STPUTC(c, p);
1081556Srgrimes			continue;
1091556Srgrimes		}
1101556Srgrimes		if (eflag && c == '\\') {
1111556Srgrimes			backslash++;
1121556Srgrimes			continue;
1131556Srgrimes		}
1141556Srgrimes		if (c == '\n')
1151556Srgrimes			break;
1161556Srgrimes		if (startword && *ifs == ' ' && strchr(ifs, c)) {
1171556Srgrimes			continue;
1181556Srgrimes		}
1191556Srgrimes		startword = 0;
1201556Srgrimes		if (backslash && c == '\\') {
1211556Srgrimes			if (read(0, &c, 1) != 1) {
1221556Srgrimes				status = 1;
1231556Srgrimes				break;
1241556Srgrimes			}
1251556Srgrimes			STPUTC(c, p);
1261556Srgrimes		} else if (ap[1] != NULL && strchr(ifs, c) != NULL) {
1271556Srgrimes			STACKSTRNUL(p);
1281556Srgrimes			setvar(*ap, stackblock(), 0);
1291556Srgrimes			ap++;
1301556Srgrimes			startword = 1;
1311556Srgrimes			STARTSTACKSTR(p);
1321556Srgrimes		} else {
1331556Srgrimes			STPUTC(c, p);
1341556Srgrimes		}
1351556Srgrimes	}
1361556Srgrimes	STACKSTRNUL(p);
1371556Srgrimes	setvar(*ap, stackblock(), 0);
1381556Srgrimes	while (*++ap != NULL)
1391556Srgrimes		setvar(*ap, nullstr, 0);
1401556Srgrimes	return status;
1411556Srgrimes}
1421556Srgrimes
1431556Srgrimes
1441556Srgrimes
1451556Srgrimesumaskcmd(argc, argv)  char **argv; {
1461556Srgrimes	int mask;
1471556Srgrimes	char *p;
1481556Srgrimes	int i;
1491556Srgrimes
1501556Srgrimes	if ((p = argv[1]) == NULL) {
1511556Srgrimes		INTOFF;
1521556Srgrimes		mask = umask(0);
1531556Srgrimes		umask(mask);
1541556Srgrimes		INTON;
1551556Srgrimes		out1fmt("%.4o\n", mask);	/* %#o might be better */
1561556Srgrimes	} else {
1571556Srgrimes		mask = 0;
1581556Srgrimes		do {
1591556Srgrimes			if ((unsigned)(i = *p - '0') >= 8)
1601556Srgrimes				error("Illegal number: %s", argv[1]);
1611556Srgrimes			mask = (mask << 3) + i;
1621556Srgrimes		} while (*++p != '\0');
1631556Srgrimes		umask(mask);
1641556Srgrimes	}
1651556Srgrimes	return 0;
1661556Srgrimes}
167