Allow string arguments to be quote within "".
[bertos.git] / bertos / mware / parser.c
index 142feba68c8a895b4c8932ccbc1c109707ac5c26..2197d8482391b8616ab23fcb755078440ece50c0 100644 (file)
@@ -89,17 +89,31 @@ DECLARE_HASHTABLE_STATIC(commands, CONFIG_MAX_COMMANDS_NUMBER, get_key_from_comm
  * \return True if a word was extracted, false if we got to the end
  * of the string without extracting any word.
  */
-static bool get_word(const char **begin, const char **end)
+bool get_word(const char **begin, const char **end)
 {
        const char *cur = *end;
+       bool open_quote = false;
 
        while ((*cur == ' ' || *cur == '\t') && *cur)
                ++cur;
 
+       if (*cur == '"')
+               open_quote = true;
+
        *begin = cur;
 
-       while ((*cur != ' ' && *cur != '\t') && *cur)
+       if (open_quote)
+       {
                ++cur;
+               while (*cur != '"' && *cur)
+                       ++cur;
+               if (*cur != '"')
+                       return false;
+               ++cur;
+       }
+       else
+               while ((*cur != ' ' && *cur != '\t') && *cur)
+                       ++cur;
 
        *end = cur;
 
@@ -137,7 +151,15 @@ static bool parseArgs(const char *fmt, const char *input, parms argv[])
                                break;
 
                        case 's':
-                               (*argv++).s = begin;
+                               (*argv).str.p = begin;
+                               (*argv).str.sz = end - begin;
+                               /* Remove the quotes from argument */
+                               if (*begin == '"' && *(end - 1) == '"')
+                               {
+                                       (*argv).str.p += 1;
+                                       (*argv).str.sz -= 2;
+                               }
+                               argv++;
                                break;
 
                        default:
@@ -264,7 +286,7 @@ bool parser_get_cmd_arguments(const char* input, const struct CmdTemplate* cmdp,
        if (!input)
                return false;
 
-       args[0].s = cmdp->name;
+       args[0].str.p = cmdp->name;
        if (!parseArgs(cmdp->arg_fmt, input, args + 1))
                return false;
 
@@ -309,10 +331,11 @@ bool parser_process_line(const char* input)
  * Register a new command into the parser
  *
  * \param cmd Command template describing the command
+ * \return true if registration was successful, false otherwise
  */
-void parser_register_cmd(const struct CmdTemplate* cmd)
+bool parser_register_cmd(const struct CmdTemplate* cmd)
 {
-       ht_insert(&commands, cmd);
+       return ht_insert(&commands, cmd);
 }
 
 void parser_init(void)