| | |
| | | #include "scpi/config.h" |
| | | #include "scpi/parser.h" |
| | | #include "scpi/utils_private.h" |
| | | #include "scpi/utils.h" |
| | | #include "scpi/error.h" |
| | | #include "scpi/constants.h" |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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 |
| | | */ |
| | |
| | | const scpi_command_t * cmd = context->paramlist.cmd; |
| | | |
| | | context->cmd_error = FALSE; |
| | | |
| | | /* conditionaly write ; */ |
| | | writeSemicolon(context); |
| | | |
| | | context->output_count = 0; |
| | | context->input_count = 0; |
| | | |
| | |
| | | SCPI_ErrorPush(context, SCPI_ERROR_EXECUTION_ERROR); |
| | | } |
| | | } |
| | | |
| | | /* conditionaly write new line */ |
| | | writeNewLine(context); |
| | | |
| | | /* skip all whitespaces */ |
| | | paramSkipWhitespace(context); |
| | |
| | | |
| | | for (i = 0; context->cmdlist[i].pattern != NULL; i++) { |
| | | cmd = &context->cmdlist[i]; |
| | | if (matchCommand(cmd->pattern, cmdline_ptr, cmd_len)) { |
| | | if (matchCommand(cmd->pattern, cmdline_ptr, cmd_len, NULL, 0)) { |
| | | context->paramlist.cmd = cmd; |
| | | context->paramlist.parameters = cmdline_ptr + cmd_len; |
| | | context->paramlist.length = cmdline_len - cmd_len; |
| | |
| | | return -1; |
| | | } |
| | | |
| | | context->output_count = 0; |
| | | |
| | | while (cmdline_ptr < cmdline_end) { |
| | | result = 0; |
| | | cmd_len = cmdTerminatorPos(cmdline_ptr, cmdline_end - cmdline_ptr); |
| | |
| | | cmdline_ptr += skipCmdLine(cmdline_ptr, cmdline_end - cmdline_ptr); |
| | | cmdline_ptr += skipWhitespace(cmdline_ptr, cmdline_end - cmdline_ptr); |
| | | } |
| | | |
| | | /* conditionaly write new line */ |
| | | writeNewLine(context); |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | size_t SCPI_ResultInt(scpi_t * context, int32_t val) { |
| | | char buffer[12]; |
| | | size_t result = 0; |
| | | size_t len = longToStr(val, buffer, sizeof (buffer)); |
| | | size_t len = SCPI_LongToStr(val, buffer, sizeof (buffer), 10); |
| | | result += writeDelimiter(context); |
| | | result += writeData(context, buffer, len); |
| | | context->output_count++; |
| | |
| | | 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++; |
| | |
| | | return FALSE; |
| | | } |
| | | |
| | | paramSkipWhitespace(context); |
| | | if (locateText(context->paramlist.parameters, context->paramlist.length, value, &length)) { |
| | | paramSkipBytes(context, length); |
| | | paramSkipBytes(context, length + 2); |
| | | paramSkipWhitespace(context); |
| | | if (len) { |
| | | *len = length; |
| | | } |
| | |
| | | return FALSE; |
| | | } |
| | | |
| | | if (matchPattern("ON", 2, param, param_len)) { |
| | | if (matchPattern("ON", 2, param, param_len, NULL)) { |
| | | *value = TRUE; |
| | | } else if (matchPattern("OFF", 3, param, param_len)) { |
| | | } else if (matchPattern("OFF", 3, param, param_len, NULL)) { |
| | | *value = FALSE; |
| | | } else { |
| | | num_len = strToLong(param, &i); |
| | |
| | | } |
| | | |
| | | for (res = 0; options[res]; ++res) { |
| | | if (matchPattern(options[res], strlen(options[res]), param, param_len)) { |
| | | if (matchPattern(options[res], strlen(options[res]), param, param_len, NULL)) { |
| | | *value = res; |
| | | return TRUE; |
| | | } |
| | |
| | | return FALSE; |
| | | } |
| | | |
| | | scpi_bool_t SCPI_IsCmd(scpi_t * context, const char * cmd) { |
| | | if (! context->paramlist.cmd) { |
| | | return FALSE; |
| | | } |
| | | |
| | | const char * pattern = context->paramlist.cmd->pattern; |
| | | return matchCommand (pattern, cmd, strlen (cmd), NULL, 0); |
| | | } |
| | | |
| | | scpi_bool_t SCPI_Match(const char * pattern, const char * value, size_t len) { |
| | | return matchCommand (pattern, value, len, NULL, 0); |
| | | } |
| | | |
| | | scpi_bool_t SCPI_CommandNumbers(scpi_t * context, int32_t * numbers, size_t len) { |
| | | return matchCommand (context->paramlist.cmd->pattern, context->paramlist.cmd_raw.data, context->paramlist.cmd_raw.length, numbers, len); |
| | | } |