#include <ctype.h>
#include <string.h>
#include <stddef.h>
+#include <unistd.h>
#include <sys/param.h>
#include <assert.h>
+#include <config.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"
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 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)
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;
+ char * ret = NULL;
+ if (prompt)
+ printf ("%s", prompt);
+ fgets(line, sizeof(line), stdin);
+ n = strlen(line);
+ if (n && line[n-1] == '\n')
+ line[--n] = '\0';
+ if (n == 0 && feof(stdin)) {
+ ret = NULL;
+ } else
+ ret = strdup(line);
+ return ret;
+}
+#endif
+
+/* this is the command execution machine */
+int Parser_commands(void)
+{
+ char *line, *s;
+ int rc = 0;
+ int interactive;
+
+ interactive = init_input();
while(!done) {
- line = readline(parser_prompt);
+ line = readline(interactive ? parser_prompt : NULL);
if (!line) break;
/* 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"))
+ 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"))
+ if (!strcasecmp (str, "yes") ||
+ !strcasecmp (str, "y") ||
+ !strcasecmp (str, "on") ||
+ !strcasecmp (str, "enable"))
{
*b = 1;
return (0);