cmdfile for config -ef

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

cmdfile for config -ef

Ted Unangst-6
This adds a new option to config, -c cmdfile, that allows reading commands
from a file instead of stdin. This makes it easier to script.


Index: config.8
===================================================================
RCS file: /home/cvs/src/usr.sbin/config/config.8,v
retrieving revision 1.66
diff -u -p -r1.66 config.8
--- config.8 25 Apr 2018 12:01:11 -0000 1.66
+++ config.8 11 May 2019 15:16:45 -0000
@@ -43,6 +43,7 @@
 .Op Fl s Ar srcdir
 .Op Ar config-file
 .Nm config
+.Op Fl c Ar cmdfile
 .Op Fl u
 .Op Fl f | o Ar outfile
 .Fl e
@@ -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 ) .
Index: main.c
===================================================================
RCS file: /home/cvs/src/usr.sbin/config/main.c,v
retrieving revision 1.59
diff -u -p -r1.59 main.c
--- main.c 22 Jun 2017 15:57:16 -0000 1.59
+++ main.c 11 May 2019 15:00:34 -0000
@@ -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 [-c cmdfile] [-u] [-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;
Index: misc.c
===================================================================
RCS file: /home/cvs/src/usr.sbin/config/misc.c,v
retrieving revision 1.9
diff -u -p -r1.9 misc.c
--- misc.c 2 Oct 2011 22:20:49 -0000 1.9
+++ misc.c 11 May 2019 15:08:56 -0000
@@ -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);
@@ -59,6 +56,17 @@ ask_cmd(cmd_t *cmd)
  strlcpy(cmd->args, buf, sizeof cmd->args);
 
  return (0);
+}
+
+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
Index: misc.h
===================================================================
RCS file: /home/cvs/src/usr.sbin/config/misc.h,v
retrieving revision 1.4
diff -u -p -r1.4 misc.h
--- misc.h 3 Jun 2003 00:52:35 -0000 1.4
+++ misc.h 11 May 2019 15:10:04 -0000
@@ -33,6 +33,7 @@
 
 /* Prototypes */
 int ask_cmd(cmd_t *);
+int parse_cmd(cmd_t *, char *);
 int ask_yn(const char *);
 
 #endif /* _MISC_H */
Index: ukcutil.c
===================================================================
RCS file: /home/cvs/src/usr.sbin/config/ukcutil.c,v
retrieving revision 1.23
diff -u -p -r1.23 ukcutil.c
--- ukcutil.c 27 Sep 2017 15:14:52 -0000 1.23
+++ ukcutil.c 11 May 2019 15:14:25 -0000
@@ -29,6 +29,7 @@
 #include <sys/device.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <limits.h>
 #include <nlist.h>
@@ -1295,13 +1296,61 @@ add_history(int devno, short unit, short
 }
 
 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");
 

Reply | Threaded
Open this post in threaded view
|

Re: cmdfile for config -ef

Benjamin Baier
On Sat, 11 May 2019 11:18:02 -0400
"Ted Unangst" <[hidden email]> wrote:

> This adds a new option to config, -c cmdfile, that allows reading commands
> from a file instead of stdin. This makes it easier to script.

Interesting. Can the cmdfile be /bsd ?
So something like config -u but without the need of /dev/mem ?


