Blame lib/xstrtol-error.c

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