;
;		ish file converter for MS-DOS  Ver 2.00 (90/3/27)
;
;
;		Copyright (c) 1986, 1987, 1989, 1990  by  M. ishizuka
;		All rights reserved.
;



;-----------------------------------------------;
;						;
;	@@get_hex2				;
;						;
;	INPUT :	si (pointer)			;
;	OUTPUT:	al (value)			;
;		si (next pointer)		;
;	ERROR :	error_01			;
;	BREAK :	flags, al, cx			;
;						;
;						;
;-----------------------------------------------;

@@get_hex2	macro
		lodsb
		call	tolower
		cmp	al, 0dh
		jz	error_01
		sub	al, '0'
		jc	get_hex2_4
		cmp	al, 9 + 1
		jc	get_hex2_1
		sub	al, 'a' - '0'
		jc	get_hex2_4
		cmp	al, 'f' - 'a' + 1
		jnc	get_hex2_4
		add	al, 10d
get_hex2_1:	mov	ch, al
		lodsb
		call	tolower
		cmp	al, 0dh
		jz	get_hex2_3
		sub	al, '0'
		jc	get_hex2_3
		cmp	al, 9 + 1
		jc	get_hex2_2
		sub	al, 'a' - '0'
		jc	get_hex2_3
		cmp	al, 'f' - 'a' + 1
		jnc	get_hex2_3
		add	al, 10d
get_hex2_2:	mov	cl, 4
		shl	ch, cl
		add	al, ch
		jmp	get_hex2_ret

get_hex2_3:	dec	si
		mov	al, cl
		jmp	get_hex2_ret

get_hex2_4:	mov	al, [si-1]
get_hex2_ret:
		endm



;-----------------------------------------------;
;						;
;	@@check_stack				;
;						;
;	INPUT :	sp				;
;	OUTPUT:	flags				;
;	ERROR :	error_10			;
;	BREAK :	NON				;
;						;
;	FUNC:	check stack area		;
;		if short memory then error_10	;
;						;
;-----------------------------------------------;

@@check_stack	macro
		cmp	sp, offset end_of_stack
		jc	error_10
		endm



;-----------------------------------------------;
;						;
;	@@allocate_memory_and_set_buffer_seg	;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_10, error_19		;
;	BREAK : ax, bx, cx, dx, flags		;
;						;
;-----------------------------------------------;

@@allocate_memory_and_set_buffer_seg	macro
		mov	ah, 4ah
		mov	bx, offset end_of_stack_para + 0fc0h + 0fc0h
		int	21h
		jnc	alloc_mem_0
		cmp	ax, 8
		jnz	error_19
		mov	byte ptr shell_escape, 0
alloc_mem_0:	sub	bx, offset end_of_stack_para
		jc	error_19
		shr	bx, 1
		jz	error_10
		mov	dx, ds
		add	dx, offset end_of_stack_para
		mov	word ptr _read_buffer_seg, dx
		cmp	bx, 40h
		jc	alloc_mem_1
		and	bx, 0ffc0h
		mov	ax, 0fc0h
		cmp	bx, ax
		jnc	alloc_mem_2
alloc_mem_1:	mov	ax, bx
alloc_mem_2:	add	dx, ax
		mov	word ptr _write_buffer_seg_1, dx
		mov	word ptr _write_buffer_seg_2, dx
		mov	cl, 4
		shl	ax, cl
		mov	word ptr _read_buffer_length, ax
		mov	word ptr _write_buffer_length_1, ax
		mov	word ptr _write_buffer_length_2, ax
		mov	word ptr _write_buffer_length_3, ax
		mov	word ptr _write_buffer_length_4, ax
		mov	disk_write_length_left, ax
		endm



;-----------------------------------------------;
;						;
;	@@get_dos_version			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	ax				;
;	ERROR :	NON				;
;	BREAK : bx, cx				;
;						;
;-----------------------------------------------;

@@get_dos_version	macro
		mov	ah, 30h
		int	21h			; get dos version
		mov	byte ptr dos_version, al
		endm



;-----------------------------------------------;
;						;
;	@@get_file_name				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	si(input filename pointer)	;
;		dl(switch character)		;
;	ERROR :	NON				;
;	BREAK : flags, ax			;
;						;
;-----------------------------------------------;

