X-Git-Url: https://git.whamcloud.com/?p=fs%2Flustre-release.git;a=blobdiff_plain;f=lustre%2Futils%2Fparser.c;h=239c9b99e845e707b4fe0fb378b0c85667d82416;hp=a2bbe205396257e198956e9f5be8af682d05cb8f;hb=3d21af5fd098ba971f835e94fbe1f61b6a994a09;hpb=090c677210ee2946d99c71412e4ff762bb300f4f diff --git a/lustre/utils/parser.c b/lustre/utils/parser.c index a2bbe20..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 @@ -248,18 +236,19 @@ static char * command_generator(const char * text, int state) /* probably called by readline */ static char **command_completion(char * text, int start, int end) { - command_t * table; + command_t * table; 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; } - return(completion_matches(text, command_generator)); + return completion_matches(text, command_generator); } #endif @@ -340,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