From a9bbb35dbfb434a89c38c403e31cf2fae9de56f9 Mon Sep 17 00:00:00 2001 From: jcs Date: Sat, 20 Nov 2021 03:13:37 +0000 Subject: [PATCH] When cmdfile is specified, use lines from it for all input, not just commands. This allows complex actions like changing device parameters to be done with a command file. --- usr.sbin/config/config.8 | 7 ++-- usr.sbin/config/main.c | 10 ++++-- usr.sbin/config/misc.c | 22 ++++++++---- usr.sbin/config/ukcutil.c | 70 ++++++++++----------------------------- 4 files changed, 46 insertions(+), 63 deletions(-) diff --git a/usr.sbin/config/config.8 b/usr.sbin/config/config.8 index ab48c1314ed..d098faa3242 100644 --- a/usr.sbin/config/config.8 +++ b/usr.sbin/config/config.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: config.8,v 1.74 2021/11/02 22:26:46 kn Exp $ +.\" $OpenBSD: config.8,v 1.75 2021/11/20 03:13:37 jcs 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: November 2 2021 $ +.Dd $Mdocdate: November 20 2021 $ .Dt CONFIG 8 .Os .Sh NAME @@ -110,7 +110,8 @@ directories above the build directory). 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. +Read commands and answers 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 diff --git a/usr.sbin/config/main.c b/usr.sbin/config/main.c index d00bfded2b0..cde1006efbe 100644 --- a/usr.sbin/config/main.c +++ b/usr.sbin/config/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.62 2021/01/26 18:23:49 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.63 2021/11/20 03:13:37 jcs Exp $ */ /* $NetBSD: main.c,v 1.22 1997/02/02 21:12:33 thorpej Exp $ */ /* @@ -131,7 +131,8 @@ int pflag = 0; char *sflag = NULL; char *bflag = NULL; char *startdir; -char *cmdfile; +char *cmdfile = NULL; +FILE *cmdfp = NULL; int main(int argc, char *argv[]) @@ -211,6 +212,11 @@ main(int argc, char *argv[]) #ifdef MAKE_BOOTSTRAP errx(1, "UKC not available in this binary"); #else + if (cmdfile != NULL) { + cmdfp = fopen(cmdfile, "r"); + if (cmdfp == NULL) + err(1, "open %s", cmdfile); + } return (ukc(argv[0], outfile, uflag, fflag)); #endif } diff --git a/usr.sbin/config/misc.c b/usr.sbin/config/misc.c index d309300057d..840662e03e3 100644 --- a/usr.sbin/config/misc.c +++ b/usr.sbin/config/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.10 2019/05/14 13:44:25 tedu Exp $ */ +/* $OpenBSD: misc.c,v 1.11 2021/11/20 03:13:37 jcs Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -62,32 +62,42 @@ int ask_cmd(cmd_t *cmd) { char lbuf[100]; + extern FILE *cmdfp; /* Get input */ - if (fgets(lbuf, sizeof lbuf, stdin) == NULL) + if (fgets(lbuf, sizeof lbuf, cmdfp ? cmdfp : stdin) == NULL) { + if (cmdfp) { + cmd->cmd[0] = '\0'; + return -1; + } errx(1, "eof"); + } + if (cmdfp) + printf("%s", lbuf); return parse_cmd(cmd, lbuf); } int ask_yn(const char *str) { + extern FILE *cmdfp; int ch, first; printf("%s [n] ", str); fflush(stdout); - first = ch = getchar(); - if (verbose) { + first = ch = getc(cmdfp ? cmdfp : stdin); + if (verbose || (cmdfp && ch != EOF)) { printf("%c", ch); fflush(stdout); } while (ch != '\n' && ch != EOF) { - ch = getchar(); + ch = getc(cmdfp ? cmdfp : stdin); if (verbose) { printf("%c\n", ch); fflush(stdout); - } + } else if (cmdfp) + putchar('\n'); } if (ch == EOF || first == EOF) errx(1, "eof"); diff --git a/usr.sbin/config/ukcutil.c b/usr.sbin/config/ukcutil.c index be117c6bd84..90cfb8a784c 100644 --- a/usr.sbin/config/ukcutil.c +++ b/usr.sbin/config/ukcutil.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ukcutil.c,v 1.25 2019/09/06 21:30:31 cheloha Exp $ */ +/* $OpenBSD: ukcutil.c,v 1.26 2021/11/20 03:13:37 jcs Exp $ */ /* * Copyright (c) 1999-2001 Mats O Jansson. All rights reserved. @@ -1294,49 +1294,6 @@ add_history(int devno, short unit, short state, int newno) pdev(val); } -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) { @@ -1347,21 +1304,26 @@ config(void) /* Set up command table pointer */ cmd.table = cmd_table; - if (cmdfile != NULL) { - return config_fromfile(cmdfile); - } - - printf("Enter 'help' for information\n"); + if (cmdfile == NULL) + printf("Enter 'help' for information\n"); /* Edit cycle */ do { + char lbuf[100]; + again: - printf("ukc> "); - fflush(stdout); + /* Get input */ + if (cmdfile == NULL) { + printf("ukc> "); + fflush(stdout); + } ask_cmd(&cmd); - if (cmd.cmd[0] == '\0') + if (cmd.cmd[0] == '\0') { + if (cmdfile != NULL) + return 1; goto again; + } for (i = 0; cmd_table[i].cmd != NULL; i++) if (strstr(cmd_table[i].cmd, cmd.cmd) == cmd_table[i].cmd) @@ -1373,6 +1335,10 @@ again: /* Check for valid command */ if (cmd_table[i].cmd == NULL) { + if (cmdfile == NULL) { + printf("Invalid command '%s'\n", cmd.cmd); + exit(1); + } printf("Invalid command '%s'. Try 'help'.\n", cmd.cmd); continue; } else -- 2.20.1