@@get_file_name	macro
		mov	ax, 3700h
		int	21h			; get switch character
		mov	byte ptr switch_character, dl
		cld
		mov	si, 81h
		call	skip_space
		endm



;-----------------------------------------------;
;						;
;	@@skip_file_name			;
;						;
;	INPUT :	si(input filename pointer)	;
;		dl(switch character)		;
;	OUTPUT:	si(parameter list pointer)	;
;	ERROR :	NON				;
;	BREAK : flags, al			;
;						;
;-----------------------------------------------;

@@skip_file_name	macro
skip_fname_1:	lodsb
		cmp	al, dl
		jz	skip_fname_ret
		cmp	al, 0dh
		jnz	skip_fname_1

skip_fname_ret:	dec	si
		endm



;-----------------------------------------------;
;						;
;	@@get_file_name2			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	bx(input filename pointer <without path name>)
;		flags				;
;		( ZR : no more path name )	;
;	ERROR :	NON				;
;	BREAK : al, si, di			;
;						;
;-----------------------------------------------;

@@get_file_name2	macro
		mov	di, offset save_input_filename
		mov	si, word ptr save_pathname_2
		call	skip_space
		mov	word ptr save_pathname, si
get_file_name2:	mov	bx, di
get_fname2_1:	lodsb
		cmp	al, byte ptr switch_character
		jz	get_fname2_ret
		cmp	al, 0dh
		jz	get_fname2_ret
		cmp	al, 9h
		jz	get_fname2_ret
		cmp	al, 20h
		jz	get_fname2_ret
		stosb
		cmp	al, '/'
		jz	get_file_name2
		cmp	al, '\'
		jz	get_file_name2
		cmp	al, ':'
		jz	get_file_name2
		cmp	al, 81h
		jc	get_fname2_1
		cmp	al, 0a0h
		jb	get_fname2_2
		cmp	al, 0e0h
		jb	get_fname2_1
		cmp	al, 0fdh
		jnb	get_fname2_1
get_fname2_2:	movsb
		jmp	get_fname2_1

get_fname2_ret:	xor	al, al
		stosb
		dec	si
		mov	word ptr save_pathname_2, si
		cmp	si, word ptr save_pathname
		endm



;-----------------------------------------------;
;						;
;	@@check_parameter			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_01, error_29, error_30	;
;	BREAK : flags, al, bx, cx, dx		;
;						;
;-----------------------------------------------;

@@check_parameter	macro
		mov	al, mode
		or	al, al
		jnz	skip_mode_default
		mov	al, default_mode
		mov	mode, al
skip_mode_default:
		cmp	al, 'r'
		jz	check_restore_parameter
		cmp	al, 'q'
		jz	check_restore_parameter_0
		mov	al, byte ptr lf_flag
		or	al, byte ptr qu_flag
		or	al, byte ptr it_flag
		jnz	error_01
		or	al, byte ptr stdin
		jnz	error_32
		cmp	word ptr title_length, 0
		jnz	skip_title_default
		mov	word ptr title_length, default_title_length - 1
skip_title_default:
		mov	bx, offset msdos_path
		mov	cx, 7
check_os_1:	cmp	byte ptr [bx], 0
		jz	check_os_2
		cmp	byte ptr os, 0
		jnz	error_01
		mov	os, cl
check_os_2:	add	bx, 129d
		loop	check_os_1
		cmp	byte ptr os, 0
		mov	al, '\'
		jz	set_default_os_store
		jmp	check_parameter_ret
;
check_restore_parameter_0:
		@@check_stderr
check_restore_parameter:
		mov	ax, word ptr title_length
		or	al, byte ptr n_flag
		or	al, byte ptr prefix_count
		or	ax, mv_num
		jnz	error_01
		cmp	lf_flag, 0
		jnz	do_lf_patch
		mov	al, 5			; add ax, XXXX
		mov	word ptr lf_flag_patch_1, ax
		mov	byte ptr lf_flag_patch_1[2], al
		jmp	do_lf_patch_1

do_lf_patch:	mov	ax, 0c3f8h		; clc & ret
		mov	word ptr lf_flag_patch_2, ax
