OSDN Git Service

Replace local include scheme with #include of system.h
[pf3gnuchains/gcc-fork.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 #include "auto-host.h"
49 #include "gansidecl.h"
50 #include "system.h"
51
52 #include "server.h"
53
54 /* If this particular system's header files define the macro `MAXPATHLEN',
55    we happily take advantage of it; otherwise we use a value which ought
56    to be large enough.  */
57 #ifndef MAXPATHLEN
58 # define MAXPATHLEN     4096
59 #endif
60
61 #ifndef STDIN_FILENO
62 # define STDIN_FILENO   0
63 #endif
64 #ifndef STDOUT_FILENO
65 # define STDOUT_FILENO  1
66 #endif
67
68 #ifdef DEBUG
69 #define STATIC
70 #else
71 #define STATIC static
72 #endif
73 #ifndef tSCC
74 #define tSCC static const char
75 #endif
76 #ifndef NUL
77 #define NUL '\0'
78 #endif
79
80 STATIC t_pchar def_args[] =
81 { (char *) NULL, (char *) NULL };
82
83 /*
84  *  chain_open
85  *
86  *  Given an FD for an inferior process to use as stdin,
87  *  start that process and return a NEW FD that that process
88  *  will use for its stdout.  Requires the argument vector
89  *  for the new process and, optionally, a pointer to a place
90  *  to store the child's process id.
91  */
92 int
93 chain_open (stdin_fd, pp_args, p_child)
94      int stdin_fd;
95      t_pchar *pp_args;
96      pid_t *p_child;
97 {
98   t_fd_pair stdout_pair;
99   pid_t ch_id;
100   char *pz_cmd;
101
102   stdout_pair.read_fd = stdout_pair.write_fd = -1;
103
104   /*
105    *  Create a pipe it will be the child process' stdout,
106    *  and the parent will read from it.
107    */
108   if (pipe ((int *) &stdout_pair) < 0)
109     {
110       if (p_child != (pid_t *) NULL)
111         *p_child = NOPROCESS;
112       return -1;
113     }
114
115   /*
116    *  If we did not get an arg list, use the default
117    */
118   if (pp_args == (t_pchar *) NULL)
119     pp_args = def_args;
120
121   /*
122    *  If the arg list does not have a program,
123    *  assume the "SHELL" from the environment, or, failing
124    *  that, then sh.  Set argv[0] to whatever we decided on.
125    */
126   if (pz_cmd = *pp_args,
127       (pz_cmd == (char *) NULL) || (*pz_cmd == '\0'))
128     {
129
130       pz_cmd = getenv ("SHELL");
131       if (pz_cmd == (char *) NULL)
132         pz_cmd = "sh";
133     }
134
135 #ifdef DEBUG_PRINT
136   printf ("START:  %s\n", pz_cmd);
137   {
138     int idx = 0;
139     
140     while (pp_args[++idx] != (char *) NULL)
141       printf ("  ARG %2d:  %s\n", idx, pp_args[idx]);
142   }
143 #endif
144
145   /*
146    *  Call fork() and see which process we become
147    */
148   ch_id = fork ();
149   switch (ch_id)
150     {
151     case NOPROCESS:             /* parent - error in call */
152       close (stdout_pair.read_fd);
153       close (stdout_pair.write_fd);
154       if (p_child != (pid_t *) NULL)
155         *p_child = NOPROCESS;
156       return -1;
157
158     default:                    /* parent - return opposite FD's */
159       if (p_child != (pid_t *) NULL)
160         *p_child = ch_id;
161 #ifdef DEBUG_PRINT
162       printf ("for pid %d:  stdin from %d, stdout to %d\n"
163               "for parent:  read from %d\n",
164               ch_id, stdin_fd, stdout_pair.write_fd, stdout_pair.read_fd);
165 #endif
166       close (stdin_fd);
167       close (stdout_pair.write_fd);
168       return stdout_pair.read_fd;
169
170     case NULLPROCESS:           /* child - continue processing */
171       break;
172     }
173
174   /*
175    *  Close the pipe end handed back to the parent process
176    */
177   close (stdout_pair.read_fd);
178
179   /*
180    *  Close our current stdin and stdout
181    */
182   close (STDIN_FILENO);
183   close (STDOUT_FILENO);
184
185   /*
186    *  Make the fd passed in the stdin, and the write end of
187    *  the new pipe become the stdout.
188    */
189   fcntl (stdout_pair.write_fd, F_DUPFD, STDOUT_FILENO);
190   fcntl (stdin_fd, F_DUPFD, STDIN_FILENO);
191
192   if (*pp_args == (char *) NULL)
193     *pp_args = pz_cmd;
194
195   execvp (pz_cmd, pp_args);
196   fprintf (stderr, "Error %d:  Could not execvp( '%s', ... ):  %s\n",
197            errno, pz_cmd, strerror (errno));
198   exit (EXIT_PANIC);
199 }
200
201
202 /*
203  *  proc2_open
204  *
205  *  Given a pointer to an argument vector, start a process and
206  *  place its stdin and stdout file descriptors into an fd pair
207  *  structure.  The "write_fd" connects to the inferior process
208  *  stdin, and the "read_fd" connects to its stdout.  The calling
209  *  process should write to "write_fd" and read from "read_fd".
210  *  The return value is the process id of the created process.
211  */
212 pid_t
213 proc2_open (p_pair, pp_args)
214      t_fd_pair *p_pair;
215      t_pchar *pp_args;
216 {
217   pid_t ch_id;
218
219   /*  Create a bi-directional pipe.  Writes on 0 arrive on 1 and vice
220      versa, so the parent and child processes will read and write to
221      opposite FD's.  */
222   if (pipe ((int *) p_pair) < 0)
223     return NOPROCESS;
224
225   p_pair->read_fd = chain_open (p_pair->read_fd, pp_args, &ch_id);
226   if (ch_id == NOPROCESS)
227     close (p_pair->write_fd);
228
229   return ch_id;
230 }
231
232
233 /*
234  *  proc2_fopen
235  *
236  *  Identical to "proc2_open()", except that the "fd"'s are
237  *  "fdopen(3)"-ed into file pointers instead.
238  */
239 pid_t
240 proc2_fopen (pf_pair, pp_args)
241      t_pf_pair *pf_pair;
242      t_pchar *pp_args;
243 {
244   t_fd_pair fd_pair;
245   pid_t ch_id = proc2_open (&fd_pair, pp_args);
246
247   if (ch_id == NOPROCESS)
248     return ch_id;
249
250   pf_pair->pf_read = fdopen (fd_pair.read_fd, "r");
251   pf_pair->pf_write = fdopen (fd_pair.write_fd, "w");
252   return ch_id;
253 }