| | |
| | | |
| | | |
| | | // 用于提取并处理选项的函数 |
| | | int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags) |
| | | { |
| | | int extract_required_options(const char* pattern, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int max_tags) { |
| | | const char* start = strchr(pattern, '<'); // 查找第一个 '<' |
| | | const char* end = NULL; |
| | | int tag_count = 0; |
| | |
| | | |
| | | // 提取 <...> 中的内容 |
| | | int len = end - start - 1; |
| | | if (len > 0 && len < MAX_OPTION_LEN) |
| | | { |
| | | if (len > 0 && len < MAX_OPTION_LEN) { |
| | | char buffer[MAX_OPTION_LEN]; |
| | | strncpy(buffer, start + 1, len); // 复制 '<' 和 '>' 之间的内容 |
| | | buffer[len] = '\0'; // 结束符 |
| | |
| | | // 处理 | 分隔符,提取多个选项 |
| | | char* token = strtok(buffer, "|"); |
| | | int option_count = 0; |
| | | while (token && option_count < MAX_OPTION_LEN) |
| | | { |
| | | while (token && option_count < MAX_OPTION_LEN) { |
| | | // 去除 token 中的 "[:", "]" 和空格 |
| | | char* p = token; |
| | | while (*p == ' ' || *p == '[' || *p == ':') p++; // 去除前导空格和 [: |
| | |
| | | *(q + 1) = '\0'; // 确保结尾是'\0' |
| | | |
| | | // 拷贝选项到 options 数组 |
| | | if (option_count < MAX_OPTION_LEN) |
| | | { // 确保不会越界 |
| | | 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++; |
| | |
| | | } |
| | | |
| | | // 用于检查输入是否能匹配每一组选项 |
| | | int match_input_to_options(const char* input, char options[MAX_TAGS][MAX_OPTION_LEN][MAX_OPTION_LEN], int num_tags) |
| | | { |
| | | 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' |
| | |
| | | int group_idx = 0; |
| | | |
| | | // 对每一组进行匹配 |
| | | while (token != NULL && group_idx < num_tags) |
| | | { |
| | | 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++) |
| | | { |
| | | 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; |
| | |
| | | return (group_idx == num_tags); |
| | | } |
| | | |
| | | |
| | | // 测试匹配函数 |
| | | bool test_match(const char* pattern, const char* command) |
| | | { |