commit cccdab7ea2bb922413f02698ecc5969c00e7a7aa
parent 9c7ee8473a71303ab4cb51c9593089c07c224a22
Author: Toby Goodwin <toby@paccrat.org>
Date: Sat, 17 Mar 2018 15:14:21 +0000
tidy up, test cases, document flag
Diffstat:
5 files changed, 50 insertions(+), 9 deletions(-)
diff --git a/builtins.c b/builtins.c
@@ -229,8 +229,7 @@ static void b_flag(char **av) {
if (mode != 2) goto flag_immutable;
flagp = &dashdee; break;
case 'e': flagp = &dashee; break;
- case 'I': if (mode != 2) mode = !mode; /* fallthrough */
- case 'i': flagp = &dasheye; break;
+ case 'i': flagp = &interactive; break;
case 'l':
if (mode != 2) goto flag_immutable;
flagp = &dashell; break;
diff --git a/main.c b/main.c
@@ -6,9 +6,10 @@
#include "input.h"
-bool dashdee, dashee, dashvee, dashex, dasheye,
- dashen, dashpee, interactive;
-bool dashEYE, dashell, dashoh, dashess;
+bool dashdee, dashee, dasheye, dashell, dashen;
+bool dashpee, dashoh, dashess, dashvee, dashex;
+bool interactive;
+static bool dashEYE;
char *dashsee[2];
pid_t rc_pid;
@@ -68,8 +69,10 @@ extern int main(int argc, char *argv[], char *envp[]) {
}
quitopts:
argv += rc_optind;
- /* use isatty() iff neither -i nor -I is set, and iff the input is not from a script or -c flags */
- if (!dasheye && !dashEYE && dashsee[0] == NULL && (dashess || *argv == NULL))
+ /* use isatty() iff neither -i nor -I is set, and iff the input is not
+ * from a script or -c flags */
+ if (!dasheye && !dashEYE && dashsee[0] == NULL &&
+ (dashess || *argv == NULL))
interactive = isatty(0);
if (!dashoh) {
checkfd(0, rFrom);
diff --git a/rc.1 b/rc.1
@@ -1706,6 +1706,36 @@ If no argument is given, the current value of
.Cr $status
is used.
.TP
+\fBflag\fR \fIf\fR [ \fB+\fR | \fB\-\fR ]
+Test, set (\fB+\fR), or reset (\fB\-\fR) command-line flag
+.IR f .
+For example, a script that requires ``exit if command fails'' semantics can say
+.Ds
+.Cr "flag e +"
+.De
+.TP
+\&
+Some flags cannot be set or reset using
+.BR flag ,
+but they can still be tested. These are
+.BR c ,
+.BR d ,
+.BR l ,
+.BR o ,
+.BR p ,
+and
+.BR s .
+As a special case,
+.Cr "flag i"
+operates on
+.BR rc 's
+internal
+.IR interactive
+flag, which may have been set by
+.Cr -i
+on the command line, or if standard input was a terminal; there is no
+.Cr "flag I" .
+.TP
\fBlimit \fR[\fB\-h\fR] [\fIresource \fR[\fIvalue\fR]]
Similar to the
.IR csh (1)
diff --git a/rc.h b/rc.h
@@ -170,8 +170,9 @@ enum {
/* main.c */
extern Rq *redirq;
-extern bool dashdee, dashee, dashvee, dashex, dasheye, dashell, dashen;
-extern bool dashoh, dashpee, dashess, interactive;
+extern bool dashdee, dashee, dasheye, dashell, dashen;
+extern bool dashpee, dashoh, dashess, dashvee, dashex;
+extern bool interactive;
extern char *dashsee[];
extern pid_t rc_pid;
extern int lineno;
diff --git a/trip.rc b/trip.rc
@@ -624,3 +624,11 @@ $rc -ec 'X=`{false}'
if (~ $status 0) {
fail '"rc -e" exits with zero status when backquote command returns non-zero'
}
+
+# exercise flag builtin
+submatch 'flag' 'rc: not enough arguments to flag' 'flag no args'
+submatch 'flag a b c' 'usage: flag f [ + | - ]' 'flag 3 args'
+submatch 'flag xx' 'usage: flag f [ + | - ]' 'flag wrong first arg'
+submatch 'flag x x' 'usage: flag f [ + | - ]' 'flag wrong second arg'
+submatch 'flag c && echo yes' yes 'flag c'
+submatch 'flag x +; flag x -' 'flag x -' 'setting x flag'