Patch for cat

M G Berberich <[email protected]>
Newsgroups gmane.comp.gnu.textutils.bugs
Message-ID <[email protected]>
Hello,

I appended a patch for cat.c. 

cat -v prints every character out of 32..127 as ^ or M- notation. This
might be sufficient for the english speaking world, but is an anoyance
for europeans, because it destroys text strings containing accented
charakters, umlauts, etc. I added a new option (-p or
--show-locale-chars) that does not change characters that are
printable in the actual locale (using isprint). 

This is quite usefull for non-english-speaking peoples because it
allows to "strip" control-chars from a "text"-stream without
destroying accented characters, umlauts, ...

Patch against textutils-2.0 attached.

	MfG
	bmg

-- 
"Des is völlig wurscht, was heut beschlos- | M G Berberich
 sen wird: I bin sowieso dagegn!"          | [email protected]
(SPD-Stadtrat Kurt Schindler; Regensburg)  |
textutil-diff.patch (text/plain, 8.5 KB)
diff -Naur textutils-2.0/man/cat.1 textutils-2.0-isprint/man/cat.1
--- textutils-2.0/man/cat.1	Sun Apr 21 11:41:01 2002
+++ textutils-2.0-isprint/man/cat.1	Sun Apr 21 12:01:46 2002
@@ -1,5 +1,5 @@
 .\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.24.
-.TH CAT "1" "July 2001" "GNU textutils 2.0" FSF
+.TH CAT "1" "April 2002" "GNU textutils 2.0" FSF
 .SH NAME
 cat \- concatenate files and print on the standard output
 .SH SYNOPSIS
@@ -42,6 +42,10 @@
 .TP
 \fB\-v\fR, \fB\-\-show\-nonprinting\fR
 use ^ and M- notation, except for LFD and TAB
+.TP
+\fB\-p\fR, \fB\-\-show\-locale\-chars\fR
+don't use ^ and M- notation for characters
+that are printable in the current locale
 .TP
 \fB\-\-help\fR
 display this help and exit
diff -Naur textutils-2.0/po/cat-id-tbl.c textutils-2.0-isprint/po/cat-id-tbl.c
--- textutils-2.0/po/cat-id-tbl.c	Sun Apr 21 11:41:01 2002
+++ textutils-2.0-isprint/po/cat-id-tbl.c	Sun Apr 21 12:01:47 2002
@@ -24,6 +24,8 @@
   -T, --show-tabs          display TAB characters as ^I\n\
   -u                       (ignored)\n\
   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n\
+  -p, --show-locale-chars  don't use ^ and M- notation for characters\n\
+                           that are printable in the current locale\n\
       --help               display this help and exit\n\
       --version            output version information and exit\n\
 \n\
diff -Naur textutils-2.0/po/de.po textutils-2.0-isprint/po/de.po
--- textutils-2.0/po/de.po	Fri Aug  6 22:56:36 1999
+++ textutils-2.0-isprint/po/de.po	Sun Apr 21 12:18:58 2002
@@ -42,6 +42,8 @@
 "  -T, --show-tabs          display TAB characters as ^I\n"
 "  -u                       (ignored)\n"
 "  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n"
+"  -p, --show-locale-chars  don't use ^ and M- notation for characters\n"
+"                           that are printable in the current locale\n"
 "      --help               display this help and exit\n"
 "      --version            output version information and exit\n"
 "\n"
@@ -59,9 +61,10 @@
 "  -T, --show-tabs          gib TAB-Zeichen als ^I aus\n"
 "  -u                       (ignoriert)\n"
 "  -v, --show-nonprinting   benutze ^ und M- Notation, außer für LFD und TAB\n"
+"  -p, --show-locale-chars  benutze keine ^ und M- Notation für Zeichen\n"
+"                           die in der eingestellten Sprache druckbar sind.\n"
 "      --help               gib diese Hilfe aus und beende das Programm\n"
-"      --version            gib Versionsinformation aus und beende das "
-"Programm\n"
+"      --version            gib Versionsinformation aus und beende das Programm\n"
 "\n"
 "Ohne DATEI oder wenn DATEI ist -, lies Standardeingabe.\n"
 
