;============================================================================
; SHOW.ASM
; written by Jim Gage 1996
; 4/1/96
; This file gives an example of how to use the fileio.inc and startup.inc
; include files.
;============================================================================

.include "startup.inc"
.include "fileio.inc"

@start:

  call  @ParseParams        ;parse the command line params

  push  cs                  ;put finname segment on the stack
  mov   ax,@finname         ;ax<-finname offset
  push  ax                  ;put on the stack
  xor   ax,ax               ;ax<-0, read file
  push  ax                  ;put ax on the stack
  call  @OpenTextFile       ;Open file as text

  mov   [@FileRec],dx       ;save the record pointer
  mov   [@FileRec+2],ax

@@Loop01:

;-- read a line from the file --
  push  cs                  ;put segment on stack
  mov   ax,@filebuff        ;put offset of buffer on the stack
  push  ax
  mov   ax,255              ;ax<-255, max size of buffer
  push  ax                  ;put it on the stack
  mov   ax,[@FileRec+2]     ;get the record's segment
  push  ax                  ;put it on the stack
  mov   ax,[@FileRec]       ;get the record's offset
  push  ax                  ;put it on the stack
  call  @ReadTextFile       ;read the file into the buffer

;-- print the line --
  push  cs                  ;put segment on stack
  mov   ax,@filebuff        ;put buffer offset into ax
  push  ax                  ;put ax on the stack
  call @WritePchar          ;write the buffer to the screen

;-- print a return, linefeed --
  mov   dx,@Return          ;dx<- offset of return string
  push  cs                  ;save cs
  pop   ds                  ;restore as ds
  mov   ah,09h              ;ah<-09h DOS print string
  int   021h                ;call DOS interrupt

;-- check for end of file
  mov   ax,[@FileRec+2]     ;put offset of FileRec in ax
  push  ax                  ;put ax onto the stack
  mov   ax,[@FileRec]       ;put segment of FileRec in ax
  push  ax                  ;put ax onto the stack
  call  @TextEOF            ;check for EOF
  cmp   ax,0                ;check return value
  je    @@Loop01            ;keep looping

  ret


;----------------------------------------------------------------------------
; @ParseParams

@ParseParams:
  push  ds                  ;save ds
  push  si                  ;save si
  push  es                  ;save es
  push  di                  ;save di

  cld                       ;clear decrement flag
  xor   ch,ch               ;clear ch
  mov   cl,[@commcount]     ;load size of string
  jcxz  @DisplayUsage       ;if nothing there then display usage
  inc   cx                  ;for good measure
  push  cs                  ;move cs to ds
  pop   ds
  mov   si,@commline        ;put commline segment into si

@@ParseParams1:
@@ParseParams2:
  lodsb                     ;get byte
  dec   cx                  ;decrease counter
  jcxz  @@ParseParamsDone   ;if at the end then quit
  cmp   al,32               ;space?
  je    @@ParseParams2      ;
  cmp   al,9                ;tab?
  je    @@ParseParams2      ;

;these could be used later
  cmp   al,'-'              ;hyphen?
  je    @@ParseParams7      ;
  cmp   al,'/'              ;slash?
  je    @@ParseParams7      ;

; it has to be a file name
  push  cs                  ;move cs to es
  pop   es
  mov   di,@finname         ;get finname offset
@@ParseParams3:
  cmp   al,32               ;hit a space?
  je    @@ParseParams4      ;then done
  cmp   al,9                ;hit a tab?
  je    @@ParseParams4      ;then done
  stosb                     ;save the byte
  lodsb                     ;load next byte
  dec   cx                  ;decrease counter
  jcxz  @@ParseParams4      ;cx=0?
  jmp   @@ParseParams3      ;keep going
@@ParseParams4:
  mov   al,0                ;make sure file name is NULL terminated
  stosb                     ;save zero
  mov   di,@finname         ;put filename segment in di
; -- check filename extension --
@@ParseParams5:
  mov   al,es:[di]          ;get a byte from the filename
  cmp   al,0
  je    @@ParseParams6      ;yes then jmp out of loop
  cmp   al,'.'              ;hit a .?
  je    @@ParseParams99     ;yes then go back to parsing
  inc   di                  ;keep moving
  jmp   @@ParseParams5
@@ParseParams6:
; file extension can be added here
  mov   al,0                ;set al<-0
  stosb                     ;NULL terminate the string
  jmp   @@ParseParams99     ;skip the switches

;must be a switch
@@ParseParams7:
; parse switch commands here

@@ParseParams99:
  jcxz  @@ParseParamsDone   ;at the end of counter?
  jmp   @@ParseParams1      ;no keep parsing

@@ParseParamsDone:          ;
  mov   cx,[@finname]       ;check the filename
  xor   ch,ch               ;is anything there?
  jcxz  @DisplayUsage       ;no display usage
  pop   di                  ;restore di
  pop   es                  ;restore es
  pop   si                  ;restore si
  pop   ds                  ;restore ds
  ret                       ;return

@DisplayUsage:
  mov   dx,@UsageMessage    ;dx<-offset of Usage message
  mov   ah,9                ;ah<-9 DOS print string
  int   021h                ;call DOS
  jmp   @exit               ;exit the program


;============================================================================
; Data
;============================================================================

@UsageMessage:
  db "Usage: SHOW fname",0dh,0ah,'$'

@finname:    db 80 dup(0)

@filebuff: db 0100h dup(0)

@FileRec: dw 0,0

@Return:  db 0dh,0ah,'$'