cli: Share a common tokenize_cmd() implementation
[mech_eap.git] / src / common / cli.c
index 64c0159..b583d1c 100644 (file)
@@ -235,3 +235,33 @@ fail:
        printf("Too long command\n");
        return -1;
 }
+
+
+int tokenize_cmd(char *cmd, char *argv[])
+{
+       char *pos;
+       int argc = 0;
+
+       pos = cmd;
+       for (;;) {
+               while (*pos == ' ')
+                       pos++;
+               if (*pos == '\0')
+                       break;
+               argv[argc] = pos;
+               argc++;
+               if (argc == max_args)
+                       break;
+               if (*pos == '"') {
+                       char *pos2 = os_strrchr(pos, '"');
+                       if (pos2)
+                               pos = pos2 + 1;
+               }
+               while (*pos != '\0' && *pos != ' ')
+                       pos++;
+               if (*pos == ' ')
+                       *pos++ = '\0';
+       }
+
+       return argc;
+}