From nobody Sun May 21 09:43:00 2023 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QPFxd1MjGz4CHYB; Sun, 21 May 2023 09:43:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QPFxd0vW2z42Zg; Sun, 21 May 2023 09:43:01 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684662181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ok3hW7Q+YtUJGdAlRsZiiZjwSWRINSb4osdBzGdILVc=; b=IaAdPNo5b/TI+sk81C4qOM8o4MCef2a3u+F4w0g8CW6VnrKKSKAmYW1O5iTvRK6iFTuWVf ZpA6AArBCZerZZbXXTk4q/I2A9/uMynmQWIr5E8GMsZHdwIRrq8B0FrXF8O69Cgyyebuqm 4bx+/rrahmX0hEM9v81ArsMF+fkDT1N5hvxcoTqO398nVK+TkA4dnTfEbcz/RGfrsfFpMo H18/YSs5IsL5NRq/gh1+mEw6PF4f/PkrWoA9QJ5RM5hGeXGdiM3e314aSAlQCEddSkOUdp EFgGIsspdMfmaZc3XmAc+1DXB28wA1WbHTzBBmONetdIX0cP5BrPaenizz0ZhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684662181; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ok3hW7Q+YtUJGdAlRsZiiZjwSWRINSb4osdBzGdILVc=; b=YBSxO9xph7hIeavEL9tQmObuHs21tTeBdLAfJ006f2GxQjzeXxPXOPEhT/nnaUcN4+qXwx hwwd4RmTC5O72tvlg1DIUixAwGpNF7zSdYieBiWpO6nsv7lYvFsnHFHNML3vDnVWtcMPEW xFxCweB3RhzUMKb3tFtJgXNE2UtK35DUDBaFI93PGJsVGPHBcWaiuk/g2Ldu8zHy1QShIu gzTBaj2l2LjJhKiYLq+uVrDhqr+SUVAmUJN+n7hlaKL6rkmpStpDGBDUNI6b9KdZKD+/lW Ec5ydfI4Kj0ZT6F0mckaFaH/keEBCvQi2g0QrfmmMIccptBqYgD8hf4a+RtEkw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1684662181; a=rsa-sha256; cv=none; b=mgcQZVqTaq7wA1nu99VfLQpJbG7BMC8ci+WzR0zPKK2lGvQtdJB0Afdg4mFLvhH31315kx rNZckbRiBD5AxnH4vug2N+DtsNtMm9pgHfm4XkdP3AI0QtMt3CenQLrDMdv+zXPKDaf7nq hQTnQpWxqDObbgmAF3AKCrxKoj/+TBZ+KwSC8xnWosRcVFtOAqLu/jD+PEX6HxnXt+k3Po NUFgLgV4sBBUSmcZ5Tz3U4T/Q2AVXnKMZCwZdK3/JCPFOwMC/94YqqPZOcRqLw22kpedNu JYFfXu9jGH2AfSZ0MvHQL62sq0TkGy3Kw9nRCUZro8TgVHTq8+88qso9hgwl/A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QPFxc74QmzmQg; Sun, 21 May 2023 09:43:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 34L9h0EZ006347; Sun, 21 May 2023 09:43:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34L9h0Me006346; Sun, 21 May 2023 09:43:00 GMT (envelope-from git) Date: Sun, 21 May 2023 09:43:00 GMT Message-Id: <202305210943.34L9h0Me006346@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 197bff385e2c - main - ifconfig: split argument parsing List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 197bff385e2caf82c52036f572e1cc79efc217f8 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=197bff385e2caf82c52036f572e1cc79efc217f8 commit 197bff385e2caf82c52036f572e1cc79efc217f8 Author: Alexander V. Chernikov AuthorDate: 2023-05-20 11:14:39 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-05-21 09:42:11 +0000 ifconfig: split argument parsing Simplify main() by factoring out argument parsing code. Reviewed by: kp MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D40179 --- sbin/ifconfig/ifconfig.c | 165 ++++++++++++++++++++++++++++------------------- 1 file changed, 98 insertions(+), 67 deletions(-) diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index 8c3a7b4c0007..16ab651261d1 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -407,32 +407,34 @@ list_interfaces(struct ifconfig_args *args) #endif } -int -main(int argc, char *argv[]) +static char * +args_peek(struct ifconfig_args *args) { - int c; - const struct afswtch *afp = NULL; - int ifindex; - char options[1024], *envformat; - struct option *p; - size_t iflen; - int flags; + if (args->argc > 0) + return (args->argv[0]); + return (NULL); +} - f_inet = f_inet6 = f_ether = f_addr = NULL; +static char * +args_pop(struct ifconfig_args *args) +{ + if (args->argc == 0) + return (NULL); - lifh = ifconfig_open(); - if (lifh == NULL) - err(EXIT_FAILURE, "ifconfig_open"); + char *arg = args->argv[0]; - envformat = getenv("IFCONFIG_FORMAT"); - if (envformat != NULL) - setformat(envformat); + args->argc--; + args->argv++; - /* - * Ensure we print interface name when expected to, - * even if we terminate early due to error. - */ - atexit(printifnamemaybe); + return (arg); +} + +static void +args_parse(struct ifconfig_args *args, int argc, char *argv[]) +{ + char options[1024]; + struct option *p; + int c; /* Parse leading line options */ strlcpy(options, "G:adf:klmnuv", sizeof(options)); @@ -441,10 +443,10 @@ main(int argc, char *argv[]) while ((c = getopt(argc, argv, options)) != -1) { switch (c) { case 'a': /* scan all interfaces */ - args.all = true; + args->all = true; break; case 'd': /* restrict scan to "down" interfaces */ - args.downonly = true; + args->downonly = true; break; case 'f': if (optarg == NULL) @@ -452,33 +454,33 @@ main(int argc, char *argv[]) setformat(optarg); break; case 'G': - if (optarg == NULL || args.all == 0) + if (optarg == NULL || args->all == 0) usage(); - args.nogroup = optarg; + args->nogroup = optarg; break; case 'k': - args.printkeys = true; + args->printkeys = true; break; case 'l': /* scan interface names only */ - args.namesonly++; + args->namesonly++; break; case 'm': /* show media choices in status */ - args.supmedia = true; + args->supmedia = true; break; case 'n': /* suppress module loading */ - args.noload = true; + args->noload = true; break; case 'u': /* restrict scan to "up" interfaces */ - args.uponly = true; + args->uponly = true; break; case 'v': - args.verbose++; + args->verbose++; break; case 'g': - if (args.all) { + if (args->all) { if (optarg == NULL) usage(); - args.matchgroup = optarg; + args->matchgroup = optarg; break; } /* FALLTHROUGH */ @@ -496,30 +498,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - /* Sync global variables */ - printkeys = args.printkeys; - verbose = args.verbose; - /* -l cannot be used with -a or -m */ - if (args.namesonly && (args.all || args.supmedia)) + if (args->namesonly && (args->all || args->supmedia)) usage(); /* nonsense.. */ - if (args.uponly && args.downonly) + if (args->uponly && args->downonly) usage(); /* no arguments is equivalent to '-a' */ - if (!args.namesonly && argc < 1) - args.all = 1; + if (!args->namesonly && argc < 1) + args->all = 1; /* -a and -l allow an address family arg to limit the output */ - if (args.all || args.namesonly) { + if (args->all || args->namesonly) { if (argc > 1) usage(); - ifindex = 0; if (argc == 1) { - afp = af_getbyname(*argv); + const struct afswtch *afp = af_getbyname(*argv); + if (afp == NULL) { warnx("Address family '%s' unknown.", *argv); usage(); @@ -527,32 +525,68 @@ main(int argc, char *argv[]) if (afp->af_name != NULL) argc--, argv++; /* leave with afp non-zero */ + args->afp = afp; } } else { /* not listing, need an argument */ if (argc < 1) usage(); + } - args.ifname = *argv; - argc--, argv++; + args->argc = argc; + args->argv = argv; + + /* Sync global variables */ + printkeys = args->printkeys; + verbose = args->verbose; +} + +int +main(int ac, char *av[]) +{ + char *envformat; + size_t iflen; + int flags; + + f_inet = f_inet6 = f_ether = f_addr = NULL; + + lifh = ifconfig_open(); + if (lifh == NULL) + err(EXIT_FAILURE, "ifconfig_open"); + + envformat = getenv("IFCONFIG_FORMAT"); + if (envformat != NULL) + setformat(envformat); + + /* + * Ensure we print interface name when expected to, + * even if we terminate early due to error. + */ + atexit(printifnamemaybe); + + args_parse(&args, ac, av); + + if (!args.all && !args.namesonly) { + /* not listing, need an argument */ + args.ifname = args_pop(&args); /* check and maybe load support for this interface */ ifmaybeload(&args, args.ifname); - ifindex = if_nametoindex(args.ifname); - if (ifindex == 0) { + char *arg = args_peek(&args); + if (if_nametoindex(args.ifname) == 0) { /* * NOTE: We must special-case the `create' command * right here as we would otherwise fail when trying * to find the interface. */ - if (argc > 0 && (strcmp(argv[0], "create") == 0 || - strcmp(argv[0], "plumb") == 0)) { + if (arg != NULL && (strcmp(arg, "create") == 0 || + strcmp(arg, "plumb") == 0)) { iflen = strlcpy(name, args.ifname, sizeof(name)); if (iflen >= sizeof(name)) errx(1, "%s: cloning name too long", args.ifname); - ifconfig(argc, argv, 1, NULL); + ifconfig(args.argc, args.argv, 1, NULL); exit(exit_code); } #ifdef JAIL @@ -561,12 +595,12 @@ main(int argc, char *argv[]) * right here as we would otherwise fail when trying * to find the interface as it lives in another vnet. */ - if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) { + if (arg != NULL && (strcmp(arg, "-vnet") == 0)) { iflen = strlcpy(name, args.ifname, sizeof(name)); if (iflen >= sizeof(name)) errx(1, "%s: interface name too long", args.ifname); - ifconfig(argc, argv, 0, NULL); + ifconfig(args.argc, args.argv, 0, NULL); exit(exit_code); } #endif @@ -576,21 +610,21 @@ main(int argc, char *argv[]) * Do not allow use `create` command as hostname if * address family is not specified. */ - if (argc > 0 && (strcmp(argv[0], "create") == 0 || - strcmp(argv[0], "plumb") == 0)) { - if (argc == 1) + if (arg != NULL && (strcmp(arg, "create") == 0 || + strcmp(arg, "plumb") == 0)) { + if (args.argc == 1) errx(1, "interface %s already exists", args.ifname); - argc--, argv++; + args_pop(&args); } } } /* Check for address family */ - if (argc > 0) { - afp = af_getbyname(*argv); - if (afp != NULL) - argc--, argv++; + if (args.argc > 0) { + args.afp = af_getbyname(args_peek(&args)); + if (args.afp != NULL) + args_pop(&args); } /* @@ -598,7 +632,7 @@ main(int argc, char *argv[]) * which doesn't require building, sorting, and searching the entire * system address list */ - if ((argc > 0) && (args.ifname != NULL)) { + if ((args.argc > 0) && (args.ifname != NULL)) { iflen = strlcpy(name, args.ifname, sizeof(name)); if (iflen >= sizeof(name)) { warnx("%s: interface name too long, skipping", args.ifname); @@ -607,15 +641,12 @@ main(int argc, char *argv[]) if (!(((flags & IFF_CANTCONFIG) != 0) || (args.downonly && (flags & IFF_UP) != 0) || (args.uponly && (flags & IFF_UP) == 0))) - ifconfig(argc, argv, 0, afp); + ifconfig(args.argc, args.argv, 0, args.afp); } goto done; } - args.afp = afp; - args.allfamilies = afp == NULL; - args.argc = argc; - args.argv = argv; + args.allfamilies = args.afp == NULL; list_interfaces(&args);