do_lf_patch_1:	mov	al, msdos_path
		or	al, cpm_path
		or	al, os9_path
		or	al, unix_path
		or	al, mac_path
		or	al, other_os_path
		or	al, all_os_path
		jnz	check_parameter_ret
		mov	al, '\'
		cmp	mode, 'q'
		jnz	set_default_os_restore
		mov	cpm_path, al
		mov	os9_path, al
		mov	unix_path, al
		mov	mac_path, al
		mov	other_os_path, al
set_default_os_restore:
		mov	all_os_path, al
set_default_os_store:
		mov	msdos_path, al
check_parameter_ret:
		endm



;-----------------------------------------------;
;						;
;	@@disp_start_msg			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, dx			;
;						;
;-----------------------------------------------;

@@disp_start_msg	macro
		@@display	msg_start2
		mov	al, mode
		mov	dx, offset msg_restore
		cmp	al, 'r'
		jz	disp_start_msg_1
;		mov	dx, offset msg_all
;		cmp	al, 'a'
;		jz	disp_start_msg_1
		mov	dx, offset msg_question
		cmp	al, 'q'
		jz	disp_start_msg_1
		mov	dx, offset msg_store_8
		cmp	al, '8'
		jz	disp_start_msg_1
		mov	dx, offset msg_store_8_o
		cmp	al, '8' + 80h
		jz	disp_start_msg_1
		mov	dx, offset msg_store_7
		cmp	al, '7'
		jz	disp_start_msg_1
		mov	dx, offset msg_store_s
		cmp	al, 's'
		jz	disp_start_msg_1
		mov	dx, offset msg_store_s_o
		cmp	al, 's' + 80h
		jz	disp_start_msg_1
		mov	dx, offset msg_store_n
		cmp	al, 'n'
		jz	disp_start_msg_1
		mov	dx, offset msg_old_style
disp_start_msg_1:
		@@display	dx
		cmp	byte ptr msdos_path, 0
		jz	skip_ms_msg
		@@display	m_
skip_ms_msg:	cmp	byte ptr cpm_path, 0
		jz	skip_cpm_msg
		@@display	c_
skip_cpm_msg:	cmp	byte ptr os9_path, 0
		jz	skip_os9_msg
		@@display	os9_
skip_os9_msg:	cmp	byte ptr unix_path, 0
		jz	skip_unix_msg
		@@display	unix_
skip_unix_msg:	cmp	byte ptr mac_path, 0
		jz	skip_mac_msg
		@@display	mac_
skip_mac_msg:	cmp	byte ptr other_os_path, 0
		jz	skip_other_msg
		@@display	question_
skip_other_msg:	cmp	byte ptr all_os_path, 0
		jz	skip_all_msg
		@@display	aster_
skip_all_msg:	@@display	msg_
		endm



;-----------------------------------------------;
;						;
;	@@check_prefix				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_20			;
;	BREAK : flags, ax, bx, cx, dx, si	;
;						;
;-----------------------------------------------;

@@check_prefix	macro
		cmp	byte ptr prefix_count, 0
		jz	check_prefix_ret
		xor	ah, ah
		mov	cx, 7
		mov	si, offset prefix_char
check_prefix_1:	lodsb
		mov	bx, ax
		cmp	byte ptr ds:[bx][offset prefix_table], 0
		loopnz	check_prefix_1
		jnz	error_20
		mov	bl, al
		@@display	msg_prefix_1
		mov	byte ptr prefix_count, bl
		@@dispchr	bl
		@@display msg_prefix_2
		mov	si, offset prefix_table
		mov	dl, 0
check_prefix_2:	lodsb
		push	dx
		or	al, al
		jz	check_prefix_4
		cmp	dl, 21h
		jc	make_visible
		cmp	dl, '\'
		jz	make_visible
		cmp	dl, 7fh
		jc	check_prefix_3
		cmp	dl, 0a1h
		jc	make_visible
		cmp	dl, 0e0h
		jc	check_prefix_3
make_visible:	@@hex_disp	dl
		jmp	check_prefix_4

check_prefix_3:	@@dispchr	dl
check_prefix_4:	pop	dx
		inc	dl
		jnz	check_prefix_2
		@@display	msg_prefix_3
