Resolve #54: Detect not typed numbers in SCPI_CommandNumbers
| | |
| | | static scpi_result_t TEST_Numbers(scpi_t * context) { |
| | | int32_t numbers[2]; |
| | | |
| | | SCPI_CommandNumbers(context, numbers, 2); |
| | | SCPI_CommandNumbers(context, numbers, 2, 1); |
| | | |
| | | fprintf(stderr, "TEST numbers %d %d\r\n", numbers[0], numbers[1]); |
| | | |
| | |
| | | scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd); |
| | | int32_t SCPI_CmdTag(scpi_t * context); |
| | | scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len); |
| | | scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len); |
| | | scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len, int32_t default_value); |
| | | |
| | | #ifdef __cplusplus |
| | | } |
| | |
| | | |
| | | for (i = 0; context->cmdlist[i].pattern != NULL; i++) { |
| | | cmd = &context->cmdlist[i]; |
| | | if (matchCommand(cmd->pattern, header, len, NULL, 0)) { |
| | | if (matchCommand(cmd->pattern, header, len, NULL, 0, 0)) { |
| | | context->param_list.cmd = cmd; |
| | | return TRUE; |
| | | } |
| | |
| | | } |
| | | |
| | | pattern = context->param_list.cmd->pattern; |
| | | return matchCommand (pattern, cmd, strlen (cmd), NULL, 0); |
| | | return matchCommand (pattern, cmd, strlen (cmd), NULL, 0, 0); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len) { |
| | | return matchCommand (pattern, value, len, NULL, 0); |
| | | return matchCommand (pattern, value, len, NULL, 0, 0); |
| | | } |
| | | |
| | | scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len) { |
| | | return matchCommand (context->param_list.cmd->pattern, context->param_list.cmd_raw.data, context->param_list.cmd_raw.length, numbers, len); |
| | | scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len, int32_t default_value) { |
| | | return matchCommand (context->param_list.cmd->pattern, context->param_list.cmd_raw.data, context->param_list.cmd_raw.length, numbers, len, default_value); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | if (num) { |
| | | if (len1 == len2) { |
| | | *num = 1; |
| | | //*num = 1; |
| | | } else { |
| | | int32_t tmpNum; |
| | | i = len1 + strToLong(str2 + len1, &tmpNum, 10); |
| | |
| | | * @param len - max search length |
| | | * @return TRUE if pattern matches, FALSE otherwise |
| | | */ |
| | | scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len) { |
| | | scpi_bool_t matchCommand(const char * pattern, const char * cmd, size_t len, int32_t *numbers, size_t numbers_len, int32_t default_value) { |
| | | scpi_bool_t result = FALSE; |
| | | int leftFlag = 0; // flag for '[' on left |
| | | int rightFlag = 0; // flag for ']' on right |
| | |
| | | if ((pattern_sep_pos > 0) && pattern_ptr[pattern_sep_pos - 1] == '#') { |
| | | if (numbers && (numbers_idx < numbers_len)) { |
| | | number_ptr = numbers + numbers_idx; |
| | | *number_ptr = 1; // default value |
| | | *number_ptr = default_value; // default value |
| | | } else { |
| | | number_ptr = NULL; |
| | | } |
| | |
| | | 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) 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; |
| | | |
| | | #if !HAVE_STRNLEN |
| | |
| | | |
| | | #define TEST_COMPARE_STR_AND_NUM(s1, l1, s2, l2, v, r) \ |
| | | do { \ |
| | | num = 0; \ |
| | | num = -1; \ |
| | | CU_ASSERT_EQUAL(compareStrAndNum(s1, l1, s2, l2, &num),r); \ |
| | | CU_ASSERT_EQUAL(num, v); \ |
| | | } while(0); \ |
| | | |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd", 4, 1, TRUE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd", 4, -1, TRUE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd1", 5, 1, TRUE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd123", 7, 123, TRUE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd12A", 7, 0, FALSE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcdB12", 7, 0, FALSE); |
| | | TEST_COMPARE_STR_AND_NUM("abdd", 4, "abcd132", 7, 0, FALSE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcd12A", 7, -1, FALSE); |
| | | TEST_COMPARE_STR_AND_NUM("abcd", 4, "abcdB12", 7, -1, FALSE); |
| | | TEST_COMPARE_STR_AND_NUM("abdd", 4, "abcd132", 7, -1, FALSE); |
| | | } |
| | | |
| | | static void test_matchPattern() { |
| | |
| | | |
| | | #define TEST_MATCH_COMMAND(p, s, r) \ |
| | | do { \ |
| | | result = matchCommand(p, s, strlen(s), NULL, 0); \ |
| | | result = matchCommand(p, s, strlen(s), NULL, 0, 0); \ |
| | | CU_ASSERT_EQUAL(result, r); \ |
| | | } while(0) \ |
| | | |
| | |
| | | do { \ |
| | | int32_t evalues[] = {NOPAREN v}; \ |
| | | unsigned int cnt = (sizeof(evalues)/4); \ |
| | | result = matchCommand(p, s, strlen(s), values, 20); \ |
| | | result = matchCommand(p, s, strlen(s), values, 20, -1); \ |
| | | CU_ASSERT_EQUAL(result, r); \ |
| | | {unsigned int i; for (i = 0; i<cnt; i++) { \ |
| | | CU_ASSERT_EQUAL(evalues[i], values[i]); \ |
| | |
| | | TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "outp1:fm2", TRUE); // test numeric parameter |
| | | TEST_MATCH_COMMAND("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE); // test numeric parameter |
| | | |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp3:mod10:fm", TRUE, (3, 10, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output3:mod10:fm", TRUE, (3, 10, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp30:modulation:fm5", TRUE, (30, 1, 5)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output:mod:fm", TRUE, (1, 1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm", TRUE, (3, 1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:mod10:fm", TRUE, (3, 10, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm2", TRUE, (3, 1, 2)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE, (1, 1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp3:mod10:fm", TRUE, (3, 10, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output3:mod10:fm", TRUE, (3, 10, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "outp30:modulation:fm5", TRUE, (30, -1, 5)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM#", "output:mod:fm", TRUE, (-1, -1, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm", TRUE, (3, -1, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:mod10:fm", TRUE, (3, 10, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "outp3:fm2", TRUE, (3, -1, 2)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM#", "output:fm", TRUE, (-1, -1, -1)); // test numeric parameter |
| | | |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp3:mod:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output3:mod:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp3:mod:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output3:mod:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "outp30:modulation:fm5", TRUE, (30, 5)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output:mod:fm", TRUE, (1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:mod:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation:FM#", "output:mod:fm", TRUE, (-1, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:mod:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "outp3:fm2", TRUE, (3, 2)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "output:fm", TRUE, (1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation]:FM#", "output:fm", TRUE, (-1, -1)); // test numeric parameter |
| | | |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp3:mod10:fm", TRUE, (3, 10)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output3:mod10:fm", TRUE, (3, 10)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp30:modulation:fm", TRUE, (30, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output:mod:fm", TRUE, (1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "outp30:modulation:fm", TRUE, (30, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#:MODulation#:FM", "output:mod:fm", TRUE, (-1, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:mod10:fm", TRUE, (3, 10)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, (3, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "output:fm", TRUE, (1, 1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "outp3:fm", TRUE, (3, -1)); // test numeric parameter |
| | | TEST_MATCH_COMMAND2("OUTPut#[:MODulation#]:FM", "output:fm", TRUE, (-1, -1)); // test numeric parameter |
| | | } |
| | | |
| | | static void test_composeCompoundCommand(void) { |