; *****************************************************************************
;
; CMD_INTR.ASM  [ TRDOS Command Interpreter Procedure ]
; 18/07/2010 'mkdir' command completed (except singlix fs)
; 10/07/2010 'mkdir' command recognition
; 26/06/2010 'rmdir' command for Singlix FS is OK! 
; 20/06/2010 'rmdir' command Singlix FS recognition and some modification
; 13/06/2010 'rmdir' command bugfixes
; 05/06/2010 'rmdir' command (derivation from 'del' command procedures)
; 23/05/2010 'attrib' command (derivation from P2000.ASM, 17/12/2000)
; 16/05/2010 'del' command write fault message
; 09/05/2010 'del' command modification for Singlix FS 
; 23/04/2010 'del' command permission denied bugfix (push ds, pop es)
; 18/04/2010 'del' command Singlix FS recognition (not completed)
; 12/04/2010 'del' command completed (except Singlix FS)
; 28/02/2010 'del' command recognition
; 24/01/2010 'longname' singlix fs longname modification
; 07/12/2009 'run' error codes are in al (they were in ah) 
; 06/12/2009 'dir' command and 'show' command includes path
; 05/12/2009
; 29/11/2009 'run' command includes file path (directory)
; 14/11/2009
; 11/11/2009 'run' command bugfix from proc_create_psp
; 08/11/2009 'show' command in temporary format
; 04/11/2009 'run' command in temporary format
; 25/10/2009 'mem' command ('memory')
; 19/10/2009 'cd' command error return fix
; 18/10/2009 'longname' command
; 16/10/2009 'cd' command '..' modification
; 12/10/2009 'cd' command '.','..' modification
; 03/10/2009 'cd' command modification
; 22/09/2009
; 15/09/2009
; Copyright (C) 2005-2010 Erdogan TAN
; 29/01/2005
;
; TRDOS.ASM (include CMD_INTR.ASM)
;
; *****************************************************************************

command_interpreter proc near

cmp_cmd_dir:
                mov byte ptr [Program_Exit],0
                mov cx,3
                mov si, offset CommandBuffer
                mov di, offset Cmd_Dir
get_char_dir:
                mov al, byte ptr [SI]
                inc si
		scasb
                jne cmp_cmd_cd
		loop get_char_dir
                mov al, byte ptr [SI]
                cmp al, 20h
                ja loc_cmd_failed

                mov word ptr [AttributesMask], 0800h

                ; 06/12/2009
                mov al, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], al

get_dfname_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_dfname_fchar
                jb loc_print_dir_call_all
                cmp al, '-'
                jne short loc_print_dir_call_flt
get_next_attr_char:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_next_attr_char
                jb loc_cmd_failed
                and al, 0DFh
                cmp al, 'D'
                jne short pass_only_directories
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                ja loc_cmd_failed
                or byte ptr [AttributesMask], 10h
                jmp short get_dfname_fchar_attr
pass_only_directories:
                cmp al, 'F'
                jne check_attr_s
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                ja loc_cmd_failed
                or byte ptr [AttributesMask]+1, 10h

get_dfname_fchar_attr:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_dfname_fchar_attr
                jb short loc_print_dir_call_all

loc_print_dir_call_flt:
                ; 06/12/2009
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jnc short loc_print_dir_change_drv_1
                cmp al, 1
                jne loc_run_cmd_failed
                dec si
                cmp byte ptr [SI], ':'
                jne loc_cmd_failed
                dec si
                mov dl, byte ptr [SI]
                and dl, 0DFh
                sub dl, 'A'
                jmp short loc_print_dir_change_drv_2
loc_print_dir_change_drv_1:
                mov dl, byte ptr [FindFile_Drv]
loc_print_dir_change_drv_2:
                cmp dl, byte ptr [RUN_CDRV]
                je short loc_print_dir_change_directory 
                call proc_change_current_drive
                jc loc_run_cmd_failed
loc_print_dir_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short pass_print_dir_change_directory

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc loc_run_cmd_failed
loc_print_dir_change_prompt_dir_string:
                call proc_change_prompt_dir_string

pass_print_dir_change_directory:
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                ja short loc_print_dir_call

loc_print_dir_call_all:
                push si
                mov word ptr [SI], ".*"
                inc si
                inc si
                mov byte ptr [SI], "*"
                inc si
                mov byte ptr [SI], 0
                pop si

loc_print_dir_call:
                call proc_print_directory

                ; 29/11/2009  -> Restore current directory                
                push ds
                pop es

                mov bh, byte ptr [RUN_CDRV] ; it is set at the beginning
                                            ; of the 'run' command.
                cmp bh, byte ptr [Current_Drv]
                je short loc_print_dir_call_restore_cdir_retn
                mov dl, bh
                call proc_change_current_drive 
                retn
loc_print_dir_call_restore_cdir_retn:
                cmp byte ptr [Restore_CDIR], 0
                jna short pass_print_dir_call_restore_cdir_retn
                xor bl, bl
                mov si, offset Logical_DOSDisks
                add si, bx
 
                call proc_restore_current_directory

pass_print_dir_call_restore_cdir_retn:
                retn

check_attr_s_cap:
                and al, 0DFh
check_attr_s:
                cmp al, 'S'
                jne short pass_attr_s
                or byte ptr [AttributesMask], 4
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je get_dfname_fchar_attr
                jb short loc_print_dir_call_all
                and al, 0DFh
pass_attr_s:
                cmp al, 'H'
                jne short pass_attr_h
                or byte ptr [AttributesMask], 2
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je get_dfname_fchar_attr
                jb loc_print_dir_call_all
                jmp short check_attr_s_cap
pass_attr_h:
                cmp al, 'R'
                jne short pass_attr_r
                or byte ptr [AttributesMask], 1
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je get_dfname_fchar_attr
                jb loc_print_dir_call_all
                jmp short check_attr_s_cap
pass_attr_r:
                cmp al, 'A'
                jne loc_cmd_failed
                or byte ptr [AttributesMask], 20h
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je get_dfname_fchar_attr
                jb loc_print_dir_call_all
                jmp short check_attr_s_cap

AttributesMask: dw 0

cmp_cmd_cd:
                mov si, offset CommandBuffer
                mov ax, word ptr [SI]
                cmp ax, 'DC'
                jne cmp_cmd_drive
                add si, 2
                cmp byte ptr [SI], 20h
                ja loc_cmd_failed
get_char_cd:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_char_cd
                jb loc_cmd_cd_return
get_next_cd_char:
                mov ah, byte ptr [SI]+1
                cmp ah, ':'
                jne short loc_cd_change_directory
                mov ah, byte ptr [SI]+2
                cmp ah, 20h
                ja loc_cmd_failed
                and al, 0DFh
                sub al, 'A'
                jc loc_cmd_failed
                mov dl, al 
                call proc_change_current_drive
                jc short loc_cd_drive_not_ready
                retn

loc_cd_change_directory:
                ; 29/11/2009
                ; AH = CDh   ; to separate 'CD' command from others
                             ; for restoring current directory
                             ; 0CDh sign is for saving cdir into 
                             ; DOS drv description table cdir area
                
                mov ah, 0CDh ; mov byte ptr [CD_COMMAND], 0CDh 

                ; 16/10/2009
               	call proc_change_current_directory
                jnc loc_cd_change_cdir_string
              
loc_cd_error_messages:
                cmp al, 3
                je short loc_cd_path_not_found
                cmp al, 15h 
                je short loc_cd_drive_not_ready
                cmp al, 18h ; Bad request structure length 
                je short loc_cd_command_failed
                cmp al, 1Ah ; Unknown media type, non-DOS disk
                je short loc_cd_command_failed
loc_cd_path_not_found:
                ; 19/10/2009
                ;
                push ax                
                ;
                mov si, offset Msg_Dir_Not_Found
                call proc_printmsg
                ;
                pop ax
                cmp ah, byte ptr [Current_Dir_Level]
                jnb short loc_cd_change_cdir_string
                mov byte ptr [Current_Dir_Level], ah 

loc_cd_change_cdir_string:
                call proc_change_prompt_dir_string
                retn

loc_cd_command_failed:
                ; 19/10/2009
                mov si, offset Msg_Bad_Command
                call proc_printmsg
                jmp short loc_cd_fail_drive_restart

loc_cd_drive_not_ready:
                mov si, offset Msg_Not_Ready_Read_Err
                call proc_printmsg
                ;
loc_cd_fail_drive_restart:
                 ; 19/10/2009                 
                mov dl, byte ptr [Current_Drv]
                call proc_change_current_drive
loc_cmd_cd_return:
                retn

cmp_cmd_drive:
                ; C:, D:, E: etc.
                cmp ah, ':'
                jne short cmp_cmd_ver
                mov ah, byte ptr [SI]+2
                cmp ah, 20h
                ja loc_cmd_failed
                and al, 0DFh
                sub al, 'A'
                jc loc_cmd_failed
                cmp al, byte ptr [Last_Dos_DiskNo]
                ja short loc_cd_drive_not_ready
                mov dl, al
                call proc_change_current_drive
                jc short loc_cd_drive_not_ready
                retn

cmp_cmd_ver:
		mov  cx,3
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Ver
get_char_ver:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short cmp_cmd_exit
                loop get_char_ver
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja loc_cmd_failed
                mov si, offset Program_Version
                call proc_printmsg
                retn
cmp_cmd_exit:
                mov  cx,4
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Exit
get_char_exit:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short cmp_cmd_prompt
                loop get_char_exit
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja loc_cmd_failed
                mov byte ptr [Program_Exit], 1
                retn
