Require autoconf 1.11
[gpgme.git] / src / opassuan.c
1 /* opassuan.c - Low-level Assuan operations.
2    Copyright (C) 2009 g10 Code GmbH
3
4    This file is part of GPGME.
5  
6    GPGME is free software; you can redistribute it and/or modify it
7    under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of
9    the License, or (at your option) any later version.
10    
11    GPGME is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15    
16    You should have received a copy of the GNU Lesser General Public
17    License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 /* Suppress warning for accessing deprecated member "err".  */
25 #define _GPGME_IN_GPGME 1
26 #include "gpgme.h"
27 #include "context.h"
28 #include "ops.h"
29 #include "util.h"
30 #include "debug.h"
31
32 /* LEGACY: Remove this when removing the deprecated result
33    structure.  */
34 typedef struct
35 {
36   struct _gpgme_op_assuan_result result;
37 } *op_data_t;
38
39
40 static gpgme_error_t
41 opassuan_start (gpgme_ctx_t ctx, int synchronous,
42                 const char *command,
43                 gpgme_assuan_data_cb_t data_cb,
44                 void *data_cb_value,
45                 gpgme_assuan_inquire_cb_t inq_cb,
46                 void *inq_cb_value,
47                 gpgme_assuan_status_cb_t status_cb,
48                 void *status_cb_value)
49 {
50   gpgme_error_t err;
51
52   if (!command || !*command)
53     return gpg_error (GPG_ERR_INV_VALUE);
54
55   /* The flag value 256 is used to suppress an engine reset.  This is
56      required to keep the connection running.  */
57   err = _gpgme_op_reset (ctx, ((synchronous&255) | 256));
58   if (err)
59     return err;
60
61   {
62     /* LEGACY: Remove this when removing the deprecated result
63        structure.  */
64     void *hook;
65     op_data_t opd;
66     err = _gpgme_op_data_lookup (ctx, OPDATA_ASSUAN, &hook,
67                                  sizeof (*opd), NULL);
68     if (err)
69       return err;
70   }
71
72   return _gpgme_engine_op_assuan_transact (ctx->engine, command,
73                                            data_cb, data_cb_value,
74                                            inq_cb, inq_cb_value,
75                                            status_cb, status_cb_value);
76 }
77
78
79
80 /* XXXX.  This is the asynchronous variant. */
81 gpgme_error_t
82 gpgme_op_assuan_transact_start (gpgme_ctx_t ctx, 
83                                 const char *command,
84                                 gpgme_assuan_data_cb_t data_cb,
85                                 void *data_cb_value,
86                                 gpgme_assuan_inquire_cb_t inq_cb,
87                                 void *inq_cb_value,
88                                 gpgme_assuan_status_cb_t status_cb,
89                                 void *status_cb_value)
90 {
91   gpg_error_t err;
92
93   TRACE_BEG7 (DEBUG_CTX, "gpgme_op_assuan_transact_start", ctx,
94               "command=%s, data_cb=%p/%p, inq_cb=%p/%p, status_cb=%p/%p",
95               command, data_cb, data_cb_value, inq_cb, inq_cb_value,
96               status_cb, status_cb_value);
97
98   err = opassuan_start (ctx, 0, command, data_cb, data_cb_value,
99                         inq_cb, inq_cb_value, status_cb, status_cb_value);
100   return TRACE_ERR (err);
101 }
102
103
104 /* XXXX.  This is the synchronous variant. */
105 gpgme_error_t
106 gpgme_op_assuan_transact_ext (gpgme_ctx_t ctx,
107                               const char *command,
108                               gpgme_assuan_data_cb_t data_cb,
109                               void *data_cb_value,
110                               gpgme_assuan_inquire_cb_t inq_cb,
111                               void *inq_cb_value,
112                               gpgme_assuan_status_cb_t status_cb,
113                               void *status_cb_value,
114                               gpgme_error_t *op_err_p)
115 {
116   gpgme_error_t err;
117   gpgme_error_t op_err;
118
119   TRACE_BEG8 (DEBUG_CTX, "gpgme_op_assuan_transact", ctx,
120               "command=%s, data_cb=%p/%p, inq_cb=%p/%p, status_cb=%p/%p, "
121               "op_err=%p",
122               command, data_cb, data_cb_value, inq_cb, inq_cb_value,
123               status_cb, status_cb_value, op_err_p);
124
125   err = opassuan_start (ctx, 1, command, 
126                         data_cb, data_cb_value,
127                         inq_cb, inq_cb_value,
128                         status_cb, status_cb_value);
129   if (err)
130     goto out;
131
132   err = _gpgme_wait_one_ext (ctx, &op_err);
133   if (op_err)
134     {
135       TRACE_LOG2 ("op_err = %s <%s>", gpgme_strerror (op_err),
136                   gpgme_strsource (op_err));
137       if (! op_err_p)
138         {
139           TRACE_LOG ("warning: operational error ignored by user");
140         }
141     }
142   if (op_err_p)
143     *op_err_p = op_err;
144   
145  out:
146   return TRACE_ERR (err);
147 }
148
149
150
151 \f
152 /* Compatibility code for old interface.  */
153
154 /* Evil hack breaking abstractions for the purpose of localizing our
155    other hack.  This is copied from engine.c.  */
156 struct engine
157 {
158   struct engine_ops *ops;
159   void *engine;
160 };
161
162 gpg_error_t _gpgme_engine_assuan_last_op_err (void *engine);
163
164 gpgme_assuan_result_t
165 gpgme_op_assuan_result (gpgme_ctx_t ctx)
166 {
167   gpgme_error_t err;
168   void *hook;
169   op_data_t opd;
170
171   TRACE_BEG (DEBUG_CTX, "gpgme_op_assuan_result", ctx);
172
173   err = _gpgme_op_data_lookup (ctx, OPDATA_ASSUAN, &hook, -1, NULL);
174   opd = hook;
175   /* Check in case this function is used without having run a command
176      before.  */
177   if (err || !opd)
178     {
179       TRACE_SUC0 ("result=(null)");
180       return NULL;
181     }
182
183   /* All of this is a hack for the old style interface.  The new style
184      interface returns op errors directly.  */
185   opd->result.err = _gpgme_engine_assuan_last_op_err (ctx->engine->engine);
186   if (opd->result.err)
187     {
188       TRACE_LOG1 ("err = %s", gpg_strerror (0));
189     }
190   else
191     {
192       TRACE_LOG2 ("err = %s <%s>", gpg_strerror (opd->result.err),
193                   gpg_strsource (opd->result.err));
194     }
195
196   TRACE_SUC1 ("result=%p", &opd->result);
197   return &opd->result;
198 }
199
200
201 gpgme_error_t
202 gpgme_op_assuan_transact (gpgme_ctx_t ctx,
203                           const char *command,
204                           gpgme_assuan_data_cb_t data_cb,
205                           void *data_cb_value,
206                           gpgme_assuan_inquire_cb_t inq_cb,
207                           void *inq_cb_value,
208                           gpgme_assuan_status_cb_t status_cb,
209                           void *status_cb_value)
210 {
211   gpgme_error_t err;
212
213   TRACE (DEBUG_CTX, "gpgme_op_assuan_transact", ctx);
214
215   /* Users of the old-style session based interfaces need to look at
216      the result structure.  */
217   err = gpgme_op_assuan_transact_ext (ctx, command, data_cb, data_cb_value,
218                                       inq_cb, inq_cb_value,
219                                       status_cb, status_cb_value, NULL);
220   return err;
221 }