add doxygen docs

This commit is contained in:
2021-07-26 02:47:25 -04:00
parent 9ab95c799e
commit 6aca7ae42e
23 changed files with 2728 additions and 9 deletions

9
report/code/builtins.txt Normal file
View File

@@ -0,0 +1,9 @@
void run_builtin(StringArray *args) {
if (strcmp(args->array[0], "exit") == 0) {
exit_shell(args);
}
}
void exit_shell(StringArray *args) {
exit(EXIT_SUCCESS);
}

33
report/code/environ.txt Normal file
View File

@@ -0,0 +1,33 @@
typedef struct {
StringArray *keys;
StringArray *values;
size_t size;
} ArrayList;
void set_array_list(ArrayList *array_list, char *key, char *value) {
if (array_list->keys == NULL) {
array_list->keys = create_string_array();
array_list->values = create_string_array();
}
for (size_t i = 0; i < array_list->size; i++) {
if (strcmp(array_list->keys->array[i], key) == 0) {
array_list->values->array[i] = realloc(array_list->values->array[i], strlen(value) * sizeof(char *));
strcpy(array_list->values->array[i], value);
return;
}
}
insert_string_array(array_list->keys, key);
insert_string_array(array_list->values, value);
array_list->size++;
}
char *get_array_list(ArrayList *array_list, char *key) {
if (array_list->keys != NULL) {
for (size_t i = 0; i < array_list->size; i++) {
if (strcmp(array_list->keys->array[i], key) == 0) {
return array_list->values->array[i];
}
}
}
return NULL;
}

20
report/code/launch.txt Normal file
View File

@@ -0,0 +1,20 @@
void launch_program(StringArray *args) {
pid_t child = 0;
child = fork();
if (child == 0) {
if (execvp(args->array[0], args->array) == -1) {
fprintf(stderr, "%s: command not found\n", args->array[0]);
free_string_array(args);
exit(EXIT_FAILURE);
}
}
else if (child < 0) {
perror("fork");
}
else {
int child_status;
waitpid(child, &child_status, 0);
}
}

25
report/code/loop.txt Normal file
View File

@@ -0,0 +1,25 @@
void loop() {
while (1) {
print_input_line();
char *line = get_console_input();
StringArray args;
create_string_array(&args);
char *saveptr = NULL;
char *token = strtok_r(line, " ", &saveptr);
while (token) {
insert_string_array(&args, token);
token = strtok_r(NULL, " ", &saveptr);
}
if (args.size == 0) {
continue;
}
if (is_builtin(args.array[0])) {
run_builtin(&args);
}
}
}

View File

@@ -0,0 +1,44 @@
typedef struct {
int fd;
int fd_new;
int fd_copy;
char *filename;
int flags;
} Redirect;
Redirect in = {.fd = STDIN_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_RDONLY};
Redirect out = {.fd = STDOUT_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
Redirect err = {.fd = STDERR_FILENO, .fd_new = -1, .fd_copy = -1, .filename = NULL, .flags = O_WRONLY | O_CREAT | O_TRUNC};
void open_redirect(Redirect *redirect) {
redirect->fd_copy = dup(redirect->fd);
if (redirect->fd_copy == -1) {
perror("dup");
}
else {
redirect->fd_new = open(redirect->filename, redirect->flags, 0664);
if (redirect->fd_new == -1) {
fprintf(stderr, "open: Could not open file %s: \"%s\"\n", redirect->filename, strerror(errno));
}
else {
if (close(redirect->fd) == -1) {
perror("close");
}
if (dup(redirect->fd_new) == -1) {
perror("dup");
}
}
}
}
void close_redirect(Redirect *redirect) {
if (close(redirect->fd_new) == -1) {
perror("close");
}
if (dup2(redirect->fd_copy, redirect->fd) == -1) {
perror("dup2");
}
if (close(redirect->fd_copy) == -1) {
perror("close");
}
}