From 776e657b84793d5ad8687ea07c5526652b0791af Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sun, 6 Dec 2020 03:58:34 +0500 Subject: [PATCH] Add example for command line parser [skip-ci] --- .gitignore | 1 + Makefile.am | 5 +++++ examples/cmdline.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 examples/cmdline.c diff --git a/.gitignore b/.gitignore index 553529e..21a2e03 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ /tests/test*.log /tests/test*.trs +/examples/cmdline /tests/multiboot2_print1 /tests/multiboot2_print2 /tests/test_cmdline diff --git a/Makefile.am b/Makefile.am index e69efd7..6c5de13 100644 --- a/Makefile.am +++ b/Makefile.am @@ -14,6 +14,7 @@ TESTS = \ noinst_PROGRAMS = \ $(TESTS) \ + examples/cmdline \ tests/multiboot2_print1 \ tests/multiboot2_print2 @@ -30,6 +31,10 @@ if ARCH_X86 libkernaux_a_SOURCES += src/arch/x86.S endif +examples_cmdline_SOURCES = \ + $(libkernaux_a_SOURCES) \ + examples/cmdline.c + tests_multiboot2_print1_SOURCES = \ $(libkernaux_a_SOURCES) \ tests/multiboot2_print1.c diff --git a/examples/cmdline.c b/examples/cmdline.c new file mode 100644 index 0000000..6635fc6 --- /dev/null +++ b/examples/cmdline.c @@ -0,0 +1,37 @@ +#include + +#include +#include +#include + +static const unsigned int ARGV_COUNT_MAX = 100; +static const unsigned int ARG_SIZE_MAX = 4096; + +static const char *const cmdline = "foo bar\\ car"; + +int main() +{ + char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX]; + unsigned int argc; + char *argv[ARGV_COUNT_MAX]; + char buffer[ARGV_COUNT_MAX * ARG_SIZE_MAX]; + + assert(kernaux_cmdline_parse( + cmdline, + error_msg, + &argc, + argv, + buffer, + ARGV_COUNT_MAX, + ARG_SIZE_MAX + )); + + assert(strcmp(error_msg, "") == 0); + assert(argc == 2); + assert(strcmp(argv[0], "foo") == 0); + assert(strcmp(argv[1], "bar car") == 0); + + printf("OK!\n"); + + return 0; +}