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)
33 if((host == SCM_UNDEFINED) || (host == SCM_BOOL_F))
37 SCM_ASSERT(SCM_STRINGP(host), host, SCM_ARG1, "dc-connect");
38 chost = SCM_STRING_CHARS(host);
40 if((fd = dc_connect(chost)) < 0)
41 scm_syserror("dc-connect");
42 return(SCM_MAKINUM(fd));
45 static SCM scm_dc_disconnect(void)
48 return(SCM_MAKINUM(0));
51 static SCM scm_dc_connected(void)
53 return((fd == -1)?SCM_BOOL_F:SCM_BOOL_T);
56 static SCM scm_dc_select(SCM timeout)
61 if(timeout == SCM_UNDEFINED)
65 SCM_ASSERT(SCM_INUMP(timeout), timeout, SCM_ARG1, "dc-select");
66 cto = SCM_INUM(timeout);
69 scm_syserror_msg("dc-select", "Not connected", SCM_EOL, ENOTCONN);
73 pfd.events |= POLLOUT;
74 if((ret = poll(&pfd, 1, cto)) < 0)
81 scm_syserror("dc-select");
83 if(((pfd.revents & POLLIN) && dc_handleread()) || ((pfd.revents & POLLOUT) && dc_handlewrite()))
90 scm_syserror("dc-select");
92 return(ret?SCM_BOOL_T:SCM_BOOL_F);
95 static SCM makerespsmob(struct dc_response *resp)
97 struct respsmob *data;
99 data = scm_must_malloc(sizeof(*data), "respsmob");
101 SCM_RETURN_NEWSMOB(resptype, data);
104 static SCM scm_dc_getresp(SCM tag)
106 struct dc_response *resp;
109 if(tag == SCM_UNDEFINED)
111 if((resp = dc_getresp()) == NULL)
114 SCM_ASSERT(SCM_INUMP(tag), tag, SCM_ARG1, "dc-getresp");
115 if((resp = dc_gettaggedresp(SCM_INUM(tag))) == NULL)
118 ret = makerespsmob(resp);
122 static SCM scm_dc_extract(SCM scm_resp)
125 struct dc_response *resp;
128 SCM_ASSERT(SCM_SMOB_PREDICATE(resptype, scm_resp), scm_resp, SCM_ARG1, "dc-extract");
129 resp = ((struct respsmob *)SCM_SMOB_DATA(scm_resp))->resp;
131 ret = scm_cons(scm_cons(scm_str2symbol("cmd"), scm_makfrom0str(icswcstombs(resp->cmdname, "UTF-8", NULL))), ret);
132 ret = scm_cons(scm_cons(scm_str2symbol("code"), SCM_MAKINUM(resp->code)), ret);
133 ret = scm_cons(scm_cons(scm_str2symbol("tag"), SCM_MAKINUM(resp->tag)), ret);
135 for(i = resp->numlines - 1; i >= 0; i--)
138 for(o = resp->rlines[i].argc - 1; o >= 0; o--)
139 w = scm_cons(scm_makfrom0str(icswcstombs(resp->rlines[i].argv[o], "UTF-8", NULL)), w);
142 ret = scm_cons(scm_cons(scm_str2symbol("resp"), l), ret);
146 static SCM scm_dc_intresp(SCM scm_resp)
149 struct dc_response *resp;
150 struct dc_intresp *ires;
153 SCM_ASSERT(SCM_SMOB_PREDICATE(resptype, scm_resp), scm_resp, SCM_ARG1, "dc-intresp");
154 resp = ((struct respsmob *)SCM_SMOB_DATA(scm_resp))->resp;
155 if((ires = dc_interpret(resp)) == NULL)
158 for(i = ires->argc - 1; i >= 0; i--)
160 switch(ires->argv[i].type)
163 ret = scm_cons(scm_makfrom0str(icswcstombs(ires->argv[i].val.str, "UTF-8", NULL)), ret);
166 ret = scm_cons(scm_int2num(ires->argv[i].val.num), ret);
169 ret = scm_cons(scm_double2num(ires->argv[i].val.flnum), ret);
177 static int qcmd_scmcb(struct dc_response *resp)
182 scm_apply(scmcb->subr, scm_cons(makerespsmob(resp), SCM_EOL), SCM_EOL);
183 scm_gc_unprotect_object(scmcb->subr);
188 static SCM scm_dc_qcmd(SCM argv, SCM callback)
191 wchar_t **toks, *tok, *cmd;
192 size_t tokssize, toksdata;
196 SCM_ASSERT(SCM_CONSP(argv), argv, SCM_ARG1, "dc-qcmd");
197 if(callback != SCM_UNDEFINED)
198 SCM_ASSERT(SCM_CLOSUREP(callback), callback, SCM_ARG2, "dc-qcmd");
201 tokssize = toksdata = 0;
202 for(; argv != SCM_EOL; argv = SCM_CDR(argv))
204 port = scm_open_output_string();
205 scm_display(SCM_CAR(argv), port);
206 if((tok = icmbstowcs(SCM_STRING_CHARS(scm_get_output_string(port)), "UTF-8")) == NULL)
209 addtobuf(toks, NULL);
214 scm_syserror("dc-qcmd");
221 addtobuf(toks, NULL);
222 if(callback == SCM_UNDEFINED)
224 tag = dc_queuecmd(NULL, NULL, cmd, L"%%a", toks, NULL);
226 scmcb = scm_must_malloc(sizeof(*scmcb), "scmcb");
227 scm_gc_protect_object(scmcb->subr = callback);
228 tag = dc_queuecmd(qcmd_scmcb, scmcb, cmd, L"%%a", toks, NULL);
233 return(SCM_MAKINUM(tag));
236 static void login_scmcb(int err, wchar_t *reason, struct scmcb *scmcb)
242 case DC_LOGIN_ERR_SUCCESS:
243 errsym = scm_str2symbol("success");
245 case DC_LOGIN_ERR_NOLOGIN:
246 errsym = scm_str2symbol("nologin");
248 case DC_LOGIN_ERR_SERVER:
249 errsym = scm_str2symbol("server");
251 case DC_LOGIN_ERR_USER:
252 errsym = scm_str2symbol("user");
254 case DC_LOGIN_ERR_CONV:
255 errsym = scm_str2symbol("conv");
257 case DC_LOGIN_ERR_AUTHFAIL:
258 errsym = scm_str2symbol("authfail");
261 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);
262 scm_gc_unprotect_object(scmcb->subr);
266 static SCM scm_dc_loginasync(SCM callback, SCM useauthless, SCM username)
270 SCM_ASSERT(SCM_CLOSUREP(callback), callback, SCM_ARG1, "dc-loginasync");
271 scmcb = scm_must_malloc(sizeof(*scmcb), "scmcb");
272 scm_gc_protect_object(scmcb->subr = callback);
273 dc_loginasync(SCM_STRINGP(username)?SCM_STRING_CHARS(username):NULL, SCM_NFALSEP(useauthless), NULL, (void (*)(int, wchar_t *, void *))login_scmcb, scmcb);
277 static SCM scm_dc_lexsexpr(SCM sexpr)
280 wchar_t **arr, **ap, *buf;
282 SCM_ASSERT(SCM_STRINGP(sexpr), sexpr, SCM_ARG1, "dc-lexsexpr");
283 if((buf = icmbstowcs(SCM_STRING_CHARS(sexpr), NULL)) == NULL)
284 scm_syserror("dc-lexsexpr");
285 arr = dc_lexsexpr(buf);
290 for(ap = arr; *ap != NULL; ap++)
291 ret = scm_cons(scm_makfrom0str(icswcstombs(*ap, "UTF-8", NULL)), ret);
294 return(scm_reverse(ret));
297 static size_t resp_free(SCM respsmob)
299 struct respsmob *data;
301 data = (struct respsmob *)SCM_SMOB_DATA(respsmob);
302 dc_freeresp(data->resp);
304 return(sizeof(*data));
307 static int resp_print(SCM respsmob, SCM port, scm_print_state *pstate)
309 struct respsmob *data;
311 data = (struct respsmob *)SCM_SMOB_DATA(respsmob);
312 scm_puts("#<dc-response ", port);
313 scm_display(SCM_MAKINUM(data->resp->tag), port);
315 scm_puts(icswcstombs(data->resp->cmdname, "UTF-8", NULL), port);
317 scm_display(SCM_MAKINUM(data->resp->code), port);
322 void init_guiledc(void)
324 scm_c_define_gsubr("dc-connect", 0, 1, 0, scm_dc_connect);
325 scm_c_define_gsubr("dc-disconnect", 0, 0, 0, scm_dc_disconnect);
326 scm_c_define_gsubr("dc-connected", 0, 0, 0, scm_dc_connected);
327 scm_c_define_gsubr("dc-select", 0, 1, 0, scm_dc_select);
328 scm_c_define_gsubr("dc-getresp", 0, 1, 0, scm_dc_getresp);
329 scm_c_define_gsubr("dc-extract", 1, 0, 0, scm_dc_extract);
330 scm_c_define_gsubr("dc-intresp", 1, 0, 0, scm_dc_intresp);
331 scm_c_define_gsubr("dc-qcmd", 1, 1, 0, scm_dc_qcmd);
332 scm_c_define_gsubr("dc-loginasync", 2, 1, 0, scm_dc_loginasync);
333 scm_c_define_gsubr("dc-lexsexpr", 1, 0, 0, scm_dc_lexsexpr);
334 resptype = scm_make_smob_type("dc-resp", sizeof(struct respsmob));
335 scm_set_smob_free(resptype, resp_free);
336 scm_set_smob_print(resptype, resp_print);