-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.go
543 lines (508 loc) · 12.2 KB
/
editor.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// Ed is limited to displaying these error messages with the exception
// of regular expression errors.
var (
ErrDefault = errors.New("?") // descriptive error message, don't you think?
ErrCannotCloseFile = errors.New("cannot close input file")
ErrCannotNestGlobal = errors.New("cannot nest global commands")
ErrCannotOpenFile = errors.New("cannot open input file")
ErrCannotReadFile = errors.New("cannot read input file")
ErrCannotWriteFile = errors.New("cannot write file")
ErrCryptUnavailable = errors.New("crypt unavailable")
ErrDestinationExpected = errors.New("destination expected")
ErrFileModified = errors.New("warning: file modified")
ErrInterrupt = errors.New("interrupt")
ErrInvalidAddress = errors.New("invalid address")
ErrInvalidCmdSuffix = errors.New("invalid command suffix")
ErrInvalidDestination = errors.New("invalid destination")
ErrInvalidFileName = errors.New("invalid filename")
ErrInvalidMark = errors.New("invalid mark character")
ErrInvalidNumber = errors.New("number out of range")
ErrInvalidPatternDelim = errors.New("invalid pattern delimiter")
ErrInvalidRedirection = errors.New("invalid redirection")
ErrNoCmd = errors.New("no command")
ErrNoFileName = errors.New("no current filename")
ErrNoMatch = errors.New("no match")
ErrNoPrevPattern = errors.New("no previous pattern")
ErrNoPreviousCmd = errors.New("no previous command")
ErrNoPreviousSub = errors.New("no previous substitution")
ErrNothingToUndo = errors.New("nothing to undo")
ErrNumberOutOfRange = errors.New("number out of range")
ErrUnexpectedAddress = errors.New("unexpected address")
ErrUnexpectedCmdSuffix = errors.New("unexpected command suffix")
ErrUnexpectedEOF = errors.New("unexpected end-of-file")
ErrUnknownCmd = errors.New("unknown command")
ErrZero = errors.New("0")
)
type suffix int
const (
suffixPrint suffix = 1 << iota
suffixList
suffixEnumerate
)
const DefaultShell = "/bin/sh"
const DefaultHangupFile = "ed.hup"
const DefaultPrompt = "*"
type Editor struct {
file
cursor
undo
input
re *regexp.Regexp // previous regex
replace string // previous replacement text
scroll int // previous scroll value
err error // previous error
gcmd string // previous global command
g bool // global command state
list []int // indices marked by the global command
prompt bool // state for rendering the prompt
up string // user prompt
verbose bool // toggle verbose errors
silent bool // suppress diagnostics
script bool // stdin is a file
lc int // line count (script mode)
binary bool // TODO(thimc): implement "binary mode" which replaces every NULL character with a newline. When this mode is enabled ed should not append a newline on reading/writing.
sigch chan os.Signal // signal handlers
cs suffix // command suffix
stdin io.Reader
stdout io.Writer
stderr io.Writer
}
type Option func(*Editor)
func WithStdin(stdin io.Reader) Option {
return func(ed *Editor) {
ed.stdin = stdin
ed.input = input{Scanner: bufio.NewScanner(ed.stdin)}
}
}
func WithStdout(stdout io.Writer) Option {
return func(ed *Editor) { ed.stdout = stdout }
}
func WithStderr(stderr io.Writer) Option {
return func(ed *Editor) { ed.stderr = stderr }
}
func WithSilent(t bool) Option {
return func(ed *Editor) { ed.silent = t }
}
func WithPrompt(prompt string) Option {
return func(ed *Editor) {
ed.up = prompt
ed.prompt = ed.up != ""
}
}
func WithFile(path string) Option {
return func(ed *Editor) {
if err := ed.read(path); err != nil {
ed.errorln(true, err)
}
}
}
func NewEditor(opts ...Option) *Editor {
ed := &Editor{
stdin: os.Stdin,
stdout: os.Stdout,
stderr: os.Stderr,
sigch: make(chan os.Signal, 1),
lc: 1,
}
if fi, err := os.Stdin.Stat(); err == nil {
ed.script = fi.Mode()&os.ModeCharDevice == 0
}
for _, opt := range opts {
opt(ed)
}
go ed.handleSignals()
return ed
}
func (ed *Editor) validatePath(path string) (string, error) {
if path != "" {
return path, nil
}
if ed.path == "" {
return "", ErrNoFileName
}
return ed.path, nil
}
func (ed *Editor) validate(f, s int) error {
if ed.addrc == 0 {
ed.first = f
ed.second = s
}
if ed.first > ed.second || ed.first < 1 || ed.second > len(ed.file.lines) {
return ErrInvalidAddress
}
return nil
}
func (ed *Editor) doPrompt() {
if ed.prompt && ed.up != "" {
fmt.Fprint(ed.stdout, ed.up)
}
}
func (ed *Editor) errorln(verbose bool, err error) {
if ed.token() == 'H' {
return
} else if ed.token() == 'h' {
ed.consume()
if ed.err != nil {
fmt.Fprintln(ed.stderr, ed.err)
}
return
}
ed.err = err
if verbose {
if ed.script {
fmt.Fprintf(ed.stderr, "script, line: %d: %s\n", ed.lc, ed.err)
os.Exit(2)
}
fmt.Fprintln(ed.stderr, err)
return
}
fmt.Fprintln(ed.stderr, ErrDefault)
}
func (ed *Editor) run() error {
ed.doPrompt()
if !ed.input.Scan() {
if !ed.file.dirty {
ed.input.pos = -1
return nil
}
WithStdin(ed.stdin)(ed)
ed.doInput("q")
}
if ed.script {
ed.lc++
}
if err := ed.parse(); err != nil {
return err
}
if err := ed.exec(); err != nil {
return err
}
return ed.display(ed.dot, ed.dot, ed.cs)
}
func (ed *Editor) Run() {
for {
err := ed.run()
if ed.input.pos < 0 {
break
}
if err != nil {
ed.errorln(ed.verbose, err)
continue
}
ed.err = nil
}
}
func (ed *Editor) getThirdAddr() (int, error) {
start, end := ed.first, ed.second
if err := ed.parse(); err != nil {
return -1, err
}
if ed.addrc == 0 {
return -1, ErrDestinationExpected
}
if ed.second < 0 || ed.second > len(ed.file.lines) {
return -1, ErrInvalidAddress
}
addr := ed.second
ed.first, ed.second = start, end
return addr, nil
}
func (ed *Editor) read(path string) error {
var (
lines []string
err error
)
path, err = ed.validatePath(path)
if err != nil {
return err
}
if r, _ := utf8.DecodeRuneInString(path); r == '!' {
if path[1:] == "" {
return ErrNoCmd
}
lines, err = ed.shell(path[1:])
if err != nil {
return err
}
ed.file.append(ed.second, lines)
} else {
buf, err := os.ReadFile(path)
if err != nil {
return ErrCannotReadFile
}
lines = strings.Split(strings.TrimSuffix(string(buf), "\n"), "\n")
n := ed.second
if n >= len(ed.file.lines) {
n = 0
}
ed.file = file{
lines: append(ed.file.lines[:n], append(lines, ed.file.lines[n:]...)...),
path: path,
}
ed.undo.reset()
}
size := len(lines)
for _, ln := range lines {
size += len(ln)
}
ed.dot = ed.second + len(lines)
if ed.dot >= len(ed.file.lines) {
ed.dot = len(lines)
}
if !ed.silent {
fmt.Fprintln(ed.stdout, size)
}
return nil
}
func (ed *Editor) append(dot int) error {
for ed.Scan() {
ln := ed.scanString()
if ln == "." {
break
}
ed.file.append(dot, []string{ln})
dot++
ed.undo.append(undoTypeDelete, cursor{first: dot, second: dot, dot: ed.dot}, ed.file.lines[dot-1:dot])
ed.dot = dot
ed.dirty = true
if ed.script {
ed.lc++
}
}
ed.undo.store(ed.g)
return nil
}
func (ed *Editor) delete(start, end int) {
lines := make([]string, end-start+1)
copy(lines, ed.file.lines[start-1:end])
ed.undo.append(undoTypeAdd, cursor{first: start, second: end, dot: ed.dot}, lines)
ed.file.delete(start, end)
ed.dot = start - 1
ed.dirty = true
}
func (ed *Editor) display(start, end int, flags suffix) error {
if flags == 0 {
return nil
}
if start < 1 {
return ErrInvalidAddress
}
for start--; start != end; start++ {
ed.dot = start + 1
var ln string
if flags&suffixEnumerate > 0 {
ln = fmt.Sprintf("%d\t", ed.dot)
}
if flags&suffixList > 0 {
quoted := strings.Replace(strconv.QuoteToASCII(ed.file.lines[start]), "$", "\\$", -1)
ln += fmt.Sprintf("%s$", quoted[1:len(quoted)-1])
} else {
ln += ed.file.lines[start]
}
fmt.Fprintln(ed.stdout, ln)
}
ed.cs = 0
return nil
}
func (ed *Editor) shell(args string) ([]string, error) {
var sb strings.Builder
count := utf8.RuneCountInString(args)
for i := 0; i < count; {
r, w := utf8.DecodeRuneInString(args[i:])
if i+w < count {
next, nw := utf8.DecodeRuneInString(args[i+w:])
if next == '%' {
i += w + nw
if r == '\\' {
sb.WriteRune(next)
} else {
var err error
ed.path, err = ed.validatePath(ed.path)
if err != nil {
return nil, err
}
sb.WriteRune(r)
sb.WriteString(ed.path)
}
continue
}
}
i += w
sb.WriteRune(r)
}
cmd := exec.Command(DefaultShell, "-c", sb.String())
output, err := cmd.Output()
if err != nil {
return nil, err
}
return strings.Split(strings.TrimRight(string(output), "\n"), "\n"), err
}
func (ed *Editor) getSuffix() error {
var ok bool
r := ed.token()
for !ok {
r = ed.token()
switch r {
case 'n':
ed.cs |= suffixEnumerate
ed.consume()
case 'l':
ed.cs |= suffixList
ed.consume()
case 'p':
ed.cs |= suffixPrint
ed.consume()
default:
ok = true
}
}
if !ed.input.eof() && r != '\n' {
return ErrInvalidCmdSuffix
}
return nil
}
func (ed *Editor) buildList(g, interactive bool) error {
delim := ed.token()
if delim == ' ' || delim == '\n' || ed.input.eof() {
return ErrInvalidPatternDelim
}
ed.consume()
search, _ := ed.scanStringUntil(delim)
if ed.token() == delim {
ed.consume()
}
var (
re *regexp.Regexp
err error
)
if search == "" {
if ed.re == nil {
return ErrNoPrevPattern
}
re = ed.re
} else {
re, err = regexp.Compile(search)
if err != nil {
return err
}
}
if interactive {
if err := ed.getSuffix(); err != nil {
return err
}
}
ed.list = []int{}
for i := ed.first - 1; i < ed.second; i++ {
if re.MatchString(ed.file.lines[i]) == g {
ed.list = append(ed.list, i+1)
}
}
ed.re = re
return nil
}
func (ed *Editor) cmdList() (string, error) {
var (
sb strings.Builder
done bool
ln string
)
if !ed.input.eof() {
ln = ed.scanString()
}
for {
done = true
if strings.HasSuffix(ln, "\\") {
ln = strings.TrimSuffix(ln, "\\")
done = false
ed.consume()
}
sb.WriteString(ln)
if !done {
if !ed.input.Scan() {
return "", ErrUnexpectedEOF
}
ln = ed.input.buf
continue
}
break
}
return sb.String(), nil
}
func (ed *Editor) substitute(re *regexp.Regexp, replace string, nth int) error {
var subs int
for i := ed.first - 1; i < ed.second; i++ {
if !re.MatchString(ed.file.lines[i]) {
continue
}
submatch := re.FindAllStringSubmatch(ed.file.lines[i], -1)
matches := re.FindAllStringIndex(ed.file.lines[i], -1)
for mi, match := range matches {
if nth > 0 && mi != nth-1 {
continue
}
start, end := match[0], match[1]
var sb strings.Builder
sb.WriteString(ed.file.lines[i][:start])
count := utf8.RuneCountInString(replace)
for j := 0; j < count; {
r, w := utf8.DecodeRuneInString(replace[j:])
if j+w < count {
next, nw := utf8.DecodeRuneInString(replace[j+w:])
if r == '\\' && next == '&' {
j += w + nw
sb.WriteRune(next)
continue
} else if r == '\\' && unicode.IsDigit(next) {
j += w + nw
d, err := strconv.Atoi(string(next))
if err != nil || d < 1 || d > len(submatch) {
return ErrNumberOutOfRange
}
if d >= len(submatch[0]) {
sb.WriteRune(next)
} else if d > 0 {
sb.WriteString(submatch[0][d])
}
continue
}
}
if r == '&' {
sb.WriteString(ed.file.lines[i][start:end])
j += w
continue
}
j += w
sb.WriteRune(r)
}
// TODO(thimc): Handle embedded newlines in the replacement string.
sb.WriteString(ed.file.lines[i][end:])
ed.undo.append(undoTypeAdd, cursor{first: i + 1, second: i + 1, dot: ed.dot}, []string{ed.file.lines[i]})
ed.undo.append(undoTypeDelete, cursor{first: i + 1, second: i + 1, dot: ed.dot}, []string{sb.String()})
ed.file.lines[i] = sb.String()
ed.dirty = true
ed.dot = i + 1
subs++
}
}
ed.re = re
ed.replace = replace
if subs == 0 && !ed.g {
return ErrNoMatch
}
ed.undo.store(ed.g)
return ed.display(ed.dot, ed.dot, ed.cs)
}