; --------------------------------------- ;
; TYPE.ASM                                ;
;                                         ;
; Version 1.0                             ;
;                                         ;
; IC v2.0+ Installable Command            ;
;                                         ;
; Display files in a different format     ;
; from the DOS TYPE command.              ;
;                                         ;
; Copyright (C) 1992, Geoff Friesen B.Sc. ;
; All rights reserved.                    ;
;                                         ;
; Developed with: Borland TASM 2.5        ;
; --------------------------------------- ;

_TEXT           SEGMENT BYTE PUBLIC 'CODE'
		ASSUME  cs:_TEXT, ds:_TEXT, es:_TEXT, ss:_TEXT
		ORG     100h

CR              EQU     13
LF              EQU     10

CLINE		EQU	10ah
HEXW		EQU	10ch
STDINIT		EQU	10eh

_type:
		lea     dx, msg
		mov     ah, 9
		int     21h
		int     20h

msg             DB      "NONCOM$"

code_entry      DW      _code
help_entry      DW      _help
byline_entry    DW      _byline
setup_entry     DW      _setup
cleanup_entry   DW      _cleanup

msg1		DB	CR, LF
		DB	"type: unable to open filespec"
		DB	CR, LF
		DB	'$'

msg2		DB	CR, LF
		DB	"type: unable to read filespec"
		DB	CR, LF
		DB	'$'

msg3		DB	CR, LF
		DB	"type: filespec expected"
		DB	CR, LF
		DB	'$'

_code           PROC    NEAR
		push	cs
		pop	ds

		call	ds:[STDINIT]	; Initialize.

		cmp	al, CR		; Argument specified?
		jnz	_code0		; Yes, branch.

		lea	dx, msg3	; Display error message.
		mov	ah, 9
		int	21h

		ret
_code0:
		mov     dx, si          ; Get filespec start address.
_code1:
		lodsb                   ; Delimit filespec with NULL.

		cmp     al, CR		; End of command-line?
		jnz     _code1		; No, branch.

		mov     BYTE PTR [si-1], 0 ; Make command-line ASCIZ.

		mov	ax, 3d00h	; Attempt to open file.
		int	21h
		jnc	_code2		; Branch if successful.

		lea	dx, msg1	; Display error message.
		mov	ah, 9
		int	21h

		ret
_code2:
		mov	bx, ax		; Save handle.
_code3:
		lea	dx, buffer	; Read file.
		mov	cx, 4000
		mov	ah, 3fh
		int	21h
		jc	_code6

		mov	cx, ax
		jcxz	_code7

		lea	si, buffer
_code4:
		mov	ah, 1		; Key pressed?
		int	16h
		jnz	_code8		; Yes, branch.

		lodsb
		mov	dl, '.'

		cmp	al, ' '
		jb	_code5

		cmp	al, '~'
		ja	_code5

		mov	dl, al
_code5:
		mov	ah, 2
		int	21h

		loop	_code4
		jmp	SHORT _code3
_code6:
		lea	dx, msg2
		mov	ah, 9
		int	21h
_code7:
		mov	ah, 3eh		; Close file.
		int	21h

		ret
_code8:
		mov	ah, 0		; Extract keystroke.
		int	16h

		jmp	SHORT _code7
_code           ENDP

_help           DB      CR, LF
		DB      "TYPE: Display file contents"
		DB      CR, LF
		DB      CR, LF
		DB	"Syntax: TYPE filespec"
		DB	CR, LF
		DB	CR, LF
		DB	"The TYPE command allows you to view the contents"
		DB	CR, LF
		DB	"of the file specified by filespec without having"
		DB	CR, LF
		DB	"to look at nonASCII characters.  Each character"
		DB	CR, LF
		DB	"with an ASCII code less than 32 or greater than"
		DB	CR, LF
		DB	"126 is displayed as a period making it easier to"
		DB	CR, LF
		DB	"find messages in binary files.  TYPE outputs the"
		DB	CR, LF
		DB	"file contents to the standard output device.  If"
		DB	CR, LF
		DB	"any key is pressed then TYPE aborts."
		DB	CR, LF
		DB      '$'

_byline         DB      "TYPE     - display file contents"
		DB      CR, LF
		DB      '$'

smsg		DB	CR, LF
		DB	"type: setting up"
		DB	CR, LF
		DB	'$'

_setup          PROC    NEAR
		lea	dx, smsg
		mov	ah, 9
		int	21h

		ret
_setup          ENDP

cmsg		DB	CR, LF
		DB	"type: cleaning up"
		DB	CR, LF
		DB	'$'

_cleanup        PROC    NEAR
		lea	dx, cmsg
		mov	ah, 9
		int	21h

		ret
_cleanup        ENDP

buffer		LABEL	BYTE

_TEXT           ENDS
		END     _type