git: 9d406e088e95 - main - dnctl: Support reading config from file like ipfw(8)

From: Kristof Provost <kp_at_FreeBSD.org>
Date: Mon, 03 Jan 2022 12:55:59 UTC
The branch main has been updated by kp:

URL: https://cgit.FreeBSD.org/src/commit/?id=9d406e088e95f4245dbeb2b391867621a523401d

commit 9d406e088e95f4245dbeb2b391867621a523401d
Author:     Kristof Provost <kp@FreeBSD.org>
AuthorDate: 2021-12-23 14:15:44 +0000
Commit:     Kristof Provost <kp@FreeBSD.org>
CommitDate: 2022-01-03 08:50:18 +0000

    dnctl: Support reading config from file like ipfw(8)
    
    Extend the dnctl (dummynet config) tool to be able to read commands from
    a file, just like ipfw already does.
    
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D33627
---
 sbin/ipfw/main.c | 121 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 67 insertions(+), 54 deletions(-)

diff --git a/sbin/ipfw/main.c b/sbin/ipfw/main.c
index f7aa6af5369c..1b9c7f324a9c 100644
--- a/sbin/ipfw/main.c
+++ b/sbin/ipfw/main.c
@@ -519,62 +519,79 @@ ipfw_readfile(int ac, char *av[])
 	FILE	*f = NULL;
 	pid_t	preproc = 0;
 
-	while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
-		switch(c) {
-		case 'c':
-			g_co.do_compact = 1;
-			break;
+	if (is_ipfw()) {
+		while ((c = getopt(ac, av, "cfNnp:qS")) != -1) {
+			switch(c) {
+			case 'c':
+				g_co.do_compact = 1;
+				break;
+
+			case 'f':
+				g_co.do_force = 1;
+				break;
 
-		case 'f':
-			g_co.do_force = 1;
-			break;
+			case 'N':
+				g_co.do_resolv = 1;
+				break;
 
-		case 'N':
-			g_co.do_resolv = 1;
-			break;
+			case 'n':
+				g_co.test_only = 1;
+				break;
 
-		case 'n':
-			g_co.test_only = 1;
-			break;
+			case 'p':
+				/*
+				 * ipfw -p cmd [args] filename
+				 *
+				 * We are done with getopt(). All arguments
+				 * except the filename go to the preprocessor,
+				 * so we need to do the following:
+				 * - check that a filename is actually present;
+				 * - advance av by optind-1 to skip arguments
+				 *   already processed;
+				 * - decrease ac by optind, to remove the args
+				 *   already processed and the final filename;
+				 * - set the last entry in av[] to NULL so
+				 *   popen() can detect the end of the array;
+				 * - set optind=ac to let getopt() terminate.
+				 */
+				if (optind == ac)
+					errx(EX_USAGE, "no filename argument");
+				cmd = optarg;
+				av[ac-1] = NULL;
+				av += optind - 1;
+				ac -= optind;
+				optind = ac;
+				break;
 
-		case 'p':
-			/*
-			 * ipfw -p cmd [args] filename
-			 *
-			 * We are done with getopt(). All arguments
-			 * except the filename go to the preprocessor,
-			 * so we need to do the following:
-			 * - check that a filename is actually present;
-			 * - advance av by optind-1 to skip arguments
-			 *   already processed;
-			 * - decrease ac by optind, to remove the args
-			 *   already processed and the final filename;
-			 * - set the last entry in av[] to NULL so
-			 *   popen() can detect the end of the array;
-			 * - set optind=ac to let getopt() terminate.
-			 */
-			if (optind == ac)
-				errx(EX_USAGE, "no filename argument");
-			cmd = optarg;
-			av[ac-1] = NULL;
-			av += optind - 1;
-			ac -= optind;
-			optind = ac;
-			break;
-
-		case 'q':
-			g_co.do_quiet = 1;
-			break;
-
-		case 'S':
-			g_co.show_sets = 1;
-			break;
-
-		default:
-			errx(EX_USAGE, "bad arguments, for usage"
-			     " summary ``ipfw''");
+			case 'q':
+				g_co.do_quiet = 1;
+				break;
+
+			case 'S':
+				g_co.show_sets = 1;
+				break;
+
+			default:
+				errx(EX_USAGE, "bad arguments, for usage"
+				     " summary ``ipfw''");
+			}
 		}
+	} else {
+		while ((c = getopt(ac, av, "nq")) != -1) {
+			switch(c) {
+			case 'n':
+				g_co.test_only = 1;
+				break;
+
+			case 'q':
+				g_co.do_quiet = 1;
+				break;
 
+			default:
+				errx(EX_USAGE, "bad arguments, for usage"
+				     " summary ``dnctl''");
+			}
+		}
 	}
 
 	if (cmd == NULL && ac != optind + 1)
@@ -676,10 +693,6 @@ main(int ac, char *av[])
 	 */
 
 	if (ac > 1 && av[ac - 1][0] == '/') {
-		if (! is_ipfw())
-			errx(EX_USAGE, "usage: dnctl [options]\n"
-			    "do \"dnctl -h\" for details");
-
 		if (access(av[ac - 1], R_OK) == 0)
 			ipfw_readfile(ac, av);
 		else