Avoid unnecessary dependencies on COND_EXEC insns.
[official-gcc.git] / gcc / fixinc / procopen.c
blob91566eb5a956649f57a3d07e8767420a9eb0a1a6
2 /*
3 * server.c Set up and handle communications with a server process.
5 * Server Handling copyright 1992-1999 The Free Software Foundation
7 * Server Handling is free software.
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2, or (at your option) any later version.
12 * Server Handling is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Server Handling. See the file "COPYING". If not,
19 * write to: The Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
23 * As a special exception, The Free Software Foundation gives
24 * permission for additional uses of the text contained in his release
25 * of ServerHandler.
27 * The exception is that, if you link the ServerHandler library with other
28 * files to produce an executable, this does not by itself cause the
29 * resulting executable to be covered by the GNU General Public License.
30 * Your use of that executable is in no way restricted on account of
31 * linking the ServerHandler library code into it.
33 * This exception does not however invalidate any other reasons why
34 * the executable file might be covered by the GNU General Public License.
36 * This exception applies only to the code released by The Free
37 * Software Foundation under the name ServerHandler. If you copy code
38 * from other sources under the General Public License into a copy of
39 * ServerHandler, as the General Public License permits, the exception
40 * does not apply to the code that you add in this way. To avoid
41 * misleading anyone as to the status of such modified files, you must
42 * delete this exception notice from them.
44 * If you write modifications of your own for ServerHandler, it is your
45 * choice whether to permit this exception to apply to your modifications.
46 * If you do not wish that, delete this exception notice.
48 #include "auto-host.h"
49 #include "gansidecl.h"
50 #include "system.h"
52 #include "server.h"
53 #include "fixlib.h"
55 STATIC const char* def_args[] =
56 { (char *) NULL, (char *) NULL };
59 * chain_open
61 * Given an FD for an inferior process to use as stdin,
62 * start that process and return a NEW FD that that process
63 * will use for its stdout. Requires the argument vector
64 * for the new process and, optionally, a pointer to a place
65 * to store the child's process id.
67 int
68 chain_open (stdin_fd, pp_args, p_child)
69 int stdin_fd;
70 tCC **pp_args;
71 pid_t *p_child;
73 t_fd_pair stdout_pair;
74 pid_t ch_id;
75 tCC *pz_cmd;
77 stdout_pair.read_fd = stdout_pair.write_fd = -1;
80 * Create a pipe it will be the child process' stdout,
81 * and the parent will read from it.
83 if (pipe ((int *) &stdout_pair) < 0)
85 if (p_child != (pid_t *) NULL)
86 *p_child = NOPROCESS;
87 return -1;
91 * If we did not get an arg list, use the default
93 if (pp_args == (tCC **) NULL)
94 pp_args = def_args;
97 * If the arg list does not have a program,
98 * assume the "SHELL" from the environment, or, failing
99 * that, then sh. Set argv[0] to whatever we decided on.
101 if (pz_cmd = *pp_args,
102 (pz_cmd == (char *) NULL) || (*pz_cmd == '\0'))
105 pz_cmd = getenv ("SHELL");
106 if (pz_cmd == (char *) NULL)
107 pz_cmd = "sh";
110 #ifdef DEBUG_PRINT
111 printf ("START: %s\n", pz_cmd);
113 int idx = 0;
115 while (pp_args[++idx] != (char *) NULL)
116 printf (" ARG %2d: %s\n", idx, pp_args[idx]);
118 #endif
121 * Call fork() and see which process we become
123 ch_id = fork ();
124 switch (ch_id)
126 case NOPROCESS: /* parent - error in call */
127 close (stdout_pair.read_fd);
128 close (stdout_pair.write_fd);
129 if (p_child != (pid_t *) NULL)
130 *p_child = NOPROCESS;
131 return -1;
133 default: /* parent - return opposite FD's */
134 if (p_child != (pid_t *) NULL)
135 *p_child = ch_id;
136 #ifdef DEBUG_PRINT
137 printf ("for pid %d: stdin from %d, stdout to %d\n"
138 "for parent: read from %d\n",
139 ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd);
140 #endif
141 close (stdin_fd);
142 close (stdout_pair.write_fd);
143 return stdout_pair.read_fd;
145 case NULLPROCESS: /* child - continue processing */
146 break;
150 * Close the pipe end handed back to the parent process
152 close (stdout_pair.read_fd);
155 * Close our current stdin and stdout
157 close (STDIN_FILENO);
158 close (STDOUT_FILENO);
161 * Make the fd passed in the stdin, and the write end of
162 * the new pipe become the stdout.
164 fcntl (stdout_pair.write_fd, F_DUPFD, STDOUT_FILENO);
165 fcntl (stdin_fd, F_DUPFD, STDIN_FILENO);
167 if (*pp_args == (char *) NULL)
168 *pp_args = pz_cmd;
170 execvp (pz_cmd, (char**)pp_args);
171 fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n",
172 errno, pz_cmd, xstrerror (errno));
173 exit (EXIT_PANIC);
178 * proc2_open
180 * Given a pointer to an argument vector, start a process and
181 * place its stdin and stdout file descriptors into an fd pair
182 * structure. The "write_fd" connects to the inferior process
183 * stdin, and the "read_fd" connects to its stdout. The calling
184 * process should write to "write_fd" and read from "read_fd".
185 * The return value is the process id of the created process.
187 pid_t
188 proc2_open (p_pair, pp_args)
189 t_fd_pair *p_pair;
190 tCC **pp_args;
192 pid_t ch_id;
194 /* Create a bi-directional pipe. Writes on 0 arrive on 1 and vice
195 versa, so the parent and child processes will read and write to
196 opposite FD's. */
197 if (pipe ((int *) p_pair) < 0)
198 return NOPROCESS;
200 p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id);
201 if (ch_id == NOPROCESS)
202 close (p_pair->write_fd);
204 return ch_id;
209 * proc2_fopen
211 * Identical to "proc2_open()", except that the "fd"'s are
212 * "fdopen(3)"-ed into file pointers instead.
214 pid_t
215 proc2_fopen (pf_pair, pp_args)
216 t_pf_pair *pf_pair;
217 tCC **pp_args;
219 t_fd_pair fd_pair;
220 pid_t ch_id = proc2_open (&fd_pair, pp_args);
222 if (ch_id == NOPROCESS)
223 return ch_id;
225 pf_pair->pf_read = fdopen (fd_pair.read_fd, "r");
226 pf_pair->pf_write = fdopen (fd_pair.write_fd, "w");
227 return ch_id;