2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
9 /******************************************************************************
17 EXECUTE/S,QUIET/S,COMMAND/F
25 Run a program, that is start a program as a background process.
26 That means it doesn't take over the parent shell.
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
42 To make it possible to close the current shell, redirect the output
45 Run >NIL: program arguments
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>
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") )
81 struct CommandLineInterface
*cli
= Cli();
82 BPTR cis
= NULL
, cos
= NULL
, ces
= NULL
;
90 if (IsInteractive(Input()))
93 toclone
= cli
->cli_StandardInput
;
95 olddir
= CurrentDir(toclone
);
96 cis
= Open("", FMF_READ
);
99 if (IsInteractive(Output()))
102 toclone
= cli
->cli_StandardOutput
;
104 olddir
= CurrentDir(toclone
);
105 cos
= Open("", FMF_WRITE
);
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()))
115 olddir
= CurrentDir(toclone
);
116 ces
= Open("", FMF_WRITE
);
126 if ( (SHArg(EXECUTE
)) && (SHArg(COMMAND
)) )
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
);
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");
155 Seek(tmpfile
, 0, OFFSET_BEGINNING
);
159 PrintFault(IoErr(), "Run");
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
},
182 if ( SystemTagList((SHArg(EXECUTE
) && SHArg(COMMAND
)) ?
184 (CONST_STRPTR
) SHArg(COMMAND
) ,
187 PrintFault(IoErr(), "Run");
201 if ( !(SHArg(QUIET
)) )
203 IPTR data
[1] = { (IPTR
)CliNum
};
204 VPrintf("[CLI %ld]\n", data
);
208 if ( SHArg(EXECUTE
) && SHArg(COMMAND
) )
209 while( (0 == DeleteFile(tmpname
)) && (count
++ < 10) )