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, but only if we need not
69 * share stdin with our parent. In the latter case we claim
70 * control of the terminal. */
72 #if defined(LISP_FEATURE_HPUX)
74 #elif defined(LISP_FEATURE_DARWIN)
76 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
82 tcsetpgrp(0, getpgrp());
87 sigprocmask(SIG_SETMASK
, &sset
, NULL
);
89 /* If we are supposed to be part of some other pty, go for it. */
91 #if !defined(LISP_FEATURE_HPUX) && !defined(SVR4)
92 fd
= open("/dev/tty", O_RDWR
, 0);
94 ioctl(fd
, TIOCNOTTY
, 0);
98 fd
= open(pty_name
, O_RDWR
, 0);
105 /* Set up stdin, stdout, and stderr */
113 /* Close all other fds. */
115 for (fd
= sysconf(_SC_OPEN_MAX
)-1; fd
>= 3; fd
--)
118 for (fd
= getdtablesize()-1; fd
>= 3; fd
--)
123 /* Exec the program. */
125 execvp(program
, argv
);
127 execv(program
, argv
);
131 #else /* !LISP_FEATURE_WIN32 */
133 # include <windows.h>
134 # include <process.h>
140 #define READ_HANDLE 0
141 #define WRITE_HANDLE 1
143 /* These functions do not attempt to deal with wchar_t variations. */
145 /* Get the value of _environ maintained by MSVCRT */
146 char **msvcrt_environ ( void ) {
150 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
153 const char *const *argv
,
163 int stdout_backup
, stdin_backup
, stderr_backup
, wait_mode
;
167 /* Duplicate and save the original stdin/out/err handles. */
168 stdout_backup
= _dup ( _fileno ( stdout
) );
169 stdin_backup
= _dup ( _fileno ( stdin
) );
170 stderr_backup
= _dup ( _fileno ( stderr
) );
172 /* If we are not using stdin/out/err
173 * then duplicate the new pipes to current stdin/out/err handles.
175 * Default std fds are used if in, out or err parameters
178 hReturn
= (HANDLE
)-1;
179 hProcess
= (HANDLE
)-1;
180 if ( ( out
>= 0 ) && ( out
!= _fileno ( stdout
) ) ) {
181 if ( _dup2 ( out
, _fileno ( stdout
) ) != 0 ) goto error_exit
;
183 if ( ( in
>= 0 ) && ( in
!= _fileno ( stdin
) ) ) {
184 if ( _dup2 ( in
, _fileno ( stdin
) ) != 0 ) goto error_exit_out
;
186 if ( ( err
>= 0 ) && ( err
!= _fileno ( stderr
) ) ) {
187 if ( _dup2 ( err
, _fileno ( stderr
) ) != 0 ) goto error_exit_in
;
190 /* Set the wait mode. */
192 wait_mode
= P_NOWAIT
;
197 /* Spawn process given on the command line*/
199 hProcess
= (HANDLE
) spawnvp ( wait_mode
, program
, argv
);
201 hProcess
= (HANDLE
) spawnv ( wait_mode
, program
, argv
);
203 /* Now that the process is launched, replace the original
204 * in/out/err handles and close the backups. */
206 if ( _dup2 ( stderr_backup
, _fileno ( stderr
) ) != 0 ) goto error_exit
;
208 if ( _dup2 ( stdin_backup
, _fileno ( stdin
) ) != 0 ) goto error_exit
;
210 if ( _dup2 ( stdout_backup
, _fileno ( stdout
) ) != 0 ) goto error_exit
;
215 close ( stdout_backup
);
216 close ( stdin_backup
);
217 close ( stderr_backup
);
224 #endif /* !LISP_FEATURE_WIN32 */