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(LISP_FEATURE_DARWIN)
72 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
80 sigprocmask(SIG_SETMASK, &sset, NULL);
82 /* If we are supposed to be part of some other pty, go for it. */
84 #if !defined(hpux) && !defined(SVR4)
85 fd = open("/dev/tty", O_RDWR, 0);
87 ioctl(fd, TIOCNOTTY, 0);
91 fd = open(pty_name, O_RDWR, 0);
98 /* Set up stdin, stdout, and stderr */
106 /* Close all other fds. */
108 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
111 for (fd = getdtablesize()-1; fd >= 3; fd--)
115 /* Exec the program. */
116 execve(program, argv, envp);
118 /* It didn't work, so try /bin/sh. */
121 execve("/bin/sh", argv-1, envp);
123 /* The exec didn't work, flame out. */
126 #else /* !LISP_FEATURE_WIN32 */
128 # include <windows.h>
129 # include <process.h>
135 #define READ_HANDLE 0
136 #define WRITE_HANDLE 1
138 /* These functions do not attempt to deal with wchar_t variations. */
140 /* Get the value of _environ maintained by MSVCRT */
141 char **msvcrt_environ ( void ) {
145 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
148 const char *const *argv,
155 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
159 /* Duplicate and save the original stdin/out/err handles. */
160 stdout_backup = _dup ( _fileno ( stdout ) );
161 stdin_backup = _dup ( _fileno ( stdin ) );
162 stderr_backup = _dup ( _fileno ( stderr ) );
164 /* If we are not using stdin/out/err
165 * then duplicate the new pipes to current stdin/out/err handles.
167 * Default std fds are used if in, out or err parameters
170 hReturn = (HANDLE)-1;
171 hProcess = (HANDLE)-1;
172 if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
173 if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
175 if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
176 if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
178 if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
179 if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
182 /* Set the wait mode. */
184 wait_mode = P_NOWAIT;
189 /* Spawn process given on the command line*/
190 hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
192 /* Now that the process is launched, replace the original
193 * in/out/err handles and close the backups. */
195 if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
197 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
199 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
204 close ( stdout_backup );
205 close ( stdin_backup );
206 close ( stderr_backup );
213 #endif /* !LISP_FEATURE_WIN32 */