-
Notifications
You must be signed in to change notification settings - Fork 41
/
ed.chared.c
3904 lines (3403 loc) · 74.8 KB
/
ed.chared.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
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ed.chared.c: Character editing functions.
*/
/*-
* Copyright (c) 1980, 1991 The Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
Bjorn Knutsson @ Thu Jun 24 19:02:17 1999
e_dabbrev_expand() did not do proper completion if quoted spaces were present
in the string being completed. Exemple:
# echo hello\ world
hello world
# echo h<press key bound to dabbrev-expande>
# echo hello\<cursor>
Correct behavior is:
# echo h<press key bound to dabbrev-expande>
# echo hello\ world<cursor>
The same problem occurred if spaces were present in a string withing
quotation marks. Example:
# echo "hello world"
hello world
# echo "h<press key bound to dabbrev-expande>
# echo "hello<cursor>
The former problem could be solved with minor modifications of c_preword()
and c_endword(). The latter, however, required a significant rewrite of
c_preword(), since quoted strings must be parsed from start to end to
determine if a given character is inside or outside the quotation marks.
Compare the following two strings:
# echo \"" 'foo \' bar\"
" 'foo \' bar\
# echo '\"" 'foo \' bar\"
\"" foo ' bar"
The only difference between the two echo lines is in the first character
after the echo command. The result is either one or three arguments.
*/
#include "sh.h"
#include "ed.h"
#include "tw.h"
#include "ed.defns.h"
/* #define SDEBUG */
#define TCSHOP_NOP 0x00
#define TCSHOP_DELETE 0x01
#define TCSHOP_INSERT 0x02
#define TCSHOP_CHANGE 0x04
#define CHAR_FWD 0
#define CHAR_BACK 1
/*
* vi word treatment
* from: Gert-Jan Vons <vons@cesar.crbca1.sinet.slb.com>
*/
#define C_CLASS_WHITE 1
#define C_CLASS_WORD 2
#define C_CLASS_OTHER 3
static Char *InsertPos = InputBuf; /* Where insertion starts */
static Char *ActionPos = 0; /* Where action begins */
static int ActionFlag = TCSHOP_NOP; /* What delayed action to take */
/*
* Word search state
*/
static int searchdir = F_UP_SEARCH_HIST; /* Direction of last search */
static struct Strbuf patbuf; /* = Strbuf_INIT; Search target */
/*
* Char search state
*/
static int srch_dir = CHAR_FWD; /* Direction of last search */
static Char srch_char = 0; /* Search target */
/* all routines that start with c_ are private to this set of routines */
static void c_alternativ_key_map (int);
void c_insert (int);
void c_delafter (int);
void c_delbefore (int);
static int c_to_class (Char);
static Char *c_prev_word (Char *, Char *, int);
static Char *c_next_word (Char *, Char *, int);
static Char *c_number (Char *, int *, int);
static Char *c_expand (Char *);
static int c_excl (Char *);
static int c_substitute (void);
static void c_delfini (void);
static int c_hmatch (Char *);
static void c_hsetpat (void);
#ifdef COMMENT
static void c_get_word (Char **, Char **);
#endif
static Char *c_preword (Char *, Char *, int, Char *);
static Char *c_nexword (Char *, Char *, int);
static Char *c_endword (Char *, Char *, Char *, int, Char *);
static Char *c_eword (Char *, Char *, int);
static void c_push_kill (Char *, Char *);
static void c_save_inputbuf (void);
static CCRETVAL c_search_line (Char *, int);
static CCRETVAL v_repeat_srch (int);
static CCRETVAL e_inc_search (int);
#ifdef notyet
static CCRETVAL e_insert_str (Char *);
#endif
static CCRETVAL v_search (int);
static CCRETVAL v_csearch_fwd (Char, int, int);
static CCRETVAL v_action (int);
static CCRETVAL v_csearch_back (Char, int, int);
static void
c_alternativ_key_map(int state)
{
switch (state) {
case 0:
CurrentKeyMap = CcKeyMap;
break;
case 1:
CurrentKeyMap = CcAltMap;
break;
default:
return;
}
AltKeyMap = (Char) state;
}
void
c_insert(int num)
{
Char *cp;
if (LastChar + num >= InputLim)
return; /* can't go past end of buffer */
if (Cursor < LastChar) { /* if I must move chars */
for (cp = LastChar; cp >= Cursor; cp--)
cp[num] = *cp;
if (Mark && Mark > Cursor)
Mark += num;
}
LastChar += num;
}
void
c_delafter(int num)
{
Char *cp, *kp = NULL;
if (num > LastChar - Cursor)
num = (int) (LastChar - Cursor); /* bounds check */
if (num > 0) { /* if I can delete anything */
if (VImode) {
kp = UndoBuf; /* Set Up for VI undo command */
UndoAction = TCSHOP_INSERT;
UndoSize = num;
UndoPtr = Cursor;
for (cp = Cursor; cp <= LastChar; cp++) {
*kp++ = *cp; /* Save deleted chars into undobuf */
*cp = cp[num];
}
}
else
for (cp = Cursor; cp + num <= LastChar; cp++)
*cp = cp[num];
LastChar -= num;
/* Mark was within the range of the deleted word? */
if (Mark && Mark > Cursor && Mark <= Cursor+num)
Mark = Cursor;
/* Mark after the deleted word? */
else if (Mark && Mark > Cursor)
Mark -= num;
}
#ifdef notdef
else {
/*
* XXX: We don't want to do that. In emacs mode overwrite should be
* sticky. I am not sure how that affects vi mode
*/
inputmode = MODE_INSERT;
}
#endif /* notdef */
}
void
c_delbefore(int num) /* delete before dot, with bounds checking */
{
Char *cp, *kp = NULL;
if (num > Cursor - InputBuf)
num = (int) (Cursor - InputBuf); /* bounds check */
if (num > 0) { /* if I can delete anything */
if (VImode) {
kp = UndoBuf; /* Set Up for VI undo command */
UndoAction = TCSHOP_INSERT;
UndoSize = num;
UndoPtr = Cursor - num;
for (cp = Cursor - num; cp <= LastChar; cp++) {
*kp++ = *cp;
*cp = cp[num];
}
}
else
for (cp = Cursor - num; cp + num <= LastChar; cp++)
*cp = cp[num];
LastChar -= num;
Cursor -= num;
/* Mark was within the range of the deleted word? */
if (Mark && Mark > Cursor && Mark <= Cursor+num)
Mark = Cursor;
/* Mark after the deleted word? */
else if (Mark && Mark > Cursor)
Mark -= num;
}
}
static Char *
c_preword(Char *p, Char *low, int n, Char *delim)
{
while (n--) {
Char *prev = low;
Char *new;
while (prev < p) { /* Skip initial non-word chars */
if (!Strchr(delim, *prev) || (prev > low && prev[-1] == (Char)'\\'))
break;
prev++;
}
new = prev;
while (new < p) {
prev = new;
new = c_endword(prev-1, low, p, 1, delim); /* Skip to next non-word char */
new++; /* Step away from end of word */
while (new <= p) { /* Skip trailing non-word chars */
if (!Strchr(delim, *new) || (new > prev && new[-1] == (Char)'\\'))
break;
new++;
}
}
p = prev; /* Set to previous word start */
}
if (p < low)
p = low;
return (p);
}
/*
* c_to_class() returns the class of the given character.
*
* This is used to make the c_prev_word(), c_next_word() and c_eword() functions
* work like vi's, which classify characters. A word is a sequence of
* characters belonging to the same class, classes being defined as
* follows:
*
* 1/ whitespace
* 2/ alphanumeric chars, + underscore
* 3/ others
*/
static int
c_to_class(Char ch)
{
if (Isspace(ch))
return C_CLASS_WHITE;
if (isword(ch))
return C_CLASS_WORD;
return C_CLASS_OTHER;
}
static Char *
c_prev_word(Char *p, Char *low, int n)
{
p--;
if (!VImode) {
while (n--) {
while ((p >= low) && !isword(*p))
p--;
while ((p >= low) && isword(*p))
p--;
}
/* cp now points to one character before the word */
p++;
if (p < low)
p = low;
/* cp now points where we want it */
return(p);
}
while (n--) {
int c_class;
if (p < low)
break;
/* scan until beginning of current word (may be all whitespace!) */
c_class = c_to_class(*p);
while ((p >= low) && c_class == c_to_class(*p))
p--;
/* if this was a non_whitespace word, we're ready */
if (c_class != C_CLASS_WHITE)
continue;
/* otherwise, move back to beginning of the word just found */
c_class = c_to_class(*p);
while ((p >= low) && c_class == c_to_class(*p))
p--;
}
p++; /* correct overshoot */
return (p);
}
static Char *
c_next_word(Char *p, Char *high, int n)
{
if (!VImode) {
while (n--) {
while ((p < high) && !isword(*p))
p++;
while ((p < high) && isword(*p))
p++;
}
if (p > high)
p = high;
/* p now points where we want it */
return(p);
}
while (n--) {
int c_class;
if (p >= high)
break;
/* scan until end of current word (may be all whitespace!) */
c_class = c_to_class(*p);
while ((p < high) && c_class == c_to_class(*p))
p++;
/* if this was all whitespace, we're ready */
if (c_class == C_CLASS_WHITE)
continue;
/* if we've found white-space at the end of the word, skip it */
while ((p < high) && c_to_class(*p) == C_CLASS_WHITE)
p++;
}
p--; /* correct overshoot */
return (p);
}
static Char *
c_nexword(Char *p, Char *high, int n)
{
while (n--) {
while ((p < high) && !Isspace(*p))
p++;
while ((p < high) && Isspace(*p))
p++;
}
if (p > high)
p = high;
/* p now points where we want it */
return(p);
}
/*
* Expand-History (originally "Magic-Space") code added by
* Ray Moody <ray@gibbs.physics.purdue.edu>
* this is a neat, but odd, addition.
*/
/*
* c_number: Ignore character p points to, return number appearing after that.
* A '$' by itself means a big number; "$-" is for negative; '^' means 1.
* Return p pointing to last char used.
*/
/*
* dval is the number to subtract from for things like $-3
*/
static Char *
c_number(Char *p, int *num, int dval)
{
int i;
int sign = 1;
if (*++p == '^') {
*num = 1;
return(p);
}
if (*p == '$') {
if (*++p != '-') {
*num = INT_MAX; /* Handle $ */
return(--p);
}
sign = -1; /* Handle $- */
++p;
}
for (i = 0; *p >= '0' && *p <= '9'; i = 10 * i + *p++ - '0')
continue;
*num = (sign < 0 ? dval - i : i);
return(--p);
}
/*
* excl_expand: There is an excl to be expanded to p -- do the right thing
* with it and return a version of p advanced over the expanded stuff. Also,
* update tsh_cur and related things as appropriate...
*/
static Char *
c_expand(Char *p)
{
Char *q;
struct Hist *h = Histlist.Hnext;
struct wordent *l;
int i, from, to, dval;
int all_dig;
int been_once = 0;
Char *op = p;
Char *buf;
size_t buf_len;
Char *modbuf;
buf = NULL;
if (!h)
goto excl_err;
excl_sw:
switch (*(q = p + 1)) {
case '^':
buf = expand_lex(&h->Hlex, 1, 1);
break;
case '$':
if ((l = (h->Hlex).prev) != 0)
buf = expand_lex(l->prev->prev, 0, 0);
break;
case '*':
buf = expand_lex(&h->Hlex, 1, INT_MAX);
break;
case '?':
h = findev(p + 2, 1);
if (h == NULL)
goto excl_err;
buf = expand_lex(&h->Hlex, 0, INT_MAX);
while (*q)
q++;
q--;
break;
default:
if (been_once) { /* unknown argument */
/* assume it's a modifier, e.g. !foo:h, and get whole cmd */
buf = expand_lex(&h->Hlex, 0, INT_MAX);
q -= 2;
break;
}
been_once = 1;
if (*q == ':') /* short form: !:arg */
--q;
if (HIST != '\0' && *q != HIST) {
/*
* Search for a space, tab, or colon. See if we have a number (as
* in !1234:xyz). Remember the number.
*/
for (i = 0, all_dig = 1;
*q != ' ' && *q != '\t' && *q != ':' && q < Cursor; q++) {
/*
* PWP: !-4 is a valid history argument too, therefore the test
* is if not a digit, or not a - as the first character.
*/
if ((*q < '0' || *q > '9') && (*q != '-' || q != p + 1))
all_dig = 0;
else if (*q == '-')
all_dig = 2;/* we are sneeky about this */
else
i = 10 * i + *q - '0';
}
--q;
/*
* If we have a number, search for event i. Otherwise, search for
* a named event (as in !foo). (In this case, I is the length of
* the named event).
*/
if (all_dig) {
if (all_dig == 2)
i = -i; /* make it negitive */
if (i < 0) /* if !-4 (for example) */
i = eventno + 1 + i; /* remember: i is < 0 */
for (; h; h = h->Hnext) {
if (h->Hnum == i)
break;
}
}
else {
for (i = (int) (q - p); h; h = h->Hnext) {
if ((l = &h->Hlex) != 0) {
if (!Strncmp(p + 1, l->next->word, (size_t) i))
break;
}
}
}
}
if (!h)
goto excl_err;
if (q[1] == ':' || q[1] == '-' || q[1] == '*' ||
q[1] == '$' || q[1] == '^') { /* get some args */
p = q[1] == ':' ? ++q : q;
/*
* Go handle !foo:*
*/
if ((q[1] < '0' || q[1] > '9') &&
q[1] != '-' && q[1] != '$' && q[1] != '^')
goto excl_sw;
/*
* Go handle !foo:$
*/
if (q[1] == '$' && (q[2] != '-' || q[3] < '0' || q[3] > '9'))
goto excl_sw;
/*
* Count up the number of words in this event. Store it in dval.
* Dval will be fed to number.
*/
dval = 0;
if ((l = h->Hlex.prev) != 0) {
for (l = l->prev; l != h->Hlex.next; l = l->prev, dval++)
continue;
}
if (!dval)
goto excl_err;
if (q[1] == '-')
from = 0;
else
q = c_number(q, &from, dval);
if (q[1] == '-') {
++q;
if ((q[1] < '0' || q[1] > '9') && q[1] != '$')
to = dval - 1;
else
q = c_number(q, &to, dval);
}
else if (q[1] == '*') {
++q;
to = INT_MAX;
}
else {
to = from;
}
if (from < 0 || to < from)
goto excl_err;
buf = expand_lex(&h->Hlex, from, to);
}
else /* get whole cmd */
buf = expand_lex(&h->Hlex, 0, INT_MAX);
break;
}
if (buf == NULL)
buf = SAVE("");
/*
* Apply modifiers, if any.
*/
if (q[1] == ':') {
modbuf = buf;
while (q[1] == ':' && modbuf != NULL) {
switch (q[2]) {
case 'r':
case 'e':
case 'h':
case 't':
case 'q':
case 'x':
case 'u':
case 'l':
if ((modbuf = domod(buf, (int) q[2])) != NULL) {
xfree(buf);
buf = modbuf;
}
++q;
break;
case 'a':
case 'g':
/* Not implemented; this needs to be done before expanding
* lex. We don't have the words available to us anymore.
*/
++q;
break;
case 'p':
/* Ok */
++q;
break;
case '\0':
break;
default:
++q;
break;
}
if (q[1])
++q;
}
}
buf_len = Strlen(buf);
/*
* Now replace the text from op to q inclusive with the text from buf.
*/
q++;
/*
* Now replace text non-inclusively like a real CS major!
*/
if (LastChar + buf_len - (q - op) >= InputLim)
goto excl_err;
(void) memmove(op + buf_len, q, (LastChar - q) * sizeof(Char));
LastChar += buf_len - (q - op);
Cursor += buf_len - (q - op);
(void) memcpy(op, buf, buf_len * sizeof(Char));
*LastChar = '\0';
xfree(buf);
return op + buf_len;
excl_err:
xfree(buf);
SoundBeep();
return(op + 1);
}
/*
* c_excl: An excl has been found at point p -- back up and find some white
* space (or the beginning of the buffer) and properly expand all the excl's
* from there up to the current cursor position. We also avoid (trying to)
* expanding '>!'
* Returns number of expansions attempted (doesn't matter whether they succeeded
* or not).
*/
static int
c_excl(Char *p)
{
int i;
Char *q;
int nr_exp;
/*
* if />[SPC TAB]*![SPC TAB]/, back up p to just after the >. otherwise,
* back p up to just before the current word.
*/
if ((p[1] == ' ' || p[1] == '\t') &&
(p[-1] == ' ' || p[-1] == '\t' || p[-1] == '>')) {
for (q = p - 1; q > InputBuf && (*q == ' ' || *q == '\t'); --q)
continue;
if (*q == '>')
++p;
}
else {
while (*p != ' ' && *p != '\t' && p > InputBuf)
--p;
}
/*
* Forever: Look for history char. (Stop looking when we find the cursor.)
* Count backslashes. If odd, skip history char. Expand if even number of
* backslashes.
*/
nr_exp = 0;
for (;;) {
if (HIST != '\0')
while (*p != HIST && p < Cursor)
++p;
for (i = 1; (p - i) >= InputBuf && p[-i] == '\\'; i++)
continue;
if (i % 2 == 0)
++p;
if (p >= Cursor) /* all done */
return nr_exp;
if (i % 2 == 1) {
p = c_expand(p);
++nr_exp;
}
}
}
static int
c_substitute(void)
{
Char *p;
int nr_exp;
/*
* Start p out one character before the cursor. Move it backwards looking
* for white space, the beginning of the line, or a history character.
*/
for (p = Cursor - 1;
p > InputBuf && *p != ' ' && *p != '\t' && *p && *p != HIST; --p)
continue;
/*
* If we found a history character, go expand it.
*/
if (p >= InputBuf && HIST != '\0' && *p == HIST)
nr_exp = c_excl(p);
else
nr_exp = 0;
Refresh();
return nr_exp;
}
static void
c_delfini(void) /* Finish up delete action */
{
int Size;
if (ActionFlag & TCSHOP_INSERT)
c_alternativ_key_map(0);
ActionFlag = TCSHOP_NOP;
if (ActionPos == 0)
return;
UndoAction = TCSHOP_INSERT;
if (Cursor > ActionPos) {
Size = (int) (Cursor-ActionPos);
c_delbefore(Size);
RefCursor();
}
else if (Cursor < ActionPos) {
Size = (int)(ActionPos-Cursor);
c_delafter(Size);
}
else {
Size = 1;
c_delafter(Size);
}
UndoPtr = Cursor;
UndoSize = Size;
}
static Char *
c_endword(Char *p, Char *low, Char *high, int n, Char *delim)
{
Char inquote = 0;
p++;
while (n--) {
while (p < high) { /* Skip non-word chars */
if (!Strchr(delim, *p) || p[-1] == (Char)'\\')
break;
p++;
}
while (p < high) { /* Skip string */
if ((*p == (Char)'\'' || *p == (Char)'"')) { /* Quotation marks? */
/* Should it be honored? */
if (inquote || (p > low && p[-1] != (Char)'\\')) {
if (inquote == 0) inquote = *p;
else if (inquote == *p) inquote = 0;
}
}
/* Break if unquoted non-word char */
if (!inquote && Strchr(delim, *p) && p > low && p[-1] != (Char)'\\')
break;
p++;
}
}
p--;
return(p);
}
static Char *
c_eword(Char *p, Char *high, int n)
{
p++;
while (n--) {
int c_class;
if (p >= high)
break;
/* scan until end of current word (may be all whitespace!) */
c_class = c_to_class(*p);
while ((p < high) && c_class == c_to_class(*p))
p++;
/* if this was a non_whitespace word, we're ready */
if (c_class != C_CLASS_WHITE)
continue;
/* otherwise, move to the end of the word just found */
c_class = c_to_class(*p);
while ((p < high) && c_class == c_to_class(*p))
p++;
}
p--;
return(p);
}
/* Set the max length of the kill ring */
void
SetKillRing(int max)
{
CStr *new;
int count, i, j;
if (max < 1)
max = 1; /* no ring, but always one buffer */
if (max == KillRingMax)
return;
new = xcalloc(max, sizeof(CStr));
if (KillRing != NULL) {
if (KillRingLen != 0) {
if (max >= KillRingLen) {
count = KillRingLen;
j = KillPos;
} else {
count = max;
j = (KillPos - count + KillRingLen) % KillRingLen;
}
for (i = 0; i < KillRingLen; i++) {
if (i < count) /* copy latest */
new[i] = KillRing[j];
else /* free the others */
xfree(KillRing[j].buf);
j = (j + 1) % KillRingLen;
}
KillRingLen = count;
KillPos = count % max;
YankPos = count - 1;
}
xfree(KillRing);
}
KillRing = new;
KillRingMax = max;
}
/* Push string from start upto (but not including) end onto kill ring */
static void
c_push_kill(Char *start, Char *end)
{
CStr save, *pos;
Char *dp, *cp, *kp;
int len = end - start, i, j, k;
/* Check for duplicates? */
if (KillRingLen > 0 && (dp = varval(STRkilldup)) != STRNULL) {
YankPos = (KillPos - 1 + KillRingLen) % KillRingLen;
if (eq(dp, STRerase)) { /* erase earlier one (actually move up) */
j = YankPos;
for (i = 0; i < KillRingLen; i++) {
if (Strncmp(KillRing[j].buf, start, (size_t) len) == 0 &&
KillRing[j].buf[len] == '\0') {
save = KillRing[j];
for ( ; i > 0; i--) {
k = j;
j = (j + 1) % KillRingLen;
KillRing[k] = KillRing[j];
}
KillRing[j] = save;
return;
}
j = (j - 1 + KillRingLen) % KillRingLen;
}
} else if (eq(dp, STRall)) { /* skip if any earlier */
for (i = 0; i < KillRingLen; i++)
if (Strncmp(KillRing[i].buf, start, (size_t) len) == 0 &&
KillRing[i].buf[len] == '\0')
return;
} else if (eq(dp, STRprev)) { /* skip if immediately previous */
j = YankPos;
if (Strncmp(KillRing[j].buf, start, (size_t) len) == 0 &&
KillRing[j].buf[len] == '\0')
return;
}
}
/* No duplicate, go ahead and push */
len++; /* need space for '\0' */
YankPos = KillPos;
if (KillRingLen < KillRingMax)
KillRingLen++;
pos = &KillRing[KillPos];
KillPos = (KillPos + 1) % KillRingMax;
if (pos->len < len) {
pos->buf = xrealloc(pos->buf, len * sizeof(Char));
pos->len = len;
}
cp = start;
kp = pos->buf;
while (cp < end)
*kp++ = *cp++;
*kp = '\0';
}
/* Save InputBuf etc in SavedBuf etc for restore after cmd exec */
static void
c_save_inputbuf(void)
{
SavedBuf.len = 0;
Strbuf_append(&SavedBuf, InputBuf);
Strbuf_terminate(&SavedBuf);
LastSaved = LastChar - InputBuf;
CursSaved = Cursor - InputBuf;
HistSaved = Hist_num;
RestoreSaved = 1;
}
CCRETVAL
GetHistLine(void)
{
struct Hist *hp;
int h;
if (Hist_num == 0) { /* if really the current line */
if (HistBuf.s != NULL)
copyn(InputBuf, HistBuf.s, INBUFSIZE);/*FIXBUF*/
else
*InputBuf = '\0';
LastChar = InputBuf + HistBuf.len;
#ifdef KSHVI
if (VImode)
Cursor = InputBuf;
else
#endif /* KSHVI */
Cursor = LastChar;
return(CC_REFRESH);
}
hp = Histlist.Hnext;
if (hp == NULL)
return(CC_ERROR);
for (h = 1; h < Hist_num; h++) {
if ((hp->Hnext) == NULL) {
Hist_num = h;