From a5a84c429ac548eafd1d3903225a4ce72104201f Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周三, 22 4月 2015 03:50:26 +0800
Subject: [PATCH] Support RESPONSE MESSAGE UNIT SEPARATOR, issue #21

---
 libscpi/src/parser.c |   50 ++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index 1f13b1b..4b73e3c 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -43,6 +43,7 @@
 #include "lexer_private.h"
 #include "scpi/error.h"
 #include "scpi/constants.h"
+#include "scpi/utils.h"
 
 /**
  * Write data to SCPI output
@@ -98,12 +99,28 @@
 }
 
 /**
+ * Conditionaly write ";"
+ * @param context
+ * @return number of characters written
+ */
+static size_t writeSemicolon(scpi_t * context) {
+    if (context->output_count > 0) {
+        return writeData(context, ";", 1);
+    } else {
+        return 0;
+    }
+}
+
+/**
  * Process command
  * @param context
  */
 static void processCommand(scpi_t * context) {
     const scpi_command_t * cmd = context->param_list.cmd;
     lex_state_t * state = &context->param_list.lex_state;
+
+    /* conditionaly write ; */
+    writeSemicolon(context);
 
     context->cmd_error = FALSE;
     context->output_count = 0;
@@ -116,9 +133,6 @@
             SCPI_ErrorPush(context, SCPI_ERROR_EXECUTION_ERROR);
         }
     }
-
-    /* conditionaly write new line */
-    writeNewLine(context);
 
     /* set error if command callback did not read all parameters */
     if (state->pos < (state->buffer + state->len) && !context->cmd_error) {
@@ -152,16 +166,18 @@
  * @param len - command line length
  * @return 1 if the last evaluated command was found
  */
-int SCPI_Parse(scpi_t * context, const char * data, int len) {
+int SCPI_Parse(scpi_t * context, char * data, int len) {
     int result = 0;
     scpi_parser_state_t * state;
     int r;
+    scpi_token_t cmd_prev = {SCPI_TOKEN_UNKNOWN, NULL, 0};
 
     if (context == NULL) {
         return -1;
     }
 
     state = &context->parser_state;
+    context->output_count = 0;
 
     while (1) {
         result = 0;
@@ -171,6 +187,9 @@
         if (state->programHeader.type == SCPI_TOKEN_INVALID) {
             SCPI_ErrorPush(context, SCPI_ERROR_INVALID_CHARACTER);
         } else if (state->programHeader.len > 0) {
+
+            composeCompoundCommand(&cmd_prev, &state->programHeader);
+
             if (findCommandHeader(context, state->programHeader.ptr, state->programHeader.len)) {
 
                 context->param_list.lex_state.buffer = state->programData.ptr;
@@ -183,6 +202,7 @@
                 processCommand(context);
 
                 result = 1;
+                cmd_prev = state->programHeader;
             } else {
                 SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER);
             }
@@ -196,6 +216,10 @@
         }
 
     }
+
+    /* conditionaly write new line */
+    writeNewLine(context);
+
     return result;
 }
 
@@ -327,7 +351,7 @@
     size_t result = 0;
     size_t len;
 
-    len = longToStr(val, buffer, sizeof (buffer), base);
+    len = SCPI_LongToStr(val, buffer, sizeof (buffer), base);
     basePrefix = getBasePrefix(base);
 
     result += writeDelimiter(context);
@@ -348,7 +372,7 @@
 size_t SCPI_ResultDouble(scpi_t * context, double val) {
     char buffer[32];
     size_t result = 0;
-    size_t len = doubleToStr(val, buffer, sizeof (buffer));
+    size_t len = SCPI_DoubleToStr(val, buffer, sizeof (buffer));
     result += writeDelimiter(context);
     result += writeData(context, buffer, len);
     context->output_count++;
@@ -385,7 +409,7 @@
     char block_header[12];
     size_t header_len;
     block_header[0] = '#';
-    longToStr(len, block_header + 2, 10, 10);
+    SCPI_LongToStr(len, block_header + 2, 10, 10);
 
     header_len = strlen(block_header + 2);
     block_header[1] = header_len + '0';
@@ -414,7 +438,7 @@
  * @param token
  * @param ptr
  */
-static void invalidateToken(scpi_token_t * token, const char * ptr) {
+static void invalidateToken(scpi_token_t * token, char * ptr) {
     token->len = 0;
     token->ptr = ptr;
     token->type = SCPI_TOKEN_UNKNOWN;
@@ -934,7 +958,7 @@
  * @param len
  * @return 
  */
-int scpiParser_detectProgramMessageUnit(scpi_parser_state_t * state, const char * buffer, int len) {
+int scpiParser_detectProgramMessageUnit(scpi_parser_state_t * state, char * buffer, int len) {
     lex_state_t lex_state;
     scpi_token_t tmp;
     int result = 0;
@@ -1009,11 +1033,9 @@
     }
 }
 
-
-
-
-
-
+scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len) {
+    return matchCommand (pattern, value, len);
+}
 
 
 

--
Gitblit v1.9.1