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 int
63 threadmaybackground(void)
64 {
65 return 1;
66 }
68 void
69 threadmain(int argc, char* argv[])
70 {
71 char **cmd, *p;
72 int i, ncmd, tflag;
74 fmtinstall('D', dirfmt);
75 fmtinstall('F', fcallfmt);
76 fmtinstall('M', dirmodefmt);
77 quotefmtinstall();
79 /*
80 * Insulate from the invoker's environment.
81 */
82 #ifdef PLAN9PORT
83 if(rfork(RFNAMEG) < 0)
84 #else
85 if(rfork(RFREND|RFNOTEG|RFNAMEG) < 0)
86 #endif
87 sysfatal("rfork: %r");
89 close(0);
90 open("/dev/null", OREAD);
91 close(1);
92 open("/dev/null", OWRITE);
94 cmd = nil;
95 ncmd = tflag = 0;
97 ARGBEGIN{
98 case '?':
99 default:
100 usage();
101 break;
102 case 'c':
103 p = EARGF(usage());
104 currfsysname = p;
105 cmd = vtrealloc(cmd, (ncmd+1)*sizeof(char*));
106 cmd[ncmd++] = p;
107 break;
108 case 'D':
109 Dflag ^= 1;
110 break;
111 case 'f':
112 p = EARGF(usage());
113 currfsysname = foptname = p;
114 readCmdPart(p, &cmd, &ncmd);
115 break;
116 case 'm':
117 mempcnt = atoi(EARGF(usage()));
118 if(mempcnt <= 0 || mempcnt >= 100)
119 usage();
120 break;
121 case 't':
122 tflag = 1;
123 break;
124 }ARGEND
125 if(argc != 0)
126 usage();
128 consInit();
129 cliInit();
130 msgInit();
131 conInit();
132 cmdInit();
133 fsysInit();
134 exclInit();
135 fidInit();
137 srvInit();
138 lstnInit();
139 usersInit();
141 for(i = 0; i < ncmd; i++)
142 if(cliExec(cmd[i]) == 0)
143 fprint(2, "%s: %r\n", cmd[i]);
144 vtfree(cmd);
146 if(tflag && consTTY() == 0)
147 consPrint("%r\n");
149 threadexits(0);