Added a more convenient fork-handler to dirplex configuration.
[ashd.git] / src / dirplex.c
index fa4bae5..7216d95 100644 (file)
@@ -59,10 +59,12 @@ struct rule {
 struct pattern {
     struct pattern *next;
     char *childnm;
+    char **fchild;
     struct rule **rules;
 };
 
-struct config *cflist;
+static struct config *cflist;
+static struct config *gconfig, *lconfig;
 static time_t now;
 
 static void freepattern(struct pattern *pat)
@@ -76,6 +78,7 @@ static void freepattern(struct pattern *pat)
     }
     if(pat->childnm != NULL)
        free(pat->childnm);
+    freeca(pat->fchild);
     free(pat);
 }
 
@@ -90,7 +93,8 @@ static void freeconfig(struct config *cf)
        cf->next->prev = cf->prev;
     if(cf == cflist)
        cflist = cf->next;
-    free(cf->path);
+    if(cf->path != NULL)
+       free(cf->path);
     for(ch = cf->children; ch != NULL; ch = nch) {
        nch = ch->next;
        freechild(ch);
@@ -138,7 +142,7 @@ static struct pattern *parsepattern(struct cfstate *s)
 {
     struct pattern *pat;
     struct rule *rule;
-    int sl;
+    int sl, i;
 
     if(!strcmp(s->argv[0], "match")) {
        s->expstart = 1;
@@ -178,6 +182,11 @@ static struct pattern *parsepattern(struct cfstate *s)
            if(pat->childnm != NULL)
                free(pat->childnm);
            pat->childnm = sstrdup(s->argv[1]);
+       } else if(!strcmp(s->argv[0], "fork")) {
+           pat->fchild = smalloc(sizeof(*pat->fchild) * s->argc);
+           for(i = 0; i < s->argc - 1; i++)
+               pat->fchild[i] = sstrdup(s->argv[i + 1]);
+           pat->fchild[i] = 0;
        } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
            break;
        } else {
@@ -190,7 +199,7 @@ static struct pattern *parsepattern(struct cfstate *s)
        freepattern(pat);
        return(NULL);
     }
-    if(pat->childnm == NULL) {
+    if((pat->childnm == NULL) && (pat->fchild == NULL)) {
        flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
        freepattern(pat);
        return(NULL);
@@ -301,6 +310,10 @@ static struct config **getconfigs(char *file)
     free(tmp);
     if((cf = getconfig(".")) != NULL)
        bufadd(buf, cf);
+    if(lconfig != NULL)
+       bufadd(buf, lconfig);
+    if(gconfig != NULL)
+       bufadd(buf, gconfig);
     bufadd(buf, NULL);
     return(ret = buf.b);
 }
@@ -364,14 +377,17 @@ static void handlefile(struct hthead *req, int fd, char *path)
        simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
        return;
     }
-    if((ch = findchild(path, pat->childnm)) == NULL) {
-       flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
-       simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
-       return;
+    if(pat->fchild) {
+       stdforkserve(pat->fchild, req, fd);
+    } else {
+       if((ch = findchild(path, pat->childnm)) == NULL) {
+           flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
+           simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
+           return;
+       }
+       if(childhandle(ch, req, fd))
+           simpleerror(fd, 500, "Server Error", "The request handler crashed.");
     }
-    
-    if(childhandle(ch, req, fd))
-       simpleerror(fd, 500, "Server Error", "The request handler crashed.");
 }
 
 static void handledir(struct hthead *req, int fd, char *path)
@@ -506,17 +522,53 @@ out:
     free(path);
 }
 
+static void usage(FILE *out)
+{
+    fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
+}
+
 int main(int argc, char **argv)
 {
+    int c;
+    int nodef;
+    char *gcf, *lcf;
     struct hthead *req;
     int fd;
     
-    if(argc < 2) {
-       flog(LOG_ERR, "usage: dirplex DIR");
+    nodef = 0;
+    lcf = NULL;
+    while((c = getopt(argc, argv, "hNc:")) >= 0) {
+       switch(c) {
+       case 'h':
+           usage(stdout);
+           exit(0);
+       case 'N':
+           nodef = 1;
+           break;
+       case 'c':
+           lcf = optarg;
+           break;
+       default:
+           usage(stderr);
+           exit(1);
+       }
+    }
+    if(argc - optind < 1) {
+       usage(stderr);
        exit(1);
     }
-    if(chdir(argv[1])) {
-       flog(LOG_ERR, "could not change directory to %s: %s", argv[1], strerror(errno));
+    if(!nodef) {
+       if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
+           gconfig = readconfig(gcf);
+           free(gcf);
+       }
+    }
+    if(lcf != NULL) {
+       if((lconfig = readconfig(lcf)) == NULL)
+           exit(1);
+    }
+    if(chdir(argv[optind])) {
+       flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
        exit(1);
     }
     signal(SIGCHLD, SIG_IGN);