1 /* gnu-h-v.c --- GNUish --help and --version handling
2 
3    Copyright (C) 2010-2020 Thien-Thi Nguyen
4 
5    This file is part of GNU RCS.
6 
7    GNU RCS is free software: you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    GNU RCS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty
14    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15    See the GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "base.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "gnu-h-v.h"
26 #include "b-complain.h"
27 
28 int
nice_getopt(int argc,char ** argv,const struct option * longopts)29 nice_getopt (int argc, char **argv, const struct option *longopts)
30 {
31   /* Support multiple calls.  */
32   optind = 0;
33   /* Don't display error message.  */
34   opterr = 0;
35   /* Do it!  */
36   return getopt_long
37     (argc, argv,
38      "+",                       /* stop at first non-option */
39      longopts, NULL);
40 }
41 
42 #define COMMAND_VERSION                                         \
43   (" (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"                  \
44    "Copyright (C) 2010-2020 Thien-Thi Nguyen\n"                 \
45    "Copyright (C) 1990-1995 Paul Eggert\n"                      \
46    "Copyright (C) 1982,1988,1989 Walter F. Tichy, Purdue CS\n"  \
47    "License GPLv3+: GNU GPL version 3 or later"                 \
48    " <http://gnu.org/licenses/gpl.html>\n"                      \
49    "This is free software: you are free"                        \
50    " to change and redistribute it.\n"                          \
51    "There is NO WARRANTY, to the extent permitted by law.\n")
52 
53 #define AB(blurb,uri)    blurb ": <" uri ">\n"
54 #define GNU(blurb,rest)  AB (blurb, "http://www.gnu.org/" rest)
55 
56 #define BUGME                                           \
57   ("\n"                                                 \
58    AB ("Report bugs to", PACKAGE_BUGREPORT)             \
59    GNU ("RCS home page", "software/rcs/")               \
60    GNU ("General help using GNU software", "gethelp/"))
61 
62 void
display_version(struct program const * prog,int flags)63 display_version (struct program const *prog, int flags)
64 {
65   if (DV_WARN & flags)
66     PWARN ("-V is obsolete; instead, use --version");
67   printf ("%s%s", prog->name, COMMAND_VERSION);
68   if (DV_EXIT & flags)
69     exit (EXIT_SUCCESS);
70 }
71 
72 enum hv_option_values
73   {
74     hv_help,
75     hv_version
76   };
77 
78 static struct option const ok[] =
79   {
80     NICE_OPT ("help",    hv_help),
81     NICE_OPT ("version", hv_version),
82     NO_MORE_OPTIONS
83   };
84 
85 void
check_hv(int argc,char ** argv,struct program const * prog)86 check_hv (int argc, char **argv, struct program const *prog)
87 {
88   if (1 >= argc)
89     return;
90 
91   switch (nice_getopt (argc, argv, ok))
92     {
93     case hv_help:
94       {
95         char usage[128];
96         int nl;
97 
98         snprintf (usage, 128, "%s", prog->help);
99         nl = strchr (usage, '\n') - usage;
100         usage[nl] = '\0';
101 
102         printf ("Usage: %s %s\n\n%s\n%s%s",
103                 prog->name, usage,
104                 prog->desc,
105                 prog->help + nl,
106                 BUGME);
107         exit (EXIT_SUCCESS);
108       }
109     case hv_version:
110       display_version (prog, DV_EXIT);
111     }
112 }
113 
114 /* gnu-h-v.c ends here */
115