5个文件已修改
262 ■■■■ 已修改文件
libscpi/inc/scpi/parser.h 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/lexer.c 11 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/parser.c 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/utils.c 170 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/src/utils_private.h 72 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
libscpi/inc/scpi/parser.h
@@ -64,7 +64,7 @@
#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
    void SCPI_InitHeap(scpi_t * context, char * error_info_heap, size_t error_info_heap_length);
#endif
    scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len);
    scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len);
    scpi_bool_t SCPI_Parse(scpi_t * context, char * data, int len);
libscpi/src/lexer.c
@@ -409,9 +409,14 @@
 /*识别和分类SCPI命令头     
    公共命令头 以*识别  如  *IDN?
    复合命令头 以:识别 如  SYSTem:ERRor?
    命令查询  以?识别
        普通命令:MEAS:VOLT
        查询命令:MEAS:VOLT?
    命令查询  以?识别
    普通命令:MEAS:VOLT
    查询命令:MEAS:VOLT?
    @param state 词法解析器
    @param token 用于存储解析结果的token
    @return 返回解析结果的长度
 */
int scpiLex_ProgramHeader(lex_state_t * state, scpi_token_t * token) {
    
libscpi/src/parser.c
@@ -180,11 +180,14 @@
 * @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) {
scpi_bool_t findCommandHeader(scpi_t * context, const char * header, int len)
{
    int32_t i;
    const scpi_command_t * cmd = NULL;
    for (i = 0; i<context->cmdlistSize; i++) {
        cmd = &context->cmdlist[i];
        bool result =test_match(cmd->pattern, header);
        if(result)
        {
@@ -362,7 +365,7 @@
 */
scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len)
{
     scpi_bool_t result = FALSE;
    scpi_bool_t result = FALSE;
    size_t totcmdlen = 0;
    int cmdlen = 0;
    //当长度为0时  解析当前的数据 并且清空context的buff标记位
libscpi/src/utils.c
@@ -1145,107 +1145,105 @@
           ((val & 0xFF00000000000000ull) >> 56);
}
int parse_pattern(const char* pattern, Segment segments[], int max_segments) {
    int seg_count = 0;
    const char* p = pattern;
    while (*p && seg_count < max_segments)
    {
        if (*p == '[')
        {
            // 处理可变段
            segments[seg_count].is_variable = TRUE;
            // 跳过'['
            p++;
            int i = 0;
            while (*p && *p != ']' && i < sizeof(segments[seg_count].text)-1)
            {
                segments[seg_count].text[i++] = toupper(*p++);
            }
            segments[seg_count].text[i] = '\0';
            if (*p == ']')
            {
                // 跳过']'
                p++;
            }
        }
        else
        {
            // 处理固定段
            segments[seg_count].is_variable = FALSE;
            int i = 0;
            while (*p && *p != '[' && i < sizeof(segments[seg_count].text)-1)
            {
                segments[seg_count].text[i++] = toupper(*p++);
            }
            segments[seg_count].text[i] = '\0';
        }
        seg_count++;
    }
    return seg_count;
}
// 匹配命令与模式
bool match_command(const char* command, Segment segments[], int seg_count)
// 用于提取并处理选项的函数
int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags)
{
    const char* cmd = command;
    int current_seg = 0;
    const char* start = strchr(pattern, '<');  // 查找第一个 '<'
    const char* end = NULL;
    int tag_count = 0;
    while (*cmd && current_seg < seg_count)
    // 处理 <...> 内的选项
    while (start != NULL && tag_count < max_tags)
    {
        // 跳过命令中的分隔符(冒号)
        if (*cmd == ':')
        {
            cmd++;
            // 模式中也应该有对应的分隔符
            if (segments[current_seg].text[0] != ':')
            {
                return FALSE;
            }
            continue;
        end = strchr(start, '>');  // 查找对应的 '>'
        if (end == NULL) {
            break;  // 如果没有找到 '>',则退出
        }
        // 获取当前段长度
        size_t seg_len = strlen(segments[current_seg].text);
        // 提取 <...> 中的内容
        int len = end - start - 1;
        if (len > 0 && len < MAX_OPTION_LEN)
        {
            char buffer[MAX_OPTION_LEN];
            strncpy(buffer, start + 1, len);  // 复制 '<' 和 '>' 之间的内容
            buffer[len] = '\0';  // 结束符
        if (segments[current_seg].is_variable)
        {
            // 可变段 - 跳过对应长度的字符
            int i = 0;
            while (*cmd && *cmd != ':' && i < seg_len)
            // 处理 | 分隔符,提取多个选项
            char* token = strtok(buffer, "|");
            int option_count = 0;
            while (token && option_count < MAX_OPTION_LEN)
            {
                cmd++;
                i++;
                // 去除 token 中的 "[:", "]" 和空格
                char* p = token;
                while (*p == ' ' || *p == '[' || *p == ':') p++;  // 去除前导空格和 [:
                char* q = p + strlen(p) - 1;
                while (q > p && (*q == ' ' || *q == ']')) q--;  // 去除尾随空格和 ]
                *(q + 1) = '\0';  // 确保结尾是'\0'
                // 拷贝选项到 options 数组
                if (option_count < MAX_OPTION_LEN)
                {  // 确保不会越界
                    strncpy(options[tag_count][option_count], p, MAX_OPTION_LEN - 1);
                    options[tag_count][option_count][MAX_OPTION_LEN - 1] = '\0';  // 确保结束符
                    option_count++;
                }
                token = strtok(NULL, "|");
            }
        } else
        {
            // 固定段 - 必须精确匹配
            if (strncasecmp(cmd, segments[current_seg].text, seg_len) != 0)
            {
                return FALSE;
            }
            cmd += seg_len;
        }
        current_seg++;
        // 移动到下一个 '<' 位置,继续查找
        start = strchr(end + 1, '<');
        tag_count++;
    }
    // 检查是否处理完所有命令和所有段
    return (*cmd == '\0') && (current_seg == seg_count);
    return tag_count;
}
// 测试函数
// 用于检查输入是否能匹配每一组选项
int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags)
{
    char input_copy[MAX_INPUT_LEN];
    strncpy(input_copy, input, MAX_INPUT_LEN - 1);
    input_copy[MAX_INPUT_LEN - 1] = '\0';  // 确保结尾是'\0'
    // 拆分输入字符串,按 ":" 分割
    char* token = strtok(input_copy, ":");
    int group_idx = 0;
    // 对每一组进行匹配
    while (token != NULL && group_idx < num_tags)
    {
        int match_found = 0;
        // 检查当前组的每个选项是否与输入的 token 匹配
        printf("Checking input token: '%s' against group %d options\n", token, group_idx + 1); // Debug info
        for (int i = 0; i < MAX_OPTION_LEN && options[group_idx][i][0] != '\0'; i++)
        {
            printf("  Comparing with option: '%s'\n", options[group_idx][i]); // Debug info
            if (strcmp(options[group_idx][i], token) == 0) {
                match_found = 1;
                break;
            }
        }
        // 如果当前组的某个选项没有匹配上,返回 false
        if (!match_found) {
            return 0;  // 不匹配
        }
        // 处理下一个输入部分
        token = strtok(NULL, ":");
        group_idx++;
    }
    return (group_idx == num_tags);
}
// 测试匹配函数
bool test_match(const char* pattern, const char* command)
{
    Segment segments[16];
    int seg_count = parse_pattern(pattern, segments, 16);
    return (match_command(command, segments, seg_count));
    char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN] = { {{0}} };
    int num_tags = extract_required_options(pattern, options, MAX_TAGS);
    return match_input_to_options(command, options, num_tags);
}
libscpi/src/utils_private.h
@@ -60,62 +60,60 @@
#define LOCAL
#endif
    char * strnpbrk(const char *str, size_t size, const char *set) LOCAL;
    scpi_bool_t compareStr(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL;
    scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2, int32_t * num) LOCAL;
    size_t UInt32ToStrBaseSign(uint32_t val, char * str, size_t len, int8_t base, scpi_bool_t sign) LOCAL;
    size_t UInt64ToStrBaseSign(uint64_t val, char * str, size_t len, int8_t base, scpi_bool_t sign) LOCAL;
    size_t strBaseToInt32(const char * str, int32_t * val, int8_t base) LOCAL;
    size_t strBaseToUInt32(const char * str, uint32_t * val, int8_t base) LOCAL;
    size_t strBaseToInt64(const char * str, int64_t * val, int8_t base) LOCAL;
    size_t strBaseToUInt64(const char * str, uint64_t * val, int8_t base) LOCAL;
    size_t strToFloat(const char * str, float * val) LOCAL;
    size_t strToDouble(const char * str, double * val) LOCAL;
    scpi_bool_t locateText(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
    scpi_bool_t locateStr(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
    size_t skipWhitespace(const char * cmd, size_t len) LOCAL;
    scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len, int32_t * num) LOCAL;
    scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len, int32_t default_value) LOCAL;
    scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) LOCAL;
char * strnpbrk(const char *str, size_t size, const char *set) LOCAL;
scpi_bool_t compareStr(const char * str1, size_t len1, const char * str2, size_t len2) LOCAL;
scpi_bool_t compareStrAndNum(const char * str1, size_t len1, const char * str2, size_t len2, int32_t * num) LOCAL;
size_t UInt32ToStrBaseSign(uint32_t val, char * str, size_t len, int8_t base, scpi_bool_t sign) LOCAL;
size_t UInt64ToStrBaseSign(uint64_t val, char * str, size_t len, int8_t base, scpi_bool_t sign) LOCAL;
size_t strBaseToInt32(const char * str, int32_t * val, int8_t base) LOCAL;
size_t strBaseToUInt32(const char * str, uint32_t * val, int8_t base) LOCAL;
size_t strBaseToInt64(const char * str, int64_t * val, int8_t base) LOCAL;
size_t strBaseToUInt64(const char * str, uint64_t * val, int8_t base) LOCAL;
size_t strToFloat(const char * str, float * val) LOCAL;
size_t strToDouble(const char * str, double * val) LOCAL;
scpi_bool_t locateText(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
scpi_bool_t locateStr(const char * str1, size_t len1, const char ** str2, size_t * len2) LOCAL;
size_t skipWhitespace(const char * cmd, size_t len) LOCAL;
scpi_bool_t matchPattern(const char * pattern, size_t pattern_len, const char * str, size_t str_len, int32_t * num) LOCAL;
scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len, int32_t default_value) LOCAL;
scpi_bool_t composeCompoundCommand(const scpi_token_t * prev, scpi_token_t * current) LOCAL;
#define MAX_OPTION_LEN 256
#define MAX_TAGS 10
#define MAX_INPUT_LEN 256
int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags);
int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags);
bool test_match(const char* pattern, const char* command);
    typedef struct {
        bool is_variable;  // 是否为可变部分(用[]括起来的)
        char text[32];     // 段内容(不包含[])
    } Segment;
    int parse_pattern(const char* pattern, Segment segments[], int max_segments);
    bool match_command(const char* command, Segment segments[], int seg_count);
    bool test_match(const char* pattern, const char* command);
