|
Packit |
fc043f |
/* gpg-error.c - Determining gpg-error error codes.
|
|
Packit |
fc043f |
Copyright (C) 2004, 2016 g10 Code GmbH
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
This file is part of libgpg-error.
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
libgpg-error is free software; you can redistribute it and/or
|
|
Packit |
fc043f |
modify it under the terms of the GNU Lesser General Public License
|
|
Packit |
fc043f |
as published by the Free Software Foundation; either version 2.1 of
|
|
Packit |
fc043f |
the License, or (at your option) any later version.
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
libgpg-error is distributed in the hope that it will be useful, but
|
|
Packit |
fc043f |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
fc043f |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
fc043f |
Lesser General Public License for more details.
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
fc043f |
License along with libgpg-error; if not, write to the Free
|
|
Packit |
fc043f |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
Packit |
fc043f |
02111-1307, USA. */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#if HAVE_CONFIG_H
|
|
Packit |
fc043f |
#include <config.h>
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include <stddef.h>
|
|
Packit |
fc043f |
#include <stdlib.h>
|
|
Packit |
fc043f |
#include <string.h>
|
|
Packit |
fc043f |
#include <errno.h>
|
|
Packit |
fc043f |
#include <limits.h>
|
|
Packit |
fc043f |
#include <stdio.h>
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#ifdef HAVE_LOCALE_H
|
|
Packit |
fc043f |
# include <locale.h>
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
#ifdef ENABLE_NLS
|
|
Packit |
fc043f |
#ifdef HAVE_W32_SYSTEM
|
|
Packit |
fc043f |
# include "gettext.h"
|
|
Packit |
fc043f |
#else
|
|
Packit |
fc043f |
# include <libintl.h>
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
# define _(a) gettext (a)
|
|
Packit |
fc043f |
# ifdef gettext_noop
|
|
Packit |
fc043f |
# define N_(a) gettext_noop (a)
|
|
Packit |
fc043f |
# else
|
|
Packit |
fc043f |
# define N_(a) (a)
|
|
Packit |
fc043f |
# endif
|
|
Packit |
fc043f |
#else
|
|
Packit |
fc043f |
# define _(a) (a)
|
|
Packit |
fc043f |
# define N_(a) (a)
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include <gpg-error.h>
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#if HAVE_W32_SYSTEM
|
|
Packit |
fc043f |
/* The implementation follows below. */
|
|
Packit |
fc043f |
static char *get_locale_dir (void);
|
|
Packit |
fc043f |
static void drop_locale_dir (char *locale_dir);
|
|
Packit |
fc043f |
#else
|
|
Packit |
fc043f |
#define get_locale_dir() LOCALEDIR
|
|
Packit |
fc043f |
#define drop_locale_dir(dir)
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static void
|
|
Packit |
fc043f |
i18n_init (void)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
#ifdef ENABLE_NLS
|
|
Packit |
fc043f |
char *locale_dir;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#ifdef HAVE_LC_MESSAGES
|
|
Packit |
fc043f |
setlocale (LC_TIME, "");
|
|
Packit |
fc043f |
setlocale (LC_MESSAGES, "");
|
|
Packit |
fc043f |
#else
|
|
Packit |
fc043f |
# ifndef HAVE_W32_SYSTEM
|
|
Packit |
fc043f |
setlocale (LC_ALL, "" );
|
|
Packit |
fc043f |
# endif
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
/* Note that for this program we would only need the textdomain call
|
|
Packit |
fc043f |
because libgpg-error already initializes itself to its locale dir
|
|
Packit |
fc043f |
(via gpg_err_init or a constructor). However this is only done
|
|
Packit |
fc043f |
for the static standard locale and thus if the above setlocale
|
|
Packit |
fc043f |
calls select a different locale the bindtext below will do
|
|
Packit |
fc043f |
something else. */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
locale_dir = get_locale_dir ();
|
|
Packit |
fc043f |
if (locale_dir)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
bindtextdomain (PACKAGE, locale_dir);
|
|
Packit |
fc043f |
drop_locale_dir (locale_dir);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
textdomain (PACKAGE);
|
|
Packit |
fc043f |
#endif
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#ifdef HAVE_W32_SYSTEM
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#include <windows.h>
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static char *
|
|
Packit |
fc043f |
get_locale_dir (void)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
static wchar_t moddir[MAX_PATH+5];
|
|
Packit |
fc043f |
char *result, *p;
|
|
Packit |
fc043f |
int nbytes;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!GetModuleFileNameW (NULL, moddir, MAX_PATH))
|
|
Packit |
fc043f |
*moddir = 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#define SLDIR "\\share\\locale"
|
|
Packit |
fc043f |
if (*moddir)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
nbytes = WideCharToMultiByte (CP_UTF8, 0, moddir, -1, NULL, 0, NULL, NULL);
|
|
Packit |
fc043f |
if (nbytes < 0)
|
|
Packit |
fc043f |
return NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
result = malloc (nbytes + strlen (SLDIR) + 1);
|
|
Packit |
fc043f |
if (result)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
nbytes = WideCharToMultiByte (CP_UTF8, 0, moddir, -1,
|
|
Packit |
fc043f |
result, nbytes, NULL, NULL);
|
|
Packit |
fc043f |
if (nbytes < 0)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
free (result);
|
|
Packit |
fc043f |
result = NULL;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
p = strrchr (result, '\\');
|
|
Packit |
fc043f |
if (p)
|
|
Packit |
fc043f |
*p = 0;
|
|
Packit |
fc043f |
/* If we are installed below "bin" strip that part and
|
|
Packit |
fc043f |
use the top directory instead. */
|
|
Packit |
fc043f |
p = strrchr (result, '\\');
|
|
Packit |
fc043f |
if (p && !strcmp (p+1, "bin"))
|
|
Packit |
fc043f |
*p = 0;
|
|
Packit |
fc043f |
/* Append the static part. */
|
|
Packit |
fc043f |
strcat (result, SLDIR);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else /* Use the old default value. */
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
result = malloc (10 + strlen (SLDIR) + 1);
|
|
Packit |
fc043f |
if (result)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
strcpy (result, "c:\\gnupg");
|
|
Packit |
fc043f |
strcat (result, SLDIR);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
#undef SLDIR
|
|
Packit |
fc043f |
return result;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static void
|
|
Packit |
fc043f |
drop_locale_dir (char *locale_dir)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
free (locale_dir);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
#endif /* HAVE_W32_SYSTEM */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
const char *gpg_strerror_sym (gpg_error_t err);
|
|
Packit |
fc043f |
const char *gpg_strsource_sym (gpg_error_t err);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
get_err_from_number (char *str, gpg_error_t *err)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
unsigned long nr;
|
|
Packit |
fc043f |
char *tail;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
gpg_err_set_errno (0);
|
|
Packit |
fc043f |
nr = strtoul (str, &tail, 0);
|
|
Packit |
fc043f |
if (errno)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (nr > UINT_MAX)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (*tail)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
unsigned long cnr = strtoul (tail + 1, &tail, 0);
|
|
Packit |
fc043f |
if (errno || *tail)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (nr >= GPG_ERR_SOURCE_DIM || cnr >= GPG_ERR_CODE_DIM)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
nr = gpg_err_make (nr, cnr);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*err = (unsigned int) nr;
|
|
Packit |
fc043f |
return 1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
get_err_from_symbol_one (char *str, gpg_error_t *err,
|
|
Packit |
fc043f |
int *have_source, int *have_code)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
static const char src_prefix[] = "GPG_ERR_SOURCE_";
|
|
Packit |
fc043f |
static const char code_prefix[] = "GPG_ERR_";
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!strncasecmp (src_prefix, str, sizeof (src_prefix) - 1))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_source_t src;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (*have_source)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
*have_source = 1;
|
|
Packit |
fc043f |
str += sizeof (src_prefix) - 1;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (src = 0; src < GPG_ERR_SOURCE_DIM; src++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
const char *src_sym;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
src_sym = gpg_strsource_sym (src << GPG_ERR_SOURCE_SHIFT);
|
|
Packit |
fc043f |
if (src_sym && !strcasecmp (str, src_sym + sizeof (src_prefix) - 1))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
*err |= src << GPG_ERR_SOURCE_SHIFT;
|
|
Packit |
fc043f |
return 1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else if (!strncasecmp (code_prefix, str, sizeof (code_prefix) - 1))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_code_t code;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (*have_code)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
*have_code = 1;
|
|
Packit |
fc043f |
str += sizeof (code_prefix) - 1;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (code = 0; code < GPG_ERR_CODE_DIM; code++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
const char *code_sym = gpg_strerror_sym (code);
|
|
Packit |
fc043f |
if (code_sym
|
|
Packit |
fc043f |
&& !strcasecmp (str, code_sym + sizeof (code_prefix) - 1))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
*err |= code;
|
|
Packit |
fc043f |
return 1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
get_err_from_symbol (char *str, gpg_error_t *err)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
char *str2 = str;
|
|
Packit |
fc043f |
int have_source = 0;
|
|
Packit |
fc043f |
int have_code = 0;
|
|
Packit |
fc043f |
int ret;
|
|
Packit |
fc043f |
char *saved_pos = NULL;
|
|
Packit |
fc043f |
char saved_char;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*err = 0;
|
|
Packit |
fc043f |
while (*str2 && ((*str2 >= 'A' && *str2 <= 'Z')
|
|
Packit |
fc043f |
|| (*str2 >= '0' && *str2 <= '9')
|
|
Packit |
fc043f |
|| *str2 == '_'))
|
|
Packit |
fc043f |
str2++;
|
|
Packit |
fc043f |
if (*str2)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
saved_pos = str2;
|
|
Packit |
fc043f |
saved_char = *str2;
|
|
Packit |
fc043f |
*str2 = '\0';
|
|
Packit |
fc043f |
str2++;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
str2 = NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
ret = get_err_from_symbol_one (str, err, &have_source, &have_code);
|
|
Packit |
fc043f |
if (ret && str2)
|
|
Packit |
fc043f |
ret = get_err_from_symbol_one (str2, err, &have_source, &have_code);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (saved_pos)
|
|
Packit |
fc043f |
*saved_pos = saved_char;
|
|
Packit |
fc043f |
return ret;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
get_err_from_str_one (char *str, gpg_error_t *err,
|
|
Packit |
fc043f |
int *have_source, int *have_code)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
gpg_err_source_t src;
|
|
Packit |
fc043f |
gpg_err_code_t code;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (src = 0; src < GPG_ERR_SOURCE_DIM; src++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
const char *src_str = gpg_strsource (src << GPG_ERR_SOURCE_SHIFT);
|
|
Packit |
fc043f |
if (src_str && !strcasecmp (str, src_str))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (*have_source)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*have_source = 1;
|
|
Packit |
fc043f |
*err |= src << GPG_ERR_SOURCE_SHIFT;
|
|
Packit |
fc043f |
return 1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (code = 0; code < GPG_ERR_CODE_DIM; code++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
const char *code_str = gpg_strerror (code);
|
|
Packit |
fc043f |
if (code_str && !strcasecmp (str, code_str))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (*have_code)
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*have_code = 1;
|
|
Packit |
fc043f |
*err |= code;
|
|
Packit |
fc043f |
return 1;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
return 0;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static int
|
|
Packit |
fc043f |
get_err_from_str (char *str, gpg_error_t *err)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
char *str2 = str;
|
|
Packit |
fc043f |
int have_source = 0;
|
|
Packit |
fc043f |
int have_code = 0;
|
|
Packit |
fc043f |
int ret;
|
|
Packit |
fc043f |
char *saved_pos = NULL;
|
|
Packit |
fc043f |
char saved_char = 0; /* (avoid warning) */
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
*err = 0;
|
|
Packit |
fc043f |
ret = get_err_from_str_one (str, err, &have_source, &have_code);
|
|
Packit |
fc043f |
if (ret)
|
|
Packit |
fc043f |
return ret;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
while (*str2 && ((*str2 >= 'A' && *str2 <= 'Z')
|
|
Packit |
fc043f |
|| (*str2 >= 'a' && *str2 <= 'z')
|
|
Packit |
fc043f |
|| (*str2 >= '0' && *str2 <= '9')
|
|
Packit |
fc043f |
|| *str2 == '_'))
|
|
Packit |
fc043f |
str2++;
|
|
Packit |
fc043f |
if (*str2)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
saved_pos = str2;
|
|
Packit |
fc043f |
saved_char = *str2;
|
|
Packit |
fc043f |
*((char *) str2) = '\0';
|
|
Packit |
fc043f |
str2++;
|
|
Packit |
fc043f |
while (*str2 && !((*str2 >= 'A' && *str2 <= 'Z')
|
|
Packit |
fc043f |
|| (*str2 >= 'a' && *str2 <= 'z')
|
|
Packit |
fc043f |
|| (*str2 >= '0' && *str2 <= '9')
|
|
Packit |
fc043f |
|| *str2 == '_'))
|
|
Packit |
fc043f |
str2++;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
str2 = NULL;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
ret = get_err_from_str_one (str, err, &have_source, &have_code);
|
|
Packit |
fc043f |
if (ret && str2)
|
|
Packit |
fc043f |
ret = get_err_from_str_one (str2, err, &have_source, &have_code);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (saved_pos)
|
|
Packit |
fc043f |
*saved_pos = saved_char;
|
|
Packit |
fc043f |
return ret;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static void
|
|
Packit |
fc043f |
print_desc (const char *symbol)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
static int initialized;
|
|
Packit |
fc043f |
static FILE *fp;
|
|
Packit |
fc043f |
char line[512];
|
|
Packit |
fc043f |
char *p;
|
|
Packit |
fc043f |
int indesc = 0;
|
|
Packit |
fc043f |
int blanklines = 0;
|
|
Packit |
fc043f |
int last_was_keyword = 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!symbol)
|
|
Packit |
fc043f |
return;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!initialized)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
initialized = 1;
|
|
Packit |
fc043f |
fp = fopen (PKGDATADIR "/errorref.txt", "r");
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
if (!fp)
|
|
Packit |
fc043f |
return;
|
|
Packit |
fc043f |
rewind (fp);
|
|
Packit |
fc043f |
while (fgets (line, sizeof line, fp))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (*line == '#')
|
|
Packit |
fc043f |
continue;
|
|
Packit |
fc043f |
if (*line && line[strlen(line)-1] == '\n')
|
|
Packit |
fc043f |
line[strlen(line)-1] = 0;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (!strncmp (line, "GPG_ERR_", 8))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (indesc == 1 && last_was_keyword)
|
|
Packit |
fc043f |
continue; /* Skip keywords immediately following a matched
|
|
Packit |
fc043f |
* keyword. */
|
|
Packit |
fc043f |
last_was_keyword = 1;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
indesc = 0;
|
|
Packit |
fc043f |
p = strchr (line, ' ');
|
|
Packit |
fc043f |
if (!p)
|
|
Packit |
fc043f |
continue;
|
|
Packit |
fc043f |
*p = 0;
|
|
Packit |
fc043f |
if (!strcmp (line, symbol))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
indesc = 1;
|
|
Packit |
fc043f |
continue; /* Skip this line. */
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
last_was_keyword = 0;
|
|
Packit |
fc043f |
if (!indesc)
|
|
Packit |
fc043f |
continue;
|
|
Packit |
fc043f |
if (indesc == 1 && !*line)
|
|
Packit |
fc043f |
continue; /* Skip leading empty lines in a description. */
|
|
Packit |
fc043f |
if (indesc == 1)
|
|
Packit |
fc043f |
putchar ('\n'); /* One leading empty line. */
|
|
Packit |
fc043f |
indesc = 2;
|
|
Packit |
fc043f |
if (!*line)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
blanklines++;
|
|
Packit |
fc043f |
continue;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
for (; blanklines; blanklines--)
|
|
Packit |
fc043f |
putchar ('\n');
|
|
Packit |
fc043f |
printf ("%s\n", line);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
putchar ('\n'); /* One trailing blank line. */
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
static const char *
|
|
Packit |
fc043f |
my_strusage (int level)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
const char *p;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
switch (level)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
case 9: p = "LGPL-2.1-or-later"; break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
case 11: p = "gpg-error"; break;
|
|
Packit |
fc043f |
case 12: p = PACKAGE_NAME; break;
|
|
Packit |
fc043f |
case 13: p = PACKAGE_VERSION; break;
|
|
Packit |
fc043f |
case 14: p = "Copyright (C) 2018 g10 Code GmbH"; break;
|
|
Packit |
fc043f |
case 19: p = _("Please report bugs to <https://bugs.gnupg.org>.\n"); break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
case 1:
|
|
Packit |
fc043f |
case 40:
|
|
Packit |
fc043f |
p = ("Usage: gpg-error [options] error-numbers");
|
|
Packit |
fc043f |
break;
|
|
Packit |
fc043f |
case 41:
|
|
Packit |
fc043f |
p = ("Map error numbers to strings and vice versa.\n");
|
|
Packit |
fc043f |
break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
case 42:
|
|
Packit |
fc043f |
p = "1"; /* Flag: print 40 as part of 41. */
|
|
Packit |
fc043f |
break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
default: p = NULL; break;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
return p;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
int
|
|
Packit |
fc043f |
main (int argc, char *argv[])
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
enum { CMD_DEFAULT = 0,
|
|
Packit |
fc043f |
CMD_LIB_VERSION = 501,
|
|
Packit |
fc043f |
CMD_LIST,
|
|
Packit |
fc043f |
CMD_DEFINES,
|
|
Packit |
fc043f |
OPT_DESC
|
|
Packit |
fc043f |
};
|
|
Packit |
fc043f |
static gpgrt_opt_t opts[] = {
|
|
Packit |
fc043f |
ARGPARSE_c (CMD_LIB_VERSION, "lib-version",
|
|
Packit |
fc043f |
"Print library version"),
|
|
Packit |
fc043f |
ARGPARSE_c (CMD_LIST, "list",
|
|
Packit |
fc043f |
"Print all error codes"),
|
|
Packit |
fc043f |
ARGPARSE_c (CMD_DEFINES, "defines",
|
|
Packit |
fc043f |
"Print all error codes as #define lines"),
|
|
Packit |
fc043f |
ARGPARSE_s_n (OPT_DESC, "desc",
|
|
Packit |
fc043f |
"Print with error description"),
|
|
Packit |
fc043f |
ARGPARSE_end()
|
|
Packit |
fc043f |
};
|
|
Packit |
fc043f |
gpgrt_argparse_t pargs = { &argc, &argv };
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
int i;
|
|
Packit |
fc043f |
int listmode = 0;
|
|
Packit |
fc043f |
int desc = 0;
|
|
Packit |
fc043f |
const char *source_sym;
|
|
Packit |
fc043f |
const char *error_sym;
|
|
Packit |
fc043f |
gpg_error_t err;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
gpgrt_init ();
|
|
Packit |
fc043f |
i18n_init ();
|
|
Packit |
fc043f |
gpgrt_set_strusage (my_strusage);
|
|
Packit |
fc043f |
gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
while (gpgrt_argparse (NULL, &pargs, opts))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
switch (pargs.r_opt)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
case CMD_LIB_VERSION: break;
|
|
Packit |
fc043f |
case CMD_LIST: listmode = 1; break;
|
|
Packit |
fc043f |
case CMD_DEFINES: listmode = 2; break;
|
|
Packit |
fc043f |
case OPT_DESC: desc = 1; break;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
default: pargs.err = ARGPARSE_PRINT_WARNING; break;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
gpgrt_argparse (NULL, &pargs, NULL); /* Free internal memory. */
|
|
Packit |
fc043f |
log_debug ("argc=%d listmode=%d\n", argc, listmode);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if ((argc && listmode) || (!argc && !listmode))
|
|
Packit |
fc043f |
gpgrt_usage (1);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
if (listmode == 1)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
for (i=0; i < GPG_ERR_SOURCE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
/* We use error code 1 because gpg_err_make requires a
|
|
Packit |
fc043f |
non-zero error code. */
|
|
Packit |
fc043f |
err = gpg_err_make (i, 1);
|
|
Packit |
fc043f |
err -= 1;
|
|
Packit |
fc043f |
source_sym = gpg_strsource_sym (err);
|
|
Packit |
fc043f |
if (source_sym)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
printf ("%u = (%u, -) = (%s, -) = (%s, -)\n",
|
|
Packit |
fc043f |
err, gpg_err_source (err),
|
|
Packit |
fc043f |
source_sym, gpg_strsource (err));
|
|
Packit |
fc043f |
if (desc)
|
|
Packit |
fc043f |
print_desc (source_sym);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
for (i=0; i < GPG_ERR_CODE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = gpg_err_make (GPG_ERR_SOURCE_UNKNOWN, i);
|
|
Packit |
fc043f |
error_sym = gpg_strerror_sym (err);
|
|
Packit |
fc043f |
if (error_sym)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
printf ("%u = (-, %u) = (-, %s) = (-, %s)\n",
|
|
Packit |
fc043f |
err, gpg_err_code (err),
|
|
Packit |
fc043f |
error_sym, gpg_strerror (err));
|
|
Packit |
fc043f |
if (desc)
|
|
Packit |
fc043f |
print_desc (error_sym);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else if (listmode == 2)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
int n, nmax;
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (i=0, nmax=0; i < GPG_ERR_SOURCE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = gpg_err_make (i, 1);
|
|
Packit |
fc043f |
source_sym = gpg_strsource_sym (err);
|
|
Packit |
fc043f |
if (source_sym)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
n = strlen (source_sym);
|
|
Packit |
fc043f |
if (n > nmax)
|
|
Packit |
fc043f |
nmax = n;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
for (i=0; i < GPG_ERR_SOURCE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = gpg_err_make (i, 1);
|
|
Packit |
fc043f |
source_sym = gpg_strsource_sym (err);
|
|
Packit |
fc043f |
if (source_sym)
|
|
Packit |
fc043f |
printf ("#define %-*s %3u\n", nmax,source_sym,gpg_err_source (err));
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
for (i=0, nmax = 0; i < GPG_ERR_CODE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = gpg_err_make (GPG_ERR_SOURCE_UNKNOWN, i);
|
|
Packit |
fc043f |
error_sym = gpg_strerror_sym (err);
|
|
Packit |
fc043f |
if (error_sym)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
n = strlen (error_sym);
|
|
Packit |
fc043f |
if (n > nmax)
|
|
Packit |
fc043f |
nmax = n;
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
for (i=0; i < GPG_ERR_CODE_DIM; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
err = gpg_err_make (GPG_ERR_SOURCE_UNKNOWN, i);
|
|
Packit |
fc043f |
error_sym = gpg_strerror_sym (err);
|
|
Packit |
fc043f |
if (error_sym)
|
|
Packit |
fc043f |
printf ("#define %-*s %5u\n", nmax, error_sym, gpg_err_code (err));
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else /* Standard mode. */
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
for (i=0; i < argc; i++)
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
if (get_err_from_number (argv[i], &err)
|
|
Packit |
fc043f |
|| get_err_from_symbol (argv[i], &err)
|
|
Packit |
fc043f |
|| get_err_from_str (argv[i], &err))
|
|
Packit |
fc043f |
{
|
|
Packit |
fc043f |
source_sym = gpg_strsource_sym (err);
|
|
Packit |
fc043f |
error_sym = gpg_strerror_sym (err);
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
printf ("%u = (%u, %u) = (%s, %s) = (%s, %s)\n",
|
|
Packit |
fc043f |
err, gpg_err_source (err), gpg_err_code (err),
|
|
Packit |
fc043f |
source_sym ? source_sym : "-", error_sym ? error_sym:"-",
|
|
Packit |
fc043f |
gpg_strsource (err), gpg_strerror (err));
|
|
Packit |
fc043f |
if (desc)
|
|
Packit |
fc043f |
print_desc (error_sym);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
else
|
|
Packit |
fc043f |
log_error (_("warning: could not recognize %s\n"), argv[i]);
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
}
|
|
Packit |
fc043f |
|
|
Packit |
fc043f |
exit (0);
|
|
Packit |
fc043f |
}
|