In Savannah #67309, I mused:

> It's long seemed weird to me that one could force color off with the
> `-c` option, but a document could blithely turn it back on again with
> impunity.
>
> If the point of this feature is to avoid problems with monochrome
> output devices, or to conserve color ink in printers, foreclosing the
> emission [of] color-related grout commands would seem to serve the
> purpose better.

Here's a patch.  I refactored the logic a little bit to avoid writing
racily to a global variable in the event we ever make GNU troff
multi-threaded.

diff --git a/src/roff/troff/input.cpp b/src/roff/troff/input.cpp
index 56877ffad..ed94aad0d 100644
--- a/src/roff/troff/input.cpp
+++ b/src/roff/troff/input.cpp
@@ -102,6 +102,7 @@ void transparent_file();
 token tok;
 bool want_break = false;
 bool using_character_classes = false;
+static bool permit_color_output = true;
 bool want_color_output = true;
 static bool want_backtraces = false;
 char *pipe_command = 0 /* nullptr */;
@@ -1523,10 +1524,16 @@ static color *read_gray(char end = 0)
 static void activate_color()
 {
   int n;
+  bool is_color_desired = false;
   if (has_arg() && get_integer(&n))
-    want_color_output = (n > 0);
+    is_color_desired = (n > 0);
   else
-    want_color_output = true;
+    is_color_desired = true;
+  if (is_color_desired && !permit_color_output) {
+    error("color output disabled via command line");
+    is_color_desired = false;
+  }
+  want_color_output = is_color_desired;
   skip_line();
 }
 
@@ -9265,7 +9278,7 @@ int main(int argc, char **argv)
       want_att_compat = true;
       // fall through
     case 'c':
-      want_color_output = false;
+      permit_color_output = false;
       break;
     case 'M':
       macro_path.command_line_dir(optarg);

Demonstration:

$ echo '.color 0' | ./build/test-groff
$ echo '.color 1' | ./build/test-groff
$ echo '.color 0' | ./build/test-groff -c
$ echo '.color 1' | ./build/test-groff -c
troff:<standard input>:1: error: color output disabled via command line

Any objection?

Regards,
Branden

Attachment: signature.asc
Description: PGP signature

Reply via email to