Merge git://sbcl.boinkor.net/sbcl
[sbcl/lichteblau.git] / src / runtime / run-program.c
blob08feaa19322622b3ef35c9220ab0a70567a5b351
1 /*
2 * support for the Lisp function RUN-PROGRAM and friends
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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.
16 #include "sbcl.h"
18 #ifndef LISP_FEATURE_WIN32
20 #include <stdlib.h>
21 #include <sys/file.h>
22 #include <sys/types.h>
23 #include <signal.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <unistd.h>
29 #include <sys/ioctl.h>
30 #include <termios.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
37 -brkint
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;
54 return 1;
57 int spawn(char *program, char *argv[], char *envp[], char *pty_name,
58 int stdin, int stdout, int stderr)
60 int pid = fork();
61 int fd;
62 sigset_t sset;
64 if (pid != 0)
65 return pid;
67 /* Put us in our own process group. */
68 #if defined(hpux)
69 setsid();
70 #elif defined(LISP_FEATURE_DARWIN)
71 setpgid(0, getpid());
72 #elif defined(SVR4) || defined(__linux__) || defined(__osf__)
73 setpgrp();
74 #else
75 setpgrp(0, getpid());
76 #endif
78 /* unblock signals */
79 sigemptyset(&sset);
80 sigprocmask(SIG_SETMASK, &sset, NULL);
82 /* If we are supposed to be part of some other pty, go for it. */
83 if (pty_name) {
84 #if !defined(hpux) && !defined(SVR4)
85 fd = open("/dev/tty", O_RDWR, 0);
86 if (fd >= 0) {
87 ioctl(fd, TIOCNOTTY, 0);
88 close(fd);
90 #endif
91 fd = open(pty_name, O_RDWR, 0);
92 dup2(fd, 0);
93 set_noecho(0);
94 dup2(fd, 1);
95 dup2(fd, 2);
96 close(fd);
97 } else{
98 /* Set up stdin, stdout, and stderr */
99 if (stdin >= 0)
100 dup2(stdin, 0);
101 if (stdout >= 0)
102 dup2(stdout, 1);
103 if (stderr >= 0)
104 dup2(stderr, 2);
106 /* Close all other fds. */
107 #ifdef SVR4
108 for (fd = sysconf(_SC_OPEN_MAX)-1; fd >= 3; fd--)
109 close(fd);
110 #else
111 for (fd = getdtablesize()-1; fd >= 3; fd--)
112 close(fd);
113 #endif
115 /* Exec the program. */
116 execve(program, argv, envp);
118 /* It didn't work, so try /bin/sh. */
119 argv[0] = program;
120 argv[-1] = "sh";
121 execve("/bin/sh", argv-1, envp);
123 /* The exec didn't work, flame out. */
124 exit(1);
126 #else /* !LISP_FEATURE_WIN32 */
128 # include <windows.h>
129 # include <process.h>
130 # include <stdio.h>
131 # include <stdlib.h>
132 # include <fcntl.h>
133 # include <io.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 ) {
142 return ( _environ );
145 /* Set up in, out, err pipes and spawn a program, waiting or otherwise. */
146 HANDLE spawn (
147 const char *program,
148 const char *const *argv,
149 int in,
150 int out,
151 int err,
152 int wait
155 int stdout_backup, stdin_backup, stderr_backup, wait_mode;
156 HANDLE hProcess;
157 HANDLE hReturn;
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
168 * are -1. */
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. */
183 if ( 0 == wait ) {
184 wait_mode = P_NOWAIT;
185 } else {
186 wait_mode = P_WAIT;
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;
196 error_exit_in:
197 if ( _dup2 ( stdin_backup, _fileno ( stdin ) ) != 0 ) goto error_exit;
198 error_exit_out:
199 if ( _dup2 ( stdout_backup, _fileno ( stdout ) ) != 0 ) goto error_exit;
201 hReturn = hProcess;
203 error_exit:
204 close ( stdout_backup );
205 close ( stdin_backup );
206 close ( stderr_backup );
208 return hReturn;
213 #endif /* !LISP_FEATURE_WIN32 */