summaryrefslogtreecommitdiff
path: root/dmake/msdos/runargv.c
blob: 11a5ebd4b066bdcc4b7fd0bf9af752d4d62df377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* RCS  $Id: runargv.c,v 1.5 2007-10-15 15:43:15 ihi Exp $
--
-- SYNOPSIS
--      Run a sub process.
--
-- DESCRIPTION
--  Use spawn to run a subprocess.
--
-- AUTHOR
--      Dennis Vadura, dvadura@dmake.wticorp.com
--
-- WWW
--      http://dmake.wticorp.com/
--
-- COPYRIGHT
--      Copyright (c) 1996,1997 by WTI Corp.  All rights reserved.
--
--      This program is NOT free software; you can redistribute it and/or
--      modify it under the terms of the Software License Agreement Provided
--      in the file <distribution-root>/readme/license.txt.
--
-- LOG
--      Use cvs log to obtain detailed change logs.
*/

#include <process.h>
#include <errno.h>
#include "extern.h"
#include "sysintf.h"

static int  _abort_flg = FALSE;
static void _add_child ANSI((CELLPTR, int));
static void _finished_child ANSI((int));

PUBLIC int
runargv(target, group, last, cmnd_attr, cmd)
CELLPTR target;
int group;
int last;
t_attr  cmnd_attr; /* Attributes for current cmnd. */
char  **cmd; /* Simulate a reference to *cmd. */
{
   int  ignore = (cmnd_attr & A_IGNORE)!= 0; /* Ignore errors ('-'). */
   int  shell  = (cmnd_attr & A_SHELL) != 0; /* Use shell ('+'). */
   int  mute = (cmnd_attr & A_MUTE) != 0; /* Mute output ('@@'). */
#if ! defined(_MSC_VER)
#if defined(__BORLANDC__) && __BORLANDC__ >= 0x500
   extern char ** _RTLENTRY _EXPDATA environ;
#else
   extern char **environ;
#endif
#endif
   int status;
   char **argv;
   int old_stdout = -1; /* For redirecting shell escapes */
   int old_stderr = -1; /* and silencing @@-recipes      */
   char *tcmd = *cmd; /* For saver/easier string arithmetic on *cmd. */

   if( Measure & M_RECIPE )
      Do_profile_output( "s", M_RECIPE, target );

   _add_child(target, ignore);

   /* redirect output for _exec_shell / @@-recipes. */
   if( Is_exec_shell ) {
      /* Add error checking? */
      old_stdout = dup(1);
      dup2( fileno(stdout_redir), 1 );
   }
   if( mute ) {
      old_stderr = dup(2);
      dup2( zerofd, 2 );

      if( !Is_exec_shell ) {
     old_stdout = dup(1);
     dup2( zerofd, 1 );
      }
   }

   /* Return immediately for empty line or noop command. */
   if ( !*tcmd ||               /* empty line */
    ( strncmp(tcmd, "noop", 4) == 0 &&  /* noop command */
      (iswhite(tcmd[4]) || tcmd[4] == '\0')) ) {
      status = 0;
   }
   else if( !shell &&  /* internal echo only if not in shell */
        strncmp(tcmd, "echo", 4) == 0 &&
        (iswhite(tcmd[4]) || tcmd[4] == '\0') ) {
      int nl = 1;

      tcmd = tcmd + 4;

      while( iswhite(*tcmd) ) ++tcmd;
      if ( strncmp(tcmd,"-n",2 ) == 0) {
     nl = 0;
     tcmd = tcmd+2;
     while( iswhite(*tcmd) ) ++tcmd;
      }
      printf("%s%s", tcmd, nl ? "\n" : "");
      fflush(stdout);
      status = 0;
   }
   else {
      argv = Pack_argv( group, shell, cmd );
      Packed_shell = shell||group;

      /* The last two arguments would need (const char *const *) casts
       * to silence the warning when building with MinGW. */
      status = spawnvpe(P_WAIT, *argv, argv, environ);
   }

   /* Restore stdout/stderr if needed. */
   if( old_stdout != -1 ) {
      dup2(old_stdout, 1);
      if( old_stderr != -1 )
     dup2(old_stderr, 2);
   }

   if( status == -1 ) {
      /* spawnvpe failed */
      fprintf(stderr, "%s:  Error executing '%s': %s",
          Pname, argv[0], strerror(errno) );
      if( ignore||Continue ) {
     fprintf(stderr, " (Ignored)" );
      }
      fprintf(stderr, "\n");
   }

   if( Measure & M_RECIPE )
      Do_profile_output( "e", M_RECIPE, target );

   _finished_child(status);
   if( last && !Doing_bang ) Update_time_stamp( target );

   return( 0 );
}


PUBLIC void
Clean_up_processes()
{
   _abort_flg = TRUE;
   _finished_child(-1);
}


PUBLIC int
Wait_for_child( abort_flg, pid )
int abort_flg;
int pid;
{
   /* There is currently no parallel processing for this OS, always
    * return -1 indicating that there was nothing to wait for. */
   return(-1);
}


static int     _valid = -1;
static CELLPTR _tg;
static int     _ignore;

static void
_add_child( target, ignore )
CELLPTR target;
int ignore;
{
   _tg = target;
   _ignore = ignore;
   _valid = 0;

   Current_target = NIL(CELL);
}


static void
_finished_child(status)
int status;
{
   if( _valid == -1 ) return;
   _valid = -1;
   Handle_result( status, _ignore, _abort_flg, _tg );
}