bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/3] grep: add --warnings={always,never,auto}.


From: Paolo Bonzini
Subject: [PATCH 1/3] grep: add --warnings={always,never,auto}.
Date: Sun, 15 Aug 2010 10:54:18 -0400

* src/grep.h (no_warnings): New declaration.
* src/main.c (no_warnings): New.
(WARNINGS_OPTION): Add to enum.
(main): Add --warnings.  Handle color_option == 2 together with it.
---
 src/grep.h |    2 ++
 src/main.c |   52 ++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/src/grep.h b/src/grep.h
index 67ea793..868037e 100644
--- a/src/grep.h
+++ b/src/grep.h
@@ -43,3 +43,5 @@ extern int match_icase;               /* -i */
 extern int match_words;                /* -w */
 extern int match_lines;                /* -x */
 extern unsigned char eolbyte;  /* -z */
+
+extern int no_warnings;
diff --git a/src/main.c b/src/main.c
index 20168bb..67fa335 100644
--- a/src/main.c
+++ b/src/main.c
@@ -280,7 +280,8 @@ enum
   LABEL_OPTION,
   EXCLUDE_DIRECTORY_OPTION,
   GROUP_SEPARATOR_OPTION,
-  MMAP_OPTION
+  MMAP_OPTION,
+  WARNINGS_OPTION,
 };
 
 /* Long options equivalences. */
@@ -336,6 +337,7 @@ static struct option const long_options[] =
   {"binary", no_argument, NULL, 'U'},
   {"unix-byte-offsets", no_argument, NULL, 'u'},
   {"version", no_argument, NULL, 'V'},
+  {"warnings", optional_argument, NULL, WARNINGS_OPTION},
   {"with-filename", no_argument, NULL, 'H'},
   {"word-regexp", no_argument, NULL, 'w'},
   {0, 0, 0, 0}
@@ -352,6 +354,8 @@ unsigned char eolbyte;
 static char const *filename;
 static int errseen;
 
+int no_warnings;
+
 enum directories_type
   {
     READ_DIRECTORIES = 2,
@@ -1785,6 +1789,7 @@ main (int argc, char **argv)
   exit_failure = EXIT_TROUBLE;
   atexit (close_stdout);
 
+  no_warnings = getenv ("POSIXLY_CORRECT") != NULL;
   prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
   setmatcher (NULL);
 
@@ -2018,15 +2023,6 @@ main (int argc, char **argv)
             show_help = 1;
         } else
           color_option = 2;
-        if (color_option == 2)
-          {
-            char const *t;
-            if (isatty (STDOUT_FILENO) && (t = getenv ("TERM"))
-                && !STREQ (t, "dumb"))
-              color_option = 1;
-            else
-              color_option = 0;
-          }
         break;
 
       case EXCLUDE_OPTION:
@@ -2074,6 +2070,23 @@ main (int argc, char **argv)
         /* long options */
         break;
 
+      case WARNINGS_OPTION:
+        if(optarg) {
+          if(!strcasecmp(optarg, "always") || !strcasecmp(optarg, "yes") ||
+             !strcasecmp(optarg, "force"))
+            no_warnings = 0;
+          else if(!strcasecmp(optarg, "never") || !strcasecmp(optarg, "no") ||
+                  !strcasecmp(optarg, "none"))
+            no_warnings = 1;
+          else if(!strcasecmp(optarg, "auto") || !strcasecmp(optarg, "tty") ||
+                  !strcasecmp(optarg, "if-tty"))
+            no_warnings = 2;
+          else
+            show_help = 1;
+        } else
+          no_warnings = 0;
+        break;
+
       default:
         usage (EXIT_TROUBLE);
         break;
@@ -2096,6 +2109,25 @@ main (int argc, char **argv)
   if (out_before < 0)
     out_before = default_context;
 
+  char const *t;
+  if (isatty (STDOUT_FILENO)
+      && (t = getenv ("TERM")) && !STREQ (t, "dumb"))
+    {
+      if (color_option == 2)
+        color_option = 1;
+      if (no_warnings == 2)
+        no_warnings = 0;
+    }
+  else
+    {
+      if (color_option == 2)
+        color_option = 0;
+
+      /* Redirected stdout: we're likely in a script, disable warnings.  */
+      if (no_warnings == 2)
+        no_warnings = 1;
+    }
+
   if (color_option)
     {
       /* Legacy.  */
-- 
1.7.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]