From 9718defcaa134c9413a985811f60d9a48414302e Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 25 Mar 2023 12:02:26 +0200 Subject: [PATCH] Add an argument parser --- Library/targp.c | 30 ++++++++++++++++++++++++++++++ include/targp.h | 15 +++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 Library/targp.c create mode 100644 include/targp.h diff --git a/Library/targp.c b/Library/targp.c new file mode 100644 index 0000000..6a40017 --- /dev/null +++ b/Library/targp.c @@ -0,0 +1,30 @@ +#include + +#include +#include + +void targp_parse(const char *cmd, char **argv, int *argc) +{ + const char delim[] = " "; + + char *token = strtok((char *)cmd, delim); + while (token != NULL) + { + char *quote = strchr(token, '"'); + if (quote != NULL) + { + memmove(quote, quote + 1, strlen(quote)); + char *endQuote = strchr(quote, '"'); + if (endQuote != NULL) + *endQuote = '\0'; + } + + char *arg = (char *)kmalloc(strlen(token) + 1); + strcpy(arg, token); + + argv[(*argc)++] = arg; + + token = strtok(NULL, delim); + } + argv[(*argc)] = NULL; +} diff --git a/include/targp.h b/include/targp.h new file mode 100644 index 0000000..eba8407 --- /dev/null +++ b/include/targp.h @@ -0,0 +1,15 @@ +#ifndef TinyArgumentParser_H__ +#define TinyArgumentParser_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + void targp_parse(const char *cmd, char **argv, int *argc); + +#ifdef __cplusplus +} +#endif + +#endif // !TinyArgumentParser_H__