Module Name:    src
Committed By:   kre
Date:           Mon Oct 14 08:27:54 UTC 2024

Modified Files:
        src/bin/sh: option.list options.c sh.1

Log Message:
Add a -r (version) option to sh

This new -r (or +r) option is for command line use only.
When encountered, the shell simply prints its version info
(such as it has, which isn't much) and exits (immediately).

This allows "funny" uses like
        sh -version
the -v and -e options are standard, and processed as normal
(changing nothing, yet, except setting the option).  Then the
'r' option is seen, the version info is printed, and the shell
exits.  Any remaining "options" (there is no "-o n" option) are
ignored, as are any other args given to the shell.

The string printed (currently) is just "NetBSD shell:" followed
by the value of the NETBSD_SHELL variable (which has been established
already by the time options are processed).


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/bin/sh/option.list
cvs rdiff -u -r1.60 -r1.61 src/bin/sh/options.c
cvs rdiff -u -r1.266 -r1.267 src/bin/sh/sh.1

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/option.list
diff -u src/bin/sh/option.list:1.10 src/bin/sh/option.list:1.11
--- src/bin/sh/option.list:1.10	Sun Sep 18 06:03:19 2022
+++ src/bin/sh/option.list	Mon Oct 14 08:27:53 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: option.list,v 1.10 2022/09/18 06:03:19 kre Exp $ */
+/* $NetBSD: option.list,v 1.11 2024/10/14 08:27:53 kre Exp $ */
 
 /*
  * define the shell's settable options
@@ -54,6 +54,7 @@ hflag	trackall	h		# [U] locate cmds in f
 // 's' is standard for command line, not as 'set' option, nor 'stdin' name
 sflag	stdin		s		# read from standard input
 // minusc		c		# command line option only.
+// --			r		# command line option only.
 // --			o		# handled differently...
 
 // non-standard options -- 'i' is just a state, not an option in standard.

Index: src/bin/sh/options.c
diff -u src/bin/sh/options.c:1.60 src/bin/sh/options.c:1.61
--- src/bin/sh/options.c:1.60	Sat Sep 21 20:48:50 2024
+++ src/bin/sh/options.c	Mon Oct 14 08:27:53 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: options.c,v 1.60 2024/09/21 20:48:50 kre Exp $	*/
+/*	$NetBSD: options.c,v 1.61 2024/10/14 08:27:53 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: options.c,v 1.60 2024/09/21 20:48:50 kre Exp $");
+__RCSID("$NetBSD: options.c,v 1.61 2024/10/14 08:27:53 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -244,6 +244,9 @@ options(int cmdline)
 				else
 					set_debug("*$", val);
 #endif
+			} else if (cmdline && c == 'r') {
+				out1fmt("NetBSD shell: %s\n", lookupvar("NETBSD_SHELL"));
+				sh_exit(0);
 			} else {
 				setoption(c, val);
 			}

Index: src/bin/sh/sh.1
diff -u src/bin/sh/sh.1:1.266 src/bin/sh/sh.1:1.267
--- src/bin/sh/sh.1:1.266	Fri Oct 11 09:02:10 2024
+++ src/bin/sh/sh.1	Mon Oct 14 08:27:53 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sh.1,v 1.266 2024/10/11 09:02:10 kre Exp $
+.\"	$NetBSD: sh.1,v 1.267 2024/10/14 08:27:53 kre Exp $
 .\" Copyright (c) 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
 .\"
@@ -31,10 +31,10 @@
 .\"
 .\"	@(#)sh.1	8.6 (Berkeley) 5/4/95
 .\"
-.Dd October 11, 2024
+.Dd October 14, 2024
 .Dt SH 1
 .\" everything except c o and s (keep them ordered)
-.ds flags abCEeFfhIiLlmnpquVvXx
+.ds flags abCEeFfhIiLlmnpqruVvXx
 .Os
 .Sh NAME
 .Nm sh
@@ -108,6 +108,28 @@ That is, commands
 can be typed directly to the running shell or can be put into a file and
 the file can be executed directly by the shell.
 .Ss Invocation
+If the
+.Fl r
+option is present on the command line,
+alone, or following other (valid) options,
+the
+.Nm
+immediately prints its version information to standard output
+and exits.
+This allows invocations like
+.Dl Nm Fl version
+as the (preceding)
+.Fl v
+and
+.Fl e
+are valid options, then when the
+.Fl r
+is encountered, the version information is printed, and
+no more processing is attempted.
+Whether the remaining (apparent) options after the
+.Fl r
+would have been valid or not is immaterial.
+.Pp
 If no arguments are present and if the standard input,
 and standard error output, of the shell
 are connected to a terminal (or terminals, or if the
@@ -476,6 +498,18 @@ these being
 and the file specified by the
 .Ev ENV
 environment variable.
+.It Fl r
+Print
+.Nm
+version information and exit.
+It is immaterial whether the option follows
+.Fl
+or
+.Cm + .
+See
+.Sx Invocation
+above.
+This option is valid only on the command line.
 .It Fl s Em stdin
 Read commands from standard input (set automatically if
 neither

Reply via email to