git: 54290c48db65 - stable/13 - ifconfig: Add format shortcuts.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 20 May 2024 09:35:04 UTC
The branch stable/13 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=54290c48db65986b9953e27420c86a18c3bb21f7

commit 54290c48db65986b9953e27420c86a18c3bb21f7
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-05-14 06:51:42 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-05-20 09:04:58 +0000

    ifconfig: Add format shortcuts.
    
    MFC after:      1 week
    Reviewed by:    imp
    Differential Revision:  https://reviews.freebsd.org/D45166
    
    (cherry picked from commit 847ef59d4b5eab234bd1f8eb947ad74bdab5614e)
---
 sbin/ifconfig/ifconfig.8 | 14 ++++++++++++--
 sbin/ifconfig/ifconfig.c | 41 +++++++++++++++++++++++++----------------
 2 files changed, 37 insertions(+), 18 deletions(-)

diff --git a/sbin/ifconfig/ifconfig.8 b/sbin/ifconfig/ifconfig.8
index f280a263cb51..28b6f2dfb72e 100644
--- a/sbin/ifconfig/ifconfig.8
+++ b/sbin/ifconfig/ifconfig.8
@@ -27,7 +27,7 @@
 .\"
 .\"     From: @(#)ifconfig.8	8.3 (Berkeley) 1/5/94
 .\"
-.Dd February 1, 2023
+.Dd May 12, 2024
 .Dt IFCONFIG 8
 .Os
 .Sh NAME
@@ -131,7 +131,7 @@ and their associated
 .Ar format
 strings are:
 .Pp
-.Bl -tag -width ether
+.Bl -tag -width default
 .It Cm addr
 Adjust the display of inet and inet6 addresses:
 .Pp
@@ -193,6 +193,16 @@ Integer format, for example:
 .Ql prefixlen 64
 .El
 .El
+.Pp
+In addition, the following shortcuts are accepted:
+.Bl -tag -width default
+.It Cm default
+Resets all formats to their default values.
+.It Cm cidr
+Shortcut notation for
+.Cm inet:cidr,inet6:cidr .
+.El
+.Pp
 .It Fl G Ar groupname
 Exclude members of the specified
 .Ar groupname
diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c
index 1537edfc5ea2..74dfb73f565f 100644
--- a/sbin/ifconfig/ifconfig.c
+++ b/sbin/ifconfig/ifconfig.c
@@ -303,14 +303,10 @@ cmpifaddrs(struct ifaddrs *a, struct ifaddrs *b, struct ifa_queue *q)
 static void freeformat(void)
 {
 
-	if (f_inet != NULL)
-		free(f_inet);
-	if (f_inet6 != NULL)
-		free(f_inet6);
-	if (f_ether != NULL)
-		free(f_ether);
-	if (f_addr != NULL)
-		free(f_addr);
+	free(f_inet);
+	free(f_inet6);
+	free(f_ether);
+	free(f_addr);
 }
 
 static void setformat(char *input)
@@ -320,9 +316,18 @@ static void setformat(char *input)
 	formatstr = strdup(input);
 	while ((category = strsep(&formatstr, ",")) != NULL) {
 		modifier = strchr(category, ':');
-		if (modifier == NULL || modifier[1] == '\0') {
-			warnx("Skipping invalid format specification: %s\n",
-			    category);
+		if (modifier == NULL) {
+			if (strcmp(category, "default") == 0) {
+				freeformat();
+			} else if (strcmp(category, "cidr") == 0) {
+				free(f_inet);
+				f_inet = strdup(category);
+				free(f_inet6);
+				f_inet6 = strdup(category);
+			} else {
+				warnx("Skipping invalid format: %s\n",
+				    category);
+			}
 			continue;
 		}
 
@@ -330,14 +335,19 @@ static void setformat(char *input)
 		modifier[0] = '\0';
 		modifier++;
 
-		if (strcmp(category, "addr") == 0)
+		if (strcmp(category, "addr") == 0) {
+			free(f_addr);
 			f_addr = strdup(modifier);
-		else if (strcmp(category, "ether") == 0)
+		} else if (strcmp(category, "ether") == 0) {
+			free(f_ether);
 			f_ether = strdup(modifier);
-		else if (strcmp(category, "inet") == 0)
+		} else if (strcmp(category, "inet") == 0) {
+			free(f_inet);
 			f_inet = strdup(modifier);
-		else if (strcmp(category, "inet6") == 0)
+		} else if (strcmp(category, "inet6") == 0) {
+			free(f_inet6);
 			f_inet6 = strdup(modifier);
+		}
 	}
 	free(formatstr);
 }
@@ -427,7 +437,6 @@ main(int argc, char *argv[])
 #endif
 
 	all = downonly = uponly = namesonly = noload = verbose = 0;
-	f_inet = f_inet6 = f_ether = f_addr = NULL;
 	matchgroup = nogroup = NULL;
 
 	lifh = ifconfig_open();