Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / c / shellcommands / Execute.c
blobb720cb1e6f4a323039604119b345fe33499eac96
1 /*
2 Copyright © 1995-2007, 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
21 Sys:C
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/filesystem.h>
49 #include <dos/dos.h>
50 #include <dos/bptr.h>
51 #include <proto/dos.h>
52 #include <proto/alib.h>
53 #include <string.h>
55 #define SH_GLOBAL_SYSBASE 1
57 #define DEBUG 0
58 #include <aros/debug.h>
59 #include <aros/shcommands.h>
61 AROS_SH2(Execute, 41.1,
62 AROS_SHA(STRPTR, ,NAME , /A, NULL),
63 AROS_SHA(STRPTR, ,ARGUMENTS, /F, NULL))
65 AROS_SHCOMMAND_INIT
67 BPTR from;
68 struct CommandLineInterface *cli = Cli();
70 if (!cli)
72 return RETURN_ERROR;
75 from = Open(SHArg(NAME), FMF_READ);
77 if (!from)
79 IPTR data[] = { (IPTR)SHArg(NAME) };
80 VFPrintf(Error(), "EXECUTE: can't open %s\n", data);
81 PrintFault(IoErr(), NULL);
83 return RETURN_FAIL;
86 if (!cli->cli_Interactive)
88 struct DateStamp ds;
89 BYTE tmpname[256];
90 BPTR tmpfile = NULL;
91 int count = 0;
93 DateStamp(&ds);
95 do {
96 count++;
97 __sprintf(tmpname, "T:Tmp%lu%lu%lu%lu%d",
98 ((struct Process *)FindTask(NULL))->pr_TaskNum,
99 ds.ds_Days, ds.ds_Minute, ds.ds_Tick, count);
100 tmpfile = Open(tmpname, MODE_NEWFILE);
101 } while (tmpfile == NULL && IoErr() == ERROR_OBJECT_IN_USE);
103 if (tmpfile)
105 LONG c, len;
106 STRPTR arguments, s;
108 if (FPuts(tmpfile, ".pushis\n") != -1)
109 while((c = FGetC(from)) != -1 && FPutC(tmpfile, c) != -1);
111 c = IoErr();
112 Close(from);
114 if (c)
116 FPuts(Error(),
117 "EXECUTE: error while creating temporary file\n");
118 PrintFault(c, NULL);
119 Close(tmpfile);
120 DeleteFile(tmpname);
122 return RETURN_FAIL;
125 c = '\n';
126 FPutC(tmpfile, c);
128 FPuts(tmpfile, ".popis\n");
130 while((c = FGetC(cli->cli_CurrentInput)) != -1 && FPutC(tmpfile, c) != -1);
132 c = IoErr();
134 if (c)
136 FPuts(Error(), "EXECUTE: error while creating temporary file\n");
137 PrintFault(c, NULL);
138 Close(tmpfile);
139 DeleteFile(tmpname);
141 return RETURN_FAIL;
144 Close(cli->cli_CurrentInput);
145 if (AROS_BSTR_strlen(cli->cli_CommandFile))
146 DeleteFile(AROS_BSTR_ADDR(cli->cli_CommandFile));
149 LONG len = strlen(tmpname);
150 CopyMem(tmpname, AROS_BSTR_ADDR(cli->cli_CommandFile), len);
151 AROS_BSTR_setstrlen(cli->cli_CommandFile, len);
154 arguments = SHArg(ARGUMENTS);
155 if (arguments)
157 s = AROS_BSTR_ADDR(cli->cli_CommandName);
158 len = strlen(arguments);
160 AROS_BSTR_setstrlen(cli->cli_CommandName, len + 1);
161 CopyMem((APTR)arguments, s, len);
162 s[len] = '\n';
164 else
165 AROS_BSTR_setstrlen(cli->cli_CommandName, 0);
167 cli->cli_CurrentInput = tmpfile;
169 Seek(tmpfile, 0, OFFSET_BEGINNING);
171 else
174 we should try to open ":T", but since ":"
175 is not handled correctly yet, we just give up
177 LONG c = IoErr();
178 FPuts(Error(), "EXECUTE: error while creating temporary file\n");
179 PrintFault(c, NULL);
180 Close(from);
182 return RETURN_FAIL;
185 else
187 STRPTR arguments = SHArg(ARGUMENTS), s;
188 LONG len;
190 if (arguments)
192 kprintf("[Execute] args: %s\n", arguments);
193 s = AROS_BSTR_ADDR(cli->cli_CommandName);
194 len = strlen(arguments);
196 AROS_BSTR_setstrlen(cli->cli_CommandName, len + 1);
197 CopyMem((APTR)arguments, s, len);
198 s[len] = '\n';
200 else
201 AROS_BSTR_setstrlen(cli->cli_CommandName, 0);
203 cli->cli_Interactive = FALSE;
204 cli->cli_CurrentInput = from;
207 return RETURN_OK;
209 AROS_SHCOMMAND_EXIT