Re: Add "-c command" option to script(1)

2018-01-17 Thread Paul de Weerd
ping

Anyone?  Buehler? :)

Paul

On Mon, Dec 25, 2017 at 12:23:44PM +0100, Paul de Weerd wrote:
| Hi all,
| 
| Sorry to keep harping on this script stuff, but I'd really like to see
| this committed.  I've just upgraded my laptop while doing some
| vlan-bridging debugging and suddenly script(1) lost my new favorite
| feature.
| 
| The manpage bits are OK jmc@; job@ and ian@ (off-list) OK'd the diff.
| Is anyone willing to commit it with those?
| 
| Thanks,
| 
| Paul
| 
| On Sat, Dec 16, 2017 at 09:45:02AM +0100, Paul de Weerd wrote:
| | On Fri, Dec 15, 2017 at 12:24:45PM +0100, Paul de Weerd wrote:
| | | I've updated the diff to add this example as per jmc's suggestion.  It
| | | now has:
| | | 
| | |   - add the `-c command` feature
| | |   - updates usage
| | |   - removes /* ARGSUSED */ lint comments
| | |   - documents the -c feature
| | |   - adds an example to the manpage
| | 
| | jmc@ pointed out a missing colon at the end of the example.  Apologies
| | for the extra noise.  Updated diff (still covers the above five
| | changes) included.
| | 
| | Cheers,
| | 
| | Paul
| | 
| | 
| | Index: script.1
| | ===
| | RCS file: /cvs/src/usr.bin/script/script.1,v
| | retrieving revision 1.14
| | diff -u -p -r1.14 script.1
| | --- script.115 Jan 2012 20:06:40 -  1.14
| | +++ script.116 Dec 2017 08:42:24 -
| | @@ -39,6 +39,7 @@
| |  .Sh SYNOPSIS
| |  .Nm script
| |  .Op Fl a
| | +.Op Fl c Ar command
| |  .Op Ar file
| |  .Sh DESCRIPTION
| |  .Nm
| | @@ -65,9 +66,14 @@ Append the output to
| |  or
| |  .Pa typescript ,
| |  retaining the prior contents.
| | +.It Fl c Ar command
| | +Run
| | +.Ar command
| | +instead of an interactive shell.
| | +To run a command with arguments, enclose both in quotes.
| |  .El
| |  .Pp
| | -The script ends when the forked shell exits (a control-D
| | +The script ends when the forked program exits (a control-D
| |  .Pq Ql ^D
| |  to exit
| |  the Bourne shell
| | @@ -102,6 +108,11 @@ Name of the shell to be forked by
| |  If not set, the Bourne shell is assumed.
| |  (Most shells set this variable automatically.)
| |  .El
| | +.Sh EXAMPLES
| | +Start a virtual machine and log all console output to a file:
| | +.Bd -literal -offset indent
| | +$ script -c "vmctl start myvm -c" myvm.typescript
| | +.Ed
| |  .Sh HISTORY
| |  A predecessor called
| |  .Nm dribble
| | Index: script.c
| | ===
| | RCS file: /cvs/src/usr.bin/script/script.c,v
| | retrieving revision 1.33
| | diff -u -p -r1.33 script.c
| | --- script.c12 Apr 2017 14:49:05 -  1.33
| | +++ script.c14 Dec 2017 07:34:10 -
| | @@ -89,7 +89,7 @@ int   istty;
| |  
| |  __dead void done(int);
| |  void dooutput(void);
| | -void doshell(void);
| | +void doshell(char *);
| |  void fail(void);
| |  void finish(int);
| |  void scriptflush(int);
| | @@ -102,17 +102,23 @@ main(int argc, char *argv[])
| | struct sigaction sa;
| | struct winsize win;
| | char ibuf[BUFSIZ];
| | +   char *cmd;
| | ssize_t cc, off;
| | int aflg, ch;
| |  
| | +   cmd = NULL;
| | aflg = 0;
| | -   while ((ch = getopt(argc, argv, "a")) != -1)
| | +   while ((ch = getopt(argc, argv, "ac:")) != -1)
| | switch(ch) {
| | case 'a':
| | aflg = 1;
| | break;
| | +   case 'c':
| | +   cmd = optarg;
| | +   break;
| | default:
| | -   fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
| | +   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
| | +   __progname);
| | exit(1);
| | }
| | argc -= optind;
| | @@ -163,7 +169,7 @@ main(int argc, char *argv[])
| | if (child)
| | dooutput();
| | else
| | -   doshell();
| | +   doshell(cmd);
| | }
| |  
| | bzero(&sa, sizeof sa);
| | @@ -196,7 +202,6 @@ main(int argc, char *argv[])
| | done(sigdeadstatus);
| |  }
| |  
| | -/* ARGSUSED */
| |  void
| |  finish(int signo)
| |  {
| | @@ -215,7 +220,6 @@ finish(int signo)
| | errno = save_errno;
| |  }
| |  
| | -/* ARGSUSED */
| |  void
| |  handlesigwinch(int signo)
| |  {
| | @@ -294,7 +298,6 @@ dooutput(void)
| | done(0);
| |  }
| |  
| | -/* ARGSUSED */
| |  void
| |  scriptflush(int signo)
| |  {
| | @@ -302,9 +305,10 @@ scriptflush(int signo)
| |  }
| |  
| |  void
| | -doshell(void)
| | +doshell(char *cmd)
| |  {
| | char *shell;
| | +   char *argp[] = {"sh", "-c", NULL, NULL};
| |  
| | shell = getenv("SHELL");
| | if (shell == NULL)
| | @@ -313,8 +317,15 @@ doshell(void)
| | (void)close(master);
| | (void)fclose(fscript);
| | login_tty(slave);
| | -   execl(shell, shell, "-i", (char *)NULL);
| | -   warn("%s", shell);
| | +

Re: Add "-c command" option to script(1)

2017-12-25 Thread Paul de Weerd
Hi all,

Sorry to keep harping on this script stuff, but I'd really like to see
this committed.  I've just upgraded my laptop while doing some
vlan-bridging debugging and suddenly script(1) lost my new favorite
feature.

The manpage bits are OK jmc@; job@ and ian@ (off-list) OK'd the diff.
Is anyone willing to commit it with those?

Thanks,

Paul

On Sat, Dec 16, 2017 at 09:45:02AM +0100, Paul de Weerd wrote:
| On Fri, Dec 15, 2017 at 12:24:45PM +0100, Paul de Weerd wrote:
| | I've updated the diff to add this example as per jmc's suggestion.  It
| | now has:
| | 
| | - add the `-c command` feature
| | - updates usage
| | - removes /* ARGSUSED */ lint comments
| | - documents the -c feature
| | - adds an example to the manpage
| 
| jmc@ pointed out a missing colon at the end of the example.  Apologies
| for the extra noise.  Updated diff (still covers the above five
| changes) included.
| 
| Cheers,
| 
| Paul
| 
| 
| Index: script.1
| ===
| RCS file: /cvs/src/usr.bin/script/script.1,v
| retrieving revision 1.14
| diff -u -p -r1.14 script.1
| --- script.1  15 Jan 2012 20:06:40 -  1.14
| +++ script.1  16 Dec 2017 08:42:24 -
| @@ -39,6 +39,7 @@
|  .Sh SYNOPSIS
|  .Nm script
|  .Op Fl a
| +.Op Fl c Ar command
|  .Op Ar file
|  .Sh DESCRIPTION
|  .Nm
| @@ -65,9 +66,14 @@ Append the output to
|  or
|  .Pa typescript ,
|  retaining the prior contents.
| +.It Fl c Ar command
| +Run
| +.Ar command
| +instead of an interactive shell.
| +To run a command with arguments, enclose both in quotes.
|  .El
|  .Pp
| -The script ends when the forked shell exits (a control-D
| +The script ends when the forked program exits (a control-D
|  .Pq Ql ^D
|  to exit
|  the Bourne shell
| @@ -102,6 +108,11 @@ Name of the shell to be forked by
|  If not set, the Bourne shell is assumed.
|  (Most shells set this variable automatically.)
|  .El
| +.Sh EXAMPLES
| +Start a virtual machine and log all console output to a file:
| +.Bd -literal -offset indent
| +$ script -c "vmctl start myvm -c" myvm.typescript
| +.Ed
|  .Sh HISTORY
|  A predecessor called
|  .Nm dribble
| Index: script.c
| ===
| RCS file: /cvs/src/usr.bin/script/script.c,v
| retrieving revision 1.33
| diff -u -p -r1.33 script.c
| --- script.c  12 Apr 2017 14:49:05 -  1.33
| +++ script.c  14 Dec 2017 07:34:10 -
| @@ -89,7 +89,7 @@ int istty;
|  
|  __dead void done(int);
|  void dooutput(void);
| -void doshell(void);
| +void doshell(char *);
|  void fail(void);
|  void finish(int);
|  void scriptflush(int);
| @@ -102,17 +102,23 @@ main(int argc, char *argv[])
|   struct sigaction sa;
|   struct winsize win;
|   char ibuf[BUFSIZ];
| + char *cmd;
|   ssize_t cc, off;
|   int aflg, ch;
|  
| + cmd = NULL;
|   aflg = 0;
| - while ((ch = getopt(argc, argv, "a")) != -1)
| + while ((ch = getopt(argc, argv, "ac:")) != -1)
|   switch(ch) {
|   case 'a':
|   aflg = 1;
|   break;
| + case 'c':
| + cmd = optarg;
| + break;
|   default:
| - fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
| + fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
| + __progname);
|   exit(1);
|   }
|   argc -= optind;
| @@ -163,7 +169,7 @@ main(int argc, char *argv[])
|   if (child)
|   dooutput();
|   else
| - doshell();
| + doshell(cmd);
|   }
|  
|   bzero(&sa, sizeof sa);
| @@ -196,7 +202,6 @@ main(int argc, char *argv[])
|   done(sigdeadstatus);
|  }
|  
| -/* ARGSUSED */
|  void
|  finish(int signo)
|  {
| @@ -215,7 +220,6 @@ finish(int signo)
|   errno = save_errno;
|  }
|  
| -/* ARGSUSED */
|  void
|  handlesigwinch(int signo)
|  {
| @@ -294,7 +298,6 @@ dooutput(void)
|   done(0);
|  }
|  
| -/* ARGSUSED */
|  void
|  scriptflush(int signo)
|  {
| @@ -302,9 +305,10 @@ scriptflush(int signo)
|  }
|  
|  void
| -doshell(void)
| +doshell(char *cmd)
|  {
|   char *shell;
| + char *argp[] = {"sh", "-c", NULL, NULL};
|  
|   shell = getenv("SHELL");
|   if (shell == NULL)
| @@ -313,8 +317,15 @@ doshell(void)
|   (void)close(master);
|   (void)fclose(fscript);
|   login_tty(slave);
| - execl(shell, shell, "-i", (char *)NULL);
| - warn("%s", shell);
| +
| + if (cmd != NULL) {
| + argp[2] = cmd;
| + execv(_PATH_BSHELL, argp);
| + warn("unable to execute %s", _PATH_BSHELL);
| + } else {
| + execl(shell, shell, "-i", (char *)NULL);
| + warn("%s", shell);
| + }
|   fail();
|  }
|  
| 
| -- 
| >[<++>-]<+++.>+++[<-->-]<.>+++[<

Re: Add "-c command" option to script(1)

2017-12-16 Thread Job Snijders
On Sat, Dec 16, 2017 at 09:45:02AM +0100, Paul de Weerd wrote:
> On Fri, Dec 15, 2017 at 12:24:45PM +0100, Paul de Weerd wrote:
> | I've updated the diff to add this example as per jmc's suggestion.  It
> | now has:
> | 
> | - add the `-c command` feature
> | - updates usage
> | - removes /* ARGSUSED */ lint comments
> | - documents the -c feature
> | - adds an example to the manpage
> 
> jmc@ pointed out a missing colon at the end of the example.  Apologies
> for the extra noise.  Updated diff (still covers the above five
> changes) included.

OK job@



Re: Add "-c command" option to script(1)

2017-12-16 Thread Paul de Weerd
On Fri, Dec 15, 2017 at 12:24:45PM +0100, Paul de Weerd wrote:
| I've updated the diff to add this example as per jmc's suggestion.  It
| now has:
| 
|   - add the `-c command` feature
|   - updates usage
|   - removes /* ARGSUSED */ lint comments
|   - documents the -c feature
|   - adds an example to the manpage

jmc@ pointed out a missing colon at the end of the example.  Apologies
for the extra noise.  Updated diff (still covers the above five
changes) included.

Cheers,

Paul


Index: script.1
===
RCS file: /cvs/src/usr.bin/script/script.1,v
retrieving revision 1.14
diff -u -p -r1.14 script.1
--- script.115 Jan 2012 20:06:40 -  1.14
+++ script.116 Dec 2017 08:42:24 -
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm script
 .Op Fl a
+.Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
@@ -65,9 +66,14 @@ Append the output to
 or
 .Pa typescript ,
 retaining the prior contents.
+.It Fl c Ar command
+Run
+.Ar command
+instead of an interactive shell.
+To run a command with arguments, enclose both in quotes.
 .El
 .Pp
-The script ends when the forked shell exits (a control-D
+The script ends when the forked program exits (a control-D
 .Pq Ql ^D
 to exit
 the Bourne shell
@@ -102,6 +108,11 @@ Name of the shell to be forked by
 If not set, the Bourne shell is assumed.
 (Most shells set this variable automatically.)
 .El
+.Sh EXAMPLES
+Start a virtual machine and log all console output to a file:
+.Bd -literal -offset indent
+$ script -c "vmctl start myvm -c" myvm.typescript
+.Ed
 .Sh HISTORY
 A predecessor called
 .Nm dribble
Index: script.c
===
RCS file: /cvs/src/usr.bin/script/script.c,v
retrieving revision 1.33
diff -u -p -r1.33 script.c
--- script.c12 Apr 2017 14:49:05 -  1.33
+++ script.c14 Dec 2017 07:34:10 -
@@ -89,7 +89,7 @@ int   istty;
 
 __dead void done(int);
 void dooutput(void);
-void doshell(void);
+void doshell(char *);
 void fail(void);
 void finish(int);
 void scriptflush(int);
@@ -102,17 +102,23 @@ main(int argc, char *argv[])
struct sigaction sa;
struct winsize win;
char ibuf[BUFSIZ];
+   char *cmd;
ssize_t cc, off;
int aflg, ch;
 
+   cmd = NULL;
aflg = 0;
-   while ((ch = getopt(argc, argv, "a")) != -1)
+   while ((ch = getopt(argc, argv, "ac:")) != -1)
switch(ch) {
case 'a':
aflg = 1;
break;
+   case 'c':
+   cmd = optarg;
+   break;
default:
-   fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
+   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+   __progname);
exit(1);
}
argc -= optind;
@@ -163,7 +169,7 @@ main(int argc, char *argv[])
if (child)
dooutput();
else
-   doshell();
+   doshell(cmd);
}
 
bzero(&sa, sizeof sa);
@@ -196,7 +202,6 @@ main(int argc, char *argv[])
done(sigdeadstatus);
 }
 
