1 /* asshelp.c - Helper functions for Assuan
2 * Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
38 send_one_option (assuan_context_t ctx
, gpg_err_source_t errsource
,
39 const char *name
, const char *value
)
44 if (!value
|| !*value
)
45 err
= 0; /* Avoid sending empty strings. */
46 else if (asprintf (&optstr
, "OPTION %s=%s", name
, value
) < 0)
47 err
= gpg_error_from_syserror ();
50 err
= assuan_transact (ctx
, optstr
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
58 /* Send the assuan commands pertaining to the pinenry environment. The
59 OPT_* arguments are optional and may be used to override the
60 defaults taken from the current locale. */
62 send_pinentry_environment (assuan_context_t ctx
,
63 gpg_err_source_t errsource
,
64 const char *opt_display
,
65 const char *opt_ttyname
,
66 const char *opt_ttytype
,
67 const char *opt_lc_ctype
,
68 const char *opt_lc_messages
)
71 char *dft_display
= NULL
;
72 char *dft_ttyname
= NULL
;
73 char *dft_ttytype
= NULL
;
77 /* Send the DISPLAY variable. */
78 dft_display
= getenv ("DISPLAY");
79 if (opt_display
|| dft_display
)
81 err
= send_one_option (ctx
, errsource
, "display",
82 opt_display
? opt_display
: dft_display
);
87 /* Send the name of the TTY. */
90 dft_ttyname
= getenv ("GPG_TTY");
91 if ((!dft_ttyname
|| !*dft_ttyname
) && ttyname (0))
92 dft_ttyname
= ttyname (0);
94 if (opt_ttyname
|| dft_ttyname
)
96 err
= send_one_option (ctx
, errsource
, "ttyname",
97 opt_ttyname
? opt_ttyname
: dft_ttyname
);
102 /* Send the type of the TTY. */
103 dft_ttytype
= getenv ("TERM");
104 if (opt_ttytype
|| (dft_ttyname
&& dft_ttytype
))
106 err
= send_one_option (ctx
, errsource
, "ttytype",
107 opt_ttyname
? opt_ttytype
: dft_ttytype
);
112 /* Send the value for LC_CTYPE. */
113 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
114 old_lc
= setlocale (LC_CTYPE
, NULL
);
117 old_lc
= strdup (old_lc
);
119 return gpg_error_from_syserror ();
121 dft_lc
= setlocale (LC_CTYPE
, "");
123 if (opt_lc_ctype
|| (dft_ttyname
&& dft_lc
))
125 err
= send_one_option (ctx
, errsource
, "lc-ctype",
126 opt_lc_ctype
? opt_lc_ctype
: dft_lc
);
128 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
131 setlocale (LC_CTYPE
, old_lc
);
138 /* Send the value for LC_MESSAGES. */
139 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
140 old_lc
= setlocale (LC_MESSAGES
, NULL
);
143 old_lc
= strdup (old_lc
);
145 return gpg_error_from_syserror ();
147 dft_lc
= setlocale (LC_MESSAGES
, "");
149 if (opt_lc_messages
|| (dft_ttyname
&& dft_lc
))
151 err
= send_one_option (ctx
, errsource
, "lc-messages",
152 opt_lc_messages
? opt_lc_messages
: dft_lc
);
154 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
157 setlocale (LC_MESSAGES
, old_lc
);