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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <fcall.h>
#include <plumb.h>
#include "dat.h"
#include "edit.h"
#include "fns.h"
static char linex[]="\n";
static char wordx[]=" \t\n";
struct cmdtab cmdtab[]={
/* cmdc text regexp addr defcmd defaddr count token fn */
'\n', 0, 0, 0, 0, aDot, 0, 0, nl_cmd,
'a', 1, 0, 0, 0, aDot, 0, 0, a_cmd,
'b', 0, 0, 0, 0, aNo, 0, linex, b_cmd,
'c', 1, 0, 0, 0, aDot, 0, 0, c_cmd,
'd', 0, 0, 0, 0, aDot, 0, 0, d_cmd,
'e', 0, 0, 0, 0, aNo, 0, wordx, e_cmd,
'f', 0, 0, 0, 0, aNo, 0, wordx, f_cmd,
'g', 0, 1, 0, 'p', aDot, 0, 0, g_cmd,
'i', 1, 0, 0, 0, aDot, 0, 0, i_cmd,
'm', 0, 0, 1, 0, aDot, 0, 0, m_cmd,
'p', 0, 0, 0, 0, aDot, 0, 0, p_cmd,
'r', 0, 0, 0, 0, aDot, 0, wordx, e_cmd,
's', 0, 1, 0, 0, aDot, 1, 0, s_cmd,
't', 0, 0, 1, 0, aDot, 0, 0, m_cmd,
'u', 0, 0, 0, 0, aNo, 2, 0, u_cmd,
'v', 0, 1, 0, 'p', aDot, 0, 0, g_cmd,
'w', 0, 0, 0, 0, aAll, 0, wordx, w_cmd,
'x', 0, 1, 0, 'p', aDot, 0, 0, x_cmd,
'y', 0, 1, 0, 'p', aDot, 0, 0, x_cmd,
'=', 0, 0, 0, 0, aDot, 0, linex, eq_cmd,
'B', 0, 0, 0, 0, aNo, 0, linex, B_cmd,
'D', 0, 0, 0, 0, aNo, 0, linex, D_cmd,
'X', 0, 1, 0, 'f', aNo, 0, 0, X_cmd,
'Y', 0, 1, 0, 'f', aNo, 0, 0, X_cmd,
'<', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
'|', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
'>', 0, 0, 0, 0, aDot, 0, linex, pipe_cmd,
/* deliberately unimplemented:
'k', 0, 0, 0, 0, aDot, 0, 0, k_cmd,
'n', 0, 0, 0, 0, aNo, 0, 0, n_cmd,
'q', 0, 0, 0, 0, aNo, 0, 0, q_cmd,
'!', 0, 0, 0, 0, aNo, 0, linex, plan9_cmd,
*/
0, 0, 0, 0, 0, 0, 0, 0
};
Cmd *parsecmd(int);
Addr *compoundaddr(void);
Addr *simpleaddr(void);
void freecmd(void);
void okdelim(int);
Rune *cmdstartp;
Rune *cmdendp;
Rune *cmdp;
Channel *editerrc;
String *lastpat;
int patset;
List cmdlist;
List addrlist;
List stringlist;
Text *curtext;
int editing = Inactive;
String* newstring(int);
void
editthread(void *v)
{
Cmd *cmdp;
USED(v);
threadsetname("editthread");
while((cmdp=parsecmd(0)) != 0){
if(cmdexec(curtext, cmdp) == 0)
break;
freecmd();
}
sendp(editerrc, nil);
}
void
allelogterm(Window *w, void *x)
{
USED(x);
elogterm(w->body.file);
}
void
alleditinit(Window *w, void *x)
{
USED(x);
textcommit(&w->tag, TRUE);
textcommit(&w->body, TRUE);
w->body.file->editclean = FALSE;
}
void
allupdate(Window *w, void *x)
{
Text *t;
int i;
File *f;
USED(x);
t = &w->body;
f = t->file;
if(f->curtext != t) /* do curtext only */
return;
if(f->elog.type == Null)
elogterm(f);
else if(f->elog.type != Empty){
elogapply(f);
if(f->editclean){
f->mod = FALSE;
for(i=0; i<f->ntext; i++)
f->text[i]->w->dirty = FALSE;
}
}
textsetselect(t, t->q0, t->q1);
textscrdraw(t);
winsettag(w);
}
void
editerror(char *fmt, ...)
{
va_list arg;
char *s;
va_start(arg, fmt);
s = vsmprint(fmt, arg);
va_end(arg);
freecmd();
allwindows(allelogterm, nil); /* truncate the edit logs */
sendp(editerrc, s);
threadexits(nil);
}
void
editcmd(Text *ct, Rune *r, uint n)
{
char *err;
if(n == 0)
return;
if(2*n > RBUFSIZE){
warning(nil, "string too long\n");
return;
}
allwindows(alleditinit, nil);
if(cmdstartp)
free(cmdstartp);
cmdstartp = runemalloc(n+2);
runemove(cmdstartp, r, n);
if(r[n] != '\n')
cmdstartp[n++] = '\n';
cmdstartp[n] = '\0';
cmdendp = cmdstartp+n;
cmdp = cmdstartp;
if(ct->w == nil)
curtext = nil;
else
curtext = &ct->w->body;
resetxec();
if(editerrc == nil){
editerrc = chancreate(sizeof(char*), 0);
chansetname(editerrc, "editerrc");
lastpat = allocstring(0);
}
threadcreate(editthread, nil, STACK);
err = recvp(editerrc);
editing = Inactive;
if(err != nil){
if(err[0] != '\0')
warning(nil, "Edit: %s\n", err);
free(err);
}
/* update everyone whose edit log has data */
allwindows(allupdate, nil);
}
int
getch(void)
{
if(*cmdp == *cmdendp)
return -1;
return *cmdp++;
}
int
nextc(void)
{
if(*cmdp == *cmdendp)
return -1;
return *cmdp;
}
void
ungetch(void)
{
if(--cmdp < cmdstartp)
error("ungetch");
}
long
getnum(int signok)
{
long n;
int c, sign;
n = 0;
sign = 1;
if(signok>1 && nextc()=='-'){
sign = -1;
getch();
}
if((c=nextc())<'0' || '9'<c) /* no number defaults to 1 */
return sign;
while('0'<=(c=getch()) && c<='9')
n = n*10 + (c-'0');
ungetch();
return sign*n;
}
int
cmdskipbl(void)
{
int c;
do
c = getch();
while(c==' ' || c=='\t');
if(c >= 0)
ungetch();
return c;
}
/*
* Check that list has room for one more element.
*/
void
growlist(List *l)
{
if(l->u.listptr==0 || l->nalloc==0){
l->nalloc = INCR;
l->u.listptr = emalloc(INCR*sizeof(void*));
l->nused = 0;
}else if(l->nused == l->nalloc){
l->u.listptr = erealloc(l->u.listptr, (l->nalloc+INCR)*sizeof(void*));
memset(l->u.ptr+l->nalloc, 0, INCR*sizeof(void*));
l->nalloc += INCR;
}
}
/*
* Remove the ith element from the list
*/
void
dellist(List *l, int i)
{
memmove(&l->u.ptr[i], &l->u.ptr[i+1], (l->nused-(i+1))*sizeof(void*));
l->nused--;
}
/*
* Add a new element, whose position is i, to the list
*/
void
inslist(List *l, int i, void *v)
{
growlist(l);
memmove(&l->u.ptr[i+1], &l->u.ptr[i], (l->nused-i)*sizeof(void*));
l->u.ptr[i] = v;
l->nused++;
}
void
listfree(List *l)
{
free(l->u.listptr);
free(l);
}
String*
allocstring(int n)
{
String *s;
s = emalloc(sizeof(String));
s->n = n;
s->nalloc = n+10;
s->r = emalloc(s->nalloc*sizeof(Rune));
s->r[n] = '\0';
return s;
}
void
freestring(String *s)
{
free(s->r);
free(s);
}
Cmd*
newcmd(void){
Cmd *p;
p = emalloc(sizeof(Cmd));
inslist(&cmdlist, cmdlist.nused, p);
return p;
}
String*
newstring(int n)
{
String *p;
p = allocstring(n);
inslist(&stringlist, stringlist.nused, p);
return p;
}
Addr*
newaddr(void)
{
Addr *p;
p = emalloc(sizeof(Addr));
inslist(&addrlist, addrlist.nused, p);
return p;
}
void
freecmd(void)
{
int i;
while(cmdlist.nused > 0)
free(cmdlist.u.ucharptr[--cmdlist.nused]);
while(addrlist.nused > 0)
free(addrlist.u.ucharptr[--addrlist.nused]);
while(stringlist.nused>0){
i = --stringlist.nused;
freestring(stringlist.u.stringptr[i]);
}
}
void
okdelim(int c)
{
if(c=='\\' || ('a'<=c && c<='z')
|| ('A'<=c && c<='Z') || ('0'<=c && c<='9'))
editerror("bad delimiter %c\n", c);
}
void
atnl(void)
{
int c;
cmdskipbl();
c = getch();
if(c != '\n')
editerror("newline expected (saw %C)", c);
}
void
Straddc(String *s, int c)
{
if(s->n+1 >= s->nalloc){
s->nalloc += 10;
s->r = erealloc(s->r, s->nalloc*sizeof(Rune));
}
s->r[s->n++] = c;
s->r[s->n] = '\0';
}
void
getrhs(String *s, int delim, int cmd)
{
int c;
while((c = getch())>0 && c!=delim && c!='\n'){
if(c == '\\'){
if((c=getch()) <= 0)
error("bad right hand side");
if(c == '\n'){
ungetch();
c='\\';
}else if(c == 'n')
c='\n';
else if(c!=delim && (cmd=='s' || c!='\\')) /* s does its own */
Straddc(s, '\\');
}
Straddc(s, c);
}
ungetch(); /* let client read whether delimiter, '\n' or whatever */
}
String *
collecttoken(char *end)
{
String *s = newstring(0);
int c;
while((c=nextc())==' ' || c=='\t')
Straddc(s, getch()); /* blanks significant for getname() */
while((c=getch())>0 && utfrune(end, c)==0)
Straddc(s, c);
if(c != '\n')
atnl();
return s;
}
String *
collecttext(void)
{
String *s;
int begline, i, c, delim;
s = newstring(0);
if(cmdskipbl()=='\n'){
getch();
i = 0;
do{
begline = i;
while((c = getch())>0 && c!='\n')
i++, Straddc(s, c);
i++, Straddc(s, '\n');
if(c < 0)
goto Return;
}while(s->r[begline]!='.' || s->r[begline+1]!='\n');
s->r[s->n-2] = '\0';
s->n -= 2;
}else{
okdelim(delim = getch());
getrhs(s, delim, 'a');
if(nextc()==delim)
getch();
atnl();
}
Return:
return s;
}
int
cmdlookup(int c)
{
int i;
for(i=0; cmdtab[i].cmdc; i++)
if(cmdtab[i].cmdc == c)
return i;
return -1;
}
Cmd*
parsecmd(int nest)
{
int i, c;
struct cmdtab *ct;
Cmd *cp, *ncp;
Cmd cmd;
cmd.next = cmd.u.cmd = 0;
cmd.re = 0;
cmd.flag = cmd.num = 0;
cmd.addr = compoundaddr();
if(cmdskipbl() == -1)
return 0;
if((c=getch())==-1)
return 0;
cmd.cmdc = c;
if(cmd.cmdc=='c' && nextc()=='d'){ /* sleazy two-character case */
getch(); /* the 'd' */
cmd.cmdc='c'|0x100;
}
i = cmdlookup(cmd.cmdc);
if(i >= 0){
if(cmd.cmdc == '\n')
goto Return; /* let nl_cmd work it all out */
ct = &cmdtab[i];
if(ct->defaddr==aNo && cmd.addr)
editerror("command takes no address");
if(ct->count)
cmd.num = getnum(ct->count);
if(ct->regexp){
/* x without pattern -> .*\n, indicated by cmd.re==0 */
/* X without pattern is all files */
if((ct->cmdc!='x' && ct->cmdc!='X') ||
((c = nextc())!=' ' && c!='\t' && c!='\n')){
cmdskipbl();
if((c = getch())=='\n' || c<0)
editerror("no address");
okdelim(c);
cmd.re = getregexp(c);
if(ct->cmdc == 's'){
cmd.u.text = newstring(0);
getrhs(cmd.u.text, c, 's');
if(nextc() == c){
getch();
if(nextc() == 'g')
cmd.flag = getch();
}
}
}
}
if(ct->addr && (cmd.u.mtaddr=simpleaddr())==0)
editerror("bad address");
if(ct->defcmd){
if(cmdskipbl() == '\n'){
getch();
cmd.u.cmd = newcmd();
cmd.u.cmd->cmdc = ct->defcmd;
}else if((cmd.u.cmd = parsecmd(nest))==0)
error("defcmd");
}else if(ct->text)
cmd.u.text = collecttext();
else if(ct->token)
cmd.u.text = collecttoken(ct->token);
else
atnl();
}else
switch(cmd.cmdc){
case '{':
cp = 0;
do{
if(cmdskipbl()=='\n')
getch();
ncp = parsecmd(nest+1);
if(cp)
cp->next = ncp;
else
cmd.u.cmd = ncp;
}while(cp = ncp);
break;
case '}':
atnl();
if(nest==0)
editerror("right brace with no left brace");
return 0;
default:
editerror("unknown command %c", cmd.cmdc);
}
Return:
cp = newcmd();
*cp = cmd;
return cp;
}
String*
getregexp(int delim)
{
String *buf, *r;
int i, c;
buf = allocstring(0);
for(i=0; ; i++){
if((c = getch())=='\\'){
if(nextc()==delim)
c = getch();
else if(nextc()=='\\'){
Straddc(buf, c);
c = getch();
}
}else if(c==delim || c=='\n')
break;
if(i >= RBUFSIZE)
editerror("regular expression too long");
Straddc(buf, c);
}
if(c!=delim && c)
ungetch();
if(buf->n > 0){
patset = TRUE;
freestring(lastpat);
lastpat = buf;
}else
freestring(buf);
if(lastpat->n == 0)
editerror("no regular expression defined");
r = newstring(lastpat->n);
runemove(r->r, lastpat->r, lastpat->n); /* newstring put \0 at end */
return r;
}
Addr *
simpleaddr(void)
{
Addr addr;
Addr *ap, *nap;
addr.num = 0;
addr.next = 0;
addr.u.left = 0;
switch(cmdskipbl()){
case '#':
addr.type = getch();
addr.num = getnum(1);
break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
addr.num = getnum(1);
addr.type='l';
break;
case '/': case '?': case '"':
addr.u.re = getregexp(addr.type = getch());
break;
case '.':
case '$':
case '+':
case '-':
case '\'':
addr.type = getch();
break;
default:
return 0;
}
if(addr.next = simpleaddr())
switch(addr.next->type){
case '.':
case '$':
case '\'':
if(addr.type!='"')
case '"':
editerror("bad address syntax");
break;
case 'l':
case '#':
if(addr.type=='"')
break;
/* fall through */
case '/':
case '?':
if(addr.type!='+' && addr.type!='-'){
/* insert the missing '+' */
nap = newaddr();
nap->type='+';
nap->next = addr.next;
addr.next = nap;
}
break;
case '+':
case '-':
break;
default:
error("simpleaddr");
}
ap = newaddr();
*ap = addr;
return ap;
}
Addr *
compoundaddr(void)
{
Addr addr;
Addr *ap, *next;
addr.u.left = simpleaddr();
if((addr.type = cmdskipbl())!=',' && addr.type!=';')
return addr.u.left;
getch();
next = addr.next = compoundaddr();
if(next && (next->type==',' || next->type==';') && next->u.left==0)
editerror("bad address syntax");
ap = newaddr();
*ap = addr;
return ap;
}
|