Whamcloud - gitweb
Changelog update
[fs/lustre-release.git] / lustre / utils / parser.c
index 26f66d8..a750d4c 100644 (file)
@@ -1,23 +1,37 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * Copyright (C) 2001 Cluster File Systems, Inc.
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.sf.net/projects/lustre/
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  */
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/param.h>
 #include <assert.h>
 
-#ifdef HAVE_LIBREADLINE
-#define READLINE_LIBRARY
-#include <readline/readline.h>
-
-/* completion_matches() is #if 0-ed out in modern glibc */
-#ifndef completion_matches
-#  define completion_matches rl_completion_matches
-#endif
-extern void using_history(void);
-extern void stifle_history(int);
-extern void add_history(char *);
-#endif
-
+#include "platform.h"
 #include "parser.h"
 
 static command_t * top_level;           /* Top level of commands, initialized by
@@ -116,6 +118,11 @@ void Parser_ignore_errors(int ignore)
         ignore_errors = ignore;
 }
 
+/* Returns:
+        0 on success
+        >0 CMD_* values
+        <0 errnos
+*/
 int Parser_execarg(int argc, char **argv, command_t cmds[])
 {
         command_t *cmd;
@@ -127,12 +134,12 @@ int Parser_execarg(int argc, char **argv, command_t cmds[])
                         fprintf(stderr, "%s\n", cmd->pc_help);
                 return rc;
         } else {
-               printf("Try interactive use without arguments or use one of:\n");
+                printf("Try interactive use without arguments or use one of:\n");
                 for (cmd = cmds; cmd->pc_name; cmd++)
                         printf("\"%s\"\n", cmd->pc_name);
                 printf("as argument.\n");
         }
-        return -1;
+        return -EINVAL;
 }
 
 /* returns the command_t * (NULL if not found) corresponding to a
@@ -252,9 +259,9 @@ static char **command_completion(char * text, int start, int end)
         char        * pos;
 
         match_tbl = top_level;
-        
+
         for (table = find_cmd(rl_line_buffer, match_tbl, &pos);
-             table; table = find_cmd(pos, match_tbl, &pos)) 
+             table; table = find_cmd(pos, match_tbl, &pos))
         {
 
                 if (*(pos - 1) == ' ') match_tbl = table->pc_sub_cmd;
@@ -341,16 +348,52 @@ int init_input()
 #define add_history(s)
 char * readline(char * prompt)
 {
-        char line[2048];
-        int n = 0;
+        int size = 2048;
+        char *line = malloc(size);
+        char *ptr = line;
+        int c;
+        int eof = 0;
+
+        if (line == NULL)
+                return NULL;
         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);
+
+        while (1) {
+                if ((c = fgetc(stdin)) != EOF) {
+                        if (c == '\n')
+                                goto out;
+                        *ptr++ = c;
+
+                        if (ptr - line >= size - 1) {
+                                char *tmp;
+
+                                size *= 2;
+                                tmp = malloc(size);
+                                if (tmp == NULL)
+                                        goto outfree;
+                                memcpy(tmp, line, ptr - line);
+                                ptr = tmp + (ptr - line);
+                                free(line);
+                                line = tmp;
+                        }
+                } else {
+                        eof = 1;
+                        if (ferror(stdin) || feof(stdin))
+                                goto outfree;
+                        goto out;
+                }
+        }
+out:
+        *ptr = 0;
+        if (eof && (strlen(line) == 0)) {
+                free(line);
+                line = NULL;
+        }
+        return line;
+outfree:
+        free(line);
+        return NULL;
 }
 #endif