NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/man
*--------------------------------------
				.INB inc/macros.i
				.INB inc/mli.i
				.INB inc/a2osx.i
				.INB inc/libtui.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
hFileBuf		.BS 1
hLineBuf		.BS 1

TimeOut			.BS 1

hCTX			.BS 1
hSCR			.BS 1
hTBOX			.BS 1

BufSize			.BS 2
ZPLinePtr		.BS 2
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPPtr3			.BS 2
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT		S.PS.F
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data Segment Size
				.DA #64					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT

L.LIBTUI		.DA LIBTUI

L.ENV.MANPATH	.DA ENV.MANPATH
L.FILE.INDEX	.DA FILE.INDEX
L.MSG.ScrTitle	.DA MSG.ScrTitle

L.MSG.USAGE		.DA MSG.USAGE
L.MSG.CRLF		.DA MSG.CRLF
L.MSG.NULL		.DA MSG.NULL
L.MSG.GOTO		.DA MSG.GOTO	
L.MSG.OK		.DA MSG.OK		
L.MSG.ERROR		.DA MSG.ERROR	
				.DA 0
*--------------------------------------
CS.INIT			>LDYA L.LIBTUI
                >SYSCALL LoadLib
                bcs .9

                sta hLIBTUI

.9
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN.USAGE	>LDYA L.MSG.USAGE
				>SYSCALL PutS
				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.RUN			>LDYAI 256
				>SYSCALL GetMem
				bcs CS.INIT.RTS

				>STYA ZPLinePtr
				stx hLineBuf
				
				lda #1
				>SYSCALL ArgV
				bcc .1
				
				>LDYA L.FILE.INDEX

.1				jsr CS.GetLink
				bcs CS.RUN.USAGE
				
				jsr CS.GetFile
				bcs CS.INIT.RTS

				jsr CS.Scr.Init
				bcs CS.INIT.RTS
*--------------------------------------
CS.RUN.LOOP0	jsr CS.Scr.SetBuf
				jsr CS.Scr.SetStatusOkErr
*--------------------------------------
CS.RUN.LOOP		>SLEEP 

				lda hSCR
                >LIBCALL hLIBTUI,LIBTUI.Exec
                bcs CS.RUN.LOOP.9

				tay
				beq CS.RUN.LOOP			No Event

				cmp #3
				beq CS.RUN.LOOP.9

				cmp #EV.HL.SELECTED
				bne .1

				>LEA.G LinkBuf
				jsr CS.GetLink
				
				jsr CS.Scr.SetStatusGoto
				bcc CS.RUN.LOOP

				rts

.1				cmp #EV.HL.PRESSED
				bne CS.RUN.LOOP
				
				jsr CS.GetFile
				jsr CS.Scr.SetStatusOkErr
				bcc CS.RUN.LOOP0

CS.RUN.LOOP.9	rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9					is it a TIMER event?

				lda TimeOut
				beq .9

				dec TimeOut

.9				sec						do not discard TIMER event
				rts
*--------------------------------------
CS.QUIT			lda hLineBuf
				jsr .7
				
				lda hFileBuf
				jsr .7
				
				lda hCTX
				beq .1
				
				>LIBCALL hLIBTUI,LIBTUI.Close

.1				lda hLIBTUI
                beq .2

                >SYSCALL UnloadLib

.2				clc
				rts

.7				beq .8

				>SYSCALL Freemem
				
.8				rts
*--------------------------------------
CS.Scr.Init		>LIBCALL hLIBTUI,LIBTUI.Init
                bcs .99

                sta hCTX

				>PUSHA					hCTX
                >PUSHBI S.OBJ.F.bTitle+S.OBJ.F.bStatus
                >LIBCALL hLIBTUI,LIBTUI.NewScrn
                bcs .99

                sta hSCR

				jsr CS.Scr.SetTitle
				jsr CS.Scr.SetStatus

				>PUSHB hSCR
                >PUSHBI 0    			X1
                >PUSHBI 0    			Y1
                >PUSHBI 80    			W
                >PUSHBI 22    			H
                >PUSHBI 0				F
                lda hFileBuf
                >SYSCALL GetMemPtr
                >PUSHYA
                >PUSHW BufSize
                >LIBCALL hLIBTUI,LIBTUI.NewTBox