-/* ARGSUSED */
 void
 finish(int signo)
 {
@@ -215,7 +220,6 @@ finish(int signo)
errno = save_errno;
 }
 
-/* ARGSUSED */
 void
 handlesigwinch(int signo)
 {
@@ -294,7 +298,6 @@ dooutput(void)
done(0);
 }
 
-/* ARGSUSED */
 void
 scriptflush(int signo)
 {
@@ -302,9 +305,10 @@ scriptflush(int signo)
 }
 
 void
-doshell(void)
+doshell(char *cmd)
 {
char *shell;
+   char *argp[] = {"sh", "-c", NULL, NULL};
 
shell = getenv("SHELL");
if (shell == NULL)
@@ -313,8 +317,15 @@ doshell(void)
(void)close(master);
(void)fclose(fscript);
login_tty(slave);
-   execl(shell, shell, "-i", (char *)NULL);
-   warn("%s", shell);
+
+   if (cmd != NULL) {
+   argp[2] = cmd;
+   execv(_PATH_BSHELL, argp);
+   warn("unable to execute %s", _PATH_BSHELL);
+   } else {
+   execl(shell, shell, "-i", (char *)NULL);
+   warn("%s", shell);
+   }
fail();
 }
 

-- 
>[<++>-]<+++.>+++[<-->-]<.>+++[<+
+++>-]<.>++[<>-]<+.--.[-]
 http://www.weirdnet.nl/ 