cmp_cmd_prompt:
                mov  cx,6
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Prompt
get_char_prompt:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short cmp_cmd_volume
                loop get_char_prompt
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_prompt_name_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_prompt_name_fchar
                ja short loc_change_prompt_label
                mov si, offset TRDOSPromptLabel
                mov word ptr [SI], "RT"
                inc si
                inc si
                mov word ptr [SI], "OD"
                inc si
                inc si
                mov byte ptr [SI], "S"
                inc si
                mov byte ptr [SI], 0
loc_cmd_prompt_return:
                retn
loc_change_prompt_label:
                ; push ds
                ; pop es
                mov cx, 11
                mov di, offset TRDOSPromptLabel
put_char_new_prompt_label:
                lodsb
                cmp al, 20h
                jb short pass_put_new_prompt_label
                stosb
                loop put_char_new_prompt_label
pass_put_new_prompt_label:
                mov byte ptr [DI], 0
                retn
cmp_cmd_volume:
                mov  cx,6
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Volume
get_char_volume:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short check_vol_str
                loop get_char_volume
                mov  al, byte ptr [SI]
                cmp  al,20h
                ja   loc_cmd_failed
get_vol_drive_name_fchar:
                inc si
get_vol_drive_name_fchar1:
                mov al, byte ptr [SI]
                cmp al, 20h
                ja short pass_vol_current_drive
                je short get_vol_drive_name_fchar
                mov al, byte ptr [Current_Drv]
                jmp short loc_get_volume_size_info
check_vol_str:
                cmp SI, 4
                jne short cmp_cmd_longname
                cmp al,20h
                ja  loc_cmd_failed
                jmp short get_vol_drive_name_fchar1
pass_vol_current_drive:
                cmp al, 'A'
                jb  loc_cmd_failed
                cmp al, 'z'
                ja  loc_cmd_failed
                cmp al, 'Z'
                jna short pass_vol_drv_capitalize
                cmp al, 'a'
                jb  loc_cmd_failed
                and al, 0DFh
pass_vol_drv_capitalize:
                inc si
                mov ah, byte ptr [SI]
                cmp ah, ":"
                jne loc_cmd_failed
                sub al, 'A'
loc_get_volume_size_info:
                call proc_print_volume_info
                jc loc_cd_drive_not_ready
                retn
cmp_cmd_longname:
                mov  cx,8
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_LongName
get_char_longname:
                lodsb
		scasb
                jne  short cmp_cmd_date
                loop get_char_longname
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_longname_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_longname_fchar
                jb short loc_longname_retn
                call proc_find_longname
                jnc short loc_print_longname
               
                or al, al
                jz short loc_longname_not_found
                  
                cmp al, 15h
                je loc_cd_drive_not_ready
               ;cmp al, 2
               ;je short loc_ln_file_dir_not_found
               ;cmp al, 3
               ;je short loc_ln_file_dir_not_found
               ;jmp short loc_ln_file_dir_not_found                
                
               ;cmp al, 8
               ;jne short pass_long_name_im_error
               ;mov si, offset Msg_Insufficient_Memory
               ;call proc_printmsg
               ;retn
;pass_long_name_im_error:

loc_ln_file_dir_not_found:
                mov si, offset Msg_File_Directory_Not_Found
                call proc_printmsg
                retn
loc_longname_not_found:
                mov si, offset Msg_Longname_Not_Found
                call proc_printmsg
                retn
loc_print_longname:
                ; 19/10/2009
               ;mov si, offset LongFileName
                mov di, offset TextBuffer
                push di 
               ; 24/01/2010
                cmp al, 0
                ja short loc_print_longname_1
loc_print_FS_longname: 
                lodsb
                stosb  
                or al, al
                jnz short loc_print_FS_longname
                jmp short loc_print_longname_2
               ; 
loc_print_longname_1:
                lodsw
                stosb  
                or al, al
                jnz short loc_print_longname_1
loc_print_longname_2:               
                pop si
                call proc_printmsg
                mov si, offset NextLine
                call proc_printmsg
loc_longname_retn:
                retn
               
cmp_cmd_date:
                mov  cx,4
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Date
get_char_date:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short cmp_cmd_time
                loop get_char_date
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
                call proc_show_date
                call proc_set_date
                retn
cmp_cmd_time:
                mov  cx,4
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Time
get_char_time:
                mov  al, byte ptr [SI]
		inc  si
		scasb
              ; jne  short cmp_cmd_show
                jne  short cmp_cmd_cls
                loop get_char_time
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
                call proc_show_time
                call proc_set_time
                retn

cmp_cmd_cls:
                mov  cx,3
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_cls
get_char_cls:
                mov  al, byte ptr [SI]
		inc  si
		scasb
               ;jne  loc_cmd_failed
                jne  short cmp_cmd_mem
                loop get_char_cls
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed

                call proc_clear_screen
retn_from_cls:
                retn

cmp_cmd_mem:
		mov  cx,3
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Mem
get_char_mem:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  short cmp_cmd_show
                loop get_char_mem
                mov  al, byte ptr [SI]
		cmp  al,20h
                jna short loc_print_mem_info
                cmp al, 'O'
                jne loc_cmd_failed
                inc si
                mov ax, word ptr [SI]
                cmp ax, 'YR'
                jne loc_cmd_failed
                inc si
                inc si
                cmp byte ptr [SI], 20h
                ja loc_cmd_failed
loc_print_mem_info:
                call proc_print_memory_info
                retn

cmp_cmd_show:
                mov  cx,4
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_show
get_char_show:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  cmp_cmd_del
                loop get_char_show
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_showfile_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_showfile_fchar
                ja short loc_show_parse_path_name
                retn

loc_show_parse_path_name:
                ; 06/12/2009
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_show_check_filename_exists:
                ; 06/12/2009
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed 

                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

loc_show_change_drv:
                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_show_change_directory

                call proc_change_current_drive
                jc loc_file_rw_cmd_failed

loc_show_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_findload_showfile

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc loc_file_rw_cmd_failed

loc_show_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_findload_showfile:
                mov si, offset FindFile_Name
                xor ax, ax
                call proc_open_file
                jc loc_file_rw_cmd_failed

loc_start_show_file:
                ; 06/12/2009
                mov word ptr [PSP_Address], ax

                mov es, ax
                mov di, OF_Direntry
                add di, DirEntry_FileSize
 
                mov ax, word ptr ES:[DI]
                mov dx, word ptr ES:[DI]+2
 
                push ax
 
               ;mov word ptr [Show_FileSize], ax
               ;mov word ptr [Show_FileSize]+2, dx  

	        mov si, offset nextline
	        call proc_printmsg

                mov si, 100h
                 
                mov ah, 0Fh ; Get Video Display Mode
                int 10h
                ;AH= Number of columns
                ;AL= Video mode
                ;BH= Current video page
                mov word ptr [Program_VideoMode], AX
                mov byte ptr [Program_VideoPage], BH

                pop bx

               ;mov bx, word ptr [Show_FileSize]
	       ;mov dx, word ptr [Show_FileSize]+2
		mov cx, 22
                jmp short pass_show_wait_for_key
loop_show_file_char:
                and cx, cx
                jnz short pass_show_wait_for_key
		xor ah, ah
		int 16h
		cmp al, 1Bh
		jnz short pass_exit_show
end_of_show_file:
pass_show_file:
		mov si, offset nextline
		call proc_printmsg

                ; 14/11/2009
                mov ax, word ptr [PSP_Address]
                call proc_close_file

loc_show_file_restore_retn:
               ; 28/02/2010
               jmp loc_file_rw_restore_retn

;Show_FileSize: dd 0

pass_exit_show:
		mov cx, 20
pass_show_wait_for_key:
		mov al, byte ptr ES:[SI]
		cmp al, 0Dh
		jnz short pass_show_dec_cx
		dec cx
pass_show_dec_cx:
		cmp al, 09h
		jnz short pass_put_tab_space
		mov al, 20h
pass_put_tab_space:
                push bx
                mov ah, 0Eh
                mov BH, byte ptr [Program_VideoPage]
                mov BL, byte ptr [Program_CharColorAtr]
		int 10h
                pop bx
compare_bx_file_size:
		inc si
		jnz short pass_show_update_si_es
		mov ax, es
		add ax, 1000h
		mov es, ax
pass_show_update_si_es:
                or bx, bx
		jz short check_dx_show_file
                dec bx
		jmp short loop_show_file_char
check_dx_show_file:
                dec bx
		or dx, dx
                jz short end_of_show_file 
                dec dx
		jmp short loop_show_file_char

cmp_cmd_del:
                mov  cx,3
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_del
get_char_del:
                mov  al, byte ptr [SI]
		inc  si
		scasb
               ;jne  cmp_cmd_rename
                jne cmp_cmd_rmdir
                loop get_char_del
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_delfile_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_delfile_fchar
                jb short loc_delfile_nofilename_retn

loc_delfile_parse_path_name:
                ; 28/02/2010
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_delfile_check_filename_exists:
                ; 28/02/2010
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed
                mov word ptr [DelFile_FNPointer], si 

loc_delfile_drv:
                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_delfile_change_directory

                call proc_change_current_drive
                jc loc_file_rw_cmd_failed

loc_delfile_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_delfile_find

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc loc_file_rw_cmd_failed

loc_delfile_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_delfile_find:
	       ;mov si, offset FindFile_Name
 		mov si, word ptr [DelFile_FNPointer]
                mov ax, 1800h ; Except volume label and dirs
                call proc_find_first_file
                jc loc_file_rw_cmd_failed

loc_delfile_ambgfn_check:
                cmp bx, 0 ; Ambiguous filename chars used sign (BX>0)
                jna short loc_delfile_found
                mov al, 2 ; File not found sign
                stc
                jmp loc_file_rw_cmd_failed   

