From 1128038d0f67a5432e1f966be0b13a8d451df49a Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 1 Oct 2013 20:57:54 -0700 Subject: [PATCH] Implement rest of parser. --- src/libguac/guacamole/instruction.h | 4 +-- src/libguac/instruction.c | 41 +++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/libguac/guacamole/instruction.h b/src/libguac/guacamole/instruction.h index 9131f884..db22a7ec 100644 --- a/src/libguac/guacamole/instruction.h +++ b/src/libguac/guacamole/instruction.h @@ -117,9 +117,9 @@ typedef struct guac_instruction { guac_instruction_parse_state state; /** - * The remaining length of the current element, if known. + * The length of the current element, if known. */ - int __element_remaining_length; + int __element_length; /** * The number of elements currently parsed. diff --git a/src/libguac/instruction.c b/src/libguac/instruction.c index 7b2ba3b8..157de953 100644 --- a/src/libguac/instruction.c +++ b/src/libguac/instruction.c @@ -91,8 +91,7 @@ int guac_instruction_append(guac_instruction* instr, /* If period, switch to parsing content */ else if (c == '.') { - instr->__elementv[instr->__elementc++] = char_buffer; - instr->__element_remaining_length = parsed_length; + instr->__element_length = parsed_length; instr->state = GUAC_INSTRUCTION_PARSE_CONTENT; break; } @@ -111,12 +110,44 @@ int guac_instruction_append(guac_instruction* instr, return 0; } - } + } /* end parse length */ /* Parse element content */ if (instr->state == GUAC_INSTRUCTION_PARSE_CONTENT) { - /* STUB */ - } + + /* If enough data given, finish element */ + if (length - bytes_parsed > instr->__element_length) { + + /* Pull terminator */ + char terminator = char_buffer[instr->__element_length]; + + /* Store reference to string within elementv */ + instr->__elementv[instr->__elementc++] = char_buffer; + char_buffer[instr->__element_length] = '\0'; + + bytes_parsed += instr->__element_length+1; + + /* If semicolon, store end-of-instruction */ + if (terminator == ';') { + instr->state = GUAC_INSTRUCTION_PARSE_COMPLETE; + instr->opcode = instr->__elementv[0]; + instr->argv = &(instr->__elementv[1]); + instr->argc = instr->__elementc - 1; + } + + /* If comma, move on to next element */ + else if (terminator == ',') + instr->state = GUAC_INSTRUCTION_PARSE_LENGTH; + + /* Otherwise, parse error */ + else { + instr->state = GUAC_INSTRUCTION_PARSE_ERROR; + return 0; + } + + } + + } /* end parse content */ return bytes_parsed;