added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / c / shellcommands / Execute.c
blob4976d368d0a5cf2f03701b5544ef6deabe28e811
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 #include <aros/shcommands.h>
59 AROS_SH1(Execute, 41.1,
60 AROS_SHA(STRPTR, ,NAME,/A,NULL))
62 AROS_SHCOMMAND_INIT
64 BPTR from;
65 struct CommandLineInterface *cli = Cli();
67 if (!cli)
69 return RETURN_ERROR;
72 from = Open(SHArg(NAME), FMF_READ);
74 if (!from)
76 IPTR data[] = { (IPTR)SHArg(NAME) };
77 VFPrintf(Error(), "EXECUTE: can't open %s\n", data);
78 PrintFault(IoErr(), NULL);
80 return RETURN_FAIL;
83 if (!cli->cli_Interactive)
85 struct DateStamp ds;
86 BYTE tmpname[256];
87 BPTR tmpfile = NULL;
88 int count = 0;
90 DateStamp(&ds);
92 do {
93 count++;
94 __sprintf(tmpname, "T:Tmp%lu%lu%lu%lu%d",
95 ((struct Process *)FindTask(NULL))->pr_TaskNum,
96 ds.ds_Days, ds.ds_Minute, ds.ds_Tick, count);
97 tmpfile = Open(tmpname, MODE_NEWFILE);
98 } while (tmpfile == NULL && IoErr() == ERROR_OBJECT_IN_USE);
100 if (tmpfile)
102 LONG c;
104 while((c = FGetC(from)) != -1 && FPutC(tmpfile, c) != -1);
106 c = IoErr();
107 Close(from);
109 if (c)
111 FPuts(Error(),
112 "EXECUTE: error while creating temporary file\n");
113 PrintFault(c, NULL);
114 Close(tmpfile);
115 DeleteFile(tmpname);
117 return RETURN_FAIL;
120 c = '\n';
121 FPutC(tmpfile, c);
123 while((c = FGetC(cli->cli_CurrentInput)) != -1 && FPutC(tmpfile, c) != -1);
125 c = IoErr();
127 if (c)
129 FPuts(Error(), "EXECUTE: error while creating temporary file\n");
130 PrintFault(c, NULL);
131 Close(tmpfile);
132 DeleteFile(tmpname);
134 return RETURN_FAIL;
137 Close(cli->cli_CurrentInput);
138 if (AROS_BSTR_strlen(cli->cli_CommandFile))
139 DeleteFile(AROS_BSTR_ADDR(cli->cli_CommandFile));
142 LONG len = strlen(tmpname);
143 CopyMem(tmpname, AROS_BSTR_ADDR(cli->cli_CommandFile), len);
144 AROS_BSTR_setstrlen(cli->cli_CommandFile, len);
147 cli->cli_CurrentInput = tmpfile;
149 Seek(tmpfile, 0, OFFSET_BEGINNING);
151 else
154 we should try to open ":T", but since ":"
155 is not handled correctly yet, we just give up
157 LONG c = IoErr();
158 FPuts(Error(), "EXECUTE: error while creating temporary file\n");
159 PrintFault(c, NULL);
160 Close(from);
162 return RETURN_FAIL;
165 else
167 cli->cli_Interactive = FALSE;
168 cli->cli_CurrentInput = from;
171 return RETURN_OK;
173 AROS_SHCOMMAND_EXIT