/
acme.go
187 lines (162 loc) · 3.97 KB
/
acme.go
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
package main
import (
"code.google.com/p/goplan9/plan9/acme"
"fmt"
"io"
"os"
"strconv"
"os/user"
"strings"
"github.com/stretchr/stew/slice"
)
var Marcs []byte = []byte{'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '=', '+', '[', ']', '{', '}', '|', ';', ':', '\'', '"', '\b', '\n', '\r', '\t', '\\', '<', '>','?', ',', '.', ' ', }
var marks []uint8 = []uint8(Marcs)
type acmeFile struct {
name string
body []byte
offset int
runeOffset int
}
func acmeCurrentFile() (*acmeFile, error) {
win, err := acmeCurrentWin()
if err != nil {
return nil, err
}
defer win.CloseFiles()
carrent_win = win
_, _, err = win.ReadAddr() // make sure address file is already open.
if err != nil {
return nil, fmt.Errorf("cannot read address: %v", err)
}
err = win.Ctl("addr=dot")
if err != nil {
return nil, fmt.Errorf("cannot set addr=dot: %v", err)
}
q0, q1, err := win.ReadAddr()
if err != nil {
return nil, fmt.Errorf("cannot read address: %v", err)
}
body, err := readBody(win)
if err != nil {
return nil, fmt.Errorf("cannot read body: %v", err)
}
tagb, err := win.ReadAll("tag")
if err != nil {
return nil, fmt.Errorf("cannot read tag: %v", err)
}
tag := string(tagb)
i := strings.Index(tag, " ")
if i == -1 {
return nil, fmt.Errorf("strange tag with no spaces")
}
q0exp = q0
q1exp = q1
for i:=q0-1; slice.ContainsUInt8(marks, uint8(body[i])) == false; i-- {
q0exp=i
}
for i:=q1; slice.ContainsUInt8(marks, uint8(body[i])) == false; i++ {
q1exp=i+1
}
if err != nil {
return nil, fmt.Errorf("cannot set dot=addr: %v", err)
}
w := &acmeFile{
name: tag[0:i],
body: body,
offset: runeOffset2ByteOffset(body, q0),
runeOffset: q0,
}
return w, nil
}
// We would use win.ReadAll except for a bug in acme
// where it crashes when reading trying to read more
// than the negotiated 9P message size.
func readBody(win *acme.Win) ([]byte, error) {
var body []byte
buf := make([]byte, 8000)
for {
n, err := win.Read("body", buf)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
body = append(body, buf[0:n]...)
}
return body, nil
}
func acmeCurrentWin() (*acme.Win, error) {
winid := os.Getenv("winid")
if winid == "" {
return nil, fmt.Errorf("$winid not set - not running inside acme?")
}
id, err := strconv.Atoi(winid)
if err != nil {
return nil, fmt.Errorf("invalid $winid %q", winid)
}
if err := setNameSpace(); err != nil {
return nil, err
}
carrent_win_id = id
win, err := acme.Open(id, nil)
if err != nil {
return nil, fmt.Errorf("cannot open acme window: %v", err)
}
return win, nil
}
func runeOffset2ByteOffset(b []byte, off int) int {
r := 0
for i, _ := range string(b) {
if r == off {
return i
}
r++
}
return len(b)
}
func setNameSpace() error {
if ns := os.Getenv("NAMESPACE"); ns != "" {
return nil
}
ns, err := nsFromDisplay()
if err != nil {
return fmt.Errorf("cannot get name space: %v", err)
}
os.Setenv("NAMESPACE", ns)
return nil
}
// taken from src/lib9/getns.c
// This should go into goplan9/plan9/client.
func nsFromDisplay() (string, error) {
disp := os.Getenv("DISPLAY")
if disp == "" {
// original code had heuristic for OS X here;
// we'll just assume that and fail anyway if it
// doesn't work.
disp = ":0.0"
}
// canonicalize: xxx:0.0 => xxx:0
if i := strings.LastIndex(disp, ":"); i >= 0 {
if strings.HasSuffix(disp, ".0") {
disp = disp[:len(disp)-2]
}
}
// turn /tmp/launch/:0 into _tmp_launch_:0 (OS X 10.5)
disp = strings.Replace(disp, "/", "_", -1)
u, err := user.Current()
if err != nil {
return "", fmt.Errorf("cannot get current user name: %v", err)
}
ns := fmt.Sprintf("/tmp/ns.%s.%s", u.Username, disp)
_, err = os.Stat(ns)
if os.IsNotExist(err) {
return "", fmt.Errorf("no name space directory found")
}
if err != nil {
return "", fmt.Errorf("cannot stat name space directory: %v", err)
}
// heuristics for checking permissions and owner of name space
// directory omitted.
return ns, nil
}