diff -Naur textutils-2.0/po/textutils.pot textutils-2.0-isprint/po/textutils.pot
--- textutils-2.0/po/textutils.pot	Sun Apr 21 11:41:01 2002
+++ textutils-2.0-isprint/po/textutils.pot	Sun Apr 21 12:01:47 2002
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: PACKAGE VERSION\n"
-"POT-Creation-Date: 2002-03-07 19:48+1100\n"
+"POT-Creation-Date: 2002-04-21 12:01+0200\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <[email protected]>\n"
@@ -44,20 +44,22 @@
 "  -T, --show-tabs          display TAB characters as ^I\n"
 "  -u                       (ignored)\n"
 "  -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n"
+"  -p, --show-locale-chars  don't use ^ and M- notation for characters\n"
+"                           that are printable in the current locale\n"
 "      --help               display this help and exit\n"
 "      --version            output version information and exit\n"
 "\n"
 "With no FILE, or when FILE is -, read standard input.\n"
 msgstr ""
 
-#: src/cat.c:112
+#: src/cat.c:114
 msgid ""
 "\n"
 "  -B, --binary             use binary writes to the console device.\n"
 "\n"
 msgstr ""
 
-#: src/cat.c:117 src/cksum.c:280 src/comm.c:87 src/csplit.c:1533 src/cut.c:227
+#: src/cat.c:119 src/cksum.c:280 src/comm.c:87 src/csplit.c:1533 src/cut.c:227
 #: src/expand.c:124 src/fmt.c:289 src/fold.c:79 src/head.c:107 src/join.c:196
 #: src/md5sum.c:126 src/nl.c:213 src/od.c:327 src/paste.c:454 src/pr.c:2859
 #: src/sort.c:298 src/split.c:106 src/sum.c:75 src/tac.c:142 src/tail.c:264
@@ -67,7 +69,7 @@
 "Report bugs to <[email protected]>."
 msgstr ""
 
-#: src/cat.c:177 src/cat.c:259 src/cat.c:312 src/cat.c:840 src/comm.c:220
+#: src/cat.c:179 src/cat.c:262 src/cat.c:315 src/cat.c:848 src/comm.c:220
 #: src/csplit.c:1493 src/cut.c:801 src/expand.c:392 src/fmt.c:416
 #: src/fold.c:225 src/fold.c:306 src/head.c:141 src/head.c:171 src/head.c:389
 #: src/join.c:1131 src/md5sum.c:629 src/nl.c:608 src/od.c:1940 src/paste.c:519
@@ -77,16 +79,16 @@
 msgid "write error"
 msgstr ""
 
-#: src/cat.c:300
+#: src/cat.c:303
 #, c-format
 msgid "cannot do ioctl on `%s'"
 msgstr ""
 
-#: src/cat.c:650 src/od.c:1152
+#: src/cat.c:658 src/od.c:1152
 msgid "standard output"
 msgstr ""
 
-#: src/cat.c:781
+#: src/cat.c:789
 #, c-format
 msgid "%s: input file is output file"
 msgstr ""
diff -Naur textutils-2.0/src/cat.c textutils-2.0-isprint/src/cat.c
--- textutils-2.0/src/cat.c	Sun Apr 21 11:41:01 2002
+++ textutils-2.0-isprint/src/cat.c	Sun Apr 21 11:38:20 2002
@@ -103,6 +103,8 @@
   -T, --show-tabs          display TAB characters as ^I\n\
   -u                       (ignored)\n\
   -v, --show-nonprinting   use ^ and M- notation, except for LFD and TAB\n\
+  -p, --show-locale-chars  don't use ^ and M- notation for characters\n\
+                           that are printable in the current locale\n\
       --help               display this help and exit\n\
       --version            output version information and exit\n\
 \n\
@@ -200,6 +202,7 @@
 
      /* Variables that have values according to the specified options.  */
      int quote,
+     int printable,
      int output_tabs,
      int numbers,
      int numbers_at_empty_lines,
