; This is a simple test of the ARP GADS function, which parses the CLI
; command line parameters.  GADS automatically handles templates, additional
; help, and orders arguments positionally even if they were given in some other
; order.  It also handles interactive processing with both the template and
; additional help string.  GADS is an internal function of the arp.library.
;
; assembler: Howe Asm68k ver 1.2.1
; linker   : Blink 6.9
; author   : P. J. Rovero 05 Feb 88

  option nl
  include "libraries/arpbase.i"
  option l

  section arptest,code,public

  xref  _AbsExecBase
  xref  _LVOFindTask
  xref  _LVOOpenLibrary

; All references to the data section are external for seperate assembly
; utilizing the project tool make

  xref  help,doslib,dosbase,stack_ptr
  xref  count,argarray,template
  xref  parm_len,parm_ptr,msg1,msg2,error_msg

; CLI-only startup code
;    save command line pointer and length
;    check to ensure the program was NOT started from workbench
  move.l   a7,stack_ptr
  move.l   d0,parm_len
  move.l   a0,parm_ptr
  move.l   _AbsExecBase,a6
  suba.l   a1,a1
  LINKDOS  FindTask
  move.l   d0,a4

  moveq    #20,d0
  tst.l    140(a4)
  beq      exit

; Open the ARP library
  jsr      opendos
  tst.l    d0
  beq.s    exit
  move.l   d0,a6
  move.l   d0,dosbase

; Parse the command line -- call GADS
;    Count = GADS( parm_ptr,  parm_len,   help,  argarray,  template)
;      d0            a0          d0        a1      a2          a3
; See ARP programmers manual for more details
  move.l   parm_ptr,a0
  move.l   parm_len,d0
  lea      help,    a1
  lea      argarray,a2
  lea      template,a3
  LINKDOS  GADS
  move.l   d0,count       ; number of parameters GADS recognized
  move.l   count,d3       ; save in a loop count register
  subq.l   #1,d3          ; massage for DBcc loop (-1 trigger vice 0)

; was there an error ? (d0 <= 0)
  cmpi     #0,d0
  bgt.s    skip1     ; nope

; print an error message, then quit
     lea      error_msg,a1
     LINKDOS  Puts
     bra.s    exit

skip1:
; print out the number of parameters
  lea   msg1,a0
  lea   count,a1
  LINKDOS  Printf
  lea   argarray,a1
  move.l   a1,a5

print_args:
  lea msg2,a0
  LINKDOS  Printf
  add.l   #4,a5           ; point to next long word
  move.l   a5,a1          ; just in case a1 gets trashed...
  dbeq     d3,print_args

  moveq.l  #0,d0          ; normal non-error exit
exit
  LINKDOS  ArpExit
  move.l   stack_ptr,a7
  rts

opendos
  lea      doslib,a1
  move.l   #0,d0
  LINKDOS  OpenLibrary
  rts

  END