check_prefix_ret:
		endm



;-----------------------------------------------;
;						;
;	@@open_input_file_and_get_file_size	;
;						;
;	INPUT :	dx(input filenam)		;
;	OUTPUT:	NON				;
;	ERROR :	error_03, error_04, error_05,	;
;		error_06			;
;	BREAK : flags, ax, bx, cx, dx		;
;						;
;-----------------------------------------------;

@@open_input_file_and_get_file_size	macro
		mov	ax, 3d00h
		int	21h		; open input_file
		jnc	open_infile_1
		cmp	al, 2
		jnz	error_04
		jmp	error_03

open_infile_1:	mov	read_handler, ax
		mov	bx, ax
		mov	ax, 4202h
		xor	cx, cx
		xor	dx, dx
		push	bx
		push	cx
		int	21h		; get input_file size
		jc	error_05
		pop	cx
		pop	bx
		mov	word ptr work_table[2], ax
		mov	word ptr work_table[4], dx
		or	ax, dx
		jz	error_06
		mov	ax, 4200h
		xor	dx, dx
		int	21h		; move file pointer (rewind)
		jc	error_05
		endm



;-----------------------------------------------;
;						;
;	@@check_cpu_and_patch			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, cx			;
;						;
;-----------------------------------------------;

@@check_cpu_and_patch	macro
		cmp	not_v30, 0
		jnz	non_v30
		mov	cx, 33d
		mov	ax, 1
		rcr	ax, cl
		jc	non_v30		; if 80186 or 80286 then non_v30
		xor	cl, cl
		push	cs
        	db      0fh, 33h, 0c9h	; if V30	  ext cl, cl
					; else if uPD9002 xor cx, cx (?)
					; else		  pop cs ; xor cx, cx
		dec	cl
		jz	v30
non_v30:	mov	ax, offset emulate_ext_dl_5
		mov	address_emu_ext_dl_5, ax
		mov	ax, offset emulate_ext_dl_6
		mov	address_emu_ext_dl_6, ax
		mov	ax, offset emulate_ext_dl_12
		mov	address_emu_ext_dl_12, ax
		mov	ax, offset emulate_ext_dl_13
		mov	address_emu_ext_dl_13, ax
		mov	ax, offset emulate_ext_dl_14
		mov	address_emu_ext_dl_14, ax
		mov	ax, offset emulate_ins_dl_5
		mov	address_emu_ins_dl_5, ax
		mov	ax, offset emulate_ins_dl_6
		mov	address_emu_ins_dl_6, ax
		mov	ax, offset emulate_ins_dl_12
		mov	address_emu_ins_dl_12, ax
		mov	ax, offset emulate_ins_dl_13
		mov	address_emu_ins_dl_13, ax
		mov	ax, offset emulate_ins_dl_14
		mov	address_emu_ins_dl_14, ax
		mov	ax, 16ffh	; call [xxxx]
		mov	word ptr ext_dl_5_1, ax
		mov	word ptr ext_dl_5_2, ax
		mov	word ptr ext_dl_5_3, ax
		mov	word ptr ext_dl_6_1, ax
		mov	word ptr ext_dl_6_2, ax
		mov	word ptr ext_dl_6_3, ax
		mov	word ptr ext_dl_6_4, ax
		mov	word ptr ext_dl_6_5, ax
		mov	word ptr ext_dl_12_1, ax
		mov	word ptr ext_dl_12_2, ax
		mov	word ptr ext_dl_13_1, ax
		mov	word ptr ext_dl_14_1, ax
		mov	word ptr ins_dl_5_1, ax
		mov	word ptr ins_dl_5_2, ax
		mov	word ptr ins_dl_5_3, ax
		mov	word ptr ins_dl_6_1, ax
		mov	word ptr ins_dl_6_2, ax
		mov	word ptr ins_dl_6_3, ax
		mov	word ptr ins_dl_6_4, ax
		mov	word ptr ins_dl_6_5, ax
		mov	word ptr ins_dl_12_1, ax
		mov	word ptr ins_dl_12_2, ax
		mov	word ptr ins_dl_13_1, ax
		mov	word ptr ins_dl_13_2, ax
		mov	word ptr ins_dl_14_1, ax
		mov	ax, offset address_emu_ext_dl_5
		mov	word ptr ext_dl_5_1[2], ax
		mov	word ptr ext_dl_5_2[2], ax
		mov	word ptr ext_dl_5_3[2], ax
		mov	ax, offset address_emu_ext_dl_6
		mov	word ptr ext_dl_6_1[2], ax
		mov	word ptr ext_dl_6_2[2], ax
		mov	word ptr ext_dl_6_3[2], ax
		mov	word ptr ext_dl_6_4[2], ax
		mov	word ptr ext_dl_6_5[2], ax
		mov	ax, offset address_emu_ext_dl_12
		mov	word ptr ext_dl_12_1[2], ax
		mov	word ptr ext_dl_12_2[2], ax
		mov	ax, offset address_emu_ext_dl_13
		mov	word ptr ext_dl_13_1[2], ax
		mov	ax, offset address_emu_ext_dl_14
		mov	word ptr ext_dl_14_1[2], ax
		mov	ax, offset address_emu_ins_dl_5
		mov	word ptr ins_dl_5_1[2], ax
		mov	word ptr ins_dl_5_2[2], ax
		mov	word ptr ins_dl_5_3[2], ax
		mov	ax, offset address_emu_ins_dl_6
		mov	word ptr ins_dl_6_1[2], ax
		mov	word ptr ins_dl_6_2[2], ax
		mov	word ptr ins_dl_6_3[2], ax
		mov	word ptr ins_dl_6_4[2], ax
		mov	word ptr ins_dl_6_5[2], ax
		mov	ax, offset address_emu_ins_dl_12
		mov	word ptr ins_dl_12_1[2], ax
		mov	word ptr ins_dl_12_2[2], ax
		mov	ax, offset address_emu_ins_dl_13
		mov	word ptr ins_dl_13_1[2], ax
		mov	word ptr ins_dl_13_2[2], ax
		mov	ax, offset address_emu_ins_dl_14
		mov	word ptr ins_dl_14_1[2], ax
