1 /* gnu-h-v.h --- 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 <getopt.h>
22 
23 /* Clear ‘optind’ and ‘opterr’ then call ‘getopt_long’, arranging
24    to do not permute ‘argv’.  Return what ‘getopt_long’ returns.  */
25 extern int
26 nice_getopt (int argc, char **argv, const struct option *longopts)
27   ALL_NONNULL;
28 
29 #define DV_ONLY   0
30 #define DV_WARN   1
31 #define DV_EXIT   2
32 
33 /* Display the version blurb to stdout, starting with:
34    | NAME (GNU RCS) PACKAGE_VERSION
35    | ...
36    and ending with newline.  NAME is the value of ‘prog->name’.
37    FLAGS is the logical-OR of:
38    | DV_ONLY -- don't do anything special
39    | DV_WARN -- warn that this usage is obsolete (for ‘-V’);
40    |            suggest using --version, instead
41    | DV_EXIT -- finish w/ ‘exit (EXIT_SUCCESS)’
42    The default is 0.  */
43 extern void
44 display_version (struct program const *prog, int flags)
45   ALL_NONNULL;
46 
47 /* If ARGC is less than 2, do nothing.
48    If ARGV[1] is "--version", use ‘display_version’ and exit successfully.
49    If ARGV[1] is "--help", display the help blurb, starting with:
50    | NAME HELP
51    and exit successfully.  NAME is the value of ‘prog->name’,
52    while HELP is the value of ‘prog->help’.  */
53 extern void
54 check_hv (int argc, char **argv, struct program const *prog)
55   ALL_NONNULL;
56 
57 /* Idioms.  */
58 
59 #define NICE_OPT(name,value)  \
60   { name, no_argument, NULL, value }
61 
62 #define NO_MORE_OPTIONS \
63   {NULL, 0, NULL, 0}
64 
65 #define CHECK_HV(cmd)  do                       \
66     {                                           \
67       program.invoke = argv[0];                 \
68       program.name = cmd;                       \
69       check_hv (argc, argv, &program);          \
70     }                                           \
71   while (0)
72 
73 #define DECLARE_PROGRAM(prog,__tyag)            \
74   static struct program program =               \
75     {                                           \
76       .desc = prog ## _blurb,                   \
77       .help = prog ## _help,                    \
78       .tyag = __tyag                            \
79     }
80 
81 /* gnu-h-v.h ends here */
82