The following patch adds a new option -r (--reversible) which allows the
display of control characters to be done without ambiguity.  To illustrate,
suppose that we were given the input

\011^I

cat -A would display

^I^I

while with this patch, cat -rA would display

^I\^I
-- 
Debian GNU/Linux 2.1 is out! ( http://www.debian.org/ )
Email:  Herbert Xu ~{PmV>HI~} <[EMAIL PROTECTED]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Index: doc/textutils.texi
===================================================================
RCS file: /home/gondor/herbert/src/CVS/debian/textutils/doc/textutils.texi,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 textutils.texi
--- doc/textutils.texi  1999/07/31 09:03:30     1.1.1.1
+++ doc/textutils.texi  2000/06/27 10:20:43
@@ -358,6 +358,15 @@
 Number all output lines, starting with 1.  On MS-DOS and MS-Windows,
 this option causes @code{cat} to read and write files in text mode.
 
+@item -r
+@itemx --reversible
+@opindex -r
+@opindex --reversible
+Display control characters without being ambiguous by inserting a
+@samp{\} before all occurrences of @samp{^}, @samp{M}, @samp{\} and
+@samp{$} in the input, as well as any occurrences of literal @samp{^}
+and @samp{\} after the @samp{M-} notation.  This also implies @samp{-v}.
+
 @item -s
 @itemx --squeeze-blank
 @opindex -s
Index: man/cat.1
===================================================================
RCS file: /home/gondor/herbert/src/CVS/debian/textutils/man/cat.1,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 cat.1
--- man/cat.1   1999/08/06 19:24:07     1.1.1.1
+++ man/cat.1   2000/06/27 10:18:52
@@ -26,6 +26,11 @@
 \fB\-n\fR, \fB\-\-number\fR
 number all output lines
 .TP
+\fB\-r\fR, \fB\-\-reversible\fR
+put a backslash before all occurrences of ^, M, \\, and $
+in the input, as well as all occurrences of literal ^ and
+\\ after the M- notation, implies \fB\-v\fR
+.TP
 \fB\-s\fR, \fB\-\-squeeze\-blank\fR
 never more than one single blank line
 .TP
Index: src/cat.c
===================================================================
RCS file: /home/gondor/herbert/src/CVS/debian/textutils/src/cat.c,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 cat.c
--- src/cat.c   1999/05/18 14:12:03     1.1.1.1
+++ src/cat.c   2000/06/27 10:13:36
@@ -97,6 +97,7 @@
   -e                       equivalent to -vE\n\
   -E, --show-ends          display $ at end of each line\n\
   -n, --number             number all output lines\n\
+  -r, --reversible         use \\ to make the output reversible, implies -v\n\
   -s, --squeeze-blank      never more than one single blank line\n\
   -t                       equivalent to -vT\n\
   -T, --show-tabs          display TAB characters as ^I\n\
@@ -203,7 +204,8 @@
      int numbers,
      int numbers_at_empty_lines,
      int mark_line_ends,
-     int squeeze_empty_lines)
+     int squeeze_empty_lines,
+     int reversible)
 {
   /* Last character read from the input buffer.  */
   unsigned char ch;
@@ -398,7 +400,12 @@
              if (ch >= 32)
                {
                  if (ch < 127)
-                   *bpout++ = ch;
+                   {
+                     if (reversible && (ch == '^' || ch == 'M' ||
+                                        ch == '\\' || ch == '$'))
+                       *bpout++ = '\\';
+                     *bpout++ = ch;
+                   }
                  else if (ch == 127)
                    {
                      *bpout++ = '^';
@@ -411,7 +418,12 @@
                      if (ch >= 128 + 32)
                        {
                          if (ch < 128 + 127)
-                           *bpout++ = ch - 128;
+                           {
+                             ch -= 128;
+                             if (reversible && (ch == '^' || ch == '\\'))
+                               *bpout++ = '\\';
+                             *bpout++ = ch;
+                           }
                          else
                            {
                              *bpout++ = '^';
@@ -506,6 +518,7 @@
   int mark_line_ends = 0;
   int quote = 0;
   int output_tabs = 1;
+  int reversible = 0;
 #if O_BINARY
   int binary_files  = 0;
   int binary_output = 0;
@@ -524,6 +537,7 @@
     {"show-ends", no_argument, NULL, 'E'},
     {"show-tabs", no_argument, NULL, 'T'},
     {"show-all", no_argument, NULL, 'A'},
+    {"reversible", no_argument, NULL, 'r'},
 #if O_BINARY
     {"binary", no_argument, NULL, 'B'},
 #endif
@@ -541,9 +555,9 @@
 
   while ((c = getopt_long (argc, argv,
 #if O_BINARY
-                          "benstuvABET"
+                          "benrstuvABET"
 #else
-                          "benstuvAET"
+                          "benrstuvAET"
 #endif
                           , long_options, NULL)) != -1)
     {
@@ -569,6 +583,12 @@
          numbers = 1;
          break;
 
+       case 'r':
+         ++options;
+         reversible = 1;
+         quote = 1;
+         break;
+
        case 's':
          ++options;
          squeeze_empty_lines = 1;
@@ -798,7 +818,7 @@
 
          cat (inbuf, insize, outbuf, outsize, quote,
               output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
-              squeeze_empty_lines);
+              squeeze_empty_lines, reversible);
 
          free (outbuf);
        }

Reply via email to