diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-05-03 21:00:01 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-05-03 21:00:01 -0700 |
commit | 0d7b3425f8eb4c7754d8a18c438af0541fa9ba91 (patch) | |
tree | e3e39e1d1c8461b1f4965ec200234ead22221d4a | |
parent | 81e976380e571f1df8c5a4bdc5f4387c9a684260 (diff) | |
download | pw-0d7b3425f8eb4c7754d8a18c438af0541fa9ba91.tar.gz pw-0d7b3425f8eb4c7754d8a18c438af0541fa9ba91.tar.bz2 pw-0d7b3425f8eb4c7754d8a18c438af0541fa9ba91.zip |
Implement quit count safeguard.
-rw-r--r-- | pw.1 | 17 | ||||
-rw-r--r-- | pw.c | 23 |
2 files changed, 37 insertions, 3 deletions
@@ -148,7 +148,10 @@ command. The commands are: .IP "\fBq\fP, \fBCtrl-C\fP" -Quit the program. +Quit the program. Must be used multiple times in succession if a quit +count greater than 1 is in effect. (See the +.B -q +option). .IP "\fBl\fP, \fILeft Arrow\fP" Scroll the display to the left. @@ -571,6 +574,18 @@ and staying in the interactive mode. This is useful when the last portion of the input is of interest, and contains long lines that require horizontal scrolling. +.IP "\fB-q\fP \fIinteger\fP" +Install a quit count specified by +.I integer +which must be positive. The quit count determines how many times the +.B q +or +.B Ctrl-C +commands have to be issued in succession before +.I pw +will quit. This is a safeguard against quitting accidentally. The default +is 1: the commands quit immediately. + .IP \fB-E\fP Enable extended regular expressions (EREs). By default, regular expressions processed by @@ -243,6 +243,7 @@ static void usage(void) "-l realnum long update interval (s)\n" "-n integer display size (# of lines)\n" "-d do not quit on end-of-input\n" + "-q integer Require this many repetitions of q to quit\n" "-E treat regular expressions as extended\n" "-B treat regular expressions as basic (default)\n\n" "-g [!]pattern add pattern to grep stack; ! inverts.\n\n" @@ -702,6 +703,7 @@ int main(int argc, char **argv) kbd_colon, kbd_result, kbd_trig }; int auto_quit = 1; + int quit_count = 1, quit_countdown = quit_count; int exit_status = EXIT_FAILURE; char cmdbuf[cmdsize], *curcmd = 0, *savedcmd = 0; #ifdef SIGWINCH @@ -726,7 +728,7 @@ int main(int argc, char **argv) } } - while ((opt = getopt(argc, argv, "n:i:l:dEBg:")) != -1) { + while ((opt = getopt(argc, argv, "n:i:l:dEBg:q:")) != -1) { switch (opt) { case 'n': { @@ -756,6 +758,15 @@ int main(int argc, char **argv) case 'd': auto_quit = 0; break; + case 'q': + { + char *err; + if ((quit_countdown = quit_count = getzp(optarg, &err)) < 0) { + error("-%c option: %s\n", opt, err); + return EXIT_FAILURE; + } + break; + } case 'E': regex_flags = REG_EXTENDED; break; @@ -1044,9 +1055,17 @@ int main(int argc, char **argv) curcmd = 0; // fallthrough case kbd_cmd: + if (ch != 'q' && ch != 3) + quit_countdown = quit_count; switch (ch) { case 'q': case 3: - kbd_state = kbd_exit; + if (--quit_countdown == 0) { + kbd_state = kbd_exit; + } else { + sprintf(cmdbuf, "%d more to quit", quit_countdown); + curcmd = cmdbuf; + kbd_state = kbd_result; + } break; case 'h': if (hpos >= 8) { |