Grid map filed loading algorithm updated to accept tokens in quotes.
authorZdeněk Šustr <sustr4@cesnet.cz>
Mon, 10 Sep 2012 08:45:46 +0000 (08:45 +0000)
committerZdeněk Šustr <sustr4@cesnet.cz>
Mon, 10 Sep 2012 08:45:46 +0000 (08:45 +0000)
org.glite.lb.server/src/lb_authz.c
org.glite.lb.server/src/lb_html.c

index 42e8c17..b0d4ceb 100644 (file)
@@ -1298,8 +1298,8 @@ parse_gridmap(edg_wll_Context ctx,
 {
     FILE *fd = NULL;
     char line[4096];
-    char *p, *a, *b;
-    int ret;
+    char *p, *a;
+    int ret, index = 0, len, i, lineno = 0;
 
     fd = fopen(file, "r");
     if (fd == NULL)
@@ -1307,42 +1307,58 @@ parse_gridmap(edg_wll_Context ctx,
 
     /* XXX -1 */
     while (fgets(line, sizeof(line), fd) != NULL) {
-       p = strchr(line, '\n');
-       if (p)
+       lineno++;
+       if ((p = strchr(line, '\n')))   // Removes trailing \n
            *p = '\0';
 
-       if(strlen(line) == strspn(line, " \t")) continue;
-
-       p = line;
-       while(p && *p == ' ')
-           p++;
-       a = p;
-
-       p = strchr(line, ' ');
-       if (!p) {
-           ret = edg_wll_SetError(ctx, EINVAL, "Wrong format of mapping file");
-           goto end;
-       }
-       *p++ = '\0';
-       
-       while(p && *p == ' ')
-           p++;
-       b = p;
+       index = strspn(line, " \t");
 
+       if(strlen(line) == index) continue; // Skips empty lines
+       if(line[index]=='#') continue; // Skips commented-out lines
 
        mapping->rules = realloc(mapping->rules, (mapping->num+1) * sizeof(_edg_wll_mapping_rule));
        if (mapping->rules == NULL) {
            ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
            goto end;
        }
-       if (!(mapping->rules[mapping->num].a = strdup(a)) ||
-           !(mapping->rules[mapping->num].b = strdup(b))) {
-           ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
-           goto end;
+
+       for (i = 0; i < 2; i++) {
+
+               if (i) {
+                       if((index + len) < strlen(line)) index = index + len + strspn(line + index + len, " \t"); // Move index to next
+                       else {
+                               char *errdesc;
+                               asprintf(&errdesc, "Wrong format of mapping file, line %d", lineno);
+                               ret = edg_wll_SetError(ctx, EINVAL, errdesc);
+                               goto end;
+                       }
+               }
+
+               len = (line[index] == '"' || line[index] == '\'') ?     // Get token length
+                       strcspn(line + (++index), "\"'") :
+                       strcspn(line + index, " ");
+
+               if(!(a = calloc(sizeof(char*), len + 1))) { // Allocate and check
+                       ret = edg_wll_SetError(ctx, ENOMEM, "Not enough memory");
+                       goto end;
+               }
+
+               strncpy(a, line + index, len); // Copy token contents and assign
+               if (!i) mapping->rules[mapping->num].a = a;
+               else mapping->rules[mapping->num].b = a;
+
+               if(!strlen(a)) {
+                       char *errdesc;
+                       asprintf(&errdesc, "Orphaned ID in mapping file, line %d", lineno);
+                       ret = edg_wll_SetError(ctx, EINVAL, errdesc);
+                       goto end;
+               }
+
+               if(line[index+len] == '"' || line[index+len] == '\'') len++;
        }
+
        mapping->num++;
     }
-
     ret = 0;
 
 end:
index d287fd0..bc90d20 100644 (file)
@@ -221,13 +221,13 @@ int edg_wll_ConfigurationToHTML(edg_wll_Context ctx, int admin, char **message,
 
                a = NULL;
                for (i = 0; i < ctx->id_mapping.num; i++ ) {
-                       asprintf(&out_tmp, "%s%s%s%s%s%s%s",
+                       asprintf(&out_tmp, "%s%s\"%s\"%s\"%s\"%s%s",
                                a ? a : "",
-                               text ? "\"" : "<code>",
+                               text ? "" : "<code>",
                                ctx->id_mapping.rules[i].a,
-                               text ? "\"=\"" : " ",
+                               text ? "=" : " ",
                                ctx->id_mapping.rules[i].b,
-                               text ? "\"" : "<code>",
+                               text ? "" : "<code>",
                                i == ctx->id_mapping.num - 1 ? "" : (text ? "," : "<BR>"));
                        free(a);
                        a=out_tmp;