4
0

xstrtol-error.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2015 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include "xstrtol.h"
  16. #include <stdlib.h>
  17. #include "error.h"
  18. #include "exitfail.h"
  19. #include "gettext.h"
  20. #define N_(msgid) msgid
  21. /* Report an error for an invalid integer in an option argument.
  22. ERR is the error code returned by one of the xstrto* functions.
  23. Use OPT_IDX to decide whether to print the short option string "C"
  24. or "-C" or a long option string derived from LONG_OPTION. OPT_IDX
  25. is -2 if the short option "C" was used, without any leading "-"; it
  26. is -1 if the short option "-C" was used; otherwise it is an index
  27. into LONG_OPTIONS, which should have a name preceded by two '-'
  28. characters.
  29. ARG is the option-argument containing the integer.
  30. After reporting an error, exit with status EXIT_STATUS if it is
  31. nonzero. */
  32. static void
  33. xstrtol_error (enum strtol_error err,
  34. int opt_idx, char c, struct option const *long_options,
  35. char const *arg,
  36. int exit_status)
  37. {
  38. char const *hyphens = "--";
  39. char const *msgid;
  40. char const *option;
  41. char option_buffer[2];
  42. switch (err)
  43. {
  44. default:
  45. abort ();
  46. case LONGINT_INVALID:
  47. msgid = N_("invalid %s%s argument '%s'");
  48. break;
  49. case LONGINT_INVALID_SUFFIX_CHAR:
  50. case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW:
  51. msgid = N_("invalid suffix in %s%s argument '%s'");
  52. break;
  53. case LONGINT_OVERFLOW:
  54. msgid = N_("%s%s argument '%s' too large");
  55. break;
  56. }
  57. if (opt_idx < 0)
  58. {
  59. hyphens -= opt_idx;
  60. option_buffer[0] = c;
  61. option_buffer[1] = '\0';
  62. option = option_buffer;
  63. }
  64. else
  65. option = long_options[opt_idx].name;
  66. error (exit_status, 0, gettext (msgid), hyphens, option, arg);
  67. }
  68. /* Like xstrtol_error, except exit with a failure status. */
  69. void
  70. xstrtol_fatal (enum strtol_error err,
  71. int opt_idx, char c, struct option const *long_options,
  72. char const *arg)
  73. {
  74. xstrtol_error (err, opt_idx, c, long_options, arg, exit_failure);
  75. abort ();
  76. }