;*************************** AMUS Program Label ******************************
; Filename: UDIR.M68                                        Date: 01/30/89
; Category: UTIL         Hash Code: 755-547-731-423      Version: 1.0(100)
; Initials: ULTR/AM      Name: DAVID PALLMANN
; Company: ULTRASOFT CORP.                         Telephone #: 
; Related Files: 
; Min. Op. Sys.:                               Expertise Level: BEG
; Special: Demonstration program only. Source code not complete.
; Description: A fancy directory program using windowing like the Macintosh.
; Please send all feedback regarding this program to ULTR/AM.
; 
;*****************************************************************************
;Copyright (C) 1988 UltraSoft Corp.  All Rights Reserved.
;Written by: David Pallmann
;
;Edit History:
;1.0(100)  01-Sep-88  created. /DFP

	VMAJOR	=1
	VMINOR	=0
	VEDIT	=100.

	SEARCH	SYS
	SEARCH	SYSSYM
	SEARCH	TRM
	SEARCH	ULTRA

	FILMAX=1000.			; max #files we have room for

;**********************
;*  FILE TABLE ENTRY  *
;**********************

	.OFINI
	.OFDEF	FI.NAM,4		; filename
	.OFDEF	FI.EXT,2		; file extension
	.OFDEF	FI.BLK,4		; file size in blocks
	.OFDEF	FI.SIZ,4		; file size in bytes
	.OFDEF	FI.TYP,2		; file type
	.OFSIZ	FI.ESZ

;*************
;*  STORAGE  *
;*************

	.OFINI
	.OFDEF	FLAGS,4			; flags:
		F$HELP=1		;   help text is displayed
	.OFDEF	SPEC,30.		; wildcard specification
	.OFDEF	FILTBL,FI.ESZ*FILMAX	; file table
	.OFDEF	FILCNT,4		; file count
	.OFDEF	BLKCNT,4		; total blocks
	.OFDEF	OFFSET,4		; display offset
	.OFSIZ	MEMSIZ

START:	PHDR	-1,0,PH$REE!PH$REU	; program header
	GETIMP	MEMSIZ,A5		; allocate memory
	WINIT				; initialize WLDSCN

CMDLIN:	BYP
	LEA	A0,SPEC(A5)
10$:	LIN
	BEQ	20$
	MOVB	(A2)+,(A0)+
	BR	10$
20$:	CLRB	@A0

TERM:	TRMRST	D0
	ORW	#T$DAT!T$ECS,D0
	TRMWST	D0

LOOKUP:	LEA	A2,SPEC(A5)
	WSPEC				; process command line specification
	JNE	EXIT			;   error

LOAD:	CLR	FILCNT(A5)
	MOV	#FILMAX,D3
	LEA	A3,FILTBL(A5)
10$:	CTRLC	EXIT
	WSCAN
	BNE	20$
	CALL	GETFIL
	ADD	#FI.ESZ,A3
	SOB	D3,10$
20$:

SCREEN:	CUROFF
	CLS
	HIGH
	TYPESP	UltraSoft Directory of
	TSTB	SPEC(A5)
	BNE	10$
	TYPE	*.*
	BR	20$
10$:	TTYL	SPEC(A5)
20$:	CURSOR	#1,#47.
	TYPESP	Total of
	MOV	BLKCNT(A5),D1
	DCVT	0,OT$TRM!OT$TSP
	TYPE	block
	CMP	D1,#1
	BEQ	30$
	TYPE	s
30$:	TYPE	< in >
	MOV	FILCNT(A5),D1
	DCVT	0,OT$TRM!OT$TSP
	TYPE	file
	CMP	D1,#1
	BEQ	40$
	TYPE	s
40$:	CURSOR	#3,#1
	LOW
	TYPE	<File  .Ext  Blocks  Bytes>
	HIGH
	CALL	SORT.BY.EXT

PAGE:	CURSOR	#4,#1
	CLREOS
	MOV	#4,D4			; init row
	MOV	FILCNT(A5),D0
	BEQ	30$
	LEA	A3,FILTBL(A5)
	MOV	OFFSET(A5),D7
	SUB	D7,D0
	BEQ	30$
	MUL	D7,#FI.ESZ
	ADD	D7,A3
