html5dom.c (1358B)
1 #include <u.h> 2 #include <libc.h> 3 #include <String.h> 4 #include <thread.h> 5 6 #include "html5dom.h" 7 8 static char *drpath = "/mnt/dom"; 9 static char *tpath = nil; 10 11 void 12 usage(void) 13 { 14 fprint(2, "usage: %s [-m /mnt/dom] [-n 123]\n", argv0); 15 threadexitsall("usage"); 16 } 17 18 void 19 threadmain(int argc, char **argv) 20 { 21 Dir *d; 22 ARGBEGIN{ 23 case 'm': 24 drpath = EARGF(usage()); 25 break; 26 case 'n': 27 tpath = EARGF(usage()); 28 default: 29 usage(); 30 } ARGEND; 31 if (argc != 0) usage(); 32 33 d = dirstat(drpath); 34 if (d==nil) sysfatal("%r"); 35 if ((d->mode & DMDIR) == 0) sysfatal("%s - not a directory", drpath); 36 if (chdir(drpath) != 0) sysfatal("can't chdir to %s, %r", drpath); 37 if (tpath == nil) { 38 char *buf[128]; 39 long n; 40 int fd; 41 fd = open("new", OREAD); 42 if (fd < 0) sysfatal("can't open %s/new. %r", drpath); 43 n = read(fd, buf, 128); 44 if (n <= 0) sysfatal("failed to read from %s/new. %r", drpath); 45 tpath = mallocz(n+1, 1); 46 memmove(tpath, buf, n); 47 if (tpath[n-1] == '\n') tpath[n-1] = '\0'; 48 close(fd); 49 } 50 if (chdir(tpath) != 0) sysfatal("can't chdir to %s, %r", tpath); 51 52 Tokctl *tc; 53 Treeconstrctl *trc; 54 tc = malloc(sizeof(Tokctl)); 55 tc->c = chancreate(sizeof(Token*), 1024); 56 trc = malloc(sizeof(Treeconstrctl)); 57 trc->treeroot = "."; 58 trc->in = tc->c; 59 60 61 threadcreate(threadtokenize, tc, 64 * 1024); 62 threadcreate(threadtreeconstr, trc, 64 * 1024); 63 }