From c12ce55b25685738bc1668df7b7bde87c4ba023c Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 19 Oct 2007 15:58:38 +0000 Subject: [PATCH] Factored utf8 switching code out to i18n.c. --- agent/ChangeLog | 4 +++ agent/protect-tool.c | 34 ++--------------------- common/ChangeLog | 4 +++ common/i18n.c | 64 ++++++++++++++++++++++++++++++++++++++++++ common/i18n.h | 3 ++ g10/ChangeLog | 4 +++ g10/passphrase.c | 34 ++++------------------- sm/ChangeLog | 6 ++++ sm/certdump.c | 35 ++--------------------- sm/qualified.c | 66 ++++---------------------------------------- tools/ChangeLog | 2 ++ tools/symcryptrun.c | 35 ++--------------------- 12 files changed, 107 insertions(+), 184 deletions(-) diff --git a/agent/ChangeLog b/agent/ChangeLog index a30c1c7be..ab825e35b 100644 --- a/agent/ChangeLog +++ b/agent/ChangeLog @@ -1,3 +1,7 @@ +2007-10-19 Werner Koch + + * protect-tool.c (get_passphrase): Use new utf8 switch fucntions. + 2007-10-15 Daiki Ueno (wk) * command-ssh.c (reenter_compare_cb): New function; imported from diff --git a/agent/protect-tool.c b/agent/protect-tool.c index 0e062a230..910ba036d 100644 --- a/agent/protect-tool.c +++ b/agent/protect-tool.c @@ -1171,38 +1171,16 @@ get_passphrase (int promptno, int opt_check) char *pw; int err; const char *desc; -#ifdef ENABLE_NLS - char *orig_codeset = NULL; -#endif + char *orig_codeset; int error_msgno; - if (opt_passphrase) return xstrdup (opt_passphrase); error_msgno = promptno / 100; promptno %= 100; -#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 && !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 + orig_codeset = i18n_switchto_utf8 (); if (promptno == 1 && opt_prompt) desc = opt_prompt; @@ -1226,13 +1204,7 @@ get_passphrase (int promptno, int opt_check) _("Passphrase:"), desc, opt_check, &err); err = map_spwq_error (err); -#ifdef ENABLE_NLS - if (orig_codeset) - { - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); - xfree (orig_codeset); - } -#endif + i18n_switchback (orig_codeset); if (!pw) { diff --git a/common/ChangeLog b/common/ChangeLog index 577367292..b7c583797 100644 --- a/common/ChangeLog +++ b/common/ChangeLog @@ -1,3 +1,7 @@ +2007-10-19 Werner Koch + + * i18n.c (i18n_switchto_utf8, i18n_switchback): New. + 2007-10-01 Werner Koch * sysutils.h (FD2INT, INT2FD): New. diff --git a/common/i18n.c b/common/i18n.c index 1592994d4..3694947a9 100644 --- a/common/i18n.c +++ b/common/i18n.c @@ -18,9 +18,17 @@ */ #include +#ifdef HAVE_LOCALE_H +#include +#endif +#ifdef HAVE_LANGINFO_CODESET +#include +#endif +#include "util.h" #include "i18n.h" + void i18n_init (void) { @@ -35,3 +43,59 @@ i18n_init (void) #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; +} diff --git a/common/i18n.h b/common/i18n.h index b692feaa7..7405f9a55 100644 --- a/common/i18n.h +++ b/common/i18n.h @@ -39,6 +39,9 @@ #endif /*!USE_SIMPLE_GETTEXT*/ 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*/ diff --git a/g10/ChangeLog b/g10/ChangeLog index 6c5067d2e..3fedb9979 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,7 @@ +2007-10-19 Werner Koch + + * passphrase.c (passphrase_get): Use new utf8 switching fucntions. + 2007-09-14 Werner Koch * gpg.c (build_lib_list): New. diff --git a/g10/passphrase.c b/g10/passphrase.c index ad085486b..cf67b7f9f 100644 --- a/g10/passphrase.c +++ b/g10/passphrase.c @@ -257,9 +257,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid, PKT_public_key *pk = xmalloc_clear( sizeof *pk ); byte fpr[MAX_FINGERPRINT_LEN]; int have_fpr = 0; -#ifdef ENABLE_NLS - char *orig_codeset = NULL; -#endif + char *orig_codeset; char *my_prompt; char hexfprbuf[20*2+1]; 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 */ } -#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. */ - orig_codeset = xstrdup (orig_codeset); - if (!bind_textdomain_codeset (PACKAGE_GT, "utf-8")) - { - xfree (orig_codeset); - orig_codeset = NULL; - } - } -#endif + orig_codeset = i18n_switchto_utf8 (); if (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 (atext); atext = NULL; + i18n_switchback (orig_codeset); + + if (!rc) ; else if ( gpg_err_code (rc) == GPG_ERR_CANCELED ) @@ -392,14 +377,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid, if (canceled) *canceled = 1; } - -#ifdef ENABLE_NLS - if (orig_codeset) - { - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); - xfree (orig_codeset); - } -#endif + if (pk) free_public_key( pk ); if (rc) diff --git a/sm/ChangeLog b/sm/ChangeLog index 63a790bcc..d67d23fbc 100644 --- a/sm/ChangeLog +++ b/sm/ChangeLog @@ -1,3 +1,9 @@ +2007-10-19 Werner Koch + + * 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 * gpgsm.c (build_lib_list): New. diff --git a/sm/certdump.c b/sm/certdump.c index c8b9958ae..9798cce4c 100644 --- a/sm/certdump.c +++ b/sm/certdump.c @@ -891,9 +891,7 @@ gpgsm_format_keydesc (ksba_cert_t cert) char created[20]; char *sn; ksba_sexp_t sexp; -#ifdef ENABLE_NLS - char *orig_codeset = NULL; -#endif + char *orig_codeset; name = ksba_cert_get_subject (cert, 0); subject = name? gpgsm_format_name2 (name, 0) : NULL; @@ -909,28 +907,7 @@ gpgsm_format_keydesc (ksba_cert_t cert) else *created = 0; - -#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 - + orig_codeset = i18n_switchto_utf8 (); rc = asprintf (&name, _("Please enter the passphrase to unlock the" @@ -942,13 +919,7 @@ gpgsm_format_keydesc (ksba_cert_t cert) gpgsm_get_short_fingerprint (cert), created); -#ifdef ENABLE_NLS - if (orig_codeset) - { - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); - xfree (orig_codeset); - } -#endif + i18n_switchback (orig_codeset); if (rc < 0) { diff --git a/sm/qualified.c b/sm/qualified.c index 1a0f52790..507c1517f 100644 --- a/sm/qualified.c +++ b/sm/qualified.c @@ -24,12 +24,6 @@ #include #include #include -#ifdef HAVE_LOCALE_H -#include -#endif -#ifdef HAVE_LANGINFO_CODESET -#include -#endif #include "gpgsm.h" #include "i18n.h" @@ -200,26 +194,7 @@ gpgsm_qualified_consent (ctrl_t ctrl, ksba_cert_t cert) subject = gpgsm_format_name2 (name, 0); ksba_free (name); name = NULL; -#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 + orig_codeset = i18n_switchto_utf8 (); if (asprintf (&name, _("You are about to create a signature using your " @@ -239,10 +214,7 @@ gpgsm_qualified_consent (ctrl_t ctrl, ksba_cert_t cert) else err = 0; -#ifdef ENABLE_NLS - if (orig_codeset) - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); -#endif + i18n_switchback (orig_codeset); xfree (orig_codeset); xfree (subject); @@ -288,9 +260,7 @@ gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert) gpg_error_t err; char *name, *subject, *buffer, *p; const char *s; -#ifdef ENABLE_NLS - char *orig_codeset = NULL; -#endif + char *orig_codeset; if (!opt.qualsig_approval) return 0; @@ -301,27 +271,7 @@ gpgsm_not_qualified_warning (ctrl_t ctrl, ksba_cert_t cert) subject = gpgsm_format_name2 (name, 0); ksba_free (name); name = NULL; - -#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 + orig_codeset = i18n_switchto_utf8 (); if (asprintf (&name, _("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 err = 0; -#ifdef ENABLE_NLS - if (orig_codeset) - { - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); - xfree (orig_codeset); - } -#endif + i18n_switchback (orig_codeset); xfree (subject); if (err) diff --git a/tools/ChangeLog b/tools/ChangeLog index 118a0fe12..9d34e7d62 100644 --- a/tools/ChangeLog +++ b/tools/ChangeLog @@ -1,5 +1,7 @@ 2007-10-19 Werner Koch + * symcryptrun.c (confucius_get_pass): Use utf8 switching functions. + * gpg-connect-agent.c (get_var_ext): New. (substitute_line): Use it. (assign_variable): Implement /slet in terms of get_var_ext. diff --git a/tools/symcryptrun.c b/tools/symcryptrun.c index fced86fb7..e720eab2f 100644 --- a/tools/symcryptrun.c +++ b/tools/symcryptrun.c @@ -424,46 +424,17 @@ confucius_get_pass (const char *cacheid, int again, int *canceled) { int err; char *pw; -#ifdef ENABLE_NLS - char *orig_codeset = NULL; -#endif + char *orig_codeset; if (canceled) *canceled = 0; -#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 && !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 - + orig_codeset = i18n_switchto_utf8 (); pw = simple_pwquery (cacheid, again ? _("does not match - try again"):NULL, _("Passphrase:"), NULL, 0, &err); err = map_spwq_error (err); - -#ifdef ENABLE_NLS - if (orig_codeset) - { - bind_textdomain_codeset (PACKAGE_GT, orig_codeset); - xfree (orig_codeset); - } -#endif + i18n_switchback (orig_codeset); if (!pw) {