Whamcloud - gitweb
Fix unused variable warning.
[fs/lustre-release.git] / lustre / utils / parser.c
index 6d26bc3..ffd0075 100644 (file)
 #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"
-#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   */
 
@@ -96,25 +95,13 @@ static int line2args(char *line, char **argv, int maxargs)
 }
 
 /* find a command -- return it if unique otherwise print alternatives */
-    
 static command_t *Parser_findargcmd(char *name, command_t cmds[])
 {
         command_t *cmd;
-        int i;
-
-        for (i = 0; cmds[i].pc_name; i++) {
-                cmd = &cmds[i];
-
-                if (strlen(name) != strlen(cmd->pc_name))
-                        continue;
-
-                if (strlen(name) == strlen(cmd->pc_name)) {
-                        if (strcmp(name, cmd->pc_name) == 0) 
-                                return cmd;
-                        else
-                                continue;
-                }
 
+        for (cmd = cmds; cmd->pc_name; cmd++) {
+                if (strcmp(name, cmd->pc_name) == 0)
+                        return cmd;
         }
         return NULL;
 }
@@ -122,18 +109,15 @@ static command_t *Parser_findargcmd(char *name, command_t cmds[])
 int Parser_execarg(int argc, char **argv, command_t cmds[])
 {
         command_t *cmd;
-        int i;
 
         cmd = Parser_findargcmd(argv[0], cmds);
         if ( cmd ) {
                 return (cmd->pc_func)(argc, argv);
         } else {
-                printf("Try interactive use without arguments or use one of: ");
-                for (i=0 ; cmds[i].pc_name ; i++) {
-                        cmd = &cmds[i];
+                printf("Try interactive use without arguments or use one of:\n");
+                for (cmd = cmds; cmd->pc_name; cmd++)
                         printf("\"%s\" ", cmd->pc_name);
-                }
-                printf("as argument.\n");
+                printf("\nas argument.\n");
         }
         return -1;
 }
@@ -198,23 +182,26 @@ static int process(char *s, char ** next, command_t *lookup,
         }
 }
 
-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++;
 
@@ -228,7 +215,7 @@ static char * command_generator(const char * text, int state)
 }
 
 /* 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;
@@ -243,6 +230,7 @@ static char **command_completion(char * text, int start, int end)
 
         return(completion_matches(text, command_generator));
 }
+#endif
 
 /* take a string and execute the function or print help */
 int execute_line(char * line)
@@ -279,27 +267,76 @@ 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);
 
-        rl_attempted_completion_function = 
-                (CPPFunction *)command_completion;
+        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;
 
@@ -351,11 +388,10 @@ int Parser_int(char *s, int *val)
 }
 
 
-    
 void Parser_qhelp(int argc, char *argv[]) {
 
         printf("Available commands are:\n");
-        
+
         print_commands(NULL, top_level);
         printf("For more help type: help command-name\n");
 }
@@ -406,6 +442,14 @@ int Parser_help(int argc, char **argv)
         return 0;
 }  
 
+
+void Parser_printhelp(char *cmd)
+{
+        char *argv[] = { "help", cmd }; 
+        Parser_help(2, argv);
+}
+
+
 /*************************************************************************
  * COMMANDS                                                                 *
  *************************************************************************/ 
@@ -575,7 +619,6 @@ long Parser_intarg(const char *inp, const char *prompt, int deft,
 char *Parser_strarg(char *inp, const char *prompt, const char *deft,
                     char *answer, int len)
 {
-    
         if ( inp == NULL || *inp == '\0' ) {
                 return Parser_getstr(prompt, deft, answer, len);
         } else 
@@ -599,10 +642,70 @@ int Parser_arg2int(const char *inp, long *result, int base)
                 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;
         argv = argv;
-        done = 1; 
+        done = 1;
         return 0;
 }