2 * support for the Lisp function RUN-PROGRAM and friends
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
18 #ifndef LISP_FEATURE_WIN32
22 #include <sys/types.h>
26 #include <sys/ioctl.h>
29 #include <sys/ioctl.h>
32 #ifdef LISP_FEATURE_OPENBSD
33 /* FIXME: there has to be a better way to avoid ./util.h here */
34 #include </usr/include/util.h>
37 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
38 * example code found at
39 * http://www.yendor.com/programming/unix/apue/pty/main.c
45 int set_noecho(int fd)
47 struct termios stermios;
49 if (tcgetattr(fd, &stermios) < 0) return 0;
51 stermios.c_lflag &= ~( ECHO | /* ECHOE | ECHOK | */ ECHONL);
52 stermios.c_oflag |= (ONLCR);
53 stermios.c_iflag &= ~(BRKINT);
54 stermios.c_iflag |= (ICANON|ICRNL);
56 stermios.c_cc[VERASE]=0177;
57 if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
61 #if defined(LISP_FEATURE_OPENBSD)
64 set_pty(char *pty_name)
68 if ((fd = open(pty_name, O_RDWR, 0)) == -1 ||
71 return (set_noecho(STDIN_FILENO));
74 #else /* !LISP_FEATURE_OPENBSD */
77 set_pty(char *pty_name)
81 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
82 fd = open("/dev/tty", O_RDWR, 0);
84 ioctl(fd, TIOCNOTTY, 0);
88 if ((fd = open(pty_name, O_RDWR, 0)) == -1)
98 #endif /* !LISP_FEATURE_OPENBSD */
100 extern char **environ;
101 int spawn(char *program, char *argv[], int sin, int sout, int serr,
102 int search, char *envp[], char *pty_name, int wait)
111 /* Put us in our own process group, but only if we need not
112 * share stdin with our parent. In the latter case we claim
113 * control of the terminal. */
115 #if defined(LISP_FEATURE_HPUX) || defined(LISP_FEATURE_OPENBSD)
117 #elif defined(LISP_FEATURE_DARWIN)
118 setpgid(0, getpid());
119 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
122 setpgrp(0, getpid());
125 tcsetpgrp(0, getpgrp());
128 /* unblock signals */
130 sigprocmask(SIG_SETMASK, &sset, NULL);
132 /* If we are supposed to be part of some other pty, go for it. */
136 /* Set up stdin, stdout, and stderr */
144 /* Close all other fds. */
146 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
149 for (fd = getdtablesize()-1; fd >= 3; fd--)
154 /* Exec the program. */
156 execvp(program, argv);
158 execv(program, argv);
162 #else /* !LISP_FEATURE_WIN32 */
164 # include <windows.h>
165 # include <process.h>
171 #define READ_HANDLE 0
172 #define WRITE_HANDLE 1
174 /* These functions do not attempt to deal with wchar_t variations. */
176 /* Get the value of _environ maintained by MSVCRT */
177 char **msvcrt_environ ( void ) {
181 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
184 const char *const *argv,
194 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
198 /* Duplicate and save the original stdin/out/err handles. */
199 stdout_backup = _dup ( _fileno ( stdout ) );
200 stdin_backup = _dup ( _fileno ( stdin ) );
201 stderr_backup = _dup ( _fileno ( stderr ) );
203 /* If we are not using stdin/out/err
204 * then duplicate the new pipes to current stdin/out/err handles.
206 * Default std fds are used if in, out or err parameters
209 hReturn = (HANDLE)-1;
210 hProcess = (HANDLE)-1;
211 if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
212 if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
214 if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
215 if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
217 if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
218 if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
221 /* Set the wait mode. */
223 wait_mode = P_NOWAIT;
228 /* Spawn process given on the command line*/
230 hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
232 hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
234 /* Now that the process is launched, replace the original
235 * in/out/err handles and close the backups. */
237 if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
239 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
241 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
246 close ( stdout_backup );
247 close ( stdin_backup );
248 close ( stderr_backup );
255 #endif /* !LISP_FEATURE_WIN32 */