-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdat.go
More file actions
317 lines (279 loc) · 4.88 KB
/
dat.go
File metadata and controls
317 lines (279 loc) · 4.88 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"9fans.net/go/draw"
"bytes"
"fmt"
"image"
"io/ioutil"
"os"
"os/user"
"sync"
"unicode/utf8"
)
const (
Qdir int = iota
Qacme
Qcons
Qconsctl
Qdraw
Qeditout
Qindex
Qlabel
Qnew
QWaddr
QWbody
QWctl
QWdata
QWeditout
QWerrors
QWevent
QWrdsel
QWwrsel
QWtag
QWxdata
QMAX
Blockincr = 256
MaxBlock = 8 * 1024
NRange = 10
Infinity = 0x7FFFFFFF
STACK = 65536
Empty = 0
Null = '-'
Delete = 'd'
Insert = 'i'
Replace = 'r'
Filename = 'f'
Inactive = 0
Inserting = 1
Collecting = 2
NCOL = 5
)
var (
blist *Block
globalincref bool
seq uint
maxtab uint /*size of a tab, in units of the '0' character */
display *draw.Display
screen *draw.Image
font *draw.Font
mouse *draw.Mouse
mousectl *draw.Mousectl
keyboardctl *draw.Keyboardctl
reffont Reffont
modbutton *draw.Image
colbutton *draw.Image
button *draw.Image
but2col *draw.Image
but3col *draw.Image
// boxcursor Cursor
row Row
timerpid int
disk *Disk
seltext *Text
argtext *Text
mousetext *Text
typetext *Text
barttext *Text
bartflag int
swapscrollbuttons int
activewin *Window
activecol *Column
snarfbuf Buffer
nullrect image.Rectangle
fsyspid int
cputype string
objtype string
acmeshell string
tagcols [NCOL]*draw.Image
textcols [NCOL]*draw.Image
wdir string
editing bool
erroutfd int
messagesize int
globalautoindent bool
dodollarsigns bool
mtpt string
// cplumb chan *Plumbmsg
// cwait chan Waitmsg
ccommand chan Command
ckill chan []rune
cxfidalloc chan *Xfid
cxfidfree chan *Xfid
cnewwindow chan chan interface{}
mouseexit0 chan int
mouseexit1 chan int
cexit chan int
cerr chan string
cedit chan int
cwarn chan uint
editoutlk *sync.Mutex
)
type Range struct {
q0, q1 int
}
type Block struct {
addr uint // disk address in bytes
n uint // number of used runes in block
next *Block // pointer to next in free list
}
type Disk struct {
fd *os.File
addr uint
free [MaxBlock/Blockincr + 1]*Block
}
func NewDisk() *Disk {
d := new(Disk)
u, err := user.Current()
if err != nil {
panic(err)
}
tmp, err := ioutil.TempFile("/tmp", fmt.Sprintf("X%d.%.4sacme", os.Getpid(), u.Username))
if err != nil {
panic(err)
}
d.fd = tmp
return d
}
func ntosize(n uint) (uint, uint) {
if n > MaxBlock {
panic("internal error: ntosize")
}
size := n
if size&(Blockincr-1) != 0 {
size += Blockincr - (size & (Blockincr - 1))
}
// last bucket holds blocks of exactly Maxblock
ip := size / Blockincr
return size, ip
}
func (d *Disk) NewBlock(n uint) *Block {
size, i := ntosize(n)
b := d.free[i]
if b != nil {
d.free[i] = b.next
} else {
if blist == nil {
bl := new(Block)
blist = bl
for j := 0; j < 100-1; j++ {
bl.next = new(Block)
bl = bl.next
}
}
b = blist
blist = b.next
b.addr = d.addr
d.addr += size
}
b.n = n
return b
}
func (d *Disk) Release(b *Block) {
_, i := ntosize(b.n)
b.next = d.free[i]
d.free[i] = b
}
func (d *Disk) Read(b *Block, r []rune, n uint) {
if n > b.n {
panic("internal error: disk.Read")
}
// this is a simplified way of checking that b.n < MaxBlock
_, _ = ntosize(b.n)
buf := make([]byte, n)
if m, err := d.fd.ReadAt(buf, int64(b.addr)); err != nil {
panic(err)
} else if m != len(r) {
panic("read error from temp file")
}
copy(r, bytes.Runes(buf))
}
func (d *Disk) Write(bp **Block, r []rune, n uint) {
bl := *bp
size, _ := ntosize(bl.n)
nsize, _ := ntosize(n)
if size != nsize {
d.Release(bl)
bl = d.NewBlock(n)
*bp = bl
}
if m, err := d.fd.WriteAt([]byte(string(r)), int64(bl.addr)); err != nil {
panic(err)
} else if m != len(r) {
panic("write error to temp file")
}
bl.n = n
}
type Command struct {
pid int
name []rune
text string
av []string
iseditcommand bool
md *MntDir
next *Command
}
type DirTab struct {
name string
t byte
qid uint
perm uint
}
type MntDir struct {
id int
ref int
dir string
ndir int
next *MntDir
nincl int
incl []string
}
type Fid struct {
fid int
busy int
open int
//qid Qid
w *Window
dir *DirTab
next *Fid
mntdir *MntDir
nrpart int
rpart [utf8.UTFMax]byte
}
type Xfid struct {
arg interface{}
// fcall Fcall
next *Xfid
c chan func(*Xfid)
f *Fid
buf []byte
flushed bool
}
type Reffont struct {
ref Ref
f *draw.Font
}
type RangeSet [NRange]Range
type Dirlist struct {
r []rune
nr int
wid int
}
type Expand struct {
q0 uint
q1 uint
name string
bname string
jump int
at *Text
ar []rune
agetc func(interface{}, uint) int
a0 int
a1 int
}
type Ref int
func (r *Ref) Inc() {
*r++
}
func (r *Ref) Dec() {
*r--
}