merge from gcc
[binutils.git] / libiberty / pex-mpw.c
blob9a8879c9b271e30201e698abe5e75104bf2453a9
1 /* Utilities to execute a program in a subprocess (possibly linked by pipes
2 with other subprocesses), and wait for it. MPW specialization.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003
4 Free Software Foundation, Inc.
6 This file is part of the libiberty library.
7 Libiberty is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 Libiberty 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 GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with libiberty; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "pex-common.h"
24 #include <stdio.h>
25 #ifdef HAVE_STRING_H
26 #include <string.h>
27 #endif
29 /* MPW pexecute doesn't actually run anything; instead, it writes out
30 script commands that, when run, will do the actual executing.
32 For example, in GCC's case, GCC will write out several script commands:
34 cpp ...
35 cc1 ...
36 as ...
37 ld ...
39 and then exit. None of the above programs will have run yet. The task
40 that called GCC will then execute the script and cause cpp,etc. to run.
41 The caller must invoke pfinish before calling exit. This adds
42 the finishing touches to the generated script. */
44 static int first_time = 1;
46 extern void mpwify_filename PARAMS ((const char *, char *));
48 int
49 pexecute (program, argv, this_pname, temp_base, errmsg_fmt, errmsg_arg, flags)
50 const char *program;
51 char * const *argv;
52 const char *this_pname;
53 const char *temp_base;
54 char **errmsg_fmt, **errmsg_arg;
55 int flags;
57 char tmpprogram[255];
58 char *cp, *tmpname;
59 int i;
61 mpwify_filename (program, tmpprogram);
62 if (first_time)
64 printf ("Set Failed 0\n");
65 first_time = 0;
68 fputs ("If {Failed} == 0\n", stdout);
69 /* If being verbose, output a copy of the command. It should be
70 accurate enough and escaped enough to be "clickable". */
71 if (flags & PEXECUTE_VERBOSE)
73 fputs ("\tEcho ", stdout);
74 fputc ('\'', stdout);
75 fputs (tmpprogram, stdout);
76 fputc ('\'', stdout);
77 fputc (' ', stdout);
78 for (i=1; argv[i]; i++)
80 fputc ('\'', stdout);
81 /* See if we have an argument that needs fixing. */
82 if (strchr(argv[i], '/'))
84 tmpname = (char *) xmalloc (256);
85 mpwify_filename (argv[i], tmpname);
86 argv[i] = tmpname;
88 for (cp = argv[i]; *cp; cp++)
90 /* Write an Option-d escape char in front of special chars. */
91 if (strchr("'+", *cp))
92 fputc ('\266', stdout);
93 fputc (*cp, stdout);
95 fputc ('\'', stdout);
96 fputc (' ', stdout);
98 fputs ("\n", stdout);
100 fputs ("\t", stdout);
101 fputs (tmpprogram, stdout);
102 fputc (' ', stdout);
104 for (i=1; argv[i]; i++)
106 /* See if we have an argument that needs fixing. */
107 if (strchr(argv[i], '/'))
109 tmpname = (char *) xmalloc (256);
110 mpwify_filename (argv[i], tmpname);
111 argv[i] = tmpname;
113 if (strchr (argv[i], ' '))
114 fputc ('\'', stdout);
115 for (cp = argv[i]; *cp; cp++)
117 /* Write an Option-d escape char in front of special chars. */
118 if (strchr("'+", *cp))
119 fputc ('\266', stdout);
120 fputc (*cp, stdout);
122 if (strchr (argv[i], ' '))
123 fputc ('\'', stdout);
124 fputc (' ', stdout);
127 fputs ("\n", stdout);
129 /* Output commands that arrange to clean up and exit if a failure occurs.
130 We have to be careful to collect the status from the program that was
131 run, rather than some other script command. Also, we don't exit
132 immediately, since necessary cleanups are at the end of the script. */
133 fputs ("\tSet TmpStatus {Status}\n", stdout);
134 fputs ("\tIf {TmpStatus} != 0\n", stdout);
135 fputs ("\t\tSet Failed {TmpStatus}\n", stdout);
136 fputs ("\tEnd\n", stdout);
137 fputs ("End\n", stdout);
139 /* We're just composing a script, can't fail here. */
140 return 0;
144 pwait (pid, status, flags)
145 int pid;
146 int *status;
147 int flags;
149 *status = 0;
150 return 0;
153 /* Write out commands that will exit with the correct error code
154 if something in the script failed. */
156 void
157 pfinish ()
159 printf ("\tExit \"{Failed}\"\n");