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 1qAEBv-0001zy-94 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 18:28:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qAEBu-0005Iz-69 for pgsql-hackers@arkaria.postgresql.org; Fri, 16 Jun 2023 18:28:58 +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 1qAEBt-0005G6-RY for pgsql-hackers@lists.postgresql.org; Fri, 16 Jun 2023 18:28:57 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qAEBq-002r5u-IL for pgsql-hackers@postgresql.org; Fri, 16 Jun 2023 18:28:57 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-1b539d2f969so3956935ad.0 for ; Fri, 16 Jun 2023 11:28:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686940132; x=1689532132; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=h6c/RGQfa9PfUxnL2mzilCbzxBKoxKoaGK7xSebB3aM=; b=qlrjLESJXAEVB4q2iN7KG7Ju3QjgHCHeA1Ql2VFHfpVv8jdnNhQbKxtz27LA/lJyUT rxGXWYN70o6bMY16brpF63F/Tvei4hO4r4vNV5j4bh5I8BVtR7hfdzaStsZGWVBthaDU 0yztl5xITZOktR4fC9oDnWEuQHSUZaXHF6Ql66q3VT+wukLQhHP6WRmSl3cKGh90zWnl vpL6RsmHRoCp1y9DHJ2IJ23HXfNc3V/8zGxFSoO6FjdKN3drFdTvprs3SUTPA5bbFGny Zq2VBKHPBUjmyDXAEcHeaQCc2MGii97ba+2wsHFRsgMUyzVjOkccOboNsUczVim30LJb zJHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686940132; x=1689532132; h=in-reply-to: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=h6c/RGQfa9PfUxnL2mzilCbzxBKoxKoaGK7xSebB3aM=; b=Jvs5cAlrmPErOgpuXCtPGxU/qZ8408r3OyuV8iN0e3VGixuolMdDDIFA0xu9Hfu4XV J+amyZ/FhQWpswSTJjgNvb7jm8rfrqv8whKY5RDw6CP42z+LrbjtbfeNGxkG0yk7Wa5J FoWNgLkFkOP0sITrMAhnDWjiRpfT/iKUQGr6n9v0z6EC+VTf59XC1toirLukHzEDyqcq p61k8co2wNErvbTD0PH3hzQ5LDZUsZJdZPgmbGwJtoUqg8uBYwurPxxl/U6znqevnA7t /4GdFfBlpjaAXtCVOCMsKM5Bt2rgvjMd24xQ/6VyNKc/JVLnWioFzXyE0O7cFvJ3J01F tyDg== X-Gm-Message-State: AC+VfDzJZuX3WH0v2qqyrtQ+P2LCSY/93TbIxALjX13OVSNXHYkVq/Af veeeqY4jWMCyt12YzwaSZzA= X-Google-Smtp-Source: ACHHUZ6OQwceF6HbEepqknE+oIBc8QNXUEZHikZhykmgUuoMvbPltNz/Df6wITq4vKJx8794yH3MEg== X-Received: by 2002:a17:902:c948:b0:1b3:e184:70c5 with SMTP id i8-20020a170902c94800b001b3e18470c5mr11435449pla.11.1686940132356; Fri, 16 Jun 2023 11:28:52 -0700 (PDT) Received: from nathanxps13 ([50.216.92.212]) by smtp.gmail.com with ESMTPSA id n10-20020a170902e54a00b001b01448ba72sm15951805plf.215.2023.06.16.11.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jun 2023 11:28:51 -0700 (PDT) Date: Fri, 16 Jun 2023 11:28:47 -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: <20230616182847.GA71216@nathanxps13> References: <20230616000959.GA922959@nathanxps13> <20230616045828.GA972663@nathanxps13> <20230616.165138.1460643839992276252.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+QahgC5+KEYLbs62" Content-Disposition: inline In-Reply-To: <20230616.165138.1460643839992276252.horikyota.ntt@gmail.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --+QahgC5+KEYLbs62 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Jun 16, 2023 at 04:51:38PM +0900, Kyotaro Horiguchi wrote: > (Honestly, the rearrangement code looks somewhat tricky to grasp..) Yeah, I think there's still some room for improvement here. > It doesn't work properly if '--' is involved. > > For example, consider the following options (even though they don't > work for the command). > > psql -t -T hoho -a hoge -- -1 hage hige huge > > After the function returns -1, the argv array looks like this. The > "[..]" indicates the position of optind. > > psql -t -T hoho -a -- [-1] hage hige huge hoge > > This is clearly incorrect since "hoge" gets moved to the end. The > rearrangement logic needs to take into account the '--'. For your > information, the glibc version yields the following result for the > same options. > > psql -t -T hoho -a -- [hoge] -1 hage hige huge Ah, so it effectively retains the non-option ordering, even if there is a '--'. I think this behavior is worth keeping. I attempted to fix this in the attached patch. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --+QahgC5+KEYLbs62 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-Teach-in-tree-getopt_long-to-move-non-options-to-.patch" From 51e1033374c464ed90484f641d31b0ab705672f2 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Fri, 9 Jun 2023 15:51:58 -0700 Subject: [PATCH v4 1/1] Teach in-tree getopt_long() to move non-options to the end of argv. --- src/bin/scripts/t/040_createuser.pl | 10 +++--- src/port/getopt_long.c | 50 +++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/bin/scripts/t/040_createuser.pl b/src/bin/scripts/t/040_createuser.pl index 9ca282181d..ba40ab11a3 100644 --- a/src/bin/scripts/t/040_createuser.pl +++ b/src/bin/scripts/t/040_createuser.pl @@ -42,9 +42,9 @@ $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 +73,13 @@ $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..a4bdc6c8f0 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,38 +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 */ +retry: if (optind >= argc) { place = EMSG; + nonopt_start = -1; + force_nonopt = false; return -1; } place = argv[optind]; - if (place[0] != '-') + /* non-options, including '-' and anything after '--' */ + if (place[0] != '-' || place[1] == '\0' || force_nonopt) { - place = EMSG; - return -1; - } + char **args = (char **) argv; - place++; + /* + * If only non-options remain, return -1. Else, move the + * non-option to the end and try again. + */ + if (optind == nonopt_start) + { + place = EMSG; + nonopt_start = -1; + force_nonopt = false; + return -1; + } - if (!*place) - { - /* treat "-" as not being an option */ - place = EMSG; - return -1; + for (int i = optind; i < argc - 1; i++) + args[i] = args[i + 1]; + args[argc - 1] = place; + + if (nonopt_start == -1) + nonopt_start = argc - 1; + else + nonopt_start--; + + goto retry; } + place++; + if (place[0] == '-' && place[1] == '\0') { /* found "--", treat it as end of options */ ++optind; - place = EMSG; - return -1; + force_nonopt = true; + goto retry; } if (place[0] == '-' && place[1]) -- 2.25.1 --+QahgC5+KEYLbs62--