Made the custom memory allocator symbols hidden. This prevents
[libpwmd.git] / asshelp.c
blob580985c5bb449ef69639a83e88c5b3653d2db08d
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,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #ifdef HAVE_LOCALE_H
29 #include <locale.h>
30 #endif
32 //#include "util.h"
34 #include "asshelp.h"
37 static gpg_error_t
38 send_one_option (assuan_context_t ctx, gpg_err_source_t errsource,
39 const char *name, const char *value)
41 gpg_error_t err;
42 char *optstr;
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 ();
48 else
50 err = assuan_transact (ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL);
51 free (optstr);
54 return err;
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. */
61 gpg_error_t
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)
70 gpg_error_t err = 0;
71 char *dft_display = NULL;
72 char *dft_ttyname = NULL;
73 char *dft_ttytype = NULL;
74 char *old_lc = NULL;
75 char *dft_lc = 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);
83 if (err)
84 return err;
87 /* Send the name of the TTY. */
88 if (!opt_ttyname)
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);
98 if (err)
99 return err;
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);
108 if (err)
109 return err;
112 /* Send the value for LC_CTYPE. */
113 #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
114 old_lc = setlocale (LC_CTYPE, NULL);
115 if (old_lc)
117 old_lc = strdup (old_lc);
118 if (!old_lc)
119 return gpg_error_from_syserror ();
121 dft_lc = setlocale (LC_CTYPE, "");
122 #endif
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)
129 if (old_lc)
131 setlocale (LC_CTYPE, old_lc);
132 free (old_lc);
134 #endif
135 if (err)
136 return err;
138 /* Send the value for LC_MESSAGES. */
139 #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
140 old_lc = setlocale (LC_MESSAGES, NULL);
141 if (old_lc)
143 old_lc = strdup (old_lc);
144 if (!old_lc)
145 return gpg_error_from_syserror ();
147 dft_lc = setlocale (LC_MESSAGES, "");
148 #endif
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)
155 if (old_lc)
157 setlocale (LC_MESSAGES, old_lc);
158 free (old_lc);
160 #endif
161 if (err)
162 return err;
164 return 0;