Re: Add "-c command" option to script(1)

2017-12-15 Thread Job Snijders
On Thu, Dec 14, 2017 at 09:23:29AM +0100, Paul de Weerd wrote:
> Another use I personally find very convenient is this:
> 
> [weerd@pom] $ script -c "vmctl start test -c"
> 
> Hope others see value here too :)

That is a great use case.

Kind regards,

Job



Re: Add "-c command" option to script(1)

2017-12-15 Thread Paul de Weerd
On Thu, Dec 14, 2017 at 08:49:18AM +, Jason McIntyre wrote:
| On Thu, Dec 14, 2017 at 09:23:29AM +0100, Paul de Weerd wrote:
| > Another use I personally find very convenient is this:
| > 
| > [weerd@pom] $ script -c "vmctl start test -c"
| 
| you could add this as an EXAMPLES section, since it nicely describes
| another way to use script(1).

I've updated the diff to add this example as per jmc's suggestion.  It
now has:

- add the `-c command` feature
- updates usage
- removes /* ARGSUSED */ lint comments
- documents the -c feature
- adds an example to the manpage

There was a bit of off-list discussion with jmc@ on further
improvements that could be made to the manpage, but in order not to
add too much in one diff I decided to keep it at this.

Anybody any other comments?  Could this go in as it is now?

