]> gcc.gnu.org Git - gcc.git/blob - gcc/fixinc/procopen.c
Makefile.in (fixinc.sh): Remove gnu-regex.[ch] from dependencies.
[gcc.git] / gcc / fixinc / procopen.c
1
2 /*
3 * server.c Set up and handle communications with a server process.
4 *
5 * Server Handling copyright 1992-1999 The Free Software Foundation
6 *
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.
11 *
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.
16 *
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.
22 *
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.
26 *
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.
32 *
33 * This exception does not however invalidate any other reasons why
34 * the executable file might be covered by the GNU General Public License.
35 *
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.
43 *
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.
47 */
48
49 #include "fixlib.h"
50 #include "server.h"
51
52 STATIC const char* def_args[] =
53 { (char *) NULL, (char *) NULL };
54
55 /*
56 * chain_open
57 *
58 * Given an FD for an inferior process to use as stdin,
59 * start that process and return a NEW FD that that process
60 * will use for its stdout. Requires the argument vector
61 * for the new process and, optionally, a pointer to a place
62 * to store the child's process id.
63 */
64 int
65 chain_open (stdin_fd, pp_args, p_child)
66 int stdin_fd;
67 tCC **pp_args;
68 pid_t *p_child;
69 {
70 t_fd_pair stdout_pair;
71 pid_t ch_id;
72 tCC *pz_cmd;
73
74 stdout_pair.read_fd = stdout_pair.write_fd = -1;
75
76 /*
77 * Create a pipe it will be the child process' stdout,
78 * and the parent will read from it.
79 */
80 if (pipe ((int *) &stdout_pair) < 0)
81 {
82 if (p_child != (pid_t *) NULL)
83 *p_child = NOPROCESS;
84 return -1;
85 }
86
87 /*
88 * If we did not get an arg list, use the default
89 */
90 if (pp_args == (tCC **) NULL)
91 pp_args = def_args;
92
93 /*
94 * If the arg list does not have a program,
95 * assume the "SHELL" from the environment, or, failing
96 * that, then sh. Set argv[0] to whatever we decided on.
97 */
98 if (pz_cmd = *pp_args,
99 (pz_cmd == (char *) NULL) || (*pz_cmd == '\0'))
100 {
101
102 pz_cmd = getenv ("SHELL");
103 if (pz_cmd == (char *) NULL)
104 pz_cmd = "sh";
105 }
106
107 #ifdef DEBUG_PRINT
108 printf ("START: %s\n", pz_cmd);
109 {
110 int idx = 0;
111
112 while (pp_args[++idx] != (char *) NULL)
113 printf (" ARG %2d: %s\n", idx, pp_args[idx]);
114 }
115 #endif
116
117 /*
118 * Call fork() and see which process we become
119 */
120 ch_id = fork ();
121 switch (ch_id)
122 {
123 case NOPROCESS: /* parent - error in call */
124 close (stdout_pair.read_fd);
125 close (stdout_pair.write_fd);
126 if (p_child != (pid_t *) NULL)
127 *p_child = NOPROCESS;
128 return -1;
129
130 default: /* parent - return opposite FD's */
131 if (p_child != (pid_t *) NULL)
132 *p_child = ch_id;
133 #ifdef DEBUG_PRINT
134 printf ("for pid %d: stdin from %d, stdout to %d\n"
135 "for parent: read from %d\n",
136 ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd);
137 #endif
138 close (stdin_fd);
139 close (stdout_pair.write_fd);
140 return stdout_pair.read_fd;
141
142 case NULLPROCESS: /* child - continue processing */
143 break;
144 }
145
146 /*
147 * Close the pipe end handed back to the parent process
148 */
149 close (stdout_pair.read_fd);
150
151 /*
152 * Close our current stdin and stdout
153 */
154 close (STDIN_FILENO);
155 close (STDOUT_FILENO);
156
157 /*
158 * Make the fd passed in the stdin, and the write end of
159 * the new pipe become the stdout.
160 */
161 fcntl (stdout_pair.write_fd, F_DUPFD, STDOUT_FILENO);
162 fcntl (stdin_fd, F_DUPFD, STDIN_FILENO);
163
164 if (*pp_args == (char *) NULL)
165 *pp_args = pz_cmd;
166
167 execvp (pz_cmd, (char**)pp_args);
168 fprintf (stderr, "Error %d: Could not execvp( '%s', ... ): %s\n",
169 errno, pz_cmd, xstrerror (errno));
170 exit (EXIT_PANIC);
171 }
172
173
174 /*
175 * proc2_open
176 *
177 * Given a pointer to an argument vector, start a process and
178 * place its stdin and stdout file descriptors into an fd pair
179 * structure. The "write_fd" connects to the inferior process
180 * stdin, and the "read_fd" connects to its stdout. The calling
181 * process should write to "write_fd" and read from "read_fd".
182 * The return value is the process id of the created process.
183 */
184 pid_t
185 proc2_open (p_pair, pp_args)
186 t_fd_pair *p_pair;
187 tCC **pp_args;
188 {
189 pid_t ch_id;
190
191 /* Create a bi-directional pipe. Writes on 0 arrive on 1 and vice
192 versa, so the parent and child processes will read and write to
193 opposite FD's. */
194 if (pipe ((int *) p_pair) < 0)
195 return NOPROCESS;
196
197 p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id);
198 if (ch_id == NOPROCESS)
199 close (p_pair->write_fd);
200
201 return ch_id;
202 }
203
204
205 /*
206 * proc2_fopen
207 *
208 * Identical to "proc2_open()", except that the "fd"'s are
209 * "fdopen(3)"-ed into file pointers instead.
210 */
211 pid_t
212 proc2_fopen (pf_pair, pp_args)
213 t_pf_pair *pf_pair;
214 tCC **pp_args;
215 {
216 t_fd_pair fd_pair;
217 pid_t ch_id = proc2_open (&fd_pair, pp_args);
218
219 if (ch_id == NOPROCESS)
220 return ch_id;
221
222 pf_pair->pf_read = fdopen (fd_pair.read_fd, "r");
223 pf_pair->pf_write = fdopen (fd_pair.write_fd, "w");
224 return ch_id;
225 }
This page took 0.044569 seconds and 5 git commands to generate.