loc_delfile_nofilename_retn:
                retn

loc_delfile_found:
                and cl, 07h ; Attributes
               ;20/06/2010 
                jnz loc_rename_permission_denied

loc_delfile_found_check_lnel:
                
               ; 03/04/2010
               ;mov al, ch ; If the Long name is yes/existing, ch > 0 
               ;or al, al
                or ch, ch
                jz short pass_delfile_save_lnel

loc_delfile_save_lnel:
               ; mov al, byte ptr [LongName_EntryLength]
                mov ch, byte ptr [LongName_EntryLength]
pass_delfile_save_lnel:
               ; mov byte ptr [DelFile_LNEL], al
                mov byte ptr [DelFile_LNEL], ch
                ; di = Directory Entry Offset (DirBuff)
                ; si = Directory Entry (FFF Structure)
               ; mov word ptr [DelFile_DirEntryAddr], di ; not required
               ; mov ax, word ptr ES:[DI]+26 ; First Cluster Low Word
               ; mov dx, word ptr ES:[DI]+20 ; First Cluster High Word
               ; cmp dx, 0
               ; ja short pass_delfile_fc_check
               ; ROOT Dir First Cluster = 0
               ; cmp ax, 2
               ; jb loc_update_direntry_1
pass_delfile_fc_check:
                mov si, offset Msg_DoYouWantDelete
                call proc_printmsg
                mov si, word ptr [DelFile_FNPointer]
                call proc_printmsg
                mov si, offset Msg_YesNo
                call proc_printmsg
loc_delfile_ask_again:
                xor ah, ah
                int 16h
                cmp al, 0Bh
                je short loc_do_not_delete_file
                and al, 0DFh
 		mov byte ptr [Y_N_nextLine], al
                cmp al, 'Y'
                je short loc_yes_delete_file
                cmp al, 'N'
                jne short loc_delfile_ask_again
loc_do_not_delete_file:
                mov si, offset Y_N_nextline
                call proc_printmsg
               ; 23/05/2010
                jmp loc_file_rw_restore_retn

loc_del_fs_file:
               ; 09/05/2010
                xor bl, bl
                mov bh, byte ptr [FS_BuffDrvName]
                sub bh, 'A'  
                mov si, offset Logical_DosDisks
                add si, bx

                cmp byte ptr [SI][LD_FSType], 0A1h
                jne loc_rename_permission_denied

                mov ax, word ptr [DelFile_FCluster]
                mov dx, word ptr [DelFile_FCluster]+2  
               ;
               ;28/02/2010
                call proc_delete_fs_file
                jnc loc_print_deleted_message
                ; temporary
               ; jmp loc_rename_permission_denied
                jmp loc_file_rw_cmd_failed

loc_yes_delete_file:
                mov si, offset Y_N_nextline
                call proc_printmsg
loc_del_short_name:
               ; 03/04/2010
  	       ; ES:DI -> Directory buffer entry offset/address 
                mov ax, word ptr ES:[DI]+26 ; First Cluster Low Word
                mov dx, word ptr ES:[DI]+20 ; First Cluster High Word

                mov word ptr [DelFile_FCluster], ax
                mov word ptr [DelFile_FCluster]+2, dx

                ; 28/02/2010
 		push word ptr [DirBuff_EntryCounter]
                pop word ptr [DelFile_EntryCounter]

                ; 09/05/2010
                cmp word ptr ES:[DI]+DirEntry_NTRes, 01A1h
                je short loc_del_fs_file
                ;

                ; 28/03/2010
              	mov byte ptr ES:[DI], 0E5h  ; Deleted sign
		;mov byte ptr [DirBuff_ValidData], 2

                ; 18/04/2010 
		call proc_save_directory_buffer
                jc loc_file_rw_cmd_failed
;                jnc short loc_del_short_name_continue 
;                cmp al, 0Dh
;                jne short loc_del_short_name_stc_jmp
;loc_del_short_name_check_fs:
;                cmp cx, 0A100h ; Singlix FS sign
;                je short loc_del_short_name_continue
loc_del_short_name_stc_jmp:
;                stc
;                jmp loc_file_rw_cmd_failed
                ; 
loc_del_short_name_continue:
                xor bl, bl
		mov bh, byte ptr [FindFile_Drv]
                mov si, offset Logical_DOSDisks
                add si, bx

                xor bh, bh
               
                mov ax, word ptr [DelFile_FCluster]
                mov dx, word ptr [DelFile_FCluster]+2              
               
               ; 09/05/2010 
                cmp byte ptr [SI][LD_FATtype], 1
                jnb short loc_del_short_name_check_cluster_no

                mov ax, 0Bh ; Invalid Format
                jmp loc_file_rw_cmd_failed
               ;                    
loc_del_short_name_check_cluster_no:
               ; 04/04/2010
                mov cx, dx
                or cx, ax
                jz short loc_del_long_name
                mov cx, bx ; 0

              ; mov byte ptr [FAT_BuffValidData], 0
                mov word ptr [FAT_ClusterCounter],bx ; 0
                mov word ptr [FAT_ClusterCounter]+2,cx ; 0
unlink_file_clusters:
		call proc_update_cluster
                ; DX:AX = Next Cluster
                ; CX:BX = Cluster Value
                jnc short unlink_file_clusters
pass_unlink_file_clusters:
                mov byte ptr [DelFile_FATErr], al
                cmp byte ptr [FAT_BuffValidData], 2
                jne short pass_save_fat_buffer
                call proc_save_fat_buffer
                jnc short pass_save_fat_buffer
                mov byte ptr [DelFile_FATErr], al ; Error
              ; mov byte ptr [FAT_BuffValidData], 0
pass_save_fat_buffer:
	       ; 04/04/2010 
		mov ax, word ptr [FAT_ClusterCounter]
                mov dx, word ptr [FAT_ClusterCounter]+2
                xor bx, bx
                inc bl  ; 1 = Add DX:AX to free space count
                dec bh  ; FFh = DS:SI is ready as Drive DT Offset
               ; DS:SI -> Logical Dos Drive Description Table
                call proc_calculate_FAT_freespace
                pushf
                cmp byte ptr [DelFile_FATErr], 0
                ja short unlink_file_clusters_error
                popf
                jc loc_file_rw_cmd_failed
                jmp short loc_del_long_name
    
unlink_file_clusters_error:
                popf
                xor ah, ah
                mov al, byte ptr [DelFile_FATErr]
                stc
                jmp loc_file_rw_cmd_failed

loc_del_long_name:
               ; 03/04/2010 
                xor dh, dh
                mov dl, byte ptr [DelFile_LNEL]
                or dl, dl
               ;jz short loc_print_deleted_message
               ; 12/06/2010
                jz short loc_del_update_parent_dir_lm_date              

                mov ax, word ptr [DelFile_EntryCounter]
                sub ax, dx
                jc loc_file_rw_cmd_failed
 
               ; AX = Directory Entry Number of the long name last entry
                call proc_delete_longname
               ;jc short loc_file_rw_cmd_failed

loc_del_update_parent_dir_lm_date:
              ; 13/06/2010 BugFix 
              ; 12/06/2010
                mov al,  byte ptr [Current_Dir_Level]
                or al, al
                jz loc_print_deleted_message
               ; 18/07/2010
                mov byte ptr [DelFile_cdirlevel], al
                push word ptr [Current_Dir_FCluster]
                pop word ptr [DelFile_CDirFCluster]
                push word ptr [Current_Dir_FCluster]+2
                pop word ptr [DelFile_CDirFCluster]+2
               ; 12/06/2010 
                dec al
                push ds
                pop es
                mov cx, 12
                mov si, offset Path_Array

               ; 13/06/2010
                mov byte ptr [Current_Dir_Level], al
                or al, al
                jnz short loc_del_update_parent_dir_lm_date_load_sub_dir_1
                cmp byte ptr [Current_FATType], 2
                ja short loc_del_update_parent_dir_lm_date_load_sub_dir_2
                xor ah, ah
                xor dx, dx
                jmp short loc_del_update_parent_dir_lm_date_load_sub_dir_3
         
loc_del_update_parent_dir_lm_date_load_sub_dir_1:
              ; 13/06/2010
                mov ah, 16
                mul ah 
                add si, ax
loc_del_update_parent_dir_lm_date_load_sub_dir_2:                
                mov ax, word ptr [SI]+12 ; Parent Dir First cluster LW
                mov dx, word ptr [SI]+14 ; Parent Dir First cluster HW
loc_del_update_parent_dir_lm_date_load_sub_dir_3:
               ; 13/06/2010
                mov word ptr [Current_Dir_FCluster], ax
                mov word ptr [Current_Dir_FCluster]+2, dx               

                add si, 16
                mov di, offset Dir_File_Name                
                rep movsb

                mov si, offset Logical_DosDisks
                mov bh, byte ptr [Current_Drv]
                xor bl, bl 
                add si, bx
                call proc_reload_current_directory
                jc short loc_print_deleted_message_restore_cdirlevel

loc_del_update_parent_dir_lm_time_locate_dir: 
                mov si, offset Dir_File_Name        
                xor cx, cx
                mov ax, 0810h ; Only directories
                call proc_locate_current_dir_file
               ; ES:DI = DirBuff Directory Entry Address
                jc short loc_print_deleted_message_restore_cdirlevel

                call proc_convert_current_date_time
                mov word ptr ES:[DI]+18, dx ; Last Access Date
                mov word ptr ES:[DI]+24, dx ; Last Write Date
                mov word ptr ES:[DI]+22, ax ; Last Write Time

                ;mov byte ptr [DirBuff_ValidData], 2
		call proc_save_directory_buffer
                ;jc short loc_print_deleted_message_restore_cdirlevel

