123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- #include "private.h"
- #include "lub/string.h"
- #include "lub/argv.h"
- #include <assert.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- static clish_shell_builtin_fn_t
- clish_close,
- clish_overview,
- clish_source,
- clish_source_nostop,
- clish_history;
- static clish_shell_builtin_t clish_cmd_list[] = {
- {"clish_close", clish_close},
- {"clish_overview", clish_overview},
- {"clish_source", clish_source},
- {"clish_source_nostop", clish_source_nostop},
- {"clish_history", clish_history},
- {NULL, NULL}
- };
- static bool_t clish_close(const clish_shell_t * shell, const lub_argv_t * argv)
- {
-
- clish_shell_t *this = (clish_shell_t *) shell;
- argv = argv;
- this->state = SHELL_STATE_CLOSING;
- return BOOL_TRUE;
- }
- static bool_t
- clish_source_internal(const clish_shell_t * shell,
- const lub_argv_t * argv, bool_t stop_on_error)
- {
- bool_t result = BOOL_FALSE;
- const char *filename = lub_argv__get_arg(argv, 0);
- struct stat fileStat;
- FILE *file;
-
- clish_shell_t *this = (clish_shell_t *) shell;
-
- if (0 == stat((char *)filename, &fileStat)) {
- if (!S_ISDIR(fileStat.st_mode)) {
- file = fopen(filename, "r");
- if (NULL != file) {
-
- result =
- clish_shell_push_file((clish_shell_t *)
- this, file,
- stop_on_error);
- if (BOOL_FALSE == result) {
-
- fclose(file);
- }
- }
- }
- }
- return result;
- }
- static bool_t clish_source(const clish_shell_t * shell, const lub_argv_t * argv)
- {
- return (clish_source_internal(shell, argv, BOOL_TRUE));
- }
- static bool_t
- clish_source_nostop(const clish_shell_t * shell, const lub_argv_t * argv)
- {
- return (clish_source_internal(shell, argv, BOOL_FALSE));
- }
- static bool_t
- clish_overview(const clish_shell_t * this, const lub_argv_t * argv)
- {
- argv = argv;
- tinyrl_printf(this->tinyrl, "%s\n", this->overview);
- return BOOL_TRUE;
- }
- static bool_t clish_history(const clish_shell_t * this, const lub_argv_t * argv)
- {
- tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
- tinyrl_history_iterator_t iter;
- const tinyrl_history_entry_t *entry;
- unsigned limit = 0;
- const char *arg = lub_argv__get_arg(argv, 0);
- if ((NULL != arg) && ('\0' != *arg)) {
- limit = (unsigned)atoi(arg);
- if (0 == limit) {
-
- (void)tinyrl_history_unstifle(history);
- } else {
-
- tinyrl_history_stifle(history, limit);
- }
- }
- for (entry = tinyrl_history_getfirst(history, &iter);
- entry; entry = tinyrl_history_getnext(&iter)) {
-
- tinyrl_printf(this->tinyrl,
- "%5d %s\n",
- tinyrl_history_entry__get_index(entry),
- tinyrl_history_entry__get_line(entry));
- }
- return BOOL_TRUE;
- }
- static clish_shell_builtin_fn_t *find_builtin_callback(const
- clish_shell_builtin_t *
- cmd_list,
- const char *name)
- {
- const clish_shell_builtin_t *result;
-
- for (result = cmd_list; result && result->name; result++) {
- if (0 == strcmp(name, result->name)) {
- break;
- }
- }
- return (result && result->name) ? result->callback : NULL;
- }
- void clish_shell_cleanup_script(void *script)
- {
-
- lub_string_free(script);
- }
- bool_t
- clish_shell_execute(clish_shell_t * this,
- const clish_command_t * cmd, clish_pargv_t ** pargv)
- {
- bool_t result = BOOL_TRUE;
- const char *builtin;
- char *script;
- assert(NULL != cmd);
- builtin = clish_command__get_builtin(cmd);
- script = clish_command__get_action(cmd, this->viewid, *pargv);
-
- pthread_cleanup_push((void (*)(void *))clish_shell_cleanup_script,
- script);
- if (NULL != builtin) {
- clish_shell_builtin_fn_t *callback;
- lub_argv_t *argv = script ? lub_argv_new(script, 0) : NULL;
- result = BOOL_FALSE;
-
- callback = find_builtin_callback(clish_cmd_list, builtin);
- if (NULL == callback) {
-
- callback =
- find_builtin_callback(this->client_hooks->cmd_list,
- builtin);
- }
- if (NULL != callback) {
-
- result = callback(this, argv);
- }
- if (NULL != argv) {
- lub_argv_delete(argv);
- }
- } else if (NULL != script) {
-
- result = this->client_hooks->script_fn(this, script);
- }
- pthread_cleanup_pop(1);
- if (BOOL_TRUE == result) {
-
- clish_view_t *view = clish_command__get_view(cmd);
- char *viewid =
- clish_command__get_viewid(cmd, this->viewid, *pargv);
-
- if (this->client_hooks->config_fn)
- this->client_hooks->config_fn(this, cmd, *pargv);
- if (NULL != view) {
-
- char *line = clish_variable__get_line(cmd, *pargv);
- clish_shell__set_pwd(this,
- clish_command__get_depth(cmd),
- line);
- lub_string_free(line);
-
- this->view = view;
- }
- if (viewid) {
-
- lub_string_free(this->viewid);
- this->viewid = viewid;
- }
- }
- if (NULL != *pargv) {
- clish_pargv_delete(*pargv);
- *pargv = NULL;
- }
- return result;
- }
|