Factored utf8 switching code out to i18n.c.

This commit is contained in:
Werner Koch 2007-10-19 15:58:38 +00:00
parent 259a40c830
commit c12ce55b25
12 changed files with 107 additions and 184 deletions

View File

@ -1,3 +1,7 @@
2007-10-19 Werner Koch <wk@g10code.com>
* protect-tool.c (get_passphrase): Use new utf8 switch fucntions.
2007-10-15 Daiki Ueno <ueno@unixuser.org> (wk) 2007-10-15 Daiki Ueno <ueno@unixuser.org> (wk)
* command-ssh.c (reenter_compare_cb): New function; imported from * command-ssh.c (reenter_compare_cb): New function; imported from

View File

@ -1171,38 +1171,16 @@ get_passphrase (int promptno, int opt_check)
char *pw; char *pw;
int err; int err;
const char *desc; const char *desc;
#ifdef ENABLE_NLS char *orig_codeset;
char *orig_codeset = NULL;
#endif
int error_msgno; int error_msgno;
if (opt_passphrase) if (opt_passphrase)
return xstrdup (opt_passphrase); return xstrdup (opt_passphrase);
error_msgno = promptno / 100; error_msgno = promptno / 100;
promptno %= 100; promptno %= 100;
#ifdef ENABLE_NLS orig_codeset = i18n_switchto_utf8 ();
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset && !strcmp (orig_codeset, "UTF-8"))
orig_codeset = NULL;
if (orig_codeset)
{
/* We only switch when we are able to restore the codeset later. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
if (promptno == 1 && opt_prompt) if (promptno == 1 && opt_prompt)
desc = opt_prompt; desc = opt_prompt;
@ -1226,13 +1204,7 @@ get_passphrase (int promptno, int opt_check)
_("Passphrase:"), desc, opt_check, &err); _("Passphrase:"), desc, opt_check, &err);
err = map_spwq_error (err); err = map_spwq_error (err);
#ifdef ENABLE_NLS i18n_switchback (orig_codeset);
if (orig_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
xfree (orig_codeset);
}
#endif
if (!pw) if (!pw)
{ {

View File

@ -1,3 +1,7 @@
2007-10-19 Werner Koch <wk@g10code.com>
* i18n.c (i18n_switchto_utf8, i18n_switchback): New.
2007-10-01 Werner Koch <wk@g10code.com> 2007-10-01 Werner Koch <wk@g10code.com>
* sysutils.h (FD2INT, INT2FD): New. * sysutils.h (FD2INT, INT2FD): New.

View File

@ -18,9 +18,17 @@
*/ */
#include <config.h> #include <config.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#include "util.h"
#include "i18n.h" #include "i18n.h"
void void
i18n_init (void) i18n_init (void)
{ {
@ -35,3 +43,59 @@ i18n_init (void)
#endif #endif
} }
/* The Assuan agent protocol requires us to transmit utf-8 strings
thus we need a fucntion to temporary switch gettext from native to
utf8. */
char *
i18n_switchto_utf8 (void)
{
#ifdef ENABLE_NLS
char *orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later.
Note that bind_textdomain_codeset does only return on memory
errors but not if a codeset is not available. Thus we don't
bother printing a diagnostic here. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
return orig_codeset;
#else
return NULL;
#endif
}
/* Switch back to the saved codeset. */
void
i18n_switchback (char *saved_codeset)
{
#ifdef ENABLE_NLS
if (saved_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, saved_codeset);
xfree (saved_codeset);
}
#else
(void)saved_codeset;
#endif
}
/* Gettext variant which temporary switches to utf-8 for string. */
const char *
i18n_utf8 (const char *string)
{
char *saved = i18n_switchto_utf8 ();
const char *result = _(string);
i18n_switchback (saved);
return result;
}

View File

@ -39,6 +39,9 @@
#endif /*!USE_SIMPLE_GETTEXT*/ #endif /*!USE_SIMPLE_GETTEXT*/
void i18n_init (void); void i18n_init (void);
char *i18n_switchto_utf8 (void);
void i18n_switchback (char *saved_codeset);
const char *i18n_utf8 (const char *string);
#endif /*GNUPG_COMMON_I18N_H*/ #endif /*GNUPG_COMMON_I18N_H*/

View File

