update dev300-m58
[ooovba.git] / dmake / msdos / runargv.c
blobd91e8bffccba4a9973044f5cfe219b2118235b47
1 /* RCS $Id: runargv.c,v 1.6 2008-03-05 18:35:53 kz Exp $
2 --
3 -- SYNOPSIS
4 -- Run a sub process.
5 --
6 -- DESCRIPTION
7 -- Use spawn to run a subprocess.
8 --
9 -- AUTHOR
10 -- Dennis Vadura, dvadura@dmake.wticorp.com
12 -- WWW
13 -- http://dmake.wticorp.com/
15 -- COPYRIGHT
16 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
17 --
18 -- This program is NOT free software; you can redistribute it and/or
19 -- modify it under the terms of the Software License Agreement Provided
20 -- in the file <distribution-root>/readme/license.txt.
22 -- LOG
23 -- Use cvs log to obtain detailed change logs.
26 #if defined(USE_CREATEPROCESS)
27 /* MSVC6.0 and newer and MinGW use the parallel build enabled runargv(). */
28 Force a compile-time blowup.
29 This file should not be used, use unix/runargv.c instead.
30 #endif
32 #include <process.h>
33 #include <errno.h>
34 #include "extern.h"
35 #include "sysintf.h"
37 static int _abort_flg = FALSE;
38 static void _add_child ANSI((CELLPTR, int));
39 static void _finished_child ANSI((int));
41 PUBLIC int
42 runargv(target, group, last, cmnd_attr, cmd)
43 CELLPTR target;
44 int group;
45 int last;
46 t_attr cmnd_attr; /* Attributes for current cmnd. */
47 char **cmd; /* Simulate a reference to *cmd. */
49 int ignore = (cmnd_attr & A_IGNORE)!= 0; /* Ignore errors ('-'). */
50 int shell = (cmnd_attr & A_SHELL) != 0; /* Use shell ('+'). */
51 int mute = (cmnd_attr & A_MUTE) != 0; /* Mute output ('@@'). */
52 #if ! defined(_MSC_VER)
53 #if defined(__BORLANDC__) && __BORLANDC__ >= 0x500
54 extern char ** _RTLENTRY _EXPDATA environ;
55 #else
56 extern char **environ;
57 #endif
58 #endif
59 int status;
60 char **argv;
61 int old_stdout = -1; /* For redirecting shell escapes */
62 int old_stderr = -1; /* and silencing @@-recipes */
63 char *tcmd = *cmd; /* For saver/easier string arithmetic on *cmd. */
65 if( Measure & M_RECIPE )
66 Do_profile_output( "s", M_RECIPE, target );
68 _add_child(target, ignore);
70 /* redirect output for _exec_shell / @@-recipes. */
71 if( Is_exec_shell ) {
72 /* Add error checking? */
73 old_stdout = dup(1);
74 dup2( fileno(stdout_redir), 1 );
76 if( mute ) {
77 old_stderr = dup(2);
78 dup2( zerofd, 2 );
80 if( !Is_exec_shell ) {
81 old_stdout = dup(1);
82 dup2( zerofd, 1 );
86 /* Return immediately for empty line or noop command. */
87 if ( !*tcmd || /* empty line */
88 ( strncmp(tcmd, "noop", 4) == 0 && /* noop command */
89 (iswhite(tcmd[4]) || tcmd[4] == '\0')) ) {
90 status = 0;
92 else if( !shell && /* internal echo only if not in shell */
93 strncmp(tcmd, "echo", 4) == 0 &&
94 (iswhite(tcmd[4]) || tcmd[4] == '\0') ) {
95 int nl = 1;
97 tcmd = tcmd + 4;
99 while( iswhite(*tcmd) ) ++tcmd;
100 if ( strncmp(tcmd,"-n",2 ) == 0) {
101 nl = 0;
102 tcmd = tcmd+2;
103 while( iswhite(*tcmd) ) ++tcmd;
105 printf("%s%s", tcmd, nl ? "\n" : "");
106 fflush(stdout);
107 status = 0;
109 else {
110 argv = Pack_argv( group, shell, cmd );
111 Packed_shell = shell||group;
113 /* The last two arguments would need (const char *const *) casts
114 * to silence the warning when building with MinGW. */
115 status = spawnvpe(P_WAIT, *argv, argv, environ);
118 /* Restore stdout/stderr if needed. */
119 if( old_stdout != -1 ) {
120 dup2(old_stdout, 1);
121 if( old_stderr != -1 )
122 dup2(old_stderr, 2);
125 if( status == -1 ) {
126 /* spawnvpe failed */
127 fprintf(stderr, "%s: Error executing '%s': %s",
128 Pname, argv[0], strerror(errno) );
129 if( ignore||Continue ) {
130 fprintf(stderr, " (Ignored)" );
132 fprintf(stderr, "\n");
135 if( Measure & M_RECIPE )
136 Do_profile_output( "e", M_RECIPE, target );
138 _finished_child(status);
139 if( last && !Doing_bang ) Update_time_stamp( target );
141 return( 0 );
145 PUBLIC void
146 Clean_up_processes()
148 _abort_flg = TRUE;
149 _finished_child(-1);
153 PUBLIC int
154 Wait_for_child( abort_flg, pid )
155 int abort_flg;
156 int pid;
158 /* There is currently no parallel processing for this OS, always
159 * return -1 indicating that there was nothing to wait for. */
160 return(-1);
164 static int _valid = -1;
165 static CELLPTR _tg;
166 static int _ignore;
168 static void
169 _add_child( target, ignore )
170 CELLPTR target;
171 int ignore;
173 _tg = target;
174 _ignore = ignore;
175 _valid = 0;
177 Current_target = NIL(CELL);
181 static void
182 _finished_child(status)
183 int status;
185 if( _valid == -1 ) return;
186 _valid = -1;
187 Handle_result( status, _ignore, _abort_flg, _tg );