Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qJGIB-00016U-71 for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jul 2023 16:32:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qJGI9-0001s4-NO for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Jul 2023 16:32:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qJGI9-0001rv-Cx for pgsql-hackers@lists.postgresql.org; Tue, 11 Jul 2023 16:32:45 +0000 Received: from mail-pj1-x1034.google.com ([2607:f8b0:4864:20::1034]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qJGI1-003VRv-Qo for pgsql-hackers@postgresql.org; Tue, 11 Jul 2023 16:32:44 +0000 Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-262ef07be72so2869365a91.1 for ; Tue, 11 Jul 2023 09:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689093155; x=1691685155; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=Isswm2kxqq7TFMet0Wj3+vxnTwI0jAWw8it4H16xQrg=; b=cyO2xSpoIaQAgJBJbu2S2zmtUnSN2/0sG3zvOJUmI6/bbGoxxprjgJ8HMgeemLHffR tVoB1cw6s+x4v2agmnP7WEFnqsYZD0vhxQ0xE0KGPPHrqM2BZJwbEmcZUBPdOIirIHc2 nAKtRt5ZMi6GWH9I7vOKdvGThS31UjPfZjVAy6A+ezKwPvW5+3+nKbONME51sLILRGkW /z03NF6VDg7uI0PXz0+8BvUrFIModu3Lj/0dqBkphZ0grTGdIU2C42/9RiJBEWP5J4az rlARPgcQtbOVzKWsRMSZokgJdlkPYZ4yDfF6BITXj0P3xRgnEk3oLLZJOlO8Tl7i6HLw ZnaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689093155; x=1691685155; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Isswm2kxqq7TFMet0Wj3+vxnTwI0jAWw8it4H16xQrg=; b=jVa1DAEOHhqyi0twjyozgwggh24K6mOmlaQMOX/bzLC0bYd5gP0KVHbwjlrZskW37g h3usMsrKgf+N4beSSVJPn3ZWvzdwhFiq1jMUL90HuREQ2zYd9WMtTmcQ/9Nj/SyFuZPv UDCFfPjWgMolghN9d0sX7h2g2JwWpCUeRzvXEa9FHPmWDy92TuKPWHLK6/mbgwnMtiLY dnC7QDkqX7SKJoxHsrdgXASK8oDmxGhE7je2TlRTCW5Cze6r2I8Gc/NLRpuPCtIJAWTM hMGWU36PfgJHxd647BMA94priBpopKpUgSR0CGijV6lAw+aU97F7tij8e0Ko1gz7pkRB T20g== X-Gm-Message-State: ABy/qLZRuctOxJswue54ZVC99ZYbadmH/wZy++q7h4+sV+uyQgccV8Re unF7+J/kKpjrryqqJegpl7I= X-Google-Smtp-Source: APBJJlF1Jt6rAnnh5PiVAUlu7v8CGdx4yzKeGNcI1LP6BUdyBvfsayUqAMYB4gt41OrGU6a2MFfIqw== X-Received: by 2002:a17:90b:906:b0:263:71f0:1569 with SMTP id bo6-20020a17090b090600b0026371f01569mr11861963pjb.3.1689093154834; Tue, 11 Jul 2023 09:32:34 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id 25-20020a17090a1a1900b0025be7b69d73sm2214882pjk.12.2023.07.11.09.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 09:32:33 -0700 (PDT) Date: Tue, 11 Jul 2023 09:32:32 -0700 From: Nathan Bossart To: Kyotaro Horiguchi Cc: michael@paquier.xyz, noah@leadboat.com, pgsql-hackers@postgresql.org Subject: Re: add non-option reordering to in-tree getopt_long Message-ID: <20230711163232.GA616878@nathanxps13> References: <20230710.165711.1906730109995973216.horikyota.ntt@gmail.com> <20230710181237.GD257945@nathanxps13> <20230710200658.GA362078@nathanxps13> <20230711.161609.1881651378858129172.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="HlL+5n6rz5pIUxbD" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20230711.161609.1881651378858129172.horikyota.ntt@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --HlL+5n6rz5pIUxbD Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Tue, Jul 11, 2023 at 04:16:09PM +0900, Kyotaro Horiguchi wrote: > I like it. We don't need to overcomplicate things just for the sake of > speed here. Plus, this version looks the most simple to me. That being > said, it might be better if the last term is positioned in the second > place. This is mainly because a lone hyphen is less common than words > that starts with characters other than a pyphen. Sure. І did it this way in v7. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --HlL+5n6rz5pIUxbD Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v7-0001-Teach-in-tree-getopt_long-to-move-non-options-to-.patch" From 7c978c11c8012cbfa67646bfc37849cb061f4e07 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 7 Jul 2023 20:00:47 -0700 Subject: [PATCH v7 1/1] Teach in-tree getopt_long() to move non-options to the end of argv. Unlike the other implementations of getopt_long() I could find, the in-tree implementation does not reorder non-options to the end of argv. Instead, it returns -1 as soon as the first non-option is found, even if there are other options listed afterwards. By moving non-options to the end of argv, getopt_long() can parse all specified options and return -1 when only non-options remain. This quirk is periodically missed by hackers (e.g., 869aa40a27, ffd398021c, and d9ddc50baf). This commit introduces the aforementioned non-option reordering behavior to the in-tree getopt_long() implementation. The only systems I'm aware of that use it are Windows and AIX, both of which have been tested. Special thanks to Noah Misch for his help validating behavior on AIX. Reviewed-by: Kyotaro Horiguchi, Michael Paquier Discussion: https://postgr.es/m/20230609232257.GA121461%40nathanxps13 --- src/bin/scripts/t/040_createuser.pl | 10 +++--- src/port/getopt_long.c | 54 ++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl index 9ca282181d..3290e30c88 100644 --- a/src/bin/scripts/t/040_createuser.pl +++ b/src/bin/scripts/t/040_createuser.pl @@ -42,9 +42,8 @@ $node->issues_sql_like( 'add a role as a member with admin option of the newly created role'); $node->issues_sql_like( [ - 'createuser', '-m', - 'regress_user3', '-m', - 'regress user #4', 'REGRESS_USER5' + 'createuser', 'REGRESS_USER5', '-m', 'regress_user3', + '-m', 'regress user #4' ], qr/statement: CREATE ROLE "REGRESS_USER5" NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS ROLE regress_user3,"regress user #4";/, 'add a role as a member of the newly created role'); @@ -73,11 +72,14 @@ $node->issues_sql_like( qr/statement: CREATE ROLE regress_user11 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS IN ROLE regress_user1;/, '--role'); $node->issues_sql_like( - [ 'createuser', '--member-of', 'regress_user1', 'regress_user12' ], + [ 'createuser', 'regress_user12', '--member-of', 'regress_user1' ], qr/statement: CREATE ROLE regress_user12 NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN NOREPLICATION NOBYPASSRLS IN ROLE regress_user1;/, '--member-of'); $node->command_fails([ 'createuser', 'regress_user1' ], 'fails if role already exists'); +$node->command_fails( + [ 'createuser', 'regress_user1', '-m', 'regress_user2', 'regress_user3' ], + 'fails for too many non-options'); done_testing(); diff --git a/src/port/getopt_long.c b/src/port/getopt_long.c index c989276988..b290a2bab9 100644 --- a/src/port/getopt_long.c +++ b/src/port/getopt_long.c @@ -50,8 +50,9 @@ * This implementation does not use optreset. Instead, we guarantee that * it can be restarted on a new argv array after a previous call returned -1, * if the caller resets optind to 1 before the first call of the new series. - * (Internally, this means we must be sure to reset "place" to EMSG before - * returning -1.) + * + * Note that this routine reorders the pointers in argv (despite the const + * qualifier) so that all non-options will be at the end when -1 is returned. */ int getopt_long(int argc, char *const argv[], @@ -60,40 +61,59 @@ getopt_long(int argc, char *const argv[], { static char *place = EMSG; /* option letter processing */ char *oli; /* option letter list index */ + static int nonopt_start = -1; + static bool force_nonopt = false; if (!*place) { /* update scanning pointer */ - if (optind >= argc) + char **args = (char **) argv; + +retry: + + /* + * If we are out of arguments or only non-options remain, return -1. + */ + if (optind >= argc || optind == nonopt_start) { place = EMSG; + nonopt_start = -1; + force_nonopt = false; return -1; } place = argv[optind]; - if (place[0] != '-') + /* + * An argument is a non-option if it meets any of the following + * criteria: it follows an argument that is equivalent to the string + * "--", it does not start with '-', or it is equivalent to the string + * "-". When we encounter a non-option, we move it to the end of argv + * (after shifting all remaining arguments over to make room), and + * then we try again with the next argument. + */ + if (force_nonopt || place[0] != '-' || strcmp("-", place) == 0) { - place = EMSG; - return -1; - } + for (int i = optind; i < argc - 1; i++) + args[i] = args[i + 1]; + args[argc - 1] = place; - place++; + if (nonopt_start == -1) + nonopt_start = argc - 1; + else + nonopt_start--; - if (!*place) - { - /* treat "-" as not being an option */ - place = EMSG; - return -1; + goto retry; } - - if (place[0] == '-' && place[1] == '\0') + else if (strcmp("--", place) == 0) { /* found "--", treat it as end of options */ ++optind; - place = EMSG; - return -1; + force_nonopt = true; + goto retry; } + place++; + if (place[0] == '-' && place[1]) { /* long option */ -- 2.25.1 --HlL+5n6rz5pIUxbD--