Blame


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