Cheers,

Paul


Index: script.1
===
RCS file: /cvs/src/usr.bin/script/script.1,v
retrieving revision 1.14
diff -u -p -r1.14 script.1
--- script.115 Jan 2012 20:06:40 -  1.14
+++ script.114 Dec 2017 10:04:09 -
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm script
 .Op Fl a
+.Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
@@ -65,9 +66,14 @@ Append the output to
 or
 .Pa typescript ,
 retaining the prior contents.
+.It Fl c Ar command
+Run
+.Ar command
+instead of an interactive shell.
+To run a command with arguments, enclose both in quotes.
 .El
 .Pp
-The script ends when the forked shell exits (a control-D
+The script ends when the forked program exits (a control-D
 .Pq Ql ^D
 to exit
 the Bourne shell
@@ -102,6 +108,11 @@ Name of the shell to be forked by
 If not set, the Bourne shell is assumed.
 (Most shells set this variable automatically.)
 .El
+.Sh EXAMPLES
+Start a virtual machine and log all console output to a file
+.Bd -literal -offset indent
+$ script -c "vmctl start myvm -c" myvm.typescript
+.Ed
 .Sh HISTORY
 A predecessor called
 .Nm dribble
Index: script.c
===
RCS file: /cvs/src/usr.bin/script/script.c,v
retrieving revision 1.33
diff -u -p -r1.33 script.c
--- script.c12 Apr 2017 14:49:05 -  1.33
+++ script.c14 Dec 2017 07:34:10 -
@@ -89,7 +89,7 @@ int   istty;
 
 __dead void done(int);
 void dooutput(void);
-void doshell(void);
+void doshell(char *);
 void fail(void);
 void finish(int);
 void scriptflush(int);
@@ -102,17 +102,23 @@ main(int argc, char *argv[])
struct sigaction sa;
struct winsize win;
char ibuf[BUFSIZ];
+   char *cmd;
ssize_t cc, off;
int aflg, ch;
 
+   cmd = NULL;
aflg = 0;
-   while ((ch = getopt(argc, argv, "a")) != -1)
+   while ((ch = getopt(argc, argv, "ac:")) != -1)
switch(ch) {
case 'a':
aflg = 1;
break;
+   case 'c':
+   cmd = optarg;
+   break;
default:
-   fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
+   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+   __progname);
exit(1);
}
argc -= optind;
@@ -163,7 +169,7 @@ main(int argc, char *argv[])
if (child)
dooutput();
else
-   doshell();
+   doshell(cmd);
}
 
bzero(&sa, sizeof sa);
@@ -196,7 +202,6 @@ main(int argc, char *argv[])
done(sigdeadstatus);
 }
 
