Blame lib/xstrtol-error.c

Packit Service a2489d
/* A more useful interface to strtol.
Packit Service a2489d
Packit Service a2489d
   Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2018 Free Software
Packit Service a2489d
   Foundation, Inc.
Packit Service a2489d
Packit Service a2489d
   This program is free software: you can redistribute it and/or modify
Packit Service a2489d
   it under the terms of the GNU General Public License as published by
Packit Service a2489d
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
   (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   This program is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
   GNU General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public License
Packit Service a2489d
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
#include "xstrtol.h"
Packit Service a2489d
Packit Service a2489d
#include <stdlib.h>
Packit Service a2489d
Packit Service a2489d
#include "error.h"
Packit Service a2489d
#include "exitfail.h"
Packit Service a2489d
#include "gettext.h"
Packit Service a2489d
Packit Service a2489d
#define N_(msgid) msgid
Packit Service a2489d
Packit Service a2489d
/* Report an error for an invalid integer in an option argument.
Packit Service a2489d
Packit Service a2489d
   ERR is the error code returned by one of the xstrto* functions.
Packit Service a2489d
Packit Service a2489d
   Use OPT_IDX to decide whether to print the short option string "C"
Packit Service a2489d
   or "-C" or a long option string derived from LONG_OPTION.  OPT_IDX
Packit Service a2489d
   is -2 if the short option "C" was used, without any leading "-"; it
Packit Service a2489d
   is -1 if the short option "-C" was used; otherwise it is an index
Packit Service a2489d
   into LONG_OPTIONS, which should have a name preceded by two '-'
Packit Service a2489d
   characters.
Packit Service a2489d
Packit Service a2489d
   ARG is the option-argument containing the integer.
Packit Service a2489d
Packit Service a2489d
   After reporting an error, exit with status EXIT_STATUS if it is
Packit Service a2489d
   nonzero.  */
Packit Service a2489d
Packit Service a2489d
static void
Packit Service a2489d
xstrtol_error (enum strtol_error err,
Packit Service a2489d
               int opt_idx, char c, struct option const *long_options,
Packit Service a2489d
               char const *arg,
Packit Service a2489d
               int exit_status)
Packit Service a2489d
{
Packit Service a2489d
  char const *hyphens = "--";
Packit Service a2489d
  char const *msgid;
Packit Service a2489d
  char const *option;
Packit Service a2489d
  char option_buffer[2];
Packit Service a2489d
Packit Service a2489d
  switch (err)
Packit Service a2489d
    {
Packit Service a2489d
    default:
Packit Service a2489d
      abort ();
Packit Service a2489d
Packit Service a2489d
    case LONGINT_INVALID:
Packit Service a2489d
      msgid = N_("invalid %s%s argument '%s'");
Packit Service a2489d
      break;
Packit Service a2489d
Packit Service a2489d
    case LONGINT_INVALID_SUFFIX_CHAR:
Packit Service a2489d
    case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW:
Packit Service a2489d
      msgid = N_("invalid suffix in %s%s argument '%s'");
Packit Service a2489d
      break;
Packit Service a2489d
Packit Service a2489d
    case LONGINT_OVERFLOW:
Packit Service a2489d
      msgid = N_("%s%s argument '%s' too large");
Packit Service a2489d
      break;
Packit Service a2489d
    }
Packit Service a2489d
Packit Service a2489d
  if (opt_idx < 0)
Packit Service a2489d
    {
Packit Service a2489d
      hyphens -= opt_idx;
Packit Service a2489d
      option_buffer[0] = c;
Packit Service a2489d
      option_buffer[1] = '\0';
Packit Service a2489d
      option = option_buffer;
Packit Service a2489d
    }
Packit Service a2489d
  else
Packit Service a2489d
    option = long_options[opt_idx].name;
Packit Service a2489d
Packit Service a2489d
  error (exit_status, 0, gettext (msgid), hyphens, option, arg);
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
/* Like xstrtol_error, except exit with a failure status.  */
Packit Service a2489d
Packit Service a2489d
void
Packit Service a2489d
xstrtol_fatal (enum strtol_error err,
Packit Service a2489d
               int opt_idx, char c, struct option const *long_options,
Packit Service a2489d
               char const *arg)
Packit Service a2489d
{
Packit Service a2489d
  xstrtol_error (err, opt_idx, c, long_options, arg, exit_failure);
Packit Service a2489d
  abort ();
Packit Service a2489d
}