> Index: config.8
> ===================================================================
> RCS file: /home/cvs/src/usr.sbin/config/config.8,v
> retrieving revision 1.66
> diff -u -p -r1.66 config.8
> --- config.8 25 Apr 2018 12:01:11 -0000 1.66
> +++ config.8 11 May 2019 15:16:45 -0000
> @@ -43,6 +43,7 @@
>  .Op Fl s Ar srcdir
>  .Op Ar config-file
>  .Nm config
> +.Op Fl c Ar cmdfile
>  .Op Fl u
>  .Op Fl f | o Ar outfile
>  .Fl e
> @@ -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 ) .
> Index: main.c
> ===================================================================
> RCS file: /home/cvs/src/usr.sbin/config/main.c,v
> retrieving revision 1.59
> diff -u -p -r1.59 main.c
> --- main.c 22 Jun 2017 15:57:16 -0000 1.59
> +++ main.c 11 May 2019 15:00:34 -0000
> @@ -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 [-c cmdfile] [-u] [-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;
> Index: misc.c
> ===================================================================
> RCS file: /home/cvs/src/usr.sbin/config/misc.c,v
> retrieving revision 1.9
> diff -u -p -r1.9 misc.c
> --- misc.c 2 Oct 2011 22:20:49 -0000 1.9
> +++ misc.c 11 May 2019 15:08:56 -0000
> @@ -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);
> @@ -59,6 +56,17 @@ ask_cmd(cmd_t *cmd)
>   strlcpy(cmd->args, buf, sizeof cmd->args);
>  
>   return (0);
> +}
> +
> +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
> Index: misc.h
> ===================================================================
> RCS file: /home/cvs/src/usr.sbin/config/misc.h,v
> retrieving revision 1.4
> diff -u -p -r1.4 misc.h
> --- misc.h 3 Jun 2003 00:52:35 -0000 1.4
> +++ misc.h 11 May 2019 15:10:04 -0000
> @@ -33,6 +33,7 @@
>  
>  /* Prototypes */
>  int ask_cmd(cmd_t *);
> +int parse_cmd(cmd_t *, char *);
>  int ask_yn(const char *);
>  
>  #endif /* _MISC_H */
> Index: ukcutil.c
> ===================================================================
> RCS file: /home/cvs/src/usr.sbin/config/ukcutil.c,v
> retrieving revision 1.23
> diff -u -p -r1.23 ukcutil.c
> --- ukcutil.c 27 Sep 2017 15:14:52 -0000 1.23
> +++ ukcutil.c 11 May 2019 15:14:25 -0000
> @@ -29,6 +29,7 @@
>  #include <sys/device.h>
>  
>  #include <ctype.h>
> +#include <err.h>
>  #include <errno.h>
>  #include <limits.h>
>  #include <nlist.h>
> @@ -1295,13 +1296,61 @@ add_history(int devno, short unit, short
>  }
>  
>  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");
>  
>

Reply | Threaded
Open this post in threaded view
|

Re: cmdfile for config -ef

Ted Unangst-6
Benjamin Baier wrote:
> On Sat, 11 May 2019 11:18:02 -0400
> "Ted Unangst" <[hidden email]> wrote:
>
> > This adds a new option to config, -c cmdfile, that allows reading commands
> > from a file instead of stdin. This makes it easier to script.
>
> Interesting. Can the cmdfile be /bsd ?
> So something like config -u but without the need of /dev/mem ?

No, the intended use case is not to copy options between kernels, but to apply
a consistent set of changes after linking.

Reply | Threaded
Open this post in threaded view
|

Re: cmdfile for config -ef

Theo de Raadt-2
Ted Unangst <[hidden email]> wrote:

> Benjamin Baier wrote:
> > On Sat, 11 May 2019 11:18:02 -0400
> > "Ted Unangst" <[hidden email]> wrote:
> >
> > > This adds a new option to config, -c cmdfile, that allows reading commands
> > > from a file instead of stdin. This makes it easier to script.
> >
> > Interesting. Can the cmdfile be /bsd ?
> > So something like config -u but without the need of /dev/mem ?
>
> No, the intended use case is not to copy options between kernels, but to apply
> a consistent set of changes after linking.

@ boot-time, /etc/rc kernel relink script
        KARL links a new kernel
        apply config -e changes
        install
        hash it

Reply | Threaded
Open this post in threaded view
|

Re: cmdfile for config -ef

Benjamin Baier
On Sat, 11 May 2019 13:26:14 -0600
"Theo de Raadt" <[hidden email]> wrote:

> Ted Unangst <[hidden email]> wrote:
>
> > Benjamin Baier wrote:
> > > On Sat, 11 May 2019 11:18:02 -0400
> > > "Ted Unangst" <[hidden email]> wrote:
> > >
> > > > This adds a new option to config, -c cmdfile, that allows reading commands
> > > > from a file instead of stdin. This makes it easier to script.
> > >
> > > Interesting. Can the cmdfile be /bsd ?
> > > So something like config -u but without the need of /dev/mem ?
> >
> > No, the intended use case is not to copy options between kernels, but to apply
> > a consistent set of changes after linking.
>
> @ boot-time, /etc/rc kernel relink script
> KARL links a new kernel
> apply config -e changes
> install
> hash it

I do realize that it's a polished version of what I wrote yesterday.
https://marc.info/?l=openbsd-misc&m=155751021025538&w=2
It just made me think that it may be possible without yet another config file.

Just thinking out loud, do carry on...