
/* plug.c */

/********************************************************************/
/*                                                                  */
/* v2.0 - text output to WorkBench via open on "*" implemented      */
/*                                                                  */
/* This program is placed in the public domain, upon one condition: */
/* that the company and author information be retained upon any     */
/* copies that are redistributed. For your personal use, this isn't */
/* required. When this source is used for profit, the company name  */
/* and author data MUST be retained. This includes use in articles  */
/* in magazines, on ARexx release disks, or internally in a product */
/* for public consumption.                                          */
/*                                                                  */
/* Software produced by: SoftCircuits, Inc.                         */
/* Software written by:  Ben Blish, for SoftCircuits, Inc.          */
/*                                                                  */
/********************************************************************
 *
 * plug is a program designed to allow you to create ARexx scripts
 * that can be run under the workbench by using project icons. You
 * simply make a project-type icon for your script. For instance, if
 * your ARexx script is called RouteMyPCB, then you would create an
 * icon called RouteMyPCB.info, that is of the type project. In the
 * default tool slot in the INFO window, you place 'plug'. Since most
 * ARexx tools (rx, etc) are kept in the c: directory, we suggest
 * you place plug there too... this means that each of these icons
 * should have 'c:plug' in the default tool field instead. This is
 * up to you, however. plug will also launch ARexx scripts from the
 * CLI, and so can be used INSTEAD of rx... you could also rename
 * it rx, which would be a little easier to use. Again, it's up to you.
 * plug does NOT currently support multiple arguments either under
 * WorkBench or CLI. This is not only possible, but easy - it's
 * left as an exercise for the hackers out there. This is meant to be
 * an example of how arexx tasks can be initiated from c
 *
 * This is Lattice 'c' source - written for v4.0 and above. It may
 * work fine with previous versions, then again, maybe not. In any case,
 * the examples hold true for any language.
 *
 * plug is linked with c.o, rexxglue.o, Amiga.lib, and the lc libraries.
 * no real attempt has been made to shrink it - it's only 6k, so what
 * the heck, you know? If you want to trim it, have a ball.
 *
 * Ben Blish, May 6th, 1988
 *
 ********************************************************************/

#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <stdio.h>
#include <fcntl.h>
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <exec/tasks.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>
#include <graphics/gels.h>
#include <graphics/copper.h>
#include <graphics/display.h>
#include <graphics/regions.h>
#include <devices/input.h>
#include <exec/devices.h>
#include <devices/inputevent.h>
#include <devices/keymap.h>
#include <graphics/gfxbase.h>
#include <graphics/text.h>
#include <graphics/view.h>
#include <hardware/blit.h>
#include <graphics/sprite.h>
#include <intuition/intuitionbase.h>
#include <workbench/startup.h>
#include <rexx/storage.h>
#include <rexx/rxslib.h>

extern struct WBStartup *WBenchMsg;
struct RexxLib *RexxSysBase;

void main(argc,argv)
  int argc;
  char *argv[];
  {
  char rxarg[256];
  struct WBArg *WB_arg;
  struct RexxMsg *RM_ptr;
  struct MsgPort *MyPort,*RxPort;
  ULONG waitmask;
  LONG retvalue;
  LONG oldlock;
  struct Process *procID;
  
    procID = (struct Process*)0;
    if (!(RxPort = (struct MsgPort*)FindPort("REXX")))
      {
        exit(60);
      }
    
    if ((RexxSysBase=(struct RexxLib*)OpenLibrary("rexxsyslib.library",RXSVERS))==NULL)
      {
        exit(40); /* no rexx, no run!!! */
      }
    if (argc) /* CLI invocation */
      {
        if (argc >= 2)
          {
            strcpy(&rxarg[0],argv[1]);
          }
        else
          {
            exit(20); /* too few parms from CLI */
          }
      }
    else /* WB invocation */
      {
        if(WBenchMsg->sm_NumArgs >= 2)
          {
            WB_arg = WBenchMsg->sm_ArgList;
            ++WB_arg; /* move to 2nd argument... */
            strcpy (&rxarg[0],WB_arg->wa_Name);
            oldlock=(LONG)CurrentDir(WB_arg->wa_Lock);
            procID=(struct Process*)FindTask(0);
            procID->pr_CIS = Open("*",MODE_OLDFILE);
            procID->pr_COS = procID->pr_CIS;
          }
        else
          {
            exit(20); /* too few parms from WB */
          }
      }
    if (!(MyPort = (struct MsgPort*)CreatePort(NULL,0)))
      {
        exit(15);
      }
    
    RM_ptr = (struct RexxMsg*)CreateRexxMsg(MyPort,NULL,"REXX"); /* make a rexx message; */
    RM_ptr->rm_Args[0] = (STRPTR)CreateArgstring(&rxarg[0],strlen(&rxarg[0]));
    RM_ptr->rm_Action  = RXCOMM;
    
    PutMsg(RxPort,RM_ptr);
    waitmask = 1L << MyPort->mp_SigBit;
    Wait(waitmask);
    GetMsg(MyPort);
    
    DeletePort(MyPort);
    
    retvalue = RM_ptr->rm_Result1;
    
    DeleteArgstring(RM_ptr->rm_Args[0]);
    DeleteRexxMsg(RM_ptr);
    CloseLibrary(RexxSysBase);
    Delay(100);
    CurrentDir(oldlock);
    if (procID)
      {
        Close(procID->pr_CIS);
      }
    exit(retvalue);
  }
