Whamcloud - gitweb
LU-6245 libcfs: create userland and kernel string operations
[fs/lustre-release.git] / libcfs / libcfs / util / parser.c
index b40250b..6ca87e0 100644 (file)
@@ -1,6 +1,8 @@
 /*
  * Copyright (C) 2001 Cluster File Systems, Inc.
  *
+ * Copyright (c) 2014, Intel Corporation.
+ *
  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
  *
  *   Lustre is free software; you can redistribute it and/or
@@ -18,6 +20,7 @@
  *
  */
 
+#include <lustre_ver.h>
 #include <libcfs/libcfsutil.h>
 
 static command_t * top_level;           /* Top level of commands, initialized by
@@ -219,7 +222,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(const char *text, int start, int end)
 {
         command_t   * table;
         char        * pos;
@@ -240,74 +243,71 @@ static char **command_completion(char * text, int start, int end)
 /* take a string and execute the function or print help */
 int execute_line(char * line)
 {
-        command_t         *cmd, *ambig;
-        char *prev;
-        char *next, *tmp;
-        char *argv[MAXARGS];
-        int         i;
-        int rc = 0;
-
-        switch (process(line, &next, top_level, &cmd, &prev)) {
-        case CMD_AMBIG:
-                fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
-                while( (ambig = find_cmd(prev, cmd, &tmp)) ) {
-                        fprintf(stderr, "%s ", ambig->pc_name);
-                        cmd = ambig + 1;
-                }
-                fprintf(stderr, "\n");
-                break;
-        case CMD_NONE:
-                fprintf(stderr, "No such command, type help\n");
-                break;
-        case CMD_INCOMPLETE:
-                fprintf(stderr,
-                        "'%s' incomplete command.  Use '%s x' where x is one of:\n",
-                        line, line);
-                fprintf(stderr, "\t");
-                for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++) {
-                        fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
-                }
-                fprintf(stderr, "\n");
-                break;
-        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;
+       command_t       *cmd, *ambig;
+       char            *prev;
+       char            *next, *tmp;
+       char            *argv[MAXARGS];
+       int             i;
+       int             rc = 0;
+
+       switch (process(line, &next, top_level, &cmd, &prev)) {
+       case CMD_AMBIG:
+               fprintf(stderr, "Ambiguous command \'%s\'\nOptions: ", line);
+               while ((ambig = find_cmd(prev, cmd, &tmp))) {
+                       fprintf(stderr, "%s ", ambig->pc_name);
+                       cmd = ambig + 1;
+               }
+               fprintf(stderr, "\n");
+               break;
+       case CMD_NONE:
+               fprintf(stderr, "No such command, type help\n");
+               break;
+       case CMD_INCOMPLETE:
+               fprintf(stderr, "'%s' incomplete command.  Use '%s x' where "
+                       "x is one of:\n", line, line);
+               fprintf(stderr, "\t");
+               for (i = 0; cmd->pc_sub_cmd[i].pc_name; i++)
+                       fprintf(stderr, "%s ", cmd->pc_sub_cmd[i].pc_name);
+               fprintf(stderr, "\n");
+               break;
+       case CMD_COMPLETE:
+               optind = 0;
+               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;
 }
 
-int
-noop_fn ()
-{
-        return (0);
-}
+#ifdef HAVE_LIBREADLINE
+static void noop_int_fn(int unused) { }
+static void noop_void_fn(void) { }
+#endif
 
 /* just in case you're ever in an airplane and discover you
  forgot to install readline-dev. :) */
-int init_input()
* forgot to install readline-dev. :) */
+static int init_input(void)
 {
-        int   interactive = isatty (fileno (stdin));
+       int interactive = isatty(fileno(stdin));
 
 #ifdef HAVE_LIBREADLINE
-        using_history();
-        stifle_history(HISTORY);
+       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;
-        }
+       if (!interactive) {
+               rl_prep_term_function = noop_int_fn;
+               rl_deprep_term_function = noop_void_fn;
+       }
 
-        rl_attempted_completion_function = (CPPFunction *)command_completion;
-        rl_completion_entry_function = (void *)command_generator;
+       rl_attempted_completion_function = command_completion;
+       rl_completion_entry_function = command_generator;
 #endif
-        return interactive;
+       return interactive;
 }
 
 #ifndef HAVE_LIBREADLINE
@@ -454,12 +454,20 @@ int Parser_help(int argc, char **argv)
                 return 0;
         }
 
-        line[0]='\0';
-        for ( i = 1 ;  i < argc ; i++ ) {
-               if (strlen(argv[i]) > sizeof(line)-strlen(line)-1)
+       /* Joining command line arguments without space is not critical here
+        * because of this string is used for search a help topic and assume
+        * that only one argument will be (the name of topic). For example:
+        * lst > help ping run
+        * pingrun: Unknown command. */
+       line[0] = '\0';
+       for (i = 1;  i < argc; i++) {
+               if (strlen(argv[i]) >= sizeof(line) - strlen(line))
                        return -E2BIG;
-               strncat(line, argv[i], sizeof(line)-strlen(line)-1);
-        }
+               /* The function strlcat() cannot be used here because of
+                * this function is used in LNet utils that is not linked
+                * with libcfs.a. */
+               strncat(line, argv[i], sizeof(line) - strlen(line));
+       }
 
         switch ( process(line, &next, top_level, &result, &prev) ) {
         case CMD_COMPLETE:
@@ -535,18 +543,20 @@ char *Parser_getstr(const char *prompt, const char *deft, char *res,
         line  = readline(theprompt);
         free(theprompt);
 
-        if ( line == NULL || *line == '\0' ) {
-                strncpy(res, deft, len);
-        } else {
-                strncpy(res, line, len);
-        }
-
-        if ( line ) {
-                free(line);
-                return res;
-        } else {
-                return NULL;
-        }
+       /* The function strlcpy() cannot be used here because of
+        * this function is used in LNet utils that is not linked
+        * with libcfs.a. */
+       if (line == NULL || *line == '\0')
+               strncpy(res, deft, len);
+       else
+               strncpy(res, line, len);
+       res[len - 1] = '\0';
+
+       if (line != NULL) {
+               free(line);
+               return res;
+       }
+       return NULL;
 }
 
 /* get integer from prompt, loop forever to get it */
@@ -757,3 +767,10 @@ int Parser_quit(int argc, char **argv)
         done = 1;
         return 0;
 }
+
+int Parser_version(int argc, char **argv)
+{
+       fprintf(stdout, "%s %s\n", program_invocation_short_name,
+               LUSTRE_VERSION_STRING);
+       return 0;
+}