In   -
Out  ExtraHelp
Type Module
Ver  1.00i

Define Module
 Name      ExtraHelp
 Author    Justin Fletcher
 Services
  Help     help
 End Services
End Module

Pre
 REM Set this to allow directories to be recognised as 'runnable'
 #Cond set ALLOWDIRS 1
 f_extended=1
End Pre

#Rem off
>help
   STMFD   (sp)!,{r0-r8,link}            ; Stack registers
   REM     "Searching for %$0"
   MOV     r8,#0                         ; r8 = flags
$checkflags
   LDRB    r1,[r0]                       ; read a byte from cli
   CMP     r1,#ASC"-"                    ; is it - ?
   BNE     $noflags                      ; if not, there're no flags
   LDRB    r1,[r0,#1]!                   ; read next byte
$anotherflag
   CMP     r1,#ASC"x"                    ; is it 'x' (extended) ?
   ORREQ   r8,r8,#f_extended             ; if so, flag as such
   BEQ     $flagsknown                   ; yep, we know it
$syntax
   SWI     "XOS_WriteS"                  ; write the message
   EQUZA   "Syntax: *Help [[-x] <libcommand>] | <command>"
   SWI     "XOS_NewLine"                 ; and a newline afterwards
   B       $claimed                      ; we've claimed it
$flagsknown
   LDRB    r1,[r0,#1]!                   ; read next byte
   CMP     r1,#32                        ; is that it ?
   BLT     $syntax                       ; yep - urgh
   BNE     $anotherflag                  ; if not space, it's another flag
$skipspaces
   LDRB    r1,[r0,#1]!                   ; read next byte
   CMP     r1,#32                        ; is this space ?
   BNE     $checkflags                   ; jump to the topy bit
   B       $skipspaces                   ; find more
$noflags
   MOV     r6,r0                         ; r6-> command line
   MOV     r7,r0                         ; r7-> cli too

   XSWI    "XOS_File",15,r6,,,^$`runpath ; find the file
   MOVVS   r0,#0                         ; if error, return 0 (not found)
   CMP     r0,#1                         ; was it found (as a file) ?
#Cond of ALLOWDIRS
   CMPNE   r0,#2                         ; or a directory ?
#Cond end
   BNE     $notours                      ; nope - not ours
   REM     "Found command in Run$Path"
; found it, now copy the path
   MOV     r1,#0                         ; offset into string
   ADR     r2,`buffer                    ; address of our buffer
   MVN     r3,#NOT -1                    ; offset of last '.'
   REM     "Copy path"
$copypath
   LDRB    r0,[r6,r1]                    ; read byte
   TEQ     r0,#ASC"."                    ; is it '.' ?
   MOVEQ   r3,r1                         ; if so, this is the offset
   CMP     r0,#32                        ; is it <= space ?
   MOVLES  r0,#0                         ; if so, use 0
   STRB    r0,[r2,r1]                    ; store it
   ADD     r1,r1,#1                      ; inc offset
   BNE     $copypath                     ; and if not 0 go around again
; copied path (r3= offset of .) now we put the 'docs.' in there
   ADR     r4,$`docpath                  ; the 'docs.' string
   ADD     r2,r2,r3                      ; r2-> the bit after the . in dest
   REM     "Copy docs. (r3=%r3, r2=%&2)"
$copydocs
   LDRB    r0,[r4],#1                    ; read byte and inc
   STRB    r0,[r2,#1]!                   ; store it at +1 and inc
   TEQ     r0,#0                         ; is that it ?
   BNE     $copydocs                     ; and copy some more
; now to copy the command on to the end of it
   ADD     r6,r6,r3                      ; r5-> the .
   REM     "Copy command (r2=%&2)"
$copyprog
   LDRB    r0,[r6,#1]!                   ; read byte at +1 and inc
   STRB    r0,[r2],#1                    ; store it and inc
   CMP     r0,#32                        ; is it <= space ?
   MOVLES  r0,#0                         ; if so, use 0
   BNE     $copyprog                     ; copy some more
; now we need to dump the file
   ADR     r1,`buffer                    ; address of our filename
   REM     "Docs file is '%$1'"
   XSWI    "XOS_File",22,r1,,,^$`runpath ; read filetype to r6
   MOVVS   r0,#0                         ; if error, then file not there
   BVS     $notours                      ; if not there, exit
   SUB     r6,r6,#&F00                   ; leave just the 'base'
   MOV     r4,#0                         ; r4=0 means use PrettyPrint
   CMP     r6,#&FD                       ; is it 'data' ?
   MOVEQ   r4,#1                         ; r4=1 means use OS_Write0
   CMPNE   r6,#&FF                       ; is it 'text' ?
   BNE     $notours                      ; if not text or data, ignore it
   XSWI    "XOS_Find",&42,,^$`runpath    ; open file
   MOVVS   r0,#0                         ; if error, r0=0
   REM     "Returned handle = %&0"
   TEQ     r0,#0                         ; did it open ?
   BEQ     $notours                      ; if not, jump out
; now to read input to the buffer and print it
   MOV     r6,r0                         ; hang on to file handle
   SWI     "XOS_WriteS"
   EQUZA   "==> Help on keyword "
$keyloop
   LDRB    r0,[r7],#1                    ; read and inc
   CMP     r0,#32                        ; is it space, or lower ?
   SWIGT   "XOS_WriteC"                  ; if not, show it
   BGT     $keyloop
   SWI     "XOS_WriteS"
   EQUZA   " (library command)"
   XSWI    "XOS_NewLine"
$linestart
   MOV     r1,r6                         ; r1 = file handle
   ADR     r2,`buffer                    ; r2-> buffer for result
   MOV     r3,#0                         ; offset
$getmore
   SWI     "XOS_BGet"                    ; read a byte
   BVS     $eof                          ; if error, that's your lot
   BCS     $eof                          ; if eof, that's your lot
   CMP     r0,#1                         ; is it 1 (Extended info)
   BNE     $notextended                  ; nope, so we don't care
   TST     r8,#f_extended                ; are we showing extended things ?
   BNE     $getmore                      ; if so, continue showing...
; skip extended sequence
$skipextended
   SWI     "XOS_BGet"                    ; read a byte
   BVS     $eof                          ; if error, that's your lot
   BCS     $eof                          ; if eof, that's your lot
   CMP     r0,#1                         ; is it 1 (Extended info end) ?
   CMPNE   r0,#10                        ; or is it 10 ?
   BEQ     $getmore                      ; if so, start showing again
   B       $skipextended                 ; if not, let's hand around...
$notextended
   CMP     r0,#10                        ; is it 10 ?
   MOVEQ   r0,#13                        ; if so, make it 13
   STRB    r0,[r2,r3]                    ; store in our buffer
   ADD     r3,r3,#1                      ; inc offset
   CMPEQ   r4,#1                         ; if r4=1 then 10 becomes 13,10
   MOVEQ   r5,#10                        ; if so, get our 10
   STREQB  r5,[r2,r3]                    ;        and store
   ADDEQ   r3,r3,#1                      ;        inc offset
   CMP     r3,#254                       ; are we almost full ?
   CMPNE   r3,#253                       ; or nearly almost full ?
   CMPNE   r0,#13                        ; or is it cr ?
   BNE     $getmore                      ; if not, get some more
   MOV     r0,#0                         ; if so, get a 0 terminator
   STRB    r0,[r2,r3]                    ; and store it
   CMP     r4,#0                         ; if r4=0 then PrettyPrint it
   MOV     r0,r2                         ; r0-> string to send
   XSWIEQ  "XOS_PrettyPrint",,0,0        ; send the line (no dict, no cmd)
   XSWINE  "XOS_Write0"                  ; otherwise write it
   BVS     $closefile                    ; argh... disc full ?
   B       $linestart                    ; and back to the top for the rest
$eof
   MOV     r0,#0                         ; if so, get a 0 terminator
   STRB    r0,[r2,r3]                    ; and store it
   XSWI    "XOS_PrettyPrint",r2,0,0      ; send the line (no dict, no cmd)
$closefile
   XSWI    "XOS_Find",0,r6               ; close the file
$claimed
   MOV     r0,#0                         ; we need to claim the service
   STR     r0,[sp,#4*1]                  ; and store as r1
$notours
   LDMFD   (sp)!,{r0-r8,pc}              ; Return from call

$`runpath
   EQUZA   "Run$Path"
$`docpath
   EQUZA   "Help."
.`buffer
   RES     256

#Post
#Run <CODE>