10$:	CTRLC	EXIT
	CALL	DISPLAY
	INCB	D4
	CMPB	D4,#23.
	BHI	30$
	ADD	#FI.ESZ,A3
	SOB	D0,10$
30$:	CALL	HLP

OPTION:	KBD	EXIT
	UCS
	BIT	#F$HELP,FLAGS(A5)
	BEQ	10$
	PUSH	D1
	CURSOR	#5,#47.
	CLREOL
	CURSOR	#6,#47.
	CLREOL
	CURSOR	#7,#47.
	CLREOL
	CURSOR	#8.,#47.
	CLREOL
	POP	D1
10$:	CMPB	D1,#'H
	JEQ	HELP
	CMPB	D1,#'[-'@
	JEQ	FINISH
	CMPB	D1,#'C-'@
	JEQ	FINISH
	CMPB	D1,#'R-'@
	JEQ	PAGE.UP
	CMPB	D1,#'T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	JMP	PAGE

ENDDIR:	MOV	FILCNT(A5),D7
	DEC	D7
	CMP	D7,#16.
	BLOS	10$
	SUB	#16.,D7
10$:	MOV	D7,OFFSET(A5)
	JMP	PAGE

FINISH:	CURSOR	#24.,#1
	CURON
	HIGH

EXIT:	EXIT

;************
;*  GETFIL  *
;************
;Copy file information from DDB @A4 into file entry @A3
;Adds one to FILCNT(A5)
;Sets space (linked) or C (contiguous) into FI.TYP(A3)

GETFIL:	INC	FILCNT(A5)
	MOV	D.FIL(A4),FI.NAM(A3)
	MOVW	D.EXT(A4),FI.EXT(A3)
	MOV	D.FSZ(A4),D1
	MOV	D1,FI.BLK(A3)
	ADD	D1,BLKCNT(A5)
	MOV	D.LSZ(A4),D0
	CMP	D0,#512.
	BHIS	20$

;SEQUENTIAL FILE

	MOVB	#40,FI.TYP(A3)
	DEC	D1
	MUL	D1,#510.
	SUB	#2,D0
	ADD	D0,D1
	MOV	D1,FI.SIZ(A3)
	BR	30$

;RANDOM FILE

20$:	MOVB	#'C,FI.TYP(A3)
	MUL	D1,#512.
	MOV	D1,FI.SIZ(A3)

30$:	RTN

;*************
;*  DISPLAY  *
;*************
;D4 contains row

DISPLAY:
	CURSOR	D4,#1
	DEPACK	FI.NAM(A3)
	DEPACK	FI.NAM+2(A3)
	TYPE	.
	DEPACK	FI.EXT(A3)
	TYPESP
	MOVB	FI.TYP(A3),D1
	TTY
	TYPESP
	MOV	FI.BLK(A3),D1
	DCVT	5,OT$TRM!OT$ZER
	MOV	FI.SIZ(A3),D1
	DCVT	7,OT$TRM!OT$ZER
	RTN

;Sort files by extension

SORT.BY.EXT:
	MOV	FILCNT(A5),D5
	JEQ	99$
	DEC	D5
	JEQ	99$
	CLR	D4
	LEA	A3,FILTBL(A5)
10$:	CMPW	D4,D5
	JHIS	99$
	CMMW	FI.EXT(A3),FI.EXT+FI.ESZ(A3)
	BLO	40$
	BHI	15$
	CMM	FI.NAM(A3),FI.NAM+FI.ESZ(A3)
	BLO	40$
15$:	MOV	A3,A0			; SWAP
	LEA	A1,FI.ESZ(A3)
	MOV	#FI.ESZ,D0
20$:	MOVB	@A0,D7
	MOVB	@A1,(A0)+
	MOVB	D7,(A1)+
	SOB	D0,20$
	TSTW	D4			; BACK-UP
	BEQ	10$
	SUB	#FI.ESZ,A3
	DECW	D4
	BR	10$
