src/server/config/conf.c

changeset 17
d2a97bbeb57d
parent 16
a9bbd82d2dce
child 18
73aacbf6e492
--- a/src/server/config/conf.c	Sun Jan 15 17:00:16 2012 +0100
+++ b/src/server/config/conf.c	Sun Jan 15 20:30:45 2012 +0100
@@ -145,17 +145,6 @@
     return s;
 }
 
-/*
- * checks if the line contains only a comment or space
- */
-int cfg_get_basic_type(sstr_t line) {
-    if(line.length == 0) {
-        return LINE_NOCONTENT;
-    } else if(line.ptr[0] == '#') {
-        return LINE_NOCONTENT;
-    }
-    return LINE_OTHER;
-}
 
 /*
  * removes a comment from the line
@@ -265,6 +254,62 @@
 }
 
 /*
+ * parses a line containing a directive and returns a ConfigDirective object
+ * or NULL if an error occurs
+ */
+ConfigDirective* cfg_parse_directive(sstr_t line, UcxMempool *mp) {
+    if(line.length < 6) {
+        return NULL; // line too short
+    }
+
+    sstr_t name;
+
+    int i;
+    for(i=0;i<line.length;i++) {
+        if(line.ptr[i] < 33) {
+            break;
+        }
+    }
+    name.ptr = line.ptr;
+    name.length = i;
+
+    // create directive object
+    ConfigDirective *directive = OBJ_NEW(mp, ConfigDirective);
+    directive->directive_type = sstrdub_mp(mp, name);
+    directive->type_num = cfg_get_directive_type_num(name);
+    directive->condition = NULL; // set later by main parsing function
+    directive->param = NULL;
+
+    sstr_t param_str;
+    param_str.ptr = name.ptr + i;
+    param_str.length = line.length - i;
+    param_str = sstrtrim(param_str);
+    sstr_t pname;
+    sstr_t pvalue;
+    for(;;) {
+        param_str = cfg_param(param_str, &pname, &pvalue);
+        if(pname.length <= 0) {
+            break;
+        }
+
+        // create param object
+        ConfigParam *param = OBJ_NEW(mp, ConfigParam);
+        param->name = sstrdub_mp(mp, pname);
+        if(pvalue.length > 0) {
+            param->value = sstrdub_mp(mp, pvalue);
+        } else {
+            param->value.ptr = NULL;
+            param->value.length = 0;
+        }
+
+        // add param to list
+        directive->param = ucx_list_append(directive->param, param);
+    }
+
+    return directive;
+}
+
+/*
  * gets the directive type number from a type string
  * valid types are:
  *   AuthTrans      0
@@ -295,3 +340,142 @@
     }
     return dt;
 }
+
+/*
+ * checks if the line contains only a comment or space
+ */
+int cfg_get_basic_type(sstr_t line) {
+    if(line.length == 0) {
+        return LINE_NOCONTENT;
+    } else if(line.ptr[0] == '#') {
+        return LINE_NOCONTENT;
+    }
+    return LINE_OTHER;
+}
+
+/*
+ * checks if the line contains a begin/end tag or a directive
+ */
+int cfg_get_line_type(sstr_t line) {
+    if(line.length < 3) {
+        // this line is to short to be correct
+        return LINE_ERROR;
+    }
+
+    if(line.ptr[0] == '<') {
+        // start or end tag
+        // TODO: check for space between '<' and '/'
+        if(line.ptr[1] == '/') {
+            return LINE_END_TAG;
+        } else {
+            return LINE_BEGIN_TAG;
+        }
+    } else {
+        return LINE_DIRECTIVE;
+    }
+}
+
+int cfg_get_tag_type(sstr_t tag) {
+    if(!sstrcmp(tag, sstr("Object"))) {
+        return TAG_OBJECT;
+    } else if(!sstrcmp(tag, sstr("If"))) {
+        return TAG_IF;
+    } else if(!sstrcmp(tag, sstr("ElseIf"))) {
+        return TAG_ELSEIF;
+    } else if(!sstrcmp(tag, sstr("Else"))) {
+        return TAG_ELSE;
+    } else if(!sstrcmp(tag, sstr("Client"))) {
+        return TAG_CLIENT;
+    }
+    return -1;
+}
+
+/*
+ * returns the name of the ending tag
+ * on error, this functions returns a zero length string
+ */
+sstr_t cfg_get_end_tag_name(sstr_t line) {
+    sstr_t ns;
+    ns.ptr = NULL;
+    ns.length = 0;
+
+    if(line.length < 4) {
+        // minimum of 4 chars: </a>
+        return ns;
+    }
+
+    sstr_t name;
+    name.ptr = line.ptr + 2;
+    name.length = line.length - 3;
+
+    // check for </ > frame
+    if(line.ptr[0] != '<'
+            || line.ptr[1] != '/'
+            || line.ptr[line.length - 1] != '>')
+    {
+        return ns;
+    }
+
+    return sstrtrim(name);
+}
+
+ConfigTag* cfg_parse_begin_tag(sstr_t line, UcxMempool *mp) {
+    if(line.length < 4) {
+        return NULL; // this line can't contain a valid tag
+    }
+
+    if(line.ptr[0] != '<' || line.ptr[line.length - 1] != '>') {
+        return NULL; // syntax error
+    }
+
+    sstr_t name;
+    name.ptr = line.ptr + 1;
+    int i;
+    for(i=1;i<line.length - 1;i++) {
+        if(line.ptr[i] < 33) { // char is space
+            name.length = i - 1;
+            break;
+        }
+    }
+    if(name.length < 1) {
+        return NULL; // syntax error
+    }
+
+    // create tag object
+    ConfigTag *tag = OBJ_NEW(mp, ConfigTag);
+    tag->name = sstrdub_mp(mp, name);
+    tag->param = NULL;
+
+    // parse parameters
+    sstr_t param_str;
+    param_str.ptr = line.ptr + i;
+    param_str.length = line.length - name.length - 2;
+    param_str = sstrtrim(param_str);
+    if(param_str.length <= 0) {
+        return tag; // no parameters
+    }
+
+    sstr_t pname;
+    sstr_t pvalue;
+    for(;;) {
+        param_str = cfg_param(param_str, &pname, &pvalue);
+        if(pname.length <= 0) {
+            break;
+        }
+
+        // create param object
+        ConfigParam *param = OBJ_NEW(mp, ConfigParam);
+        param->name = sstrdub_mp(mp, pname);
+        if(pvalue.length > 0) {
+            param->value = sstrdub_mp(mp, pvalue);
+        } else {
+            param->value.ptr = NULL;
+            param->value.length = 0;
+        }
+
+        // add param to list
+        tag->param = ucx_list_append(tag->param, param);
+    }
+
+    return tag;
+}

mercurial