libidav/davqlexec.c

Fri, 05 Jun 2015 10:10:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 05 Jun 2015 10:10:14 +0200
changeset 131
32e7b3e6b482
parent 128
649eb328674a
child 133
8c37028f5024
permissions
-rw-r--r--

added missing keywords + fixed missing source text for boolean primaries

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2015 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ucx/utils.h>
#include "davqlexec.h"
#include "utils.h"
#include "methods.h"
#include "session.h"

DavResult* dav_statement_exec(DavSession *sn, DavQLStatement *st, ...) {
    va_list ap;
    va_start(ap, st);
    DavResult *result = dav_statement_execv(sn, st, ap);
    va_end(ap);
    return result;
}

DavResult* dav_statement_execv(DavSession *sn, DavQLStatement *st, va_list ap) {
    DavResult *result = dav_session_malloc(sn, sizeof(DavResult));
    result->result = NULL;
    result->status = 1;
    
    // make sure the statement was successfully parsed
    if(st->type == DAVQL_ERROR) {
        return result;
    }
    
    // get path string
    davqlerror_t error;
    sstr_t path = dav_format_string(sn->mp->allocator, st->path, ap, &error);
    
    if(st->type == DAVQL_SELECT) {
        *result = dav_exec_select(sn, st, path.ptr, ap);
    } else {
        // TODO
    }
    
    return result;
}

sstr_t dav_format_string(UcxAllocator *a, sstr_t fstr, va_list ap, davqlerror_t *error) {
    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
    
    int placeholder = 0;
    for(int i=0;i<fstr.length;i++) {
        char c = fstr.ptr[i];
        if(placeholder) {
            if(c == '%') {
                // no placeholder, %% transposes to %
                ucx_buffer_putc(buf, c);
            } else {
                // detect placeholder type and insert arg
                int err = 0;
                switch(c) {
                    case 's': {
                        char *arg = va_arg(ap, char*);
                        ucx_buffer_puts(buf, arg);
                        break;
                    }
                    case 'd': {
                        int arg = va_arg(ap, int);
                        ucx_bprintf(buf, "%d", arg);
                        break;
                    }
                    case 'u': {
                        unsigned int arg = va_arg(ap, unsigned int);
                        ucx_bprintf(buf, "%u", arg);
                        break;
                    }
                    case 't': {
                        // time arguments not supported for strings
                        err = 1;
                        break;
                    }
                    default: {
                        *error = DAVQL_UNKNOWN_FORMATCHAR;
                        err = 1;
                    }
                }
                if(err) {
                    ucx_buffer_free(buf);
                    sstr_t n;
                    n.ptr = NULL;
                    n.length = 0;
                    return n;
                }
            }
            placeholder = 0;
        } else {
            if(c == '%') {
                placeholder = 1;
            } else {
                ucx_buffer_putc(buf, c);
            }
        }
    }
    *error = DAVQL_OK;
    
    return sstrdup_a(a, sstrn(buf->space, buf->size));
}

/*
 * execute a davql select statement
 */
