Example #1
0
int16_t ecmd_parse_command(char *cmd, char *output, uint16_t len)
{

#ifdef DEBUG_ECMD
    debug_printf("called ecmd_parse_command %s\n", cmd);
#endif

#ifdef ALIASCMD_SUPPORT
    if (cmd[0] == '$') { // alias command names start with $
#ifdef DEBUG_ECMD
        debug_printf("try alias\n");
#endif
	if (aliascmd_decode(cmd) == NULL) {
	    // command not found in alias list
#ifdef DEBUG_ECMD
	    debug_printf("Alias failed\n");
#endif
	}else{
#ifdef DEBUG_ECMD
            debug_printf("new command: %s\n", cmd);
#endif
	}
    }
#endif

    if (strlen(cmd) < 2) {
#ifdef DEBUG_ECMD
        debug_printf("cmd is too short\n");
#endif
        return 0;
    }

    int ret = -1;

    char *text = NULL;
    int16_t (*func)(char*, char*, uint16_t) = NULL;
    uint8_t pos = 0;

    while (1) {
        /* load pointer to text */
        text = (char *)pgm_read_word(&ecmd_cmds[pos].name);

#ifdef DEBUG_ECMD
        debug_printf("loaded text addres %p: \n", text);
#endif

        /* return if we reached the end of the array */
        if (text == NULL)
            break;

#ifdef DEBUG_ECMD
        debug_printf("text is: \"%S\"\n", text);
#endif

        /* else compare texts */
        if (memcmp_P(cmd, text, strlen_P(text)) == 0) {
#ifdef DEBUG_ECMD
            debug_printf("found match\n");
#endif
            cmd += strlen_P(text);
            func = (void *)pgm_read_word(&ecmd_cmds[pos].func);
            break;
        }

        pos++;
    }

#ifdef DEBUG_ECMD
    debug_printf("rest cmd: \"%s\"\n", cmd);
#endif

    if (func != NULL)
        ret = func(cmd, output, len);

    if (output != NULL) {
        if (ret == -1) {
            memcpy_P(output, PSTR("parse error"), 11);
            ret = 11;
        }
        else if (ret == 0) {
            output[0] = 'O';
            output[1] = 'K';
            ret = 2;
        }
    }

    return ret;
}
Example #2
0
int16_t
ecmd_parse_command(char *cmd, char *output, uint16_t len)
{
#ifdef DEBUG_ECMD
  debug_printf("called ecmd_parse_command %s\n", cmd);
#endif
#ifdef ECMD_LOG_VIA_SYSLOG
  if (0 == strchr(cmd, ECMD_STATE_MAGIC))
    syslog_sendf_P(PSTR("ecmd: %s"), cmd);
#endif

#ifdef ECMD_REMOVE_BACKSPACE_SUPPORT
  uint8_t i = 0;
  while (cmd[i] != '\0' && cmd[i] != ECMD_STATE_MAGIC &&
         i < ECMD_OUTPUTBUF_LENGTH)
  {                             // search until end of string
    if (cmd[i] == '\b')
    {                           // check cmd for backspaces
      uint16_t cmdlen = strlen(cmd + i);
      memmove(cmd + i - 1, cmd + i + 1, cmdlen);        // we found a backspace, so we move all chars backwards
      i--;                      // and decrement char counter
    }
    else
    {
      i++;                      // goto char
    }
  }
#endif /* ECMD_REMOVE_BACKSPACE_SUPPORT */

#ifdef ALIASCMD_SUPPORT
  if (cmd[0] == '$')
  {                             // alias command names start with $
#ifdef DEBUG_ECMD
    debug_printf("try alias\n");
#endif
    if (aliascmd_decode(cmd) == NULL)
    {
      // command not found in alias list
#ifdef DEBUG_ECMD
      debug_printf("Alias failed\n");
#endif
    }
    else
    {
#ifdef DEBUG_ECMD
      debug_printf("new command: %s\n", cmd);
#endif
    }
  }
#endif /* ALIASCMD_SUPPORT */

  if (strlen(cmd) < 2)
  {
#ifdef DEBUG_ECMD
    debug_printf("cmd is too short\n");
#endif
    return 0;
  }

  int ret = -1;

  char *text = NULL;
  int16_t(*func) (char *, char *, uint16_t) = NULL;
  uint8_t pos = 0;

  while (1)
  {
    /* load pointer to text */
    text = (char *) pgm_read_word(&ecmd_cmds[pos].name);

#ifdef DEBUG_ECMD
    debug_printf("loaded text addres %p: \n", text);
#endif

    /* return if we reached the end of the array */
    if (text == NULL)
      break;

#ifdef DEBUG_ECMD
    debug_printf("text is: \"%S\"\n", text);
#endif

    /* else compare texts */
    if (memcmp_P(cmd, text, strlen_P(text)) == 0)
    {
#ifdef DEBUG_ECMD
      debug_printf("found match\n");
#endif
      cmd += strlen_P(text);
      func = (void *) pgm_read_word(&ecmd_cmds[pos].func);
      break;
    }

    pos++;
  }

#ifdef DEBUG_ECMD
  debug_printf("rest cmd: \"%s\"\n", cmd);
#endif

  ACTIVITY_LED_ECMD;

  if (func != NULL)
    ret = func(cmd, output, len);

  if (output != NULL)
  {
    if (ret == -1)
    {
      memcpy_P(output, PSTR("parse error"), 11);
      ret = 11;
    }
    else if (ret == 0)
    {
      output[0] = 'O';
      output[1] = 'K';
      ret = 2;
    }
  }

  return ret;
}