Blame lib/xstrtol-error.c

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