loc_print_deleted_message_restore_cdirlevel:
               ; 18/07/2010 current directory level restoration 
                mov al, byte ptr [DelFile_cdirlevel]
                mov byte ptr [Current_Dir_Level], al
                push word ptr [DelFile_CDirFCluster]
                pop word ptr [Current_Dir_FCluster]
                push word ptr [DelFile_CDirFCluster]+2
                pop word ptr [Current_Dir_FCluster]+2

loc_print_deleted_message:
                mov si, offset Msg_Deleted
                call proc_printmsg
 
               ; clc 

loc_file_rw_restore_retn:
                ; 28/02/2010
loc_file_rw_cmd_failed:
                ; 28/02/2010
                pushf 
                ; loc_run_cmd_failed  
                ; 07/12/2009 -> error codes are in al (they were in ah)
                ; 06/12/2009 -> Restore Current Drive
                ; 29/11/2009 -> Restore Current Directory
                push ds
                pop es
 
                push ax

                mov bh, byte ptr [RUN_CDRV] ; it is set at the beginning
                                            ; of the 'run' command.
                cmp bh, byte ptr [Current_Drv]
                je short loc_file_rw_restore_cdir
                mov dl, bh
                call proc_change_current_drive 
                jmp short loc_file_rw_err_pass_restore_cdir

loc_file_rw_restore_cdir:
                cmp byte ptr [Restore_CDIR], 0
                jna short loc_file_rw_err_pass_restore_cdir
 		xor bl, bl
                mov si, offset Logical_DOSDisks
                add si, bx
                call proc_restore_current_directory

loc_file_rw_err_pass_restore_cdir:
                pop ax
                popf
                jc loc_file_rw_check_write_fault
                retn
; 16/05/2010
loc_file_rw_check_write_fault:
                cmp al, 1Dh ; Write Fault
                jne loc_run_cmd_failed_cmp_al
                mov si, offset Msg_Not_Ready_Write_Err
                call proc_printmsg
                retn

DelFile_FNPointer: dw 0
;28/03/2010
;DelFile_DirEntryAddr: dw 0
DelFile_FCluster: dd 0
; 28/02/2010
DelFile_EntryCounter: dw 0
DelFile_LNEL: db 0
; 04/04/2010
DelFile_FATErr: db 0
; 18/07/2010
DelFile_cdirlevel: db 0
DelFile_CDirFCluster: dd 0

; temporary 28/02/2010
loc_rename_permission_denied:
 		mov si, offset Msg_Permission_denied
		call proc_printmsg
               ; 23/05/2010 
                jmp short loc_file_rw_restore_retn

cmp_cmd_rmdir:
               ; 05/06/2010
                mov  cx,5
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_rmdir
get_char_rmdir:
                mov  al, byte ptr [SI]
		inc  si
		scasb
               ;jne  cmp_cmd_rename
                jne cmp_cmd_attrib
                loop get_char_rmdir
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_rmdir_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_rmdir_fchar
                jb short loc_rmdir_nodirname_retn

loc_rmdir_parse_path_name:
                ; 28/02/2010
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_rmdir_check_dirname_exists:
                ; 28/02/2010
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed
                mov word ptr [DelFile_FNPointer], si 

loc_rmdir_drv:
                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_rmdir_change_directory

                call proc_change_current_drive
                jc loc_file_rw_cmd_failed

loc_rmdir_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_rmdir_find_directory

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc short loc_rmdir_check_error_code

loc_rmdir_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_rmdir_find_directory:
	       ;mov si, offset FindFile_Name
 		mov si, word ptr [DelFile_FNPointer]
                mov ax, 0810h ; Only directories
                call proc_find_first_file
                jnc short loc_rmdir_ambgfn_check
loc_rmdir_check_error_code:
               ; 05/06/2010
                cmp al, 2
                je short loc_rmdir_directory_not_found
                stc
                jmp loc_file_rw_cmd_failed
                 
loc_rmdir_ambgfn_check:
                cmp bx, 0 ; Ambiguous filename chars used sign (BX>0)
                jna short loc_rmdir_directory_found
loc_rmdir_directory_not_found:
               ; 05/06/2010 
                mov si, offset Msg_Dir_Not_Found
                call proc_printmsg

                jmp loc_file_rw_restore_retn

loc_rmdir_nodirname_retn:
                retn

loc_rmdir_directory_found:
                and cl, 07h ; Attributes
               ; 20/06/2010 
                jnz loc_rename_permission_denied

loc_rmdir_found_check_lnel:
               ;mov al, ch ; If the Long name is yes/existing, ch > 0 
               ;or al, al
                or ch, ch
                jz short pass_rmdir_save_lnel

loc_rmdir_save_lnel:
               ; mov al, byte ptr [LongName_EntryLength]
                mov ch, byte ptr [LongName_EntryLength]
pass_rmdir_save_lnel:
               ; mov byte ptr [DelFile_LNEL], al
                mov byte ptr [DelFile_LNEL], ch
                ; di = Directory Entry Offset (DirBuff)
                ; si = Directory Entry (FFF Structure)
               ; mov word ptr [DelFile_DirEntryAddr], di ; not required
               ; mov ax, word ptr ES:[DI]+26 ; First Cluster Low Word
               ; mov dx, word ptr ES:[DI]+20 ; First Cluster High Word
               ; cmp dx, 0
               ; ja short pass_rmdir_fc_check
               ; ROOT Dir First Cluster = 0
               ; cmp ax, 2
               ; jb loc_update_direntry_1
pass_rmdir_fc_check:
                mov si, offset Msg_DoYouWantRmDir
                call proc_printmsg
                mov si, word ptr [DelFile_FNPointer]
                call proc_printmsg
                mov si, offset Msg_YesNo
                call proc_printmsg
loc_rmdir_ask_again:
                xor ah, ah
                int 16h
                cmp al, 0Bh
                je short loc_do_not_delete_directory
                and al, 0DFh
 		mov byte ptr [Y_N_nextLine], al
                cmp al, 'Y'
                je short loc_rmdir_yes_delete_directory
                cmp al, 'N'
                jne short loc_rmdir_ask_again
loc_do_not_delete_directory:
                mov si, offset Y_N_nextline
                call proc_printmsg
               ; 23/05/2010
                jmp loc_file_rw_restore_retn

loc_rmdir_delete_fs_directory:
               ;20/06/2010
                cmp byte ptr [SI][LD_FSType], 0A1h
                jne loc_rename_permission_denied

                call proc_delete_fs_directory
                jnc loc_print_deleted_message

               ; 26/06/2010
                or ax, ax
                jz loc_rmdir_directory_not_empty_2         
                stc
                jmp loc_file_rw_cmd_failed

loc_rmdir_yes_delete_directory:
                mov si, offset Y_N_nextline
                call proc_printmsg

loc_rmdir_delete_short_name_check_dir_empty:
  	       ; ES:DI -> Directory buffer entry offset/address 
                mov ax, word ptr ES:[DI]+26 ; First Cluster Low Word
                mov dx, word ptr ES:[DI]+20 ; First Cluster High Word

                mov word ptr [DelFile_FCluster], ax
                mov word ptr [DelFile_FCluster]+2, dx

 		push word ptr [DirBuff_EntryCounter]
                pop word ptr [DelFile_EntryCounter]

               ; 20/06/2010
                xor bl, bl
		mov bh, byte ptr [FindFile_Drv]
                mov si, offset Logical_DOSDisks
                add si, bx

                cmp word ptr ES:[DI]+DirEntry_NTRes, 01A1h
                je short loc_rmdir_delete_fs_directory

               ;cmp byte ptr [SI][LD_FATtype], 1
               ;jnb short loc_rmdir_get_last_cluster
               ;mov ax, 0Bh ; Invalid Format
               ;jmp loc_file_rw_cmd_failed
  
loc_rmdir_get_last_cluster:
               ; 20/06/2010 
                push word ptr [DirBuff_Cluster]
                pop word ptr [RmDir_ParentDirCluster]

                push word ptr [DirBuff_Cluster]+2
                pop word ptr [RmDir_ParentDirCluster]+2

               ;push es
               ;pop word ptr [RmDir_DirEntrySegment]
                mov word ptr [RmDir_DirEntryOffset], di

                call proc_get_last_cluster
                jc loc_file_rw_cmd_failed
                cmp ax, word ptr [DelFile_FCluster]
                jne short loc_rmdir_multi_dir_clusters
                cmp dx, word ptr [DelFile_FCluster]+2
                jne short loc_rmdir_multi_dir_clusters
                mov byte ptr [RmDir_MultiClusters], 0
                jmp short pass_rmdir_multi_dir_clusters
 
loc_rmdir_multi_dir_clusters:
                mov byte ptr [RmDir_MultiClusters], 1
pass_rmdir_multi_dir_clusters:
                mov word ptr [RmDir_DirLastCluster], ax
                mov word ptr [RmDir_DirLastCluster]+2, dx
               ;12/06/2010 
                mov word ptr [RmDir_PreviousCluster], cx
                mov word ptr [RmDir_PreviousCluster]+2, bx
loc_rmdir_load_fat_sub_directory:
                call proc_load_FAT_sub_directory
                jc loc_file_rw_cmd_failed

loc_rmdir_find_last_dir_entry:
                push si
                mov si, offset Dir_File_Name
                mov byte ptr [SI], "*"
                mov byte ptr [SI]+8, "*"
                xor bx, bx ; Entry offset  = 0
loc_rmdir_find_last_dir_entry_next:
                mov ax, 0800h ; Except volume/long names
                xor cx, cx ; 0 = Find a valid file or dir name
                call proc_find_direntry  
                jc short loc_rmdir_empty_dir_cluster
                cmp bx, 1
                ja short loc_rmdir_directory_not_empty_1
