Blob


1 #include "stdinc.h"
2 #include <ctype.h>
4 #include "9.h"
6 int Dflag;
7 int mempcnt; /* for 9fsys.c */
8 char* none = "none";
9 char* foptname = "/none/such";
11 static void
12 usage(void)
13 {
14 fprint(2, "usage: %s [-Dt] [-c cmd] [-f partition] [-m %%]\n", argv0);
15 threadexitsall("usage");
16 }
18 static void
19 readCmdPart(char *file, char ***pcmd, int *pncmd)
20 {
21 char buf[1024+1], *f[1024];
22 char tbuf[1024];
23 int nf;
24 int i, fd, n;
25 char **cmd, *p;
26 int ncmd;
28 cmd = *pcmd;
29 ncmd = *pncmd;
31 if((fd = open(file, OREAD)) < 0)
32 sysfatal("open %s: %r", file);
33 if(seek(fd, 127*1024, 0) != 127*1024)
34 sysfatal("seek %s 127kB: %r", file);
35 n = readn(fd, buf, sizeof buf-1);
36 if(n == 0)
37 sysfatal("short read of %s at 127kB", file);
38 if(n < 0)
39 sysfatal("read %s: %r", file);
40 buf[n] = 0;
41 if(memcmp(buf, "fossil config\n", 6+1+6+1) != 0)
42 sysfatal("bad config magic in %s", file);
43 nf = getfields(buf+6+1+6+1, f, nelem(f), 1, "\n");
44 for(i=0; i<nf; i++){
45 if(f[i][0] == '#')
46 continue;
47 cmd = vtrealloc(cmd, (ncmd+1)*sizeof(char*));
48 /* expand argument '*' to mean current file */
49 if((p = strchr(f[i], '*')) && (p==f[i]||isspace(p[-1])) && (p[1]==0||isspace(p[1]))){
50 memmove(tbuf, f[i], p-f[i]);
51 strecpy(tbuf+(p-f[i]), tbuf+sizeof tbuf, file);
52 strecpy(tbuf+strlen(tbuf), tbuf+sizeof tbuf, p+1);
53 f[i] = tbuf;
54 }
55 cmd[ncmd++] = vtstrdup(f[i]);
56 }
57 close(fd);
58 *pcmd = cmd;
59 *pncmd = ncmd;
60 }
62 void
63 threadmain(int argc, char* argv[])
64 {
65 char **cmd, *p;
66 int i, ncmd, tflag;
68 fmtinstall('D', dirfmt);
69 fmtinstall('F', fcallfmt);
70 fmtinstall('M', dirmodefmt);
71 quotefmtinstall();
73 /*
74 * Insulate from the invoker's environment.
75 */
76 #ifdef PLAN9PORT
77 if(rfork(RFNAMEG) < 0)
78 #else
79 if(rfork(RFREND|RFNOTEG|RFNAMEG) < 0)
80 #endif
81 sysfatal("rfork: %r");
83 close(0);
84 open("/dev/null", OREAD);
85 close(1);
86 open("/dev/null", OWRITE);
88 cmd = nil;
89 ncmd = tflag = 0;
91 ARGBEGIN{
92 case '?':
93 default:
94 usage();
95 break;
96 case 'c':
97 p = EARGF(usage());
98 currfsysname = p;
99 cmd = vtrealloc(cmd, (ncmd+1)*sizeof(char*));
100 cmd[ncmd++] = p;
101 break;
102 case 'D':
103 Dflag ^= 1;
104 break;
105 case 'f':
106 p = EARGF(usage());
107 currfsysname = foptname = p;
108 readCmdPart(p, &cmd, &ncmd);
109 break;
110 case 'm':
111 mempcnt = atoi(EARGF(usage()));
112 if(mempcnt <= 0 || mempcnt >= 100)
113 usage();
114 break;
115 case 't':
116 tflag = 1;
117 break;
118 }ARGEND
119 if(argc != 0)
120 usage();
122 consInit();
123 cliInit();
124 msgInit();
125 conInit();
126 cmdInit();
127 fsysInit();
128 exclInit();
129 fidInit();
131 srvInit();
132 lstnInit();
133 usersInit();
135 for(i = 0; i < ncmd; i++)
136 if(cliExec(cmd[i]) == 0)
137 fprint(2, "%s: %r\n", cmd[i]);
138 vtfree(cmd);
140 if(tflag && consTTY() == 0)
141 consPrint("%r\n");
143 threadexits(0);