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>
33 /* borrowed from detachtty's detachtty.c, in turn borrowed from APUE
34 * example code found at
35 * http://www.yendor.com/programming/unix/apue/pty/main.c
41 int set_noecho(int fd)
43 struct termios stermios;
45 if (tcgetattr(fd, &stermios) < 0) return 0;
47 stermios.c_lflag &= ~( ECHO | /* ECHOE | ECHOK | */ ECHONL);
48 stermios.c_oflag |= (ONLCR);
49 stermios.c_iflag &= ~(BRKINT);
50 stermios.c_iflag |= (ICANON|ICRNL);
52 stermios.c_cc[VERASE]=0177;
53 if (tcsetattr(fd, TCSANOW, &stermios) < 0) return 0;
57 int spawn(char *program, char *argv[], char *envp[], char *pty_name,
58 int stdin, int stdout, int stderr)
67 /* Put us in our own process group. */
70 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
78 sigprocmask(SIG_SETMASK, &sset, NULL);
80 /* If we are supposed to be part of some other pty, go for it. */
82 #if !defined(hpux) && !defined(SVR4)
83 fd = open("/dev/tty", O_RDWR, 0);
85 ioctl(fd, TIOCNOTTY, 0);
89 fd = open(pty_name, O_RDWR, 0);
96 /* Set up stdin, stdout, and stderr */
104 /* Close all other fds. */
106 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
109 for (fd = getdtablesize()-1; fd >= 3; fd--)
113 /* Exec the program. */
114 execve(program, argv, envp);
116 /* It didn't work, so try /bin/sh. */
119 execve("/bin/sh", argv-1, envp);
121 /* The exec didn't work, flame out. */
124 #else /* !LISP_FEATURE_WIN32 */
126 # include <windows.h>
127 # include <process.h>
133 #define READ_HANDLE 0
134 #define WRITE_HANDLE 1
136 /* These functions do not attempt to deal with wchar_t variations. */
138 /* Get the value of _environ maintained by MSVCRT */
139 char **msvcrt_environ ( void ) {
143 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
146 const char *const *argv,
153 int fdOut, fdIn, fdErr, fdInPipe[2], fdOutPipe[2], fdErrPipe[2], wait_mode;
156 /* Make pipes to be passed to the spawned process as in/out/err */
157 if ( _pipe ( fdOutPipe, 512, O_TEXT | O_NOINHERIT ) == -1 ) return (HANDLE)-1;
158 if ( _pipe ( fdInPipe, 512, O_TEXT | O_NOINHERIT ) == -1 ) return (HANDLE)-1;
159 if ( _pipe ( fdErrPipe, 512, O_TEXT | O_NOINHERIT ) == -1 ) return (HANDLE)-1;
161 /* Duplicate and save original in/out/err handles */
162 fdOut = _dup ( out );
164 fdErr = _dup ( err );
166 /* Duplicate write end of new pipes to current out/err handles,
168 if ( _dup2 ( fdOutPipe[WRITE_HANDLE], out ) != 0 ) return (HANDLE)-1;
169 if ( _dup2 ( fdInPipe[READ_HANDLE], in ) != 0 ) return (HANDLE)-1;
170 if ( _dup2 ( fdErrPipe[WRITE_HANDLE], err ) != 0 ) return (HANDLE)-1;
173 /* Close the duplicated handles to the new pipes */
174 close ( fdOutPipe[WRITE_HANDLE] );
175 close ( fdInPipe[READ_HANDLE] );
176 close ( fdErrPipe[WRITE_HANDLE] );
178 /* Set the wait mode. */
180 wait_mode = P_NOWAIT;
185 /* Spawn process given on the command line*/
186 hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
188 /* Now that the process is launched, replace the original
189 * in/out/err handles */
190 if ( _dup2 ( fdOut, out ) != 0 ) return (HANDLE)-1;
191 if ( _dup2 ( fdIn, in ) != 0 ) return (HANDLE)-1;
192 if ( _dup2 ( fdErr, err ) != 0 ) return (HANDLE)-1;
194 /* Close duplicates */
203 #endif /* !LISP_FEATURE_WIN32 */