/** * Parse one command line * @param context * @param data - complete command line * @param len - command line length * @return 1 if the last evaluated command was found */ int SCPI_Parse(scpi_t * context, char * data, int len) { int result = 0; scpi_parser_state_t * state; int r; scpi_token_t cmd_prev = {SCPI_TOKEN_UNKNOWN, NULL, 0}; if (context == NULL) { return -1; } state = &context->parser_state; context->output_count = 0; while (1) { result = 0; r = scpiParser_detectProgramMessageUnit(state, data, len); if (state->programHeader.type == SCPI_TOKEN_INVALID) { SCPI_ErrorPush(context, SCPI_ERROR_INVALID_CHARACTER); } else if (state->programHeader.len > 0) { composeCompoundCommand(&cmd_prev, &state->programHeader); if (findCommandHeader(context, state->programHeader.ptr, state->programHeader.len)) { context->param_list.lex_state.buffer = state->programData.ptr; context->param_list.lex_state.pos = context->param_list.lex_state.buffer; context->param_list.lex_state.len = state->programData.len; context->param_list.cmd_raw.data = state->programHeader.ptr; context->param_list.cmd_raw.position = 0; context->param_list.cmd_raw.length = state->programHeader.len; processCommand(context); result = 1; cmd_prev = state->programHeader; } else { SCPI_ErrorPush(context, SCPI_ERROR_UNDEFINED_HEADER); } } if (r < len) { data += r; len -= r; } else { break; } } /* conditionaly write new line */ writeNewLine(context); return result; }
/** * Interface to the application. Adds data to system buffer and try to search * command line termination. If the termination is found or if len=0, command * parser is called. * * @param context * @param data - data to process * @param len - length of data * @return */ scpi_bool_t SCPI_Input(scpi_t * context, const char * data, int len) { scpi_bool_t result = TRUE; size_t totcmdlen = 0; int cmdlen = 0; if (len == 0) { context->buffer.data[context->buffer.position] = 0; result = SCPI_Parse(context, context->buffer.data, context->buffer.position); context->buffer.position = 0; } else { int buffer_free; buffer_free = context->buffer.length - context->buffer.position; if (len > (buffer_free - 1)) { /* Input buffer overrun - invalidate buffer */ context->buffer.position = 0; context->buffer.data[context->buffer.position] = 0; SCPI_ErrorPush(context, SCPI_ERROR_INPUT_BUFFER_OVERRUN); return FALSE; } memcpy(&context->buffer.data[context->buffer.position], data, len); context->buffer.position += len; context->buffer.data[context->buffer.position] = 0; while (1) { cmdlen = scpiParser_detectProgramMessageUnit(&context->parser_state, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); totcmdlen += cmdlen; if (context->parser_state.termination == SCPI_MESSAGE_TERMINATION_NL) { result = SCPI_Parse(context, context->buffer.data, totcmdlen); memmove(context->buffer.data, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); context->buffer.position -= totcmdlen; totcmdlen = 0; } else { if (context->parser_state.programHeader.type == SCPI_TOKEN_UNKNOWN) break; if (totcmdlen >= context->buffer.position) break; } } } return result; }
/** * Interface to the application. Adds data to system buffer and try to search * command line termination. If the termination is found or if len=0, command * parser is called. * * @param context * @param data - data to process * @param len - length of data * @return */ int SCPI_Input(scpi_t * context, const char * data, int len) { int result = 0; size_t totcmdlen = 0; int cmdlen = 0; if (len == 0) { context->buffer.data[context->buffer.position] = 0; result = SCPI_Parse(context, context->buffer.data, context->buffer.position); context->buffer.position = 0; } else { int buffer_free; buffer_free = context->buffer.length - context->buffer.position; if (len > (buffer_free - 1)) { return -1; } memcpy(&context->buffer.data[context->buffer.position], data, len); context->buffer.position += len; context->buffer.data[context->buffer.position] = 0; while (1) { cmdlen = scpiParser_detectProgramMessageUnit(&context->parser_state, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); totcmdlen += cmdlen; if (context->parser_state.termination == SCPI_MESSAGE_TERMINATION_NL) { result = SCPI_Parse(context, context->buffer.data, totcmdlen); memmove(context->buffer.data, context->buffer.data + totcmdlen, context->buffer.position - totcmdlen); context->buffer.position -= totcmdlen; totcmdlen = 0; } else { if (context->parser_state.programHeader.type == SCPI_TOKEN_UNKNOWN) break; if (totcmdlen >= context->buffer.position) break; } } } return result; }