From 67aba6718f372eaf48a90385f492aac7ee4e4855 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 2 Dec 2020 01:55:09 +0500 Subject: [PATCH] Some basic implementation of command line parser --- src/cmdline.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/cmdline.c b/src/cmdline.c index 6831156..58dbb9c 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -31,5 +31,34 @@ kernaux_bool kernaux_cmdline_parse( kernaux_memset(buffer, '\0', argv_count_max * arg_size_max); + if (cmdline[0] == '\0') { + return KERNAUX_TRUE; + } + + unsigned int start = 0; + + for (unsigned int index = 1; ; ++index) { + const char prev = cmdline[index - 1]; + const char cur = cmdline[index]; + + if ((cur == ' ' || cur == '\0') && prev != ' ') { + const unsigned size = index - start + 1; + + // TODO: check size + + argv[(*argc)++] = buffer; + kernaux_strncpy(buffer, &cmdline[start], size - 1); + buffer += size; + } + + if (prev == ' ' && cur != ' ' && cur != '\0') { + start = index; + } + + if (cur == '\0') { + break; + } + } + return KERNAUX_TRUE; }