Use hashmap for args
There, now it's fast.
This commit is contained in:
parent
68941f6afd
commit
26cfa78f89
@ -1,13 +1,6 @@
|
||||
#ifndef KERNEL_ARGS_H
|
||||
#define KERNEL_ARGS_H
|
||||
|
||||
struct kernel_arg {
|
||||
char * name;
|
||||
char * value;
|
||||
};
|
||||
|
||||
list_t * kernel_args_list;
|
||||
|
||||
int args_present(char * karg);
|
||||
char * args_value(char * karg);
|
||||
void args_parse(char * _arg);
|
||||
|
@ -11,11 +11,12 @@
|
||||
#include <system.h>
|
||||
#include <logging.h>
|
||||
#include <args.h>
|
||||
#include <hashmap.h>
|
||||
#include <tokenize.h>
|
||||
|
||||
char * cmdline = NULL;
|
||||
|
||||
list_t * kernel_args_list = NULL;
|
||||
hashmap_t * kernel_args_map = NULL;
|
||||
|
||||
/**
|
||||
* Check if an argument was provided to the kernel. If the argument is
|
||||
@ -24,32 +25,18 @@ list_t * kernel_args_list = NULL;
|
||||
* so the caller should check whether it is correctly set.
|
||||
*/
|
||||
int args_present(char * karg) {
|
||||
if (!kernel_args_list) return 0; /* derp */
|
||||
if (!kernel_args_map) return 0; /* derp */
|
||||
|
||||
foreach(n, kernel_args_list) {
|
||||
struct kernel_arg * arg = (struct kernel_arg *)n->value;
|
||||
if (!strcmp(arg->name, karg)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return hashmap_has(kernel_args_map, karg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value associated with an argument provided to the kernel.
|
||||
*/
|
||||
char * args_value(char * karg) {
|
||||
if (!kernel_args_list) return NULL; /* derp */
|
||||
if (!kernel_args_map) return 0; /* derp */
|
||||
|
||||
foreach(n, kernel_args_list) {
|
||||
struct kernel_arg * arg = (struct kernel_arg *)n->value;
|
||||
if (!strcmp(arg->name, karg)) {
|
||||
return arg->value;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
return hashmap_get(kernel_args_map, karg);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,33 +55,32 @@ void args_parse(char * _arg) {
|
||||
/* New let's parse the tokens into the arguments list so we can index by key */
|
||||
/* TODO I really need a dictionary/hashmap implementation */
|
||||
|
||||
if (kernel_args_list) {
|
||||
/* Uh, crap. You've called me already... */
|
||||
list_destroy(kernel_args_list);
|
||||
list_free(kernel_args_list);
|
||||
if (!kernel_args_map) {
|
||||
kernel_args_map = hashmap_create(10);
|
||||
}
|
||||
kernel_args_list = list_create();
|
||||
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
char * c = strdup(argv[i]);
|
||||
|
||||
struct kernel_arg * karg = malloc(sizeof(struct kernel_arg));
|
||||
karg->name = c;
|
||||
karg->value = NULL;
|
||||
char * name;
|
||||
char * value;
|
||||
|
||||
name = c;
|
||||
value = NULL;
|
||||
/* Find the first = and replace it with a null */
|
||||
char * v = c;
|
||||
while (*v) {
|
||||
if (*v == '=') {
|
||||
*v = '\0';
|
||||
v++;
|
||||
karg->value = v;
|
||||
value = v;
|
||||
goto _break;
|
||||
}
|
||||
v++;
|
||||
}
|
||||
|
||||
_break:
|
||||
list_insert(kernel_args_list, karg);
|
||||
hashmap_set(kernel_args_map, name, value);
|
||||
}
|
||||
|
||||
free(arg);
|
||||
|
@ -7,6 +7,8 @@
|
||||
#include <process.h>
|
||||
#include <version.h>
|
||||
#include <termios.h>
|
||||
#include <tokenize.h>
|
||||
#include <hashmap.h>
|
||||
|
||||
#include <debug_shell.h>
|
||||
|
||||
@ -114,7 +116,7 @@ struct shell_command {
|
||||
char * description;
|
||||
};
|
||||
|
||||
#define NUM_COMMANDS 6
|
||||
#define NUM_COMMANDS 8
|
||||
static struct shell_command shell_commands[NUM_COMMANDS];
|
||||
|
||||
/*
|
||||
@ -181,6 +183,33 @@ static int shell_ls(fs_node_t * tty, int argc, char * argv[]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int shell_test_hash(fs_node_t * tty, int argc, char * argv[]) {
|
||||
|
||||
fs_printf(tty, "Creating a hash...\n");
|
||||
|
||||
hashmap_t * map = hashmap_create(2);
|
||||
|
||||
hashmap_set(map, "a", (void *)1);
|
||||
hashmap_set(map, "b", (void *)2);
|
||||
hashmap_set(map, "c", (void *)3);
|
||||
|
||||
fs_printf(tty, "value at a: %d\n", (int)hashmap_get(map, "a"));
|
||||
fs_printf(tty, "value at b: %d\n", (int)hashmap_get(map, "b"));
|
||||
fs_printf(tty, "value at c: %d\n", (int)hashmap_get(map, "c"));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int shell_log_on(fs_node_t * tty, int argc, char * argv[]) {
|
||||
kprint_to_serial = 1;
|
||||
if (argc < 2) {
|
||||
debug_level = 1;
|
||||
} else {
|
||||
debug_level = atoi(argv[1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct shell_command shell_commands[NUM_COMMANDS] = {
|
||||
{"shell", &shell_create_userspace_shell,
|
||||
"Runs a userspace shell on this tty."},
|
||||
@ -192,6 +221,10 @@ static struct shell_command shell_commands[NUM_COMMANDS] = {
|
||||
"Change current directory."},
|
||||
{"ls", &shell_ls,
|
||||
"List files in current or other directory."},
|
||||
{"test-hash", &shell_test_hash,
|
||||
"Test hashmap functionality."},
|
||||
{"log-on", &shell_log_on,
|
||||
"Enable serial logging."},
|
||||
{NULL, NULL, NULL}
|
||||
};
|
||||
|
||||
@ -348,6 +381,8 @@ void debug_shell_run(void * data, char * name) {
|
||||
char * argv[1024]; /* Command tokens (space-separated elements) */
|
||||
int argc = tokenize(arg, " ", argv);
|
||||
|
||||
if (!argc) continue;
|
||||
|
||||
/* Parse the command string */
|
||||
struct shell_command * sh = &shell_commands[0];
|
||||
while (sh->name) {
|
||||
|
Loading…
Reference in New Issue
Block a user