include	clib.inc
include	mouse.inc
include	conio.inc

	.186

	public	main
	public	thelp
	public	_bufin

_DATA	SEGMENT

thelp	dw 0
_bufin	dw 256 dup(?)
psp	dw 20 dup(?)
index	dw -1
dialog	dd ?

format_d label byte
	db "(%d) ",0

format_2d label byte
	db "%2d,%2d,%2d,%2d,%2d,%2d,%2d,%2d,%2d,%2d",0

format_psp label byte
	db "%04X",10,10,10,10
	db "%04X",10
	db "%04X",10
	db "%04X",10,10
	db "%p",10
	db "%p",10
	db "%p",10,10
	db "%p",10
	db "%-3d",10,0

;******** Resource begin IDPSP *
;	{ 0x001C,   3,   0, { 6, 2,61,21} },
;	{ 0x4000,   0, 'P', { 4,19,11, 1} },
;	{ 0x4000,   0, 'C', {18,19,11, 1} },
;	{ 0x0A00,   0, 'Q', {48,19, 8, 1} },
;******** Resource data  *******************
IDPSP_RC LABEL WORD
	DW	00AA7h	; Alloc size
	DW	0001Ch,00003h,00206h,0153Dh,04000h,05000h,01304h,0010Bh
	DW	04000h,04300h,01312h,0010Bh,00A00h,05100h,01330h,00108h
	DW	03DF0h,0F470h,0174Eh,07070h,0F07Fh,07008h,003F0h,07010h
	DW	07F70h,008F0h,01070h,012F0h,07017h,07F70h,005F0h,01070h
	DW	009F0h,0F017h,0100Bh,003F0h,0F017h,0100Bh,013F0h,0F017h
	DW	01008h,004F0h,0F017h,02010h,07250h,0676Fh,06172h,0206Dh
	DW	06553h,06D67h,06E65h,02074h,07250h,06665h,07869h,02820h
	DW	05350h,02950h,051F0h,05320h,06769h,0616Eh,07574h,06572h
	DW	0F03Ah,02033h,07250h,0676Fh,06172h,03A6Dh,035F0h,04320h
	DW	06D6Fh,0616Dh,0646Eh,0F03Ah,02072h,06553h,06D67h,06E65h
	DW	02074h,0666Fh,06520h,0766Eh,07269h,06E6Fh,0656Dh,0746Eh
	DW	0F03Ah,02026h,06553h,06D67h,06E65h,02074h,0666Fh,05020h
	DW	05053h,0F03Ah,0202Eh,06553h,06D67h,06E65h,02074h,0666Fh
	DW	07020h,07261h,06572h,0746Eh,0F03Ah,02067h,07453h,0726Fh
	DW	06465h,07420h,07265h,0696Dh,0616Eh,06974h,06E6Fh,0F03Ah
	DW	0202Ah,07453h,0726Fh,06465h,06320h,06E6Fh,07274h,06C6Fh
	DW	0422Dh,06572h,06B61h,0F03Ah,02028h,07453h,0726Fh,06465h
	DW	06320h,06972h,06974h,06163h,0206Ch,07265h,06F72h,03A72h
	DW	064F0h,04120h,07264h,07365h,02073h,0666Fh,05420h,06261h
	DW	0656Ch,0F03Ah,0202Dh,0754Eh,0206Dh,06E45h,07274h,06569h
	DW	03A73h,031F0h,04520h,0746Eh,07972h,05420h,06261h,0656Ch
	DW	0F03Ah,020AEh,06150h,07272h,06E65h,02074h,0DC20h,004F0h
	DW	04320h,06968h,0646Ch,004F0h,0DC20h,014F0h,05120h,06975h
	DW	02074h,0DC20h,009F0h,0F020h,0DF0Bh,003F0h,0F020h,0DF0Bh
	DW	013F0h,0F020h,0DF08h,004F0h,01720h
;	396 byte
;******** Resource end   IDPSP *

_DATA	ENDS

_TEXT	SEGMENT

getargv:	; ax = seg
	push	di
	mov	es,ax
	xor	ax,ax
	mov	di,ax
	mov	cx,7FFFh
	cld
    getargv_next:
	repnz	scasb
	jcxz	getargv_end
	cmp	es:[di],al
	jne	getargv_next
	or	ch,80h
	neg     cx
	mov	ax,cx
	inc	ax
    getargv_end:
	pop	di
	ret

