Fix timestamp parsing for y2038 hack.
[gpgme.git] / src / wait-user.c
1 /* wait-user.c 
2    Copyright (C) 2000 Werner Koch (dd9jn)
3    Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
4  
5    This file is part of GPGME.
6  
7    GPGME is free software; you can redistribute it and/or modify it
8    under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 2.1 of
10    the License, or (at your option) any later version.
11    
12    GPGME is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16    
17    You should have received a copy of the GNU Lesser General Public
18    License along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <assert.h>
26
27 #include "gpgme.h"
28 #include "context.h"
29 #include "priv-io.h"
30 #include "wait.h"
31 #include "ops.h"
32
33 \f
34 /* The user event loops are used for all asynchronous operations for
35    which a user callback is defined.  */
36
37 \f
38 /* Internal I/O Callbacks.  */
39
40 gpgme_error_t
41 _gpgme_user_io_cb_handler (void *data, int fd)
42 {
43   gpgme_error_t err = 0;
44   gpgme_error_t op_err = 0;
45   struct tag *tag = (struct tag *) data;
46   gpgme_ctx_t ctx;
47
48   assert (data);
49   ctx = tag->ctx;
50   assert (ctx);
51
52   LOCK (ctx->lock);
53   if (ctx->canceled)
54     err = gpg_error (GPG_ERR_CANCELED);
55   UNLOCK (ctx->lock);
56
57   if (! err)
58     err = _gpgme_run_io_cb (&ctx->fdt.fds[tag->idx], 0, &op_err);
59   if (err || op_err)
60     _gpgme_cancel_with_err (ctx, err, op_err);
61   else
62     {
63       unsigned int i;
64
65       for (i = 0; i < ctx->fdt.size; i++)
66         if (ctx->fdt.fds[i].fd != -1)
67           break;
68
69       if (i == ctx->fdt.size)
70         {
71           struct gpgme_io_event_done_data done_data;
72
73           done_data.err = 0;
74           done_data.op_err = 0;
75           _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &done_data);
76         }
77     }
78   return 0;
79 }
80
81
82 /* Register the file descriptor FD with the handler FNC (which gets
83    FNC_DATA as its first argument) for the direction DIR.  DATA should
84    be the context for which the fd is added.  R_TAG will hold the tag
85    that can be used to remove the fd.  */
86 gpgme_error_t
87 _gpgme_wait_user_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc,
88                             void *fnc_data, void **r_tag)
89 {
90   gpgme_ctx_t ctx = (gpgme_ctx_t) data;
91   struct tag *tag;
92   gpgme_error_t err;
93
94   assert (ctx);
95   err = _gpgme_add_io_cb (data, fd, dir, fnc, fnc_data, r_tag);
96   if (err)
97     return err;
98   tag = *r_tag;
99   assert (tag);
100   err = (*ctx->io_cbs.add) (ctx->io_cbs.add_priv, fd, dir,
101                             _gpgme_user_io_cb_handler, *r_tag,
102                             &tag->user_tag);
103   if (err)
104     _gpgme_remove_io_cb (*r_tag);
105   return err;
106 }
107
108
109 void
110 _gpgme_wait_user_remove_io_cb (void *data)
111 {
112   struct tag *tag = (struct tag *) data;
113   gpgme_ctx_t ctx;
114
115   assert (tag);
116   ctx = tag->ctx;
117
118   (*ctx->io_cbs.remove) (tag->user_tag);
119   _gpgme_remove_io_cb (data);
120 }
121
122
123 void
124 _gpgme_wait_user_event_cb (void *data, gpgme_event_io_t type, void *type_data)
125 {
126   gpgme_ctx_t ctx = data;
127
128   if (ctx->io_cbs.event)
129     (*ctx->io_cbs.event) (ctx->io_cbs.event_priv, type, type_data);
130 }