|
@@ -0,0 +1,303 @@
|
|
|
+
|
|
|
+ * sym_navy.c
|
|
|
+ */
|
|
|
+#include "private.h"
|
|
|
+#include "lub/ctype.h"
|
|
|
+#include "lub/string.h"
|
|
|
+#include "lub/argv.h"
|
|
|
+#include "lub/conv.h"
|
|
|
+
|
|
|
+#include <assert.h>
|
|
|
+#include <stdio.h>
|
|
|
+#include <unistd.h>
|
|
|
+#include <string.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <errno.h>
|
|
|
+#include <sys/types.h>
|
|
|
+#include <sys/stat.h>
|
|
|
+#include <sys/file.h>
|
|
|
+#include <signal.h>
|
|
|
+#include <fcntl.h>
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+CLISH_PLUGIN_SYM(clish_close)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ clish_shell__set_state(this, SHELL_STATE_CLOSING);
|
|
|
+
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Open a file and interpret it as a script in the context of a new
|
|
|
+ thread. Whether the script continues after command, but not script,
|
|
|
+ errors depends on the value of the stop_on_error flag.
|
|
|
+*/
|
|
|
+static int clish_source_internal(clish_context_t *context,
|
|
|
+ const char *fn, int stop_on_error)
|
|
|
+{
|
|
|
+ int result = -1;
|
|
|
+ const char *filename = fn;
|
|
|
+ struct stat fileStat;
|
|
|
+
|
|
|
+
|
|
|
+ clish_shell_t *this = clish_context__get_shell(context);
|
|
|
+
|
|
|
+
|
|
|
+ * Check file specified is not a directory
|
|
|
+ */
|
|
|
+ if ((0 == stat((char *)filename, &fileStat)) &&
|
|
|
+ (!S_ISDIR(fileStat.st_mode))) {
|
|
|
+
|
|
|
+ * push this file onto the file stack associated with this
|
|
|
+ * session. This will be closed by clish_shell_pop_file()
|
|
|
+ * when it is finished with.
|
|
|
+ */
|
|
|
+ result = clish_shell_push_file(this, filename,
|
|
|
+ stop_on_error);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result ? -1 : 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Open a file and interpret it as a script in the context of a new
|
|
|
+ thread. Invoking a script in this way will cause the script to
|
|
|
+ stop on the first error
|
|
|
+*/
|
|
|
+CLISH_PLUGIN_SYM(clish_source)
|
|
|
+{
|
|
|
+ clish_context_t *context = (clish_context_t *)clish_context;
|
|
|
+
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return (clish_source_internal(context, script, 1));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Open a file and interpret it as a script in the context of a new
|
|
|
+ thread. Invoking a script in this way will cause the script to
|
|
|
+ continue after command, but not script, errors.
|
|
|
+*/
|
|
|
+CLISH_PLUGIN_SYM(clish_source_nostop)
|
|
|
+{
|
|
|
+ clish_context_t *context = (clish_context_t *)clish_context;
|
|
|
+
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return (clish_source_internal(context, script, 0));
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Show the shell overview
|
|
|
+*/
|
|
|
+CLISH_PLUGIN_SYM(clish_overview)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
|
|
|
+ tinyrl_printf(tinyrl, "%s\n", clish_shell__get_overview(this));
|
|
|
+
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+CLISH_PLUGIN_SYM(clish_history)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
|
|
|
+ tinyrl_history_t *history = tinyrl__get_history(tinyrl);
|
|
|
+ tinyrl_history_iterator_t iter;
|
|
|
+ const tinyrl_history_entry_t *entry;
|
|
|
+ unsigned int limit = 0;
|
|
|
+ const char *arg = script;
|
|
|
+
|
|
|
+ if (arg && ('\0' != *arg)) {
|
|
|
+ lub_conv_atoui(arg, &limit, 0);
|
|
|
+ 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(tinyrl,
|
|
|
+ "%5d %s\n",
|
|
|
+ tinyrl_history_entry__get_index(entry),
|
|
|
+ tinyrl_history_entry__get_line(entry));
|
|
|
+ }
|
|
|
+
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Find out the previous view in the stack and go to it
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_nested_up)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ unsigned int depth;
|
|
|
+
|
|
|
+ if (!this)
|
|
|
+ return -1;
|
|
|
+
|
|
|
+ if (((depth = clish_shell__get_depth(this)) == 0) ||
|
|
|
+ !clish_shell__set_depth(this, --depth)) {
|
|
|
+ clish_shell__set_state(this, SHELL_STATE_CLOSING);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Builtin: NOP function
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_nop)
|
|
|
+{
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+ clish_context = clish_context;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Builtin: Set watchdog timeout. The "0" to turn watchdog off.
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_wdog)
|
|
|
+{
|
|
|
+ const char *arg = script;
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ unsigned int wdto = 0;
|
|
|
+
|
|
|
+
|
|
|
+ if (!arg || ('\0' == *arg)) {
|
|
|
+ clish_shell__set_wdog_timeout(this, 0);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ lub_conv_atoui(arg, &wdto, 0);
|
|
|
+ clish_shell__set_wdog_timeout(this, wdto);
|
|
|
+
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Get the ACTION context as a macros
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_macros)
|
|
|
+{
|
|
|
+ if (!script)
|
|
|
+ return 0;
|
|
|
+ *out = lub_string_dup(script);
|
|
|
+
|
|
|
+ clish_context = clish_context;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+CLISH_PLUGIN_SYM(clish_machine_interface)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ clish_shell_set_machine_interface(this);
|
|
|
+
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+CLISH_PLUGIN_SYM(clish_human_interface)
|
|
|
+{
|
|
|
+ clish_shell_t *this = clish_context__get_shell(clish_context);
|
|
|
+ clish_shell_set_human_interface(this);
|
|
|
+
|
|
|
+ script = script;
|
|
|
+ out = out;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Builtin: Print script
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_print_script)
|
|
|
+{
|
|
|
+ if (!script)
|
|
|
+ return 0;
|
|
|
+ printf("%s\n", script);
|
|
|
+
|
|
|
+ out = out;
|
|
|
+ clish_context = clish_context;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * Builtin: Print param
|
|
|
+ */
|
|
|
+CLISH_PLUGIN_SYM(clish_print_var)
|
|
|
+{
|
|
|
+ char *str = NULL;
|
|
|
+ char *copy = NULL;
|
|
|
+ char *varname = NULL;
|
|
|
+ char *t = NULL;
|
|
|
+
|
|
|
+
|
|
|
+ if (!script)
|
|
|
+ return 0;
|
|
|
+
|
|
|
+
|
|
|
+ copy = lub_string_dup(script);
|
|
|
+ varname = copy;
|
|
|
+ while (*varname && lub_ctype_isspace(*varname))
|
|
|
+ varname++;
|
|
|
+ t = varname;
|
|
|
+ while (*t && !lub_ctype_isspace(*t))
|
|
|
+ t++;
|
|
|
+ *t = '\0';
|
|
|
+
|
|
|
+ str = clish_shell_expand_var(varname, clish_context);
|
|
|
+ lub_string_free(copy);
|
|
|
+ if (!str)
|
|
|
+ return 0;
|
|
|
+ printf("%s\n", str);
|
|
|
+ lub_string_free(str);
|
|
|
+
|
|
|
+ out = out;
|
|
|
+ clish_context = clish_context;
|
|
|
+
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+
|