#define SCPI_DTOSTRE_UPPERCASE   1
#define SCPI_DTOSTRE_ALWAYS_SIGN 2
#define SCPI_DTOSTRE_PLUS_SIGN   4
    char * SCPI_dtostre(double __val, char * __s, size_t __ssize, unsigned char __prec, unsigned char __flags);
char * SCPI_dtostre(double __val, char * __s, size_t __ssize, unsigned char __prec, unsigned char __flags);
    scpi_array_format_t SCPI_GetNativeFormat(void);
    uint16_t SCPI_Swap16(uint16_t val);
    uint32_t SCPI_Swap32(uint32_t val);
    uint64_t SCPI_Swap64(uint64_t val);
scpi_array_format_t SCPI_GetNativeFormat(void);
uint16_t SCPI_Swap16(uint16_t val);
uint32_t SCPI_Swap32(uint32_t val);
uint64_t SCPI_Swap64(uint64_t val);
#if !HAVE_STRNLEN
    size_t BSD_strnlen(const char *s, size_t maxlen) LOCAL;
size_t BSD_strnlen(const char *s, size_t maxlen) LOCAL;
#endif
#if !HAVE_STRNCASECMP && !HAVE_STRNICMP
    int OUR_strncasecmp(const char *s1, const char *s2, size_t n) LOCAL;
