summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefrag.am2
-rw-r--r--kern/arg.c162
-rw-r--r--kern/arg.h57
3 files changed, 221 insertions, 0 deletions
diff --git a/Makefrag.am b/Makefrag.am
index 90c146b8..55bf93b5 100644
--- a/Makefrag.am
+++ b/Makefrag.am
@@ -8,6 +8,8 @@ x15_SOURCES += \
include/string.h
x15_SOURCES += \
+ kern/arg.c \
+ kern/arg.h \
kern/assert.h \
kern/atomic.h \
kern/bitmap.c \
diff --git a/kern/arg.c b/kern/arg.c
new file mode 100644
index 00000000..02ed4b7b
--- /dev/null
+++ b/kern/arg.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2017 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <kern/arg.h>
+#include <kern/assert.h>
+#include <kern/init.h>
+#include <kern/macros.h>
+#include <kern/panic.h>
+
+/*
+ * Internally, all space characters are turned into null bytes so that
+ * values returned to callers directly point in the command line string,
+ * with no need for dynamic allocation.
+ */
+static char arg_cmdline[ARG_CMDLINE_MAX_SIZE] __initdata;
+static const char *arg_cmdline_end __initdata;
+
+void __init
+arg_setup(const char *cmdline)
+{
+ size_t i, length;
+
+ if (cmdline == NULL) {
+ arg_cmdline[0] = '\0';
+ return;
+ }
+
+ length = strlen(cmdline);
+
+ if ((length + 1) > ARRAY_SIZE(arg_cmdline)) {
+ panic("arg: command line too long");
+ }
+
+ memcpy(arg_cmdline, cmdline, length + 1);
+
+ for (i = 0; i < length; i++) {
+ if (arg_cmdline[i] == ' ') {
+ arg_cmdline[i] = '\0';
+ }
+ }
+
+ arg_cmdline_end = arg_cmdline + length;
+}
+
+void __init
+arg_info(void)
+{
+ printf("arg: %s\n", arg_cmdline);
+}
+
+static const char * __init
+arg_walk(const char *s)
+{
+ if (s == NULL) {
+ s = arg_cmdline;
+ } else {
+ for (;;) {
+ if (s >= arg_cmdline_end) {
+ return NULL;
+ }
+
+ if (*s == '\0') {
+ break;
+ }
+
+ s++;
+ }
+ }
+
+ for (;;) {
+ if (s >= arg_cmdline_end) {
+ return NULL;
+ }
+
+ if (*s != '\0') {
+ return s;
+ }
+
+ s++;
+ }
+}
+
+static const char * __init
+arg_find_name_end(const char *arg)
+{
+ const char *end;
+
+ end = strchr(arg, '=');
+
+ if (end == NULL) {
+ end = arg + strlen(arg);
+ }
+
+ return end;
+}
+
+static const char * __init
+arg_find(const char *name)
+{
+ const char *arg, *arg_name_end;
+ size_t arg_name_length, name_length;
+
+ name_length = strlen(name);
+ assert(name_length != 0);
+
+ for (arg = arg_walk(NULL); arg != NULL; arg = arg_walk(arg)) {
+ arg_name_end = arg_find_name_end(arg);
+ arg_name_length = arg_name_end - arg;
+
+ if ((arg_name_length == name_length)
+ && (memcmp(arg, name, name_length) == 0)) {
+ return arg;
+ }
+ }
+
+ return NULL;
+}
+
+bool __init
+arg_present(const char *name)
+{
+ return (arg_find(name) != NULL);
+}
+
+const char * __init
+arg_value(const char *name)
+{
+ const char *arg, *value;
+
+ arg = arg_find(name);
+
+ if (arg == NULL) {
+ return NULL;
+ }
+
+ value = strchr(arg, '=');
+
+ if (value == NULL) {
+ return "";
+ }
+
+ return value + 1;
+}
diff --git a/kern/arg.h b/kern/arg.h
new file mode 100644
index 00000000..937f596b
--- /dev/null
+++ b/kern/arg.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Kernel command line argument parsing.
+ *
+ * Arguments are separated by spaces (there is no escape character).
+ * They can be of the form "name" when used as boolean values (present
+ * or not), or "name=value".
+ */
+
+#ifndef _KERN_ARG_H
+#define _KERN_ARG_H
+
+#include <stdbool.h>
+
+#define ARG_CMDLINE_MAX_SIZE 256
+
+/*
+ * Initialize the arg module.
+ */
+void arg_setup(const char *cmdline);
+
+/*
+ * Display command line information.
+ */
+void arg_info(void);
+
+/*
+ * Return true if an argument with the given name is present in the
+ * command line.
+ */
+bool arg_present(const char *name);
+
+/*
+ * Return the value of the argument with the given name in the command
+ * line.
+ *
+ * If the argument form is "name", the empty string is returned. If the
+ * argument isn't present, NULL is returned.
+ */
+const char * arg_value(const char *name);
+
+#endif /* _KERN_ARG_H */