@ -1,3 +1,7 @@
2007-10-19 Werner Koch <wk@g10code.com>
* passphrase.c (passphrase_get): Use new utf8 switching fucntions.
2007-09-14 Werner Koch <wk@g10code.com> 2007-09-14 Werner Koch <wk@g10code.com>
* gpg.c (build_lib_list): New. * gpg.c (build_lib_list): New.

View File

@ -257,9 +257,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid,
PKT_public_key *pk = xmalloc_clear( sizeof *pk ); PKT_public_key *pk = xmalloc_clear( sizeof *pk );
byte fpr[MAX_FINGERPRINT_LEN]; byte fpr[MAX_FINGERPRINT_LEN];
int have_fpr = 0; int have_fpr = 0;
#ifdef ENABLE_NLS char *orig_codeset;
char *orig_codeset = NULL;
#endif
char *my_prompt; char *my_prompt;
char hexfprbuf[20*2+1]; char hexfprbuf[20*2+1];
const char *my_cacheid; const char *my_cacheid;
@ -279,23 +277,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid,
pk = NULL; /* oops: no key for some reason */ pk = NULL; /* oops: no key for some reason */
} }
#ifdef ENABLE_NLS orig_codeset = i18n_switchto_utf8 ();
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
if (custom_description) if (custom_description)
atext = native_to_utf8 (custom_description); atext = native_to_utf8 (custom_description);
@ -371,6 +353,9 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid,
xfree (my_prompt); xfree (my_prompt);
xfree (atext); atext = NULL; xfree (atext); atext = NULL;
i18n_switchback (orig_codeset);
if (!rc) if (!rc)
; ;
else if ( gpg_err_code (rc) == GPG_ERR_CANCELED ) else if ( gpg_err_code (rc) == GPG_ERR_CANCELED )
@ -392,14 +377,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid,
if (canceled) if (canceled)
*canceled = 1; *canceled = 1;
} }
#ifdef ENABLE_NLS
if (orig_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
xfree (orig_codeset);
}
#endif
if (pk) if (pk)
free_public_key( pk ); free_public_key( pk );
if (rc) if (rc)

View File

@ -1,3 +1,9 @@
2007-10-19 Werner Koch <wk@g10code.com>
* qualified.c (gpgsm_qualified_consent): Use i18N-swicth functions.
(gpgsm_not_qualified_warning): Ditto.
* certdump.c (gpgsm_format_keydesc): Ditto.
2007-09-14 Werner Koch <wk@g10code.com> 2007-09-14 Werner Koch <wk@g10code.com>
* gpgsm.c (build_lib_list): New. * gpgsm.c (build_lib_list): New.

View File