@@ -397,45 +400,43 @@
 	{
 	  for (;;)
 	    {
-	      if (ch >= 32)
+	      if ((!printable && ch >= 32 && ch < 127) ||
+		  (printable && isprint(ch)))
 		{
-		  if (ch < 127)
-		    {
-		      if (reversible && (ch == '^' || ch == 'M' ||
-					 ch == '\\' || ch == '$'))
-			*bpout++ = '\\';
-		      *bpout++ = ch;
-		    }
-		  else if (ch == 127)
-		    {
-		      *bpout++ = '^';
-		      *bpout++ = '?';
-		    }
-		  else
+		  if (reversible && (ch == '^' || ch == 'M' ||
+				     ch == '\\' || ch == '$'))
+		    *bpout++ = '\\';
+		  *bpout++ = ch;
+		}
+	      else if (ch == 127)
+		{
+		  *bpout++ = '^';
+		  *bpout++ = '?';
+		}
+	      else if (ch > 127)
+		{
+		  *bpout++ = 'M';
+		  *bpout++ = '-';
+		  if (ch >= 128 + 32)
 		    {
-		      *bpout++ = 'M';
-		      *bpout++ = '-';
-		      if (ch >= 128 + 32)
+		      if (ch < 128 + 127)
 			{
-			  if (ch < 128 + 127)
-			    {
-			      ch -= 128;
-			      if (reversible && (ch == '^' || ch == '\\'))
-				*bpout++ = '\\';
-			      *bpout++ = ch;
-			    }
-			  else
-			    {
-			      *bpout++ = '^';
-			      *bpout++ = '?';
-			    }
+			  ch -= 128;
+			  if (reversible && (ch == '^' || ch == '\\'))
+			    *bpout++ = '\\';
+			  *bpout++ = ch;
 			}
 		      else
 			{
 			  *bpout++ = '^';
-			  *bpout++ = ch - 128 + 64;
+			  *bpout++ = '?';
 			}
 		    }
+		  else
+		    {
+		      *bpout++ = '^';
+		      *bpout++ = ch - 128 + 64;
+		    }
 		}
 	      else if (ch == '\t' && output_tabs)
 		*bpout++ = '\t';
@@ -517,6 +518,7 @@
   int squeeze_empty_lines = 0;
   int mark_line_ends = 0;
   int quote = 0;
+  int printable = 0;
   int output_tabs = 1;
   int reversible = 0;
 #if O_BINARY
@@ -534,6 +536,7 @@
     {"number", no_argument, NULL, 'n'},
     {"squeeze-blank", no_argument, NULL, 's'},
     {"show-nonprinting", no_argument, NULL, 'v'},
+    {"show-locale-chars", no_argument, NULL, 'p'},
     {"show-ends", no_argument, NULL, 'E'},
     {"show-tabs", no_argument, NULL, 'T'},
     {"show-all", no_argument, NULL, 'A'},
@@ -555,9 +558,9 @@
 
   while ((c = getopt_long (argc, argv,
 #if O_BINARY
-			   "benrstuvABET"
+			   "benprstuvABET"
 #else
-			   "benrstuvAET"
+			   "benprstuvAET"
 #endif
 			   , long_options, NULL)) != -1)
     {
@@ -583,6 +586,11 @@
 	  numbers = 1;
 	  break;
 
+	case 'p':
+	  ++options;
+	  printable = 1;
+	  break;
+
 	case 'r':
 	  ++options;
 	  reversible = 1;
@@ -816,7 +824,7 @@
 
 	  outbuf = (unsigned char *) xmalloc (outsize - 1 + insize * 4 + 13);
 
-	  cat (inbuf, insize, outbuf, outsize, quote,
+	  cat (inbuf, insize, outbuf, outsize, quote, printable,
 	       output_tabs, numbers, numbers_at_empty_lines, mark_line_ends,
 	       squeeze_empty_lines, reversible);
signature.asc (application/pgp-signature, 240 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Weitere Infos: siehe http://www.gnupg.org

iD8DBQE8wp7Lnp4msu7jrxMRAp/sAKCXrEvX/WeeQxhHn8D2VTs0DfSmOgCfSTWT
y11/49llE1Xnrr6bQi19b/Y=
=PTnG
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.