(*---------------------------------------------------------------------------
  :Program.    Devices.mod
  :Author.     Fridtjof Siebert
  :Address.    Nobileweg 67, D-7-Stgt-40
  :Shortcut.   [fbs]
  :Version.    1.0
  :Date.       14-Jan-88
  :Copyright.  PD
  :Language.   Modula-II
  :Translator. M2Amiga v3.1d
  :Imports.    ARP
  :History.    Original C Version by Scott Ballantyne
  :Usage.      Devices [DEVICES] [DISKS] [VOLUMES] [ASSIGNS]
  :Contents.   arp.library demo, uses DA lists to determine what devices
  :Contents..  are connected to the system.
---------------------------------------------------------------------------*)

MODULE Devices;

FROM SYSTEM IMPORT ADR;
FROM Arts   IMPORT dosCmdBuf, dosCmdLen, Terminate;
FROM ARP    IMPORT DirectoryEntryPtr, CLIArgPtr, DirEntryTypes, DirEntryTypeSet,
                   AddDADevs, FreeDAList, GADS, deDevice, deVolume, ArpOpen,
                   deUnmounted, deAssign, FPrintf, STRPTR, Output;
FROM Dos    IMPORT FileHandlePtr, newFile;

CONST
  cliTemplate = "DEVICES/S,DISKS/S,VOLUMES/S,ASSIGNS/S";
  cliHelp = "Displays devices connected to this system.";

TYPE
  Args = (Devices,Disks,Volumes,Assigns);

VAR
  DeviceList: DirectoryEntryPtr;  (* List header                     *)
  numdevs: LONGINT;               (* # devices found                 *)
  dev: DirectoryEntryPtr;         (* used for displaying devices     *)
  argv: ARRAY Args OF CLIArgPtr;  (* Arg table                       *)
  argc: LONGINT;                  (* # of args                       *)
  WhichDevices: DirEntryTypeSet;  (* Which devices shall I display ? *)
  q: LONGINT;                     (* dummy trashcan variable         *)
  StrPtr: STRPTR;                 (* used for Printf on string       *)
  out: FileHandlePtr;

PROCEDURE WriteLn();
VAR LF: CARDINAL;
BEGIN
  LF := 0A00H; (* this is used as a string *)
  q := FPrintf(out,ADR(LF),NIL);
END WriteLn;

(*------  MAIN:  ------*)

BEGIN

  out := Output();
  IF out=NIL THEN
    out := ArpOpen(ADR("CON:0/0/480/200/Devices"),newFile);
    IF out=NIL THEN Terminate(0) END;
  END;

  argc := GADS(dosCmdBuf,dosCmdLen,ADR(cliHelp),ADR(argv),ADR(cliTemplate));

  WhichDevices := DirEntryTypeSet{};
  IF argc<1 THEN INCL(WhichDevices,devices) END; (* set default *)
  IF argv[Devices]^.bool THEN INCL(WhichDevices,devices); END;
  IF argv[Disks  ]^.bool THEN
    WhichDevices := WhichDevices + DirEntryTypeSet{devices,diskonly};
  END;
  IF argv[Volumes]^.bool THEN INCL(WhichDevices,volumes) END;
  IF argv[Assigns]^.bool THEN INCL(WhichDevices,dirs   ) END;

  (* Now get the requested devices *)

  DeviceList := NIL;
  numdevs := AddDADevs(ADR(DeviceList),WhichDevices);

  IF numdevs=0 THEN
    q := FPrintf(out,ADR("No entries found"),NIL); WriteLn;
    Terminate(0)
  END;

  q := FPrintf(out,ADR("Found %ld entries"),ADR(numdevs)); WriteLn;

  (* Now display the name and the type of device it is. *)

  dev := DeviceList;
  WHILE dev#NIL DO
    StrPtr := ADR(dev^.name); q := FPrintf(out,ADR("%-32s"),ADR(StrPtr));
    WITH dev^ DO
      IF    type=deDevice    THEN
        q := FPrintf(out,ADR("Resident device")     ,NIL);
      ELSIF type=deVolume    THEN
        q := FPrintf(out,ADR("Volume [mounted]")    ,NIL);
      ELSIF type=deUnmounted THEN
        q := FPrintf(out,ADR("Volume [not mounted]"),NIL);
      ELSIF type=deAssign    THEN
        q := FPrintf(out,ADR("Logical Assignment")  ,NIL);
      ELSE
        q := FPrintf(out,ADR("Encountered Device of mystery!"),NIL);
      END;
      WriteLn;
      dev := next;
    END;
  END;

  FreeDAList(DeviceList);
END Devices.
