X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=lustre%2Futils%2Fparser.c;h=239c9b99e845e707b4fe0fb378b0c85667d82416;hb=d17206dd0cc5a0243c6a17e70377032c092667c1;hp=26f66d86576a569ce7f374bd7e1197fc8fcd24fd;hpb=2dc9c16e770415d56839e1996015fec5fab93f29;p=fs%2Flustre-release.git diff --git a/lustre/utils/parser.c b/lustre/utils/parser.c index 26f66d8..239c9b9 100644 --- a/lustre/utils/parser.c +++ b/lustre/utils/parser.c @@ -28,19 +28,7 @@ #include #include -#ifdef HAVE_LIBREADLINE -#define READLINE_LIBRARY -#include - -/* 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 @@ -341,16 +329,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