1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>
#include "dat.h"
Channel *fschan;
Channel *writechan;
static File *devcons, *devnew;
static void
fsread(Req *r)
{
Fsevent e;
if(r->fid->file == devnew){
if(r->fid->aux==nil){
respond(r, "phase error");
return;
}
readstr(r, r->fid->aux);
respond(r, nil);
return;
}
assert(r->fid->file == devcons);
e.type = 'r';
e.r = r;
send(fschan, &e);
}
static void
fsflush(Req *r)
{
Fsevent e;
e.type = 'f';
e.r = r;
send(fschan, &e);
}
static void
fswrite(Req *r)
{
static Event *e[4];
Event *ep;
int i, j, ei, nb, wid, pid;
Rune rune;
char *s;
char tmp[UTFmax], *t;
static int n, partial;
if(r->fid->file == devnew){
if(r->fid->aux){
respond(r, "already created a window");
return;
}
s = emalloc(r->ifcall.count+1);
memmove(s, r->ifcall.data, r->ifcall.count);
s[r->ifcall.count] = 0;
pid = strtol(s, &t, 0);
if(*t==' ')
t++;
i = newpipewin(pid, t);
free(s);
s = emalloc(32);
sprint(s, "%lud", (ulong)i);
r->fid->aux = s;
r->ofcall.count = r->ifcall.count;
respond(r, nil);
return;
}
assert(r->fid->file == devcons);
if(e[0] == nil){
for(i=0; i<nelem(e); i++){
e[i] = emalloc(sizeof(Event));
e[i]->c1 = 'S';
}
}
ep = e[n];
n = (n+1)%nelem(e);
assert(r->ifcall.count <= 8192); /* is this guaranteed by lib9p? */
nb = r->ifcall.count;
memmove(ep->b+partial, r->ifcall.data, nb);
nb += partial;
ep->b[nb] = '\0';
if(strlen(ep->b) < nb){ /* nulls in data */
t = ep->b;
for(i=j=0; i<nb; i++)
if(ep->b[i] != '\0')
t[j++] = ep->b[i];
nb = j;
t[j] = '\0';
}
ei = nb>8192? 8192 : nb;
/* process bytes into runes, transferring terminal partial runes into next buffer */
for(i=j=0; i<ei && fullrune(ep->b+i, ei-i); i+=wid,j++)
wid = chartorune(&rune, ep->b+i);
memmove(tmp, ep->b+i, nb-i);
partial = nb-i;
ep->nb = i;
ep->nr = j;
ep->b[i] = '\0';
if(i != 0){
sendp(win->cevent, ep);
recvp(writechan);
}
partial = nb-i;
memmove(e[n]->b, tmp, partial);
r->ofcall.count = r->ifcall.count;
respond(r, nil);
}
void
fsdestroyfid(Fid *fid)
{
if(fid->aux)
free(fid->aux);
}
Srv fs = {
.read= fsread,
.write= fswrite,
.flush= fsflush,
.destroyfid= fsdestroyfid,
.leavefdsopen= 1,
};
void
mountcons(void)
{
fschan = chancreate(sizeof(Fsevent), 0);
writechan = chancreate(sizeof(void*), 0);
fs.tree = alloctree("win", "win", DMDIR|0555, nil);
devcons = createfile(fs.tree->root, "cons", "win", 0666, nil);
if(devcons == nil)
sysfatal("creating /dev/cons: %r");
devnew = createfile(fs.tree->root, "wnew", "win", 0666, nil);
if(devnew == nil)
sysfatal("creating /dev/wnew: %r");
threadpostmountsrv(&fs, nil, "/dev", MBEFORE);
}
|