-/* ARGSUSED */
 void
 finish(int signo)
 {
@@ -215,7 +220,6 @@ finish(int signo)
errno = save_errno;
 }
 
-/* ARGSUSED */
 void
 handlesigwinch(int signo)
 {
@@ -294,7 +298,6 @@ dooutput(void)
done(0);
 }
 
-/* ARGSUSED */
 void
 scriptflush(int signo)
 {
@@ -302,9 +305,10 @@ scriptflush(int signo)
 }
 
 void
-doshell(void)
+doshell(char *cmd)
 {
char *shell;
+   char *argp[] = {"sh", "-c", NULL, NULL};
 
shell = getenv("SHELL");
if (shell == NULL)
@@ -313,8 +317,15 @@ doshell(void)
(void)close(master);
(void)fclose(fscript);
login_tty(slave);
-   execl(shell, shell, "-i", (char *)NULL);
-   warn("%s", shell);
+
+   if (cmd != NULL) {
+   argp[2] = cmd;
+   execv(_PATH_BSHELL, argp);
+   warn("unable to execute %s", _PATH_BSHELL);
+   } else {
+   execl(shell, shell, "-i", (char *)NULL);
+   warn("%s", shell);
+   }
fail();
 }
 

-- 
>[<++>-]<+++.>+++[<-->-]<.>+++[<+
+++>-]<.>++[<>-]<+.--.[-]
 http://www.weirdnet.nl/ 



Re: Add "-c command" option to script(1)

2017-12-14 Thread Jason McIntyre
On Thu, Dec 14, 2017 at 09:23:29AM +0100, Paul de Weerd wrote:
> Another use I personally find very convenient is this:
> 
> [weerd@pom] $ script -c "vmctl start test -c"
> 
> Hope others see value here too :)
> 
> Paul
> 

you could add this as an EXAMPLES section, since it nicely describes
another way to use script(1).

jmc



Re: Add "-c command" option to script(1)

2017-12-14 Thread Paul de Weerd
Another use I personally find very convenient is this:

[weerd@pom] $ script -c "vmctl start test -c"

Hope others see value here too :)

Paul

On Thu, Dec 14, 2017 at 08:45:19AM +0100, Paul de Weerd wrote:
| Hi Jason,
| 
| Thank you for your quick feedback!  I've incorporated yours and
| off-list feedback from ian@ into a new diff included below.  The diff
| now also includes the removal of /* ARGSUSED */ lint comments.
| 
| On Wed, Dec 13, 2017 at 09:34:03PM +, Jason McIntyre wrote:
| | > +To run a
| | 
| | i'd remove "a", but it's preference only - your choice.
| | 
| | > +.Ar command
| | > +with its own arguments, enclose it in quotes.
| | 
| | and s/its own//
| 
| Rewrote this sentence a bit, it now reads:
| 
| -c command
| Run command instead of an interactive shell.  To run a command
| with arguments, enclose both in quotes.
| 
| Still not 100% happy with it, but the best I could come up with.
| Better suggestions wanted :)
| 
| Paul
| 
| Index: script.1
| ===
| RCS file: /cvs/src/usr.bin/script/script.1,v
| retrieving revision 1.14
| diff -u -p -r1.14 script.1
| --- script.1  15 Jan 2012 20:06:40 -  1.14
| +++ script.1  14 Dec 2017 07:36:11 -
| @@ -39,6 +39,7 @@
|  .Sh SYNOPSIS
|  .Nm script
|  .Op Fl a
| +.Op Fl c Ar command
|  .Op Ar file
|  .Sh DESCRIPTION
|  .Nm
| @@ -65,9 +66,14 @@ Append the output to
|  or
|  .Pa typescript ,
|  retaining the prior contents.
| +.It Fl c Ar command
| +Run
| +.Ar command
| +instead of an interactive shell.
| +To run a command with arguments, enclose both in quotes.
|  .El
|  .Pp
| -The script ends when the forked shell exits (a control-D
| +The script ends when the forked program exits (a control-D
|  .Pq Ql ^D
|  to exit
|  the Bourne shell
| Index: script.c
| ===
| RCS file: /cvs/src/usr.bin/script/script.c,v
| retrieving revision 1.33
| diff -u -p -r1.33 script.c
| --- script.c  12 Apr 2017 14:49:05 -  1.33
| +++ script.c  14 Dec 2017 07:34:10 -
| @@ -89,7 +89,7 @@ int istty;
|  
|  __dead void done(int);
|  void dooutput(void);
| -void doshell(void);
| +void doshell(char *);
|  void fail(void);
|  void finish(int);
|  void scriptflush(int);
| @@ -102,17 +102,23 @@ main(int argc, char *argv[])
|   struct sigaction sa;
|   struct winsize win;
|   char ibuf[BUFSIZ];
| + char *cmd;
|   ssize_t cc, off;
|   int aflg, ch;
|  
| + cmd = NULL;
|   aflg = 0;
| - while ((ch = getopt(argc, argv, "a")) != -1)
| + while ((ch = getopt(argc, argv, "ac:")) != -1)
|   switch(ch) {
|   case 'a':
|   aflg = 1;
|   break;
| + case 'c':
| + cmd = optarg;
| + break;
|   default:
| - fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
| + fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
| + __progname);
|   exit(1);
|   }
|   argc -= optind;
| @@ -163,7 +169,7 @@ main(int argc, char *argv[])
|   if (child)
|   dooutput();
|   else
| - doshell();
| + doshell(cmd);
|   }
|  
|   bzero(&sa, sizeof sa);
| @@ -196,7 +202,6 @@ main(int argc, char *argv[])
|   done(sigdeadstatus);
|  }
|  
| -/* ARGSUSED */
|  void
|  finish(int signo)
|  {
| @@ -215,7 +220,6 @@ finish(int signo)
|   errno = save_errno;
|  }
|  
| -/* ARGSUSED */
|  void
|  handlesigwinch(int signo)
|  {
| @@ -294,7 +298,6 @@ dooutput(void)
|   done(0);
|  }
|  
| -/* ARGSUSED */
|  void
|  scriptflush(int signo)
|  {
| @@ -302,9 +305,10 @@ scriptflush(int signo)
|  }
|  
|  void
| -doshell(void)
| +doshell(char *cmd)
|  {
|   char *shell;
| + char *argp[] = {"sh", "-c", NULL, NULL};
|  
|   shell = getenv("SHELL");
|   if (shell == NULL)
| @@ -313,8 +317,15 @@ doshell(void)
|   (void)close(master);
|   (void)fclose(fscript);
|   login_tty(slave);
| - execl(shell, shell, "-i", (char *)NULL);
| - warn("%s", shell);
| +
| + if (cmd != NULL) {
| + argp[2] = cmd;
| + execv(_PATH_BSHELL, argp);
| + warn("unable to execute %s", _PATH_BSHELL);
| + } else {
| + execl(shell, shell, "-i", (char *)NULL);
| + warn("%s", shell);
| + }
|   fail();
|  }
| 
|  
| -- 
| >[<++>-]<+++.>+++[<-->-]<.>+++[<+
| +++>-]<.>++[<>-]<+.--.[-]
|  http://www.weirdnet.nl/ 
| 

