xref: /freebsd/bin/sh/redir.c (revision aa9caaf65748ace0f3cd08c2deaf3ef3048d6e4d)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
3589730b29SDavid Greenman  *
36aa9caaf6SPeter Wemm  *	$Id: redir.c,v 1.4 1995/10/21 00:47:31 joerg Exp $
374b88c807SRodney W. Grimes  */
384b88c807SRodney W. Grimes 
394b88c807SRodney W. Grimes #ifndef lint
40aa9caaf6SPeter Wemm static char sccsid[] = "@(#)redir.c	8.2 (Berkeley) 5/4/95";
414b88c807SRodney W. Grimes #endif /* not lint */
424b88c807SRodney W. Grimes 
43aa9caaf6SPeter Wemm #include <sys/types.h>
44aa9caaf6SPeter Wemm #include <signal.h>
45aa9caaf6SPeter Wemm #include <string.h>
46aa9caaf6SPeter Wemm #include <fcntl.h>
47aa9caaf6SPeter Wemm #include <errno.h>
48aa9caaf6SPeter Wemm #include <unistd.h>
49aa9caaf6SPeter Wemm #include <stdlib.h>
50aa9caaf6SPeter Wemm 
514b88c807SRodney W. Grimes /*
524b88c807SRodney W. Grimes  * Code for dealing with input/output redirection.
534b88c807SRodney W. Grimes  */
544b88c807SRodney W. Grimes 
554b88c807SRodney W. Grimes #include "shell.h"
564b88c807SRodney W. Grimes #include "nodes.h"
574b88c807SRodney W. Grimes #include "jobs.h"
584b88c807SRodney W. Grimes #include "expand.h"
594b88c807SRodney W. Grimes #include "redir.h"
604b88c807SRodney W. Grimes #include "output.h"
614b88c807SRodney W. Grimes #include "memalloc.h"
624b88c807SRodney W. Grimes #include "error.h"
634b88c807SRodney W. Grimes 
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes #define EMPTY -2		/* marks an unused slot in redirtab */
664b88c807SRodney W. Grimes #define PIPESIZE 4096		/* amount of buffering in a pipe */
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes MKINIT
704b88c807SRodney W. Grimes struct redirtab {
714b88c807SRodney W. Grimes 	struct redirtab *next;
724b88c807SRodney W. Grimes 	short renamed[10];
734b88c807SRodney W. Grimes };
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes MKINIT struct redirtab *redirlist;
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes /*
794b88c807SRodney W. Grimes  * We keep track of whether or not fd0 has been redirected.  This is for
804b88c807SRodney W. Grimes  * background commands, where we want to redirect fd0 to /dev/null only
814b88c807SRodney W. Grimes  * if it hasn't already been redirected.
824b88c807SRodney W. Grimes */
834b88c807SRodney W. Grimes int fd0_redirected = 0;
844b88c807SRodney W. Grimes 
85aa9caaf6SPeter Wemm STATIC void openredirect __P((union node *, char[10 ]));
86aa9caaf6SPeter Wemm STATIC int openhere __P((union node *));
874b88c807SRodney W. Grimes 
884b88c807SRodney W. Grimes 
894b88c807SRodney W. Grimes /*
904b88c807SRodney W. Grimes  * Process a list of redirection commands.  If the REDIR_PUSH flag is set,
914b88c807SRodney W. Grimes  * old file descriptors are stashed away so that the redirection can be
924b88c807SRodney W. Grimes  * undone by calling popredir.  If the REDIR_BACKQ flag is set, then the
934b88c807SRodney W. Grimes  * standard output, and the standard error if it becomes a duplicate of
944b88c807SRodney W. Grimes  * stdout, is saved in memory.
954b88c807SRodney W. Grimes  */
964b88c807SRodney W. Grimes 
974b88c807SRodney W. Grimes void
984b88c807SRodney W. Grimes redirect(redir, flags)
994b88c807SRodney W. Grimes 	union node *redir;
1004b88c807SRodney W. Grimes 	int flags;
1014b88c807SRodney W. Grimes 	{
1024b88c807SRodney W. Grimes 	union node *n;
103aa9caaf6SPeter Wemm 	struct redirtab *sv = NULL;
1044b88c807SRodney W. Grimes 	int i;
1054b88c807SRodney W. Grimes 	int fd;
1064b88c807SRodney W. Grimes 	char memory[10];		/* file descriptors to write to memory */
1074b88c807SRodney W. Grimes 
1084b88c807SRodney W. Grimes 	for (i = 10 ; --i >= 0 ; )
1094b88c807SRodney W. Grimes 		memory[i] = 0;
1104b88c807SRodney W. Grimes 	memory[1] = flags & REDIR_BACKQ;
1114b88c807SRodney W. Grimes 	if (flags & REDIR_PUSH) {
1124b88c807SRodney W. Grimes 		sv = ckmalloc(sizeof (struct redirtab));
1134b88c807SRodney W. Grimes 		for (i = 0 ; i < 10 ; i++)
1144b88c807SRodney W. Grimes 			sv->renamed[i] = EMPTY;
1154b88c807SRodney W. Grimes 		sv->next = redirlist;
1164b88c807SRodney W. Grimes 		redirlist = sv;
1174b88c807SRodney W. Grimes 	}
1184b88c807SRodney W. Grimes 	for (n = redir ; n ; n = n->nfile.next) {
1194b88c807SRodney W. Grimes 		fd = n->nfile.fd;
120769bbc65SJoerg Wunsch 		if ((n->nfile.type == NTOFD || n->nfile.type == NFROMFD) &&
121769bbc65SJoerg Wunsch 		    n->ndup.dupfd == fd)
122769bbc65SJoerg Wunsch 			continue; /* redirect from/to myself */
1234b88c807SRodney W. Grimes 		if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
1244b88c807SRodney W. Grimes 			INTOFF;
1254b88c807SRodney W. Grimes 			if ((i = copyfd(fd, 10)) != EMPTY) {
1264b88c807SRodney W. Grimes 				sv->renamed[fd] = i;
1274b88c807SRodney W. Grimes 				close(fd);
1284b88c807SRodney W. Grimes 			}
1294b88c807SRodney W. Grimes 			INTON;
1304b88c807SRodney W. Grimes 			if (i == EMPTY)
1314b88c807SRodney W. Grimes 				error("Out of file descriptors");
1324b88c807SRodney W. Grimes 		} else {
1334b88c807SRodney W. Grimes 			close(fd);
1344b88c807SRodney W. Grimes 		}
1354b88c807SRodney W. Grimes                 if (fd == 0)
1364b88c807SRodney W. Grimes                         fd0_redirected++;
1374b88c807SRodney W. Grimes 		openredirect(n, memory);
1384b88c807SRodney W. Grimes 	}
1394b88c807SRodney W. Grimes 	if (memory[1])
1404b88c807SRodney W. Grimes 		out1 = &memout;
1414b88c807SRodney W. Grimes 	if (memory[2])
1424b88c807SRodney W. Grimes 		out2 = &memout;
1434b88c807SRodney W. Grimes }
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 
1464b88c807SRodney W. Grimes STATIC void
1474b88c807SRodney W. Grimes openredirect(redir, memory)
1484b88c807SRodney W. Grimes 	union node *redir;
1494b88c807SRodney W. Grimes 	char memory[10];
1504b88c807SRodney W. Grimes 	{
1514b88c807SRodney W. Grimes 	int fd = redir->nfile.fd;
1524b88c807SRodney W. Grimes 	char *fname;
1534b88c807SRodney W. Grimes 	int f;
1544b88c807SRodney W. Grimes 
1554b88c807SRodney W. Grimes 	/*
1564b88c807SRodney W. Grimes 	 * We suppress interrupts so that we won't leave open file
1574b88c807SRodney W. Grimes 	 * descriptors around.  This may not be such a good idea because
1584b88c807SRodney W. Grimes 	 * an open of a device or a fifo can block indefinitely.
1594b88c807SRodney W. Grimes 	 */
1604b88c807SRodney W. Grimes 	INTOFF;
1614b88c807SRodney W. Grimes 	memory[fd] = 0;
1624b88c807SRodney W. Grimes 	switch (redir->nfile.type) {
1634b88c807SRodney W. Grimes 	case NFROM:
1644b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
1654b88c807SRodney W. Grimes 		if ((f = open(fname, O_RDONLY)) < 0)
1664b88c807SRodney W. Grimes 			error("cannot open %s: %s", fname, errmsg(errno, E_OPEN));
1674b88c807SRodney W. Grimes movefd:
1684b88c807SRodney W. Grimes 		if (f != fd) {
1694b88c807SRodney W. Grimes 			copyfd(f, fd);
1704b88c807SRodney W. Grimes 			close(f);
1714b88c807SRodney W. Grimes 		}
1724b88c807SRodney W. Grimes 		break;
1734b88c807SRodney W. Grimes 	case NTO:
1744b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
1754b88c807SRodney W. Grimes #ifdef O_CREAT
1764b88c807SRodney W. Grimes 		if ((f = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
1774b88c807SRodney W. Grimes 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
1784b88c807SRodney W. Grimes #else
1794b88c807SRodney W. Grimes 		if ((f = creat(fname, 0666)) < 0)
1804b88c807SRodney W. Grimes 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
1814b88c807SRodney W. Grimes #endif
1824b88c807SRodney W. Grimes 		goto movefd;
1834b88c807SRodney W. Grimes 	case NAPPEND:
1844b88c807SRodney W. Grimes 		fname = redir->nfile.expfname;
1854b88c807SRodney W. Grimes #ifdef O_APPEND
1864b88c807SRodney W. Grimes 		if ((f = open(fname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
1874b88c807SRodney W. Grimes 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
1884b88c807SRodney W. Grimes #else
1894b88c807SRodney W. Grimes 		if ((f = open(fname, O_WRONLY)) < 0
1904b88c807SRodney W. Grimes 		 && (f = creat(fname, 0666)) < 0)
1914b88c807SRodney W. Grimes 			error("cannot create %s: %s", fname, errmsg(errno, E_CREAT));
1924b88c807SRodney W. Grimes 		lseek(f, (off_t)0, 2);
1934b88c807SRodney W. Grimes #endif
1944b88c807SRodney W. Grimes 		goto movefd;
1954b88c807SRodney W. Grimes 	case NTOFD:
1964b88c807SRodney W. Grimes 	case NFROMFD:
1974b88c807SRodney W. Grimes 		if (redir->ndup.dupfd >= 0) {	/* if not ">&-" */
1984b88c807SRodney W. Grimes 			if (memory[redir->ndup.dupfd])
1994b88c807SRodney W. Grimes 				memory[fd] = 1;
2004b88c807SRodney W. Grimes 			else
2014b88c807SRodney W. Grimes 				copyfd(redir->ndup.dupfd, fd);
2024b88c807SRodney W. Grimes 		}
2034b88c807SRodney W. Grimes 		break;
2044b88c807SRodney W. Grimes 	case NHERE:
2054b88c807SRodney W. Grimes 	case NXHERE:
2064b88c807SRodney W. Grimes 		f = openhere(redir);
2074b88c807SRodney W. Grimes 		goto movefd;
2084b88c807SRodney W. Grimes 	default:
2094b88c807SRodney W. Grimes 		abort();
2104b88c807SRodney W. Grimes 	}
2114b88c807SRodney W. Grimes 	INTON;
2124b88c807SRodney W. Grimes }
2134b88c807SRodney W. Grimes 
2144b88c807SRodney W. Grimes 
2154b88c807SRodney W. Grimes /*
2164b88c807SRodney W. Grimes  * Handle here documents.  Normally we fork off a process to write the
2174b88c807SRodney W. Grimes  * data to a pipe.  If the document is short, we can stuff the data in
2184b88c807SRodney W. Grimes  * the pipe without forking.
2194b88c807SRodney W. Grimes  */
2204b88c807SRodney W. Grimes 
2214b88c807SRodney W. Grimes STATIC int
2224b88c807SRodney W. Grimes openhere(redir)
2234b88c807SRodney W. Grimes 	union node *redir;
2244b88c807SRodney W. Grimes 	{
2254b88c807SRodney W. Grimes 	int pip[2];
226aa9caaf6SPeter Wemm 	int len = 0;
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes 	if (pipe(pip) < 0)
2294b88c807SRodney W. Grimes 		error("Pipe call failed");
2304b88c807SRodney W. Grimes 	if (redir->type == NHERE) {
2314b88c807SRodney W. Grimes 		len = strlen(redir->nhere.doc->narg.text);
2324b88c807SRodney W. Grimes 		if (len <= PIPESIZE) {
2334b88c807SRodney W. Grimes 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
2344b88c807SRodney W. Grimes 			goto out;
2354b88c807SRodney W. Grimes 		}
2364b88c807SRodney W. Grimes 	}
2374b88c807SRodney W. Grimes 	if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
2384b88c807SRodney W. Grimes 		close(pip[0]);
2394b88c807SRodney W. Grimes 		signal(SIGINT, SIG_IGN);
2404b88c807SRodney W. Grimes 		signal(SIGQUIT, SIG_IGN);
2414b88c807SRodney W. Grimes 		signal(SIGHUP, SIG_IGN);
2424b88c807SRodney W. Grimes #ifdef SIGTSTP
2434b88c807SRodney W. Grimes 		signal(SIGTSTP, SIG_IGN);
2444b88c807SRodney W. Grimes #endif
2454b88c807SRodney W. Grimes 		signal(SIGPIPE, SIG_DFL);
2464b88c807SRodney W. Grimes 		if (redir->type == NHERE)
2474b88c807SRodney W. Grimes 			xwrite(pip[1], redir->nhere.doc->narg.text, len);
2484b88c807SRodney W. Grimes 		else
2494b88c807SRodney W. Grimes 			expandhere(redir->nhere.doc, pip[1]);
2504b88c807SRodney W. Grimes 		_exit(0);
2514b88c807SRodney W. Grimes 	}
2524b88c807SRodney W. Grimes out:
2534b88c807SRodney W. Grimes 	close(pip[1]);
2544b88c807SRodney W. Grimes 	return pip[0];
2554b88c807SRodney W. Grimes }
2564b88c807SRodney W. Grimes 
2574b88c807SRodney W. Grimes 
2584b88c807SRodney W. Grimes 
2594b88c807SRodney W. Grimes /*
2604b88c807SRodney W. Grimes  * Undo the effects of the last redirection.
2614b88c807SRodney W. Grimes  */
2624b88c807SRodney W. Grimes 
2634b88c807SRodney W. Grimes void
2644b88c807SRodney W. Grimes popredir() {
2654b88c807SRodney W. Grimes 	register struct redirtab *rp = redirlist;
2664b88c807SRodney W. Grimes 	int i;
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 	for (i = 0 ; i < 10 ; i++) {
2694b88c807SRodney W. Grimes 		if (rp->renamed[i] != EMPTY) {
2704b88c807SRodney W. Grimes                         if (i == 0)
2714b88c807SRodney W. Grimes                                 fd0_redirected--;
2724b88c807SRodney W. Grimes 			close(i);
2734b88c807SRodney W. Grimes 			if (rp->renamed[i] >= 0) {
2744b88c807SRodney W. Grimes 				copyfd(rp->renamed[i], i);
2754b88c807SRodney W. Grimes 				close(rp->renamed[i]);
2764b88c807SRodney W. Grimes 			}
2774b88c807SRodney W. Grimes 		}
2784b88c807SRodney W. Grimes 	}
2794b88c807SRodney W. Grimes 	INTOFF;
2804b88c807SRodney W. Grimes 	redirlist = rp->next;
2814b88c807SRodney W. Grimes 	ckfree(rp);
2824b88c807SRodney W. Grimes 	INTON;
2834b88c807SRodney W. Grimes }
2844b88c807SRodney W. Grimes 
2854b88c807SRodney W. Grimes /*
2864b88c807SRodney W. Grimes  * Undo all redirections.  Called on error or interrupt.
2874b88c807SRodney W. Grimes  */
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes #ifdef mkinit
2904b88c807SRodney W. Grimes 
2914b88c807SRodney W. Grimes INCLUDE "redir.h"
2924b88c807SRodney W. Grimes 
2934b88c807SRodney W. Grimes RESET {
2944b88c807SRodney W. Grimes 	while (redirlist)
2954b88c807SRodney W. Grimes 		popredir();
2964b88c807SRodney W. Grimes }
2974b88c807SRodney W. Grimes 
2984b88c807SRodney W. Grimes SHELLPROC {
2994b88c807SRodney W. Grimes 	clearredir();
3004b88c807SRodney W. Grimes }
3014b88c807SRodney W. Grimes 
3024b88c807SRodney W. Grimes #endif
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes /* Return true if fd 0 has already been redirected at least once.  */
3054b88c807SRodney W. Grimes int
3064b88c807SRodney W. Grimes fd0_redirected_p () {
3074b88c807SRodney W. Grimes         return fd0_redirected != 0;
3084b88c807SRodney W. Grimes }
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes /*
3114b88c807SRodney W. Grimes  * Discard all saved file descriptors.
3124b88c807SRodney W. Grimes  */
3134b88c807SRodney W. Grimes 
3144b88c807SRodney W. Grimes void
3154b88c807SRodney W. Grimes clearredir() {
3164b88c807SRodney W. Grimes 	register struct redirtab *rp;
3174b88c807SRodney W. Grimes 	int i;
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes 	for (rp = redirlist ; rp ; rp = rp->next) {
3204b88c807SRodney W. Grimes 		for (i = 0 ; i < 10 ; i++) {
3214b88c807SRodney W. Grimes 			if (rp->renamed[i] >= 0) {
3224b88c807SRodney W. Grimes 				close(rp->renamed[i]);
3234b88c807SRodney W. Grimes 			}
3244b88c807SRodney W. Grimes 			rp->renamed[i] = EMPTY;
3254b88c807SRodney W. Grimes 		}
3264b88c807SRodney W. Grimes 	}
3274b88c807SRodney W. Grimes }
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes 
3304b88c807SRodney W. Grimes 
3314b88c807SRodney W. Grimes /*
3324b88c807SRodney W. Grimes  * Copy a file descriptor to be >= to.  Returns -1
3334b88c807SRodney W. Grimes  * if the source file descriptor is closed, EMPTY if there are no unused
3344b88c807SRodney W. Grimes  * file descriptors left.
3354b88c807SRodney W. Grimes  */
3364b88c807SRodney W. Grimes 
3374b88c807SRodney W. Grimes int
338aa9caaf6SPeter Wemm copyfd(from, to)
339aa9caaf6SPeter Wemm 	int from;
340aa9caaf6SPeter Wemm 	int to;
341aa9caaf6SPeter Wemm {
3424b88c807SRodney W. Grimes 	int newfd;
3434b88c807SRodney W. Grimes 
3444b88c807SRodney W. Grimes 	newfd = fcntl(from, F_DUPFD, to);
3454b88c807SRodney W. Grimes 	if (newfd < 0 && errno == EMFILE)
3464b88c807SRodney W. Grimes 		return EMPTY;
347769bbc65SJoerg Wunsch 	if (newfd < 0)
348769bbc65SJoerg Wunsch 		error("%d: %s", from, strerror(errno));
3494b88c807SRodney W. Grimes 	return newfd;
3504b88c807SRodney W. Grimes }
351