Discussion:
sed -u option
Yuji Yamano
2014-06-12 19:03:12 UTC
Permalink
Hi!

I just found our sed supports -l option.
How about adding -u option for GNU and OpenBSD compatibility?

-- Yuji Yamano

Index: main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/main.c,v
retrieving revision 1.24
diff -u -r1.24 main.c
--- main.c 6 Jun 2014 12:46:54 -0000 1.24
+++ main.c 12 Jun 2014 18:52:34 -0000
@@ -137,7 +137,7 @@
fflag = 0;
inplace = NULL;

- while ((c = getopt(argc, argv, "EI::ae:f:i::lnr")) != -1)
+ while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
switch (c) {
case 'r': /* Gnu sed compat */
case 'E':
@@ -165,6 +165,7 @@
inplace = optarg ? optarg : __UNCONST("");
ispan = 0; /* don't span across input files */
break;
+ case 'u': /* Gnu and OpenBSD sed compat */
case 'l':
#ifdef _IOLBF
c = setvbuf(stdout, NULL, _IOLBF, 0);
@@ -209,8 +210,8 @@
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n",
- "usage: sed script [-Ealn] [-i extension] [file ...]",
- " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
+ "usage: sed script [-Ealnru] [-i extension] [file ...]",
+ " sed [-Ealnru] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
exit(1);
}

Index: sed.1
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/sed.1,v
retrieving revision 1.34
diff -u -r1.34 sed.1
--- sed.1 6 Jun 2014 14:36:38 -0000 1.34
+++ sed.1 12 Jun 2014 18:52:34 -0000
@@ -40,11 +40,11 @@
.Nd stream editor
.Sh SYNOPSIS
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Ar command
.Op Ar
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Op Fl e Ar command
.Op Fl f Ar command_file
.Op Fl I Op Ar extension
@@ -149,6 +149,10 @@
Same as
.Fl E
for compatibility with GNU sed.
+.It Fl u
+Same as
+.Fl l
+for compatibility with GNU sed.
.El
.Pp
The form of a
@@ -599,9 +603,9 @@
specification.
.Pp
The
-.Fl E , I , a
+.Fl E , I , a , i , l , r
and
-.Fl i
+.Fl u
options, the prefixing
.Dq \&+
in the second member of an address range,
Christos Zoulas
2014-06-13 01:08:39 UTC
Permalink
Post by Yuji Yamano
Hi!
I just found our sed supports -l option.
How about adding -u option for GNU and OpenBSD compatibility?
OpenBSD:
-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
setbuf(3) for a more detailed explanation.

GNU:

-u
--unbuffered
Buffer both input and output as minimally as practical. (This is
particularly useful if the input is coming from the likes of `tail
-f', and you wish to see the transformed output as soon as possible.)

I think we should go the GNU way, since this is more mnemonic and
useful.

christos
Post by Yuji Yamano
-- Yuji Yamano
Index: main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/main.c,v
retrieving revision 1.24
diff -u -r1.24 main.c
--- main.c 6 Jun 2014 12:46:54 -0000 1.24
+++ main.c 12 Jun 2014 18:52:34 -0000
@@ -137,7 +137,7 @@
fflag = 0;
inplace = NULL;
- while ((c = getopt(argc, argv, "EI::ae:f:i::lnr")) != -1)
+ while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
switch (c) {
case 'r': /* Gnu sed compat */
@@ -165,6 +165,7 @@
inplace = optarg ? optarg : __UNCONST("");
ispan = 0; /* don't span across input files */
break;
+ case 'u': /* Gnu and OpenBSD sed compat */
#ifdef _IOLBF
c = setvbuf(stdout, NULL, _IOLBF, 0);
@@ -209,8 +210,8 @@
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n",
- "usage: sed script [-Ealn] [-i extension] [file ...]",
- " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
+ "usage: sed script [-Ealnru] [-i extension] [file ...]",
+ " sed [-Ealnru] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
exit(1);
}
Index: sed.1
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/sed.1,v
retrieving revision 1.34
diff -u -r1.34 sed.1
--- sed.1 6 Jun 2014 14:36:38 -0000 1.34
+++ sed.1 12 Jun 2014 18:52:34 -0000
@@ -40,11 +40,11 @@
.Nd stream editor
.Sh SYNOPSIS
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Ar command
.Op Ar
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Op Fl e Ar command
.Op Fl f Ar command_file
.Op Fl I Op Ar extension
@@ -149,6 +149,10 @@
Same as
.Fl E
for compatibility with GNU sed.
+.It Fl u
+Same as
+.Fl l
+for compatibility with GNU sed.
.El
.Pp
The form of a
@@ -599,9 +603,9 @@
specification.
.Pp
The
-.Fl E , I , a
+.Fl E , I , a , i , l , r
and
-.Fl i
+.Fl u
options, the prefixing
.Dq \&+
in the second member of an address range,
David Laight
2014-06-15 12:10:42 UTC
Permalink
Post by Christos Zoulas
Post by Yuji Yamano
Hi!
I just found our sed supports -l option.
How about adding -u option for GNU and OpenBSD compatibility?
-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
setbuf(3) for a more detailed explanation.
I wonder if it would be better to get the shell to generate a pipe
that will force line buffering?

Then you could add a shell option and 'fix' a lot of problems in
one go.

Probably takes an extra ioctl in the pipe driver and some collusion
between the shells and libc.

David
--
David Laight: ***@l8s.co.uk
Christos Zoulas
2014-06-15 15:43:52 UTC
Permalink
On Jun 15, 1:10pm, ***@l8s.co.uk (David Laight) wrote:
-- Subject: Re: sed -u option

| On Fri, Jun 13, 2014 at 01:08:39AM +0000, Christos Zoulas wrote:
| > In article <***@kt.rim.or.jp>,
| > Yuji Yamano <***@kt.rim.or.jp> wrote:
| > >Hi!
| > >
| > >I just found our sed supports -l option.
| > >How about adding -u option for GNU and OpenBSD compatibility?
| >
| > OpenBSD:
| > -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
| > setbuf(3) for a more detailed explanation.
|
| I wonder if it would be better to get the shell to generate a pipe
| that will force line buffering?

No need, that part is in the source already.

christos
Yuji Yamano
2014-06-17 16:09:45 UTC
Permalink
Post by Christos Zoulas
-u
--unbuffered
Buffer both input and output as minimally as practical. (This is
particularly useful if the input is coming from the likes of `tail
-f', and you wish to see the transformed output as soon as possible.)
I think we should go the GNU way, since this is more mnemonic and
useful.
I'm not sure if I understand what you want to say.
Do you mean -u for no buffering and -l for line buffering?

-- Yuji Yamano
Christos Zoulas
2014-06-17 16:30:35 UTC
Permalink
On Jun 18, 1:09am, ***@kt.rim.or.jp (Yuji Yamano) wrote:
-- Subject: Re: sed -u option

| On Fri, 13 Jun 2014 01:08:39 +0000 (UTC), ***@astron.com (Christos Zoulas) wrote:
|
| > GNU:
| >
| > -u
| > --unbuffered
| > Buffer both input and output as minimally as practical. (This is
| > particularly useful if the input is coming from the likes of `tail
| > -f', and you wish to see the transformed output as soon as possible.)
| >
| > I think we should go the GNU way, since this is more mnemonic and
| > useful.
|
| I'm not sure if I understand what you want to say.
| Do you mean -u for no buffering and -l for line buffering?

Yes. Follow GNU. No point in making -u line-buffered, when we have -l for it!
Also what does 'u' stand for on OpenBSD? 'U'npredictable?

christos
Yuji Yamano
2014-06-25 13:31:10 UTC
Permalink
Post by Christos Zoulas
Yes. Follow GNU. No point in making -u line-buffered, when we have -l for it!
Also what does 'u' stand for on OpenBSD? 'U'npredictable?
How about this patch?

Index: main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/main.c,v
retrieving revision 1.24
diff -u -r1.24 main.c
--- main.c 6 Jun 2014 12:46:54 -0000 1.24
+++ main.c 25 Jun 2014 13:27:56 -0000
@@ -105,6 +105,7 @@

int aflag, eflag, nflag;
int rflags = 0;
+static int uflag;
static int rval; /* Exit status */

static int ispan; /* Whether inplace editing spans across files */
@@ -137,7 +138,7 @@
fflag = 0;
inplace = NULL;

- while ((c = getopt(argc, argv, "EI::ae:f:i::lnr")) != -1)
+ while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
switch (c) {
case 'r': /* Gnu sed compat */
case 'E':
@@ -165,6 +166,14 @@
inplace = optarg ? optarg : __UNCONST("");
ispan = 0; /* don't span across input files */
break;
+ case 'u':
+ uflag = 1;
+#ifdef _IONBF
+ c = setvbuf(stdout, NULL, _IONBF, 0);
+ if (c)
+ warn("setting no buffered output failed");
+#endif
+ break;
case 'l':
#ifdef _IOLBF
c = setvbuf(stdout, NULL, _IOLBF, 0);
@@ -209,8 +218,8 @@
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n",
- "usage: sed script [-Ealn] [-i extension] [file ...]",
- " sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
+ "usage: sed script [-Ealnru] [-i extension] [file ...]",
+ " sed [-Ealnru] [-i extension] [-e script] ... [-f script_file] ... [file ...]");
exit(1);
}

@@ -434,6 +443,13 @@
rval = 1;
continue;
}
+ if (uflag) {
+#ifdef _IONBF
+ c = setvbuf(infile, NULL, _IONBF, 0);
+ if (c)
+ warn("setting no buffered input failed");
+#endif
+ }
}
/*
* We are here only when infile is open and we still have something
Index: sed.1
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/sed.1,v
retrieving revision 1.34
diff -u -r1.34 sed.1
--- sed.1 6 Jun 2014 14:36:38 -0000 1.34
+++ sed.1 25 Jun 2014 13:27:56 -0000
@@ -40,11 +40,11 @@
.Nd stream editor
.Sh SYNOPSIS
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Ar command
.Op Ar
.Nm
-.Op Fl Ealnr
+.Op Fl Ealnru
.Op Fl e Ar command
.Op Fl f Ar command_file
.Op Fl I Op Ar extension
@@ -149,6 +149,8 @@
Same as
.Fl E
for compatibility with GNU sed.
+.It Fl u
+Unbuffered input and output.
.El
.Pp
The form of a
@@ -599,9 +601,9 @@
specification.
.Pp
The
-.Fl E , I , a
+.Fl E , I , a , i , l , r
and
-.Fl i
+.Fl u
options, the prefixing
.Dq \&+
in the second member of an address range,
Christos Zoulas
2014-06-25 15:02:00 UTC
Permalink
On Jun 25, 10:31pm, ***@kt.rim.or.jp (Yuji Yamano) wrote:
-- Subject: Re: sed -u option

| On Tue, 17 Jun 2014 12:30:35 -0400, ***@zoulas.com (Christos Zoulas) wrote:
|
| > Yes. Follow GNU. No point in making -u line-buffered, when we have -l for it!
| > Also what does 'u' stand for on OpenBSD? 'U'npredictable?
|
| How about this patch?

Thanks, I've applied part of this patch already. You are bringing up
an interesting question: how to treat the input. We don't do the
anything for _IOLBF, so I guess we could just ignore it too for _IONBF.

Or we could always set the input buffering to match the output?
What do others think?

christos

Loading...