40$:	ADD	#FI.ESZ,A3
	INCW	D4
	BR	10$
99$:
	CURSOR	#3,#47.
	TYPE	extension order
	CLREOL
	RTN

;Sort files by name

SORT.BY.NAME:
	MOV	FILCNT(A5),D5
	JEQ	99$
	DEC	D5
	JEQ	99$
	CLR	D4
	LEA	A3,FILTBL(A5)
10$:	CMPW	D4,D5
	JHIS	99$
	CMM	FI.NAM(A3),FI.NAM+FI.ESZ(A3)
	BLO	40$
	BHI	15$
	CMMW	FI.EXT(A3),FI.EXT+FI.ESZ(A3)
	BLO	40$
15$:	MOV	A3,A0			; SWAP
	LEA	A1,FI.ESZ(A3)
	MOV	#FI.ESZ,D0
20$:	MOVB	@A0,D7
	MOVB	@A1,(A0)+
	MOVB	D7,(A1)+
	SOB	D0,20$
	TSTW	D4			; BACK-UP
	BEQ	10$
	SUB	#FI.ESZ,A3
	DECW	D4
	BR	10$
40$:	ADD	#FI.ESZ,A3
	INCW	D4
	BR	10$
99$:
	CURSOR	#3,#47.
	TYPE	alphabetical filename order
	CLREOL
	RTN

;Sort files by ascending size

SORT.BY.ASIZE:
	MOV	FILCNT(A5),D5
	JEQ	99$
	DEC	D5
	JEQ	99$
	CLR	D4
	LEA	A3,FILTBL(A5)
10$:	CMPW	D4,D5
	JHIS	99$
	CMM	FI.SIZ(A3),FI.SIZ+FI.ESZ(A3)
	BLOS	40$
	MOV	A3,A0			; SWAP
	LEA	A1,FI.ESZ(A3)
	MOV	#FI.ESZ,D0
20$:	MOVB	@A0,D7
	MOVB	@A1,(A0)+
	MOVB	D7,(A1)+
	SOB	D0,20$
	TSTW	D4			; BACK-UP
	BEQ	10$
	SUB	#FI.ESZ,A3
	DECW	D4
	BR	10$
40$:	ADD	#FI.ESZ,A3
	INCW	D4
	BR	10$
99$:
	CURSOR	#3,#47.
	TYPE	ascending size order
	CLREOL
	RTN

;Sort files by descending size

SORT.BY.DSIZE:
	MOV	FILCNT(A5),D5
	JEQ	99$
	DEC	D5
	JEQ	99$
	CLR	D4
	LEA	A3,FILTBL(A5)
10$:	CMPW	D4,D5
	JHIS	99$
	CMM	FI.SIZ(A3),FI.SIZ+FI.ESZ(A3)
	BHIS	40$
	MOV	A3,A0			; SWAP
	LEA	A1,FI.ESZ(A3)
	MOV	#FI.ESZ,D0
20$:	MOVB	@A0,D7
	MOVB	@A1,(A0)+
	MOVB	D7,(A1)+
	SOB	D0,20$
	TSTW	D4			; BACK-UP
	BEQ	10$
	SUB	#FI.ESZ,A3
	DECW	D4
	BR	10$
40$:	ADD	#FI.ESZ,A3
	INCW	D4
	BR	10$
99$:	CURSOR	#3,#47.
	TYPE	descending size order
	CLREOL
	RTN

HLP:	HIGH
	CURSOR	#5,#47.
	TYPE	N
	CURSOR	#6,#47.
	TYPE	E
	CURSOR	#7,#47.
	TYPE	A
	CURSOR	#8.,#47.
	TYPE	D
	LOW
	CURSOR	#5,#49.
	TYPE	<- name order>
	CURSOR	#6,#49.
	TYPE	<- extension order>
	CURSOR	#7,#49.
	TYPE	<- ascending size order>
	CURSOR	#8.,#49.
	TYPE	<- descending size order>
	HIGH
	OR	#F$HELP,FLAGS(A5)
	RTN

	END