From 5e68cb0db1499c7cda1e7448e8fc1ebdb3e9f322 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周一, 05 10月 2015 21:47:23 +0800
Subject: [PATCH] Resolve #54: Detect not typed numbers in SCPI_CommandNumbers

---
 libscpi/src/lexer.c |  103 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 76 insertions(+), 27 deletions(-)

diff --git a/libscpi/src/lexer.c b/libscpi/src/lexer.c
index 349bcc5..ee4696c 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
@@ -342,6 +344,7 @@
 }
 
 /* 7.6.1 <COMMAND PROGRAM HEADER> */
+
 /**
  * Skip command program header \*<PROGRAM MNEMONIC>
  * @param state
@@ -424,7 +427,7 @@
             }
         } else if (res <= SKIP_INCOMPLETE) {
             token->type = SCPI_TOKEN_INCOMPLETE_COMPOUND_PROGRAM_HEADER;
-        } 
+        }
     }
 
     if (token->type != SCPI_TOKEN_UNKNOWN) {
@@ -438,6 +441,7 @@
 }
 
 /* 7.7.1 <CHARACTER PROGRAM DATA> */
+
 /**
  * Detect token "Character program data"
  * @param state
@@ -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)) {
@@ -590,8 +594,8 @@
  * @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])) {
@@ -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, '\'');
 }
 
@@ -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 = 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,23 +737,29 @@
 
             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)) {
+    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 {
+        // invalid
         token->type = SCPI_TOKEN_UNKNOWN;
         state->pos = token->ptr;
         token->len = 0;
@@ -760,12 +771,12 @@
 /* 7.7.7 <EXPRESSION PROGRAM DATA> */
 static int isProgramExpression(int c) {
     if ((c >= 0x20) && (c <= 0x7e)) {
-        if ((c != 0x22)
-                && (c != 0x23)
-                && (c != 0x27)
-                && (c != 0x28)
-                && (c != 0x29)
-                && (c != 0x3B)) {
+        if ((c != '"')
+                && (c != '#')
+                && (c != '\'')
+                && (c != '(')
+                && (c != ')')
+                && (c != ';')) {
             return 1;
         }
     }
@@ -780,6 +791,7 @@
 }
 
 // TODO: 7.7.7.2-2 recursive - any program data
+
 /**
  * Detect token Expression
  * @param state
@@ -853,6 +865,46 @@
 }
 
 /**
+ * Detect token colon
+ * @param state
+ * @param token
+ * @return 
+ */
+int scpiLex_Colon(lex_state_t * state, scpi_token_t * token) {
+    token->ptr = state->pos;
+
+    if (skipChr(state, ':')) {
+        token->len = 1;
+        token->type = SCPI_TOKEN_COLON;
+    } else {
+        token->len = 0;
+        token->type = SCPI_TOKEN_UNKNOWN;
+    }
+
+    return token->len;
+}
+
+/**
+ * Detect specified character
+ * @param state
+ * @param token
+ * @return 
+ */
+int scpiLex_SpecificCharacter(lex_state_t * state, scpi_token_t * token, char chr) {
+    token->ptr = state->pos;
+
+    if (skipChr(state, chr)) {
+        token->len = 1;
+        token->type = SCPI_TOKEN_SPECIFIC_CHARACTER;
+    } else {
+        token->len = 0;
+        token->type = SCPI_TOKEN_UNKNOWN;
+    }
+
+    return token->len;
+}
+
+/**
  * Detect token New line
  * @param state
  * @param token
@@ -876,6 +928,3 @@
 
     return token->len;
 }
-
-
-

--
Gitblit v1.9.1