-- 
>[<++>-]<+++.>+++[<-->-]<.>+++[<+
+++>-]<.>++[<>-]<+.--.[-]
 http://www.weirdnet.nl/ 



Re: Add "-c command" option to script(1)

2017-12-13 Thread Paul de Weerd
Hi Jason,

Thank you for your quick feedback!  I've incorporated yours and
off-list feedback from ian@ into a new diff included below.  The diff
now also includes the removal of /* ARGSUSED */ lint comments.

On Wed, Dec 13, 2017 at 09:34:03PM +, Jason McIntyre wrote:
| > +To run a
| 
| i'd remove "a", but it's preference only - your choice.
| 
| > +.Ar command
| > +with its own arguments, enclose it in quotes.
| 
| and s/its own//

Rewrote this sentence a bit, it now reads:

-c command
Run command instead of an interactive shell.  To run a command
with arguments, enclose both in quotes.

Still not 100% happy with it, but the best I could come up with.
Better suggestions wanted :)

Paul

Index: script.1
===
RCS file: /cvs/src/usr.bin/script/script.1,v
retrieving revision 1.14
diff -u -p -r1.14 script.1
--- script.115 Jan 2012 20:06:40 -  1.14
+++ script.114 Dec 2017 07:36:11 -
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm script
 .Op Fl a
+.Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
@@ -65,9 +66,14 @@ Append the output to
 or
 .Pa typescript ,
 retaining the prior contents.
+.It Fl c Ar command
+Run
+.Ar command
+instead of an interactive shell.
+To run a command with arguments, enclose both in quotes.
 .El
 .Pp
-The script ends when the forked shell exits (a control-D
+The script ends when the forked program exits (a control-D
 .Pq Ql ^D
 to exit
 the Bourne shell
Index: script.c
===
RCS file: /cvs/src/usr.bin/script/script.c,v
retrieving revision 1.33
diff -u -p -r1.33 script.c
--- script.c12 Apr 2017 14:49:05 -  1.33
+++ script.c14 Dec 2017 07:34:10 -
@@ -89,7 +89,7 @@ int   istty;
 
 __dead void done(int);
 void dooutput(void);
-void doshell(void);
+void doshell(char *);
 void fail(void);
 void finish(int);
 void scriptflush(int);
@@ -102,17 +102,23 @@ main(int argc, char *argv[])
struct sigaction sa;
struct winsize win;
char ibuf[BUFSIZ];
+   char *cmd;
ssize_t cc, off;
int aflg, ch;
 
+   cmd = NULL;
aflg = 0;
-   while ((ch = getopt(argc, argv, "a")) != -1)
+   while ((ch = getopt(argc, argv, "ac:")) != -1)
switch(ch) {
case 'a':
aflg = 1;
break;
+   case 'c':
+   cmd = optarg;
+   break;
default:
-   fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
+   fprintf(stderr, "usage: %s [-a] [-c command] [file]\n",
+   __progname);
exit(1);
}
argc -= optind;
@@ -163,7 +169,7 @@ main(int argc, char *argv[])
if (child)
dooutput();
else
-   doshell();
+   doshell(cmd);
}
 
bzero(&sa, sizeof sa);
@@ -196,7 +202,6 @@ main(int argc, char *argv[])
done(sigdeadstatus);
 }
 