loc_rmdir_dot_entry_check:
                cmp ch, '.' ; The first char of the dir entry
                jne short loc_rmdir_directory_not_empty_1
                or bl, bl
                jnz short pass_rmdir_dot_entry_check
                cmp byte ptr ES:[DI]+1, 20h
                jne short loc_rmdir_directory_not_empty_1
                inc bx
                jmp short loc_rmdir_find_last_dir_entry_next  

pass_rmdir_dot_entry_check:
                cmp word ptr ES:[DI]+1, ' .'
                jne short loc_rmdir_directory_not_empty_1 
                inc bx
                jmp short loc_rmdir_find_last_dir_entry_next  

loc_rmdir_directory_not_empty_1:
                pop ax ; pushed si 
loc_rmdir_directory_not_empty_2:
                mov si, offset Msg_Dir_Not_Empty
                call proc_printmsg
                jmp loc_file_rw_restore_retn

loc_rmdir_empty_dir_cluster:
                pop si

               ; 12/06/2010
loc_rmdir_set_prev_cluster_dir_last_cluster:
                cmp byte ptr [RmDir_MultiClusters], 0
                jna short loc_rmdir_unlink_dir_last_cluster

                mov ax, word ptr [RmDir_PreviousCluster]
                mov dx, word ptr [RmDir_PreviousCluster]+2
                xor cx, cx
                dec cx
               ; 13/06/2010
                mov bx,cx ; FFFFh
                call proc_update_cluster
                jnc short loc_rmdir_unlink_dir_last_cluster

loc_rmdir_unlink_stc_retn:
                cmp ax, 1
                cmc 
                jc loc_file_rw_cmd_failed
              ; 13/06/2010
                jmp short loc_rmdir_save_fat_buffer 
loc_rmdir_unlink_stc_retn_0Bh:
                mov ax, 0Bh ; Invalid format
                stc
                jmp loc_file_rw_cmd_failed
 
loc_rmdir_unlink_dir_last_cluster:
                mov ax, word ptr [RmDir_DirLastCluster]
                mov dx, word ptr [RmDir_DirLastCluster]+2
                xor cx, cx
                xor bx, bx
                call proc_update_cluster
                jnc short loc_rmdir_unlink_stc_retn_0Bh 
                ; Because of it is the last cluster
                ; uc procedure must return with eocc error 
                or ax, ax
                jz short loc_rmdir_save_fat_buffer ; eocc                
                stc
                jmp loc_file_rw_cmd_failed
                 
loc_rmdir_save_fat_buffer:
                cmp byte ptr [FAT_BuffValidData], 2
                jne short pass_rmdir_save_fat_buffer
                call proc_save_fat_buffer
                jc loc_file_rw_cmd_failed
pass_rmdir_save_fat_buffer:
	        mov ax, 1
                xor dx, dx
                mov bx, ax ; 1
              ;  bl=1 -> Add DX:AX to free space count
                dec bh  ; FFh = DS:SI is ready as Drive DT Offset
               ; DS:SI -> Logical Dos Drive Description Table
                call proc_calculate_FAT_freespace
                jc loc_file_rw_cmd_failed

                cmp byte ptr [RmDir_MultiClusters], 0
                jna short loc_rmdir_delete_short_name_continue

                mov ax, word ptr [DelFile_FCluster]
                mov dx, word ptr [DelFile_FCluster]+2       
              
                jmp loc_rmdir_get_last_cluster

loc_rmdir_delete_short_name_invalid_data:
                mov al, 0Dh ; Invalid data
                xor ah, ah
                stc
                jc loc_file_rw_cmd_failed
                          
loc_rmdir_delete_short_name_continue:
                mov ax, word ptr [RmDir_ParentDirCluster]
                mov dx, word ptr [RmDir_ParentDirCluster]+2
              ; 13/06/2010
                cmp ax, 2
                jnb short loc_rmdir_delete_short_name_cont_load_sub_dir
                or dx, dx
                jnz short loc_rmdir_delete_short_name_cont_load_sub_dir
                call proc_load_FAT_root_directory
                jc loc_file_rw_cmd_failed
                jmp short loc_rmdir_del_short_name_cont_ld_check_fclust

loc_rmdir_delete_short_name_cont_load_sub_dir:                
                call proc_load_FAT_sub_directory
                jc loc_file_rw_cmd_failed

loc_rmdir_del_short_name_cont_ld_check_fclust:                
                mov es, bx  
                mov di, word ptr [RmDir_DirEntryOffset]

                mov ax, word ptr ES:[DI]+26 ; First Cluster Low Word
                mov dx, word ptr ES:[DI]+20 ; First Cluster High Word
               ; 12-06-2010 Not necessary, if procedure is in solidstate 
                cmp ax, word ptr [DelFile_FCluster]
                jne short loc_rmdir_delete_short_name_invalid_data
                cmp dx, word ptr [DelFile_FCluster]+2
                jne short loc_rmdir_delete_short_name_invalid_data

                mov byte ptr ES:[DI], 0E5h ; Deleted sign
	       ;mov byte ptr [DirBuff_ValidData], 2
		call proc_save_directory_buffer
                jc loc_file_rw_cmd_failed 
               ;                    

loc_rmdir_del_long_name:
                xor dh, dh
                mov dl, byte ptr [DelFile_LNEL]
                or dl, dl
               ;12/06/2010
                jz loc_del_update_parent_dir_lm_date
             
                mov ax, word ptr [DelFile_EntryCounter]
                sub ax, dx
                jc loc_file_rw_cmd_failed
 
               ; AX = Directory Entry Number of the long name last entry
                call proc_delete_longname
               ;jc short loc_file_rw_cmd_failed

               ; 12/06/2010
                jmp loc_del_update_parent_dir_lm_date

; 06/06/2010
RmDir_ParentDirCluster: dd 0
RmDir_DirlastCluster: dd 0
;12/06/2010
RmDir_PreviousCluster: dd 0
; 06/06/2010
;RmDir_DirEntrySegment: dw 0
RmDir_DirEntryOffset: dw 0
RmDir_MultiClusters: db 0

cmp_cmd_attrib:
                ; 23/05/2010
                ; Derivation from P2000.ASM
		; 17/12/2000
                mov cx,6
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Attrib
get_char_attrib:
                mov al, byte ptr [SI]
                inc si
		scasb
               ; 10/07/2010 
                jne cmp_cmd_mkdir
               ; 
                loop get_char_attrib
                mov al, byte ptr [SI]
                cmp al, 20h
                ja loc_cmd_failed
                xor ax, ax
                mov word ptr [Attr_Chars], ax
                mov byte ptr [Attributes], al
loop_scan_attrib_fparam:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                ja short loop_scan_attrib_params
                je short loop_scan_attrib_fparam
               ; 23/05/2010
loc_attr_file_nofilename_retn:
                retn
loop_scan_attrib_params:
                cmp al, '-'
                ja  loc_attr_file_parse_path_name
                je  short loc_attr_space
                cmp al, '+'
                jne loc_cmd_failed
loc_attr_space:
                mov ah, byte ptr [SI]+1 
                cmp ah, 20h
                ja  short pass_attr_space
                jb  loc_cmd_failed
loc_attr_space_inc:
                inc si
                jmp short loc_attr_space

pass_attr_space:
                and ah, 0DFh
                cmp ah, 'S'
                ja  loc_cmd_failed
                jb  short pass_attr_system
                mov ah, 04h   ; System
                jmp short pass_attr_archive
pass_attr_system:
                cmp ah, 'H'
                ja  short pass_attr_hidden
                jb  short pass_attr_read_only
                mov ah, 02h    ; Hidden
                jmp short pass_attr_archive
pass_attr_hidden:
                cmp ah, 'R'
                ja  loc_cmd_failed
                jb  short pass_attr_read_only ; Read only
                mov ah, 01h
                jmp short pass_attr_archive
pass_attr_read_only:
                cmp ah, 'A'
                jne short loc_chk_attr_enter
                mov ah, 20h    ; Archive
pass_attr_archive:
                cmp al, '-'
                jne short pass_reducing_attributes
                or  byte ptr [Attr_Chars], ah
                jmp short loc_change_attributes_inc
pass_reducing_attributes:
                or  byte ptr [Attr_Chars]+1, ah
loc_change_attributes_inc:
                inc si
                mov ah, byte ptr [SI]+1
                cmp ah, 20h
                jb  short pass_change_attr
                je  short loc_change_attributes_inc
                cmp ah, '-'
                ja  short loc_chk_next_attr_char1
                je  short loc_chk_next_attr_char0
                cmp ah, '+'
                jne short loc_chk_next_attr_char1
loc_chk_next_attr_char0:
                inc si
                mov ax, word ptr [SI]
                jmp short pass_attr_space
loc_chk_next_attr_char1:
                cmp byte ptr [SI], '-'
                ja  short pass_attr_space
                jmp short loc_attr_file_check_fname_fchar
loc_chk_attr_enter:
                cmp ah, 0Dh
                jne loc_cmd_failed
pass_change_attr:
                mov al, byte ptr [Attr_Chars]
                not al
                and byte ptr [Attributes], al
                mov al, byte ptr [Attr_Chars]+1
                or  byte ptr [Attributes], al
loc_show_attributes:
		mov si, offset nextline
		call proc_printmsg
loc_show_attributes_no_nextline:
                mov word ptr [Attr_Chars], 'ON'
                mov word ptr [Attr_Chars]+2, 'MR'
                mov word ptr [Attr_Chars]+4, 'LA'
                mov si, offset Attr_Chars
                mov al, byte ptr [Attributes]
                test al, 04h
                jz short pass_put_attr_s
                mov word ptr [SI], 0053h     ; S
                inc si
