Mercurial > hg > octave-kai > gnulib-hg
annotate lib/long-options.c @ 6836:e2c19cb657e7
Tweak for FreeBSD.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 17 Jun 2006 20:02:16 +0000 |
parents | 96c32553b4c6 |
children | 8a1a9361108c |
rev | line source |
---|---|
317 | 1 /* Utility to accept --help and --version options as unobtrusively as possible. |
4659 | 2 |
5813 | 3 Copyright (C) 1993, 1994, 1998, 1999, 2000, 2002, 2003, 2004, 2005 Free |
4659 | 4 Software Foundation, Inc. |
317 | 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 | |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
17 along with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5813
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
317 | 19 |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
446
diff
changeset
|
20 /* Written by Jim Meyering. */ |
317 | 21 |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
1199
e7da0dec4cd2
(parse_long_options): Check for write error to stdout before exiting.
Jim Meyering <jim@meyering.net>
parents:
880
diff
changeset
|
23 # include <config.h> |
317 | 24 #endif |
25 | |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
26 /* Specification. */ |
4659 | 27 #include "long-options.h" |
28 | |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
29 #include <stdarg.h> |
317 | 30 #include <stdio.h> |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
31 #include <stdlib.h> |
317 | 32 #include <getopt.h> |
2669
3668079f961d
Include <stdlib.h>, for declaration of exit.
Jim Meyering <jim@meyering.net>
parents:
2507
diff
changeset
|
33 |
1755
f486d0e61ad0
(parse_long_options): Remove version-, copyright-,
Jim Meyering <jim@meyering.net>
parents:
1727
diff
changeset
|
34 #include "version-etc.h" |
317 | 35 |
36 static struct option const long_options[] = | |
37 { | |
5813 | 38 {"help", no_argument, NULL, 'h'}, |
39 {"version", no_argument, NULL, 'v'}, | |
40 {NULL, 0, NULL, 0} | |
317 | 41 }; |
42 | |
43 /* Process long options --help and --version, but only if argc == 2. | |
44 Be careful not to gobble up `--'. */ | |
45 | |
46 void | |
1719 | 47 parse_long_options (int argc, |
48 char **argv, | |
49 const char *command_name, | |
50 const char *package, | |
51 const char *version, | |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4771
diff
changeset
|
52 void (*usage_func) (int), |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
53 /* const char *author1, ...*/ ...) |
317 | 54 { |
55 int c; | |
56 int saved_opterr; | |
57 | |
58 saved_opterr = opterr; | |
59 | |
60 /* Don't print an error message for unrecognized options. */ | |
61 opterr = 0; | |
62 | |
63 if (argc == 2 | |
880
3bee11a1c21d
(parse_long_options): Compare getopt_long return
Jim Meyering <jim@meyering.net>
parents:
869
diff
changeset
|
64 && (c = getopt_long (argc, argv, "+", long_options, NULL)) != -1) |
317 | 65 { |
66 switch (c) | |
67 { | |
68 case 'h': | |
4934
0ffd1692e066
Exit-status fixes from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4771
diff
changeset
|
69 (*usage_func) (EXIT_SUCCESS); |
317 | 70 |
71 case 'v': | |
4771
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
72 { |
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
73 va_list authors; |
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
74 va_start (authors, usage_func); |
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
75 version_etc_va (stdout, command_name, package, version, authors); |
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
76 exit (0); |
ffc6ac581833
Merge version-etc-2 back into version-etc: Better way to internationalize
Bruno Haible <bruno@clisp.org>
parents:
4659
diff
changeset
|
77 } |
446
8461c3b8595a
Remove trailing white space.
Jim Meyering <jim@meyering.net>
parents:
317
diff
changeset
|
78 |
317 | 79 default: |
80 /* Don't process any other long-named options. */ | |
81 break; | |
82 } | |
83 } | |
84 | |
85 /* Restore previous value. */ | |
86 opterr = saved_opterr; | |
87 | |
869
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
88 /* Reset this to zero so that getopt internals get initialized from |
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
89 the probably-new parameters when/if getopt is called later. */ |
ca8a87089389
(parse_long_options): Reset optind to zero
Jim Meyering <jim@meyering.net>
parents:
706
diff
changeset
|
90 optind = 0; |
317 | 91 } |