v30:		mov	sp, offset end_of_stack
		endm



;-----------------------------------------------;
;						;
;	@@write					;
;						;
;	INPUT :	cx (length)			;
;		dx (pointer)			;
;	OUTPUT:	ax (length of write)		;
;		flags (NC: ok)			;
;		      (CY: end of file or error);
;	ERROR :	error_33			;
;	BREAK :	flags				;
;						;
;-----------------------------------------------;

@@write		macro
		xor	ax, ax
		push	es
		push	si
		push	di
		push	dx
		mov	si, 0
_write_buffer_seg_1 equ	$-2
		mov	es, si
		mov	si, dx
_write_1:	push	cx
		cmp	disk_write_length_left, 0
		jnz	_write_3
		push	ax
		push	bx
		xor	dx, dx
		mov	disk_write_ptr, dx
		mov	cx, 1000h
_write_buffer_length_1	equ	$-2
		mov	disk_write_length_left, cx
		mov	bx, write_handler
		mov	ah, 40h
		push	ds
		push	es
		pop	ds
		int	21h
		pop	ds
		jc	_write_2
		cmp	ax, cx
		jnc	_write_2
		cmp	byte ptr error_33_flag, 0
		jnz	error_33
		stc
_write_2:	pop	bx
		pop	ax
		pop	cx
		jc	_write_5
		jmp	_write_1

_write_3:	sub	cx, ax
		sub	disk_write_length_left, cx
		jnc	_write_4
		add	cx, disk_write_length_left
		mov	disk_write_length_left, 0
_write_4:	add	ax, cx
		mov	di, disk_write_ptr
		rep	movsb
		mov	disk_write_ptr, di
		pop	cx
		cmp	disk_write_length_left, 0
		jz	_write_1
_write_5:	pop	dx
		pop	di
		pop	si
		pop	es
		endm



;-----------------------------------------------;
;						;
;	@@expand_wildcards			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	flags				;
;		( CY : not found any more )	;
;	ERROR :	error_22, error_23		;
;	BREAK :	ax, bx, cx, dx, si, di		;
;						;
;-----------------------------------------------;

