#include <exec/types.h>
#include <libraries/dos.h>
#include <Assembly/tb.h>

LONG SysBase;
LONG DOSBase;

/*

   CCLI --

      This routine handles CLI-type commands from True BASIC.  The True
   BASIC routine CLI is the one you should call; this routine does the
   work, but CLI checks the return and signals an error when appropriate.

      We are called as a function, def CCLI(s$), which returns zero for
   failure and non-zero for success.

*/

#define STRINGLENGTH 512 /* max length of our string */

void CCLI(arg,uv)

   struct String ***arg; /* pointer to parameters */
   struct TBUserVector *uv; /* pointer to user vector */

  {struct TBString *string;
   struct TBInt *result;
   long inhandle, outhandle;
   char command[STRINGLENGTH];

   SysBase = uv->SysBase; /* copy library pointers */
   DOSBase = uv->DOSBase;

   string = (struct TBString *) **arg--; /* point to string */
   result = (struct TBInt *) *arg; /* point to result number */
   result->exponent = -1; /* set integer flag */
   result->integer = 0; /* assume failure */

   if (string->length == 0) /* zero-length string? */
     {inhandle = Open("CON:100/50/200/100/True BASIC CLI",MODE_NEWFILE); /* open a console window */
      if (inhandle==0) return; /* return on error */
      outhandle = 0;}
   else /* there is a command string */
     {outhandle = Open("nil:",MODE_NEWFILE);
      if (outhandle==0) return;
      inhandle = 0;
      movmem(string->text,command,string->length);} /* copy string */

   command[string->length] = 0; /* null-terminate command string */
   result->integer = Execute(command,inhandle,outhandle);
   if (inhandle) Close(inhandle); /* close input file if any */
   if (outhandle) Close(outhandle);} /* close output file */
