Git init
[external/mawk.git] / msdos / dosexec.c
1
2 /********************************************
3 dosexec.h
4 copyright 1991, Michael D. Brennan
5
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
8
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
12
13 /*$Log: dosexec.c,v $
14  * Revision 1.3  1995/08/20  16:37:22  mike
15  * exit(1) -> exit(2)
16  *
17  * Revision 1.2  1994/10/08  18:50:03  mike
18  * remove SM_DOS
19  *
20  * Revision 1.1.1.1  1993/07/03  18:58:47  mike
21  * move source to cvs
22  *
23  * Revision 1.4  1992/12/05  22:29:43  mike
24  * dos patch 112d:
25  * don't use string_buff
26  * check COMSPEC
27  *
28  * Revision 1.3  1992/07/10  16:21:57  brennan
29  * store exit code of input pipes
30  *
31  * Revision 1.2  1991/11/16  10:27:18  brennan
32  * BINMODE
33  *
34  * Revision 1.1  91/10/29  09:45:56  brennan
35  * Initial revision
36  * 
37 */
38
39 /* system() and pipes() for MSDOS */
40
41 #include "mawk.h"
42
43 #if MSDOS
44 #include "memory.h"
45 #include "files.h"
46 #include "fin.h"
47
48 #include <process.h>
49
50 static void PROTO(get_shell, (void)) ;
51
52 static char *shell ;     /* e.g.   "c:\\sys\\command.com"  */
53 static char *command_opt ;  /*  " /c"  */
54
55 static void get_shell()
56 { char *s , *p ;
57   int len ;
58
59   if ( s = getenv("MAWKSHELL") )
60   {
61     /* break into shell part and option part */
62     p = s ;
63     while ( *p != ' ' && *p != '\t' ) p++ ;
64     len = p - s ;
65     shell = (char *) zmalloc(len+1) ;
66     memcpy(shell, s, len) ; shell[len] = 0 ;
67     command_opt = p ;
68   }
69   else
70   if ( s = getenv("COMSPEC") )
71   {
72     shell = s ;
73     command_opt = " /c" ;
74         /* leading space needed because of bug in command.com */
75   }
76   else
77   {
78     errmsg(0,
79     "cannot exec(), must set MAWKSHELL or COMSPEC in environment" ) ;
80     exit(2) ;
81   }
82 }
83
84
85 int DOSexec( command )
86   char *command ;
87 {
88   char xbuff[256] ;
89
90   if ( ! shell )  get_shell() ;
91
92   sprintf(xbuff, "%s %s", command_opt, command) ;
93
94   fflush(stderr) ; fflush(stdout) ;
95
96   return   spawnl(P_WAIT, shell, shell, xbuff, (char *) 0 ) ;
97 }
98
99
100 static int next_tmp ;  /* index for naming temp files */
101 static char *tmpdir ;  /* directory to hold temp files */
102 static unsigned mawkid ; /* unique to this mawk process */
103
104
105 /* compute the unique temp file name associated with id */
106 char *tmp_file_name(id, buffer )
107   int id ;
108   char *buffer ;
109 {
110   if ( mawkid == 0 )
111   {
112     /* first time */
113     union {
114     void far *ptr ;
115     unsigned w[2] ;
116     } xptr ;
117
118     xptr.ptr = (void far*)&mawkid ;
119     mawkid = xptr.w[1] ;
120
121     tmpdir = getenv("MAWKTMPDIR") ;
122     if ( ! tmpdir || strlen(tmpdir) > 80 ) tmpdir = "" ;
123   }
124
125   (void) sprintf(buffer, "%sMAWK%04X.%03X",tmpdir, mawkid, id) ;
126   return buffer ;
127 }
128
129 /* open a pipe, returning a temp file identifier by
130    reference
131 */
132
133 PTR  get_pipe( command, type, tmp_idp)
134   char *command ;
135   int type, *tmp_idp ;
136 {
137   PTR  retval ;
138   char xbuff[256] ;
139   char *tmpname ;
140
141
142   *tmp_idp = next_tmp ;
143   tmpname = tmp_file_name(next_tmp, xbuff+163) ;
144
145   if ( type == PIPE_OUT )  
146   {
147     retval = (PTR) fopen(tmpname, (binmode()&2)? "wb":"w") ;
148   }
149   else
150   { 
151     sprintf(xbuff, "%s > %s" , command, tmpname) ;
152     tmp_idp[1] = DOSexec(xbuff) ;
153     retval = (PTR) FINopen(tmpname, 0) ;
154   }
155
156   next_tmp++ ;
157   return retval ;
158 }
159
160 /* closing a fake pipes involves running the out pipe 
161    command
162 */
163
164 int close_fake_outpipe(command, tid)
165   char *command ;
166   int tid ; /* identifies the temp file */
167 {
168   char xbuff[256] ;
169   char *tmpname = tmp_file_name(tid, xbuff+163) ;
170   int retval ;
171
172   sprintf(xbuff, "%s < %s", command, tmpname) ;
173   retval = DOSexec(xbuff) ;
174   (void) unlink(tmpname) ;
175   return retval ;
176 }
177
178 #endif  /* MSDOS */