#include <ctype.h>
#include <string.h>
#include <stddef.h>
+#include <unistd.h>
#include <sys/param.h>
#include <assert.h>
+#ifdef HAVE_LIBREADLINE
#define READLINE_LIBRARY
#include <readline/readline.h>
extern void using_history(void);
extern void stifle_history(int);
extern void add_history(char *);
+#endif
#include "parser.h"
-#define CMD_COMPLETE 0
-#define CMD_INCOMPLETE 1
-#define CMD_NONE 2
-#define CMD_AMBIG 3
static command_t * top_level; /* Top level of commands, initialized by
* InitParser */
-static command_t * match_tbl; /* Command completion against this table */
static char * parser_prompt = NULL;/* Parser prompt, set by InitParser */
static int done; /* Set to 1 if user types exit or quit */
+static int ignore_errors; /* Normally, the parser will quit when
+ an error occurs in non-interacive
+ mode. Setting this to non-zero will
+ force it to keep buggering on. */
/* static functions */
static char *skipwhitespace(char *s);
static char *skiptowhitespace(char *s);
static command_t *find_cmd(char *name, command_t cmds[], char **next);
-static int process(char *s, char **next, command_t *lookup, command_t **result, char **prev);
-static char *command_generator(const char *text, int state);
-static char **command_completion(char *text, int start, int end);
+static int process(char *s, char **next, command_t *lookup, command_t **result,
+ char **prev);
static void print_commands(char *str, command_t *table);
static char * skipwhitespace(char * s)
return NULL;
}
+void Parser_ignore_errors(int ignore)
+{
+ ignore_errors = ignore;
+}
+
int Parser_execarg(int argc, char **argv, command_t cmds[])
{
command_t *cmd;
}
}
-static char * command_generator(const char * text, int state)
+#ifdef HAVE_LIBREADLINE
+static command_t * match_tbl; /* Command completion against this table */
+static char * command_generator(const char * text, int state)
{
static int index,
len;
char *name;
/* Do we have a match table? */
- if (!match_tbl)
+ if (!match_tbl)
return NULL;
-
+
/* If this is the first time called on this word, state is 0 */
if (!state) {
index = 0;
len = (int)strlen(text);
}
- /* Return the next name in the command list that paritally matches test */
+ /* Return next name in the command list that paritally matches test */
while ( (name = (match_tbl + index)->pc_name) ) {
index++;
}
/* probably called by readline */
-static char **command_completion(char * text, int start, int end)
+static char **command_completion(char * text, int start, int end)
{
command_t * table;
char * pos;
return(completion_matches(text, command_generator));
}
+#endif
/* take a string and execute the function or print help */
int execute_line(char * line)
case CMD_COMPLETE:
i = line2args(line, argv, MAXARGS);
rc = (cmd->pc_func)(i, argv);
+
+ if (rc == CMD_HELP)
+ fprintf(stderr, "%s\n", cmd->pc_help);
+
break;
}
return rc;
}
-/* this is the command execution machine */
-int Parser_commands(void)
+int
+noop_fn ()
{
- char *line, *s;
- int rc = 0;
+ return (0);
+}
+
+/* just in case you're ever in an airplane and discover you
+ forgot to install readline-dev. :) */
+int init_input()
+{
+ int interactive = isatty (fileno (stdin));
+#ifdef HAVE_LIBREADLINE
using_history();
stifle_history(HISTORY);
+ if (!interactive)
+ {
+ rl_prep_term_function = (rl_vintfunc_t *)noop_fn;
+ rl_deprep_term_function = (rl_voidfunc_t *)noop_fn;
+ }
+
rl_attempted_completion_function = (CPPFunction *)command_completion;
rl_completion_entry_function = (void *)command_generator;
+#endif
+ return interactive;
+}
+
+#ifndef HAVE_LIBREADLINE
+#define add_history(s)
+char * readline(char * prompt)
+{
+ char line[2048];
+ int n = 0;
+ if (prompt)
+ printf ("%s", prompt);
+ if (fgets(line, sizeof(line), stdin) == NULL)
+ return (NULL);
+ n = strlen(line);
+ if (n && line[n-1] == '\n')
+ line[n-1] = '\0';
+ return strdup(line);
+}
+#endif
+
+/* this is the command execution machine */
+int Parser_commands(void)
+{
+ char *line, *s;
+ int rc = 0, save_error = 0;
+ int interactive;
+
+ interactive = init_input();
while(!done) {
- line = readline(parser_prompt);
+ line = readline(interactive ? parser_prompt : NULL);
if (!line) break;
add_history(s);
rc = execute_line(s);
}
+ /* stop on error if not-interactive */
+ if (rc != 0 && !interactive) {
+ if (save_error == 0)
+ save_error = rc;
+ if (!ignore_errors)
+ done = 1;
+ }
free(line);
}
+ if (save_error)
+ rc = save_error;
return rc;
}
return 1;
}
+/* Convert human readable size string to and int; "1k" -> 1000 */
+int Parser_size (int *sizep, char *str) {
+ int size;
+ char mod[32];
+
+ switch (sscanf (str, "%d%1[gGmMkK]", &size, mod)) {
+ default:
+ return (-1);
+
+ case 1:
+ *sizep = size;
+ return (0);
+
+ case 2:
+ switch (*mod) {
+ case 'g':
+ case 'G':
+ *sizep = size << 30;
+ return (0);
+
+ case 'm':
+ case 'M':
+ *sizep = size << 20;
+ return (0);
+
+ case 'k':
+ case 'K':
+ *sizep = size << 10;
+ return (0);
+
+ default:
+ *sizep = size;
+ return (0);
+ }
+ }
+}
+
+/* Convert a string boolean to an int; "enable" -> 1 */
+int Parser_bool (int *b, char *str) {
+ if (!strcasecmp (str, "no") ||
+ !strcasecmp (str, "n") ||
+ !strcasecmp (str, "off") ||
+ !strcasecmp (str, "disable"))
+ {
+ *b = 0;
+ return (0);
+ }
+
+ if (!strcasecmp (str, "yes") ||
+ !strcasecmp (str, "y") ||
+ !strcasecmp (str, "on") ||
+ !strcasecmp (str, "enable"))
+ {
+ *b = 1;
+ return (0);
+ }
+
+ return (-1);
+}
+
int Parser_quit(int argc, char **argv)
{
argc = argc;