/
DOSCL1.C
268 lines (226 loc) · 10.3 KB
/
DOSCL1.C
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************************/
/* */
/* */
/* */
/* COPYRIGHT IBM CORP., 1992 */
/* */
/* ------------------------------------------------------------------------ */
/* */
/* */
/* This is the OS/2 component of the DOSRUN package. It's responsible */
/* for establishing a unique container between this OS/2 component and */
/* its sister DOS component. The DOS component will handle redirecting */
/* all STDOUT and STDERR messages from the DOS tool into this unique */
/* container. Once the DOS tool has completed, it will also communicate */
/* the return codes either through the container or through some other */
/* means. In the current DOSRUN package, the unique container implemented */
/* is a simple temporary file. Once the DOS tool has completed, the */
/* return codes will also be added to the end of the temporary file. */
/* While the OS/2 component is waiting for the DOS tool to end, it will */
/* read from the unique container and echo it out to its STDOUT. Since */
/* the WorkFrame/2 has already redirected its STDOUT and STDERR into */
/* the WorkFrame/2's output listbox, this is all that is required to */
/* redirect the DOS tool output into the WorkFrame/2's output listbox. */
/* */
/* The following illustrates this process for a DOS compiler: */
/* */
/* */
/* ÚÄÄÄÄ¿ */
/* ³ ³ */
/* ³ ³ */
/* ³ ³ */
/* ³WF/2³ */
/* ³ ³ */
/* ³ ³ */
/* ÀÄÂÄÄÙ */
/* ³ */
/* ³ User invokes */
/* ³ action COMPILE */
/* */
/* ÚÄÁÄÄÄÄÄÄÄÄÄ¿ ÚÄÄÄÄÄÄÄÄ¿ */
/* ³ ³ ³WF/2 ³ */
/* ³WF/2 ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´Output ³ */
/* ³Background ³ ³Listbox ³ */
/* ³Thread ³ ÀÄÄÄÂÄÄÄÄÙ */
/* ÀÄÂÄÄÄÄÄÄÄÄÄÙ ³ */
/* ³ Contents of the unique container */
/* ³ Redirect STDOUT/STDERR ³is echoed by DOSCL1.EXE through */
/* ³ back to output listbox ³its STDOUT which has been */
/* ³ and invoke DOSCL1.EXE ³redirected back to the WF/2. */
/* ÚÄÄÄÁÄÄÄÄÄ¿ */
/* ÚÄÁÄÄÄÄÄÄÄÄ¿ ³Unique ³ */
/* ³DOSCL1.EXEÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´Container³ */
/* ÀÄÂÄÄÄÄÄÄÄÄÙ ³ ³ */
/* ³ ÀÄÄÄÂÄÄÄÄÄÙ */
/* ³ Create a unique container ³ */
/* ³ and invoke DOSCL2.EXE */
/* ³ the name of the container ³ */
/* ³ */
/* ÚÄÁÄÄÄÄÄÄÄÄ¿ ³ */
/* ³DOSCL2.EXE³ ³ */
/* ÀÄÂÄÄÄÄÄÄÄÄÙ ³ */
/* ³ ³ */
/* ³ Redirect STDOUT/STDERR ³ */
/* ³ back to the unique container ³STDOUT/STDERR from DOS */
/* ³ and invoke DOS Compiler ³compiler redirected back to */
/* ³the unique container */
/* ÚÄÁÄÄÄÄÄÄÄÄÄÄ¿ ³ */
/* ³DOS CompilerÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ */
/* ÀÄÄÄÄÄÄÄÄÄÄÄÄÙ */
/* */
/* */
/* */
/* A better implementation could use a named pipe as the unique container. */
/* This way, you can avoid the expense of the physical I/O to disk. */
/* */
/****************************************************************************/
#define INCL_DOS
#define INCL_ERRORS
#define INCL_WIN
#include <os2.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <signal.h>
#define EOFFLAG 26
static unsigned char *get_cmdline( void );
#define BUFSIZE 256
ULONG sess_id,pid;
char *tempfile;
HFILE handle;
void handler( int signum );
BOOL ReadData( HFILE handle );
char tmpbuf[BUFSIZE];
int rc;
int main( void )
{
unsigned char *pCmdline;
STARTDATA sd; /* structure defined in bsedos.h */
STATUSDATA statusData;
char *buffer;
char *p1, *p2;
char *doscl2;
ULONG action;
BOOL Done, Done2=FALSE;
/* Set exception handler for CTRL-C and CTRL-BREAK. */
if ( SIG_ERR==signal(SIGILL, handler ) ||
SIG_ERR==signal(SIGTERM, handler ) ||
SIG_ERR==signal(SIGBREAK, handler ) )
exit(2);
/* Get a pointer to the command line. */
pCmdline = get_cmdline( );
/* Get the executable directory, use it for doscl2.exe */
p1 = pCmdline + strlen( pCmdline );
p2 = p1 + 1;
while ( p1 != pCmdline && *(p1-1) != '\\' && *(p1-1) != '/' ) --p1;
*p1 = '\0';
/* Fully qualify doscl2.exe */
doscl2 = malloc( strlen(pCmdline) + strlen("doscl2.exe") + 1 );
strcpy( doscl2, pCmdline );
strcat( doscl2, "doscl2.exe" );
/* Open a tempfile for doscl2.exe to write and doscl1.exe to read */
tempfile = tmpnam( NULL );
if ( (rc=DosOpen( tempfile,
&handle,
&action,
0,
FILE_NORMAL,
OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW,
OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
0 )) != 0 )
{
return rc;
}
/* Construct the input parameter string to doscl2.exe */
buffer = malloc( strlen(tempfile) + strlen(p2) + 2 );
strcpy( buffer, tempfile );
strcat( buffer, " " );
strcat( buffer, p2 );
sd.Length = 32;
sd.Related = SSF_RELATED_CHILD;
sd.FgBg = SSF_FGBG_BACK;
sd.TraceOpt = SSF_TRACEOPT_NONE;
sd.PgmTitle = "WF/2 DOS Client";
sd.PgmName = doscl2;
sd.PgmInputs = buffer;
sd.TermQ = NULL;
sd.Environment = NULL;
sd.InheritOpt = SSF_INHERTOPT_PARENT;
sd.SessionType = SSF_TYPE_WINDOWEDVDM;
statusData.Length = 6;
statusData.SelectInd = 0;
statusData.SelectInd = 0;
if (!DosStartSession( &sd, &sess_id, &pid ))
Done = FALSE;
/* Wait for doscl2.exe to finish, keep reading tempfile at the same time */
while( !Done )
{
if ( DosSetSession( sess_id, &statusData ) )
Done = TRUE;
DosSleep(5000);
Done2 = ReadData( handle );
}
while ( !Done2 )
{
Done2 = ReadData( handle );
} /* endwhile */
DosClose( handle );
DosDelete( tempfile );
free( doscl2 );
free( buffer );
return(rc);
}
BOOL ReadData( HFILE handle )
{
BOOL ret = FALSE;
char * p1;
int dif;
char crc[2];
ULONG bytesread;
ULONG actual;
DosRead( handle, tmpbuf, BUFSIZE, &bytesread );
if ( bytesread )
{
if (p1=memchr( tmpbuf, EOFFLAG, bytesread ) )
{
dif = bytesread - (int)(p1-tmpbuf);
bytesread -= dif;
if (dif < 3)
{
DosRead( handle, crc+dif, 3-dif, &bytesread );
if (dif==1)
crc[0] = *(p1+1);
rc = *(unsigned int *)crc;
}
else
rc = *(unsigned int *)(p1+1);
ret = TRUE;
} /* endif */
DosWrite( 1, tmpbuf, bytesread, &actual );
}
return( ret );
}
/* This function will return a pointer to the command line string. */
static unsigned char *get_cmdline( void )
{
PTIB ptib;
PPIB ppib;
/* Get a pointer to the first through n arguments that were entered on */
/* the command line. */
DosGetInfoBlocks( &ptib, &ppib );
return (ppib->pib_pchcmd);
}
static void handler( int signum )
{
int rc;
rc = DosStopSession( STOP_SESSION_ALL, sess_id );
printf( "DosStopSession rc = %d\n", rc );
rc = DosClose( handle );
printf( "DosClose rc = %d\n", rc );
rc = DosDelete( tempfile );
printf( "DosDelete rc = %d\n", rc );
exit( EXIT_FAILURE );
signum = 0;
}