--- code/trunk/pcretest.c 2007/12/17 14:46:11 286 +++ code/trunk/pcretest.c 2007/12/18 20:11:28 287 @@ -48,6 +48,12 @@ #include #include +#ifdef SUPPORT_LIBREADLINE +#include +#include +#include +#endif + /* A number of things vary for Windows builds. Originally, pcretest opened its input and output without "b"; then I was told that "b" was needed in some @@ -189,6 +195,7 @@ Arguments: f the file to read start where in buffer to start (this *must* be within buffer) + prompt for stdin or readline() Returns: pointer to the start of new data could be a copy of start, or could be moved @@ -196,7 +203,7 @@ */ static uschar * -extend_inputline(FILE *f, uschar *start) +extend_inputline(FILE *f, uschar *start, const char *prompt) { uschar *here = start; @@ -207,8 +214,36 @@ if (rlen > 1000) { int dlen; - if (fgets((char *)here, rlen, f) == NULL) - return (here == start)? NULL : start; + + /* If libreadline support is required, use readline() to read a line if the + input is a terminal. Note that readline() removes the trailing newline, so + we must put it back again, to be compatible with fgets(). */ + +#ifdef SUPPORT_LIBREADLINE + if (isatty(fileno(f))) + { + size_t len; + char *s = readline(prompt); + if (s == NULL) return (here == start)? NULL : start; + len = strlen(s); + if (len > 0) add_history(s); + if (len > rlen - 1) len = rlen - 1; + memcpy(here, s, len); + here[len] = '\n'; + here[len+1] = 0; + free(s); + } + else +#endif + + /* Read the next line by normal means, prompting if the file is stdin. */ + + { + if (f == stdin) printf(prompt); + if (fgets((char *)here, rlen, f) == NULL) + return (here == start)? NULL : start; + } + dlen = (int)strlen((char *)here); if (dlen > 0 && here[dlen - 1] == '\n') return start; here += dlen; @@ -728,7 +763,14 @@ static void usage(void) { -printf("Usage: pcretest [options] [ []]\n"); +printf("Usage: pcretest [options] [ []]\n\n"); +printf("Input and output default to stdin and stdout.\n"); +#ifdef SUPPORT_LIBREADLINE +printf("If input is a terminal, readline() is used to read from it.\n"); +#else +printf("This version of pcretest is not linked with readline().\n"); +#endif +printf("\nOptions:\n"); printf(" -b show compiled code (bytecode)\n"); printf(" -C show PCRE compile-time options and exit\n"); printf(" -d debug: show compiled code and information (-b and -i)\n"); @@ -997,8 +1039,7 @@ use_utf8 = 0; debug_lengths = 1; - if (infile == stdin) printf(" re> "); - if (extend_inputline(infile, buffer) == NULL) break; + if (extend_inputline(infile, buffer, " re> ") == NULL) break; if (infile != stdin) fprintf(outfile, "%s", (char *)buffer); fflush(outfile); @@ -1114,8 +1155,7 @@ pp++; } if (*pp != 0) break; - if (infile == stdin) printf(" > "); - if ((pp = extend_inputline(infile, pp)) == NULL) + if ((pp = extend_inputline(infile, pp, " > ")) == NULL) { fprintf(outfile, "** Unexpected EOF\n"); done = 1; @@ -1289,7 +1329,7 @@ { for (;;) { - if (extend_inputline(infile, buffer) == NULL) + if (extend_inputline(infile, buffer, NULL) == NULL) { done = 1; goto CONTINUE; @@ -1684,8 +1724,7 @@ len = 0; for (;;) { - if (infile == stdin) printf("data> "); - if (extend_inputline(infile, buffer + len) == NULL) + if (extend_inputline(infile, buffer + len, "data> ") == NULL) { if (len > 0) break; done = 1;