static char rcsid[] = 
  "$Id: online.c,v 1.6 1993/08/10 20:46:23 jraja Exp $";
/*
 * online.c --- Send an online/offline command to a SANA-II device
 *
 * Author: ppessi <Pekka.Pessi@hut.fi>
 *
 * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
 *		    Helsinki University of Technology, Finland.
 *		    All rights reserved. 
 *
 * Created      : Sat Feb 20 14:15:07 1993 ppessi
 * Last modified: Tue Aug 10 23:40:00 1993 jraja
 */

static const char version[] = "$VER: online 2.0 (13.8.93)";

char copyright[] =
  "Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>\n"
                   "Helsinki University of Technology, Finland.\n"
                   "All rights reserved.\n";

/****** netutil.doc/offline *************************************************
*
*  NAME
*       Offline - put a SANA-II device offline 
*
*  TEMPLATE
*       Offline DEV=DEVICE devicename[/unit] [UNIT unit]
*
*  DESCRIPTION 
*       Offline sends the S2_OFFLINE command to the given SANA-II device
*       driver and unit. This command is normally used to disconnect SANA-II
*       device driver from the network adapter hardware. Device driver does
*       not accept any more read or write requests.
*
*  EXAMPLES 
*       This command puts the SLIP offline, which frees then the serial port
*       to your use.
*
*       OFFLINE slip.device/1
*
*  SEE ALSO
*       Online, sana2.device/S2_OFFLINE
*
*****************************************************************************
*
*/

/****** netutil.doc/online **************************************************
*
*  NAME
*       Online - put a SANA-II device online 
*
*  TEMPLATE
*       Online DEV=DEVICE devicename[/unit] [UNIT unit]
*
*  DESCRIPTION 
*       Online sends the S2_ONLINE command to the given SANA-II device driver
*       and unit. The device driver restarts the network adapter hardware and
*       accepts read and write request again.
*
*  EXAMPLES 
*
*       This command puts the Commodore Ethernet driver online.
*
*       Online a2065.device/0
*
*  SEE ALSO
*       Offline, sana2.device/S2_ONLINE
*
*****************************************************************************
*
*/

#include <exec/types.h>
#include <exec/devices.h>
#include <exec/ports.h>
#include <exec/semaphores.h>
#include <exec/memory.h>
#include <exec/execbase.h>

#include <dos/dos.h>
#include <dos/dosextens.h>

#ifdef __SASC 
#include <clib/exec_protos.h>
extern struct ExecBase* SysBase;
#include <pragmas/exec_sysbase_pragmas.h>
#include <proto/dos.h>
#else
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#endif

#include <devices/sana2.h>
#include <net/sana2errno.h>
#include <string.h>

extern int main(char *myname);

__asm LONG _main(void)
{
  struct Process *p;
  LONG retval = 128;

  SysBase = *(struct ExecBase**)4;
  DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37L);
  if (DOSBase) {
    p = (struct Process *)SysBase->ThisTask;
    if (p->pr_CLI) {
      struct CommandLineInterface *cli = (void *)(p->pr_CLI << 2);
      retval = main((char *)(cli->cli_CommandName<<2)+1);
    } else {
      /* No CLI. A process or a son of WB? */
    }
    CloseLibrary((struct Library *)DOSBase);
  }

  return retval;
}

struct DosLibrary *DOSBase;
struct ExecBase *SysBase;

int
main(char *myname)
{
  struct MsgPort *port = NULL;
  struct IOSana2Req *req = NULL;
  struct RDArgs *rdargs = NULL;
  LONG args[10] = { 0 };
  char *name; LONG unit;
  LONG retval = 0;
  LONG openerr = 1;

  rdargs = ReadArgs("DEV=DEVICE/A,UNIT/N", args, NULL);
  if (!rdargs) {
    retval = 10;
    PrintFault(IoErr(), myname);
    goto close;
  }

  if (port = CreateMsgPort()) 
    req = CreateIORequest(port, sizeof(*req));
  if (!port || !req) {
    retval = 40;
    PrintFault(ERROR_NO_FREE_STORE, "CreateIORequest");
    goto close;
  }

  name = (STRPTR) args[0];
  /* find unit number */
  {
    int namlen = strlen(name);
    char c;

    if (args[1]) {
      unit = *(LONG *)args[1];
    } else {
      unit = 0;
      c = '0';
      do {
	unit = unit * 10 + c - '0';
	c = name[--namlen];
      }
      while (c >= '0' && c <= '9');

      if (c != '/') {
	retval = 10;
	PrintFault(ERROR_REQUIRED_ARG_MISSING, myname);
	goto close;
      }
      name[namlen] = '\0';	
    }
    if (openerr = OpenDevice(name, unit, (struct IORequest *)req, 0L)) {
      retval = 10;
      Sana2PrintFault("OpenDevice", req);
      goto close;
    }
  }

  switch (FindArg("online,offline", FilePart(myname))) {
  case 0: 
    req->ios2_Req.io_Command = S2_ONLINE;
    name = "S2_ONLINE";
    break;
  case 1:
    req->ios2_Req.io_Command = S2_OFFLINE;
    name = "S2_OFFLINE";
    break;
  default:
    Printf("But what to do?\n");
    goto close;
  }
  DoIO((struct IORequest *)req);
  if (req->ios2_Req.io_Error) {
    Sana2PrintFault(name, req);
  }

 close:
  if (!openerr) 
    CloseDevice((struct IORequest*)req);
  if (req)
    DeleteIORequest((struct IORequest *)req);
  if (port)
    DeleteMsgPort(port);
  if (rdargs)
    FreeArgs(rdargs);

  return (retval);
}


