diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2019-05-14 13:44:26 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2019-05-14 13:44:26 +0000 |
commit | 5fcf1c6c496065dc3c89683ab37d49877c9cc6c4 (patch) | |
tree | 835ea5d510fa4b5ab2d798254b33401c7b22f624 | |
parent | 83614345fb635586e2f9d5f0fbbda18e4006dd2b (diff) |
allow specifying a cmdfile instead of trying to pipe in commands for -ef.
ok deraadt
-rw-r--r-- | usr.sbin/config/config.8 | 8 | ||||
-rw-r--r-- | usr.sbin/config/main.c | 11 | ||||
-rw-r--r-- | usr.sbin/config/misc.c | 20 | ||||
-rw-r--r-- | usr.sbin/config/misc.h | 3 | ||||
-rw-r--r-- | usr.sbin/config/ukcutil.c | 51 |
5 files changed, 79 insertions, 14 deletions
diff --git a/usr.sbin/config/config.8 b/usr.sbin/config/config.8 index d79619fc663..36d81526693 100644 --- a/usr.sbin/config/config.8 +++ b/usr.sbin/config/config.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: config.8,v 1.66 2018/04/25 12:01:11 jmc Exp $ +.\" $OpenBSD: config.8,v 1.67 2019/05/14 13:44:25 tedu Exp $ .\" $NetBSD: config.8,v 1.10 1996/08/31 20:58:16 mycroft Exp $ .\" .\" Copyright (c) 1980, 1991, 1993 @@ -30,7 +30,7 @@ .\" .\" from: @(#)config.8 8.2 (Berkeley) 4/19/94 .\" -.Dd $Mdocdate: April 25 2018 $ +.Dd $Mdocdate: May 14 2019 $ .Dt CONFIG 8 .Os .Sh NAME @@ -44,6 +44,7 @@ .Op Ar config-file .Nm config .Op Fl u +.Op Fl c Ar cmdfile .Op Fl f | o Ar outfile .Fl e .Ar infile @@ -103,6 +104,9 @@ directories above the build directory). .Pp For kernel modification, the options are as follows: .Bl -tag -width Ds +.It Fl c Ar cmdfile +Read commands from the specified file instead of the standard input. +Save and quit automatically when the end of file is reached. .It Fl e Allows the modification of kernel device configuration (see .Xr boot_config 8 ) . diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index a2b19656246..e7efacf871a 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.59 2017/06/22 15:57:16 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.60 2019/05/14 13:44:25 tedu Exp $ */ /* $NetBSD: main.c,v 1.22 1997/02/02 21:12:33 thorpej Exp $ */ /* @@ -82,7 +82,7 @@ usage(void) fprintf(stderr, "usage: %s [-p] [-b builddir] [-s srcdir] [config-file]\n" - " %s [-u] [-f | -o outfile] -e infile\n", + " %s [-u] [-c cmdfile] [-f | -o outfile] -e infile\n", __progname, __progname); exit(1); @@ -92,6 +92,7 @@ int pflag = 0; char *sflag = NULL; char *bflag = NULL; char *startdir; +char *cmdfile; int main(int argc, char *argv[]) @@ -105,9 +106,11 @@ main(int argc, char *argv[]) err(1, "pledge"); pflag = eflag = uflag = fflag = 0; - while ((ch = getopt(argc, argv, "epfb:s:o:u")) != -1) { + while ((ch = getopt(argc, argv, "c:epfb:s:o:u")) != -1) { switch (ch) { - + case 'c': + cmdfile = optarg; + break; case 'o': outfile = optarg; break; diff --git a/usr.sbin/config/misc.c b/usr.sbin/config/misc.c index 5972f4f9881..d309300057d 100644 --- a/usr.sbin/config/misc.c +++ b/usr.sbin/config/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.9 2011/10/02 22:20:49 edd Exp $ */ +/* $OpenBSD: misc.c,v 1.10 2019/05/14 13:44:25 tedu Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -38,13 +38,10 @@ extern int verbose; int -ask_cmd(cmd_t *cmd) +parse_cmd(cmd_t *cmd, char *lbuf) { - char lbuf[100], *cp, *buf; + char *cp, *buf; - /* Get input */ - if (fgets(lbuf, sizeof lbuf, stdin) == NULL) - errx(1, "eof"); lbuf[strcspn(lbuf, "\n")] = '\0'; if (verbose) printf("%s\n", lbuf); @@ -62,6 +59,17 @@ ask_cmd(cmd_t *cmd) } int +ask_cmd(cmd_t *cmd) +{ + char lbuf[100]; + + /* Get input */ + if (fgets(lbuf, sizeof lbuf, stdin) == NULL) + errx(1, "eof"); + return parse_cmd(cmd, lbuf); +} + +int ask_yn(const char *str) { int ch, first; diff --git a/usr.sbin/config/misc.h b/usr.sbin/config/misc.h index 691fe08b384..d9771fa174d 100644 --- a/usr.sbin/config/misc.h +++ b/usr.sbin/config/misc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.h,v 1.4 2003/06/03 00:52:35 weingart Exp $ */ +/* $OpenBSD: misc.h,v 1.5 2019/05/14 13:44:25 tedu Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -33,6 +33,7 @@ /* Prototypes */ int ask_cmd(cmd_t *); +int parse_cmd(cmd_t *, char *); int ask_yn(const char *); #endif /* _MISC_H */ diff --git a/usr.sbin/config/ukcutil.c b/usr.sbin/config/ukcutil.c index fa19c7a2301..19053115107 100644 --- a/usr.sbin/config/ukcutil.c +++ b/usr.sbin/config/ukcutil.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ukcutil.c,v 1.23 2017/09/27 15:14:52 deraadt Exp $ */ +/* $OpenBSD: ukcutil.c,v 1.24 2019/05/14 13:44:25 tedu Exp $ */ /* * Copyright (c) 1999-2001 Mats O Jansson. All rights reserved. @@ -29,6 +29,7 @@ #include <sys/device.h> #include <ctype.h> +#include <err.h> #include <errno.h> #include <limits.h> #include <nlist.h> @@ -1295,14 +1296,62 @@ add_history(int devno, short unit, short state, int newno) } int +config_fromfile(const char *cmdfile) { + FILE *fp; + cmd_t cmd; + int i; + + fp = fopen(cmdfile, "r"); + if (fp == NULL) + err(1, "open %s", cmdfile); + + /* Set up command table pointer */ + cmd.table = cmd_table; + + /* Edit cycle */ + do { + char lbuf[100]; + + /* Get input */ + if (fgets(lbuf, sizeof lbuf, fp) == NULL) + break; + parse_cmd(&cmd, lbuf); + + if (cmd.cmd[0] == '\0') + continue; + for (i = 0; cmd_table[i].cmd != NULL; i++) + if (strstr(cmd_table[i].cmd, cmd.cmd) == + cmd_table[i].cmd) + break; + + /* Check for valid command */ + if (cmd_table[i].cmd == NULL) { + printf("Invalid command '%s'\n", cmd.cmd); + exit(1); + } + strlcpy(cmd.cmd, cmd_table[i].cmd, sizeof cmd.cmd); + + /* Call function */ + cmd_table[i].fcn(&cmd); + + } while (1); + return 1; +} + +int config(void) { + extern char *cmdfile; cmd_t cmd; int i, st; /* Set up command table pointer */ cmd.table = cmd_table; + if (cmdfile != NULL) { + return config_fromfile(cmdfile); + } + printf("Enter 'help' for information\n"); /* Edit cycle */ |