home *** CD-ROM | disk | FTP | other *** search
- /*
- * CLIExe.c - Copyright © 1990 by S.R.
- *
- * Created: 25 May 1991 09:58:27
- * Modified: 28 Jan 1992 19:39:29
- *
- * M ake>> delete <file>.o
- * Make>> cc -qf -ps -wp -wd -wu -wr -so -sb -hi Aztec:Include/x16.dmp <file>.c
- * Make>> ln <file>.o -larpsc -lreq
- */
-
-
- /***** Same code that ParM.library, so same Struct ******/
- #include "ParMBase.h"
- #include "System2.0.h"
-
- struct ExecBase *SysBase;
- struct ArpBase *ArpBase;
- struct GfxBase *GfxBase;
- struct IntuitionBase *IntuitionBase;
- struct IconBase *IconBase;
- struct ReqLib *ReqBase;
- struct DosLibrary *DOSBase;
-
- struct WBStartup *WBenchMsg;
-
- char HelpUsage[] = "CLIExe V2.0 © 1991 by S.R.\nUsage: CLIExe <FULLCMDS> command1;command2;...\n\t[MODERUN] [SHELLCMD] newshell_command\n\t[WINDOW] con:a/b/c/d/title [STACK] stacksize\n\t[TMPDIR] directory [PRI] pri [NOIO]\n";
- char HelpArg[] = "FULLCMDS/A,MODERUN/s,SHELLCMD/k,WINDOW/k,STACK/k,TMPDIR/k,PRI/k,NOIO/s";
-
- long DosWrite(BPTR file, char *buffer, long length);
- #pragma amicall(DOSBase, 0x30, DosWrite(d1,d2,d3))
-
- extern void setmem(void *mem, size_t size, long value);
-
- /******* Came from ParM.library.c *********/
- /* Execute a CLI command as background or interactive shell */
-
- static void BuiltIn_ASyncExec(char *Cmd, long Stack, long Pri, BPTR InputFH, APTR ConsoleTask)
- {
- struct TagItem TagArray[] = {
- { SYS_Input, 0L },
- { SYS_Output, 0L },
- { SYS_Asynch, 1L },
- { NP_ConsoleTask, 0L },
- { NP_Priority, 0L },
- { NP_StackSize, 0L },
- { TAG_DONE, 0L }
- };
-
- TagArray[0].ti_Data = InputFH;
- TagArray[1].ti_Data = OpenFromLock(DupLockFromFH(InputFH), MODE_OLDFILE);
- TagArray[3].ti_Data = (ULONG)ConsoleTask;
- TagArray[4].ti_Data = Pri;
- TagArray[5].ti_Data = Stack;
- System(Cmd, TagArray);
- }
-
- void BuiltIn_Run(struct ParMConfig * PCfg, struct RunInfo * Command, BYTE Mode)
- {
- struct NewShell *NS;
- struct Process *pp;
- char *Window, *Cmd;
- BPTR fh;
- short i = 0, err;
- char FromFile[32], CmdBuf[128];
-
- if (!(NS = AllocMem(sizeof(struct NewShell), MEMF_PUBLIC | MEMF_CLEAR)))
- return;
- pp = (struct Process *) SysBase->ThisTask;
- NS->nsh_StackSize = Command->ri_Stack;
- NS->nsh_Pri = Command->ri_Pri;
- NS->nsh_Input = pp->pr_CIS;
- NS->nsh_Output = pp->pr_COS;
- NS->nsh_Control = BACKGROUND_SHELL;
-
- Cmd = Command->ri_Cmd;
- if (Mode == TOK_SHELL)
- {
- for (;;)
- {
- SPrintf(FromFile, "%sParMCmd%d", PCfg->TmpDir, i++);
- fh = Open(FromFile, MODE_NEWFILE);
- if (fh)
- break;
- else if ((err = IoErr()) != ERROR_OBJECT_IN_USE || i > 32)
- {
- if (ReqBase)
- SimpleRequest(PCfg->ReqTitle, "Unable to open script file\n");
- FreeMem(NS, sizeof(struct NewShell));
- return;
- }
- }
- FPrintf(fh, "%s\nEndCLI >NIL:\n", Cmd);
- Close(fh);
- if (!(Window = Command->ri_Window))
- Window = PCfg->ShellWindow;
- SPrintf(CmdBuf, "\"%s\" \"%s\" From %s", PCfg->ShellCmd, Window, FromFile);
- Cmd = CmdBuf;
- }
- /* 2.0 compatibility */
- if (IntuitionBase->LibNode.lib_Version >= 36)
- {
- BPTR DupOutput; /* to duplicate Output() file handle (closed on exit by System()) */
- DupOutput = OpenFromLock(DupLockFromFH(pp->pr_COS), MODE_OLDFILE);
- BuiltIn_ASyncExec(Cmd, Command->ri_Stack, Command->ri_Pri, DupOutput, pp->pr_ConsoleTask);
- }
- else
- ASyncRun(Cmd, NULL, (struct ProcessControlBlock *) NS);
-
- FreeMem(NS, sizeof(struct NewShell));
- }
-
- /*
- * Parse a line that may contain semicolons. Backslash ('\') is the override
- * char. This function is called from ParseConfig() and from Command().
- */
-
- void BuiltIn_ParseLine(char *cmd)
- {
- char *s, *d, c;
-
- s = d = cmd;
- while (c = *d++ = *s++)
- {
- if (c == '\\')
- *(d - 1) = *s++;
- else if (c == ';')
- *(d - 1) = '\n';
- }
- }
-
- /***** make (and allocate) a copy of the passed string *****/
-
- char *BuiltIn_CopyStr(char *str)
- {
- /*struct ParMBase *ParMBase;*/
- char *newstr;
-
- if (newstr = AllocMem(strlen(str) + 1, MEMF_PUBLIC))
- strcpy(newstr, str);
- return newstr;
- }
-
-
- void BuiltIn_FreeStr(char *str)
- {
- /*struct ParMBase *ParMBase;*/
-
- if (str)
- FreeMem(str, strlen(str) + 1);
- }
-
- /***** End of ParM.library.c import *****/
-
- void main(int argc, char **argv)
- {
- struct ParMConfig ParMConfig;
- struct RunInfo Command;
- long Stack = 0; /* Stack=0 mean use IconStack */
- BYTE Pri = 0, i;
- char Cmd[255];
- struct DiskObject *dop;
- char *ToolArg;
- BYTE Mode = TOK_SHELL;
-
- APTR OldWindowPtr;
- BPTR Nfh;
- struct Process *MainProcess;
- BOOL NoIO;
-
- setmem(&ParMConfig, sizeof(struct ParMConfig), 0);
- setmem(&Command, sizeof(struct RunInfo), 0);
-
- MainProcess = (struct Process *) SysBase->ThisTask;
-
- ParMConfig.ReqTitle = "CliExe";
- strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
-
- if (WBenchMsg)
- { /* Tool Types parsing */
- for (i = 1; i < WBenchMsg->sm_NumArgs; i++)
- {
- CurrentDir(WBenchMsg->sm_ArgList[i].wa_Lock); /* enter in the dir containing the Icon */
- if (dop = GetDiskObject(WBenchMsg->sm_ArgList[i].wa_Name))
- {
- ParMConfig.DefaultStack = dop->do_StackSize;
-
- if (ToolArg = FindToolType(dop->do_ToolTypes, "FULLCMD"))
- {
- strcpy(Cmd, ToolArg);
- BuiltIn_ParseLine(Cmd);
- Command.ri_Cmd = Cmd;
- }
- else
- {
- /* Error: no Cmd !! */
- FreeDiskObject(dop);
- if (ReqBase)
- SimpleRequest(ParMConfig.ReqTitle, "I can't find the CMD.");
- return;
- }
-
- if (ToolArg = FindToolType(dop->do_ToolTypes, "MODERUN"))
- {
- /* MODERUN so run mode is TOK_RUN */
- Mode = TOK_RUN;
- if (ToolArg = FindToolType(dop->do_ToolTypes, "PRI"))
- Command.ri_Pri = Atol(ToolArg);
- else
- Command.ri_Pri = 0;
- }
- else
- {
- Mode = TOK_SHELL;
- if (ToolArg = FindToolType(dop->do_ToolTypes, "SHELLCMD"))
- strcpy(ParMConfig.ShellCmd, ToolArg);
- else
- strcpy(ParMConfig.ShellCmd, DEFAULT_SHELL_CMD);
-
- if (ToolArg = FindToolType(dop->do_ToolTypes, "WINDOW"))
- Command.ri_Window = BuiltIn_CopyStr(ToolArg);
- else
- Command.ri_Window = BuiltIn_CopyStr(DEFAULT_SHELL_WINDOW);
-
- if (ToolArg = FindToolType(dop->do_ToolTypes, "STACK"))
- Command.ri_Stack = Atol(ToolArg);
- else
- Command.ri_Stack = 4096;
-
- Command.ri_Pri = 0; /* in SHELL mode pri can't be something else */
-
- if (ToolArg = FindToolType(dop->do_ToolTypes, "TMPDIR"))
- strcpy(ParMConfig.TmpDir, ToolArg);
- else
- strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
- }
- if (ToolArg = FindToolType(dop->do_ToolTypes, "NOIO"))
- NoIO = TRUE;
- else
- NoIO = FALSE;
-
- FreeDiskObject(dop);
-
- }
- else
- {
- /* no icon: I can't do anything ! */
- if (ReqBase)
- SimpleRequest(ParMConfig.ReqTitle, "I can't find the icon:\"%s\".", WBenchMsg->sm_ArgList[i].wa_Name);
- return;
- }
- }
- }
- else
- { /* CLI parsing */
-
- if (!argc)
- {
- Printf("%s", HelpUsage);
- return;
- }
-
- /* record line */
- strcpy(Cmd, argv[0]);
- BuiltIn_ParseLine(Cmd);
- Command.ri_Cmd = Cmd;
-
- /* test PRI */
- if (argv[6])
- Command.ri_Pri = Atol( argv[6]);
- else
- Command.ri_Pri = 0;
-
- if (argv[7])
- NoIO = TRUE;
- else
- NoIO = FALSE;
-
- /* test run mode */
- if (argv[1])
- {
- /* MODERUN so run mode is TOK_RUN */
- Mode = TOK_RUN;
- }
- else
- {
- Mode = TOK_SHELL;
-
- if (argv[2])
- strcpy(ParMConfig.ShellCmd, argv[2]);
- else
- strcpy(ParMConfig.ShellCmd, DEFAULT_SHELL_CMD);
-
- if (argv[3])
- Command.ri_Window = BuiltIn_CopyStr(argv[3]);
- else
- Command.ri_Window = BuiltIn_CopyStr(DEFAULT_SHELL_WINDOW);
-
- if (argv[4])
- Command.ri_Stack = Atol(argv[4]);
- else
- Command.ri_Stack = 4096;
-
- if (argv[5])
- strcpy(ParMConfig.TmpDir, argv[5]);
- else
- strcpy(ParMConfig.TmpDir, DEFAULT_TMP_DIR);
-
- }
-
-
- }
-
- if ( NoIO)
- {
- /* this is for redirection problem in RUN mode */
- OldWindowPtr = MainProcess->pr_WindowPtr;
- MainProcess->pr_WindowPtr = (APTR) - 1; /* Prevent request if NULL: is not mounted */
- if ((Nfh = Open("NULL:", MODE_NEWFILE)) || (Nfh = Open("NIL:", MODE_NEWFILE)))
- {
- MainProcess->pr_CIS = Nfh;
- MainProcess->pr_COS = Nfh;
- MainProcess->pr_ConsoleTask = (APTR) ((struct FileHandle *) (Nfh << 2))->fh_Type;
- }
- MainProcess->pr_WindowPtr = OldWindowPtr;
- }
-
- BuiltIn_Run(&ParMConfig, &Command, Mode);
- BuiltIn_FreeStr(Command.ri_Window);
-
- }
-
-
- /**** Start *********************************/
- void exit(int code)
- {
- if (WBenchMsg)
- {
- Forbid();
- ReplyMsg((struct Message *) WBenchMsg);
- }
- CloseLibrary(ArpBase);
- Exit(code);
- }
-
-
- void _main(long alen, char *aptr)
- {
- register struct Process *pp;
- int _argc;
- char **_argv;
-
- pp = (struct Process *) SysBase->ThisTask;
- if (!pp->pr_CLI)
- {
- WaitPort(&pp->pr_MsgPort);
- WBenchMsg = (struct WBStartup *) GetMsg(&pp->pr_MsgPort);
- }
- if (!(ArpBase = (struct ArpBase *) OpenLibrary("arp.library", 39L)))
- {
- if (pp->pr_CLI && (DOSBase = OpenLibrary("dos.library", 0L)))
- {
- DosWrite(pp->pr_COS, "You need arp.library V39+\n", 26L);
- DosWrite(pp->pr_COS, "You need Req.library V1.22+\n", 27L);
- CloseLibrary(DOSBase);
- }
- return;
- }
- DOSBase = (struct DosLibrary *) ArpBase->DosBase;
- IntuitionBase = (struct IntuitionBase *) ArpBase->IntuiBase;
- GfxBase = (struct GfxBase *) ArpBase->GfxBase;
-
- ReqBase = (struct ReqLib *) ArpOpenLibrary("req.library", 1L);
- IconBase = (struct IconBase *) ArpOpenLibrary("icon.library", 1L);
- if (!(IconBase = (struct IconBase *) ArpOpenLibrary("icon.library", 1L)))
- {
- if (pp->pr_CLI && DOSBase)
- {
- DosWrite(pp->pr_COS, "You need icon.library V1+ \n", 27L);
- }
- return;
- }
-
-
- if (WBenchMsg)
- {
- CurrentDir(WBenchMsg->sm_ArgList->wa_Lock);
- _argv = (char **) WBenchMsg;
- _argc = 0;
- }
- else
- {
- /*
- * WARNING: With that function, argv[] array will not start with the program
- * name but with it's first argument. If there's no args, argc will be zero.
- */
- _argv = ArpAlloc(4 * 7);
- _argc = (int) GADS(aptr, alen, HelpUsage, _argv, HelpArg);
- if (_argc < 0)
- {
- Puts(_argv[0]);
- exit(20);
- }
- }
-
-
- main(_argc, _argv);
-
- exit(0);
- }
-