4 * Revision 1.2 2001/04/05 16:58:26 stegerg
5 * AROS does not have system() and chmod() functions, so for now #if 0 them out
7 * Revision 1.1 2001/04/04 05:43:38 wang
8 * First commit: compiles on Linux, Amiga, Windows, Windows CE, generic gcc
10 * Revision 1.3 1999/11/26 13:13:02 bnv
11 * Added: Windows CE support.
13 * Revision 1.2 1999/03/10 16:53:32 bnv
16 * Revision 1.1 1998/07/02 17:34:50 bnv
33 #if defined(MSDOS) || defined(__WIN32__)
39 # if defined(__BORLANDC__) && !defined(__WIN32__)
42 #elif defined(__MPW__)
43 #elif defined(_MSC_VER)
65 /* ---------------------- chkcmd4stack ---------------------- */
67 chkcmd4stack(PLstr cmd
, int *in
, int *out
)
72 if (LLEN(*cmd
)<7) return;
76 /* Search for string "STACK>" in front of command
77 or for strings "(STACK", "(FIFO", "(LIFO"
78 ">STACK", ">FIFO", ">LIFO" at the end */
80 if (LLEN(*cmd
)<=5) return;
82 Lstrcpy(&Ucmd
,cmd
); Lupper(&Ucmd
);
84 if (!MEMCMP(LSTR(Ucmd
),"STACK>",6)) *in
=FIFO
;
85 if (!MEMCMP(LSTR(Ucmd
)+LLEN(Ucmd
)-5,"STACK",5)) *out
= STACK
;
86 if (!MEMCMP(LSTR(Ucmd
)+LLEN(Ucmd
)-4,"FIFO",4)) *out
= FIFO
;
87 if (!MEMCMP(LSTR(Ucmd
)+LLEN(Ucmd
)-4,"LIFO",4)) *out
= LIFO
;
89 if (LSTR(Ucmd
)[LLEN(Ucmd
)-((*out
==STACK
)?6:5)]!='(' &&
90 LSTR(Ucmd
)[LLEN(Ucmd
)-((*out
==STACK
)?6:5)]!='>') *out
= 0;
94 MEMMOVE(LSTR(*cmd
),LSTR(*cmd
)+6,LLEN(*cmd
)-6);
98 LLEN(*cmd
) -= (*out
==STACK
)?6:5;
104 /* ------------------ RxRedirectCmd ----------------- */
106 RxRedirectCmd(PLstr cmd
, int in
, int out
, PLstr resultstr
)
108 char fnin
[250], fnout
[250];
109 int old_stdin
, old_stdout
;
116 /* --- redirect input --- */
118 fnin
[0] = '\0'; c
= getenv("TEMP");
119 if (c
) STRCPY(fnin
,c
);
122 if (fnin
[l
-1] != FILESEP
) {
126 STRCAT(fnin
,"OXXXXXX");
129 if ((f
=fopen(fnin
,"w"))!=NULL
) {
130 while (StackQueued()>0) {
131 str
= PullFromStack();
132 L2STR(str
); LASCIIZ(*str
);
133 fputs(LSTR(*str
),f
); fputc('\n',f
);
138 old_stdin
= dup(LOW_STDIN
);
139 filein
= open(fnin
,S_IREAD
);
140 dup2(filein
,LOW_STDIN
);
146 /* --- redirect output --- */
148 fnout
[0] = '\0'; c
= getenv("TEMP");
149 if (c
) STRCPY(fnout
,c
);
152 if (fnout
[l
-1] != FILESEP
) {
156 STRCAT(fnout
,"OXXXXXX");
158 old_stdout
= dup(LOW_STDOUT
);
159 fileout
= creat(fnout
,S_IWRITE
);
160 dup2(fileout
,LOW_STDOUT
);
164 /* --- Execute the command --- */
166 #if defined(__BORLANDC__) && !defined(__WIN32__)
167 RxReturnCode
= systemx(LSTR(*cmd
));
169 RxReturnCode
= system(LSTR(*cmd
));
172 /* --- restore input --- */
175 dup2(old_stdin
,LOW_STDIN
);
180 /* --- restore output --- */
183 dup2(old_stdout
,LOW_STDOUT
); /* restore stdout */
185 #if !defined(MSDOS) && !defined(AROS)
188 if ((f
=fopen(fnout
,"r"))!=NULL
) {
190 Lread(f
,resultstr
,LREADFILE
);
192 if (LSTR(*resultstr
)[LLEN(*resultstr
)-1]=='\n')
195 } else /* push it to stack */
198 Lread(f
,str
,LREADLINE
);
199 if (LLEN(*str
)==0 && feof(f
)) {
215 } /* RxRedirectCmd */
218 /* ------------------ RxExecuteCmd ----------------- */
220 RxExecuteCmd( PLstr cmd
, PLstr env
)
226 LINITSTR(cmdN
); Lfx(&cmdN
,1);
232 chkcmd4stack(&cmdN
,&in
,&out
);
233 RxReturnCode
= RxRedirectCmd(&cmdN
,in
,out
,FALSE
);
235 if ( !Lcmp(env
,"COMMAND") ||
238 !Lstrcmp(env
,&(SystemStr
->key
))) {
239 chkcmd4stack(&cmdN
,&in
,&out
);
240 RxReturnCode
= RxRedirectCmd(&cmdN
,in
,out
,FALSE
);
242 #if defined(__BORLANDC__) && !defined(__WIN32__)
244 if (!Lcmp(env
,"INT2E"))
248 if (!Lcmp(env
,"EXEC")) ; /*execl(...); */
255 RxSetSpecialVar(RCVAR
,RxReturnCode
);
257 if (_Proc
[_rx_proc
].trace
& (error_trace
| normal_trace
)) {
258 TraceCurline(NULL
,TRUE
);
259 fprintf(STDERR
," +++ RC(%d) +++\n",RxReturnCode
);
260 if (_Proc
[_rx_proc
].interactive_trace
)
261 TraceInteractive(FALSE
);
263 if (_Proc
[_rx_proc
].condition
& SC_ERROR
)
264 RxSignalCondition(SC_ERROR
);
270 TCHAR
*uFile
, *uArgs
;
271 PROCESS_INFORMATION p
;
277 if ((*ch
=='\'') || (*ch
=='\"')) {
278 ch
= STRCHR(ch
+1,*ch
);
280 len
= (DWORD
)ch
- (DWORD
)LSTR(*cmd
);
283 Lsubstr(&file
, cmd
, 2, len
-1, ' ');
284 Lsubstr(&args
, cmd
, len
+3, LREST
, ' ');
286 Lword(&file
, cmd
, 1);
287 Lsubword(&args
, cmd
, 2, LREST
);
290 uFile
= (TCHAR
*)MALLOC(sizeof(TCHAR
)*LLEN(file
)+2,NULL
);
291 uArgs
= (TCHAR
*)MALLOC(sizeof(TCHAR
)*LLEN(args
)+2,NULL
);
292 mbstowcs(uFile
,LSTR(file
),LLEN(file
)); uFile
[LLEN(file
)] = (TCHAR
)0;
293 mbstowcs(uArgs
,LSTR(args
),LLEN(args
)); uArgs
[LLEN(args
)] = (TCHAR
)0;
295 CreateProcess(uFile
, uArgs
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, NULL
, &p
);
296 CloseHandle(p
.hProcess
);
297 CloseHandle(p
.hThread
);