putpsp:	push	si
	push	di
	mov	si,ax
	les	bx,dialog
	mov	di,es:[bx+4]
	add	di,0203h
	mov	bx,di
	add	bl,24
	inc	bh
	mov	cx,32
	mov	ax,' '
	call	@scputc
	mov	es,si
	xor	bx,bx
	mov	ax,es:[bx+2Ch]
	call	getargv
	or	ax,ax
	jz	putpsp_end
	mov	bx,di
	add	bl,24
	inc	bh
	mov	cx,1
	push	si
	push	di
	inc	ax
	mov	di,ax
	mov	si,1
    putpsp_argv:
	mov	al,es:[di]
	cmp	al,32
	jb	putpsp_cmd
	call	@scputc
	inc	di
	inc	si
	inc	bl
	cmp	si,32
	jb	putpsp_argv
    putpsp_cmd:
	pop	bx
	pop	es
	push	es
	push	bx
	add	bx,0217h
	mov	cx,30
	mov	al,' '
	call	@scputc
	mov	cx,1
	mov	si,cx
	mov	di,0081h
	cmp	al,es:[di]
	jne	putpsp_cmdl
	inc	di
    putpsp_cmdl:
	mov	al,es:[di]
	cmp	al,32
	jb	putpsp_break
	cmp	si,40
	jnb	putpsp_break
	inc	bl
	inc	si
	inc	di
	call	@scputc
	jmp	putpsp_cmdl
    putpsp_break:
	pop	di
	pop	si
	mov	bx,di
	add	bl,11
	xor	cx,cx
	push	index
	mov	ax,offset format_d
	call	@scputf
	add	sp,2
	xor	bx,bx
	mov	es,si
	push	es:[bx+32h]
	push	es:[bx+36h]
	push	es:[bx+34h]
	push	es:[bx+14h]
	push	es:[bx+12h]
	push	es:[bx+10h]
	push	es:[bx+0Eh]
	push	es:[bx+0Ch]
	push	es:[bx+0Ah]
	push	es:[bx+16h]
	push	es
	push	es:[bx+2Ch]
	push	es:[bx]
	mov	bx,di
	add	bl,24
	mov	ax,offset format_psp
	call	@scputf
	add	sp,26
	mov	es,si
	mov	bx,0018h+20
	mov	cx,20
    putpsp_jft_0:
	dec	bx
	mov	al,es:[bx]
	cbw
	push	ax
	dec	cx
	jnz	putpsp_jft_0
	mov	bx,di
	add	bl,23
	add	bh,14
	mov	ax,offset format_2d
	call	@scputf
	add	sp,20
	inc	bh
	mov	ax,offset format_2d
	call	@scputf
	add	sp,20
    putpsp_end:
	pop	di
	pop	si
	ret

event_parrent:
	mov	ax,index
	inc	ax
	cmp	ax,20
	jnb	event_parrent_end
	mov	index,ax
	add	ax,ax
	mov	bx,offset psp
	add	bx,ax
	mov	ax,[bx]
	cmp	ax,[bx-2]
	je	event_parrent_dec
	call	putpsp
    event_parrent_end:
	mov	ax,_C_NORMAL
	ret
    event_parrent_dec:
	dec	index
	jmp	event_parrent_end

event_child:
	mov	ax,index
	or	ax,ax
	jz	event_child_end
	dec	ax
	mov	index,ax
	mov	bx,offset psp
	add	ax,ax
	add	bx,ax
	mov	ax,[bx]
	call	putpsp
    event_child_end:
	mov	ax,_C_NORMAL
	ret

main:	invoke	rsopen, addr IDPSP_RC
	jnz	main_init
	inc	ax
	ret
    main_init:
	mov	word ptr dialog,ax
	mov	word ptr dialog+2,dx
	push	dx
	push	ax
	mov	ax,_psp
	mov	bx,16h
	mov	cx,20
	mov	si,offset psp
    initpsp_loop:
	mov	[si],ax
	add	si,2
	mov	es,ax
	mov	ax,es:[bx]
	dec	cx
	jnz	initpsp_loop
	call	mouseinit
	call	mouseon
	call	dlshow
	fpmov	es:[bx+1*16].to_proc,event_parrent
	fpmov	es:[bx+2*16].to_proc,event_child
	call	event_parrent
	invoke	dlmodal, dialog
	call	mouseoff
	xor	ax,ax
	ret

_TEXT	ENDS

PPROC	tdedit, a1:DPTR, a2:DWORD, a3:WORD
	ret
PEND	tdedit

	END
