;*************************** AMUS Program Label ******************************
; Filename: AMOS.M68                                        Date: 01/04/89
; Category: SBR          Hash Code: 235-426-227-446      Version: 2.0(102)
; Initials: ULTR/AM      Name: DAVID PALLMANN
; Company: ULTRASOFT CORP.                         Telephone #: 5163484848
; Related Files:
; Min. Op. Sys.: AMOSL 1.3B                    Expertise Level: BEG
; Special:
; Description: AlphaBASIC Subroutine to execute an AMOS command.
;
;*****************************************************************************
;Copyright (C) 1988 UltraSoft Corp.  All Rights Reserved.
;Written by David Pallmann
;
;Edit History:
;2.0(100)  12-May-88  Created.  Now works with .CMD & .DO files. /DFP
;2.0(101)  27-Dec-88  Correct problem in restoring user partition where
;                     SAVCMZ(A4) was erroneously coded as SAVCMZ(A5).
;                     Thanks to Ladd Lindsay of Digital Solutions, Fresno,
;                     for finding this problem. /DFP
;2.0(102)  09-Jul-90  Restore JOBPRG after call. This helps programs like
;                     SPM give a more accurate report of program use. /KFC
;
;Calling Format:        XCALL AMOS, command$
;Example:               XCALL AMOS, "SYSTAT"

       VMAJOR=2
       VMINOR=0
       VSUB=0
       VEDIT=102.
       VWHO=0

       OBJNAM  .SBR

       SEARCH  SYS
       SEARCH  SYSSYM

;*****************************************
;*  XCALL argument list (indexed by A3)  *
;*****************************************

       .OFINI
       .OFDEF  COUNT,2
       .OFDEF  TYPE1,2
       .OFDEF  ADDR1,4
       .OFDEF  SIZE1,4
       .OFSIZ  XCSIZE

;*******************************
;*  XCALL variable type codes  *
;*******************************

       UNFORMATTED=0
       STRING=2
       FLOAT=4
       BINARY=6

;**********************
;*  ASCII characters  *
;**********************

       LF=12
       CR=15

;************************
;*  variable workspace  *
;************************

       .OFINI
       .OFDEF  SAVEA4,4                ; saved copy of A4 register
       .OFDEF  CMDLIN,512.             ; command line buffer
       .OFDEF  SAVBAS,4                ; saved job memory base
       .OFDEF  SAVSIZ,4                ; saved job memory size
       .OFDEF  SAVCMZ,2                ; saved job command file size
       .OFDEF  SAVCMS,2                ; saved job command file status
       .OFDEF  SAVPRG,4                ; saved job program name [102]
       .OFSIZ  MEMSIZ

;********************
;*  program header  *
;********************

       PHDR    -1,0,PH$REE!PH$REU      ; reentrant & reusable (of course)

;**********************
;*  check parameters  *
;**********************

ENTRY:  CMPW    COUNT(A3),#1
       JNE     ERROR0
       CMPW    TYPE1(A3),#STRING
       JNE     ERROR1

;******************************************************************
;*  copy user-supplied command to our internal command-line area  *
;******************************************************************

CPYLIN: MOV     ADDR1(A3),A0
       LEA     A1,CMDLIN(A4)
       MOV     SIZE1(A3),D0
10$:    MOVB    (A0)+,(A1)+
       BEQ     20$
       SOB     D0,10$
       CLRB    (A1)+
20$:    MOVB    #CR,-1(A1)
       MOVB    #LF,(A1)+
       CLRB    @A1

;*********************************************************************
;*  save our current memory partition (so we can get it back later)  *
;*********************************************************************

SAVE.PARTITION:
       JOBIDX  A0
       MOV     JOBBAS(A0),SAVBAS(A4)   ; save job base address
       MOV     JOBSIZ(A0),SAVSIZ(A4)   ; save job size in bytes
       MOVW    JOBCMZ(A0),SAVCMZ(A4)   ; save job command file size
       MOVW    JOBCMS(A0),SAVCMS(A4)   ; save job command file status
       MOV     JOBPRG(A0),SAVPRG(A4)   ; save job program name [102]

;******************************************
;*  create a fake empty memory partition  *
;******************************************

NEW.PARTITION:
       LEA     A1,MEMSIZ(A4)
       ADD     #4,A1
       MOV     A1,JOBBAS(A0)
       CLR     @A1
       MOV     A5,D0
       SUB     A1,D0
       SUB     #10,D0
       MOV     D0,JOBSIZ(A0)
       CLRW    JOBCMZ(A0)              ; clear command file size
       CLRW    JOBCMS(A0)              ; clear command file status
       MOV     A4,SAVEA4(A4)           ; make a copy of A4 register
       PUSH    A4
       LEA     A2,CMDLIN(A4)           ; index command line

;******************************************
;*  go for it!  execute the command line  *
;******************************************

EXEC:   AMOS                            ; execute command
       CTRLC   RESTORE.PARTITION       ; abort on ^C

;***********************************************************
;*  check to see if a .CMD or .DO file is still executing  *
;***********************************************************

CMD.DO: JOBIDX                          ; A6 := JCB address
       TSTW    JOBCMZ(A6)              ; command file running?
       BEQ     RESTORE.PARTITION       ;  no - we're done
       KBD     RESTORE.PARTITION       ; get a line (from the command file)
       BR      EXEC                    ; go execute it

;****************************************************************
;*  restore original memory partition and return to AlphaBASIC  *
;****************************************************************

RESTORE.PARTITION:
       POP     A4                      ; restore base register
       CMM     A4,SAVEA4(A4)           ; are we okay?
       JNE     ERROR2                  ;  no - better exit
       JOBIDX  A0                      ; A0 := JCB address
       MOV     SAVBAS(A4),JOBBAS(A0)   ; restore job partition base
       MOV     SAVSIZ(A4),JOBSIZ(A0)   ; restore job partition size
       MOVW    SAVCMZ(A4),JOBCMZ(A0)   ; restore command file size
       MOVW    SAVCMS(A4),JOBCMS(A0)   ; restore command file status   [101]
       MOV     SAVPRG(A4),JOBPRG(A0)   ; restore job program name      [102]
       RTN                             ; return control to BASIC

;********************
;*  error handling  *
;********************

ERROR0: TYPESP  ?Argument count error
       BR      ERROR

ERROR1: TYPESP  ?Argument type error
       BR      ERROR

ERROR2: TYPECR  ?Job partition corrupted in AMOS.SBR
       TYPECR  ?Fatal error - check job partition base and size
       EXIT

ERROR:  TYPECR  in AMOS.SBR
       RTN

       END