2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
9 /******************************************************************************
13 Execute <script> [{<arguments>}]
25 Executes a script with DOS commands.
29 FILE -- file to execute
45 ******************************************************************************/
47 #include <proto/exec.h>
50 #include <dos/stdio.h>
51 #include <proto/dos.h>
52 #include <proto/alib.h>
56 #include <aros/debug.h>
58 #ifndef USE_EMBEDDED_COMMANDS
59 #define SH_GLOBAL_SYSBASE 1 /* For __sprintf */
61 #include <aros/shcommands.h>
63 AROS_SH2(Execute
, 41.1,
64 AROS_SHA(STRPTR
, ,NAME
, /A
, NULL
),
65 AROS_SHA(STRPTR
, ,ARGUMENTS
, /F
, NULL
))
69 struct CommandLineInterface
*cli
= Cli();
70 STRPTR arguments
= SHArg(ARGUMENTS
), s
;
71 STRPTR extraargs
= NULL
;
74 struct Process
*me
= (struct Process
*)FindTask(NULL
);
82 /* See if we have extra arguments buffered in Input()
84 if (Input() != BNULL
) {
85 struct FileHandle
*fh
= BADDR(Input());
87 if (fh
->fh_Pos
> 0 && fh
->fh_End
> 0) {
88 LONG extraargsize
= fh
->fh_End
- fh
->fh_Pos
;
90 if (extraargsize
> 0) {
91 LONG argsize
= strlen(arguments
);
92 extraargs
= AllocVec(argsize
+ 1 + extraargsize
+ 1, MEMF_ANY
);
94 CopyMem(arguments
, extraargs
, argsize
);
95 extraargs
[argsize
++] = '\n';
96 CopyMem(BADDR(fh
->fh_Buf
)+fh
->fh_Pos
, &extraargs
[argsize
], extraargsize
);
97 argsize
+= extraargsize
;
98 extraargs
[argsize
++] = 0;
99 arguments
= extraargs
;
105 from
= Open(SHArg(NAME
), MODE_OLDFILE
);
109 IPTR data
[] = { (IPTR
)SHArg(NAME
) };
110 VFPrintf(me
->pr_CES
, "EXECUTE: can't open %s\n", data
);
111 PrintFault(IoErr(), NULL
);
117 if (cli
->cli_StandardInput
== cli
->cli_CurrentInput
)
119 cli
->cli_CurrentInput
= from
;
125 BPTR tmpfile
= BNULL
;
130 struct Process
*proc
= (struct Process
*)FindTask(0);
134 win
= proc
->pr_WindowPtr
;
135 proc
->pr_WindowPtr
= (struct Window
*)-1;
136 tmplock
= Lock("T:", SHARED_LOCK
);
137 proc
->pr_WindowPtr
= win
;
139 strcpy(tmpdir
, "T:");
142 strcpy(tmpdir
, ":T/");
147 __sprintf(tmpname
, "%sTmp%lu%lu%lu%lu%d", tmpdir
,
148 ((struct Process
*)FindTask(NULL
))->pr_TaskNum
,
149 ds
.ds_Days
, ds
.ds_Minute
, ds
.ds_Tick
, count
);
150 tmpfile
= Open(tmpname
, MODE_NEWFILE
);
151 } while (tmpfile
== BNULL
&& IoErr() == ERROR_OBJECT_IN_USE
);
157 //if (FPuts(tmpfile, ".pushis\n") != -1)
158 while((c
= FGetC(from
)) != -1 && FPutC(tmpfile
, c
) != -1);
166 "EXECUTE: error while creating temporary file\n");
179 //FPuts(tmpfile, ".popis\n");
181 while((c
= FGetC(cli
->cli_CurrentInput
)) != -1 && FPutC(tmpfile
, c
) != -1);
187 FPuts(me
->pr_CES
, "EXECUTE: error while creating temporary file\n");
197 Close(cli
->cli_CurrentInput
);
198 if (AROS_BSTR_strlen(cli
->cli_CommandFile
))
199 DeleteFile(AROS_BSTR_ADDR(cli
->cli_CommandFile
));
202 LONG len
= strlen(tmpname
);
203 CopyMem(tmpname
, AROS_BSTR_ADDR(cli
->cli_CommandFile
), len
);
204 AROS_BSTR_setstrlen(cli
->cli_CommandFile
, len
);
207 cli
->cli_CurrentInput
= tmpfile
;
208 Seek(tmpfile
, 0, OFFSET_BEGINNING
);
213 we should try to open ":T", but since ":"
214 is not handled correctly yet, we just give up
217 FPuts(me
->pr_CES
, "EXECUTE: error while creating temporary file\n");
227 /* Update cli_CommandName to be the name of the script */
228 len
= strlen(SHArg(NAME
));
229 s
= AROS_BSTR_ADDR(cli
->cli_CommandName
);
230 AROS_BSTR_setstrlen(cli
->cli_CommandName
, len
);
231 CopyMem(SHArg(NAME
), s
, len
);
233 if (arguments
&& strlen(arguments
)) {
234 struct FileHandle
*fh
;
237 len
= strlen(arguments
);
239 /* Inject the command args into cli->cli_StandardInput
241 * It would be nice to have a standard DOS LVO that
242 * could do this for us.
244 Flush(cli
->cli_StandardInput
);
245 if (SetVBuf(cli
->cli_StandardInput
, NULL
, BUF_LINE
, len
+ 1) == 0) {
246 fh
= BADDR(cli
->cli_StandardInput
);
248 fh
->fh_End
= len
+ 1;
249 fh_buff
= BADDR(fh
->fh_Buf
);
250 CopyMem(arguments
, fh_buff
, len
);
252 /* Prevent RunCommand() from flushing cli_StandardInput */
255 VFPrintf(me
->pr_CES
, "EXECUTE: Can't inject command line\n", NULL
);