This repository has been archived on 2024-05-28. You can view files and clone it, but cannot push or open issues or pull requests.
2023-11-14 16:25:09 -05:00

40 lines
1.1 KiB
C
Executable File

#ifndef COMMAND_H
#define COMMAND_H
#include <stdint.h>
static void command_parse(char *data, uint32_t len, char **argv, uint8_t *argc) {
// merge all space to one space and remove the space at the beginning and end
uint32_t new_ptr = 0;
uint8_t argc_tmp = 0;
for (uint32_t i = 0; i < len; i++) {
if (data[i] != ' ' && data[i] != '\r' && data[i] != '\n') {
data[new_ptr] = data[i];
new_ptr++;
} else {
data[i] = ' ';
if (new_ptr != 0 && data[new_ptr - 1] != ' ') {
data[new_ptr] = data[i];
new_ptr++;
argc_tmp++;
}
}
}
if (data[new_ptr] == ' ')
data[new_ptr] = '\0';
else if (new_ptr >= 2 && data[new_ptr - 2] == ' ')
data[new_ptr - 2] = '\0';
// char **argv = (char **) malloc(sizeof(char *) * (argc_tmp));
argv[0] = data;
uint8_t argc_iter = 1;
for (uint32_t i = 1; i < len; i++) {
if (data[i] == ' ') {
data[i] = '\0';
argv[argc_iter++] = data + i + 1;
}
}
*argc = argc_tmp;
}
#endif