DavResult dav_exec_select(DavSession *sn, DavQLStatement *st, char* path, va_list ap) {
    UcxMempool *mp = ucx_mempool_new(128);
    
    // TODO: get property list
    UcxBuffer *rqbuf = create_allprop_propfind_request();
    
    UcxBuffer *where = dav_compile_expr(sn->context, mp->allocator, st->where, ap);
    
    DavResource *selroot = dav_resource_new(sn, path);
    DavResult result;
    result.result = selroot;
    result.status = 0;
    
    
    UcxList *stack = NULL; // stack with DavResource* elements
    // initialize the stack with the requested resource
    DavQLRes *r = ucx_mempool_malloc(mp, sizeof(DavQLRes));
    r->resource = selroot;
    r->depth = 0;
    stack = ucx_list_prepend(stack, r);
    
    // reuseable response buffer
    UcxBuffer *rpbuf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
    
    // do a propfind request for each resource on the stack
    while(stack) {
        DavQLRes *sr = stack->data; // get first element from the stack
        stack = ucx_list_remove(stack, stack); // remove first element
        DavResource *root = sr->resource;
        
        util_set_url(sn, dav_resource_get_href(sr->resource));
        CURLcode ret = do_propfind_request(sn->handle, rqbuf, rpbuf);
        int http_status = 0;
        curl_easy_getinfo (sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
        
        if(ret == CURLE_OK && http_status == 207) {
            // propfind request successful, now parse the response
            char *url = "http://url/";
            PropfindParser *parser = create_propfind_parser(rpbuf, url);
            ResponseTag response;
            int r;
            while((r = get_propfind_response(parser, &response)) != 0) {
                if(r == -1) {
                    // error
                    result.status = -1;
                    // TODO: free resources
                    break;
                }
                
                // the propfind multistatus response contains responses
                // for the requsted resource and all childs
                // determine if the response is a child or not
                if(hrefeq(sn, root->href, response.href)) {
                    // response is the currently requested resource
                    // and not a child
                    
                    // add properties
                    add_properties(root, &response);
                    
                    if(root == selroot) {
                        // The current root is the root of the select query.
                        // In this case we have to check the where clause.
                        // If root is not selroot, the where clause was
                        // already checked for the resource before it was
                        // added to the stack.
                        DavQLStackObj where_result;
                        if(!dav_exec_expr(where, root, &where_result)) {
                            if(where_result.data.integer != 0) {
                                continue;
                            }
                        } else {
                            result.status = -1;
                        }
                        dav_resource_free_all(selroot);
                        ucx_list_free(stack);
                        break;
                    }
                } else {
                    DavResource *child = response2resource(
                            sn,
                            &response,
                            root->path);
                    // check where clause
                    DavQLStackObj where_result;
                    if(!dav_exec_expr(where, child, &where_result)) {
                        if(where_result.data.integer != 0) {
                            resource_add_child(root, child);
                            if(child->iscollection &&
                                (st->depth < 0 || st->depth > sr->depth+1))
                            {
                                DavQLRes *rs = ucx_mempool_malloc(
                                        mp,
                                        sizeof(DavQLRes));
                                rs->resource = child;
                                rs->depth = sr->depth + 1;
                                stack = ucx_list_prepend(stack, rs);
                            }
                        } else {
                            dav_resource_free(child);
                        }
                    }
                }    
            }
            
        } else  {
            dav_session_set_error(sn, ret, http_status);
            result.status = -1;
            dav_resource_free_all(selroot);
            break;
        }
        
        // reset response buffer
        ucx_buffer_seek(rpbuf, SEEK_SET, 0);
    }
    
    ucx_mempool_destroy(mp);
    
    result.result = selroot;
    return result;
}

static int count_func_args(DavQLExpression *expr) {
    int count = 0;
    DavQLExpression *arg = expr->right;
    while(arg) {
        count++;
        if(arg->op == DAVQL_ARGLIST) {
            arg = arg->right;
        } else {
            break;
        }
    }
    return count;
}

static int add_cmd(DavContext *ctx, UcxAllocator *a, UcxBuffer *bcode, DavQLExpression *expr, va_list ap) {
    if(!expr) {
        return 0;
    }
     
    int numcmd = 1;
    DavQLCmd cmd;
    memset(&cmd, sizeof(DavQLCmd), 0);
    davqlerror_t error;
    
    sstr_t src = expr->srctext;
    switch(expr->type) {
        case DAVQL_NUMBER: {   
            cmd.type = DAVQL_CMD_INT;
            if(src.ptr[0] == '%') {
                cmd.data.integer = va_arg(ap, int);
            } else if(util_strtoint(src.ptr, &cmd.data.integer)) {
                ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
            } else {
                // error
                return -1;
            }
            
            break;
        }
        case DAVQL_STRING: {
            cmd.type = DAVQL_CMD_STRING;
            cmd.data.string = dav_format_string(a, src, ap, &error);
            ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
            break;
        }
        case DAVQL_TIMESTAMP: {
            if(src.ptr[0] == '%') {
                cmd.data.timestamp = va_arg(ap, time_t);
                ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
            } else {
                // error
                return -1;
            }
            break;
        }
        case DAVQL_IDENTIFIER: {
            sstr_t propertyname = sstrchr(src, ':');
            cmd.type = DAVQL_CMD_RES_IDENTIFIER;
            if(propertyname.length > 0) {
                cmd.type = DAVQL_CMD_PROP_IDENTIFIER;
                char *ns;
                char *name;
                dav_get_property_namespace(
                        ctx,
                        sstrdup_a(a, src).ptr,
                        &ns,
                        &name);
                if(ns && name) {
                    cmd.data.property.ns = ns;
                    cmd.data.property.name = name;
                } else {
                    // error
                    return -1;
                }
            } else if(!sstrcmp(src, S("name"))) {
                cmd.data.resprop = DAVQL_RES_NAME;
            } else if(!sstrcmp(src, S("path"))) {
                cmd.data.resprop = DAVQL_RES_PATH;
            } else if(!sstrcmp(src, S("href"))) {
                cmd.data.resprop = DAVQL_RES_HREF;
            } else if(!sstrcmp(src, S("contentlength"))) {
                cmd.data.resprop = DAVQL_RES_CONTENTLENGTH;
            } else if(!sstrcmp(src, S("contenttype"))) {
                cmd.data.resprop = DAVQL_RES_CONTENTTYPE;
            } else if(!sstrcmp(src, S("creationdate"))) {
                cmd.data.resprop = DAVQL_RES_CREATIONDATE;
            } else if(!sstrcmp(src, S("lastmodified"))) {
                cmd.data.resprop = DAVQL_RES_LASTMODIFIED;
            } else if(!sstrcmp(src, S("iscollection"))) {
                cmd.data.resprop = DAVQL_RES_ISCOLLECTION;
            } else if(!sstrcmp(src, S("true"))) {
                cmd.type = DAVQL_CMD_INT;
                cmd.data.integer = 1;
            } else if(!sstrcmp(src, S("false"))) {
                cmd.type = DAVQL_CMD_INT;
                cmd.data.integer = 0;
            } else {
                // error, unknown identifier
                return -1;
            }
            ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
            break;
        }
        case DAVQL_UNARY: {
            numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
            switch(expr->op) {
                case DAVQL_ADD: {
                    // noop
                    numcmd = 0;
                    break;
                }
                case DAVQL_SUB: {
                    cmd.type = DAVQL_CMD_OP_UNARY_SUB;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_NEG: {
                    cmd.type = DAVQL_CMD_OP_UNARY_NEG;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
            }
            break;
        }
        case DAVQL_BINARY: {
            numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
            numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
            switch(expr->op) {
                case DAVQL_ADD: {
                    cmd.type = DAVQL_CMD_OP_BINARY_ADD;
                    break;
                }
                case DAVQL_SUB: {
                    cmd.type = DAVQL_CMD_OP_BINARY_SUB;
                    break;
                }
                case DAVQL_MUL: {
                    cmd.type = DAVQL_CMD_OP_BINARY_MUL;
                    break;
                }
                case DAVQL_DIV: {
                    cmd.type = DAVQL_CMD_OP_BINARY_DIV;
                    break;
                }
                case DAVQL_AND: {
                    cmd.type = DAVQL_CMD_OP_BINARY_AND;
                    break;
                }
                case DAVQL_OR: {
                    cmd.type = DAVQL_CMD_OP_BINARY_OR;
                    break;
                }
                case DAVQL_XOR: {
                    cmd.type = DAVQL_CMD_OP_BINARY_XOR;
                    break;
                }
            }
            ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
            break;
        }
        case DAVQL_LOGICAL: {
            if(expr->left && expr->right && expr->op != DAVQL_LOR) {
                numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
                numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
            }
            
            switch(expr->op) {
                case DAVQL_NOT: {
                    numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
                    cmd.type = DAVQL_CMD_OP_LOGICAL_NOT;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_LAND: {
                    cmd.type = DAVQL_CMD_OP_LOGICAL_AND;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_LOR: {
                    int nleft = add_cmd(ctx, a, bcode, expr->left, ap);
                    
                    cmd.type = DAVQL_CMD_OP_LOGICAL_OR_L;
                    DavQLCmd *or_l = (DavQLCmd*)(bcode->space + bcode->pos);
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    
                    int nright = add_cmd(ctx, a, bcode, expr->right, ap);
                    or_l->data.integer = nright + 1;
                    
                    cmd.type = DAVQL_CMD_OP_LOGICAL_OR;
                    cmd.data.integer = 0;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    
                    numcmd += nleft + nright;
                    break;
                }
                case DAVQL_LXOR: {
                    cmd.type = DAVQL_CMD_OP_LOGICAL_XOR;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_EQ: {
                    cmd.type = DAVQL_CMD_OP_EQ;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_NEQ: {
                    cmd.type = DAVQL_CMD_OP_NEQ;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_LT: {
                    cmd.type = DAVQL_CMD_OP_LT;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_GT: {
                    cmd.type = DAVQL_CMD_OP_GT;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_LE: {
                    cmd.type = DAVQL_CMD_OP_LE;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_GE: {
                    cmd.type = DAVQL_CMD_OP_GE;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_LIKE: {
                    cmd.type = DAVQL_CMD_OP_LIKE;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
                case DAVQL_UNLIKE: {
                    cmd.type = DAVQL_CMD_OP_UNLIKE;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    break;
                }
            }
            break;
        }
        case DAVQL_FUNCCALL: {
            switch(expr->op) {
                case DAVQL_CALL: {
                    int nright = add_cmd(ctx, a, bcode, expr->right, ap);
                    // TODO: count args
                    DavQLExpression *funcid = expr->left;
                    if(!funcid && funcid->type != DAVQL_IDENTIFIER) {
                        // fail
                        return -1;
                    }
                    
                    // numargs
                    cmd.type = DAVQL_CMD_INT;
                    cmd.data.integer = count_func_args(expr);
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    
                    // TODO: resolve function name
                    cmd.type = DAVQL_CMD_CALL;
                    cmd.data.func = NULL;
                    ucx_buffer_write(&cmd, sizeof(cmd), 1, bcode);
                    
                    numcmd = 2;
                    numcmd += nright;
                    break;
                }
                case DAVQL_ARGLIST: {
                    numcmd = 0;
                    numcmd += add_cmd(ctx, a, bcode, expr->left, ap);
                    numcmd += add_cmd(ctx, a, bcode, expr->right, ap);
                    break;
                }
            }
            break;
        }
    }
    return numcmd;
}

UcxBuffer* dav_compile_expr(DavContext *ctx, UcxAllocator *a, DavQLExpression *lexpr, va_list ap) {
    UcxBuffer *bcode = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
    if(!bcode) {
        return NULL;
    }
    
    if(add_cmd(ctx, a, bcode, lexpr, ap) <= 0) {
        ucx_buffer_free(bcode);
        return NULL;
    }
    
    return bcode;
}

int dav_exec_expr(UcxBuffer *bcode, DavResource *res, DavQLStackObj *result) {
    size_t count = bcode->pos / sizeof(DavQLCmd);
    DavQLCmd *cmds = (DavQLCmd*)bcode->space;
    
    // create execution stack
    size_t stsize = 64;
    size_t stpos = 0;
    DavQLStackObj *stack = calloc(stsize, sizeof(DavQLStackObj));
#define DAVQL_PUSH(obj) \
        if(stpos == stsize) { \
            stsize += 64; \
            stack = realloc(stack, stsize * sizeof(DavQLStackObj)); \
        } \
        stack[stpos++] = obj;
#define DAVQL_POP() stack[--stpos]
    
    DavQLStackObj obj;
    for(size_t i=0;i<count;i++) {
        DavQLCmd cmd = cmds[i];
        switch(cmd.type) {
            case DAVQL_CMD_INT: {
                printf("int %lld\n", cmd.data.integer);
                obj.type = 0;
                obj.length = 0;
                obj.data.integer = cmd.data.integer;
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_STRING: {
                printf("string \"%.*s\"\n", cmd.data.string.length, cmd.data.string.ptr);
                obj.type = 1;
                obj.length = cmd.data.string.length;
                obj.data.string = cmd.data.string.ptr;
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_TIMESTAMP: {
                printf("timestamp %d\n", cmd.data.timestamp);
                obj.type = 0;
                obj.length = 0;
                obj.data.integer = cmd.data.timestamp;
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_RES_IDENTIFIER: {
                char *rid[8] = {"name", "path", "href", "contentlength", "contenttype", "creationdate", "lastmodified", "iscollection"};
                printf("resprop %s\n", rid[cmd.data.resprop]);
                switch(cmd.data.resprop) {
                    case DAVQL_RES_NAME: {
                        obj.type = 1;
                        obj.length = strlen(res->name);
                        obj.data.string = res->name;
                        break;
                    }
                    case DAVQL_RES_PATH: {
                        obj.type = 1;
                        obj.length = strlen(res->path);
                        obj.data.string = res->path;
                        break;
                    }
                    case DAVQL_RES_HREF: {
                        obj.type = 1;
                        obj.length = strlen(res->href);
                        obj.data.string = res->href;
                        break;
                    }
                    case DAVQL_RES_CONTENTLENGTH: {
                        obj.type = 0;
                        obj.length = 0;
                        obj.data.integer = res->contentlength;
                        break;
                    }
                    case DAVQL_RES_CONTENTTYPE: {
                        obj.type = 1;
                        obj.length = strlen(res->contenttype);
                        obj.data.string = res->contenttype;
                        break;
                    }
                    case DAVQL_RES_CREATIONDATE: {
                        obj.type = 0;
                        obj.length = 0;
                        obj.data.integer = res->creationdate;
                        break;
                    }
                    case DAVQL_RES_LASTMODIFIED: {
                        obj.type = 0;
                        obj.length = 0;
                        obj.data.integer = res->lastmodified;
                        break;
                    }
                    case DAVQL_RES_ISCOLLECTION: {
                        obj.type = 0;
                        obj.length = 0;
                        obj.data.integer = res->iscollection;
                        break;
                    }
                }
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_PROP_IDENTIFIER: {
                printf("property %s:%s\n", cmd.data.property.ns, cmd.data.property.name);
                char *value = dav_get_property_ns(res, cmd.data.property.ns, cmd.data.property.name);
                obj.type = 1;
                obj.length = value ? strlen(value) : 0;
                obj.data.string = value;
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_OP_UNARY_ADD: {
                printf("uadd\n");
                break;
            }
            case DAVQL_CMD_OP_UNARY_SUB: {
                printf("usub\n");
                break;
            }
            case DAVQL_CMD_OP_UNARY_NEG: {
                printf("uneg\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_ADD: {
                printf("add\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_SUB: {
                printf("sub\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_MUL: {
                printf("mul\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_DIV: {
                printf("div\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_AND: {
                printf("and\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_OR: {
                printf("or\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_XOR: {
                printf("xor\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_NOT: {
                printf("not\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_AND: {
                printf("land\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_OR_L: {
                printf("or_l %d\n", cmd.data.integer);
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_OR: {
                printf("or\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_XOR: {
                printf("lxor\n");
                break;
            }
            case DAVQL_CMD_OP_EQ: {
                printf("eq\n");
                break;
            }
            case DAVQL_CMD_OP_NEQ: {
                printf("neq\n");
                break;
            }
            case DAVQL_CMD_OP_LT: {
                printf("lt\n");
                break;
            }
            case DAVQL_CMD_OP_GT: {
                printf("gt\n");
                DavQLStackObj obj2 = DAVQL_POP();
                DavQLStackObj obj1 = DAVQL_POP();
                // result
                obj.type = 0;
                obj.length = 0;
                
                int64_t int1;
                int64_t int2;
                int isint = 1;
                if(obj1.type == 0) {
                    int1 = obj1.data.integer;
                } else {
                    isint = util_strtoint(obj1.data.string, &int1);
                }
                if(isint) {
                    if(obj2.type == 0) {
                        int2 = obj2.data.integer;
                    } else {
                        isint = util_strtoint(obj2.data.string, &int2);
                    }
                    if(isint) {
                        obj.data.integer = int1 > int2;
                    }
                }
                
                // string compare
                // TODO
                DAVQL_PUSH(obj);
                break;
            }
            case DAVQL_CMD_OP_LE: {
                printf("le\n");
                break;
            }
            case DAVQL_CMD_OP_GE: {
                printf("ge\n");
                break;
            }
            case DAVQL_CMD_OP_LIKE: {
                printf("like\n");
                break;
            }
            case DAVQL_CMD_OP_UNLIKE: {
                printf("unlike\n");
                break;
            }
            case DAVQL_CMD_CALL: {
                printf("call %x\n", cmd.data.func);
                break;
            }
        }
    }
    
    int ret = 0;
    if(stpos == 1) {
        *result = stack[0];
    } else {
        ret = -1;
    }
    free(stack);
    
    return ret;
}


void print_bytecode(UcxBuffer *bcode) {
    bcode->pos = 0;
    DavQLCmd cmd;
    while(ucx_buffer_read(&cmd, sizeof(DavQLCmd), 1, bcode) == 1) {
        switch(cmd.type) {
            case DAVQL_CMD_INT: {
                printf("int %lld\n", cmd.data.integer);
                break;
            }
            case DAVQL_CMD_STRING: {
                printf("string \"%.*s\"\n", cmd.data.string.length, cmd.data.string.ptr);
                break;
            }
            case DAVQL_CMD_TIMESTAMP: {
                printf("timestamp %d\n", cmd.data.timestamp);
                break;
            }
            case DAVQL_CMD_RES_IDENTIFIER: {
                char *rid[8] = {"name", "path", "href", "contentlength", "contenttype", "creationdate", "lastmodified", "iscollection"};
                printf("resprop %s\n", rid[cmd.data.resprop]);
                break;
            }
            case DAVQL_CMD_PROP_IDENTIFIER: {
                printf("property %s:%s\n", cmd.data.property.ns, cmd.data.property.name);
                break;
            }
            case DAVQL_CMD_OP_UNARY_ADD: {
                printf("uadd\n");
                break;
            }
            case DAVQL_CMD_OP_UNARY_SUB: {
                printf("usub\n");
                break;
            }
            case DAVQL_CMD_OP_UNARY_NEG: {
                printf("uneg\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_ADD: {
                printf("add\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_SUB: {
                printf("sub\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_MUL: {
                printf("mul\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_DIV: {
                printf("div\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_AND: {
                printf("and\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_OR: {
                printf("or\n");
                break;
            }
            case DAVQL_CMD_OP_BINARY_XOR: {
                printf("xor\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_NOT: {
                printf("not\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_AND: {
                printf("land\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_OR_L: {
                printf("or_l %d\n", cmd.data.integer);
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_OR: {
                printf("or\n");
                break;
            }
            case DAVQL_CMD_OP_LOGICAL_XOR: {
                printf("lxor\n");
                break;
            }
            case DAVQL_CMD_OP_EQ: {
                printf("eq\n");
                break;
            }
            case DAVQL_CMD_OP_NEQ: {
                printf("neq\n");
                break;
            }
            case DAVQL_CMD_OP_LT: {
                printf("lt\n");
                break;
            }
            case DAVQL_CMD_OP_GT: {
                printf("gt\n");
                break;
            }
            case DAVQL_CMD_OP_LE: {
                printf("le\n");
                break;
            }
            case DAVQL_CMD_OP_GE: {
                printf("ge\n");
                break;
            }
            case DAVQL_CMD_OP_LIKE: {
                printf("like\n");
                break;
            }
            case DAVQL_CMD_OP_UNLIKE: {
                printf("unlike\n");
                break;
            }
            case DAVQL_CMD_CALL: {
                printf("call %x\n", cmd.data.func);
                break;
            }
        }
    }
    printf("\n");
}

mercurial