@@expand_wildcards	macro
		cmp	byte ptr _dta_id, 1
		jz	expand_wildcards_0
		mov	ah, 1ah
		mov	dx, offset _dta
		int	21h			; set DTA
		mov	byte ptr _dta_id, 1
expand_wildcards_0:
		cmp	byte ptr wildcards_expand_flag, 0
		jnz	expand_wildcards_3
expand_wildcards_1:
		@@get_file_name2
		jz	expand_wildcards_2
		mov	word ptr save_input_filename_without_path, bx
		mov	ah, 4eh
		mov	dx, offset save_input_filename
		mov	cx, 6			; system file & hidden file
		int	21h			; search first file
		jnc	expand_wildcards_4
		call	error_22
		jmp	expand_wildcards_1

expand_wildcards_2:
		cmp	byte ptr wildcards_expand_flag, 0
		stc
		jnz	expand_wildcards_ret
		jmp	error_23

expand_wildcards_3:
		mov	ah, 4fh
		int	21h			; search next file
		jc	expand_wildcards_1
expand_wildcards_4:
		mov	byte ptr wildcards_expand_flag, 0ffh
		mov	si, offset _dta[1eh]
		mov	di, word ptr save_input_filename_without_path
expand_wildcards_5:
		lodsb
		call	tolower
		cmp	al, 81h
		jc	expand_wildcards_7
		cmp	al, 0a0h
		jc	expand_wildcards_6
		cmp	al, 0e0h
		jc	expand_wildcards_7
		cmp	al, 0fdh
		jnc	expand_wildcards_7
expand_wildcards_6:
		stosb
		lodsb
expand_wildcards_7:
		stosb
		or	al, al
		jnz	expand_wildcards_5
expand_wildcards_ret:
		endm



;-----------------------------------------------;
;						;
;	@@all_close				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_05, error_09		;
;	BREAK :	flags, ax, bx			;
;						;
;-----------------------------------------------;

@@all_close	macro
		mov	bx, read_handler
		or	bx, bx
		jz	skip_close_inputfile
		mov	ah, 3eh
		int	21h
		jc	error_05
		mov	word ptr read_handler, 0
skip_close_inputfile:
		mov	bx, write_handler
		cmp	bx, 2
		jc	skip_close_outputfile
		mov	ah, 3eh
		int	21h
		jc	error_09
		mov	word ptr write_handler, 0
skip_close_outputfile:
		endm



;-----------------------------------------------;
;						;
;	@@init_constants2			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK :	NON				;
;						;
;-----------------------------------------------;

@@init_constants2	macro
		mov	word ptr max_division, 0
		mov	word ptr disk_read_length, 0
		endm



;-----------------------------------------------;
;						;
;	@@disp_input_fname			;
;						;
;	INPUT :	dx (filename)			;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, si			;
;						;
;-----------------------------------------------;

@@disp_input_fname	macro
		push	dx
		mov	si, dx
		mov	ah, 0
disp_input_fname_1:
		call	lodsb_tolower_new
		or	al, al
		jz	disp_input_fname_2
		@@dispchr	al
		jmp	disp_input_fname_1

disp_input_fname_2:
		@@display	msg_colon2
		pop	dx
		endm



;-----------------------------------------------;
;						;
;	@@check_stdin				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, bx, dx		;
;						;
;-----------------------------------------------;

@@check_stdin	macro
		mov	ax, 4400h
		mov	bx, 0
		int	21h
		jc	error_29
		and	dx, 81h
		cmp	dx, 81h
		jz	error_31
		mov	byte ptr stdin, 0ffh
		endm



;-----------------------------------------------;
;						;
;	@@check_stdout				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, bx, dx		;
;						;
;-----------------------------------------------;

@@check_stdout	macro
		mov	ax, 4400h
		mov	bx, 1
		int	21h
		jc	error_29
		mov	ax, dx
		and	dx, 82h
		cmp	dx, 82h
		jz	check_stdout_1
		mov	byte ptr stdout, 0ffh
check_stdout_1:	test	al, 80h
		jz	check_stdout_2
		test	al, 20h
		jnz	check_stdout_2
		xor	ah, ah
		mov	byte ptr error_33_flag, al
check_stdout_2:
		endm



