unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Girish Joshi via Libc-alpha <libc-alpha@sourceware.org>
To: Zack Weinberg <zackw@panix.com>
Cc: Florian Weimer <fweimer@redhat.com>,
	Girish Joshi via Libc-alpha <libc-alpha@sourceware.org>
Subject: Re: [PATCH] argp: argp.doc prints incorrectly when it starts with "\v" [BZ #19038]
Date: Fri, 19 Feb 2021 02:43:15 +0530	[thread overview]
Message-ID: <CALkY8p-_EKHtme_rDMV5ivG4iE80e4dccn0z0fj0sD1_uh2bfA@mail.gmail.com> (raw)
In-Reply-To: <CAKCAbMh0nOW5AyPTyDxeC4i1NXaT-UTZgfG2m_rS0Cu-=MBcnA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 345 bytes --]

Hi,

For a long time I could not work on this patch.
Recently I had sent the patch on gnulib mailing list, but did not get
a reply yet.
I'll send another follow up mail over there also.
Also I saw a few patches involving argp on the glibc mailing list.
So I'm sending this patch once again. Could someone please review it?
Thanks.

Girish Joshi

[-- Attachment #2: 0001-argp-argp-help.c-Corrected-the-default-value-and-usa.patch --]
[-- Type: text/x-patch, Size: 6039 bytes --]

From 4917277104171bca1fa8685e4188cb8780be7b3a Mon Sep 17 00:00:00 2001
From: Girish Joshi <girish946@gmail.com>
Date: Sun, 7 Feb 2021 23:58:36 +0530
Subject: [PATCH] argp/argp-help.c: Corrected the default value and usage for
 inp_text_limit in argp_doc(). argp/tst-argp3.c: added test case when the doc
 string contains leading \v. argp/Makefile: added tst-argp3 to tests

Overview:
argp.doc prints incorrectly when it starts with '\v'.
In argp-help.c in the function argp_doc() variable inp_text_limit is reset to 0
if the doc string starts with '\v'. Which causes the whole doc string to be
printed in the case of pre documentation, because of initialization of inp_text
and inp_text_limit

    inp_text = post ? (vt ? vt + 1 : 0) : doc;
    inp_text_limit = (!post && vt) ? (vt - doc) : 0;

and the condition where the doc string is printed.

    if (text == inp_text && inp_text_limit)
      __argp_fmtstream_write (stream, inp_text, inp_text_limit);

So for the following code

    #include<argp.h>

    static char doc[] = "\vthis is post_doc";
    static struct argp argp = {NULL, NULL, NULL, doc};

    int main(int argc, char *args[]){
         argp_parse(&argp, argc, args, 0, 0, NULL);
    }

the output is

    $ argp-help --help
    Usage: argp-help [OPTION...]

    this is post_doc

      -?, --help                 Give this help list
          --usage                Give a short usage message

    this is post_doc

As mentioned in the bugzilla entry the first occurrence of
"this is post_doc" is erroneous as it is the pre doc and there is nothing
in the doc string in predoc section.

Implementation:
Reset the value of inp_text_limit to -1 if the doc string starts with '\v'.
Modify the condition for printing the complete doc string with validation for
inp_text_limit variable which looks like.

    if (text == inp_text && inp_text_limit != -1)
      __argp_fmtstream_write (stream, inp_text, inp_text_limit);

after this patch we get the output as following

    $ argp-help --help
    Usage: argp-help [OPTION...]

      -?, --help                 Give this help list
          --usage                Give a short usage message

    this is post_doc
---
 argp/Makefile    |  2 +-
 argp/argp-help.c |  6 ++---
 argp/tst-argp3.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100644 argp/tst-argp3.c

diff --git a/argp/Makefile b/argp/Makefile
index 90023fba16..aa96a9c221 100644
--- a/argp/Makefile
+++ b/argp/Makefile
@@ -27,7 +27,7 @@ routines	= $(addprefix argp-, ba fmtstream fs-xinl help parse pv \
 				     pvh xinl eexst)
 
 tests		= argp-test tst-argp1 bug-argp1 tst-argp2 bug-argp2 \
-		  tst-ldbl-argp
+		  tst-ldbl-argp tst-argp3
 
 CFLAGS-argp-help.c += $(uses-callbacks) -fexceptions
 CFLAGS-argp-parse.c += $(uses-callbacks)
diff --git a/argp/argp-help.c b/argp/argp-help.c
index 02afba2c46..bda572465a 100644
--- a/argp/argp-help.c
+++ b/argp/argp-help.c
@@ -1575,7 +1575,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
   const char *inp_text;
   void *input = 0;
   int anything = 0;
-  size_t inp_text_limit = 0;
+  size_t inp_text_limit = -1;
   const char *doc = dgettext (argp->argp_domain, argp->doc);
   const struct argp_child *child = argp->children;
 
@@ -1583,7 +1583,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
     {
       char *vt = strchr (doc, '\v');
       inp_text = post ? (vt ? vt + 1 : 0) : doc;
-      inp_text_limit = (!post && vt) ? (vt - doc) : 0;
+      inp_text_limit = (!post && vt) ? (vt - doc) : -1;
     }
   else
     inp_text = 0;
@@ -1609,7 +1609,7 @@ argp_doc (const struct argp *argp, const struct argp_state *state,
       if (pre_blank)
 	__argp_fmtstream_putc (stream, '\n');
 
-      if (text == inp_text && inp_text_limit)
+      if (text == inp_text && inp_text_limit != -1)
 	__argp_fmtstream_write (stream, inp_text, inp_text_limit);
       else
 	__argp_fmtstream_puts (stream, text);
diff --git a/argp/tst-argp3.c b/argp/tst-argp3.c
new file mode 100644
index 0000000000..cfdace2574
--- /dev/null
+++ b/argp/tst-argp3.c
@@ -0,0 +1,68 @@
+/* Test for argparse with leading '\v' in the doc string.
+  Copyright (C) 2020 Free Software Foundation, Inc.
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <https://www.gnu.org/licenses/>.*/
+
+
+#include<stdlib.h>
+#include<argp.h>
+
+#include <support/capture_subprocess.h>
+#include <support/check.h>
+
+
+static char expected_success[] = "Usage: arp [OPTION...]\n\
+\n\
+  -?, --help                 Give this help list\n\
+      --usage                Give a short usage message\n\
+\n\
+this is post_doc\n\
+";
+char *argv[3] = { (char *) "arp", NULL, NULL };
+
+static void
+do_test_call (void)
+{
+  static char doc[] = "\vthis is post_doc";
+  static struct argp argp = {NULL, NULL, NULL, doc};
+
+  argp_parse (&argp, 2, argv, 0, 0, NULL);
+}
+
+static int
+do_one_test (const char *expected)
+{
+  struct support_capture_subprocess result;
+  result = support_capture_subprocess ((void *) &do_test_call, NULL);
+
+  TEST_COMPARE_STRING (result.out.buffer, expected);
+
+  return 0;
+}
+
+
+static int
+do_test (void)
+{
+  const char *argument = "--help";
+  argv[1] = (char *)argument;
+  // success condition
+  do_one_test (expected_success);
+  return 0;
+}
+
+/* This file references do_test above and contains the definition of
+   the main function.  */
+#include <support/test-driver.c>
+
-- 
2.26.2


  reply	other threads:[~2021-02-18 21:13 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-03 18:13 [PATCH] argp: argp.doc prints incorrectly when it starts with "\v" [BZ #19038] Girish Joshi
2020-03-19 19:38 ` Girish Joshi via Libc-alpha
2020-03-21  2:03 ` DJ Delorie via Libc-alpha
2020-03-25 17:55   ` Girish Joshi via Libc-alpha
2020-05-07 19:53     ` Girish Joshi via Libc-alpha
2020-05-23 10:10       ` Girish Joshi via Libc-alpha
2020-06-14 15:50       ` Girish Joshi via Libc-alpha
2020-06-15  8:56       ` Florian Weimer via Libc-alpha
2020-06-15  9:51         ` Girish Joshi via Libc-alpha
2020-06-15 14:38           ` Zack Weinberg
2021-02-18 21:13             ` Girish Joshi via Libc-alpha [this message]
  -- strict thread matches above, loose matches on Subject: below --
2019-05-06  8:55 Girish Joshi
2019-05-06 12:37 ` Florian Weimer
2019-05-06 13:38   ` Girish Joshi
2019-05-30  8:14     ` Girish Joshi
2019-05-30 12:52       ` Adhemerval Zanella
2019-07-05  9:01       ` Girish Joshi
2019-07-05 10:34         ` Girish Joshi
2019-07-25 15:33           ` Joseph Myers
2019-11-25 18:08             ` Girish Joshi
2019-11-30 14:47               ` Girish Joshi
2019-11-30 15:05                 ` Carlos O'Donell
2020-01-07 17:55                   ` Girish Joshi
2020-01-20 17:21               ` Girish Joshi
2020-01-24  2:38                 ` Carlos O'Donell
2020-02-11 18:40                   ` Girish Joshi
2020-02-20  7:09                     ` Girish Joshi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CALkY8p-_EKHtme_rDMV5ivG4iE80e4dccn0z0fj0sD1_uh2bfA@mail.gmail.com \
    --to=libc-alpha@sourceware.org \
    --cc=fweimer@redhat.com \
    --cc=girish946@gmail.com \
    --cc=zackw@panix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).