Splitted COMMAND/F to COMMAND/A and ARGUMENTS/F to allow commands with path like...
[AROS.git] / workbench / c / shellcommands / Run.c
blob6060cf2ab4af1e1ee5ee9c8a7f0ab62a5c1215d9
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc:
6 Lang: English
7 */
9 /******************************************************************************
11 NAME
13 Run
15 SYNOPSIS
17 EXECUTE/S,QUIET/S,COMMAND/F
19 LOCATION
21 Sys:C
23 FUNCTION
25 Run a program, that is start a program as a background process.
26 That means it doesn't take over the parent shell.
28 INPUTS
30 EXECUTE -- allows a script to be executed in the background
32 QUIET -- avoids printing of the background CLI's number
34 COMMAND -- the program/script to run
36 ARGUMENTS -- the arguments to send to the program/script
38 RESULT
40 NOTES
42 To make it possible to close the current shell, redirect the output
43 using
45 Run >NIL: program arguments
47 EXAMPLE
49 BUGS
51 SEE ALSO
53 INTERNALS
55 HISTORY
57 ******************************************************************************/
59 #include <exec/memory.h>
60 #include <proto/exec.h>
61 #include <dos/filesystem.h>
62 #include <dos/dosextens.h>
63 #include <dos/dostags.h>
64 #include <proto/dos.h>
65 #include <utility/tagitem.h>
66 #include <proto/alib.h>
68 #define DEBUG 1
69 #include <aros/debug.h>
71 #include <aros/shcommands.h>
73 AROS_SH4H(Run, 41.3, "Start a program as a background process",
74 AROS_SHAH(BOOL , ,EXECUTE ,/S,FALSE,"Allows a script to be run in background"),
75 AROS_SHAH(BOOL , ,QUIET ,/S,FALSE,"\tDon't print the background CLI's number"),
76 AROS_SHAH(STRPTR, ,COMMAND ,/A,NULL ,"The program/script to run"),
77 AROS_SHAH(STRPTR, ,ARGUMENTS,/F,NULL ,"Optional arguments for the program/script") )
79 AROS_SHCOMMAND_INIT
81 struct CommandLineInterface *cli = Cli();
82 BPTR cis = NULL, cos = NULL, ces = NULL;
83 LONG CliNum;
86 if (cli)
88 BPTR toclone, olddir;
90 if (IsInteractive(Input()))
91 toclone = Input();
92 else
93 toclone = cli->cli_StandardInput;
95 olddir = CurrentDir(toclone);
96 cis = Open("", FMF_READ);
97 CurrentDir(olddir);
99 if (IsInteractive(Output()))
100 toclone = Output();
101 else
102 toclone = cli->cli_StandardOutput;
104 olddir = CurrentDir(toclone);
105 cos = Open("", FMF_WRITE);
106 CurrentDir(olddir);
108 /* This is sort of a hack, needed because the original AmigaOS shell didn't allow
109 Error() redirection, so all the scripts written so far assume that only Input() and
110 Output() require to be redirected in order to not block the parent console */
111 if (Error() != cli->cli_StandardError && IsInteractive(Error()))
113 toclone = Error();
115 olddir = CurrentDir(toclone);
116 ces = Open("", FMF_WRITE);
117 CurrentDir(olddir);
121 struct DateStamp ds;
122 BYTE tmpname[256];
123 BPTR tmpfile = NULL;
124 int count = 0;
126 if ( (SHArg(EXECUTE)) && (SHArg(COMMAND)) )
128 DateStamp(&ds);
131 count++;
132 __sprintf(tmpname, "T:Tmp%lu%lu%lu%lu%d",
133 ((struct Process *)FindTask(NULL))->pr_TaskNum,
134 ds.ds_Days, ds.ds_Minute, ds.ds_Tick, count);
135 tmpfile = Open(tmpname, MODE_NEWFILE);
136 } while (tmpfile == NULL && IoErr() == ERROR_OBJECT_IN_USE);
138 if (tmpfile)
140 if ( (0 != FPuts(tmpfile, "Execute \"")) ||
141 (0 != FPuts(tmpfile, SHArg(COMMAND))) ||
142 (0 != FPuts(tmpfile, "\" ")) ||
143 (0 != FPuts(tmpfile, SHArg(ARGUMENTS))) ||
144 (0 != FPuts(tmpfile, "\nEndShell\n")) )
146 PrintFault(IoErr(), "Run");
147 Close(cis);
148 Close(cos);
149 Close(ces);
150 Close(tmpfile);
151 DeleteFile(tmpname);
153 return RETURN_FAIL;
155 Seek(tmpfile, 0, OFFSET_BEGINNING);
157 else
159 PrintFault(IoErr(), "Run");
160 Close(cis);
161 Close(cos);
162 Close(ces);
164 return RETURN_FAIL;
169 struct TagItem tags[] =
171 { SYS_ScriptInput, (IPTR)tmpfile },
172 { SYS_Input, (IPTR)cis },
173 { SYS_Output, (IPTR)cos },
174 { SYS_Error, (IPTR)ces },
175 { SYS_Background, TRUE },
176 { SYS_Asynch, TRUE },
177 { SYS_CliNumPtr, (IPTR)&CliNum },
178 { SYS_UserShell, TRUE },
179 { TAG_DONE, 0 }
182 if ( SystemTagList((SHArg(EXECUTE) && SHArg(COMMAND)) ?
183 (CONST_STRPTR) "" :
184 (CONST_STRPTR) SHArg(COMMAND) ,
185 tags ) == -1 )
187 PrintFault(IoErr(), "Run");
188 Close(cis);
189 Close(cos);
190 Close(ces);
191 if (tmpfile)
193 Close(tmpfile);
194 DeleteFile(tmpname);
197 return RETURN_FAIL;
201 if ( !(SHArg(QUIET)) )
203 IPTR data[1] = { (IPTR)CliNum };
204 VPrintf("[CLI %ld]\n", data);
206 #if DEBUG
207 #else
208 if ( SHArg(EXECUTE) && SHArg(COMMAND) )
209 while( (0 == DeleteFile(tmpname)) && (count++ < 10) )
210 Delay(10);
211 #endif
212 return RETURN_OK;
214 AROS_SHCOMMAND_EXIT