-/* ARGSUSED */
 void
 finish(int signo)
 {
@@ -215,7 +220,6 @@ finish(int signo)
errno = save_errno;
 }
 
-/* ARGSUSED */
 void
 handlesigwinch(int signo)
 {
@@ -294,7 +298,6 @@ dooutput(void)
done(0);
 }
 
-/* ARGSUSED */
 void
 scriptflush(int signo)
 {
@@ -302,9 +305,10 @@ scriptflush(int signo)
 }
 
 void
-doshell(void)
+doshell(char *cmd)
 {
char *shell;
+   char *argp[] = {"sh", "-c", NULL, NULL};
 
shell = getenv("SHELL");
if (shell == NULL)
@@ -313,8 +317,15 @@ doshell(void)
(void)close(master);
(void)fclose(fscript);
login_tty(slave);
-   execl(shell, shell, "-i", (char *)NULL);
-   warn("%s", shell);
+
+   if (cmd != NULL) {
+   argp[2] = cmd;
+   execv(_PATH_BSHELL, argp);
+   warn("unable to execute %s", _PATH_BSHELL);
+   } else {
+   execl(shell, shell, "-i", (char *)NULL);
+   warn("%s", shell);
+   }
fail();
 }

 
-- 
>[<++>-]<+++.>+++[<-->-]<.>+++[<+
+++>-]<.>++[<>-]<+.--.[-]
 http://www.weirdnet.nl/ 



Re: Add "-c command" option to script(1)

2017-12-13 Thread Jason McIntyre
On Wed, Dec 13, 2017 at 05:45:25PM +0100, Paul de Weerd wrote:
> 
> Index: script.1
> ===
> RCS file: /cvs/src/usr.bin/script/script.1,v
> retrieving revision 1.14
> diff -u -p -r1.14 script.1
> --- script.1  15 Jan 2012 20:06:40 -  1.14
> +++ script.1  13 Dec 2017 12:27:32 -
> @@ -39,6 +39,7 @@
>  .Sh SYNOPSIS
>  .Nm script
>  .Op Fl a
> +.Op Fl c Ar command
>  .Op Ar file
>  .Sh DESCRIPTION
>  .Nm
> @@ -65,6 +66,13 @@ Append the output to
>  or
>  .Pa typescript ,
>  retaining the prior contents.
> +.It Fl c Ar command
> +Run
> +.Ar command
> +instead of an interactive shell.
> +To run a

i'd remove "a", but it's preference only - your choice.

> +.Ar command
> +with its own arguments, enclose it in quotes.

and s/its own//

>  .El
>  .Pp
>  The script ends when the forked shell exits (a control-D
> Index: script.c
> ===

you want to update usage() too.

jmc

> RCS file: /cvs/src/usr.bin/script/script.c,v
> retrieving revision 1.33
> diff -u -p -r1.33 script.c
> --- script.c  12 Apr 2017 14:49:05 -  1.33
> +++ script.c  13 Dec 2017 12:15:20 -
> @@ -89,7 +89,7 @@ int istty;
>  
>  __dead void done(int);
>  void dooutput(void);
> -void doshell(void);
> +void doshell(char *);
>  void fail(void);
>  void finish(int);
>  void scriptflush(int);
> @@ -102,15 +102,20 @@ main(int argc, char *argv[])
>   struct sigaction sa;
>   struct winsize win;
>   char ibuf[BUFSIZ];
> + char *cmd;
>   ssize_t cc, off;
>   int aflg, ch;
>  
> + cmd = NULL;
>   aflg = 0;
> - while ((ch = getopt(argc, argv, "a")) != -1)
> + while ((ch = getopt(argc, argv, "ac:")) != -1)
>   switch(ch) {
>   case 'a':
>   aflg = 1;
>   break;
> + case 'c':
> + cmd = optarg;
> + break;
>   default:
>   fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
>   exit(1);
> @@ -163,7 +168,7 @@ main(int argc, char *argv[])
>   if (child)
>   dooutput();
>   else
> - doshell();
> + doshell(cmd);
>   }
>  
>   bzero(&sa, sizeof sa);
> @@ -302,9 +307,10 @@ scriptflush(int signo)
>  }
>  
>  void
> -doshell(void)
> +doshell(char *cmd)
>  {
>   char *shell;
> + char *argp[] = {"sh", "-c", NULL, NULL};
>  
>   shell = getenv("SHELL");
>   if (shell == NULL)
> @@ -313,8 +319,15 @@ doshell(void)
>   (void)close(master);
>   (void)fclose(fscript);
>   login_tty(slave);
> - execl(shell, shell, "-i", (char *)NULL);
> - warn("%s", shell);
> +
> + if (cmd != NULL) {
> + argp[2] = cmd;
> + execv(_PATH_BSHELL, argp);
> + warn("unable to execute %s", _PATH_BSHELL);
> + } else {
> + execl(shell, shell, "-i", (char *)NULL);
> + warn("%s", shell);
> + }
>   fail();
>  }
>  
> 
> -- 
> >[<++>-]<+++.>+++[<-->-]<.>+++[<+
> +++>-]<.>++[<>-]<+.--.[-]
>  http://www.weirdnet.nl/ 
> 



