xref: /dflybsd-src/contrib/gcc-4.7/libiberty/pexecute.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Utilities to execute a program in a subprocess (possibly linked by pipes
2*e4b17023SJohn Marino    with other subprocesses), and wait for it.
3*e4b17023SJohn Marino    Copyright (C) 2004 Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of the libiberty library.
6*e4b17023SJohn Marino Libiberty is free software; you can redistribute it and/or
7*e4b17023SJohn Marino modify it under the terms of the GNU Library General Public
8*e4b17023SJohn Marino License as published by the Free Software Foundation; either
9*e4b17023SJohn Marino version 2 of the License, or (at your option) any later version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino Libiberty is distributed in the hope that it will be useful,
12*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*e4b17023SJohn Marino Library General Public License for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino You should have received a copy of the GNU Library General Public
17*e4b17023SJohn Marino License along with libiberty; see the file COPYING.LIB.  If not,
18*e4b17023SJohn Marino write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19*e4b17023SJohn Marino Boston, MA 02110-1301, USA.  */
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino /* pexecute is an old routine.  This implementation uses the newer
22*e4b17023SJohn Marino    pex_init/pex_run/pex_get_status/pex_free routines.  Don't use
23*e4b17023SJohn Marino    pexecute in new code.  Use the newer routines instead.  */
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino #include "config.h"
26*e4b17023SJohn Marino #include "libiberty.h"
27*e4b17023SJohn Marino 
28*e4b17023SJohn Marino #ifdef HAVE_STDLIB_H
29*e4b17023SJohn Marino #include <stdlib.h>
30*e4b17023SJohn Marino #endif
31*e4b17023SJohn Marino 
32*e4b17023SJohn Marino /* We only permit a single pexecute chain to execute at a time.  This
33*e4b17023SJohn Marino    was always true anyhow, though it wasn't documented.  */
34*e4b17023SJohn Marino 
35*e4b17023SJohn Marino static struct pex_obj *pex;
36*e4b17023SJohn Marino static int idx;
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino int
pexecute(const char * program,char * const * argv,const char * pname,const char * temp_base,char ** errmsg_fmt,char ** errmsg_arg,int flags)39*e4b17023SJohn Marino pexecute (const char *program, char * const *argv, const char *pname,
40*e4b17023SJohn Marino 	  const char *temp_base, char **errmsg_fmt, char **errmsg_arg,
41*e4b17023SJohn Marino 	  int flags)
42*e4b17023SJohn Marino {
43*e4b17023SJohn Marino   const char *errmsg;
44*e4b17023SJohn Marino   int err;
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino   if ((flags & PEXECUTE_FIRST) != 0)
47*e4b17023SJohn Marino     {
48*e4b17023SJohn Marino       if (pex != NULL)
49*e4b17023SJohn Marino 	{
50*e4b17023SJohn Marino 	  *errmsg_fmt = (char *) "pexecute already in progress";
51*e4b17023SJohn Marino 	  *errmsg_arg = NULL;
52*e4b17023SJohn Marino 	  return -1;
53*e4b17023SJohn Marino 	}
54*e4b17023SJohn Marino       pex = pex_init (PEX_USE_PIPES, pname, temp_base);
55*e4b17023SJohn Marino       idx = 0;
56*e4b17023SJohn Marino     }
57*e4b17023SJohn Marino   else
58*e4b17023SJohn Marino     {
59*e4b17023SJohn Marino       if (pex == NULL)
60*e4b17023SJohn Marino 	{
61*e4b17023SJohn Marino 	  *errmsg_fmt = (char *) "pexecute not in progress";
62*e4b17023SJohn Marino 	  *errmsg_arg = NULL;
63*e4b17023SJohn Marino 	  return -1;
64*e4b17023SJohn Marino 	}
65*e4b17023SJohn Marino     }
66*e4b17023SJohn Marino 
67*e4b17023SJohn Marino   errmsg = pex_run (pex,
68*e4b17023SJohn Marino 		    (((flags & PEXECUTE_LAST) != 0 ? PEX_LAST : 0)
69*e4b17023SJohn Marino 		     | ((flags & PEXECUTE_SEARCH) != 0 ? PEX_SEARCH : 0)),
70*e4b17023SJohn Marino 		    program, argv, NULL, NULL, &err);
71*e4b17023SJohn Marino   if (errmsg != NULL)
72*e4b17023SJohn Marino     {
73*e4b17023SJohn Marino       *errmsg_fmt = (char *) errmsg;
74*e4b17023SJohn Marino       *errmsg_arg = NULL;
75*e4b17023SJohn Marino       return -1;
76*e4b17023SJohn Marino     }
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino   /* Instead of a PID, we just return a one-based index into the
79*e4b17023SJohn Marino      status values.  We avoid zero just because the old pexecute would
80*e4b17023SJohn Marino      never return it.  */
81*e4b17023SJohn Marino   return ++idx;
82*e4b17023SJohn Marino }
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino int
pwait(int pid,int * status,int flags ATTRIBUTE_UNUSED)85*e4b17023SJohn Marino pwait (int pid, int *status, int flags ATTRIBUTE_UNUSED)
86*e4b17023SJohn Marino {
87*e4b17023SJohn Marino   /* The PID returned by pexecute is one-based.  */
88*e4b17023SJohn Marino   --pid;
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino   if (pex == NULL || pid < 0 || pid >= idx)
91*e4b17023SJohn Marino     return -1;
92*e4b17023SJohn Marino 
93*e4b17023SJohn Marino   if (pid == 0 && idx == 1)
94*e4b17023SJohn Marino     {
95*e4b17023SJohn Marino       if (!pex_get_status (pex, 1, status))
96*e4b17023SJohn Marino 	return -1;
97*e4b17023SJohn Marino     }
98*e4b17023SJohn Marino   else
99*e4b17023SJohn Marino     {
100*e4b17023SJohn Marino       int *vector;
101*e4b17023SJohn Marino 
102*e4b17023SJohn Marino       vector = XNEWVEC (int, idx);
103*e4b17023SJohn Marino       if (!pex_get_status (pex, idx, vector))
104*e4b17023SJohn Marino 	{
105*e4b17023SJohn Marino 	  free (vector);
106*e4b17023SJohn Marino 	  return -1;
107*e4b17023SJohn Marino 	}
108*e4b17023SJohn Marino       *status = vector[pid];
109*e4b17023SJohn Marino       free (vector);
110*e4b17023SJohn Marino     }
111*e4b17023SJohn Marino 
112*e4b17023SJohn Marino   /* Assume that we are done after the caller has retrieved the last
113*e4b17023SJohn Marino      exit status.  The original implementation did not require that
114*e4b17023SJohn Marino      the exit statuses be retrieved in order, but this implementation
115*e4b17023SJohn Marino      does.  */
116*e4b17023SJohn Marino   if (pid + 1 == idx)
117*e4b17023SJohn Marino     {
118*e4b17023SJohn Marino       pex_free (pex);
119*e4b17023SJohn Marino       pex = NULL;
120*e4b17023SJohn Marino       idx = 0;
121*e4b17023SJohn Marino     }
122*e4b17023SJohn Marino 
123*e4b17023SJohn Marino   return pid + 1;
124*e4b17023SJohn Marino }
125