From fd11d77426a87b106c0034073a611a1a95a28bad Mon Sep 17 00:00:00 2001 From: johncoleman83 Date: Thu, 20 Apr 2017 06:29:58 +0000 Subject: [PATCH] updated betty --- aliasB.c | 8 ----- editfile | 16 ++++++++++ executeA.c | 25 ++++++++-------- executeB.c | 88 ------------------------------------------------------ file_io.c | 2 +- getline.c | 4 +-- header.h | 13 ++++---- main.c | 2 +- pipeline.c | 31 ++++--------------- ptree.c | 2 +- structs.h | 47 +++++++++++++++++++---------- write.c | 1 + 12 files changed, 80 insertions(+), 159 deletions(-) create mode 100644 editfile diff --git a/aliasB.c b/aliasB.c index c82e7f3..59fb33c 100644 --- a/aliasB.c +++ b/aliasB.c @@ -60,11 +60,9 @@ int load_alias(arg_inventory_t *arginv) home_node = fetch_node(arginv->envlist, "HOME"); home = home_node->val; lenhome = _strlen(home); - file = safe_malloc(sizeof(char) * (_strlen(home) + lenname + 1)); file = _strncat(file, home, lenhome); file = _strncat(file, name, lenname); - fd = open(file, O_RDONLY); if (fd == -1) { @@ -72,22 +70,16 @@ int load_alias(arg_inventory_t *arginv) return (1); } buffer = (char *)safe_malloc(sz); - while ((count = _readline(fd, &buffer, &sz)) != 0) { while (buffer[count - 1] == '\n') buffer[count - 1] = '\0'; - val = buffer; - while (*val && *val != ':') val++; - if (!*val) break; - *(val++) = '\0'; - add_node_alias(&arginv->alias, buffer, val); } free(file); diff --git a/editfile b/editfile new file mode 100644 index 0000000..5ab6d95 --- /dev/null +++ b/editfile @@ -0,0 +1,16 @@ +structs.h:176: warning: No description found for parameter or member 'commands' +structs.h:176: warning: No description found for parameter or member 'st_mode' +structs.h:176: warning: No description found for parameter or member 'history' +structs.h:176: warning: No description found for parameter or member 'alias' +structs.h:176: warning: No description found for parameter or member 'tokens' +structs.h:176: warning: No description found for parameter or member 'parser' +structs.h:176: warning: No description found for parameter or member 'pipeline' +structs.h:176: warning: No description found for parameter or member 'n_bg_jobs' +structs.h:176: warning: No description found for parameter or member 'pipein' +structs.h:176: warning: No description found for parameter or member 'pipeout' +structs.h:176: warning: No description found for parameter or member 'io_redir' +structs.h:176: warning: No description found for parameter or member 'filename' +structs.h:176: warning: No description found for parameter or member 'last_exit_code' +structs.h:176: warning: No description found for parameter or member 'last_bg_pid' +structs.h:176: warning: No description found for parameter or member 'exit' +structs.h:176: warning: No description found for parameter or member 'exit_status' diff --git a/executeA.c b/executeA.c index 91f761f..4e4f3df 100644 --- a/executeA.c +++ b/executeA.c @@ -34,14 +34,14 @@ int exec_builtins(arg_inventory_t *arginv) } } /* - if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir == - TOKEN_APPEND || arginv->pipeout) - { - safe_dup2(old_stdout, STDOUT_FILENO); - - close(old_stdout); - } - */ + * if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir == + * TOKEN_APPEND || arginv->pipeout) + * { + * safe_dup2(old_stdout, STDOUT_FILENO); + * + * close(old_stdout); + * } + */ return (retval); } @@ -89,10 +89,11 @@ pid_t exec_path(char *command, arg_inventory_t *arginv) { _environ = zelda_to_ganondorf(arginv->envlist); /* - redirect_output(arginv, 1); - if (redirect_input(arginv)) - exec_error_exit("No such file or directory\n", command, _environ, arginv); - */ + * redirect_output(arginv, 1); + * if (redirect_input(arginv)) + * exec_error_exit("No such file or directory\n", command, + *_environ, arginv); + */ if (execve(command, (char **)arginv->commands, _environ) < 0) exec_error_exit("No Command\n", command, _environ, arginv); } diff --git a/executeB.c b/executeB.c index 79e95b9..d87a509 100644 --- a/executeB.c +++ b/executeB.c @@ -1,93 +1,5 @@ #include "header.h" -/** - * safe_dup2 - make a dup2 and exit if an error is found - * @new_fd: file descriptor to use - * @old_fd: file descriptor to replace - * - */ -/* -void safe_dup2(int new_fd, int old_fd) -{ - if (dup2(new_fd, old_fd) < 0) - { - _perror("cannot duplicate\n"); - exit(1); - } -} -*/ - -/** - * redirect_output - redirect stdout depending on the pipeline & redirect token - * @arginv: arguments inventory - * @close_dup: if 1 closes duplicated fd - * - * Return: old stdout file descriptor - */ -/*int redirect_output(arg_inventory_t *arginv, int close_dup) -{ - int stdout_fd; - int old_stdout; - - if (arginv->io_redir == TOKEN_REWRITE || arginv->io_redir == TOKEN_APPEND) - { - if (arginv->io_redir == TOKEN_REWRITE) - stdout_fd = open(arginv->filename, O_WRONLY | O_CREAT | O_TRUNC, - 0666); - else - stdout_fd = open(arginv->filename, O_WRONLY | O_CREAT | O_APPEND, - 0666); - old_stdout = dup(STDOUT_FILENO); - safe_dup2(stdout_fd, STDOUT_FILENO); - if (close_dup) - { - close(stdout_fd); - if (arginv->pipeout) - close(arginv->pipeout); - } - } - else if (arginv->pipeout) - { - old_stdout = dup(STDOUT_FILENO); - - safe_dup2(arginv->pipeout, STDOUT_FILENO); - } - return (old_stdout); -} -*/ -/** - * redirect_input - redirect stdin depending on the pipeline & redirect token - * @arginv: arguments inventory - * - */ -/* -int redirect_input(arg_inventory_t *arginv) -{ - int stdin_fd; - - if (arginv->io_redir == TOKEN_CAT) - { - stdin_fd = open(arginv->filename, O_RDONLY); - - if (stdin_fd < 0) - return -1; - - safe_dup2(stdin_fd, STDIN_FILENO); - - close(stdin_fd); - - if (arginv->pipein) - close(arginv->pipein); - } - else if (arginv->pipein) - { - safe_dup2(arginv->pipein, STDIN_FILENO); - } - - return (0); -} -*/ - /** * is_path - checks if input command is part of directory PATH * @command: a command diff --git a/file_io.c b/file_io.c index 57e545a..421fd7b 100644 --- a/file_io.c +++ b/file_io.c @@ -40,7 +40,7 @@ ssize_t read_textfile(char *filename, size_t letters) } /** - * append_text_to_file - appends text to the end of a file + * trunc_text_to_file - appends text to the end of a file * @filename: name of file to have text appended to * @text_content: text to be appended to the file * diff --git a/getline.c b/getline.c index 5d88015..b44449d 100644 --- a/getline.c +++ b/getline.c @@ -18,13 +18,13 @@ ssize_t _getline(char **buffer, size_t *limit) free(*buffer); exit(EXT_SUCCESS); } - return (count); } /** - * _readline - custom getline currently reads 1 char at a time from a file descriptor + * _readline - custom getline currently reads 1 char at a time from a file + * descriptor * @fd: file descriptor of the file to use for reading * @buffer: address of pointer to input commands buffer * @limit: maxsize of input character string, realloc if necessary diff --git a/header.h b/header.h index 30adb4a..84b5308 100644 --- a/header.h +++ b/header.h @@ -42,7 +42,7 @@ extern char **environ; ssize_t _getline(char **buffer, size_t *limit); arg_inventory_t *buildarginv(void); int _filemode(int fd); -ssize_t _readline(int fd,char **buffer, size_t *limit); +ssize_t _readline(int fd, char **buffer, size_t *limit); /* ---------------execute--------------- */ pid_t execute(arg_inventory_t *arginv); @@ -92,7 +92,8 @@ int _unsetenv(arg_inventory_t *arginv); char *_strcat(char *dest, char *src); char *int_to_str(unsigned int n); void replace_str(char **old_str, char *new_str, int i, int j, int flg); -char *_str_replace(char *string, unsigned int start, unsigned int end, char *rep); +char *_str_replace(char *string, unsigned int start, unsigned int end, + char *rep); /* -----custom C std lib----- */ char _isspace(char c); @@ -146,12 +147,14 @@ void free_paths(char **paths); /* ---------------ptree--------------- */ ptree_t *ptree_new_node(ptree_t *parent); -ptree_t *ptree_new_string_node(ptree_t *parent, tokens_t *tokens, unsigned int *cur_token); +ptree_t *ptree_new_string_node(ptree_t *parent, tokens_t *tokens, + unsigned int *cur_token); int delete_ptree(ptree_t *node); /* ---------------parser--------------- */ int parse_error(token_t *near); -ptree_t *parse_expr(unsigned int *ntoken, tokens_t *tokens, ptree_t *lhs, int min_prec); +ptree_t *parse_expr(unsigned int *ntoken, tokens_t *tokens, ptree_t *lhs, + int min_prec); int parse(parser_t *parser, tokens_t *tokens); int delete_parser(parser_t *parser); void expand_bash_vars(arg_inventory_t *arginv); @@ -163,7 +166,7 @@ int init_pipeline_push_processes(pipeline_t *pipeline, ptree_t *tree); int init_pipeline(pipeline_t *pipeline, ptree_t *ptree); int worker_execute_core(arg_inventory_t *arginv); int worker_execute(arg_inventory_t *arginv); -int delete_pipeline(pipeline_t * pipeline); +int delete_pipeline(pipeline_t *pipeline); /* ---------------free--------------- */ int freeall(arg_inventory_t *arginv); diff --git a/main.c b/main.c index 2af28b3..e1b07d7 100644 --- a/main.c +++ b/main.c @@ -58,7 +58,7 @@ int main(void) { if (arginv->st_mode) write(STDOUT_FILENO, "$ ", 2); - if(!_getline(&arginv->input_commands, &arginv->buflimit)) + if (!_getline(&arginv->input_commands, &arginv->buflimit)) break; add_node_history(&arginv->history, arginv->input_commands); diff --git a/pipeline.c b/pipeline.c index 45c6f0e..5e4f0ad 100644 --- a/pipeline.c +++ b/pipeline.c @@ -17,11 +17,12 @@ unsigned int init_pipeline_count_processes(ptree_t *tree) if (tree->token_id == TOKEN_STRING) return (1); - return (init_pipeline_count_processes(tree->left) + init_pipeline_count_processes(tree->right)); + return (init_pipeline_count_processes(tree->left) + + init_pipeline_count_processes(tree->right)); } /** - * init_pipeline_push_processes - fetches everything and puts into pipeline struct + * init_pipeline_push_processes - fetches everything & puts to pipeline struct * @pipeline: pointer to pipeline * @tree: ptree to fetch from * @@ -33,56 +34,40 @@ int init_pipeline_push_processes(pipeline_t *pipeline, ptree_t *tree) if (!tree) return (0); - if (is_redirection(tree->token_id)) { pipeline->processes[pipeline->processesN].ptree = tree; pipeline->processes[pipeline->processesN].io_redir = tree->token_id; - filename = safe_malloc(strlen(tree->right->strings[0]) + 1); - - /* copy filename from right child */ _strcpy(filename, (char *)tree->right->strings[0]); pipeline->processes[pipeline->processesN].filename = filename; pipeline->processesN++; - tree->token_id = TOKEN_STRING; tree->strings = tree->left->strings; tree->stringsN = tree->left->stringsN; tree->left->strings = NULL; tree->left->stringsN = 0; - - /* clean up */ delete_ptree(tree->left); delete_ptree(tree->right); - tree->left = NULL; - tree->right = NULL; - + tree->left = NULL, tree->right = NULL; return (0); } - if (tree->token_id == TOKEN_STRING) { - /* Adds string to pipeline */ pipeline->processes[pipeline->processesN].ptree = tree; pipeline->processes[pipeline->processesN].io_redir = 0; pipeline->processes[pipeline->processesN].filename = NULL; pipeline->processesN++; - init_pipeline_push_processes(pipeline, tree->left); init_pipeline_push_processes(pipeline, tree->right); - return (0); } - if (tree->token_id == TOKEN_PIPE) { - /* Initialize everything from left to right, order matters */ init_pipeline_push_processes(pipeline, tree->left); init_pipeline_push_processes(pipeline, tree->right); return (0); } - return (0); } @@ -108,28 +93,22 @@ int init_pipeline(pipeline_t *pipeline, ptree_t *ptree) /* allocate memory */ pipeline->processes = safe_malloc(mem_needed * sizeof(process_t)); - if (!pipeline->processes) { _perror("Memory allocation failed!\n"); exit(1); } - /* initializes background service values */ pipeline->background = 0; pipeline->background_pid = 0; - /* initalizes actual process */ pipeline->processesN = 0; - init_pipeline_push_processes(pipeline, ptree); - if (pipeline->processesN != mem_needed) { _perror("uhhhhh number of commands does not match\n"); exit(1); } - for (i = 0; i < pipeline->processesN; i++) pipeline->processes[i].pid = 0; @@ -144,7 +123,7 @@ int init_pipeline(pipeline_t *pipeline, ptree_t *ptree) */ int delete_pipeline(pipeline_t *pipeline) { - unsigned i; + unsigned int i; for (i = 0; i < pipeline->processesN; i++) if (pipeline->processes[i].filename != NULL) diff --git a/ptree.c b/ptree.c index 77069f6..02f1abd 100644 --- a/ptree.c +++ b/ptree.c @@ -90,7 +90,7 @@ int delete_ptree(ptree_t *node) if (node->strings) { i = 0; - while(node->strings[i]) + while (node->strings[i]) { free(node->strings[i]); i++; diff --git a/structs.h b/structs.h index 068e6e8..848e1fb 100644 --- a/structs.h +++ b/structs.h @@ -5,17 +5,17 @@ * struct token - categorizes a token * @id: id of token from TOKEN_ macros * @str: token + * @prec: the prec */ typedef struct token { - int id; - char *str; - int prec; + int id; + char *str; + int prec; } token_t; /** * struct tokens - struct for tokenizing string - * @data: initial string separated with '\0', hold all the tokens * @tokens: array of token_t structs with tokenized strings and ids * @tokensN: amount of tokens parsed * @@ -24,15 +24,16 @@ typedef struct token */ typedef struct tokens { - token_t *tokens; - unsigned int tokensN; + token_t *tokens; + unsigned int tokensN; } tokens_t; /** - * struct token_id + * struct token_id - baby token * @token_id: numerical id * @token_str: the exact token string to be compared with * @token_descr: for debugging + * @precedence: the precedence */ typedef struct token_id { @@ -72,9 +73,12 @@ typedef struct parser } parser_t; /** - * struct process - this attributes a parse tree with TOKEN_STRING id with a process id + * struct process - this attributes a parse tree with TOKEN_STRING id with a + * process id * @ptree: ptree to process * @pid: pid + * @io_redir: io_redir + * @filename: filename */ typedef struct process { @@ -104,6 +108,7 @@ typedef struct pipeline /** * struct env - struct for holding custom environmental variables list * @var: environmental variable and value separated by '=' char + * @val: value of env var * @next: pointer to the next env variable node */ typedef struct env @@ -144,6 +149,22 @@ typedef struct alias * @input_commands: string of input commands * @envlist: custom davinci environ linked list * @buflimit: buflimit max of 1024 chars + * @commands: double pointer to commands list + * @st_mode: st_mode either FIFO or terminal + * @history: linked list of history + * @alias: linked list of aliases + * @tokens: tokens list + * @parser: a parse + * @pipeline: pipline list + * @n_bg_jobs: pipeline + * @pipein: pipein variable + * @pipeout: pipeout variable + * @io_redir: io redirection + * @filename: the filename + * @last_exit_code: last exit code + * @last_bg_pid: last pid + * @exit: indicator to exit or not + * @exit_status: the exit status */ typedef struct arg_inventory { @@ -154,19 +175,15 @@ typedef struct arg_inventory int st_mode; history_t *history; alias_t *alias; - tokens_t tokens; parser_t parser; pipeline_t pipeline; - int n_bg_jobs; - int pipein; - int pipeout; - + int pipeout; /* 0 for no redirection, 3 for >, 4 >>, 5 < */ int io_redir; - char *filename; + char *filename; int last_exit_code; pid_t last_bg_pid; @@ -189,7 +206,7 @@ typedef struct _builtins /** * struct bins - matches command to appropriate builtin function * @function: string of function name - * @file: string of name of file + * @help: string of name of file */ typedef struct bins { diff --git a/write.c b/write.c index cdac0eb..612ce4d 100644 --- a/write.c +++ b/write.c @@ -9,6 +9,7 @@ unsigned int write_history(history_t *head) { unsigned int i = 0; + while (head) { write_uint(head->number);