diff options
author | 2021-03-20 09:00:49 +0000 | |
---|---|---|
committer | 2021-03-20 09:00:49 +0000 | |
commit | c65492eb769f60fbd74bdb04ac96ba44d33263df (patch) | |
tree | 54f966c12861f12d2a22438d061cfda6ea0564a0 | |
parent | Add new test-tls13-multiple-ccs-messages.py (diff) | |
download | wireguard-openbsd-c65492eb769f60fbd74bdb04ac96ba44d33263df.tar.xz wireguard-openbsd-c65492eb769f60fbd74bdb04ac96ba44d33263df.zip |
Add a 'batch' mode to mg via the '-b' command line option which will
initialise a pty, run the specified file of mg commands and then exit.
This is to facilitate mg fitting into the OpenBSD regress test
framework and be able to run via a cron job.
-rw-r--r-- | usr.bin/mg/def.h | 3 | ||||
-rw-r--r-- | usr.bin/mg/main.c | 56 | ||||
-rw-r--r-- | usr.bin/mg/mg.1 | 11 | ||||
-rw-r--r-- | usr.bin/mg/tty.c | 11 | ||||
-rw-r--r-- | usr.bin/mg/ttyio.c | 4 |
5 files changed, 74 insertions, 11 deletions
diff --git a/usr.bin/mg/def.h b/usr.bin/mg/def.h index e3cc9e7cfe4..7b7d90088ec 100644 --- a/usr.bin/mg/def.h +++ b/usr.bin/mg/def.h @@ -1,4 +1,4 @@ -/* $OpenBSD: def.h,v 1.168 2021/03/01 10:51:14 lum Exp $ */ +/* $OpenBSD: def.h,v 1.169 2021/03/20 09:00:49 lum Exp $ */ /* This file is in the public domain. */ @@ -750,6 +750,7 @@ extern int doaudiblebell; extern int dovisiblebell; extern int dblspace; extern int allbro; +extern int batch; extern char cinfo[]; extern char *keystrings[]; extern char pat[NPAT]; diff --git a/usr.bin/mg/main.c b/usr.bin/mg/main.c index d0d94e365ad..ac2175adb28 100644 --- a/usr.bin/mg/main.c +++ b/usr.bin/mg/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.88 2021/02/23 08:10:51 lum Exp $ */ +/* $OpenBSD: main.c,v 1.89 2021/03/20 09:00:49 lum Exp $ */ /* This file is in the public domain. */ @@ -14,7 +14,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <termios.h> #include <unistd.h> +#include <util.h> #include "def.h" #include "kbd.h" @@ -33,6 +35,7 @@ int doaudiblebell; /* audible bell toggle */ int dovisiblebell; /* visible bell toggle */ int dblspace; /* sentence end #spaces */ int allbro; /* all buffs read-only */ +int batch; /* for regress tests */ struct buffer *curbp; /* current buffer */ struct buffer *bheadp; /* BUFFER list head */ struct mgwin *curwp; /* current window */ @@ -40,6 +43,7 @@ struct mgwin *wheadp; /* MGWIN listhead */ char pat[NPAT]; /* pattern */ static void edinit(struct buffer *); +static void pty_init(void); static __dead void usage(void); extern char *__progname; @@ -48,8 +52,8 @@ extern void closetags(void); static __dead void usage() { - fprintf(stderr, "usage: %s [-nR] [-f mode] [-u file] [+number] " - "[file ...]\n", + fprintf(stderr, "usage: %s [-nR] [-b file] [-f mode] [-u file] " + "[+number] [file ...]\n", __progname); exit(1); } @@ -58,6 +62,7 @@ int main(int argc, char **argv) { char *cp, *conffile = NULL, *init_fcn_name = NULL; + char *batchfile = NULL; PF init_fcn = NULL; int o, i, nfiles; int nobackups = 0; @@ -67,8 +72,12 @@ main(int argc, char **argv) NULL) == -1) err(1, "pledge"); - while ((o = getopt(argc, argv, "nRf:u:")) != -1) + while ((o = getopt(argc, argv, "nRb:f:u:")) != -1) switch (o) { + case 'b': + batch = 1; + batchfile = optarg; + break; case 'R': allbro = 1; break; @@ -87,6 +96,22 @@ main(int argc, char **argv) default: usage(); } + + if (batch && (conffile != NULL)) { + fprintf(stderr, "%s: -b and -u are mutually exclusive.\n", + __progname); + exit(1); + } + if (batch) { + pty_init(); + conffile = batchfile; + } + if (conffile != NULL && access(conffile, R_OK) != 0) { + fprintf(stderr, "%s: Problem with file: %s\n", __progname, + conffile); + exit(1); + } + argc -= optind; argv += optind; @@ -136,6 +161,9 @@ main(int argc, char **argv) if ((cp = startupfile(NULL, conffile)) != NULL) (void)load(cp); + if (batch) + return (0); + /* * Now ensure any default buffer modes from the startup file are * given to any files opened when parsing the startup file. @@ -252,6 +280,26 @@ edinit(struct buffer *bp) } /* + * Create pty for batch mode. + */ +static void +pty_init(void) +{ + struct winsize ws; + int master; + int slave; + + memset(&ws, 0, sizeof(ws)); + ws.ws_col = 80, + ws.ws_row = 24; + + openpty(&master, &slave, NULL, NULL, &ws); + login_tty(slave); + + return; +} + +/* * Quit command. If an argument, always quit. Otherwise confirm if a buffer * has been changed and not written out. Normally bound to "C-x C-c". */ diff --git a/usr.bin/mg/mg.1 b/usr.bin/mg/mg.1 index e5b4c4a7af6..29066df6ea7 100644 --- a/usr.bin/mg/mg.1 +++ b/usr.bin/mg/mg.1 @@ -1,7 +1,7 @@ -.\" $OpenBSD: mg.1,v 1.120 2021/02/23 18:45:33 lum Exp $ +.\" $OpenBSD: mg.1,v 1.121 2021/03/20 09:00:49 lum Exp $ .\" This file is in the public domain. .\" -.Dd $Mdocdate: February 23 2021 $ +.Dd $Mdocdate: March 20 2021 $ .Dt MG 1 .Os .Sh NAME @@ -10,6 +10,7 @@ .Sh SYNOPSIS .Nm mg .Op Fl nR +.Op Fl b Ar file .Op Fl f Ar mode .Op Fl u Ar file .Op + Ns Ar number @@ -35,6 +36,12 @@ sign and the number). If a negative number is specified, the line number counts backwards from the end of the file i.e. +-1 will be the last line of the file, +-2 will be second last, and so on. +.It Fl b Ar file +Turn on batch mode and execute the +.Nm +commands found in the specified +.Ar file +and then terminate. .It Fl f Ar mode Run the mode command for all buffers created from arguments on the command line, including the diff --git a/usr.bin/mg/tty.c b/usr.bin/mg/tty.c index 75237af2475..6522a240232 100644 --- a/usr.bin/mg/tty.c +++ b/usr.bin/mg/tty.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty.c,v 1.38 2021/03/01 10:51:14 lum Exp $ */ +/* $OpenBSD: tty.c,v 1.39 2021/03/20 09:00:49 lum Exp $ */ /* This file is in the public domain. */ @@ -34,6 +34,7 @@ #include <signal.h> #include <stdio.h> #include <term.h> +#include <unistd.h> #include "def.h" @@ -64,9 +65,15 @@ winchhandler(int sig) void ttinit(void) { + char *tty; int errret; - if (setupterm(NULL, 1, &errret)) + if (batch == 1) + tty = "pty"; + else + tty = NULL; + + if (setupterm(tty, STDOUT_FILENO, &errret)) panic("Terminal setup failed"); signal(SIGWINCH, winchhandler); diff --git a/usr.bin/mg/ttyio.c b/usr.bin/mg/ttyio.c index 57c03e40f0f..54ddd8efe15 100644 --- a/usr.bin/mg/ttyio.c +++ b/usr.bin/mg/ttyio.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ttyio.c,v 1.39 2021/03/01 10:51:14 lum Exp $ */ +/* $OpenBSD: ttyio.c,v 1.40 2021/03/20 09:00:49 lum Exp $ */ /* This file is in the public domain. */ @@ -140,7 +140,7 @@ ttflush(void) ssize_t written; char *buf = obuf; - if (nobuf == 0) + if (nobuf == 0 || batch == 1) return; while ((written = write(fileno(stdout), buf, nobuf)) != nobuf) { |