;-----------------------------------------------;
;						;
;	@@check_stderr				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, bx, dx		;
;						;
;-----------------------------------------------;

@@check_stderr	macro
		mov	ax, 4400h
		mov	bx, 2
		int	21h
		jc	error_29
		and	dx, 83h
		cmp	dx, 83h
		jnz	error_30
		endm



;-----------------------------------------------;
;						;
;	@@disp_and_set_stdout			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, bx, dx		;
;						;
;-----------------------------------------------;

@@disp_and_set_stdout	macro
		cmp	byte ptr stdout, 0
		jz	disp_and_set_stdout_skip
		@@display	msg_stdout
		mov	word ptr write_handler, 1
disp_and_set_stdout_skip:
		endm



;-----------------------------------------------;
;						;
;	@@get_ishopt_fname			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_34			;
;	BREAK : flags, ax, cx, si, di		;
;						;
;-----------------------------------------------;

@@get_ishopt_fname	macro
		mov	si, offset ishopt
		call	search_env
		jz	get_ishopt_fname_1
		cmp	byte ptr es:dos_version, 3
		jc	get_ishopt_fname_2
		inc	si
		lodsw
		cmp	ax, 1
		jnz	get_ishopt_fname_2
		call	get_ishopt_fname_sub
		mov	word ptr [di-4], 'po'
		mov	byte ptr [di-2], 't'
		jmp	get_ishopt_fname_2

get_ishopt_fname_1:
		call	get_ishopt_fname_sub
get_ishopt_fname_2:
		mov	ax, es
		mov	ds, ax
		endm



;-----------------------------------------------;
;						;
;	@@cmp_tag				;
;						;
;	INPUT :	bx (handle)			;
;	OUTPUT:	flags (ZR: compare OK)		;
;		      (NZ: compare NG)		;
;	ERROR :	error_38			;
;	BREAK : flags, ax, cx, dx, si		;
;						;
;-----------------------------------------------;

@@cmp_tag	macro
		mov	di, offset tag
		cmp	byte ptr [di], 0
		jnz	cmp_tag_1
		mov	di, offset default_tag
		mov	ah, byte ptr mode
		or	ah, ah
		jz	cmp_tag_1
		mov	byte ptr default_tag_mode, 'r'
		cmp	ah, 'r'
		jz	cmp_tag_1
		cmp	ah, 'q'
		jz	cmp_tag_1
		mov	byte ptr default_tag_mode, 's'
cmp_tag_1:	scasb
		jnz	cmp_tag_ret
		cmp	al, ':'
		jz	cmp_tag_ret
		call	get_para_char
		jc	cmp_tag_ret
		jmp	cmp_tag_1

cmp_tag_ret:
		endm



;-----------------------------------------------;
;						;
;	@@get_parameter				;
;						;
;	INPUT :	si(parameter list pointer)	;
;		dl(switch character)		;
;	OUTPUT:	di(input filename end pointer)	;
;	ERROR :	error_01, error_15, error_34,	;
;		error_35, error_36, error_37,	;
;		error_38			;
;	BREAK : flags, ax, bx, cx, si, bp	;
;						;
;-----------------------------------------------;

@@get_parameter	macro
		call	get_parameter
		push	di
		cmp	byte ptr tag, ':'
		jz	get_parameter_ret
		push	dx
		@@get_ishopt_fname
		mov	ax, 3d00h
		mov	dx, offset ishopt_fname
		int	21h
		jc	ishopt_open_error
		mov	bx, ax
search_tag:	call	get_para_char
		jc	tag_not_found
		cmp	al, ' '+1
		jc	skip_until_crlf_1
		cmp	al, '#'
		jz	skip_until_crlf
		@@cmp_tag
		jz	copy_para_to_buffer
skip_until_crlf:
		call	get_para_char
		jc	tag_not_found
skip_until_crlf_1:
		cmp	al, 0dh
		jz	search_tag
		cmp	al, 0ah
		jz	search_tag
		jmp	skip_until_crlf

ishopt_open_error:
		cmp	byte ptr tag, 0
		jnz	error_35
		jmp	get_parameter_ret_0

tag_not_found:	cmp	byte ptr tag, 0
		jnz	error_37
