libidav/davqlparser.c

changeset 79
59c518ae0641
parent 76
4c48ce3b9045
child 80
a2832c054c98
--- a/libidav/davqlparser.c	Tue Mar 24 12:02:47 2015 +0100
+++ b/libidav/davqlparser.c	Tue Mar 24 15:49:51 2015 +0100
@@ -27,4 +27,109 @@
  */
 
 #include "davqlparser.h"
+#include <string.h>
+#include <stdio.h>
 
+static const char* _map_querytype(davqltype_t type) {
+    switch(type) {
+    case GET: return "GET";
+    case SET: return "SET";
+    default: return "unknown";
+    }
+}
+
+#define dav_debug_print_sstr_t(s) fwrite((s).ptr, 1, (s).length, stdout);
+
+static void dav_debug_ql_stmt_print(DavQLStatement *stmt) {
+    
+    // Basic information
+    printf("Statement: ");
+    dav_debug_print_sstr_t(stmt->srctext);
+    printf("\nType: %s\nField count: %zu",
+        _map_querytype(stmt->type),
+        ucx_list_size(stmt->fields));
+    
+    // Has wildcard
+    _Bool wildcard = 0;
+    UCX_FOREACH(elm, stmt->fields) {
+        DavQLExpression* expr = (DavQLExpression*)elm->data;
+        if (expr->type == IDENTIFIER &&
+            expr->srctext.length == 1 && *(expr->srctext.ptr) == '*') {
+            wildcard = 1;
+        }
+    }
+    printf(" with%s wildcard\n", wildcard?"":"out");
+    
+    // Pathname
+    printf("Path: ");
+    dav_debug_print_sstr_t(stmt->path.srctext);
+    
+    // Has where clause
+    printf("\nHas where clause: %s\n", stmt->where ? "yes" : "no");
+    if (stmt->type == SET) {
+        printf("Value list size matches: %s",
+            ucx_list_size(stmt->fields) == ucx_list_size(stmt->setvalues)
+            ? "yes" : "no");
+    }
+    
+    // WITH attributes
+    if (stmt->depth == SIZE_MAX) {
+        printf("Depth: unbound\n");
+    } else {
+        printf("Depth: %zu\n", stmt->depth);
+    }
+}
+
+static int dav_debug_ql_command() {
+    printf("Command (type 'h' for help): ");
+    
+    char buffer[16];
+    fgets(buffer, 16, stdin);
+    if (!strcmp(buffer, "q\n")) {
+        return 0;
+    } else if (!strcmp(buffer, "ps\n")) {
+        return 1;
+    } else if (!strcmp(buffer, "h\n")) {
+        return 100;
+    } else {
+        return -1;
+    }
+}
+
+void dav_debug_ql_statement(DavQLStatement *stmt) {
+    if (!stmt) {
+        fprintf(stderr, "Debug DavQLStatement failed: null pointer");
+        return;
+    }
+
+    printf("Starting DavQL debugger...\n\n");
+    dav_debug_ql_stmt_print(stmt);
+    
+    while(1) {
+        int cmd = dav_debug_ql_command();
+        switch (cmd) {
+        case 0: return;
+        case 1: dav_debug_ql_stmt_print(stmt); break;
+        case 100:
+            printf(
+                "\nCommands:\n"
+                "ps:  print statement information\n"
+                "q:   quit\n");
+            break;
+        default: printf("unknown command\n");
+        }
+    }
+}
+
+DavQLStatement* dav_parse_statement(sstr_t srctext) {
+    DavQLStatement *stmt = malloc(sizeof(DavQLStatement));
+    
+    // default values
+    memset(stmt, 0, sizeof(DavQLStatement));
+    stmt->srctext = srctext;
+    stmt->type = -1;
+    stmt->depth = SIZE_MAX;
+    
+    
+    return stmt;
+}

mercurial