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 extern char **environ;
58 int spawn(char *program, char *argv[], int sin, int sout, int serr,
59 int search, char *envp[], char *pty_name, int wait)
68 /* Put us in our own process group. */
69 #if defined(LISP_FEATURE_HPUX)
71 #elif defined(LISP_FEATURE_DARWIN)
73 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
81 sigprocmask(SIG_SETMASK, &sset, NULL);
83 /* If we are supposed to be part of some other pty, go for it. */
85 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
86 fd = open("/dev/tty", O_RDWR, 0);
88 ioctl(fd, TIOCNOTTY, 0);
92 fd = open(pty_name, O_RDWR, 0);
99 /* Set up stdin, stdout, and stderr */
107 /* Close all other fds. */
109 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
112 for (fd = getdtablesize()-1; fd >= 3; fd--)
117 /* Exec the program. */
119 execvp(program, argv);
121 execv(program, argv);
125 #else /* !LISP_FEATURE_WIN32 */
127 # include <windows.h>
128 # include <process.h>
134 #define READ_HANDLE 0
135 #define WRITE_HANDLE 1
137 /* These functions do not attempt to deal with wchar_t variations. */
139 /* Get the value of _environ maintained by MSVCRT */
140 char **msvcrt_environ ( void ) {
144 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
147 const char *const *argv,
157 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
161 /* Duplicate and save the original stdin/out/err handles. */
162 stdout_backup = _dup ( _fileno ( stdout ) );
163 stdin_backup = _dup ( _fileno ( stdin ) );
164 stderr_backup = _dup ( _fileno ( stderr ) );
166 /* If we are not using stdin/out/err
167 * then duplicate the new pipes to current stdin/out/err handles.
169 * Default std fds are used if in, out or err parameters
172 hReturn = (HANDLE)-1;
173 hProcess = (HANDLE)-1;
174 if ( ( out >= 0 ) && ( out != _fileno ( stdout ) ) ) {
175 if ( _dup2 ( out, _fileno ( stdout ) ) != 0 ) goto error_exit;
177 if ( ( in >= 0 ) && ( in != _fileno ( stdin ) ) ) {
178 if ( _dup2 ( in, _fileno ( stdin ) ) != 0 ) goto error_exit_out;
180 if ( ( err >= 0 ) && ( err != _fileno ( stderr ) ) ) {
181 if ( _dup2 ( err, _fileno ( stderr ) ) != 0 ) goto error_exit_in;
184 /* Set the wait mode. */
186 wait_mode = P_NOWAIT;
191 /* Spawn process given on the command line*/
193 hProcess = (HANDLE) spawnvp ( wait_mode, program, argv );
195 hProcess = (HANDLE) spawnv ( wait_mode, program, argv );
197 /* Now that the process is launched, replace the original
198 * in/out/err handles and close the backups. */
200 if ( _dup2 ( stderr_backup, _fileno ( stderr ) ) != 0 ) goto error_exit;
202 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
204 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
209 close ( stdout_backup );
210 close ( stdin_backup );
211 close ( stderr_backup );
218 #endif /* !LISP_FEATURE_WIN32 */