pass_put_attr_s:
                test al, 02h
                jz short pass_put_attr_h
                mov word ptr [SI], 0048h     ; H
                inc si
pass_put_attr_h:
                test al, 01h
                jz short pass_put_attr_r
                mov word ptr [SI], 0052h     ; R
                inc si
pass_put_attr_r:
                cmp al, 20h
                jb short pass_put_attr_a
                mov word ptr [SI], 0041h     ; A
pass_put_attr_a:
                mov si, offset Str_Attributes
		call proc_printmsg
		mov si, offset nextline
		call proc_printmsg
                jmp loc_file_rw_restore_retn 

loc_attr_file_check_fname_fchar:
                inc si
                cmp byte ptr [SI], 20h
                je short loc_attr_file_check_fname_fchar
                jb short pass_change_attr
                                   
loc_attr_file_parse_path_name:
                ; 28/02/2010
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_attr_file_check_filename_exists:
                ; 28/02/2010
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed
                mov word ptr [DelFile_FNPointer], si 

loc_attr_file_drv:
                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_attr_file_change_directory

                call proc_change_current_drive
                jc loc_file_rw_cmd_failed

loc_attr_file_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_attr_file_find

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc loc_file_rw_cmd_failed

loc_attr_file_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_attr_file_find:
	       ;mov si, offset FindFile_Name
 		mov si, word ptr [DelFile_FNPointer]
                mov ax, 0800h ; Except volume labels
                call proc_find_first_file
                jc loc_file_rw_cmd_failed

loc_attr_file_ambgfn_check:
                cmp bx, 0 ; Ambiguous filename chars used sign (BX>0)
                jna short loc_attr_file_found
                mov al, 2 ; File not found sign
                stc
                jmp loc_file_rw_cmd_failed   

loc_attr_file_found:
               ; 23/05/2010
  	       ; ES:DI -> Directory buffer entry offset/address
               ; CL = File (or Directory) Attributes
               ; mov cl, byte ptr ES:[DI]+0Bh
                
                cmp word ptr [Attr_Chars], 0
                ja short loc_attr_file_change_attributes
                mov byte ptr [Attributes], cl
                jmp loc_show_attributes

loc_attr_file_change_attributes:
               ; 23/05/2010 
               ; 17/12/2000
                mov al, byte ptr [Attr_Chars]
                not al
                and cl, al
                mov al, byte ptr [Attr_Chars]+1
                or  cl, al

               ; 23/05/2010
                cmp word ptr ES:[DI]+DirEntry_NTRes, 01A1h ; Singlix FS
                je short loc_attr_file_fs_check

                mov byte ptr [Attributes], cl
                mov byte ptr ES:[DI]+0Bh, cl    ; Attributes

		;mov byte ptr [DirBuff_ValidData], 2

                ; 18/04/2010 
		call proc_save_directory_buffer
                jc loc_file_rw_cmd_failed

                jmp short loc_print_attr_changed_message

loc_attr_file_fs_check:
               ;23/05/2010
 	        mov bh, byte ptr [DirBuff_Drv]
                sub bh, 'A'
                xor bl, bl
                mov si, offset Logical_DosDisks
                add si, bx
                cmp byte ptr [SI][LD_FStype], 0A1h
                jnc short loc_attr_file_change_fs_file_attributes
                mov ax, 0Dh ; Invalid Data
                jmp loc_file_rw_cmd_failed
loc_attr_file_change_fs_file_attributes:
                ; cl = New MS-DOS File Attributes
                mov al, cl ; File/Directory Attributes
                xor ah, ah ; Attributes in MS-DOS format                  
                call proc_change_fs_file_attributes
                jc loc_file_rw_cmd_failed

                mov byte ptr [Attributes], cl 

loc_print_attr_changed_message:
               ; 23/05/2010
                mov si, offset Msg_New
                call proc_printmsg
 
                jmp loc_show_attributes_no_nextline

cmp_cmd_mkdir:
               ; 10/07/2010
                mov  cx,5
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_mkdir
get_char_mkdir:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne cmp_cmd_run
                loop get_char_mkdir
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_mkdir_fchar:
                inc si
                mov al, byte ptr [SI]
                cmp al, 20h
                je short get_mkdir_fchar
                jb short loc_mkdir_nodirname_retn

loc_mkdir_parse_path_name:
                ; 28/02/2010
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_mkdir_check_dirname_exists:
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed
                mov word ptr [DelFile_FNPointer], si
                call proc_check_filename
                jc short loc_mkdir_invalid_dir_name_chars

loc_mkdir_drv:
                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_mkdir_change_directory

                call proc_change_current_drive
                jc loc_file_rw_cmd_failed

loc_mkdir_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_mkdir_find_directory

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc short loc_mkdir_check_error_code

loc_mkdir_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_mkdir_find_directory:
	       ;mov si, offset FindFile_Name
 		mov si, word ptr [DelFile_FNPointer]
                xor ax, ax ; any name (dir, file, volume)
                call proc_find_first_file
                jc short loc_mkdir_check_error_code

loc_mkdir_directory_found:
               ; 10/07/2010 
                mov si, offset Msg_Name_Exists
                call proc_printmsg

                jmp loc_file_rw_restore_retn

loc_mkdir_invalid_dir_name_chars:
               ; 11/07/2010 
                mov si, offset Msg_invalid_name_chars
                call proc_printmsg

                jmp loc_file_rw_restore_retn

loc_mkdir_check_error_code:
                cmp al, 2
               ;je short loc_mkdir_directory_not_found
                je short loc_mkdir_ask_for_yes_no
                stc
                jmp loc_file_rw_cmd_failed

loc_mkdir_nodirname_retn:
                retn

loc_mkdir_directory_not_found:
loc_mkdir_ask_for_yes_no:
                mov si, offset Msg_DoYouWantMkdir
                call proc_printmsg
                mov si, word ptr [DelFile_FNPointer]
                call proc_printmsg
                mov si, offset Msg_YesNo
                call proc_printmsg
loc_mkdir_ask_again:
                xor ah, ah
                int 16h
                cmp al, 0Bh
                je short loc_do_not_make_directory
                and al, 0DFh
 		mov byte ptr [Y_N_nextLine], al
                cmp al, 'Y'
                je short loc_mkdir_yes_make_directory
                cmp al, 'N'
                jne short loc_mkdir_ask_again
loc_do_not_make_directory:
                mov si, offset Y_N_nextline
                call proc_printmsg
                jmp loc_file_rw_restore_retn

loc_mkdir_yes_make_directory:
                mov si, offset Y_N_nextline
                call proc_printmsg

loc_mkdir_call_proc_make_directory:
               ; 10/07/2010
                mov ax, word ptr [Current_Dir_FCluster]
                mov dx, word ptr [Current_Dir_FCluster]+2

 	        mov bh, byte ptr [Current_Drv]
                xor bl, bl

                mov si, word ptr [DelFile_FNPointer]
                call proc_make_directory
                jc loc_file_rw_cmd_failed

                mov si, offset Msg_OK
                call proc_printmsg
                jmp loc_file_rw_restore_retn


cmp_cmd_run:
                mov  cx,3
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_Run
get_char_run:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  cmp_cmd_find ; loc_cmd_failed
                loop get_char_run
                mov  al, byte ptr [SI]
		cmp  al, 20h
                ja   loc_cmd_failed
loc_run_check_pathname_begin:
                inc  si
                cmp byte ptr [SI], 20h
                jb loc_cmd_failed
                je short loc_run_check_pathname_begin

loc_run_parse_path_name:
                ; 29/11/2009
                push ds
                pop es
                mov di, offset FindFile_Drv
                call proc_parse_pathname
                jc loc_cmd_failed

loc_run_check_filename_exists:
                ; 06/12/2009
                mov si, offset FindFile_Name
                cmp byte ptr [SI], 20h
                jna loc_cmd_failed

                ; 06/12/2009 
loc_run_check_exe_filename_ext:
                ; 14/11/2009
                call proc_check_exe_filename_ext
                jc loc_cmd_failed
                mov byte ptr [EXE_ID], al

loc_run_drv:
                mov dh, byte ptr [Current_Drv]
                mov byte ptr [RUN_CDRV], dh

                mov dl, byte ptr [FindFile_Drv]
                cmp dl, dh
                je short loc_run_change_directory

                call proc_change_current_drive
                jc loc_run_cmd_failed

loc_run_change_directory:
                ; 05/12/2009
                cmp byte ptr [FindFile_Directory], 20h
                jna short loc_load_executable_file

                ; 29/11/2009 -> Restore_CDIR
                inc byte ptr [Restore_CDIR]
                mov si, offset FindFile_Directory
                xor ah, ah ; CD_COMMAND sign -> 0 
                call proc_change_current_directory
                jc loc_run_cmd_failed

loc_run_change_prompt_dir_string:
                call proc_change_prompt_dir_string

loc_load_executable_file:
                mov si, offset FindFile_Name
                mov ax, 04h
                call proc_open_file
                jnc short loc_run_loaded_executable_file

loc_run_cmd_failed:
                ; 07/12/2009 -> error codes are in al (they were in ah)
                ; 06/12/2009 -> Restore Current Drive
                ; 29/11/2009 -> Restore Current Directory
                push ds
                pop es
 
                push ax

                mov bh, byte ptr [RUN_CDRV] ; it is set at the beginning
                                            ; of the 'run' command.
                
                cmp bh, byte ptr [Current_Drv]
                je short loc_run_restore_cdir
                mov dl, bh
                call proc_change_current_drive 
                jmp short loc_run_err_pass_restore_cdir

