



; ----------------------= PARSE COMMAND =-----------------------
; parse_command_line takes the user input and converts it into
; a subroutine address. the subroutine for the command entered
; can then be called with the address supplied by this subroutine.
; --------------------------------------------------------------

*						* Does not save state before entering?
***
;parse_command_line: "parse.asm"
;	Takes a user input in the command buffer and converts it to the address
;	of the subroutine that matches the user's input.
;	Reg Input:
;		x = command buffer address
;	Reg Output:
;		x = address of subroutine
;		y = end of command in command buffer
***


parse_command_line:
	pshx					; X is the command buffer
	ldy		#cmd_start		; Y will point to the command list. see "prompt.asm"
	ldaa		0,y			; A will be the length of this command from the list
	iny					; Y now points to table entry for what the command
						; should look like when entered in the command prompt

pcl_loop1:
	ldab		0,y			; load the next letter of the command
	cmpb		0,x			; compare it to the command buffer
	bne		pcl_nomatch		; if not equal, go to the next command

	deca					; decrement the counter of # of letters in this command
	beq		pcl_cmd_ok		; when that counter hits zero, then the correct command
						; has been found in the command table

	inx					; increments the command buffer pointer
	iny					; increments the pointer in the command table
	bra		pcl_loop1		; continue the comparing

pcl_nomatch:
	pulx					; restore X (beginning of command buffer)
	pshx					; save for later


*						* weird way of transfering a to b. try "tab"

	psha					; need to move to next command in list
	pulb					; put A (length of command in list) into B
	addb		#2			; add 2 to get past address of command
	aby					; add B to Y (to get to end of this command)
	ldaa		0,y			; load the length of the next command
	iny					; Y now points to table entry for what the command
						; should look like when entered in the command prompt


						; check to see if we're at the end of the command list
	cpy		#cmd_end		; see "prompt.asm" for cmd_end
	beq		pcl_abs_nomatch		; absolutely no match if it reaches end of table

	bra		pcl_loop1		; do it again!


*						* strange way of transfering data around. see if
*						* it is possible to improve on later on

						; we're at the end of the command (Y) 
pcl_cmd_ok:					; so get the address there!
	pshx					; X is the end of the cmd in the cmd buffer
	iny					; increments to now point at the address of the subroutine
	ldd		0,y			; D now has the address of the subroutine
	xgdx					; put the address in X
	puld					; D = end of cmd in cmd buffer
	puly					; removes useless address from the stack'	
	xgdy					; put the end of the cmd in the buffer into Y

*						* the above sequence seems overly confusing.
*						* he's doing things that are pointless, like the last
*						* three lines are far too confusing. all he's doing is
*						* resetting the stack to return to the proper place
*						* and puting the end of the command in the buffer into Y

	rts					;


pcl_abs_nomatch:
	ldy		#error_msg		; loads pointer to null terminated error message
	jsr		print_string		; prints null terminated string. see "serial.asm"
	pulx					; X has the location of the buffer in it.
	rts					; there also was an error in the line entered as it
						; didn't match any of the valid commands
