From d2a06893015a9e2845f9278af9e2ba15f9b37d9d Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周一, 31 8月 2015 17:35:32 +0800
Subject: [PATCH] Fix #44: incorrect handling of empty string

---
 libscpi/src/lexer.c |  114 +++++++++++++++++++++++++++++++-------------------------
 1 files changed, 63 insertions(+), 51 deletions(-)

diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c
index a1be5e4..aaca475 100644
--- a/libscpi/src/lexer.c
+++ b/libscpi/src/lexer.c
@@ -245,7 +245,7 @@
  * @param chr
  * @return 
  */
-static int skipChr(lex_state_t * state, int chr) {
+static int skipChr(lex_state_t * state, char chr) {
     if (!iseos(state) && ischr(state, chr)) {
         state->pos++;
         return SKIP_OK;
@@ -297,6 +297,7 @@
 }
 
 /* 7.6.1.2 <COMMAND PROGRAM HEADER> */
+
 /**
  * Skip program mnemonic [a-z][a-z0-9_]*
  * @param state
@@ -319,6 +320,7 @@
 }
 
 /* tokens */
+
 /**
  * Detect token white space
  * @param state
@@ -333,15 +335,16 @@
     token->len = state->pos - token->ptr;
 
     if (token->len > 0) {
-        token->type = TokWhiteSpace;
+        token->type = SCPI_TOKEN_WS;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
 }
 
 /* 7.6.1 <COMMAND PROGRAM HEADER> */
+
 /**
  * Skip command program header \*<PROGRAM MNEMONIC>
  * @param state
@@ -402,32 +405,32 @@
 int scpiLex_ProgramHeader(lex_state_t * state, scpi_token_t * token) {
     int res;
     token->ptr = state->pos;
-    token->type = TokUnknown;
+    token->type = SCPI_TOKEN_UNKNOWN;
 
     res = skipCommonProgramHeader(state);
     if (res >= SKIP_OK) {
         if (skipChr(state, '?') >= SKIP_OK) {
-            token->type = TokCommonQueryProgramHeader;
+            token->type = SCPI_TOKEN_COMMON_QUERY_PROGRAM_HEADER;
         } else {
-            token->type = TokCommonProgramHeader;
+            token->type = SCPI_TOKEN_COMMON_PROGRAM_HEADER;
         }
     } else if (res <= SKIP_INCOMPLETE) {
-        token->type = TokIncompleteCommonProgramHeader;
+        token->type = SCPI_TOKEN_INCOMPLETE_COMMON_PROGRAM_HEADER;
     } else if (res == SKIP_NONE) {
         res = skipCompoundProgramHeader(state);
 
         if (res >= SKIP_OK) {
             if (skipChr(state, '?') >= SKIP_OK) {
-                token->type = TokCompoundQueryProgramHeader;
+                token->type = SCPI_TOKEN_COMPOUND_QUERY_PROGRAM_HEADER;
             } else {
-                token->type = TokCompoundProgramHeader;
+                token->type = SCPI_TOKEN_COMPOUND_PROGRAM_HEADER;
             }
         } else if (res <= SKIP_INCOMPLETE) {
-            token->type = TokIncompleteCompoundProgramHeader;
-        } 
+            token->type = SCPI_TOKEN_INCOMPLETE_COMPOUND_PROGRAM_HEADER;
+        }
     }
 
-    if (token->type != TokUnknown) {
+    if (token->type != SCPI_TOKEN_UNKNOWN) {
         token->len = state->pos - token->ptr;
     } else {
         token->len = 0;
@@ -438,6 +441,7 @@
 }
 
 /* 7.7.1 <CHARACTER PROGRAM DATA> */
+
 /**
  * Detect token "Character program data"
  * @param state
@@ -456,9 +460,9 @@
 
     token->len = state->pos - token->ptr;
     if (token->len > 0) {
-        token->type = TokProgramMnemonic;
+        token->type = SCPI_TOKEN_PROGRAM_MNEMONIC;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
@@ -502,7 +506,7 @@
  * @return 
  */
 int scpiLex_DecimalNumericProgramData(lex_state_t * state, scpi_token_t * token) {
-    const char * rollback;
+    char * rollback;
     token->ptr = state->pos;
 
     if (skipMantisa(state)) {
@@ -517,9 +521,9 @@
 
     token->len = state->pos - token->ptr;
     if (token->len > 0) {
-        token->type = TokDecimalNumericProgramData;
+        token->type = SCPI_TOKEN_DECIMAL_NUMERIC_PROGRAM_DATA;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
@@ -545,9 +549,9 @@
 
     token->len = state->pos - token->ptr;
     if ((token->len > 0)) {
-        token->type = TokSuffixProgramData;
+        token->type = SCPI_TOKEN_SUFFIX_PROGRAM_DATA;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -590,22 +594,22 @@
  * @return 
  */
 int scpiLex_NondecimalNumericData(lex_state_t * state, scpi_token_t * token) {
-    token->ptr = state->pos;
     int someNumbers = 0;
+    token->ptr = state->pos;
     if (skipChr(state, '#')) {
         if (!iseos(state)) {
             if (isH(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipHexNum(state);
-                token->type = TokHexnum;
+                token->type = SCPI_TOKEN_HEXNUM;
             } else if (isQ(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipOctNum(state);
-                token->type = TokOctnum;
+                token->type = SCPI_TOKEN_OCTNUM;
             } else if (isB(state->pos[0])) {
                 state->pos++;
                 someNumbers = skipBinNum(state);
-                token->type = TokBinnum;
+                token->type = SCPI_TOKEN_BINNUM;
             }
         }
     }
@@ -614,7 +618,7 @@
         token->ptr += 2; // ignore number prefix
         token->len = state->pos - token->ptr;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -626,7 +630,7 @@
     return (c >= 0) && (c <= 0x7f);
 }
 
-static int skipQuoteProgramData(lex_state_t * state, int quote) {
+static void skipQuoteProgramData(lex_state_t * state, char quote) {
     while (!iseos(state)) {
         if (isascii7bit(state->pos[0]) && !ischr(state, quote)) {
             state->pos++;
@@ -642,11 +646,11 @@
     }
 }
 
-static int skipDoubleQuoteProgramData(lex_state_t * state) {
+static void skipDoubleQuoteProgramData(lex_state_t * state) {
     skipQuoteProgramData(state, '"');
 }
 
-static int skipSingleQuoteProgramData(lex_state_t * state) {
+static void skipSingleQuoteProgramData(lex_state_t * state) {
     skipQuoteProgramData(state, '\'');
 }
 
@@ -662,7 +666,7 @@
     if (!iseos(state)) {
         if (ischr(state, '"')) {
             state->pos++;
-            token->type = TokDoubleQuoteProgramData;
+            token->type = SCPI_TOKEN_DOUBLE_QUOTE_PROGRAM_DATA;
             skipDoubleQuoteProgramData(state);
             if (!iseos(state) && ischr(state, '"')) {
                 state->pos++;
@@ -672,7 +676,7 @@
             }
         } else if (ischr(state, '\'')) {
             state->pos++;
-            token->type = TokSingleQuoteProgramData;
+            token->type = SCPI_TOKEN_SINGLE_QUOTE_PROGRAM_DATA;
             skipSingleQuoteProgramData(state);
             if (!iseos(state) && ischr(state, '\'')) {
                 state->pos++;
@@ -686,15 +690,15 @@
     token->len = state->pos - token->ptr;
 
     if ((token->len > 0)) {
-        token->ptr++;
-        token->len -= 2;
+        //token->ptr++;
+        //token->len -= 2;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
 
-    return token->len > 0 ? token->len + 2 : 0;
+    return token->len > 0 ? token->len : 0;
 }
 
 /* 7.7.6 <ARBITRARY BLOCK PROGRAM DATA> */
@@ -712,6 +716,7 @@
     int i;
     int arbitraryBlockLength = 0;
     const char * ptr = state->pos;
+    int validData = -1;
     token->ptr = state->pos;
 
     if (skipChr(state, '#')) {
@@ -732,24 +737,30 @@
 
             if (i == 0) {
                 state->pos += arbitraryBlockLength;
-                if ((state->buffer + state->len) < (state->pos)) {
-                    token->len = 0;
-                } else {
+                if ((state->buffer + state->len) >= (state->pos)) {
                     token->ptr = state->pos - arbitraryBlockLength;
                     token->len = arbitraryBlockLength;
+                    validData = 1;
                 }
-            } else {
-                token->len = 0;
+            } else if (iseos(state)) {
+                validData = 0;
             }
-        } else {
-            token->len = 0;
+        } else if (iseos(state)) {
+            validData = 0;
         }
     }
 
-    if ((token->len > 0)) {
-        token->type = TokArbitraryBlockProgramData;
+    if (validData == 1) {
+        // valid
+        token->type = SCPI_TOKEN_ARBITRARY_BLOCK_PROGRAM_DATA;
+    } else if (validData == 0) {
+        // incomplete
+        token->type = SCPI_TOKEN_UNKNOWN;
+        token->len = 0;
+        state->pos = state->buffer + state->len;
     } else {
-        token->type = TokUnknown;
+        // invalid
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -780,6 +791,7 @@
 }
 
 // TODO: 7.7.7.2-2 recursive - any program data
+
 /**
  * Detect token Expression
  * @param state
@@ -802,9 +814,9 @@
     }
 
     if ((token->len > 0)) {
-        token->type = TokProgramExpression;
+        token->type = SCPI_TOKEN_PROGRAM_EXPRESSION;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }
@@ -823,10 +835,10 @@
 
     if (skipChr(state, ',')) {
         token->len = 1;
-        token->type = TokComma;
+        token->type = SCPI_TOKEN_COMMA;
     } else {
         token->len = 0;
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
@@ -843,10 +855,10 @@
 
     if (skipChr(state, ';')) {
         token->len = 1;
-        token->type = TokSemicolon;
+        token->type = SCPI_TOKEN_SEMICOLON;
     } else {
         token->len = 0;
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
     }
 
     return token->len;
@@ -867,9 +879,9 @@
     token->len = state->pos - token->ptr;
 
     if ((token->len > 0)) {
-        token->type = TokNewLine;
+        token->type = SCPI_TOKEN_NL;
     } else {
-        token->type = TokUnknown;
+        token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
     }

--
Gitblit v1.9.1