Put pciehci.device in DEVS:USBHardware, like the other host-controller
[AROS.git] / workbench / c / shellcommands / Execute.c
blobc911f3e7cef93e7cbe9faa0b99861a23db1c86d2
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang:
7 */
9 /******************************************************************************
11 NAME
13 Execute <script> [{<arguments>}]
15 SYNOPSIS
17 FILE/A
19 LOCATION
23 FUNCTION
25 Executes a script with DOS commands.
27 INPUTS
29 FILE -- file to execute
31 RESULT
33 NOTES
35 EXAMPLE
37 BUGS
39 SEE ALSO
41 INTERNALS
43 HISTORY
45 ******************************************************************************/
47 #include <proto/exec.h>
48 #include <dos/dos.h>
49 #include <dos/bptr.h>
50 #include <dos/stdio.h>
51 #include <proto/dos.h>
52 #include <proto/alib.h>
53 #include <string.h>
55 #define DEBUG 0
56 #include <aros/debug.h>
58 #ifndef USE_EMBEDDED_COMMANDS
59 #define SH_GLOBAL_SYSBASE 1 /* For __sprintf */
60 #endif
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))
67 AROS_SHCOMMAND_INIT
69 struct CommandLineInterface *cli = Cli();
70 STRPTR arguments = SHArg(ARGUMENTS), s;
71 STRPTR extraargs = NULL;
72 BPTR from;
73 LONG len;
74 struct Process *me = (struct Process *)FindTask(NULL);
76 if (!cli)
77 return RETURN_ERROR;
79 if (! arguments)
80 arguments = "";
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);
93 if (extraargs) {
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);
107 if (!from)
109 IPTR data[] = { (IPTR)SHArg(NAME) };
110 VFPrintf(me->pr_CES, "EXECUTE: can't open %s\n", data);
111 PrintFault(IoErr(), NULL);
112 if (extraargs)
113 FreeVec(extraargs);
114 return RETURN_FAIL;
117 if (cli->cli_StandardInput == cli->cli_CurrentInput)
119 cli->cli_CurrentInput = from;
121 else
123 struct DateStamp ds;
124 BYTE tmpname[256];
125 BPTR tmpfile = BNULL;
126 int count = 0;
127 BYTE tmpdir[4];
128 BPTR tmplock;
129 struct Window *win;
130 struct Process *proc = (struct Process*)FindTask(0);
132 DateStamp(&ds);
134 win = proc->pr_WindowPtr;
135 proc->pr_WindowPtr = (struct Window *)-1;
136 tmplock = Lock("T:", SHARED_LOCK);
137 proc->pr_WindowPtr = win;
138 if (tmplock) {
139 strcpy(tmpdir, "T:");
140 UnLock(tmplock);
141 } else {
142 strcpy(tmpdir, ":T/");
145 do {
146 count++;
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);
153 if (tmpfile)
155 LONG c;
157 //if (FPuts(tmpfile, ".pushis\n") != -1)
158 while((c = FGetC(from)) != -1 && FPutC(tmpfile, c) != -1);
160 c = IoErr();
161 Close(from);
163 if (c)
165 FPuts(me->pr_CES,
166 "EXECUTE: error while creating temporary file\n");
167 PrintFault(c, NULL);
168 Close(tmpfile);
169 DeleteFile(tmpname);
170 if (extraargs)
171 FreeVec(extraargs);
173 return RETURN_FAIL;
176 c = '\n';
177 FPutC(tmpfile, c);
179 //FPuts(tmpfile, ".popis\n");
181 while((c = FGetC(cli->cli_CurrentInput)) != -1 && FPutC(tmpfile, c) != -1);
183 c = IoErr();
185 if (c)
187 FPuts(me->pr_CES, "EXECUTE: error while creating temporary file\n");
188 PrintFault(c, NULL);
189 Close(tmpfile);
190 DeleteFile(tmpname);
191 if (extraargs)
192 FreeVec(extraargs);
194 return RETURN_FAIL;
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);
210 else
213 we should try to open ":T", but since ":"
214 is not handled correctly yet, we just give up
216 LONG c = IoErr();
217 FPuts(me->pr_CES, "EXECUTE: error while creating temporary file\n");
218 PrintFault(c, NULL);
219 Close(from);
220 if (extraargs)
221 FreeVec(extraargs);
223 return RETURN_FAIL;
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;
235 TEXT *fh_buff;
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);
247 fh->fh_Pos = 0;
248 fh->fh_End = len + 1;
249 fh_buff = BADDR(fh->fh_Buf);
250 CopyMem(arguments, fh_buff, len);
251 fh_buff[len] = '\n';
252 /* Prevent RunCommand() from flushing cli_StandardInput */
253 SelectInput(BNULL);
254 } else {
255 VFPrintf(me->pr_CES, "EXECUTE: Can't inject command line\n", NULL);
256 return RETURN_FAIL;
260 return RETURN_OK;
262 AROS_SHCOMMAND_EXIT