@ -891,9 +891,7 @@ gpgsm_format_keydesc (ksba_cert_t cert)
char created[20]; char created[20];
char *sn; char *sn;
ksba_sexp_t sexp; ksba_sexp_t sexp;
#ifdef ENABLE_NLS char *orig_codeset;
char *orig_codeset = NULL;
#endif
name = ksba_cert_get_subject (cert, 0); name = ksba_cert_get_subject (cert, 0);
subject = name? gpgsm_format_name2 (name, 0) : NULL; subject = name? gpgsm_format_name2 (name, 0) : NULL;
@ -909,28 +907,7 @@ gpgsm_format_keydesc (ksba_cert_t cert)
else else
*created = 0; *created = 0;
orig_codeset = i18n_switchto_utf8 ();
#ifdef ENABLE_NLS
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later.
Note that bind_textdomain_codeset does only return on memory
errors but not if a codeset is not available. Thus we don't
bother printing a diagnostic here. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
rc = asprintf (&name, rc = asprintf (&name,
_("Please enter the passphrase to unlock the" _("Please enter the passphrase to unlock the"
@ -942,13 +919,7 @@ gpgsm_format_keydesc (ksba_cert_t cert)
gpgsm_get_short_fingerprint (cert), gpgsm_get_short_fingerprint (cert),
created); created);
#ifdef ENABLE_NLS i18n_switchback (orig_codeset);
if (orig_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
xfree (orig_codeset);
}
#endif
if (rc < 0) if (rc < 0)
{ {

View File

@ -24,12 +24,6 @@
#include <stdarg.h> #include <stdarg.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#include "gpgsm.h" #include "gpgsm.h"
#include "i18n.h" #include "i18n.h"
@ -200,26 +194,7 @@ gpgsm_qualified_consent (ctrl_t ctrl, ksba_cert_t cert)
subject = gpgsm_format_name2 (name, 0); subject = gpgsm_format_name2 (name, 0);
ksba_free (name); name = NULL; ksba_free (name); name = NULL;
#ifdef ENABLE_NLS orig_codeset = i18n_switchto_utf8 ();
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later.
Note that bind_textdomain_codeset does only return on memory
errors but not if a codeset is not available. Thus we don't
bother printing a diagnostic here. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
if (asprintf (&name, if (asprintf (&name,
_("You are about to create a signature using your " _("You are about to create a signature using your "
@ -239,10 +214,7 @@ gpgsm_qualified_consent (ctrl_t ctrl, ksba_cert_t cert)
else else
err = 0; err = 0;
#ifdef ENABLE_NLS i18n_switchback (orig_codeset);
if (orig_codeset)
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
#endif
xfree (orig_codeset); xfree (orig_codeset);
xfree (subject); xfree (subject);
@ -288,9 +260,7 @@ gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert)
gpg_error_t err; gpg_error_t err;
char *name, *subject, *buffer, *p; char *name, *subject, *buffer, *p;
const char *s; const char *s;
#ifdef ENABLE_NLS char *orig_codeset;
char *orig_codeset = NULL;
#endif
if (!opt.qualsig_approval) if (!opt.qualsig_approval)
return 0; return 0;
@ -301,27 +271,7 @@ gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert)
subject = gpgsm_format_name2 (name, 0); subject = gpgsm_format_name2 (name, 0);
ksba_free (name); name = NULL; ksba_free (name); name = NULL;
orig_codeset = i18n_switchto_utf8 ();
#ifdef ENABLE_NLS
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later.
Note that bind_textdomain_codeset does only return on memory
errors but not if a codeset is not available. Thus we don't
bother printing a diagnostic here. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
if (asprintf (&name, if (asprintf (&name,
_("You are about to create a signature using your " _("You are about to create a signature using your "
@ -334,13 +284,7 @@ gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert)
else else
err = 0; err = 0;
#ifdef ENABLE_NLS i18n_switchback (orig_codeset);
if (orig_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
xfree (orig_codeset);
}
#endif
xfree (subject); xfree (subject);
if (err) if (err)

View File

@ -1,5 +1,7 @@
2007-10-19 Werner Koch <wk@g10code.com> 2007-10-19 Werner Koch <wk@g10code.com>
* symcryptrun.c (confucius_get_pass): Use utf8 switching functions.
* gpg-connect-agent.c (get_var_ext): New. * gpg-connect-agent.c (get_var_ext): New.
(substitute_line): Use it. (substitute_line): Use it.
(assign_variable): Implement /slet in terms of get_var_ext. (assign_variable): Implement /slet in terms of get_var_ext.

View File

@ -424,46 +424,17 @@ confucius_get_pass (const char *cacheid, int again, int *canceled)
{ {
int err; int err;
char *pw; char *pw;
#ifdef ENABLE_NLS char *orig_codeset;
char *orig_codeset = NULL;
#endif
if (canceled) if (canceled)
*canceled = 0; *canceled = 0;
#ifdef ENABLE_NLS orig_codeset = i18n_switchto_utf8 ();
/* The Assuan agent protocol requires us to transmit utf-8 strings */
orig_codeset = bind_textdomain_codeset (PACKAGE_GT, NULL);
#ifdef HAVE_LANGINFO_CODESET
if (!orig_codeset)
orig_codeset = nl_langinfo (CODESET);
#endif
if (orig_codeset && !strcmp (orig_codeset, "UTF-8"))
orig_codeset = NULL;
if (orig_codeset)
{
/* We only switch when we are able to restore the codeset later. */
orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8"))
{
xfree (orig_codeset);
orig_codeset = NULL;
}
}
#endif
pw = simple_pwquery (cacheid, pw = simple_pwquery (cacheid,
again ? _("does not match - try again"):NULL, again ? _("does not match - try again"):NULL,
_("Passphrase:"), NULL, 0, &err); _("Passphrase:"), NULL, 0, &err);
err = map_spwq_error (err); err = map_spwq_error (err);
i18n_switchback (orig_codeset);
#ifdef ENABLE_NLS
if (orig_codeset)
{
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
xfree (orig_codeset);
}
#endif
if (!pw) if (!pw)
{ {