From 1f1f13ba7c6ad5197049fb7bed7551d42b2ed35e Mon Sep 17 00:00:00 2001 From: ph10 Date: Fri, 31 Oct 2014 15:57:47 +0000 Subject: [PATCH] Recognize '-' as standard input in pcre2test. --- doc/pcre2test.1 | 8 +++++--- src/pcre2test.c | 30 ++++++++++++++++++------------ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/doc/pcre2test.1 b/doc/pcre2test.1 index 0f9bdef..04f138a 100644 --- a/doc/pcre2test.1 +++ b/doc/pcre2test.1 @@ -181,10 +181,12 @@ Output the PCRE2 version number and then exit. .rs .sp If \fBpcre2test\fP is given two filename arguments, it reads from the first and -writes to the second. If it is given only one filename argument, it reads from +writes to the second. If the first name is "-", input is taken from the +standard input. If \fBpcre2test\fP is given only one argument, it reads from that file and writes to stdout. Otherwise, it reads from stdin and writes to -stdout, and prompts for each line of input, using "re>" to prompt for regular -expression patterns, and "data>" to prompt for subject lines. +stdout. When the input is a terminal, it prompts for each line of input, using +"re>" to prompt for regular expression patterns, and "data>" to prompt for +subject lines. .P When \fBpcre2test\fP is built, a configuration option can specify that it should be linked with the \fBlibreadline\fP or \fBlibedit\fP library. When this diff --git a/src/pcre2test.c b/src/pcre2test.c index e3f55b8..955de53 100644 --- a/src/pcre2test.c +++ b/src/pcre2test.c @@ -66,15 +66,16 @@ it references only the enabled library functions. */ #include #include +#ifdef HAVE_UNISTD_H +#include +#endif + /* Both libreadline and libedit are optionally supported. The user-supplied original patch uses readline/readline.h for libedit, but in at least one system it is installed as editline/readline.h, so the configuration code now looks for that first, falling back to readline/readline.h. */ #if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT) -#ifdef HAVE_UNISTD_H -#include -#endif #if defined(SUPPORT_LIBREADLINE) #include #include @@ -87,6 +88,11 @@ that first, falling back to readline/readline.h. */ #endif #endif +/* Put the test for interactive input into a macro so that it can be changed if +required for different environments. */ + +#define INTERACTIVE(f) isatty(fileno(f)) + /* ---------------------- System-specific definitions ---------------------- */ @@ -2537,7 +2543,7 @@ for (;;) newline, so we must put it back again, to be compatible with fgets(). */ #if defined(SUPPORT_LIBREADLINE) || defined(SUPPORT_LIBEDIT) - if (isatty(fileno(f))) + if (INTERACTIVE(f)) { size_t len; char *s = readline(prompt); @@ -2553,10 +2559,10 @@ for (;;) else #endif - /* Read the next line by normal means, prompting if the file is stdin. */ + /* Read the next line by normal means, prompting if the file is a tty. */ { - if (f == stdin) printf("%s", prompt); + if (INTERACTIVE(f)) printf("%s", prompt); if (fgets((char *)here, rlen, f) == NULL) return (here == start)? NULL : start; } @@ -3624,7 +3630,7 @@ for(;;) fprintf(outfile, "** Unexpected EOF\n"); return PR_ABEND; } - if (infile != stdin) fprintf(outfile, "%s", (char *)p); + if (!INTERACTIVE(infile)) fprintf(outfile, "%s", (char *)p); } /* If the first character after the delimiter is backslash, make the pattern @@ -5637,7 +5643,7 @@ def_datctl.cfail[0] = def_datctl.cfail[1] = CFAIL_UNSET; /* Scan command line options. */ -while (argc > 1 && argv[op][0] == '-') +while (argc > 1 && argv[op][0] == '-' && argv[op][1] != 0) { const char *endptr; char *arg = argv[op]; @@ -5883,7 +5889,7 @@ if ((arg_pattern != NULL && infile = stdin; outfile = stdout; -if (argc > 1) +if (argc > 1 && strcmp(argv[op], "-") != 0) { infile = fopen(argv[op], INPUT_MODE); if (infile == NULL) @@ -5927,7 +5933,7 @@ while (notdone) if (extend_inputline(infile, buffer, expectdata? "data> " : " re> ") == NULL) break; - if (infile != stdin) fprintf(outfile, "%s", (char *)buffer); + if (!INTERACTIVE(infile)) fprintf(outfile, "%s", (char *)buffer); fflush(outfile); p = buffer; @@ -5987,7 +5993,7 @@ while (notdone) } } - if (rc == PR_SKIP && infile != stdin) skipping = TRUE; + if (rc == PR_SKIP && !INTERACTIVE(infile)) skipping = TRUE; else if (rc == PR_ABEND) { fprintf(outfile, "** pcre2test run abandoned\n"); @@ -5998,7 +6004,7 @@ while (notdone) /* Finish off a normal run. */ -if (infile == stdin) fprintf(outfile, "\n"); +if (INTERACTIVE(infile)) fprintf(outfile, "\n"); if (showtotaltimes) {