10 #include <doldaconnect/uilib.h>
11 #include <doldaconnect/uimisc.h>
12 #include <doldaconnect/utils.h>
16 struct dc_response *resp;
25 static scm_bits_t resptype;
27 static SCM scm_dc_connect(SCM host, SCM port)
31 SCM_ASSERT(SCM_STRINGP(host), host, SCM_ARG1, "dc-connect");
32 if(port == SCM_UNDEFINED)
36 SCM_ASSERT(SCM_INUMP(port), port, SCM_ARG2, "dc-connect");
37 cport = SCM_INUM(port);
41 if((fd = dc_connect(SCM_STRING_CHARS(host), cport)) < 0)
42 scm_syserror("dc-connect");
43 return(SCM_MAKINUM(fd));
46 static SCM scm_dc_disconnect(void)
49 return(SCM_MAKINUM(0));
52 static SCM scm_dc_connected(void)
54 return((fd == -1)?SCM_BOOL_F:SCM_BOOL_T);
57 static SCM scm_dc_select(SCM timeout)
62 if(timeout == SCM_UNDEFINED)
66 SCM_ASSERT(SCM_INUMP(timeout), timeout, SCM_ARG1, "dc-select");
67 cto = SCM_INUM(timeout);
70 scm_syserror_msg("dc-select", "Not connected", SCM_EOL, ENOTCONN);
74 pfd.events |= POLLOUT;
75 if((ret = poll(&pfd, 1, cto)) < 0)
82 scm_syserror("dc-select");
84 if(((pfd.revents & POLLIN) && dc_handleread()) || ((pfd.revents & POLLOUT) && dc_handlewrite()))
91 scm_syserror("dc-select");
93 return(ret?SCM_BOOL_T:SCM_BOOL_F);
96 static SCM makerespsmob(struct dc_response *resp)
98 struct respsmob *data;
100 data = scm_must_malloc(sizeof(*data), "respsmob");
102 SCM_RETURN_NEWSMOB(resptype, data);
105 static SCM scm_dc_getresp(SCM tag)
107 struct dc_response *resp;
110 if(tag == SCM_UNDEFINED)
112 if((resp = dc_getresp()) == NULL)
115 SCM_ASSERT(SCM_INUMP(tag), tag, SCM_ARG1, "dc-getresp");
116 if((resp = dc_gettaggedresp(SCM_INUM(tag))) == NULL)
119 ret = makerespsmob(resp);
123 static SCM scm_dc_extract(SCM scm_resp)
126 struct dc_response *resp;
129 SCM_ASSERT(SCM_SMOB_PREDICATE(resptype, scm_resp), scm_resp, SCM_ARG1, "dc-extract");
130 resp = ((struct respsmob *)SCM_SMOB_DATA(scm_resp))->resp;
132 ret = scm_cons(scm_cons(scm_str2symbol("cmd"), scm_makfrom0str(icswcstombs(resp->cmdname, "UTF-8", NULL))), ret);
133 ret = scm_cons(scm_cons(scm_str2symbol("code"), SCM_MAKINUM(resp->code)), ret);
134 ret = scm_cons(scm_cons(scm_str2symbol("tag"), SCM_MAKINUM(resp->tag)), ret);
136 for(i = resp->numlines - 1; i >= 0; i--)
139 for(o = resp->rlines[i].argc - 1; o >= 0; o--)
140 w = scm_cons(scm_makfrom0str(icswcstombs(resp->rlines[i].argv[o], "UTF-8", NULL)), w);
143 ret = scm_cons(scm_cons(scm_str2symbol("resp"), l), ret);
147 static SCM scm_dc_intresp(SCM scm_resp)
150 struct dc_response *resp;
151 struct dc_intresp *ires;
154 SCM_ASSERT(SCM_SMOB_PREDICATE(resptype, scm_resp), scm_resp, SCM_ARG1, "dc-intresp");
155 resp = ((struct respsmob *)SCM_SMOB_DATA(scm_resp))->resp;
156 if((ires = dc_interpret(resp)) == NULL)
159 for(i = ires->argc - 1; i >= 0; i--)
161 switch(ires->argv[i].type)
164 ret = scm_cons(scm_makfrom0str(icswcstombs(ires->argv[i].val.str, "UTF-8", NULL)), ret);
167 ret = scm_cons(scm_int2num(ires->argv[i].val.num), ret);
170 ret = scm_cons(scm_double2num(ires->argv[i].val.flnum), ret);
178 static int qcmd_scmcb(struct dc_response *resp)
183 scm_apply(scmcb->subr, scm_cons(makerespsmob(resp), SCM_EOL), SCM_EOL);
184 scm_gc_unprotect_object(scmcb->subr);
189 static SCM scm_dc_qcmd(SCM argv, SCM callback)
192 wchar_t **toks, *tok, *cmd;
193 size_t tokssize, toksdata;
197 SCM_ASSERT(SCM_CONSP(argv), argv, SCM_ARG1, "dc-qcmd");
198 if(callback != SCM_UNDEFINED)
199 SCM_ASSERT(SCM_CLOSUREP(callback), callback, SCM_ARG2, "dc-qcmd");
202 tokssize = toksdata = 0;
203 for(; argv != SCM_EOL; argv = SCM_CDR(argv))
205 port = scm_open_output_string();
206 scm_display(SCM_CAR(argv), port);
207 if((tok = icmbstowcs(SCM_STRING_CHARS(scm_get_output_string(port)), "UTF-8")) == NULL)
210 addtobuf(toks, NULL);
215 scm_syserror("dc-qcmd");
222 addtobuf(toks, NULL);
223 if(callback == SCM_UNDEFINED)
225 tag = dc_queuecmd(NULL, NULL, cmd, L"%%a", toks, NULL);
227 scmcb = scm_must_malloc(sizeof(*scmcb), "scmcb");
228 scm_gc_protect_object(scmcb->subr = callback);
229 tag = dc_queuecmd(qcmd_scmcb, scmcb, cmd, L"%%a", toks, NULL);
234 return(SCM_MAKINUM(tag));
237 static void login_scmcb(int err, wchar_t *reason, struct scmcb *scmcb)
243 case DC_LOGIN_ERR_SUCCESS:
244 errsym = scm_str2symbol("success");
246 case DC_LOGIN_ERR_NOLOGIN:
247 errsym = scm_str2symbol("nologin");
249 case DC_LOGIN_ERR_SERVER:
250 errsym = scm_str2symbol("server");
252 case DC_LOGIN_ERR_USER:
253 errsym = scm_str2symbol("user");
255 case DC_LOGIN_ERR_CONV:
256 errsym = scm_str2symbol("conv");
258 case DC_LOGIN_ERR_AUTHFAIL:
259 errsym = scm_str2symbol("authfail");
262 scm_apply(scmcb->subr, scm_cons(errsym, scm_cons((reason == NULL)?SCM_BOOL_F:scm_makfrom0str(icswcstombs(reason, "UTF-8", NULL)), SCM_EOL)), SCM_EOL);
263 scm_gc_unprotect_object(scmcb->subr);
267 static SCM scm_dc_loginasync(SCM callback, SCM useauthless, SCM username)
271 SCM_ASSERT(SCM_CLOSUREP(callback), callback, SCM_ARG1, "dc-loginasync");
272 scmcb = scm_must_malloc(sizeof(*scmcb), "scmcb");
273 scm_gc_protect_object(scmcb->subr = callback);
274 dc_loginasync(SCM_STRINGP(username)?SCM_STRING_CHARS(username):NULL, SCM_NFALSEP(useauthless), NULL, (void (*)(int, wchar_t *, void *))login_scmcb, scmcb);
278 static SCM scm_dc_lexsexpr(SCM sexpr)
281 wchar_t **arr, **ap, *buf;
283 SCM_ASSERT(SCM_STRINGP(sexpr), sexpr, SCM_ARG1, "dc-lexsexpr");
284 if((buf = icmbstowcs(SCM_STRING_CHARS(sexpr), NULL)) == NULL)
285 scm_syserror("dc-lexsexpr");
286 arr = dc_lexsexpr(buf);
291 for(ap = arr; *ap != NULL; ap++)
292 ret = scm_cons(scm_makfrom0str(icswcstombs(*ap, "UTF-8", NULL)), ret);
295 return(scm_reverse(ret));
298 static size_t resp_free(SCM respsmob)
300 struct respsmob *data;
302 data = (struct respsmob *)SCM_SMOB_DATA(respsmob);
303 dc_freeresp(data->resp);
305 return(sizeof(*data));
308 static int resp_print(SCM respsmob, SCM port, scm_print_state *pstate)
310 struct respsmob *data;
312 data = (struct respsmob *)SCM_SMOB_DATA(respsmob);
313 scm_puts("#<dc-response ", port);
314 scm_display(SCM_MAKINUM(data->resp->tag), port);
316 scm_puts(icswcstombs(data->resp->cmdname, "UTF-8", NULL), port);
318 scm_display(SCM_MAKINUM(data->resp->code), port);
323 void init_guiledc(void)
325 scm_c_define_gsubr("dc-connect", 1, 1, 0, scm_dc_connect);
326 scm_c_define_gsubr("dc-disconnect", 0, 0, 0, scm_dc_disconnect);
327 scm_c_define_gsubr("dc-connected", 0, 0, 0, scm_dc_connected);
328 scm_c_define_gsubr("dc-select", 0, 1, 0, scm_dc_select);
329 scm_c_define_gsubr("dc-getresp", 0, 1, 0, scm_dc_getresp);
330 scm_c_define_gsubr("dc-extract", 1, 0, 0, scm_dc_extract);
331 scm_c_define_gsubr("dc-intresp", 1, 0, 0, scm_dc_intresp);
332 scm_c_define_gsubr("dc-qcmd", 1, 1, 0, scm_dc_qcmd);
333 scm_c_define_gsubr("dc-loginasync", 2, 1, 0, scm_dc_loginasync);
334 scm_c_define_gsubr("dc-lexsexpr", 1, 0, 0, scm_dc_lexsexpr);
335 resptype = scm_make_smob_type("dc-resp", sizeof(struct respsmob));
336 scm_set_smob_free(resptype, resp_free);
337 scm_set_smob_print(resptype, resp_print);