loc_run_restore_cdir:
                cmp byte ptr [Restore_CDIR], 0
                jna short loc_run_err_pass_restore_cdir
 		xor bl, bl
                mov si, offset Logical_DOSDisks
                add si, bx
                call proc_restore_current_directory

loc_run_err_pass_restore_cdir:
                pop ax 

loc_run_cmd_failed_cmp_al:
                ; End of Restore_CDIR code (29/11/2009)

                cmp al, 1
                je loc_cmd_failed
loc_run_dir_not_found:
                cmp al, 3
                jne short loc_run_file_notfound_msg
                ; Path not found (MS-DOS Error Code = 3)
                mov si, offset Msg_Dir_Not_Found
                call proc_printmsg
                retn
loc_run_file_notfound_msg:
                cmp al, 2 ; File not found
                jne short loc_run_file_drv_read_err
loc_print_file_notfound_msg: 
                mov si, offset Msg_file_not_found
                call proc_printmsg
                retn
loc_run_file_drv_read_err:
               ; 16/05/2010 -> Err: 1Eh (Read fault)
                cmp al, 1Eh ; Drive not ready or read error
                je short loc_run_file_print_drv_read_err
               ;
                cmp al, 15h ; Drive not ready (or read error)
                jne short loc_run_file_toobig
loc_run_file_print_drv_read_err:
                mov si, offset Msg_Not_Ready_Read_Err
                call proc_printmsg
                retn
loc_run_file_toobig:
                cmp al, 8 ; Not enough free memory to load&run file
                jne short loc_run_misc_error
                mov si, offset Msg_insufficient_memory
                call proc_printmsg
                retn  
loc_run_misc_error:
                call proc_hex
                mov word ptr [Error_Code], ax
                
                mov si, offset Msg_Error_Code 
                call proc_printmsg 
                retn

loc_run_loaded_executable_file:
                ; 14/11/2009
                mov word ptr [Run_Segment], ax
                mov word ptr [Run_Offset], 100h
                mov word ptr [PSP_Address], ax
                mov byte ptr [INT21h_4Bh], 0
                ; 12/11/2009 
                mov ah, 0Fh ; Get Video Display Mode
                int 10h
                ;AH= Number of columns
                ;AL= Video mode
                ;BH= Current video page
                mov word ptr [Program_VideoMode], AX
                mov byte ptr [Program_VideoPage], BH

                xor bx, bx
                mov es, bx

                ; 14/11/2009
                push word ptr ES:[084h]
                pop word ptr [INT21h_Offset]
                push word ptr ES:[086h]
                pop word ptr [INT21h_Segment]
                mov word ptr ES:[084h], offset trdos_int21h_routine
                push cs
                pop word ptr ES:[086h]
                ; 

                push word ptr ES:[6Ch]   ; Int 1Bh Offset
                pop word ptr [INT1Bh_Offset]
                push word ptr ES:[6Eh]   ; Int 1Bh Segment
                pop word ptr [INT1Bh_Segment]

                mov word ptr ES:[6Ch], offset trdos_control_break_routine
                                             ; INT1Bh vector, offset
                push cs
                pop word ptr ES:[6Eh]        ; INT1Bh vector, segment

                push word ptr ES:[80h]   ; Int 20h Offset
                pop word ptr [INT20h_Offset]
                push word ptr ES:[82h]   ; Int 20h Segment
                pop word ptr [INT20h_Segment]

                mov word ptr ES:[80h], offset Run_Com_INT20h_Handler
                                             ; INT20h handler, offset
                push cs
                pop word ptr ES:[82h]        ; INT20h handler, segment

                mov word ptr [StackSegment], ss
                mov word ptr [StackPointer], sp
                mov word ptr [BasePointer], bp

loc_run_loaded_executable_file_INT21h_4Bh:
                cmp byte ptr [EXE_ID], 'E'
                jne short far_jump_to_com_file
loc_relocate_exe_items:
                ; 2005
                ; This code written by Erdogan Tan
                ; by using "Dissecting DOS" book by
                ; Michael Podanoffsky (Rx_DOS)
                ; and source code by
                ; Alexei A. Frounze (OS for Dummies Loader)
                ; 
                mov ax, word ptr [Run_Segment]
                add ax, 10h
                mov es, ax
                add ax, word ptr ES:[08h] ;Size of header, in paragraphs
                mov cx, word ptr ES:[06h] ;Number of relocation items
                mov bx, word ptr ES:[18h] ;Relocation table offset
                jcxz short exe_hdr_relocation_done
exe_hdr_relocation_cycle:
                mov di, word ptr ES:[BX]   ;Item Offset
                mov dx, word ptr ES:[BX+2] ;Item Segment (Relative]
                add dx, ax                 ;Item Segment (Absolute)
                push es
                mov es, dx
                add word ptr ES:[DI], ax ;Fixup
                pop es
                add bx, 4
                loop exe_hdr_relocation_cycle
exe_hdr_relocation_done:
                mov bx, ax
                add bx, word ptr ES:[0Eh] ;Initial SS Value
                mov ss, bx
                mov sp, word ptr ES:[10h] ;Initial SP Value
                add ax, word ptr ES:[16h] ;Initial CS Value
                mov bx, word ptr [Run_Segment]
                mov word ptr [Run_Segment], ax
                mov ax, word ptr ES:[14h] ;Initial IP Value
                mov word ptr [Run_Offset], ax
                jmp short far_jump_to_exe_file
far_jump_to_com_file:
                ; 25/11/2009
                mov bx, word ptr [Run_Segment]
                        ; cx = Allocation size in 100h bytes
                cmp cx, 0FFh ; 255
                jb short pass_run_com_set_sp_to_FFFEh
                mov ax, 0FFFEh ; cx = 256
                jmp short loc_run_com_set_sp_to_FFFEh
pass_run_com_set_sp_to_FFFEh:
                inc cx
                mov ax, 100h 
                mul cx
loc_run_com_set_sp_to_FFFEh:
                mov ss, bx
                mov sp, ax
                ;
far_jump_to_exe_file:
                mov ds, bx
                mov es, bx
                push bx
                xor ax, ax
                push ax
                ; 12/11/2009
                mov bx, ax ; 0
                mov cx, ax ; 0
                mov dx, ax ; 0 
                xor si, si
                xor di, di
                mov bp, sp
                ;
                jmp dword ptr CS:[Run_Offset]

Run_Com_INT20h_Handler:
                ; CPU comes here via INT 20h or INT 21h Terminate Process
                ; pop ax ; IP
                ; pop ax ; CS 
                ; pop ax ; Flags

                push cs
                pop  ds

Run_Com_INT20h_Handler_ctrlbrk:
                ; 14/11/2009
                mov ax, word ptr [PSP_Address]
                call proc_close_file

                ; 29/11/2009  -> Restore current directory                
                push ds
                pop es

                mov bh, byte ptr [RUN_CDRV] ; it is set at the beginning
                                            ; of the 'run' command.
                
                cmp bh, byte ptr [Current_Drv]
                je short Run_Com_INT20h_restore_cdir
                mov dl, bh
                call proc_change_current_drive 
                jmp short Run_Com_INT20h_check_INT21h_4Bh_sign
Run_Com_INT20h_restore_cdir:
                xor bl, bl
                mov si, offset Logical_DOSDisks
                add si, bx

                call proc_restore_current_directory

Run_Com_INT20h_check_INT21h_4Bh_sign:

                ; End of 29/11/2009 restore cdir code
                
                cmp byte ptr [INT21h_4Bh], 0
                ja short run_com_exec_ret_with_iret
               
                xor ax, ax
                mov es, ax
                push word ptr [INT1Bh_Offset]
                pop word ptr ES:[6Ch]
                push word ptr [INT1Bh_Segment]
                pop word ptr ES:[6Eh]
                push word ptr [INT20h_Offset]
                pop word ptr ES:[80h]
                push word ptr [INT20h_Segment]
                pop word ptr ES:[82h]
                ; 14/11/2009
                push word ptr [INT21h_Offset]
                pop word ptr ES:[084h]
                push word ptr [INT21h_Segment]
                pop word ptr ES:[086h]
                
                push ds
		pop  es
                mov ss, word ptr [StackSegment]
                mov sp, word ptr [StackPointer]
                mov bp, word ptr [BasePointer]
                sti

                ; 12/11/2009 
                mov ah, 0Eh
                mov BH, byte ptr [Program_VideoPage]
                mov BL, byte ptr [Program_CharColorAtr]
		int 10h
                ;  

                retn

run_com_exec_ret_with_iret:
                dec byte ptr [INT21h_4Bh]
                mov ss, word ptr [StackSegment21h]
                mov sp, word ptr [StackPointer21h]
                pop bp
                pop es
                pop ds
                pop di
                pop si
                pop bx
                pop cx
                pop dx
                pop ax
                sti
                iret

;29/11/2009
RUN_CDRV: db 0

;14/11/2009
PSP_Address: dw 0
INT21h_Offset: dw 0
INT21h_Segment: dw 0
;
INT21h_4Bh: db 0
StackSegment21h: dw 0
StackPointer21h: dw 0
StackSegment: dw 0
StackPointer: dw 0
BasePointer: dw 0
Run_Offset: dw 0
Run_Segment: dw 0
EXE_ID: db 0
INT20h_Offset: dw 0
INT20h_Segment: dw 0
INT1Bh_Offset: dw 0
INT1Bh_Segment: dw 0


; !!!
; Other internal commands will be put here
; !!!

; burasi find basi

cmp_cmd_find:
                mov  cx,4
                mov  si, offset CommandBuffer
                mov  di, offset Cmd_find
