Remove K&R cruft.
[gnulib.git] / lib / long-options.c
1 /* Utility to accept --help and --version options as unobtrusively as possible.
2
3    Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Jim Meyering.  */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "long-options.h"
27
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <stdlib.h>
31
32 #include "version-etc.h"
33
34 static struct option const long_options[] =
35 {
36   {"help", no_argument, 0, 'h'},
37   {"version", no_argument, 0, 'v'},
38   {0, 0, 0, 0}
39 };
40
41 /* Process long options --help and --version, but only if argc == 2.
42    Be careful not to gobble up `--'.  */
43
44 void
45 parse_long_options (int argc,
46                     char **argv,
47                     const char *command_name,
48                     const char *package,
49                     const char *version,
50                     const char *authors,
51                     void (*usage_func)())
52 {
53   int c;
54   int saved_opterr;
55
56   saved_opterr = opterr;
57
58   /* Don't print an error message for unrecognized options.  */
59   opterr = 0;
60
61   if (argc == 2
62       && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
63     {
64       switch (c)
65         {
66         case 'h':
67           (*usage_func) (0);
68
69         case 'v':
70           version_etc (stdout, command_name, package, version, authors);
71           exit (0);
72
73         default:
74           /* Don't process any other long-named options.  */
75           break;
76         }
77     }
78
79   /* Restore previous value.  */
80   opterr = saved_opterr;
81
82   /* Reset this to zero so that getopt internals get initialized from
83      the probably-new parameters when/if getopt is called later.  */
84   optind = 0;
85 }