int OUR_strncasecmp(const char *s1, const char *s2, size_t n) LOCAL;
#endif
#if USE_DEVICE_DEPENDENT_ERROR_INFORMATION && !USE_MEMORY_ALLOCATION_FREE
    void scpiheap_init(scpi_error_info_heap_t * heap, char * error_info_heap, size_t error_info_heap_length);
    char * scpiheap_strndup(scpi_error_info_heap_t * heap, const char *s, size_t n) LOCAL;
    void scpiheap_free(scpi_error_info_heap_t * heap, char *s, scpi_bool_t rollback) LOCAL;
    scpi_bool_t scpiheap_get_parts(scpi_error_info_heap_t * heap, const char *s1, size_t * len1, const char ** s2, size_t * len2) LOCAL;
void scpiheap_init(scpi_error_info_heap_t * heap, char * error_info_heap, size_t error_info_heap_length);
char * scpiheap_strndup(scpi_error_info_heap_t * heap, const char *s, size_t n) LOCAL;
void scpiheap_free(scpi_error_info_heap_t * heap, char *s, scpi_bool_t rollback) LOCAL;
scpi_bool_t scpiheap_get_parts(scpi_error_info_heap_t * heap, const char *s1, size_t * len1, const char ** s2, size_t * len2) LOCAL;
#endif
#if !HAVE_STRNDUP
    char *OUR_strndup(const char *s, size_t n);
char *OUR_strndup(const char *s, size_t n);
#endif
#ifndef min