forked from adesutherland/CMS-370-BREXX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.c
405 lines (350 loc) · 10.5 KB
/
address.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* Modified for VM/370 CMS and GCC by Robert O'Hara, July 2010. */
/*
* $Id: address.c,v 1.15 2009/06/02 09:41:27 bnv Exp $
* $Log: address.c,v $
* Revision 1.15 2009/06/02 09:41:27 bnv
* MVS/CMS corrections
*
* Revision 1.14 2008/07/15 07:40:25 bnv
* #include changed from <> to ""
*
* Revision 1.13 2008/07/14 13:08:42 bnv
* MVS,CMS support
*
* Revision 1.12 2006/01/26 10:24:27 bnv
* Spaces...
*
* Revision 1.11 2004/08/16 15:27:37 bnv
* Include fcntl added
*
* Revision 1.9 2003/11/04 09:48:00 bnv
* Corrected for the mkstemp
*
* Revision 1.8 2003/10/30 13:14:55 bnv
* Some corrections
*
* Revision 1.7 2002/06/11 12:37:38 bnv
* Added: CDECL
*
* Revision 1.6 2002/06/06 08:23:46 bnv
* Changed: mkstemp to MKTEMP
*
* Revision 1.5 2001/11/29 13:12:52 bnv
* Corrected: The error handling when trace is off
*
* Revision 1.4 2001/06/25 18:51:48 bnv
* Header -> Id
*
* Revision 1.3 1999/11/26 13:13:02 bnv
* Added: Windows CE support.
*
* Revision 1.2 1999/03/10 16:53:32 bnv
* Added MSC support
*
* Revision 1.1 1998/07/02 17:34:50 bnv
* Initial revision
*
*/
#include <string.h>
#include <stdlib.h>
#ifdef __CMS__
#include <cmssys.h>
#endif
#include "lstring.h"
#include "rexx.h"
#include "trace.h"
#include "stack.h"
#include "compile.h"
#include "interpre.h"
#ifndef WIN
#if defined(MSDOS) || defined(__WIN32__)
# include <io.h>
# include <fcntl.h>
#ifndef _MSC_VER
# include <dir.h>
#endif
# include <process.h>
# if defined(__BORLANDC__) && !defined(__WIN32__)
# include <systemx.h>
# endif
#elif defined(__MPW__)
#elif defined(_MSC_VER)
#else
# if !defined(__CMS__) && !defined(__MVS__)
# include <fcntl.h>
# include <unistd.h>
# endif
#endif
#if !defined(__CMS__) && !defined(__MVS__)
# include <sys/stat.h>
#endif
#include <string.h>
#ifndef S_IREAD
# define S_IREAD 0
# define S_IWRITE 1
#endif
#define NOSTACK 0
#define FIFO 1
#define LIFO 2
#define STACK 3
#define LOW_STDIN 0
#define LOW_STDOUT 1
#ifndef __CMS__
/* ---------------------- chkcmd4stack ---------------------- */
static void
chkcmd4stack(PLstr cmd, int *in, int *out) {
Lstr Ucmd;
*in = *out = 0;
if (LLEN(*cmd) < 7) return;
LINITSTR(Ucmd);
/* Search for string "STACK>" in front of command
or for strings "(STACK", "(FIFO", "(LIFO"
">STACK", ">FIFO", ">LIFO" at the end */
if (LLEN(*cmd) <= 5) return;
Lstrcpy(&Ucmd, cmd);
Lupper(&Ucmd);
if (!MEMCMP(LSTR(Ucmd), "STACK>", 6)) *in = FIFO;
if (!MEMCMP(LSTR(Ucmd) + LLEN(Ucmd) - 5, "STACK", 5)) *out = STACK;
if (!MEMCMP(LSTR(Ucmd) + LLEN(Ucmd) - 4, "FIFO", 4)) *out = FIFO;
if (!MEMCMP(LSTR(Ucmd) + LLEN(Ucmd) - 4, "LIFO", 4)) *out = LIFO;
if (*out)
if (LSTR(Ucmd)[LLEN(Ucmd) - ((*out == STACK) ? 6 : 5)] != '(' &&
LSTR(Ucmd)[LLEN(Ucmd) - ((*out == STACK) ? 6 : 5)] != '>')
*out = 0;
LFREESTR(Ucmd);
if (*in) {
MEMMOVE(LSTR(*cmd), LSTR(*cmd) + 6, LLEN(*cmd) - 6);
LLEN(*cmd) -= 6;
}
if (*out)
LLEN(*cmd) -= (*out == STACK) ? 6 : 5;
if (*out == STACK)
*out = FIFO;
} /* chkcmd4stack */
#endif
#if !defined(__CMS__) && !defined(__MVS__)
/* -------------------- mkfntemp -------------------- */
static void
mkfntemp(char *fn, size_t length) {
int l;
char *c;
fn[0] = '\0';
c = getenv("TEMP");
if (c) STRCPY(fn, c);
l = STRLEN(fn);
if (l)
if (fn[l - 1] != FILESEP) {
fn[l] = FILESEP;
fn[l + 1] = '\0';
}
STRCAT(fn, "OXXXXXX");
MKTEMP(fn);
} /* mkfntemp */
/* ------------------ RxRedirectCmd ----------------- */
int __CDECL
RxRedirectCmd(PLstr cmd, int in, int out, PLstr outputstr) {
char fnin[250], fnout[250];
int old_stdin = 0, old_stdout = 0;
int filein, fileout;
FILE *f;
PLstr str;
Context *context = (Context *) CMSGetPG();
/* --- redirect input --- */
if (in) {
mkfntemp(fnin, sizeof(fnin));
if ((f = fopen(fnin, "w")) != NULL) {
while (StackQueued() > 0) {
str = PullFromStack();
L2STR(str);
LASCIIZ(*str);
fputs(LSTR(*str), f);
fputc('\n', f);
LPFREE(str);
}
fclose(f);
old_stdin = dup(LOW_STDIN);
filein = open(fnin, S_IREAD);
dup2(filein, LOW_STDIN);
close(filein);
} else
in = FALSE;
}
/* --- redirect output --- */
if (out) {
mkfntemp(fnout, sizeof(fnout));
old_stdout = dup(LOW_STDOUT);
fileout = open(fnout, O_WRONLY | O_CREAT, 0600);
dup2(fileout, LOW_STDOUT);
close(fileout);
}
/* --- Execute the command --- */
LASCIIZ(*cmd);
#if defined(__BORLANDC__) && !defined(__WIN32__)
(context->rexxrxReturnCode) = systemx(LSTR(*cmd));
#else
(context->rexxrxReturnCode) = system(LSTR(*cmd));
#endif
/* --- restore input --- */
if (in) {
close(LOW_STDIN);
dup2(old_stdin, LOW_STDIN);
close(old_stdin);
remove(fnin);
}
/* --- restore output --- */
if (out) {
close(LOW_STDOUT);
dup2(old_stdout, LOW_STDOUT); /* restore stdout */
close(old_stdout);
#ifndef MSDOS
# if !defined(__CMS__) && !defined(__MVS__)
chmod(fnout, 0666);
# endif
#endif
if ((f = fopen(fnout, "r")) != NULL) {
if (outputstr) {
Lread(f, outputstr, LREADFILE);
#ifdef RMLAST
if (LSTR(*outputstr)[LLEN(*outputstr) - 1] == '\n')
LLEN(*outputstr)--;
#endif
} else /* push it to stack */
while (!feof(f)) {
LPMALLOC(str);
Lread(f, str, LREADLINE);
if (LLEN(*str) == 0 && feof(f)) {
LPFREE(str);
break;
}
if (out == FIFO)
Queue2Stack(str);
else
Push2Stack(str);
}
fclose(f);
remove(fnout);
}
}
return (context->rexxrxReturnCode);
} /* RxRedirectCmd */
#endif
/* ------------------ RxExecuteCmd ----------------- */
int __CDECL
RxExecuteCmd(PLstr cmd, PLstr env) {
Context *context = (Context *) CMSGetPG();
#if defined(__CMS__)
int how;
LASCIIZ(*env);
/*
if (!Lcmp(env, "CMS")) how = CMS_CONSOLE;
else how = CMS_COMMAND;
(context->rexxrxReturnCode) = CMScommand(LSTR(* cmd), how); // execute the command
*/
(context->rexxrxReturnCode) = __HOSTCM(cmd, env);
RxSetSpecialVar(RCVAR,
(context->rexxrxReturnCode)); // set the returncode variable
if (((context->rexxrxReturnCode) != 0) &&
!((context->rexx_proc)[(context->rexx_rx_proc)].trace & off_trace)) {
// do the right thing for tracing
if ( ((context->rexxrxReturnCode < 0) &&
(context->rexx_proc[context->rexx_rx_proc].trace &
(failure_trace | normal_trace)
)) |
((context->rexxrxReturnCode > 0) &&
(context->rexx_proc[context->rexx_rx_proc].trace & error_trace)
) ) {
TraceCurline(NULL, TRUE);
fprintf(STDERR, " +++ RC(%d) +++\n",
(context->rexxrxReturnCode));
if ((context->rexx_proc)[(context->rexx_rx_proc)].interactive_trace)
TraceInteractive(FALSE);
}
}
if (((context->rexxrxReturnCode) < 0) &&
((context->rexx_proc)[(context->rexx_rx_proc)].condition & SC_FAILURE))
RxSignalCondition(SC_FAILURE, LSTR(* cmd));
if (((context->rexxrxReturnCode) != 0) &&
((context->rexx_proc)[(context->rexx_rx_proc)].condition & SC_ERROR))
RxSignalCondition(SC_ERROR, LSTR(* cmd));
#elif defined(__MVS__)
(context->rexxrxReturnCode) = system(LSTR(* cmd));
#else
#ifndef WIN
int in, out;
Lstr cmdN;
LINITSTR(cmdN);
Lfx(&cmdN, 1);
Lstrcpy(&cmdN, cmd);
L2STR(&cmdN);
LASCIIZ(cmdN);
if (env == NULL) {
chkcmd4stack(&cmdN, &in, &out);
(context->rexxrxReturnCode) = RxRedirectCmd(&cmdN, in, out, FALSE);
} else if (!Lcmp(env, "COMMAND") ||
!Lcmp(env, "DOS") ||
!Lcmp(env, "CMS") ||
!Lstrcmp(env, &((context->rexxsystemStr)->key))) {
chkcmd4stack(&cmdN, &in, &out);
(context->rexxrxReturnCode) = RxRedirectCmd(&cmdN, in, out, FALSE);
}
#if defined(__BORLANDC__) && !defined(__WIN32__)
else
if (!Lcmp(env,"INT2E"))
int2e(LSTR(cmdN));
#endif
else if (!Lcmp(env, "EXEC")); /*execl(...); */
else
(context->rexxrxReturnCode) = -3;
/* free string */
LFREESTR(cmdN);
RxSetSpecialVar(RCVAR, (context->rexxrxReturnCode));
if ((context->rexxrxReturnCode) &&
!((context->rexx_proc)[(context->rexx_rx_proc)].trace & off_trace)) {
if ((context->rexx_proc)[(context->rexx_rx_proc)].trace &
(error_trace | failure_trace | normal_trace)) {
TraceCurline(NULL, TRUE);
fprintf(STDERR, " +++ RC(%d) +++\n",
(context->rexxrxReturnCode));
if ((context->rexx_proc)[(context->rexx_rx_proc)].interactive_trace)
TraceInteractive(FALSE);
}
if ((context->rexx_proc)[(context->rexx_rx_proc)].condition & SC_ERROR)
RxSignalCondition(SC_ERROR, cmd);
}
#else
size_t len;
char *ch;
Lstr file, args;
TCHAR *uFile, *uArgs;
PROCESS_INFORMATION p;
LINITSTR(file);
LINITSTR(args); Lfx(&args,1);
ch = LSTR(*cmd);
if ((*ch=='\'') || (*ch=='\"')) {
ch = STRCHR(ch+1,*ch);
if (ch)
len = (DWORD)ch - (DWORD)LSTR(*cmd);
else
len = LLEN(*cmd);
Lsubstr(&file, cmd, 2, len-1, ' ');
Lsubstr(&args, cmd, len+3, LREST, ' ');
} else {
Lword(&file, cmd, 1);
Lsubword(&args, cmd, 2, LREST);
}
uFile = (TCHAR*)MALLOC(sizeof(TCHAR)*LLEN(file)+2,NULL);
uArgs = (TCHAR*)MALLOC(sizeof(TCHAR)*LLEN(args)+2,NULL);
mbstowcs(uFile,LSTR(file),LLEN(file)); uFile[LLEN(file)] = (TCHAR)0;
mbstowcs(uArgs,LSTR(args),LLEN(args)); uArgs[LLEN(args)] = (TCHAR)0;
CreateProcess(uFile, uArgs, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &p);
CloseHandle(p.hProcess);
CloseHandle(p.hThread);
FREE(uFile);
FREE(uArgs);
LFREESTR(file);
LFREESTR(args);
#endif
#endif
return (context->rexxrxReturnCode);
} /* RxExecuteCmd */
#endif