From 11f2f2e329ef404d0e9c022cb2f9fbbb45bae285 Mon Sep 17 00:00:00 2001
From: nancy.liao <huihui.liao@greentest.com.cn>
Date: 周日, 27 4月 2025 17:33:31 +0800
Subject: [PATCH] 完成了SCPI命令语法分析器的完整规则

---
 libscpi/src/parser.c |   40 +++++++++++++++++++++++++++-------------
 1 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/libscpi/src/parser.c b/libscpi/src/parser.c
index 37ca85c..a6376cf 100644
--- a/libscpi/src/parser.c
+++ b/libscpi/src/parser.c
@@ -37,6 +37,7 @@
 
 #include <ctype.h>
 #include <string.h>
+#include "scpi/externinterface.h"
 
 #include "scpi/config.h"
 #include "scpi/parser.h"
@@ -45,7 +46,7 @@
 #include "scpi/error.h"
 #include "scpi/constants.h"
 #include "scpi/utils.h"
-
+#include "scpi/externinterface.h"
 /**
  * Write data to SCPI output
  * @param context
@@ -55,7 +56,8 @@
  */
 static size_t writeData(scpi_t * context, const char * data, size_t len) {
     if ((len > 0) && (data != NULL)) {
-        return context->interface->write(context, data, len);
+        return 0;
+       // return context->interface->write(context, data, len);
     } else {
         return 0;
     }
@@ -126,7 +128,9 @@
 
  //璐熷懡浠ゅ洖璋冪殑鎵ц銆佸弬鏁板鐞嗗拰閿欒绠$悊
 static scpi_bool_t processCommand(scpi_t * context) {
+    //杩欎釜鍦版柟淇敼涓� 璇籧omdlist->pattern
     const scpi_command_t * cmd = context->param_list.cmd;
+    //const scpi_command_t * cmd = context->cmdlist->pattern;
     lex_state_t * state = &context->param_list.lex_state;
     scpi_bool_t result = TRUE;
     // 鍏堟娴嬫槸鍚︿负鏌ヨ鍛戒护(浠�?缁撳熬)
@@ -177,17 +181,18 @@
  * @param context
  * @result TRUE if context->paramlist is filled with correct values
  */
-static scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len) {
-    int32_t i;
-    const scpi_command_t * cmd;
+scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len)
+{
 
-    for (i = 0; context->cmdlist[i].pattern != NULL; i++) {
-        cmd = &context->cmdlist[i];
-        if (matchCommand(cmd->pattern, header, len, NULL, 0, 0)) {
-            context->param_list.cmd = cmd;
+    for(int i=0;i<get_pattern_count();i++)
+    {
+        if( match_segments_global(header,i) )
+        {
+            context->param_list.cmd = &context->cmdlist[i];
             return TRUE;
         }
     }
+    context->SCPIerror = RETURN_NotFind;
     return FALSE;
 }
 
@@ -229,6 +234,7 @@
 
         if (state->programHeader.type == SCPI_TOKEN_INVALID) {
             SCPI_ErrorPush(context, SCPI_ERROR_INVALID_CHARACTER);
+            context->SCPIerror = RETURN_IllegalCommand;
             result = FALSE;
         } 
         else if (state->programHeader.len > 0) 
@@ -254,6 +260,7 @@
                 size_t r2 = r;
                 while (r2 > 0 && (data[r2 - 1] == '\r' || data[r2 - 1] == '\n')) r2--;
                 SCPI_ErrorPushEx(context, SCPI_ERROR_UNDEFINED_HEADER, data, r2);
+                context->SCPIerror = RETURN_UnDefine;
                 result = FALSE;
             }
         }
@@ -310,6 +317,8 @@
     context->buffer.data = input_buffer;
     context->buffer.length = input_buffer_length;
     context->buffer.position = 0;
+
+
     SCPI_ErrorInit(context, error_queue_data, error_queue_size);
 }
 
@@ -347,7 +356,8 @@
     data锛氳緭鍏ョ殑鏁版嵁
     len锛氳緭鍏ユ暟鎹殑闀垮害
  */
-scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len) {
+scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len)
+{
     scpi_bool_t result = FALSE;
     size_t totcmdlen = 0;
     int cmdlen = 0;
@@ -371,7 +381,8 @@
             context->buffer.position = 0;
             context->buffer.data[context->buffer.position] = 0;
             SCPI_ErrorPush(context, SCPI_ERROR_INPUT_BUFFER_OVERRUN);
-            return FALSE;
+            context->SCPIerror = RETURN_LengthOverFlow;
+            return result;
         }
         memcpy(&context->buffer.data[context->buffer.position], data, len);
         context->buffer.position += len;
@@ -1050,6 +1061,9 @@
     return result;
 }
 
+
+
+
 /**
  * Read signed/unsigned 32 bit integer parameter
  * @param context
@@ -1479,8 +1493,8 @@
  */
  //璇嗗埆瀹屾暣鐨凷CPI鍛戒护
 int scpiParser_detectProgramMessageUnit(scpi_parser_state_t * state, char * buffer, int len) {
-    lex_state_t lex_state;
-    scpi_token_t tmp;
+    lex_state_t lex_state = {};
+    scpi_token_t tmp = {};
     int result = 0;
     // 鍒濆鍖栬瘝娉曞垎鏋�
     lex_state.buffer = lex_state.pos = buffer;

--
Gitblit v1.9.1