get_parameter_ret_0:
		pop	dx
		jmp	get_parameter_ret

copy_para_to_buffer:
		mov	di, offset write_buffer
copy_para_to_buffer_1:
		call	get_para_char
		jc	copy_para_to_buffer_end
		cmp	al, 0dh
		jz	copy_para_to_buffer_2
		cmp	al, 0ah 
		jz	copy_para_to_buffer_2
		stosb
		cmp	di, offset write_buffer + 80d*72d
		jnc	error_36
		jmp	copy_para_to_buffer_1

copy_para_to_buffer_3:
		call	get_para_char
		jc	copy_para_to_buffer_end
		cmp	al, 0dh
		jz	copy_para_to_buffer_2
		cmp	al, 0ah
		jz	copy_para_to_buffer_2
		jmp	copy_para_to_buffer_3

copy_para_to_buffer_2:
		call	get_para_char
		jc	copy_para_to_buffer_end
		cmp	al, '#'
		jz	copy_para_to_buffer_3
		cmp	al, 0dh
		jz	copy_para_to_buffer_2
		cmp	al, 0ah
		jz	copy_para_to_buffer_2
		cmp	al, ' '+1
		jc	copy_para_to_buffer_1
		jmp	copy_para_to_buffer_end

copy_para_to_buffer_end:
		mov	al, 0dh
		stosb
		mov	ah, 3eh
		int	21h
		jc	ishopt_open_error
		mov	byte ptr tag, ':'
		mov	si, offset write_buffer
		pop	dx
		call	get_parameter
get_parameter_ret:
		pop	di
		endm



;-----------------------------------------------;
;						;
;	@@set_int_23h				;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	NON				;
;	BREAK : flags, ax, dx			;
;						;
;-----------------------------------------------;

@@set_int_23h	macro
		mov	ax, 2523h
		mov	dx, offset int_23h_handler
		int	21h
		call	set_break_on
		endm



;-----------------------------------------------;
;						;
;	@@int_23h_handler			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	abend				;
;	BREAK : NON				;
;						;
;-----------------------------------------------;

@@int_23h_handler	macro
int_23h_handler:
		cmp	byte ptr cs:abort_flag, 0ffh
		jz	int_23h_handler_ret
		mov	ax, cs
		mov	ds, ax
		mov	es, ax
		mov	byte ptr abort_flag, 0ffh
		mov	ax, 3301h
		mov	dl, 0
		int	21h
		cmp	byte ptr mode, 'r'
		jz	int_23h_handler_1
		cmp	byte ptr mode, 'q'
		jz	int_23h_handler_1
		jmp	abend

int_23h_handler_1:
		cmp	word ptr write_handler, 1
		jbe	abend
		call	close_and_delete
		jmp	abend

int_23h_handler_ret:
		iret
		endm



;-----------------------------------------------;
;						;
;	@@set_shell_name			;
;						;
;	INPUT :	NON				;
;	OUTPUT:	NON				;
;	ERROR :	error_29, error_43		;
;	BREAK : flags, ax, bx, cx, dx, si, di	;
;						;
;-----------------------------------------------;

@@set_shell_name	macro
		cmp	byte ptr shell_escape, 0
		jz	set_shell_name_ret
		cmp	byte ptr es:dos_version, 3
		jc	set_shell_name_3
		cmp	byte ptr mode, 'q'
		jnz	set_shell_name_3
		cmp	byte ptr stdout, 0
		jnz	set_shell_name_3
		mov	ax, 4400h
		mov	bx, 0
		int	21h
		jc	error_29
		and	dx, 81h
		cmp	dx, 81h
		jnz	set_shell_name_3
		mov	si, offset comspec
		call	search_env
		jnz	set_shell_name_2
		mov	cx, 128d
		mov	di, offset shell
set_shell_name_1:
		lodsb
		stosb
		cmp	al, 0
		loopnz	set_shell_name_1
		mov	ax, es
		mov	ds, ax
		jnz	error_43
		jmp	set_shell_name_ret

set_shell_name_2:
		mov	ax, es
		mov	ds, ax
set_shell_name_3:
		mov	byte ptr shell_escape, 0
set_shell_name_ret:
		endm
