Blame lib/xstrtol-error.c

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