Add "-c command" option to script(1)

2017-12-13 Thread Paul de Weerd
Hi all,

As seen in NetBSD, FreeBSD and Linux, script(1) could do with a way to
tell it what command to run instead of your $SHELL.  This implements
the "-c command" option.  I've taken the logic to start the command
(and its arguments, if any) from crontab(1), execv'ing `sh -c
`.

I wrote this because I got fed up with typing `cu -l cuaU3 -s 19200`
right after starting script every time: it annoys my command line
editing fingers that I can't just recall what I did before.

There's a roundabout way of achieving something similar by setting
SHELL to some script that runs your command with its arguments, but
that's just silly (and nasty).

NetBSD: uses "-c command", executes the command using system(3)
Linux: uses "-c command", didn't look at the implementation
FreeBSD: uses arguments after the filename, wields execvp(3)

(these three have a bunch more extra options, all of which mostly look
like bullshit - specifying an alternative command seems the only
useful thing to me)

Illumos: feature parity with current OpenBSD, no -c command


My implementation worked from the get go, so there must be something
wrong with it.  All comments and feedback appreciated; asbestos
underwear at the ready.

Sample output:
[weerd@pom] $ obj/script -c "ls -l" output.`date +%s`
Script started, output file is output.1513183391
total 36
drwxr-xr-x  2 weerd  wsrc   512 May  9  2017 CVS
-rw-r--r--  1 weerd  wsrc   131 Sep 21  1997 Makefile
lrwxrwx---  1 weerd  wsrc23 Dec 13 17:42 obj -> /usr/obj/usr.bin/script
-rw-r--r--  1 weerd  wsrc 0 Dec 13 17:43 output.1513183391
-rw-r--r--  1 weerd  wsrc  3642 Dec 13 17:42 script.1
-rw-r--r--  1 weerd  wsrc  8464 Dec 13 17:41 script.c
Script done, output file is output.1513183391


Paul 'WEiRD' de Weerd

Index: script.1
===
RCS file: /cvs/src/usr.bin/script/script.1,v
retrieving revision 1.14
diff -u -p -r1.14 script.1
--- script.115 Jan 2012 20:06:40 -  1.14
+++ script.113 Dec 2017 12:27:32 -
@@ -39,6 +39,7 @@
 .Sh SYNOPSIS
 .Nm script
 .Op Fl a
+.Op Fl c Ar command
 .Op Ar file
 .Sh DESCRIPTION
 .Nm
@@ -65,6 +66,13 @@ Append the output to
 or
 .Pa typescript ,
 retaining the prior contents.
+.It Fl c Ar command
+Run
+.Ar command
+instead of an interactive shell.
+To run a
+.Ar command
+with its own arguments, enclose it in quotes.
 .El
 .Pp
 The script ends when the forked shell exits (a control-D
Index: script.c
===
RCS file: /cvs/src/usr.bin/script/script.c,v
retrieving revision 1.33
diff -u -p -r1.33 script.c
--- script.c12 Apr 2017 14:49:05 -  1.33
+++ script.c13 Dec 2017 12:15:20 -
@@ -89,7 +89,7 @@ int   istty;
 
 __dead void done(int);
 void dooutput(void);
-void doshell(void);
+void doshell(char *);
 void fail(void);
 void finish(int);
 void scriptflush(int);
@@ -102,15 +102,20 @@ main(int argc, char *argv[])
struct sigaction sa;
struct winsize win;
char ibuf[BUFSIZ];
+   char *cmd;
ssize_t cc, off;
int aflg, ch;
 
+   cmd = NULL;
aflg = 0;
-   while ((ch = getopt(argc, argv, "a")) != -1)
+   while ((ch = getopt(argc, argv, "ac:")) != -1)
switch(ch) {
case 'a':
aflg = 1;
break;
+   case 'c':
+   cmd = optarg;
+   break;
default:
fprintf(stderr, "usage: %s [-a] [file]\n", __progname);
exit(1);
@@ -163,7 +168,7 @@ main(int argc, char *argv[])
if (child)
dooutput();
else
-   doshell();
+   doshell(cmd);
}
 
bzero(&sa, sizeof sa);
@@ -302,9 +307,10 @@ scriptflush(int signo)
 }
 
 void
-doshell(void)
+doshell(char *cmd)
 {
char *shell;
+   char *argp[] = {"sh", "-c", NULL, NULL};
 
shell = getenv("SHELL");
if (shell == NULL)
@@ -313,8 +319,15 @@ doshell(void)
(void)close(master);
(void)fclose(fscript);
login_tty(slave);
-   execl(shell, shell, "-i", (char *)NULL);
-   warn("%s", shell);
+
+   if (cmd != NULL) {
+   argp[2] = cmd;
+   execv(_PATH_BSHELL, argp);
+   warn("unable to execute %s", _PATH_BSHELL);
+   } else {
+   execl(shell, shell, "-i", (char *)NULL);
+   warn("%s", shell);
+   }
fail();
 }
 

-- 
>[<++>-]<+++.>+++[<-->-]<.>+++[<+
+++>-]<.>++[<>-]<+.--.[-]
 http://www.weirdnet.nl/