[Pcre-svn] [129] code/trunk: Recognize '-' as standard input…

トップ ページ
このメッセージを削除
著者: Subversion repository
日付:  
To: pcre-svn
題目: [Pcre-svn] [129] code/trunk: Recognize '-' as standard input in pcre2test.
Revision: 129
          http://www.exim.org/viewvc/pcre2?view=rev&revision=129
Author:   ph10
Date:     2014-10-31 15:57:47 +0000 (Fri, 31 Oct 2014)


Log Message:
-----------
Recognize '-' as standard input in pcre2test.

Modified Paths:
--------------
    code/trunk/doc/pcre2test.1
    code/trunk/src/pcre2test.c


Modified: code/trunk/doc/pcre2test.1
===================================================================
--- code/trunk/doc/pcre2test.1    2014-10-31 12:34:34 UTC (rev 128)
+++ code/trunk/doc/pcre2test.1    2014-10-31 15:57:47 UTC (rev 129)
@@ -181,10 +181,12 @@
 .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


Modified: code/trunk/src/pcre2test.c
===================================================================
--- code/trunk/src/pcre2test.c    2014-10-31 12:34:34 UTC (rev 128)
+++ code/trunk/src/pcre2test.c    2014-10-31 15:57:47 UTC (rev 129)
@@ -66,15 +66,16 @@
 #include <locale.h>
 #include <errno.h>


+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#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 <unistd.h>
-#endif
#if defined(SUPPORT_LIBREADLINE)
#include <readline/readline.h>
#include <readline/history.h>
@@ -87,7 +88,12 @@
#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 ---------------------- */

 /* A number of things vary for Windows builds. Originally, pcretest opened its
@@ -2537,7 +2543,7 @@
     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 @@
     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 @@
     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 @@

/* 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 @@
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 @@

   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 @@
       }
     }


-  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 @@


/* Finish off a normal run. */

-if (infile == stdin) fprintf(outfile, "\n");
+if (INTERACTIVE(infile)) fprintf(outfile, "\n");

if (showtotaltimes)
{