diff options
author | Marcus Brinkmann <[email protected]> | 2005-10-23 20:01:04 +0000 |
---|---|---|
committer | Marcus Brinkmann <[email protected]> | 2005-10-23 20:01:04 +0000 |
commit | b969dc048b6a1c801d176f70aa93941491c32bd2 (patch) | |
tree | 5a6be768dae4241a5c832bea12460ed3ae17860b | |
parent | 2005-10-20 Marcus Brinkmann <[email protected]> (diff) | |
download | libgpg-error-b969dc048b6a1c801d176f70aa93941491c32bd2.tar.gz libgpg-error-b969dc048b6a1c801d176f70aa93941491c32bd2.zip |
2005-10-23 Marcus Brinkmann <[email protected]>
* src/Makefile.am (libgpg_error_la_SOURCES): Add init.c.
* src/init.c: New file.
* src/gpg-error.h.in (gpg_error_init): New function.
* src/gpg-error.def: Add gpg_err_init.
* src/gpg-error.c: Remove SIMPLE_GETTEXT stuff.
(i18n_init): Don't bind text domain.
(main): Call gpg_err_init.
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | NEWS | 4 | ||||
-rw-r--r-- | src/Makefile.am | 5 | ||||
-rw-r--r-- | src/gpg-error.c | 53 | ||||
-rw-r--r-- | src/gpg-error.def | 2 | ||||
-rw-r--r-- | src/gpg-error.h.in | 7 | ||||
-rw-r--r-- | src/init.c | 224 |
7 files changed, 271 insertions, 34 deletions
@@ -1,3 +1,13 @@ +2005-10-23 Marcus Brinkmann <[email protected]> + + * src/Makefile.am (libgpg_error_la_SOURCES): Add init.c. + * src/init.c: New file. + * src/gpg-error.h.in (gpg_error_init): New function. + * src/gpg-error.def: Add gpg_err_init. + * src/gpg-error.c: Remove SIMPLE_GETTEXT stuff. + (i18n_init): Don't bind text domain. + (main): Call gpg_err_init. + 2005-10-20 Marcus Brinkmann <[email protected]> * configure.ac: Instead checking for windres and dlltool, invoke @@ -1,6 +1,9 @@ Noteworthy changes in version 1.2 (unreleased) ---------------------------------------------- + * New function gpg_err_init, which binds the locale directory to + the text domain. + * "./autogen.sh --build-w32" does now also build a DLL for W32. Translations are not yet provided for this platform. @@ -15,6 +18,7 @@ Noteworthy changes in version 1.2 (unreleased) GPG_ERR_UNKNOWN_EXTN NEW GPG_ERR_UNKNOWN_CRIT_EXTN NEW GPG_ERR_LOCKED NEW +gpg_err_init NEW ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Noteworthy changes in version 1.1 (2005-06-20) diff --git a/src/Makefile.am b/src/Makefile.am index ffaceb0..8930c01 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -85,12 +85,11 @@ libgpg_error_la_LDFLAGS = $(gpg_error_res_ldflag) -version-info \ $(no_undefined) $(export_symbols) libgpg_error_la_SOURCES = gpg-error.h gettext.h \ - strsource.c strerror.c code-to-errno.c code-from-errno.c - + init.c strsource.c strerror.c code-to-errno.c code-from-errno.c +libgpg_error_la_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" libgpg_error_la_LIBADD = @LTLIBINTL@ gpg_error_SOURCES = strsource-sym.c strerror-sym.c gpg-error.c -gpg_error_CPPFLAGS = -DLOCALEDIR=\"$(localedir)\" gpg_error_LDADD = ./libgpg-error.la err-sources.h: Makefile mkstrtable.awk err-sources.h.in diff --git a/src/gpg-error.c b/src/gpg-error.c index 566d66e..3693b0b 100644 --- a/src/gpg-error.c +++ b/src/gpg-error.c @@ -29,28 +29,21 @@ #include <limits.h> #include <stdio.h> -#ifdef USE_SIMPLE_GETTEXT - int set_gettext_file( const char *filename ); - const char *gettext( const char *msgid ); +#ifdef HAVE_LOCALE_H +# include <locale.h> +#endif +#ifdef ENABLE_NLS +# include <libintl.h> # define _(a) gettext (a) -# define N_(a) (a) -#else -# ifdef HAVE_LOCALE_H -# include <locale.h> -# endif -# ifdef ENABLE_NLS -# include <libintl.h> -# define _(a) gettext (a) -# ifdef gettext_noop -# define N_(a) gettext_noop (a) -# else -# define N_(a) (a) -# endif +# ifdef gettext_noop +# define N_(a) gettext_noop (a) # else -# define _(a) (a) # define N_(a) (a) # endif -#endif /*!USE_SIMPLE_GETTEXT*/ +#else +# define _(a) (a) +# define N_(a) (a) +#endif #include <gpg-error.h> @@ -249,22 +242,21 @@ get_err_from_str (char *str, gpg_error_t *err) static void -i18n_init(void) +i18n_init (void) { -#ifdef USE_SIMPLE_GETTEXT - set_gettext_file (PACKAGE); -#else -# ifdef ENABLE_NLS -# ifdef HAVE_LC_MESSAGES +#ifdef ENABLE_NLS +# ifdef HAVE_LC_MESSAGES setlocale (LC_TIME, ""); setlocale (LC_MESSAGES, ""); -# else +# else setlocale (LC_ALL, "" ); -# endif - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); # endif #endif + + /* We have the same text domain as the library. Thus, we don't need + to bind our text domain to our locale directory (this is done by + gpg_err_init). We just need to set our text domain. */ + textdomain (PACKAGE); } @@ -273,8 +265,9 @@ main (int argc, char *argv[]) { int i = 1; - /* Setup I18N. */ - i18n_init(); + gpg_err_init (); + + i18n_init (); if (argc == 1) { diff --git a/src/gpg-error.def b/src/gpg-error.def index 39b0143..6439112 100644 --- a/src/gpg-error.def +++ b/src/gpg-error.def @@ -5,4 +5,4 @@ EXPORTS gpg_strsource @3 gpg_err_code_from_errno @4 gpg_err_code_to_errno @5 - + gpg_err_init @6 diff --git a/src/gpg-error.h.in b/src/gpg-error.h.in index 66c5292..fdd2e61 100644 --- a/src/gpg-error.h.in +++ b/src/gpg-error.h.in @@ -114,6 +114,12 @@ typedef unsigned int gpg_error_t; #define GPG_ERR_SOURCE_SHIFT 24 +/* Initialization function. */ + +/* Initialize the library. This function should be run early. */ +gpg_error_t gpg_err_init (void); + + /* Constructor and accessor functions. */ /* Construct an error value from an error code and source. Within a @@ -209,4 +215,5 @@ gpg_error_from_errno (int err) } #endif + #endif /* GPG_ERROR_H */ diff --git a/src/init.c b/src/init.c new file mode 100644 index 0000000..e94767e --- /dev/null +++ b/src/init.c @@ -0,0 +1,224 @@ +/* init.c - Initialize the GnuPG error library. + Copyright (C) 2005 g10 Code GmbH + + This file is part of libgpg-error. + + libgpg-error is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation; either version 2.1 of + the License, or (at your option) any later version. + + libgpg-error is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with libgpg-error; if not, write to the Free + Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. */ + +#if HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include <gpg-error.h> + +#include "gettext.h" + + +/* Locale directory support. */ + +#if HAVE_W32_SYSTEM +/* The implementation follows below. */ +static char *get_locale_dir (void); +static void drop_locale_dir (char *locale_dir); +#else +#define get_locale_dir() LOCALEDIR +#define drop_locale_dir(dir) +#endif + + +/* Initialize the library. This function should be run early. */ +gpg_error_t +gpg_err_init (void) +{ + char *locale_dir; + + /* We only have to bind our locale directory to our text domain. */ + + locale_dir = get_locale_dir (); + if (locale_dir) + { + bindtextdomain (PACKAGE, locale_dir); + drop_locale_dir (locale_dir); + } + + return 0; +} + + + +#ifdef HAVE_W32_SYSTEM + +#include <windows.h> + +static HKEY +get_root_key(const char *root) +{ + HKEY root_key; + + if( !root ) + root_key = HKEY_CURRENT_USER; + else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) ) + root_key = HKEY_CLASSES_ROOT; + else if( !strcmp( root, "HKEY_CURRENT_USER" ) ) + root_key = HKEY_CURRENT_USER; + else if( !strcmp( root, "HKEY_LOCAL_MACHINE" ) ) + root_key = HKEY_LOCAL_MACHINE; + else if( !strcmp( root, "HKEY_USERS" ) ) + root_key = HKEY_USERS; + else if( !strcmp( root, "HKEY_PERFORMANCE_DATA" ) ) + root_key = HKEY_PERFORMANCE_DATA; + else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) ) + root_key = HKEY_CURRENT_CONFIG; + else + return NULL; + return root_key; +} + +/**************** + * Return a string from the Win32 Registry or NULL in case of + * error. Caller must release the return value. A NULL for root + * is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. + * NOTE: The value is allocated with a plain malloc() - use free() and not + * the usual xfree()!!! + */ +static char * +read_w32_registry_string( const char *root, const char *dir, const char *name ) +{ + HKEY root_key, key_handle; + DWORD n1, nbytes, type; + char *result = NULL; + + if ( !(root_key = get_root_key(root) ) ) + return NULL; + + if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) ) + { + if (root) + return NULL; /* no need for a RegClose, so return direct */ + /* It seems to be common practise to fall back to HKLM. */ + if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) ) + return NULL; /* still no need for a RegClose, so return direct */ + } + + nbytes = 1; + if( RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes ) ) { + if (root) + goto leave; + /* Try to fallback to HKLM also vor a missing value. */ + RegCloseKey (key_handle); + if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle) ) + return NULL; /* Nope. */ + if (RegQueryValueEx( key_handle, name, 0, NULL, NULL, &nbytes)) + goto leave; + } + result = malloc( (n1=nbytes+1) ); + if( !result ) + goto leave; + if( RegQueryValueEx( key_handle, name, 0, &type, result, &n1 ) ) { + free(result); result = NULL; + goto leave; + } + result[nbytes] = 0; /* make sure it is really a string */ + if (type == REG_EXPAND_SZ && strchr (result, '%')) { + char *tmp; + + n1 += 1000; + tmp = malloc (n1+1); + if (!tmp) + goto leave; + nbytes = ExpandEnvironmentStrings (result, tmp, n1); + if (nbytes && nbytes > n1) { + free (tmp); + n1 = nbytes; + tmp = malloc (n1 + 1); + if (!tmp) + goto leave; + nbytes = ExpandEnvironmentStrings (result, tmp, n1); + if (nbytes && nbytes > n1) { + free (tmp); /* oops - truncated, better don't expand at all */ + goto leave; + } + tmp[nbytes] = 0; + free (result); + result = tmp; + } + else if (nbytes) { /* okay, reduce the length */ + tmp[nbytes] = 0; + free (result); + result = malloc (strlen (tmp)+1); + if (!result) + result = tmp; + else { + strcpy (result, tmp); + free (tmp); + } + } + else { /* error - don't expand */ + free (tmp); + } + } + + leave: + RegCloseKey( key_handle ); + return result; +} + + +#define REGKEY "Software\\GNU\\GnuPG" + +static char * +get_locale_dir (void) +{ + char *instdir; + char *p; + char *dname; + + instdir = read_w32_registry_string ("HKEY_LOCAL_MACHINE", REGKEY, + "Install Directory"); + if (!instdir) + return; + + /* Build the key: "<instdir>/share/locale". */ +#define SLDIR "\\share\\locale" + dname = malloc (strlen (instdir) + strlen (SLDIR) + 1); + if (!dname) + { + free (instdir); + return; + } + p = dname; + strcpy (p, instdir); + p += strlen (instdir); + strcpy (p, SLDIR); + + free (instdir); + + return dname; +} + + +static void +drop_locale_dir (char *locale_dir) +{ + free (locale_dir); +} + +#endif /* HAVE_W32_SYSTEM */ |