Hi,
here is a diff that adds optional linebuffering to tr(1) with command
line switch -u like in sed(1). I need this to remove '\r' characters
from a continues input steam which lines have to be there immediately.
Please write me if something is wrong with this diff or the change
itself. I will fix it.
bye,
Jan
Index: tr.1
===================================================================
RCS file: /cvs/src/usr.bin/tr/tr.1,v
retrieving revision 1.20
diff -u -p -r1.20 tr.1
--- tr.1 14 Aug 2013 08:39:27 -0000 1.20
+++ tr.1 19 Nov 2013 20:46:33 -0000
@@ -41,18 +41,18 @@
.Nd translate characters
.Sh SYNOPSIS
.Nm tr
-.Op Fl cs
+.Op Fl csu
.Ar string1 string2
.Nm tr
-.Op Fl c
+.Op Fl cu
.Fl d
.Ar string1
.Nm tr
-.Op Fl c
+.Op Fl cu
.Fl s
.Ar string1
.Nm tr
-.Op Fl c
+.Op Fl cu
.Fl ds
.Ar string1 string2
.Sh DESCRIPTION
@@ -86,6 +86,14 @@ or
.Ar string2 )
in the input into a single instance of the character.
This occurs after all deletion and translation is completed.
+.It Fl u
+Force output to be line buffered,
+printing each line as it becomes available.
+By default, output is line buffered when standard output is a terminal
+and block buffered otherwise.
+See
+.Xr setbuf 3
+for a more detailed explanation.
.El
.Pp
In the first synopsis form, the characters in
@@ -284,6 +292,10 @@ The
utility is compliant with the
.St -p1003.1-2008
specification.
+.Pp
+The flag
+.Op Fl u
+is an extension to that specification.
.Pp
System V has historically implemented character ranges using the syntax
.Dq [c-c]
Index: tr.c
===================================================================
RCS file: /cvs/src/usr.bin/tr/tr.c,v
retrieving revision 1.15
diff -u -p -r1.15 tr.c
--- tr.c 27 Oct 2009 23:59:46 -0000 1.15
+++ tr.c 19 Nov 2013 20:46:33 -0000
@@ -88,7 +88,7 @@ main(int argc, char *argv[])
int cflag, dflag, sflag, isstring2;
cflag = dflag = sflag = 0;
- while ((ch = getopt(argc, argv, "cds")) != -1)
+ while ((ch = getopt(argc, argv, "cdsu")) != -1)
switch((char)ch) {
case 'c':
cflag = 1;
@@ -99,6 +99,9 @@ main(int argc, char *argv[])
case 's':
sflag = 1;
break;
+ case 'u':
+ setlinebuf(stdout);
+ break;
case '?':
default:
usage();
@@ -239,9 +242,9 @@ static void
usage(void)
{
fprintf(stderr,
- "usage: tr [-cs] string1 string2\n"
- " tr [-c] -d string1\n"
- " tr [-c] -s string1\n"
- " tr [-c] -ds string1 string2\n");
+ "usage: tr [-csu] string1 string2\n"
+ " tr [-cu] -d string1\n"
+ " tr [-cu] -s string1\n"
+ " tr [-cu] -ds string1 string2\n");
exit(1);
}