2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
34 #include <ucx/utils.h>
36 #include "davqlexec.h"
42 DavQLArgList* dav_ql_get_args(DavQLStatement *st, va_list ap) {
43 DavQLArgList *args = malloc(sizeof(DavQLArgList));
50 UCX_FOREACH(elm, st->args) {
51 intptr_t type = (intptr_t)elm->data;
52 DavQLArg *arg = calloc(1, sizeof(DavQLArg));
54 dav_ql_free_arglist(args);
60 arg->value.d = va_arg(ap, int);
64 arg->value.u = va_arg(ap, unsigned int);
68 arg->value.s = va_arg(ap, char*);
72 arg->value.t = va_arg(ap, time_t);
77 dav_ql_free_arglist(args);
88 args->current = args->first;
92 void dav_ql_free_arglist(DavQLArgList *args) {
93 DavQLArg *arg = args->first;
95 DavQLArg *next = arg->next;
102 static DavQLArg* arglist_get(DavQLArgList *args) {
103 DavQLArg *a = args->current;
105 args->current = a->next;
110 int dav_ql_getarg_int(DavQLArgList *args) {
111 DavQLArg *a = arglist_get(args);
112 if(a && a->type == 'd') {
118 unsigned int dav_ql_getarg_uint(DavQLArgList *args) {
119 DavQLArg *a = arglist_get(args);
120 if(a && a->type == 'u') {
126 char* dav_ql_getarg_str(DavQLArgList *args) {
127 DavQLArg *a = arglist_get(args);
128 if(a && a->type == 's') {
134 time_t dav_ql_getarg_time(DavQLArgList *args) {
135 DavQLArg *a = arglist_get(args);
136 if(a && a->type == 't') {
143 DavResult dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) {
146 DavResult result = dav_statement_execv(sn, st, ap);
151 DavResult dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) {
153 result.result = NULL;
156 // make sure the statement was successfully parsed
157 if(st->type == DAVQL_ERROR) {
161 if(st->type == DAVQL_SELECT) {
162 return dav_exec_select(sn, st, ap);
170 sstr_t dav_format_string(UcxAllocator *a, sstr_t fstr, DavQLArgList *ap, davqlerror_t *error) {
171 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
174 for(int i=0;i<fstr.length;i++) {
175 char c = fstr.ptr[i];
178 // no placeholder, %% transposes to %
179 ucx_buffer_putc(buf, c);
181 // detect placeholder type and insert arg
185 char *arg = dav_ql_getarg_str(ap);
186 ucx_buffer_puts(buf, arg);
190 int arg = dav_ql_getarg_int(ap);
191 ucx_bprintf(buf, "%d", arg);
195 unsigned int arg = dav_ql_getarg_uint(ap);
196 ucx_bprintf(buf, "%u", arg);
200 // time arguments not supported for strings
205 *error = DAVQL_UNKNOWN_FORMATCHAR;
210 ucx_buffer_free(buf);
222 ucx_buffer_putc(buf, c);
228 sstr_t ret = sstrdup_a(a, sstrn(buf->space, buf->size));
229 ucx_buffer_free(buf);
233 static int fl_add_properties(DavSession *sn, UcxMempool *mp, UcxMap *map, DavQLExpression *expression) {
238 if(expression->type == DAVQL_IDENTIFIER) {
239 DavProperty *property = ucx_mempool_malloc(mp, sizeof(DavProperty));
242 DavNamespace *ns = dav_get_property_namespace(
244 sstrdup_a(mp->allocator, expression->srctext).ptr,
251 property->name = name;
252 property->value = NULL;
254 ucx_map_sstr_put(map, expression->srctext, property);
257 if(expression->left) {
258 if(fl_add_properties(sn, mp, map, expression->left)) {
262 if(expression->right) {
263 if(fl_add_properties(sn, mp, map, expression->right)) {
271 static UcxBuffer* fieldlist2propfindrequest(DavSession *sn, UcxMempool *mp, UcxList *fields, int *isallprop) {
272 UcxMap *properties = ucx_map_new(32);
275 UCX_FOREACH(elm, fields) {
276 DavQLField *field = elm->data;
277 if(!sstrcmp(field->name, S("*"))) {
278 ucx_map_free(properties);
280 return create_allprop_propfind_request();
281 } else if(!sstrcmp(field->name, S("-"))) {
282 ucx_map_free(properties);
283 return create_propfind_request(sn, NULL, "propfind", 0);
285 if(fl_add_properties(sn, mp, properties, field->expr)) {
287 ucx_map_free(properties);
293 UcxMapIterator i = ucx_map_iterator(properties);
296 UcxList *list = NULL;
297 UCX_MAP_FOREACH(key, value, i) {
298 list = ucx_list_append(list, value);
301 UcxBuffer *reqbuf = create_propfind_request(sn, list, "propfind", 0);
303 ucx_map_free(properties);
307 static int reset_properties(DavSession *sn, DavResult *result, DavResource *res, UcxList *fields) {
308 UcxMap *new_properties = ucx_map_new_a(sn->mp->allocator, 32);
309 DavResourceData *data = (DavResourceData*)res->data;
311 // add basic properties
314 sstr_t cl_keystr = dav_property_key("DAV:", "getcontentlength");
315 UcxKey cl_key = ucx_key(cl_keystr.ptr, cl_keystr.length);
316 value = ucx_map_get(data->properties, cl_key);
318 ucx_map_put(new_properties, cl_key, value);
321 sstr_t cd_keystr = dav_property_key("DAV:", "creationdate");
322 UcxKey cd_key = ucx_key(cd_keystr.ptr, cd_keystr.length);
323 value = ucx_map_get(data->properties, cd_key);
325 ucx_map_put(new_properties, cd_key, value);
328 sstr_t lm_keystr = dav_property_key("DAV:", "getlastmodified");
329 UcxKey lm_key = ucx_key(lm_keystr.ptr, lm_keystr.length);
330 value = ucx_map_get(data->properties, lm_key);
332 ucx_map_put(new_properties, lm_key, value);
335 sstr_t ct_keystr = dav_property_key("DAV:", "getcontenttype");
336 UcxKey ct_key = ucx_key(ct_keystr.ptr, ct_keystr.length);
337 value = ucx_map_get(data->properties, ct_key);
339 ucx_map_put(new_properties, ct_key, value);
342 sstr_t rt_keystr = dav_property_key("DAV:", "resourcetype");
343 UcxKey rt_key = ucx_key(rt_keystr.ptr, rt_keystr.length);
344 value = ucx_map_get(data->properties, rt_key);
346 ucx_map_put(new_properties, rt_key, value);
349 sstr_t cn_keystr = dav_property_key(DAV_NS, "crypto-name");
350 UcxKey cn_key = ucx_key(cn_keystr.ptr, cn_keystr.length);
351 value = ucx_map_get(data->properties, cn_key);
353 ucx_map_put(new_properties, cn_key, value);
356 sstr_t ck_keystr = dav_property_key(DAV_NS, "crypto-key");
357 UcxKey ck_key = ucx_key(ck_keystr.ptr, ck_keystr.length);
358 value = ucx_map_get(data->properties, ck_key);
360 ucx_map_put(new_properties, ck_key, value);
363 sstr_t ch_keystr = dav_property_key(DAV_NS, "crypto-hash");
364 UcxKey ch_key = ucx_key(ch_keystr.ptr, ch_keystr.length);
365 value = ucx_map_get(data->properties, ch_key);
367 ucx_map_put(new_properties, ch_key, value);
370 // add properties from field list
371 UCX_FOREACH(elm, fields) {
372 DavCompiledField *field = elm->data;
373 DavQLStackObj field_result;
374 if(!dav_exec_expr(field->code, res, &field_result)) {
378 DavXmlNode *node = NULL;
379 if(field_result.type == 0) {
383 field_result.data.integer);
384 } else if(field_result.type == 1) {
385 if(field_result.data.string) {
386 str = sstrdup_a(sn->mp->allocator, sstrn(
387 field_result.data.string,
388 field_result.length));
390 } else if(field_result.type == 2) {
391 node = dav_copy_node(field_result.data.node);
395 resource_free_properties(sn, new_properties);
399 node = dav_session_malloc(sn, sizeof(DavXmlNode));
400 memset(node, 0, sizeof(DavXmlNode));
401 node->type = DAV_XML_TEXT;
402 node->content = str.ptr;
403 node->contentlength = str.length;
406 sstr_t key = dav_property_key(field->ns, field->name);
408 DavNamespace *namespace = dav_session_malloc(sn, sizeof(DavNamespace));
409 namespace->prefix = NULL;
410 namespace->name = dav_session_strdup(sn, field->ns);
412 DavProperty *prop = dav_session_malloc(sn, sizeof(DavProperty));
413 prop->name = dav_session_strdup(sn, field->name);
414 prop->ns = namespace;
417 ucx_map_sstr_put(new_properties, key, prop);
422 resource_free_properties(sn, new_properties);
427 ucx_map_remove(data->properties, cl_key);
428 ucx_map_remove(data->properties, cd_key);
429 ucx_map_remove(data->properties, lm_key);
430 ucx_map_remove(data->properties, ct_key);
431 ucx_map_remove(data->properties, rt_key);
432 ucx_map_remove(data->properties, cn_key);
433 ucx_map_remove(data->properties, ck_key);
434 ucx_map_remove(data->properties, ch_key);
436 resource_free_properties(sn, data->properties);
437 data->properties = new_properties;
452 * execute a davql select statement
454 DavResult dav_exec_select(DavSession *sn, DavQLStatement *st, va_list ap) {
455 UcxMempool *mp = ucx_mempool_new(128);
457 result.result = NULL;
460 DavQLArgList *args = dav_ql_get_args(st, ap);
464 ucx_mempool_reg_destr(mp, args, (ucx_destructor)dav_ql_free_arglist);
467 UcxBuffer *rqbuf = fieldlist2propfindrequest(sn, mp, st->fields, &isallprop);
469 ucx_mempool_destroy(mp);
472 ucx_mempool_reg_destr(mp, rqbuf, (ucx_destructor)ucx_buffer_free);
474 // compile field list
475 UcxList *cfieldlist = NULL;
476 UCX_FOREACH(elm, st->fields) {
477 DavQLField *field = elm->data;
478 if(sstrcmp(field->name, S("*")) && sstrcmp(field->name, S("-"))) {
479 // compile field expression
480 UcxBuffer *code = dav_compile_expr(
486 // TODO: set error string
489 ucx_mempool_reg_destr(mp, code, (ucx_destructor)ucx_buffer_free);
490 DavCompiledField *cfield = ucx_mempool_malloc(
492 sizeof(DavCompiledField));
496 dav_get_property_namespace_str(
498 sstrdup_a(mp->allocator, field->name).ptr,
502 // TODO: set error string
508 cfieldlist = ucx_list_append_a(mp->allocator, cfieldlist, cfield);
514 sstr_t path = dav_format_string(mp->allocator, st->path, args, &error);
517 ucx_mempool_destroy(mp);
521 int depth = st->depth == DAV_DEPTH_PLACEHOLDER ?
522 dav_ql_getarg_int(args) : st->depth;
524 UcxBuffer *where = dav_compile_expr(sn->context, mp->allocator, st->where, args);
525 if(st->where && !where) {
527 ucx_mempool_destroy(mp);
531 ucx_mempool_reg_destr(mp, where, (ucx_destructor)ucx_buffer_free);
534 // compile order criterion
535 UcxList *ordercr = NULL;
536 UCX_FOREACH(elm, st->orderby) {
537 DavQLOrderCriterion *oc = elm->data;
538 DavQLExpression *column = oc->column;
539 //printf("%.*s %s\n", column->srctext.length, column->srctext.ptr, oc->descending ? "desc" : "asc");
540 if(column->type == DAVQL_IDENTIFIER) {
541 // TODO: remove code duplication (add_cmd)
542 davqlresprop_t resprop;
543 sstr_t propertyname = sstrchr(column->srctext, ':');
544 if(propertyname.length > 0) {
547 dav_get_property_namespace_str(
549 sstrdup_a(mp->allocator, column->srctext).ptr,
553 DavOrderCriterion *cr = ucx_mempool_malloc(mp, sizeof(DavOrderCriterion));
555 sstr_t keystr = dav_property_key_a(mp->allocator, ns, name);
556 cr->column.property = ucx_key(keystr.ptr, keystr.length);
557 cr->descending = oc->descending;
558 ordercr = ucx_list_append_a(mp->allocator, ordercr, cr);
562 ucx_mempool_destroy(mp);
565 } else if(dav_identifier2resprop(column->srctext, &resprop)) {
566 DavOrderCriterion *cr = ucx_mempool_malloc(mp, sizeof(DavOrderCriterion));
568 cr->column.resprop = resprop;
569 cr->descending = oc->descending;
570 ordercr = ucx_list_append_a(mp->allocator, ordercr, cr);
574 ucx_mempool_destroy(mp);
578 } else if(column->type == DAVQL_NUMBER) {
580 fprintf(stderr, "order by number not supported\n");
583 // something is broken
585 ucx_mempool_destroy(mp);
590 DavResource *selroot = dav_resource_new(sn, path.ptr);
592 UcxList *stack = NULL; // stack with DavResource* elements
593 // initialize the stack with the requested resource
594 DavQLRes *res = ucx_mempool_malloc(mp, sizeof(DavQLRes));
595 res->resource = selroot;
597 stack = ucx_list_prepend(stack, res);
599 // reuseable response buffer
600 UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
603 ucx_mempool_destroy(mp);
606 ucx_mempool_reg_destr(mp, rpbuf, (ucx_destructor)ucx_buffer_free);
608 result.result = selroot;
611 // do a propfind request for each resource on the stack
613 DavQLRes *sr = stack->data; // get first element from the stack
614 stack = ucx_list_remove(stack, stack); // remove first element
615 DavResource *root = sr->resource;
617 util_set_url(sn, dav_resource_get_href(sr->resource));
618 CURLcode ret = do_propfind_request(sn, rqbuf, rpbuf);
619 long http_status = 0;
620 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
621 //printf("rpbuf: %s %s\n%.*s\n\n", sr->resource->path, sr->resource->href, rpbuf->pos, rpbuf->space);
623 if(ret == CURLE_OK && http_status == 207) {
624 // in case of an redirect we have to adjust resource->href
625 dav_set_effective_href(sn, root);
627 // propfind request successful, now parse the response
628 char *url = "http://url/";
629 PropfindParser *parser = create_propfind_parser(rpbuf, url);
630 // TODO: test if parser is null
631 ResponseTag response;
633 while((r = get_propfind_response(parser, &response)) != 0) {
637 // TODO: free resources
638 cleanup_response(&response);
642 // the propfind multistatus response contains responses
643 // for the requested resource and all childs
644 // determine if the response is a child or not
645 if(hrefeq(sn, root->href, response.href)) {
646 // response is the currently requested resource
650 add_properties(root, &response);
651 cleanup_response(&response);
653 if(root == selroot) {
654 // The current root is the root of the select query.
655 // In this case we have to check the where clause.
656 // If root is not selroot, the where clause was
657 // already checked for the resource before it was
658 // added to the stack.
659 DavQLStackObj where_result;
660 if(!dav_exec_expr(where, root, &where_result)) {
661 if(where_result.data.integer != 0) {
662 if(!reset_properties(sn, &result, root, cfieldlist)) {
668 result.result = NULL;
670 dav_resource_free_all(selroot);
671 ucx_list_free(stack);
675 DavResource *child = response2resource(
679 cleanup_response(&response);
680 // check where clause
681 DavQLStackObj where_result;
682 if(!dav_exec_expr(where, child, &where_result)) {
683 if(where_result.data.integer != 0) {
684 if(!reset_properties(sn, &result, child, cfieldlist)) {
685 //resource_add_child(root, child);
686 resource_add_ordered_child(root, child, ordercr);
687 if(child->iscollection &&
688 (depth < 0 || depth > sr->depth+1))
690 DavQLRes *rs = ucx_mempool_malloc(
693 rs->resource = child;
694 rs->depth = sr->depth + 1;
695 stack = ucx_list_prepend(stack, rs);
698 dav_resource_free(child);
701 dav_resource_free(child);
706 destroy_propfind_parser(parser);
708 dav_session_set_error(sn, ret, http_status);
709 result.result = NULL;
711 dav_resource_free_all(selroot);
715 // reset response buffer
716 ucx_buffer_seek(rpbuf, SEEK_SET, 0);
719 ucx_mempool_destroy(mp);
723 static int count_func_args(DavQLExpression *expr) {
725 DavQLExpression *arg = expr->right;
728 if(arg->op == DAVQL_ARGLIST) {
737 int dav_identifier2resprop(sstr_t src, davqlresprop_t *prop) {
738 if(!sstrcmp(src, S("name"))) {
739 *prop = DAVQL_RES_NAME;
740 } else if(!sstrcmp(src, S("path"))) {
741 *prop = DAVQL_RES_PATH;
742 } else if(!sstrcmp(src, S("href"))) {
743 *prop = DAVQL_RES_HREF;
744 } else if(!sstrcmp(src, S("contentlength"))) {
745 *prop = DAVQL_RES_CONTENTLENGTH;
746 } else if(!sstrcmp(src, S("contenttype"))) {
747 *prop = DAVQL_RES_CONTENTTYPE;
748 } else if(!sstrcmp(src, S("creationdate"))) {
749 *prop = DAVQL_RES_CREATIONDATE;
750 } else if(!sstrcmp(src, S("lastmodified"))) {
751 *prop = DAVQL_RES_LASTMODIFIED;
752 } else if(!sstrcmp(src, S("iscollection"))) {
753 *prop = DAVQL_RES_ISCOLLECTION;
760 static int add_cmd(DavContext *ctx, UcxAllocator *a, UcxBuffer *bcode, DavQLExpression *expr, DavQLArgList *ap) {
767 memset(&cmd, 0, sizeof(DavQLCmd));
770 sstr_t src = expr->srctext;
774 cmd.type = DAVQL_CMD_INT;
775 if(src.ptr[0] == '%') {
776 cmd.data.integer = dav_ql_getarg_int(ap);
777 } else if(util_strtoint(src.ptr, &cmd.data.integer)) {
778 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
787 cmd.type = DAVQL_CMD_STRING;
788 cmd.data.string = dav_format_string(a, src, ap, &error);
789 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
792 case DAVQL_TIMESTAMP: {
793 if(src.ptr[0] == '%') {
794 cmd.type = DAVQL_CMD_TIMESTAMP;
795 cmd.data.timestamp = dav_ql_getarg_time(ap);
796 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
803 case DAVQL_IDENTIFIER: {
804 sstr_t propertyname = sstrchr(src, ':');
805 cmd.type = DAVQL_CMD_RES_IDENTIFIER;
806 if(propertyname.length > 0) {
807 cmd.type = DAVQL_CMD_PROP_IDENTIFIER;
810 dav_get_property_namespace_str(
812 sstrdup_a(a, src).ptr,
816 cmd.data.property.ns = ns;
817 cmd.data.property.name = name;
822 } else if(!dav_identifier2resprop(src, &cmd.data.resprop)) {
823 if(!sstrcmp(src, S("true"))) {
824 cmd.type = DAVQL_CMD_INT;
825 cmd.data.integer = 1;
826 } else if(!sstrcmp(src, S("false"))) {
827 cmd.type = DAVQL_CMD_INT;
828 cmd.data.integer = 0;
830 // error, unknown identifier
834 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
838 numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
846 cmd.type = DAVQL_CMD_OP_UNARY_SUB;
847 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
851 cmd.type = DAVQL_CMD_OP_UNARY_NEG;
852 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
860 numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
861 numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
864 cmd.type = DAVQL_CMD_OP_BINARY_ADD;
868 cmd.type = DAVQL_CMD_OP_BINARY_SUB;
872 cmd.type = DAVQL_CMD_OP_BINARY_MUL;
876 cmd.type = DAVQL_CMD_OP_BINARY_DIV;
880 cmd.type = DAVQL_CMD_OP_BINARY_AND;
884 cmd.type = DAVQL_CMD_OP_BINARY_OR;
888 cmd.type = DAVQL_CMD_OP_BINARY_XOR;
893 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
896 case DAVQL_LOGICAL: {
897 if(expr->left && expr->right && expr->op != DAVQL_LOR) {
898 numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
899 numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
904 numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
905 cmd.type = DAVQL_CMD_OP_LOGICAL_NOT;
906 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
910 cmd.type = DAVQL_CMD_OP_LOGICAL_AND;
911 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
915 int nleft = add_cmd(ctx, a, bcode, expr->left, ap);
917 cmd.type = DAVQL_CMD_OP_LOGICAL_OR_L;
918 DavQLCmd *or_l = (DavQLCmd*)(bcode->space + bcode->pos);
919 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
921 int nright = add_cmd(ctx, a, bcode, expr->right, ap);
922 or_l->data.integer = nright + 1;
924 cmd.type = DAVQL_CMD_OP_LOGICAL_OR;
925 cmd.data.integer = 0;
926 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
928 numcmd += nleft + nright;
932 cmd.type = DAVQL_CMD_OP_LOGICAL_XOR;
933 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
937 cmd.type = DAVQL_CMD_OP_EQ;
938 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
942 cmd.type = DAVQL_CMD_OP_NEQ;
943 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
947 cmd.type = DAVQL_CMD_OP_LT;
948 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
952 cmd.type = DAVQL_CMD_OP_GT;
953 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
957 cmd.type = DAVQL_CMD_OP_LE;
958 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
962 cmd.type = DAVQL_CMD_OP_GE;
963 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
967 cmd.type = DAVQL_CMD_OP_LIKE;
968 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
972 cmd.type = DAVQL_CMD_OP_UNLIKE;
973 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
980 case DAVQL_FUNCCALL: {
983 int nright = add_cmd(ctx, a, bcode, expr->right, ap);
985 DavQLExpression *funcid = expr->left;
986 if(!funcid && funcid->type != DAVQL_IDENTIFIER) {
992 cmd.type = DAVQL_CMD_INT;
993 cmd.data.integer = count_func_args(expr);
994 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
996 // TODO: resolve function name
997 cmd.type = DAVQL_CMD_CALL;
998 cmd.data.func = NULL;
999 ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
1005 case DAVQL_ARGLIST: {
1007 numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
1008 numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
1019 UcxBuffer* dav_compile_expr(DavContext *ctx, UcxAllocator *a, DavQLExpression *lexpr, DavQLArgList *ap) {
1020 UcxBuffer *bcode = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
1025 if(add_cmd(ctx, a, bcode, lexpr, ap) <= 0) {
1026 ucx_buffer_free(bcode);
1033 static int cmd_str_cmp(DavQLStackObj obj1, DavQLStackObj obj2, davqlcmdtype_t cmd) {
1034 sstr_t s1 = obj1.type == 1 ?
1035 sstrn(obj1.data.string, obj1.length) :
1036 ucx_sprintf("%" PRId64, obj1.data.integer);
1037 sstr_t s2 = obj1.type == 1 ?
1038 sstrn(obj2.data.string, obj2.length) :
1039 ucx_sprintf("%" PRId64, obj2.data.integer);
1043 case DAVQL_CMD_OP_EQ: {
1044 res = sstrcmp(s1, s2) == 0;
1047 case DAVQL_CMD_OP_NEQ: {
1048 res = sstrcmp(s1, s2) != 0;
1051 case DAVQL_CMD_OP_LT: {
1052 res = sstrcmp(s1, s2) < 0;
1055 case DAVQL_CMD_OP_GT: {
1056 res = sstrcmp(s1, s2) > 0;
1059 case DAVQL_CMD_OP_LE: {
1060 res = sstrcmp(s1, s2) <= 0;
1063 case DAVQL_CMD_OP_GE: {
1064 res = sstrcmp(s1, s2) >= 0;
1070 if(obj1.type == 0) {
1073 if(obj2.type == 0) {
1080 int dav_exec_expr(UcxBuffer *bcode, DavResource *res, DavQLStackObj *result) {
1084 result->data.integer = 1;
1088 size_t count = bcode->pos / sizeof(DavQLCmd);
1089 DavQLCmd *cmds = (DavQLCmd*)bcode->space;
1091 // create execution stack
1094 DavQLStackObj *stack = calloc(stsize, sizeof(DavQLStackObj));
1095 #define DAVQL_PUSH(obj) \
1096 if(stpos == stsize) { \
1098 DavQLStackObj *stack_newptr; \
1099 stack_newptr = realloc(stack, stsize * sizeof(DavQLStackObj)); \
1100 if(stack_newptr) { \
1101 stack = stack_newptr; \
1107 stack[stpos++] = obj;
1108 #define DAVQL_PUSH_INT(intval) \
1110 DavQLStackObj intobj; \
1112 intobj.length = 0; \
1113 intobj.data.integer = intval; \
1114 DAVQL_PUSH(intobj); \
1116 #define DAVQL_POP() stack[--stpos]
1120 for(size_t i=0;i<count;i++) {
1121 DavQLCmd cmd = cmds[i];
1123 case DAVQL_CMD_INT: {
1124 //printf("int %lld\n", cmd.data.integer);
1127 obj.data.integer = cmd.data.integer;
1131 case DAVQL_CMD_STRING: {
1132 //printf("string \"%.*s\"\n", cmd.data.string.length, cmd.data.string.ptr);
1134 obj.length = cmd.data.string.length;
1135 obj.data.string = cmd.data.string.ptr;
1139 case DAVQL_CMD_TIMESTAMP: {
1140 //printf("timestamp %d\n", cmd.data.timestamp);
1143 obj.data.integer = (int64_t)cmd.data.timestamp;
1147 case DAVQL_CMD_RES_IDENTIFIER: {
1148 //char *rid[8] = {"name", "path", "href", "contentlength", "contenttype", "creationdate", "lastmodified", "iscollection"};
1149 //printf("resprop %s\n", rid[cmd.data.resprop]);
1150 switch(cmd.data.resprop) {
1151 case DAVQL_RES_NAME: {
1153 obj.length = strlen(res->name);
1154 obj.data.string = res->name;
1157 case DAVQL_RES_PATH: {
1159 obj.length = strlen(res->path);
1160 obj.data.string = res->path;
1163 case DAVQL_RES_HREF: {
1165 obj.length = strlen(res->href);
1166 obj.data.string = res->href;
1169 case DAVQL_RES_CONTENTLENGTH: {
1172 obj.data.integer = res->contentlength;
1175 case DAVQL_RES_CONTENTTYPE: {
1177 obj.length = strlen(res->contenttype);
1178 obj.data.string = res->contenttype;
1181 case DAVQL_RES_CREATIONDATE: {
1184 obj.data.integer = res->creationdate;
1187 case DAVQL_RES_LASTMODIFIED: {
1190 obj.data.integer = res->lastmodified;
1193 case DAVQL_RES_ISCOLLECTION: {
1196 obj.data.integer = res->iscollection;
1203 case DAVQL_CMD_PROP_IDENTIFIER: {
1204 //printf("property %s:%s\n", cmd.data.property.ns, cmd.data.property.name);
1205 //char *value = dav_get_string_property_ns(res, cmd.data.property.ns, cmd.data.property.name);
1206 DavXmlNode *value = dav_get_property_ns(res, cmd.data.property.ns, cmd.data.property.name);
1207 if(dav_xml_isstring(value)) {
1209 obj.length = (uint32_t)value->contentlength;
1210 obj.data.string = value->content;
1214 obj.data.node = value;
1219 //case DAVQL_CMD_OP_UNARY_ADD: {
1220 // printf("uadd\n");
1223 case DAVQL_CMD_OP_UNARY_SUB: {
1227 obj.data.integer = -obj.data.integer;
1231 i = count; // end loop
1235 case DAVQL_CMD_OP_UNARY_NEG: {
1239 obj.data.integer = obj.data.integer == 0 ? 1 : 0;
1243 i = count; // end loop
1247 case DAVQL_CMD_OP_BINARY_ADD: {
1249 DavQLStackObj obj2 = DAVQL_POP();
1250 DavQLStackObj obj1 = DAVQL_POP();
1251 if(obj1.type == 0 && obj2.type == 0) {
1252 DAVQL_PUSH_INT(obj1.data.integer + obj2.data.integer);
1254 // TODO: string concat
1258 case DAVQL_CMD_OP_BINARY_SUB: {
1260 DavQLStackObj obj2 = DAVQL_POP();
1261 DavQLStackObj obj1 = DAVQL_POP();
1262 if(obj1.type == 0 && obj2.type == 0) {
1263 DAVQL_PUSH_INT(obj1.data.integer - obj2.data.integer);
1267 i = count; // end loop
1271 case DAVQL_CMD_OP_BINARY_MUL: {
1273 DavQLStackObj obj2 = DAVQL_POP();
1274 DavQLStackObj obj1 = DAVQL_POP();
1275 if(obj1.type == 0 && obj2.type == 0) {
1276 DAVQL_PUSH_INT(obj1.data.integer * obj2.data.integer);
1280 i = count; // end loop
1284 case DAVQL_CMD_OP_BINARY_DIV: {
1286 DavQLStackObj obj2 = DAVQL_POP();
1287 DavQLStackObj obj1 = DAVQL_POP();
1288 if(obj1.type == 0 && obj2.type == 0) {
1289 DAVQL_PUSH_INT(obj1.data.integer / obj2.data.integer);
1293 i = count; // end loop
1297 case DAVQL_CMD_OP_BINARY_AND: {
1299 DavQLStackObj obj2 = DAVQL_POP();
1300 DavQLStackObj obj1 = DAVQL_POP();
1301 if(obj1.type == 0 && obj2.type == 0) {
1302 DAVQL_PUSH_INT(obj1.data.integer & obj2.data.integer);
1306 i = count; // end loop
1310 case DAVQL_CMD_OP_BINARY_OR: {
1312 DavQLStackObj obj2 = DAVQL_POP();
1313 DavQLStackObj obj1 = DAVQL_POP();
1314 if(obj1.type == 0 && obj2.type == 0) {
1315 DAVQL_PUSH_INT(obj1.data.integer | obj2.data.integer);
1319 i = count; // end loop
1323 case DAVQL_CMD_OP_BINARY_XOR: {
1325 DavQLStackObj obj2 = DAVQL_POP();
1326 DavQLStackObj obj1 = DAVQL_POP();
1327 if(obj1.type == 0 && obj2.type == 0) {
1328 DAVQL_PUSH_INT(obj1.data.integer ^ obj2.data.integer);
1332 i = count; // end loop
1336 case DAVQL_CMD_OP_LOGICAL_NOT: {
1340 case DAVQL_CMD_OP_LOGICAL_AND: {
1342 DavQLStackObj obj2 = DAVQL_POP();
1343 DavQLStackObj obj1 = DAVQL_POP();
1344 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
1345 int v2 = obj2.type == 0 ? (int)obj2.data.integer : (obj2.data.string ? 1 : 0);
1346 DAVQL_PUSH_INT(v1 && v2);
1349 case DAVQL_CMD_OP_LOGICAL_OR_L: {
1350 //printf("or_l %d\n", cmd.data.integer);
1351 DavQLStackObj obj1 = stack[stpos];
1352 if((obj1.type == 0 && obj1.data.integer) || (obj1.type == 1 && obj1.data.string)) {
1355 i += cmd.data.integer; // jump, skip right subtree of 'or'
1359 case DAVQL_CMD_OP_LOGICAL_OR: {
1361 DavQLStackObj obj2 = DAVQL_POP();
1362 DavQLStackObj obj1 = DAVQL_POP();
1363 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
1364 int v2 = obj2.type == 0 ? (int)obj2.data.integer : (obj2.data.string ? 1 : 0);
1365 DAVQL_PUSH_INT(v1 || v2);
1368 case DAVQL_CMD_OP_LOGICAL_XOR: {
1370 DavQLStackObj obj2 = DAVQL_POP();
1371 DavQLStackObj obj1 = DAVQL_POP();
1372 int v1 = obj1.type == 0 ? (int)obj1.data.integer : (obj1.data.string ? 1 : 0);
1373 int v2 = obj2.type == 0 ? (int)obj2.data.integer : (obj2.data.string ? 1 : 0);
1374 DAVQL_PUSH_INT(!v1 != !v2);
1377 case DAVQL_CMD_OP_EQ: {
1379 DavQLStackObj obj2 = DAVQL_POP();
1380 DavQLStackObj obj1 = DAVQL_POP();
1381 if(obj1.type == 0 && obj2.type == 0) {
1382 DAVQL_PUSH_INT(obj1.data.integer == obj2.data.integer);
1384 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1388 case DAVQL_CMD_OP_NEQ: {
1390 DavQLStackObj obj2 = DAVQL_POP();
1391 DavQLStackObj obj1 = DAVQL_POP();
1392 if(obj1.type == 0 && obj2.type == 0) {
1393 DAVQL_PUSH_INT(obj1.data.integer != obj2.data.integer);
1395 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1399 case DAVQL_CMD_OP_LT: {
1401 DavQLStackObj obj2 = DAVQL_POP();
1402 DavQLStackObj obj1 = DAVQL_POP();
1403 if(obj1.type == 0 && obj2.type == 0) {
1404 DAVQL_PUSH_INT(obj1.data.integer < obj2.data.integer);
1406 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1410 case DAVQL_CMD_OP_GT: {
1412 DavQLStackObj obj2 = DAVQL_POP();
1413 DavQLStackObj obj1 = DAVQL_POP();
1414 if(obj1.type == 0 && obj2.type == 0) {
1415 DAVQL_PUSH_INT(obj1.data.integer > obj2.data.integer);
1417 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1421 case DAVQL_CMD_OP_LE: {
1423 DavQLStackObj obj2 = DAVQL_POP();
1424 DavQLStackObj obj1 = DAVQL_POP();
1425 if(obj1.type == 0 && obj2.type == 0) {
1426 DAVQL_PUSH_INT(obj1.data.integer <= obj2.data.integer);
1428 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1432 case DAVQL_CMD_OP_GE: {
1434 DavQLStackObj obj2 = DAVQL_POP();
1435 DavQLStackObj obj1 = DAVQL_POP();
1436 if(obj1.type == 0 && obj2.type == 0) {
1437 DAVQL_PUSH_INT(obj1.data.integer >= obj2.data.integer);
1439 DAVQL_PUSH_INT(cmd_str_cmp(obj1, obj2, cmd.type));
1443 case DAVQL_CMD_OP_LIKE: {
1447 case DAVQL_CMD_OP_UNLIKE: {
1448 //printf("unlike\n");
1451 case DAVQL_CMD_CALL: {
1452 //printf("call %x\n", cmd.data.func);