From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gateway24.websitewelcome.com (gateway24.websitewelcome.com [192.185.51.253]) by sourceware.org (Postfix) with ESMTPS id C7DDE385042A for ; Sun, 21 Feb 2021 02:12:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C7DDE385042A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=tom@tromey.com Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway24.websitewelcome.com (Postfix) with ESMTP id 543FE1BEF for ; Sat, 20 Feb 2021 20:12:58 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id DeF0lOGHAoE4DDeF0lKg0O; Sat, 20 Feb 2021 20:12:58 -0600 X-Authority-Reason: nr=8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=cZTdbH8pg/rveIaoJsJAknrwHLLV5jzH56fFxxenEho=; b=G/8fMeRIiEVejrynTuFqpBvoUu Ep7KkZYkEG4R35EBBkqXT8ktrZRV+RxVxDNeABD+x8+QQXTP19e2FP32rC0mqnUHA17ap5aXFoT5K qGBntbQLr877vVow1/nEXJ36P; Received: from 97-122-70-152.hlrn.qwest.net ([97.122.70.152]:55116 helo=localhost.localdomain) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lDeF0-000kMg-3W; Sat, 20 Feb 2021 19:12:58 -0700 From: Tom Tromey To: dwz@sourceware.org Cc: Tom Tromey Subject: [PATCH v2] Print --version and --help to stdout Date: Sat, 20 Feb 2021 19:12:54 -0700 Message-Id: <20210221021254.897231-1-tom@tromey.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.70.152 X-Source-L: No X-Exim-ID: 1lDeF0-000kMg-3W X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-70-152.hlrn.qwest.net (localhost.localdomain) [97.122.70.152]:55116 X-Source-Auth: tom+tromey.com X-Email-Count: 1 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3034.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: dwz@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Dwz mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 21 Feb 2021 02:13:01 -0000 Normall --version and --help output go to stdout and cause a program to exit with status 0. The rationale for the exit status is that the user asked for this behavior, and the program successfully complied. Printing --help output to stdout is nicer for piping into a pager. This patch changes dwz to follow this approach. I kept the program invocation name in the Usage line. Different programs seem to handle this differently. --- dwz.c | 148 ++++++++++++++++----------------- testsuite/dwz.tests/version.sh | 6 +- 2 files changed, 76 insertions(+), 78 deletions(-) diff --git a/dwz.c b/dwz.c index de432fb..de6b455 100644 --- a/dwz.c +++ b/dwz.c @@ -16223,20 +16223,20 @@ static struct option_help dwz_misc_options_help[] = "Display this information." } }; -/* Print LEN spaces to stderr. */ +/* Print LEN spaces to STREAM. */ static void -do_indent (unsigned int len) +do_indent (FILE *stream, unsigned int len) { unsigned int i; for (i = 0; i < len; i++) - fprintf (stderr, " "); + fprintf (stream, " "); } -/* Print MSG to stderr, indenting to INDENT and wrapping at LIMIT. Assume - starting position is at INDENT. */ +/* Print MSG to STREAM, indenting to INDENT and wrapping at LIMIT. + Assume starting position is at INDENT. */ static void -wrap (unsigned int indent, unsigned int limit, const char *msg) +wrap (FILE *stream, unsigned int indent, unsigned int limit, const char *msg) { unsigned int len = indent; const char *s = msg; @@ -16253,13 +16253,13 @@ wrap (unsigned int indent, unsigned int limit, const char *msg) if (len + 1 /* space */ + word_len > limit) { - fprintf (stderr, "\n"); - do_indent (indent); + fprintf (stream, "\n"); + do_indent (stream ,indent); len = indent; } else if (len > indent) { - fprintf (stderr, " "); + fprintf (stream, " "); len += 1; } @@ -16267,10 +16267,10 @@ wrap (unsigned int indent, unsigned int limit, const char *msg) { const char *i; for (i = s; i < e; ++i) - fprintf (stderr, "%c", *i); + fprintf (stream, "%c", *i); } else - fprintf (stderr, "%s", s); + fprintf (stream, "%s", s); len += word_len; if (e == NULL) @@ -16280,10 +16280,10 @@ wrap (unsigned int indent, unsigned int limit, const char *msg) } } -/* Print OPTIONS_HELP of length H to stderr, indenting to help message to +/* Print OPTIONS_HELP of length H to STREAM, indenting to help message to INDENT an wrapping at LIMIT. */ static void -print_options_help (struct option_help *options_help, unsigned int n, +print_options_help (FILE *stream, struct option_help *options_help, unsigned int n, unsigned int indent, unsigned int limit) { unsigned len; @@ -16294,29 +16294,29 @@ print_options_help (struct option_help *options_help, unsigned int n, { len = 0; - fprintf (stderr, " "); + fprintf (stream, " "); len += 2; s = options_help[i].short_name; if (s) { - fprintf (stderr, "-%s", s); + fprintf (stream, "-%s", s); len += 2; } s = options_help[i].long_name; if (len == 4) { - fprintf (stderr, ", "); + fprintf (stream, ", "); len += 2; } - fprintf (stderr, "--%s", s); + fprintf (stream, "--%s", s); len += 2 + strlen (s); s = options_help[i].argument; if (s) { - fprintf (stderr, " %s", s); + fprintf (stream, " %s", s); len += 1 + strlen (s); } @@ -16325,104 +16325,102 @@ print_options_help (struct option_help *options_help, unsigned int n, { if (len > indent) { - fprintf (stderr, "\n"); - do_indent (indent); + fprintf (stream, "\n"); + do_indent (stream, indent); } else - do_indent (indent - len); + do_indent (stream, indent - len); len = indent; - wrap (indent, limit, s); + wrap (stream, indent, limit, s); } - fprintf (stderr, "\n"); + fprintf (stream, "\n"); s = options_help[i].default_value; if (s) { - do_indent (indent); - fprintf (stderr, "Default value: %s.\n", s); + do_indent (stream, indent); + fprintf (stream, "Default value: %s.\n", s); } } } /* Print usage and exit. */ static void -usage (void) +usage (const char *progname, int failing) { unsigned int n; unsigned int indent, limit; - const char *msg; + FILE *stream = failing ? stderr : stdout; - msg - = ("Usage:\n" - " dwz [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [FILES]\n" - " dwz [common options] -o OUTFILE FILE\n" - " dwz [ -v | -? ]"); - error (0, 0, "%s", msg); + fprintf (stream, + ("Usage:\n" + " %s [common options] [-h] [-m COMMONFILE] [-M NAME | -r] [FILES]\n" + " %s [common options] -o OUTFILE FILE\n" + " %s [ -v | -? ]\n"), + progname, progname, progname); indent = 30; limit = 80; - fprintf (stderr, "Common options:\n"); + fprintf (stream, "Common options:\n"); n = (sizeof (dwz_common_options_help) / sizeof (dwz_common_options_help[0])); - print_options_help (dwz_common_options_help, n, indent, limit); + print_options_help (stream, dwz_common_options_help, n, indent, limit); - fprintf (stderr, "Single-file options:\n"); + fprintf (stream, "Single-file options:\n"); n = (sizeof (dwz_single_file_options_help) / sizeof (dwz_single_file_options_help[0])); - print_options_help (dwz_single_file_options_help, n, indent, limit); + print_options_help (stream, dwz_single_file_options_help, n, indent, limit); - fprintf (stderr, "Multi-file options:\n"); + fprintf (stream, "Multi-file options:\n"); n = (sizeof (dwz_multi_file_options_help) / sizeof (dwz_multi_file_options_help[0])); - print_options_help (dwz_multi_file_options_help, n, indent, limit); + print_options_help (stream, dwz_multi_file_options_help, n, indent, limit); - fprintf (stderr, "Miscellaneous options:\n"); + fprintf (stream, "Miscellaneous options:\n"); n = (sizeof (dwz_misc_options_help) / sizeof (dwz_misc_options_help[0])); - print_options_help (dwz_misc_options_help, n, indent, limit); + print_options_help (stream, dwz_misc_options_help, n, indent, limit); #if DEVEL - fprintf (stderr, "Development options:\n"); - msg - = (" --devel-trace\n" - " --devel-progress\n" - " --devel-stats\n" - " --devel-ignore-size\n" - " --devel-ignore-locus\n" - " --devel-force\n" - " --devel-save-temps\n" - " --devel-dump-checksum\n" - " --devel-dump-dies\n" - " --devel-dump-dups\n" - " --devel-dump-pus\n" - " --devel-unoptimized-multifile\n" - " --devel-verify-dups\n" - " --devel-verify-edges\n" - " --devel-dump-edges\n" - " --devel-partition-dups-opt\n" - " --devel-die-count-method\n" - " --devel-deduplication-mode={none,intra-cu,inter-cu}\n" - " --devel-uni-lang / --devel-no-uni-lang\n" - " --devel-gen-cu / --devel-no-gen-cu\n"); - fprintf (stderr, "%s", msg); + fprintf (stream, "Development options:\n"); + fprintf (stream, "%s", + (" --devel-trace\n" + " --devel-progress\n" + " --devel-stats\n" + " --devel-ignore-size\n" + " --devel-ignore-locus\n" + " --devel-force\n" + " --devel-save-temps\n" + " --devel-dump-checksum\n" + " --devel-dump-dies\n" + " --devel-dump-dups\n" + " --devel-dump-pus\n" + " --devel-unoptimized-multifile\n" + " --devel-verify-dups\n" + " --devel-verify-edges\n" + " --devel-dump-edges\n" + " --devel-partition-dups-opt\n" + " --devel-die-count-method\n" + " --devel-deduplication-mode={none,intra-cu,inter-cu}\n" + " --devel-uni-lang / --devel-no-uni-lang\n" + " --devel-gen-cu / --devel-no-gen-cu\n")); #endif - exit (1); + exit (failing); } /* Print version and exit. */ static void version (void) { - fprintf (stderr, - "dwz version " DWZ_VERSION "\n" - "Copyright (C) " RH_YEARS " Red Hat, Inc.\n" - "Copyright (C) " FSF_YEARS " Free Software Foundation, Inc.\n" - "Copyright (C) " SUSE_YEARS " SUSE LLC.\n" - "This program is free software; you may redistribute it under the terms of\n" - "the GNU General Public License version 3 or (at your option) any later version.\n" - "This program has absolutely no warranty.\n"); + printf ("dwz version " DWZ_VERSION "\n" + "Copyright (C) " RH_YEARS " Red Hat, Inc.\n" + "Copyright (C) " FSF_YEARS " Free Software Foundation, Inc.\n" + "Copyright (C) " SUSE_YEARS " SUSE LLC.\n" + "This program is free software; you may redistribute it under the terms of\n" + "the GNU General Public License version 3 or (at your option) any later version.\n" + "This program has absolutely no warranty.\n"); exit (0); } @@ -16443,7 +16441,7 @@ main (int argc, char *argv[]) while (1) { - int option_index; + int option_index = -1; int c = getopt_long (argc, argv, "m:o:qhl:L:M:r?v5", dwz_options, &option_index); if (c == -1) break; @@ -16451,7 +16449,7 @@ main (int argc, char *argv[]) { default: case '?': - usage (); + usage (argv[0], option_index == -1); break; case 0: diff --git a/testsuite/dwz.tests/version.sh b/testsuite/dwz.tests/version.sh index ccc4524..fb000c7 100644 --- a/testsuite/dwz.tests/version.sh +++ b/testsuite/dwz.tests/version.sh @@ -1,9 +1,9 @@ cp $execs/hello 1 -dwz -v 1 2> dwz.err +dwz -v 1 > dwz.out 2> /dev/null -grep -q "dwz version" dwz.err +grep -q "dwz version" dwz.out cmp 1 $execs/hello -rm -f 1 dwz.err +rm -f 1 dwz.out -- 2.26.2