include	doszip/doszip.inc
include	ini.inc
include	string.inc
include	conio.inc
include	mouse.inc

	public	cmtool0
	public	cmtool1
	public	cmtool2
	public	cmtool3
	public	cmtool4
	public	cmtool5
	public	cmtool6
	public	cmtool7
	public	cmtool8
	public	cmtool9
	public	tools_idd

ID_MTOOLS =	4

_DZIP	SEGMENT

stostart:
	lodsb
	cmp al,' '
	je stostart
	cmp al,9
	je stostart
	dec si
	ret

readtools proc
	mov di,si
      @@:
	.if func(inientryid, [bp+4], di)
	    cld
	    mov si,ax
	    push ax
	    call stostart
	    push si
	    mov cx,36
	    .while cx
		lodsb
		.break .if !al
		.if al == ',' || al == '<'
		    .if al == ','
			xor ax,ax
			mov [si-1],al	; terminate text line
			call stostart	; start of command tail
			mov cx,[bp+12]
			les bx,[bp-8]
			les bx,es:[bx.to_data]
			xchg di,bx
			.while cx
			    lodsb
			    .break .if !al || al == ']'
			    .if al == '['
				mov ah,al
				.continue
			    .endif
			    stosb
			    dec cx
			.endw
			mov al,0
			stosb
			mov di,bx
			.if ah
			    les bx,[bp-8]
			    or es:[bx.to_flag],_O_FLAGB
			.endif
		    .endif
		    pop si
		    les bx,[bp-4]
		    mov ax,76
		    mul di
		    add ax,78
		    les bx,es:[bx.dl_wp]
		    add bx,ax
		    .if byte ptr [si] == '<'
			mov cx,38
			mov ax,00C4h
			call @wcputw
			les bx,[bp-8]
			and es:[bx.to_flag],not _O_FLAGB
		    .else
			add bx,4
			mov cx,32
			mov dx,ds
			mov ax,si
			call @wcputs
			les bx,[bp-8]
			mov ax,not _O_STATE
			and es:[bx.to_flag],ax
			invoke strchr, ds::si, '&'
			.if ax
			    mov es,dx
			    mov bx,ax
			    inc bx
			    mov al,es:[bx]
			    les bx,[bp-8]
			    mov es:[bx.to_ascii],al
			    pop si
			    inc si
			    push si
			.endif
		    .endif
		    pop si
		    add word ptr [bp-8],16
		    inc di
		    .if di >= 20
			jmp @F
		    .endif
		    jmp @B
		.endif
		dec cx
	    .endw
	    pop ax
	    pop si
	    push [bp+4]
	    push ax
	    call inierror
	    xor ax,ax
	    ret
	.endif
      @@:
	or di,di
	ret
readtools endp

tools_idd proc pascal uses si di l:word, p:dword, s:dword
local	buf[450]:byte
	.while 1
	    xor si,si
	    mov ax,ID_MTOOLS
	    call open_idd
	    .if !ZERO?
		call readtools
		.if ZERO?
		    mov bx,ID_MTOOLS
		    call close_idd
		    invoke dlclose, [bp-4]
		    .break
		.endif
		les bx,[bp-4]
		mov ax,di
		mov es:[bx.dl_count],al
		add al,2
		mov es:[bx.dl_rect.rc_row],al
		mov ah,al
		mov al,es:[bx.dl_rect.rc_col]
		push ax
		push 0
		pushm es:[bx.dl_wp]
		mov ah,00h
		push ax
		push 0
		call rcframe
		invoke strncpy, addr [bp-450], s, 16
		mov byte ptr [bp-450+16],0
		mov bx,ID_MTOOLS
		call modal_idd
		mov ax,si
		shl ax,4
		les bx,[bp-4]
		add bx,ax
		invoke strncpy, addr [bp-450], es:[bx.to_data], l
		les bx,[bp-4]
		xor ax,ax
		mov al,es:[bx.dl_count]
		push ax
		invoke dlclose, [bp-4]
		pop ax
		.if si && ax >= si
		    lea ax,[bp-450]
		    mov dx,[bp+8]
		    mov cx,[bp+10]
		    .if di == _O_FLAGB
			mov [bp+4],ax
			mov [bp+6],ss
			.continue
		    .endif
		    .if dx
			invoke strcpy, cx::dx, ss::ax
			call msloop
		    .else
			invoke command, ss::ax
			mov si,ax
		    .endif
		.endif
	      ifdef __MOUSE__
		.if func(mousep)
		    mov si,MOUSECMD
		.endif
	      endif
	    .endif
	    .break
	.endw
	mov ax,si
	ret
tools_idd endp

cmtool proc DIST pascal uses si di
local	tool[128]:byte
	invoke inientryid, addr cp_tools, ax
	.if !ZERO?
	    mov si,ax
	    mov al,[si]
	    .if al == '<'
		.if al == '>'
		    jmp @F
		.endif
	    .else
		invoke strchr, ds::si, ','
		mov di,ax
		jz @F
		inc di
		invoke strstart, ds::di
		mov bx,ax
		invoke strncpy, addr [bp-128], dx::bx, 128
		mov bl,[bp-128]
		.if bl == '['
		    mov cx,ax
		    inc ax
		    invoke strcpy, dx::cx, dx::ax
		    invoke strchr, dx::ax, ']'
		    .if ax
		    	mov bx,ax
			mov byte ptr ss:[bx],0
			invoke tools_idd, 128, 0, addr [bp-128]
		    .endif
		.else
		    invoke command, dx::ax
		.endif
	    .endif
	.endif
      @@:
	ret
cmtool	endp

CMTOOLP	macro q
cmtool&q proc DIST
	mov ax,q
	jmp cmtool
cmtool&q endp
	endm

CMTOOLP 0
CMTOOLP 1
CMTOOLP 2
CMTOOLP 3
CMTOOLP 4
CMTOOLP 5
CMTOOLP 6
CMTOOLP 7
CMTOOLP 8
CMTOOLP 9

_DZIP	ENDS

	END
