-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for built-in commands that a shell needs
- Loading branch information
1 parent
cf96645
commit 2411121
Showing
13 changed files
with
801 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "builtin.h" | ||
|
||
#include <stdio.h> | ||
#include <errno.h> | ||
#include <unistd.h> | ||
#include <string.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(int argc, 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); | ||
} | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef BUILTIN_H | ||
#define BUILTIN_H | ||
|
||
void init_builtin_commands(); | ||
int run_builtin_command(int argc, char** argv); | ||
void deinit_builtin_commands(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#include "err_handling.h" | ||
#include <stdio.h> | ||
#include "vsh.h" | ||
|
||
void print_error(){ | ||
fprintf(stderr, "%s: %s: %s\n", me, last_argv[0], errstr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef ERROR_HANDLING_H | ||
#define ERROR_HANDLING_H | ||
|
||
void print_error(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "trie.h" | ||
#include "intern.h" | ||
|
||
int intern_init(struct intern *pool) | ||
{ | ||
pool->trie = trie_create(); | ||
return pool->trie == NULL ? -1 : 0; | ||
} | ||
|
||
|
||
static int visitor_free(const char *key, void *data, void *arg) | ||
{ | ||
(void) key; | ||
(void) arg; | ||
free(data); | ||
return 0; | ||
} | ||
|
||
int intern_free(struct intern *pool) | ||
{ | ||
trie_visit(pool->trie, "", visitor_free, NULL); | ||
return trie_free(pool->trie); | ||
} | ||
|
||
static void *inserter(const char *key, void *data, void *arg) | ||
{ | ||
char **dup = arg; | ||
if (data == NULL) { | ||
*dup = malloc(strlen(key) + 1); | ||
if (*dup != NULL) | ||
strcpy(*dup, key); | ||
return *dup; | ||
} else { | ||
return (*dup = data); | ||
} | ||
} | ||
|
||
const char *intern(struct intern *pool, const char *string) | ||
{ | ||
char *canonical = NULL; | ||
trie_replace(pool->trie, string, inserter, &canonical); | ||
return canonical; | ||
} | ||
|
||
const char *intern_soft(struct intern *pool, const char *string) | ||
{ | ||
return trie_search(pool->trie, string); | ||
} | ||
|
||
size_t intern_count(struct intern *pool) | ||
{ | ||
return trie_count(pool->trie, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#ifndef INTERN_H | ||
#define INTERN_H | ||
|
||
/** | ||
* String Pool Library | ||
* | ||
* Interns and allocates internal copies of provided strings. The | ||
* intern() function returns the canonical string for a given UTF-8, | ||
* NUL-terminated C string. This provides two significant advantages: | ||
* | ||
* 1. Canonicalized strings compare correctly with ==. No strcmp() is | ||
* required. | ||
* 2. Strings are only ever allocated once. For this to work properly, | ||
* canonicalized strings MUST be considered immutable. | ||
* | ||
* The underlying trie is intentionally exposed for direct access, | ||
* should any sort of trie-specific functionality be required. | ||
*/ | ||
|
||
struct intern { | ||
trie *trie; | ||
}; | ||
|
||
/** | ||
* Initializes a new string intern string pool. | ||
* @return 0 on success | ||
*/ | ||
int | ||
intern_init(struct intern *pool); | ||
|
||
/** | ||
* Frees all resources held by an intern string pool. | ||
* @return 0 on success | ||
*/ | ||
int | ||
intern_free(struct intern *pool); | ||
|
||
/** | ||
* Retrieves the canonical string, inserting one if necessary. | ||
* @return the canonical object for STRING, NULL on error | ||
*/ | ||
const char * | ||
intern(struct intern *pool, const char *string); | ||
|
||
/** | ||
* Get the canonical string without adding a new string to the pool. | ||
* @return the canonical object for STRING, NULL if none exists | ||
*/ | ||
const char * | ||
intern_soft(struct intern *pool, const char *string); | ||
|
||
/** | ||
* @return the number of strings in this pool | ||
*/ | ||
size_t | ||
intern_count(struct intern *pool); | ||
|
||
#endif |
Oops, something went wrong.