af_alg: Improve comments.
[gnulib.git] / lib / xstrtol-error.c
blob8ea6830b5086acbac289f9852eeb015776893c71
1 /* A more useful interface to strtol.
3 Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2018 Free Software
4 Foundation, Inc.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <config.h>
20 #include "xstrtol.h"
22 #include <stdlib.h>
24 #include "error.h"
25 #include "exitfail.h"
26 #include "gettext.h"
28 #define N_(msgid) msgid
30 /* Report an error for an invalid integer in an option argument.
32 ERR is the error code returned by one of the xstrto* functions.
34 Use OPT_IDX to decide whether to print the short option string "C"
35 or "-C" or a long option string derived from LONG_OPTION. OPT_IDX
36 is -2 if the short option "C" was used, without any leading "-"; it
37 is -1 if the short option "-C" was used; otherwise it is an index
38 into LONG_OPTIONS, which should have a name preceded by two '-'
39 characters.
41 ARG is the option-argument containing the integer.
43 After reporting an error, exit with status EXIT_STATUS if it is
44 nonzero. */
46 static void
47 xstrtol_error (enum strtol_error err,
48 int opt_idx, char c, struct option const *long_options,
49 char const *arg,
50 int exit_status)
52 char const *hyphens = "--";
53 char const *msgid;
54 char const *option;
55 char option_buffer[2];
57 switch (err)
59 default:
60 abort ();
62 case LONGINT_INVALID:
63 msgid = N_("invalid %s%s argument '%s'");
64 break;
66 case LONGINT_INVALID_SUFFIX_CHAR:
67 case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW:
68 msgid = N_("invalid suffix in %s%s argument '%s'");
69 break;
71 case LONGINT_OVERFLOW:
72 msgid = N_("%s%s argument '%s' too large");
73 break;
76 if (opt_idx < 0)
78 hyphens -= opt_idx;
79 option_buffer[0] = c;
80 option_buffer[1] = '\0';
81 option = option_buffer;
83 else
84 option = long_options[opt_idx].name;
86 error (exit_status, 0, gettext (msgid), hyphens, option, arg);
89 /* Like xstrtol_error, except exit with a failure status. */
91 void
92 xstrtol_fatal (enum strtol_error err,
93 int opt_idx, char c, struct option const *long_options,
94 char const *arg)
96 xstrtol_error (err, opt_idx, c, long_options, arg, exit_failure);
97 abort ();