Re: add -f option to script(1)

2018-07-16 Thread bijan

On 07/13/18 22:35, bijan wrote:

Hi tech.

While trying to test liveshell[1], I noticed the script(1) from base is
missing the -f option, flushing the output after each write, which is
kinda critical for monitoring the output file by another process. the
Linux script utility uses fflush(3) after each write (duhh) but I'm not
sure if anything more than removing buffering operation from
the stream is necessary.

So here's the diff which is working for me


ping! no one is interested or am I missing something? added -f for quiet
option out of boredom

--
Bijan Ebrahimi

diff --git usr.bin/script/script.1 usr.bin/script/script.1
index f10ec2d4b..55e7e6868 100644
--- usr.bin/script/script.1
+++ usr.bin/script/script.1
@@ -38,7 +38,7 @@
 .Nd make typescript of terminal session
 .Sh SYNOPSIS
 .Nm script
-.Op Fl a
+.Op Fl afq
 .Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
@@ -71,6 +71,10 @@ Run
 .Ar command
 instead of an interactive shell.
 To run a command with arguments, enclose both in quotes.
+.It Fl f
+Flush the output after each write.
+.It Fl q
+Only print errors and warnings.
 .El
 .Pp
 The script ends when the forked program exits (a control-D
diff --git usr.bin/script/script.c usr.bin/script/script.c
index 2e4173941..a58b618d5 100644
--- usr.bin/script/script.c
+++ usr.bin/script/script.c
@@ -85,7 +85,7 @@ volatile sig_atomic_t sigdeadstatus;
 volatile sig_atomic_t flush;
 
 struct	termios tt;

-intistty;
+intistty, qflg;
 
 __dead void done(int);

 void dooutput(void);
@@ -104,11 +104,11 @@ main(int argc, char *argv[])
char ibuf[BUFSIZ];
char *cmd;
ssize_t cc, off;
-   int aflg, ch;
+   int aflg, fflg, ch;
 
 	cmd = NULL;

-   aflg = 0;
-   while ((ch = getopt(argc, argv, "ac:")) != -1)
+   aflg = fflg = 0;
+   while ((ch = getopt(argc, argv, "ac:fq")) != -1)
switch(ch) {
case 'a':
aflg = 1;
@@ -116,8 +116,14 @@ main(int argc, char *argv[])
case 'c':
cmd = optarg;
break;
+   case 'f':
+   fflg = 1;
+   break;
+   case 'q':
+   qflg = 1;
+   break;
default:
-   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+   fprintf(stderr, "usage: %s [-afq] [-c command] 
[file]\n",
__progname);
exit(1);
}
@@ -132,6 +138,9 @@ main(int argc, char *argv[])
if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
err(1, "%s", fname);
 
+	if (fflg)

+   setvbuf(fscript, NULL, _IONBF, 0);
+
if (isatty(0)) {
if (tcgetattr(STDIN_FILENO, ) == 0 &&
ioctl(STDIN_FILENO, TIOCGWINSZ, ) == 0)
@@ -140,7 +149,8 @@ main(int argc, char *argv[])
if (openpty(, , NULL, , ) == -1)
err(1, "openpty");
 
-	(void)printf("Script started, output file is %s\n", fname);

+   if (qflg)
+   (void)printf("Script started, output file is %s\n", fname);
if (istty) {
struct termios rtt = tt;
 
@@ -350,7 +360,8 @@ done(int eval)

} else {
if (istty)
(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, );
-   (void)printf("Script done, output file is %s\n", fname);
+   if (qflg)
+   (void)printf("Script done, output file is %s\n", fname);
}
exit(eval);
 }



add -f option to script(1)

2018-07-13 Thread bijan

Hi tech.

While trying to test liveshell[1], I noticed the script(1) from base is
missing the -f option, flushing the output after each write, which is
kinda critical for monitoring the output file by another process. the
Linux script utility uses fflush(3) after each write (duhh) but I'm not
sure if anything more than removing buffering operation from
the stream is necessary.

So here's the diff which is working for me

1: http://liveshell.43z.one/

--
Bijan Ebrahimi


diff --git usr.bin/script/script.1 usr.bin/script/script.1
index f10ec2d4b..48a9d683f 100644
--- usr.bin/script/script.1
+++ usr.bin/script/script.1
@@ -38,7 +38,7 @@
 .Nd make typescript of terminal session
 .Sh SYNOPSIS
 .Nm script
-.Op Fl a
+.Op Fl af
 .Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
@@ -71,6 +71,8 @@ Run
 .Ar command
 instead of an interactive shell.
 To run a command with arguments, enclose both in quotes.
+.It Fl f
+Flush the output after each write.
 .El
 .Pp
 The script ends when the forked program exits (a control-D
diff --git usr.bin/script/script.c usr.bin/script/script.c
index 2e4173941..b23e1c4d4 100644
--- usr.bin/script/script.c
+++ usr.bin/script/script.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: script.c,v 1.34 2018/01/21 20:18:20 jasper Exp $  */
+/* $OpenBSD: script.c,v 0.34 2018/01/21 20:18:20 jasper Exp $  */
 /* $NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc Exp $   */

 /*
@@ -104,11 +104,11 @@ main(int argc, char *argv[])
char ibuf[BUFSIZ];
char *cmd;
ssize_t cc, off;
-   int aflg, ch;
+   int aflg, fflg, ch;

cmd = NULL;
-   aflg = 0;
-   while ((ch = getopt(argc, argv, "ac:")) != -1)
+   aflg = fflg = 0;
+   while ((ch = getopt(argc, argv, "ac:f")) != -1)
switch(ch) {
case 'a':
aflg = 1;
@@ -116,8 +116,11 @@ main(int argc, char *argv[])
case 'c':
cmd = optarg;
break;
+   case 'f':
+   fflg = 1;
+   break;
default:
-   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+   fprintf(stderr, "usage: %s [-af] [-c command] [file]\n",
__progname);
exit(1);
}
@@ -132,6 +135,9 @@ main(int argc, char *argv[])
if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
err(1, "%s", fname);

+   if (fflg)
+   setvbuf(fscript, NULL, _IONBF, 0);
+
if (isatty(0)) {
if (tcgetattr(STDIN_FILENO, ) == 0 &&
ioctl(STDIN_FILENO, TIOCGWINSZ, ) == 0)