-
Notifications
You must be signed in to change notification settings - Fork 1
/
te_subs.c
executable file
·551 lines (472 loc) · 16.1 KB
/
te_subs.c
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
/* TECO for Ultrix Copyright 1986 Matt Fichtenbaum */
/* This program and its components belong to GenRad Inc, Concord MA 01742 */
/* They may be copied if this copyright notice is included */
/* this one modified for higher speed in movenchars() */
/* te_subs.c subroutines 6/2/87 */
/* version for multiple buffers 04/13/89 15.57 */
#include "te_defs.h"
/* routines to copy a string of characters */
/* movenchars(from, to, n) */
/* from, to are the addresses of qps */
/* n is the number of characters to move */
/* moveuntil(from, to, c, &n, max) */
/* c is the match character that ends the move */
/* n is the returned number of chars moved */
/* max is the maximum number of chars to move */
movenchars(from, to, n)
struct qp *from, *to; /* address of buffer pointers */
register int n; /* number of characters */
{
register struct buffcell *fp, *tp; /* local qp ".p" pointers */
register int fc, tc; /* local qp ".c" subscripts */
register int nn; /* local character count */
if (n != 0)
{
fp = from->p; /* copy pointers to local registers */
fc = from->c;
tp = to->p;
tc = to->c;
while (n > 0)
{
nn = ( n < (CELLSIZE - tc)) ? n : CELLSIZE - tc; /* find shortest count */
if (nn > (CELLSIZE - fc)) nn = CELLSIZE - fc;
n -= nn; /* adjust count */
for (; nn > 0; nn--) tp->ch[tc++] = fp->ch[fc++]; /* move one char: inner loop is just this */
if (tc > CELLSIZE-1) /* check current cell done */
{
if (!tp->f) /* is there another following? */
{
tp->f = get_bcell(); /* no, add one */
tp->f->b = tp;
}
tp = tp->f;
tc = 0;
}
if (fc > CELLSIZE-1) /* check current cell done */
{
if (!fp->f) /* oops, run out of source */
{
if (n > 1) ERROR(E_UTC); /* error if not done */
}
else {
fp = fp->f; /* chain to next cell */
fc = 0;
}
}
}
from->p = fp; /* restore arguments */
to->p = tp;
from->c = fc;
to->c = tc;
}
}
moveuntil(from, to, c, n, max, trace)
struct qp *from, *to; /* address of buffer pointers */
register char c; /* match char that ends move */
int *n; /* pointer to returned value */
int max; /* limit on chars to move */
int trace; /* echo characters if nonzero */
{
register struct buffcell *fp, *tp; /* local qpr ".p" pointers */
register int fc, tc; /* local qpr ".c" subscripts */
fp = from->p; /* copy pointers to local registers */
fc = from->c;
tp = to->p;
tc = to->c;
for (*n = 0; fp->ch[fc] != c; (*n)++) /* until terminating char... */
{
if (max-- <= 0) ERROR((msp <= &mstack[0]) ? E_UTC : E_UTM);
tp->ch[tc++] = fp->ch[fc++]; /* move one char */
if (trace) type_char(tp->ch[tc-1]); /* type it out if trace mode */
if (tc > CELLSIZE-1) /* check current cell done */
{
if (!tp->f) /* is there another following? */
{
tp->f = get_bcell(); /* no, add one */
tp->f->b = tp;
}
tp = tp->f;
tc = 0;
}
if (fc > CELLSIZE-1) /* check current cell done */
{
if (!fp->f) ERROR(E_UTC); /* oops, run out of source */
else {
fp = fp->f; /* chain to next cell */
fc = 0;
}
}
}
from->p = fp; /* restore arguments */
to->p = tp;
from->c = fc;
to->c = tc;
}
/* routine to get numeric argument */
int get_value(d) /* get a value, default is argument */
int d;
{
int v;
v = (esp->flag1) ? esp->val1 :
(esp->op == OP_SUB) ? -d : d;
esp->flag1 = 0; /* consume argument */
esp->op = OP_START;
return(v);
}
/* routine to convert a line count */
/* returns number of chars between dot and nth line feed */
int lines(arg)
register int arg;
{
register int i, c;
register struct buffcell *p;
for (i = pbuff->dot / CELLSIZE, p = pbuff->f; (i > 0) && (p->f); i--) p = p->f; /* find dot */
c = pbuff->dot % CELLSIZE;
if (arg <= 0) /* scan backwards */
{
for (i = pbuff->dot; (arg < 1) && (i > 0); ) /* repeat for each line */
{
--i; /* count characters */
if (--c < 0) /* back up the pointer */
{
if (!(p = p->b)) break;
c = CELLSIZE - 1;
}
if ( (ez_val & EZ_NOVTFF) ? (p->ch[c] == LF) : (spec_chars[p->ch[c]] & A_L) ) ++arg; /* if line sep found */
}
if (arg > 0) ++i; /* if terminated on a line separator, advance over the separator */
}
else /* scan forwards */
{
for (i = pbuff->dot; (arg > 0) && (i < pbuff->z); i++)
{
if ( (ez_val & EZ_NOVTFF) ? (p->ch[c] == LF) : (spec_chars[p->ch[c]] & A_L) ) --arg;
if (++c > CELLSIZE-1)
{
if (!(p = p->f)) break;
c = 0;
}
} /* this will incr over the separator anyway */
}
return(i - pbuff->dot);
}
/* routine to handle args for K, T, X, etc. */
/* if two args, 'char x' to 'char y' */
/* if just one arg, then n lines (default 1) */
/* sets a pointer to the beginning of the specd */
/* string, and a char count value */
int line_args(d, p)
int d; /* nonzero: leave dot at start */
struct qp *p;
{
int n;
if (esp->flag1 && esp->flag2) /* if two args */
{
if (esp->val1 <= esp->val2) /* in right order */
{
if (esp->val1 < 0) esp->val1 = 0;
if (esp->val2 > pbuff->z) esp->val2 = pbuff->z;
if (d) pbuff->dot = esp->val1; /* update dot */
set_pointer(esp->val1, p); /* set the pointer */
esp->flag2 = esp->flag1 = 0; /* consume arguments */
esp->op = OP_START;
return(esp->val2 - esp->val1); /* and return the count */
}
else
{
if (esp->val2 < 0) esp->val2 = 0;
if (esp->val1 > pbuff->z) esp->val1 = pbuff->z;
if (d) pbuff->dot = esp->val2; /* update dot */
set_pointer(esp->val2, p); /* args in reverse order */
esp->flag2 = esp->flag1 = 0; /* consume arguments */
esp->op = OP_START;
return(esp->val1 - esp->val2);
}
}
else
{
n = lines(get_value(1));
if (n < -pbuff->dot) n = -pbuff->dot;
else if (n > pbuff->z - pbuff->dot) n = pbuff->z - pbuff->dot;
if (n >= 0) set_pointer(pbuff->dot, p);
else
{
n = -n;
set_pointer(pbuff->dot - n, p);
if (d) pbuff->dot -= n;
}
return(n);
}
}
/* convert character c to a q-register spec */
int getqspec(fors, c) /* fors ("file or search") nonzero = allow _ or * */
int fors;
char c;
{
if (isdigit(c)) return(c - '0' + 1);
else if (isalpha(c)) return(mapch_l[c] - 'a' + 11);
else if (fors)
{
if (c == '_') return (SERBUF);
if (c == '*') return (FILBUF);
if (c == '%') return (SYSBUF);
if (c == '#') return (TIMBUF);
}
ERROR(E_IQN);
}
/* routines to do insert operations */
/* insert1() copies current cell up to dot into a new cell */
/* leaves bb pointing to end of that text */
/* insert2() copies rest of buffer */
struct buffcell *insert_p;
insert1()
{
int nchars; /* number of chars in cell */
set_pointer(pbuff->dot, &aa); /* convert dot to a qp */
if (pbuff->dot < pbuff->buff_mod) pbuff->buff_mod = pbuff->dot; /* update earliest char loc touched */
insert_p = bb.p = get_bcell(); /* get a new cell */
bb.c = 0;
nchars = aa.c; /* save char position of dot in cell */
aa.c = 0;
/* now aa points to the beginning of the buffer cell that */
/* contains dot, bb points to the beginning of a new cell,*/
/* nchars is the number of chars before dot */
movenchars(&aa, &bb, nchars); /* copy cell up to dot */
}
insert2(count) /* count is the number of chars added */
int count;
{
aa.p->b->f = insert_p; /* put the new cell where the old one was */
insert_p->b = aa.p->b;
insert_p = NULL;
bb.p->f = aa.p; /* splice rest of buffer to end */
aa.p->b = bb.p;
movenchars(&aa, &bb, pbuff->z - pbuff->dot); /* squeeze buffer */
free_blist(bb.p->f); /* return unused cells */
bb.p->f = NULL; /* and end the buffer */
pbuff->z += count; /* add # of chars inserted */
pbuff->dot += count;
ctrl_s = -count; /* save string length */
}
/* subroutine to delete n characters starting at dot */
/* argument is number of characters */
delete1(nchars)
int nchars;
{
if (!nchars) return; /* 0 chars is a nop */
if (nchars < 0) /* delete negative number of characters? */
{
nchars = -nchars; /* make ll positive */
if (nchars > pbuff->dot) ERROR(E_POP); /* don't delete beyond beg of buffer */
pbuff->dot -= nchars; /* put pointer before deleted text */
}
else if (pbuff->dot + nchars > pbuff->z) ERROR(E_POP); /* don't delete beyond end of buffer */
set_pointer(pbuff->dot, &aa); /* pointer to beginning of area to delete */
set_pointer(pbuff->dot+nchars, &bb); /* and to end */
if (pbuff->dot < pbuff->buff_mod) pbuff->buff_mod = pbuff->dot; /* update earliest char loc touched */
movenchars(&bb, &aa, pbuff->z - (pbuff->dot+ nchars)); /* move text unless delete ends at z */
free_blist(aa.p->f); /* return any cells after end */
aa.p->f = NULL; /* end the buffer */
pbuff->z -= nchars; /* adjust z */
}
/* routine to process "O" command */
struct qh obuff; /* tag string buffer */
do_o()
{
int i, j; /* i used as start of tag, j as end */
int p, level; /* p is pointer to tag string, level is iteration lvl */
int epfound; /* flag for "second ! found" */
if (!build_string(&obuff)) return; /* no tag spec'd: continue */
if (obuff.z > CELLSIZE) ERROR(E_STL); /* string too long */
esp->op = OP_START; /* consume any argument */
if (esp->flag1) /* is there one? */
{
esp->flag1 = 0; /* consume it */
if (esp->val1 < 0) return; /* computed goto out of range - */
for (i = 0; (i < obuff.z) && (esp->val1 > 0); i++) /* scan to find right tag */
if (obuff.f->ch[i] == ',') esp->val1--; /* count commas */
if (esp->val1 > 0) return; /* computed goto out of range + */
/* now i is either at 0 or after the nth comma */
for (j = i; j < obuff.z; j++) /* find end of tag */
if (obuff.f->ch[j] == ',') break; /* stop at next comma */
if (j == i) return; /* two adjacent commas: zero length tag */
}
else
{
i = 0; /* not a computed goto: use whole tag buffer */
j = obuff.z;
}
/* start from beginning of iteration or macro, and look for tag */
if (cptr.flag & F_ITER) /* if in iteration */
{
cptr.p = cptr.il->p; /* restore */
cptr.c = cptr.il->c;
cptr.dot = cptr.il->dot;
}
else for (cptr.dot = cptr.c = 0; cptr.p->b->b != NULL; cptr.p = cptr.p->b); /* find macro start */
/* search for tag */
for (level = 0; ;) /* look through rest of command string */
{
switch (skipto(1)) /* look for interesting things, including ! */
{
case '<': /* start of iteration */
++level;
break;
case '>': /* end of iteration */
if ((level == 0) && (cptr.flag & F_ITER)) pop_iteration(1);
else --level;
break;
case '!': /* start of tag */
for (epfound = 0; ; epfound = 0) /* keep looking for tag */
{
for (p = i; p < j; p++)
{
if (getcmdc(0) == '!') epfound = 1; /* mark "trailing ! found */
if (mapch_l[cmdc] != mapch_l[obuff.f->ch[p]]) break; /* compare */
}
if (p >= j) /* if all comparison chars matched */
{
if (getcmdc(0) == '!') return; /* and tag ends with !, found it */
}
else if (!epfound) while (getcmdc(0) != '!'); /* else look for next ! and continue */
}
break;
} /* end of switch */
} /* end of scan loop */
} /* end of subroutine */
/* routine to skip to next ", ', |, <, or > */
/* skips over these chars embedded in text strings */
/* stops in ! if argument is nonzero */
/* returns character found, and leaves it in skipc */
char skipto(arg)
int arg;
{
int atsw; /* "at" prefix */
char ta, term; /* temp attributes, terminator */
for (atsw = 0; ;) /* forever */
{
while (!(ta = spec_chars[skipc = getcmdc(0)] & (A_X | A_S | A_T | A_Q))); /* read until something interesting found */
again:
if (ta & A_Q) getcmdc(0); /* if command takes a Q spec, skip the spec */
if (ta & A_X) /* sought char found: quit */
{
if (skipc == '"') getcmdc(0); /* quote must skip next char */
return(skipc);
}
if (ta & A_S) /* other special char */
{
switch (skipc)
{
case '^': /* treat next char as CTL */
if (ta = spec_chars[skipc = getcmdc(0) & 0x1f]) goto again;
break;
case '@': /* use alternative text terminator */
atsw = 1;
break;
case CTL('^'): /* ^^ is value of next char: skip that char */
getcmdc(0);
break;
case CTL('A'): /* type text */
term = (atsw) ? getcmdc(0) : CTL('A');
atsw = 0;
while (getcmdc(0) != term); /* skip text */
break;
case '!': /* tag */
if (arg) return(skipc);
while (getcmdc(0) != '!'); /* skip until next ! */
break;
case 'e': /* first char of two-letter E or F command */
case 'f':
if (spec_chars[getcmdc(0)] & ((skipc == 'e') ? A_E : A_F)) /* if one with a text arg */
{
term = (atsw) ? getcmdc(0) : ESC;
atsw = 0;
while (getcmdc(0) != term); /* read past terminator */
}
break;
} /* end "switch" */
} /* end "if (ta & A_S)" */
else if (ta & A_T) /* command with a text argument */
{
term = (atsw) ? getcmdc(0) : ESC;
atsw = 0;
while (getcmdc(0) != term); /* skip text */
}
} /* end "forever" */
} /* end "skipto()" */
/* find number of characters to next matching (, [, or { (like '%' in vi) */
do_ctlp()
{
int i, l;
char c, c1;
set_pointer(pbuff->dot, &aa); /* point to text buffer */
switch(c1 = aa.p->ch[aa.c])
{
case '(':
c = ')'; /* match char is ) */
i = 1; /* direction is positive */
break;
case ')':
c = '('; /* match char is ( */
i = -1; /* direction is negative */
break;
case '[':
c = ']';
i = 1;
break;
case ']':
c = '[';
i = -1;
break;
case '{':
c = '}';
i = 1;
break;
case '}':
c = '{';
i = -1;
break;
case '<':
c = '>';
i = 1;
break;
case '>':
c = '<';
i = -1;
break;
case '"':
c = '\'';
i = 1;
break;
case '|':
c1 = '\''; /* treat | as ' */
case '\'':
c = '"';
i = -1;
break;
default:
esp->val1 = i = 0; /* not on a matchable char, return 0 */
}
l = 1; /* start with one unmatched char */
if (i > 0) /* if searching forward */
{
for (i = pbuff->dot, fwdc(&aa); (i < pbuff->z) && (l); fwdc(&aa) )
{
++i;
if (aa.p->ch[aa.c] == c) --l;
else if (aa.p->ch[aa.c] == c1) ++l;
}
esp->val1 = (i < pbuff->z) ? i - pbuff->dot : 0;
}
else if (i < 0)
{
for (i = pbuff->dot, backc(&aa); (i >= 0) && (l); backc(&aa) )
{
--i;
if (aa.p->ch[aa.c] == c) --l;
else if (aa.p->ch[aa.c] == c1) ++l;
}
esp->val1 = (i >= 0) ? i - pbuff->dot : 0;
}
esp->flag1 = 1;
}