Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
tbm-mcu/source/shell/args.c

75 lines
991 B

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <shell.h>
size_t count_args(const char *line)
{
const char *p = line;
size_t n = 0;
while (*p != '\0') {
p += strspn(p, " \n\r");
while (*p != '\0' && *p != ' ') {
if (*p++ != '\"')
continue;
while (*p != '\0' && *p != '\"')
++p;
if (*p == '\"')
++p;
}
++n;
}
return n;
}
char **parse_args(const char *line, size_t *argc)
{
char **argv;
char **arg;
const char *p = line;
char *s;
size_t n;
if (!argc)
argc = &n;
*argc = count_args(line);
if (!(argv = malloc(*argc * sizeof(char *) + strlen(line) + 1)))
return NULL;
arg = argv;
s = (char *)argv + *argc * sizeof(char *);
while (*p != '\0') {
p += strspn(p, " \n\r");
*arg++ = s;
while (*p != '\0' && *p != ' ') {
if (*p != '\"') {
*s++ = *p++;
continue;
}
++p;
while (*p != '\0' && *p != '\"')
*s++ = *p++;
if (*p == '\"')
++p;
}
*s++ = '\0';
}
return argv;
}