.99             bcs .9

				sta hTBOX

                >LIBCALL hLIBTUI,LIBTUI.Activate
				bcs .9

				>PUSHB hTBOX
                >PUSHBI S.OBJ.pVar
                >PUSHEA.G LinkBuf
                >LIBCALL hLIBTUI,LIBTUI.SetProp

.9              rts
*--------------------------------------
CS.Scr.SetTitle	>PUSHB hSCR
                >PUSHBI S.OBJ.pTITLE
                >PUSHW L.MSG.ScrTitle
                >LIBCALL hLIBTUI,LIBTUI.SetProp
				rts
*--------------------------------------
CS.Scr.SetBuf	>PUSHB hTBOX
				>PUSHBI S.OBJ.pBuf
                lda hFileBuf
				>SYSCALL GetMemPtr
				>PUSHYA
                >LIBCALL hLIBTUI,LIBTUI.SetProp
				bcs .9
				
				>PUSHB hTBOX
				>PUSHBI S.OBJ.BufSize
				>PUSHW BufSize
                >LIBCALL hLIBTUI,LIBTUI.SetProp
				
.9				rts
*--------------------------------------
CS.Scr.SetStatusGoto
				>PUSHEA.G SCR.StatusBuf	skip type
				>PUSHW L.MSG.GOTO
				>PUSHEA.G PathBuf
				>PUSHEA.G HtagBuf
				>PUSHBI 4
				>SYSCALL SPrintF
				bra CS.Scr.SetStatus
*--------------------------------------
CS.Scr.SetStatusOkErr
				bcc .1

				pha
				>PUSHEA.G SCR.StatusBuf
				>PUSHW L.MSG.ERROR
				pla
				pha
				>PUSHA
				>PUSHBI 1
				>SYSCALL SPrintF
				jsr CS.Scr.SetStatus
				pla
				sec
				rts

.1				>PUSHEA.G SCR.StatusBuf
				>PUSHW L.MSG.OK
				>PUSHBI 0
				>SYSCALL SPrintF
*--------------------------------------
CS.Scr.SetStatus
				>PUSHB hSCR
                >PUSHBI S.OBJ.pSTATUS
                >PUSHEA.G SCR.StatusBuf
                >LIBCALL hLIBTUI,LIBTUI.SetProp
				rts
*--------------------------------------
* [[text]host:port/path#htag]
* [#htag]text]
*--------------------------------------
CS.GetLink		>STYA ZPPtr1

				>LEA.G PathBuf
				>STYA ZPPtr2
				
				>LEA.G HTagBuf
				>STYA ZPPtr3

				lda #0
				sta (ZPPtr2)
				
				>PUSHW L.ENV.MANPATH
				>PUSHW ZPPtr2
				>SYSCALL GetEnv
				
				ldy #$ff
				
.1				iny
				lda (ZPPtr2),y
				bne .1
				
.2				jsr CS.GetCharPtr1
				beq .4
				
				cmp #'#'
				beq .3
				
				sta (ZPPtr2),y
				iny
				bra .2
				
.3				lda #0

.4				sta (ZPPtr2),y

				ldy #$ff

.5				iny
				jsr CS.GetCharPtr1
				sta (ZPPtr3),y
				bne .5

				clc

.9				rts
*--------------------------------------
CS.GetCharPtr1	lda (ZPPtr1)
				beq .8
				
				inc ZPPtr1
				bne .8
				
				inc ZPPtr1+1
				
.8				rts
*--------------------------------------
CS.GetFile		>LEA.G PathBuf
				>SYSCALL LoadTxtFile
				bcs .9
		
				stx hFileBuf
				iny
				bne .1
				
				inc
				
.1				>STYA BufSize

.9				rts
*--------------------------------------
CS.END
LIBTUI          .AZ "libtui"
hLIBTUI         .BS 1
ENV.MANPATH		.AZ "MANPATH"
FILE.INDEX		.AZ "index"
*--------------------------------------
MSG.ScrTitle	.CZ "A2osX-MAN - <Ctrl-Q>uit"
*--------------------------------------
MSG.USAGE		.CS "Usage : MAN <manfile>[#htag]"
MSG.CRLF		.CS "\r\n"
MSG.NULL		.HS 00
MSG.GOTO		.CZ "Goto: %s#%s"
MSG.OK			.CZ "Ok"
MSG.ERROR		.CZ "Error: $%h"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
LinkBuf			.BS 32
PathBuf			.BS MLI.MAXPATH+1
HTagBuf			.BS 32
SCR.StatusBuf	.BS 80
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/man.s
ASM
