-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin.c
72 lines (61 loc) · 1.51 KB
/
builtin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "builtin.h"
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "envmgr.h"
#include "vsh.h"
#include "trie/trie.h"
static trie* cmd_map;
typedef int (*cmd_func_t)(int, char**);
typedef struct func_ary_elt{
cmd_func_t function;
char* cmd_name;
} func_ary_elt_t;
int shell_exit(__attribute__((unused))int argc, __attribute__((unused))char** argv){
running = 0;
return 0;
}
int change_directory(int argc, char** argv){
if(argc == 1){
//Return to ~ if home directory is set,
//or do nothing
puts("Home directory not set");
return 0;
}
int toreturn = chdir(argv[1]);
last_cmd_errno = errno;
if(toreturn){
errstr = strerror(last_cmd_errno);
}else{
update_pwd();
}
return toreturn;
}
static func_ary_elt_t func_ary[] = {
{&shell_exit, "exit"},
{&change_directory, "cd"},
{NULL, NULL}
};
void init_builtin_commands(){
cmd_map = trie_create();
for(func_ary_elt_t* i = func_ary; i->function != NULL; ++i){
trie_insert(cmd_map, i->cmd_name, &(i->function));
}
}
int run_builtin_command(int argc, char** argv){
builtin_cmd_found = 1;
//You can't cast a function pointer as void*, but you
//can cast the pointer to a function pointer as void*
cmd_func_t* to_run = (cmd_func_t*) trie_search(cmd_map, argv[0]);
if(!to_run){
builtin_cmd_found = 0;
return 0;
}
cmd_func_t func_to_run = *to_run;
last_cmd_code = func_to_run(argc, argv);
return last_cmd_code;
}
void deinit_builtin_commands(){
trie_free(cmd_map);
}