get_char_find:
                mov  al, byte ptr [SI]
		inc  si
		scasb
                jne  loc_cmd_failed
                loop get_char_find
                mov  al, byte ptr [SI]
		cmp  al,20h
                ja   loc_cmd_failed
get_cmdfindfile_fchar:
                inc  si
                mov  al, byte ptr [SI]
                cmp  al, 20h
                je   short get_cmdfindfile_fchar
                ja   short loc_cmdfind_findfile
                retn
loc_cmdfind_findfile:
                mov  ax, 0800h
                call proc_find_first_file
                push ds
                pop es
                jc   short loc_cmdfind_filenotfound
loc_cmdfind_nextfile:
                mov  di, offset FindFile_Name
                call get_file_name
                call proc_printmsg
                mov  si, offset nextline
                call proc_printmsg
                call proc_find_next_file
                push ds
                pop es
                jnc short loc_cmdfind_nextfile 

loc_cmdfind_filenotfound:
               mov si, offset Msg_Files_Found
               mov byte ptr [si], al
               call proc_printmsg
               stc
               retn

; burasi find sonu


loc_cmd_failed:
                cmp byte ptr [CommandBuffer], 20h
                jna short loc_cmd_return
                mov si, offset Msg_Bad_Command
                call proc_printmsg
loc_cmd_return:
                retn

command_interpreter endp

proc_check_exe_filename_ext proc near
                ; 14/11/2009
                ; INPUT -> DS:SI = Dot File Name
                ; 
                ; OUTPUT -> clc => EXE_ID in AL
                ;          DS:SI = Dot File Name
                ; -> stc => Invalid executable file name
                ; clc -> BX = Last Char offset of EXE file
                ; stc -> BX = 0
                ; clc-> AL='M' (COM), AL='E' (EXE)
  
                xor bx, bx
                dec bl
                push si
loc_run_check_filename_ext:                
                inc bl
                lodsb ; 'mov al, byte ptr [SI]' then 'inc si'
                cmp al, 20h
                ja short loc_run_check_filename_ext
                pop si
                cmp bl, 5
                jb short loc_check_exe_fn_stc
                cmp bl, 12
                ja short loc_check_exe_fn_stc
                sub bl, 4
                cmp byte ptr [SI][BX], '.'
                jne short loc_check_exe_fn_stc
                inc bl                  
                mov al, byte ptr [SI][BX]
                and al, 0DFh 
                cmp al, 'C'
                jne short loc_check_filename_ext_exe
                inc bl
                mov al, byte ptr [SI][BX]
                and al, 0DFh  
                cmp al, 'O'
                jne short loc_check_exe_fn_stc
                inc bl
                mov al, byte ptr [SI][BX]
                and al, 0DFh  
                cmp al, 'M'
                je short loc_check_exe_fn_retn
loc_check_filename_ext_exe:
                cmp al, 'E'
                jne short loc_check_exe_fn_stc
                inc bl
                mov al, byte ptr [SI][BX]
                and al, 0DFh
                cmp al, 'X'
                jne short loc_check_exe_fn_stc
                inc bl
                mov al, byte ptr [SI][BX]
                and al, 0DFh
                cmp al, 'E'
                je short loc_check_exe_fn_retn
loc_check_exe_fn_stc:
                xor bl, bl
                stc
loc_check_exe_fn_retn:
                retn
              
proc_check_exe_filename_ext endp


; INTERNAL COMMANDS
Cmd_Dir: db "DIR"
; Cmd_Cd: db "CD"
; Cmd_Drive: db "C:" ; D:, E: etc.
Cmd_Ver: db "VER"
Cmd_Exit: db "EXIT"
Cmd_Prompt: db "PROMPT"
Cmd_Volume: db "VOLUME"
Cmd_LongName: db "LONGNAME"
Cmd_Date: db "DATE"
Cmd_Time: db "TIME"
Cmd_Run: db "RUN"
Cmd_Set: db "SET"
Cmd_Cls: db "CLS"
Cmd_Show: db "SHOW"
Cmd_Del: db "DEL"
Cmd_Attrib: db "ATTRIB"
Cmd_Rename: db "RENAME"
Cmd_Rmdir: db "RMDIR"
Cmd_Mkdir: db "MKDIR"
Cmd_Copy: db "COPY"
Cmd_Find: db "FIND" ; temporary
Cmd_Mem: db "MEM" ; MEMORY

Program_Exit: db 0
Program_VideoMode: db 0
Program_VideoPage: db 0
                       
return_from_ctrl_break:
                iret
trdos_control_break_routine:
                cmp byte ptr CS:[InCtrlBreak], 0
                ja short return_from_ctrl_break
                push ax
                push bx
                push ds
                push si
                push cs
                pop ds
                mov byte ptr [InCtrlBreak], 1
                mov ah, 0Fh ; Get Video Display Mode
                int 10h
                ;AH= Number of columns
                ;AL= Video mode
                ;BH= Current video page
                mov word ptr [Program_VideoMode],ax 
                mov byte ptr [Program_VideoPage],bh
                cmp al, 3
                je short pass_set_video_mode_ctrlbreak_1
                mov ah, 0 ; Set Video Mode
                mov al, 3 ; 80*25 text mode, color with CGA card
                int 10h
pass_set_video_mode_ctrlbreak_1:
                mov ah, 5 ; Set Video Page
                mov al, 7 ; Page 7  (for Control+Break Yes/No Message)
                int 10h
                mov ah, 6 ; Clear Screen (AL=0)
                mov al, 0
                mov bh, 7
                push cx
                push dx
                xor cx, cx
                mov dh, 24
                mov dl, 79
                int 10h
                mov ah, 2 ; Set Cursor Position
              ; mov bh, 7
                xor dx,dx
                int 10h
                pop dx
                pop cx
                mov si, offset msg_terminate_program
                call proc_printmsg
loc_ask_for_terminate_program:
                cli
                mov     AH, 1
                int     16h
                jnz     short loc_ask_for_terminate_program
                sti
                mov	AH,0
		int	16h			; BIOS Service func ( ah ) = 0
						; Read next kbd char
						;AH-scan code AL-char code
                cli
                and al, 0DFh ;Capitalized char
                cmp al, 'Y'
                je short loc_ctrlbreak_terminate_program
                cmp al, 'N'
                jne short loc_ask_for_terminate_program
                cmp byte ptr [Program_VideoMode], 3
                je short pass_set_video_mode_1_ctrlbreak_2
                mov ah, 0 ; Set Video Mode
                mov al, byte ptr [Program_VideoMode]
                int 10h
pass_set_video_mode_1_ctrlbreak_2:
                mov ah, 5 ; Set Video Page
                mov al, byte ptr [Program_VideoPage]
                int 10h
                mov byte ptr [InCtrlBreak], 0
                pop si
                pop ds 
                pop bx 
                pop ax
                sti
                iret
loc_ctrlbreak_terminate_program:
                mov byte ptr [InCtrlBreak], 0
                jmp run_com_int20h_handler_ctrlbrk

InCtrlBreak: db 0

Msg_Terminate_Program:
                db 7
                db "Do you want to terminate running program (Y/N) ?"
                db 0Dh, 0Ah, 0

Msg_Bad_Command:
                db "Bad command or file name!"
                db 0Dh, 0Ah, 0
Msg_Not_Ready_Read_Err:
                db "Drive not ready or read error!"
                db 0Dh, 0Ah, 0
Msg_Insufficient_Memory:
                db "Insufficient memory!"
                db 0Dh, 0Ah, 0
Msg_Dir_Not_Found:
                db "Directory not found!"
                db 0Dh, 0Ah, 0
Msg_File_Not_Found:
                db "File not found!"
                db 0Dh, 0Ah, 0
Msg_File_Directory_Not_Found:
                db "File or directory not found!"
                db 0Dh, 0Ah, 0
Msg_LongName_Not_Found:
                db "Long name not found!"
                db 0Dh, 0Ah, 0
Msg_Files_Found:
                db '0'
                db ' file(s) found !'
                db 0Ah, 0Ah, 0

Msg_DoYouWantRmdir:
                db "Do you want to delete directory ",0

Msg_DoYouWantDelete:
                db "Do you want to delete file ",0
Msg_YesNo:      db " (Y/N) ? ", 0
Msg_Deleted:    db "Deleted...", 0Dh, 0Ah, 0

Msg_Permission_denied:
                db 7
                db "Permission denied!", 0Dh, 0Ah, 0

; 16/05/2010
Msg_Not_Ready_Write_Err:
                db "Drive not ready or write error!"
                db 0Dh, 0Ah, 0
; 06/06/2010
Msg_Dir_Not_Empty:
                db "Directory not empty!"
                db 0Dh, 0Ah, 0
;
; 07/10/2010
Msg_invalid_name_chars:
                db "Invalid file or directory name chars!"
                db 0Dh, 0Ah, 0
Msg_Name_Exists: db "File or directory name exists!"
                db 0Dh, 0Ah, 0
Msg_DoYouWantMkdir:
                db "Do you want to make directory ",0  
;

; 20/06/2010 -> "Run failure -> Command Failed !"
Msg_Error_Code:
                db 'Command failed ! Error code : '
Error_Code:     db '00h'
                db 0Ah, 0Ah, 0

; 04/04/2010
Y_N_nextline: db 0, 0Dh, 0Ah, 0

; 23/05/2010
Msg_New:
		   db "New "
                   db 0
Attributes:        db 0
Str_Attributes:
                   db "Attributes : "
Attr_Chars:        db "NORMAL"
                   db 0

; 11/07/2010
Msg_OK:            db "OK.", 0Dh, 0Ah, 0
