mirrored from git://git.code.sf.net/p/zsh/code
-
Notifications
You must be signed in to change notification settings - Fork 441
/
hist.c
3907 lines (3543 loc) · 87.3 KB
/
hist.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
/*
* hist.c - history expansion
*
* This file is part of zsh, the Z shell.
*
* Copyright (c) 1992-1997 Paul Falstad
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and to distribute modified versions of this software for any
* purpose, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* In no event shall Paul Falstad or the Zsh Development Group be liable
* to any party for direct, indirect, special, incidental, or consequential
* damages arising out of the use of this software and its documentation,
* even if Paul Falstad and the Zsh Development Group have been advised of
* the possibility of such damage.
*
* Paul Falstad and the Zsh Development Group specifically disclaim any
* warranties, including, but not limited to, the implied warranties of
* merchantability and fitness for a particular purpose. The software
* provided hereunder is on an "as is" basis, and Paul Falstad and the
* Zsh Development Group have no obligation to provide maintenance,
* support, updates, enhancements, or modifications.
*
*/
#include "zsh.mdh"
#include "hist.pro"
/* Functions to call for getting/ungetting a character and for history
* word control. */
/**/
mod_export int (*hgetc) _((void));
/**/
void (*hungetc) _((int));
/**/
void (*hwaddc) _((int));
/**/
void (*hwbegin) _((int));
/**/
void (*hwabort) _((void));
/**/
void (*hwend) _((void));
/**/
void (*addtoline) _((int));
/* != 0 means history substitution is turned off */
/**/
mod_export int stophist;
/* if != 0, we are expanding the current line */
/**/
mod_export int expanding;
/* these are used to modify the cursor position during expansion */
/**/
mod_export int excs, exlast;
/*
* Current history event number
*
* Note on curhist: with history inactive, this points to the
* last line actually added to the history list. With history active,
* the line does not get added to the list until hend(), if at all.
* However, curhist is incremented to reflect the current line anyway
* and a temporary history entry is inserted while the user is editing.
* If the resulting line was not added to the list, a flag is set so
* that curhist will be decremented in hbegin().
*
* Note curhist is passed to zle on variable length argument list:
* type must match that retrieved in zle_main_entry.
*/
/**/
mod_export zlong curhist;
/**/
struct histent curline;
/* current line count of allocated history entries */
/**/
zlong histlinect;
/* The history lines are kept in a hash, and also doubly-linked in a ring */
/**/
HashTable histtab;
/**/
mod_export Histent hist_ring;
/* capacity of history lists */
/**/
zlong histsiz;
/* desired history-file size (in lines) */
/**/
zlong savehistsiz;
/* if = 1, we have performed history substitution on the current line *
* if = 2, we have used the 'p' modifier */
/**/
int histdone;
/* state of the history mechanism */
/**/
int histactive;
/* Current setting of the associated option, but sometimes also includes
* the setting of the HIST_SAVE_NO_DUPS option. */
/**/
int hist_ignore_all_dups;
/* What flags (if any) we should skip when moving through the history */
/**/
mod_export int hist_skip_flags;
/* Bits of histactive variable */
#define HA_ACTIVE (1<<0) /* History mechanism is active */
#define HA_NOINC (1<<1) /* Don't store, curhist not incremented */
#define HA_INWORD (1<<2) /* We're inside a word, don't add
start and end markers */
#define HA_UNGET (1<<3) /* Recursively ungetting */
/* Array of word beginnings and endings in current history line. */
/**/
short *chwords;
/* Max, actual position in chwords.
* nwords = chwordpos/2 because we record beginning and end of words.
*/
/**/
int chwordlen, chwordpos;
/* the last l for s/l/r/ history substitution */
/**/
char *hsubl;
/* the last r for s/l/r/ history substitution */
/**/
char *hsubr;
/* pointer into the history line */
/**/
mod_export char *hptr;
/* the current history line */
/**/
mod_export char *chline;
/*
* The current history line as seen by ZLE.
* We modify chline for use in other contexts while ZLE may
* still be running; ZLE should see only the top-level value.
*
* To avoid having to modify this every time we modify chline,
* we set it when we push the stack, and unset it when we pop
* the appropriate value off the stack. As it's never modified
* on the stack this is the only maintenance we ever do on it.
* In return, ZLE has to check both zle_chline and (if that's
* NULL) chline to get the current value.
*/
/**/
mod_export char *zle_chline;
/* true if the last character returned by hgetc was an escaped bangchar *
* if it is set and NOBANGHIST is unset hwaddc escapes bangchars */
/**/
int qbang;
/* max size of histline */
/**/
int hlinesz;
/* default event (usually curhist-1, that is, "!!") */
static zlong defev;
/*
* Flag that we stopped reading line when we got to a comment,
* but we want to keep it in the histofy even if there were no words
* (i.e. the comment was the entire line).
*/
static int hist_keep_comment;
/* Remember the last line in the history file so we can find it again. */
static struct histfile_stats {
char *text;
time_t stim, mtim;
off_t fpos, fsiz;
int interrupted;
zlong next_write_ev;
} lasthist;
static struct histsave {
struct histfile_stats lasthist;
char *histfile;
HashTable histtab;
Histent hist_ring;
zlong curhist;
zlong histlinect;
zlong histsiz;
zlong savehistsiz;
int locallevel;
} *histsave_stack;
static int histsave_stack_size = 0;
static int histsave_stack_pos = 0;
static zlong histfile_linect;
/* save history context */
/**/
void
hist_context_save(struct hist_stack *hs, int toplevel)
{
if (toplevel) {
/* top level, make this version visible to ZLE */
zle_chline = chline;
/* ensure line stored is NULL-terminated */
if (hptr)
*hptr = '\0';
}
hs->histactive = histactive;
hs->histdone = histdone;
hs->stophist = stophist;
hs->hline = chline;
hs->hptr = hptr;
hs->chwords = chwords;
hs->chwordlen = chwordlen;
hs->chwordpos = chwordpos;
hs->hgetc = hgetc;
hs->hungetc = hungetc;
hs->hwaddc = hwaddc;
hs->hwbegin = hwbegin;
hs->hwabort = hwabort;
hs->hwend = hwend;
hs->addtoline = addtoline;
hs->hlinesz = hlinesz;
hs->defev = defev;
hs->hist_keep_comment = hist_keep_comment;
/*
* We save and restore the command stack with history
* as it's visible to the user interactively, so if
* we're preserving history state we'll continue to
* show the current set of commands from input.
*/
hs->cstack = cmdstack;
hs->csp = cmdsp;
stophist = 0;
chline = NULL;
hptr = NULL;
histactive = 0;
cmdstack = (unsigned char *)zalloc(CMDSTACKSZ);
cmdsp = 0;
}
/* restore history context */
/**/
void
hist_context_restore(const struct hist_stack *hs, int toplevel)
{
if (toplevel) {
/* Back to top level: don't need special ZLE value */
DPUTS(hs->hline != zle_chline, "BUG: Ouch, wrong chline for ZLE");
zle_chline = NULL;
}
histactive = hs->histactive;
histdone = hs->histdone;
stophist = hs->stophist;
chline = hs->hline;
hptr = hs->hptr;
chwords = hs->chwords;
chwordlen = hs->chwordlen;
chwordpos = hs->chwordpos;
hgetc = hs->hgetc;
hungetc = hs->hungetc;
hwaddc = hs->hwaddc;
hwbegin = hs->hwbegin;
hwabort = hs->hwabort;
hwend = hs->hwend;
addtoline = hs->addtoline;
hlinesz = hs->hlinesz;
defev = hs->defev;
hist_keep_comment = hs->hist_keep_comment;
if (cmdstack)
zfree(cmdstack, CMDSTACKSZ);
cmdstack = hs->cstack;
cmdsp = hs->csp;
}
/*
* Mark that the current level of history is within a word whatever
* characters turn up, or turn that mode off. This is used for nested
* parsing of substitutions.
*
* The caller takes care only to turn this on or off at the start
* or end of recursive use of the same mode, so a single flag is
* good enough here.
*/
/**/
void
hist_in_word(int yesno)
{
if (yesno)
histactive |= HA_INWORD;
else
histactive &= ~HA_INWORD;
}
/* add a character to the current history word */
static void
ihwaddc(int c)
{
/* Only if history line exists and lexing has not finished. */
if (chline && !(errflag || lexstop) &&
/*
* If we're reading inside a word for command substitution
* we allow the lexer to expand aliases but don't deal
* with them here. Note matching code in ihungetc().
* TBD: it might be neater to deal with all aliases in this
* fashion as we never need the expansion in the history
* line, only in the lexer and above.
*/
(inbufflags & (INP_ALIAS|INP_HIST)) != INP_ALIAS) {
/* Quote un-expanded bangs in the history line. */
if (c == bangchar && stophist < 2 && qbang)
/* If qbang is not set, we do not escape this bangchar as it's *
* not necessary (e.g. it's a bang in !=, or it is followed *
* by a space). Roughly speaking, qbang is zero only if the *
* history interpreter has already digested this bang and *
* found that it is not necessary to escape it. */
hwaddc('\\');
*hptr++ = c;
/* Resize history line if necessary */
if (hptr - chline >= hlinesz) {
int oldsiz = hlinesz;
chline = realloc(chline, hlinesz = oldsiz + 64);
hptr = chline + oldsiz;
}
}
}
/* This function adds a character to the zle input line. It is used when *
* zsh expands history (see doexpandhist() in zle_tricky.c). It also *
* calculates the new cursor position after the expansion. It is called *
* from hgetc() and from gettok() in lex.c for characters in comments. */
/**/
void
iaddtoline(int c)
{
if (!expanding || lexstop)
return;
if (qbang && c == bangchar && stophist < 2) {
exlast--;
zleentry(ZLE_CMD_ADD_TO_LINE, '\\');
}
if (excs > zlemetacs) {
excs += 1 + inbufct - exlast;
if (excs < zlemetacs)
/* this case could be handled better but it is *
* so rare that it does not worth it */
excs = zlemetacs;
}
exlast = inbufct;
zleentry(ZLE_CMD_ADD_TO_LINE, itok(c) ? ztokens[c - Pound] : c);
}
static int
ihgetc(void)
{
int c = ingetc();
if (exit_pending)
{
lexstop = 1;
errflag |= ERRFLAG_ERROR;
return ' ';
}
qbang = 0;
if (!stophist && !(inbufflags & INP_ALIAS)) {
/* If necessary, expand history characters. */
c = histsubchar(c);
if (c < 0) {
/* bad expansion */
lexstop = 1;
errflag |= ERRFLAG_ERROR;
return ' ';
}
}
if ((inbufflags & INP_HIST) && !stophist) {
/* the current character c came from a history expansion *
* (inbufflags & INP_HIST) and history is not disabled *
* (e.g. we are not inside single quotes). In that case, \! *
* should be treated as ! (since this \! came from a previous *
* history line where \ was used to escape the bang). So if *
* c == '\\' we fetch one more character to see if it's a bang, *
* and if it is not, we unget it and reset c back to '\\' */
qbang = 0;
if (c == '\\' && !(qbang = (c = ingetc()) == bangchar))
safeinungetc(c), c = '\\';
} else if (stophist || (inbufflags & INP_ALIAS))
/* If the result is a bangchar which came from history or alias *
* expansion, we treat it as an escaped bangchar, unless history *
* is disabled. If stophist == 1 it only means that history is *
* temporarily disabled by a !" which won't appear in the *
* history, so we still have an escaped bang. stophist > 1 if *
* history is disabled with NOBANGHIST or by someone else (e.g. *
* when the lexer scans single quoted text). */
qbang = c == bangchar && (stophist < 2);
hwaddc(c);
addtoline(c);
return c;
}
/**/
static void
safeinungetc(int c)
{
if (lexstop)
lexstop = 0;
else
inungetc(c);
}
/**/
void
herrflush(void)
{
inpopalias();
if (lexstop)
return;
/*
* The lex_add_raw test is needed if we are parsing a command
* substitution when expanding history for ZLE: strin is set but we
* need to finish off the input because the string we are reading is
* going to be used directly in the line that goes to ZLE.
*
* Note that this is a side effect --- this is not the usual reason
* for testing lex_add_raw which is to add the text to a different
* buffer used when we are actually parsing the command substitution
* (nothing to do with ZLE). Sorry.
*/
while (inbufct && (!strin || lex_add_raw)) {
int c = ingetc();
if (!lexstop) {
hwaddc(c);
addtoline(c);
}
}
}
/*
* Extract :s/foo/bar/ delimiters and arguments
*
* The first character expected is the first delimiter.
* The arguments are stored in the hsubl and hsubr variables.
*
* subline is the part of the command line to be matched.
*
* If a ':' was found but was not followed by a 'G',
* *cflagp is set to 1 and the input is backed up to the
* character following the colon.
*/
/**/
static int
getsubsargs(UNUSED(char *subline), int *gbalp, int *cflagp)
{
int del, follow;
char *ptr1, *ptr2;
del = ingetc();
ptr1 = hdynread2(del);
if (!ptr1)
return 1;
ptr2 = hdynread2(del);
if (strlen(ptr1)) {
zsfree(hsubl);
hsubl = ptr1;
} else if (!hsubl) { /* fail silently on this */
zsfree(ptr1);
zsfree(ptr2);
return 0;
}
zsfree(hsubr);
hsubr = ptr2;
follow = ingetc();
if (follow == ':') {
follow = ingetc();
if (follow == 'G')
*gbalp = 1;
else {
inungetc(follow);
*cflagp = 1;
}
} else
inungetc(follow);
return 0;
}
/* Get the maximum no. of words for a history entry. */
/**/
static int
getargc(Histent ehist)
{
return ehist->nwords ? ehist->nwords-1 : 0;
}
/**/
static int
substfailed(void)
{
herrflush();
zerr("substitution failed");
return -1;
}
/*
* Return a count given by decimal digits after a modifier.
*/
static int
digitcount(void)
{
int c = ingetc(), count;
if (idigit(c)) {
count = 0;
do {
count = 10 * count + (c - '0');
c = ingetc();
} while (idigit(c));
}
else
count = 0;
inungetc(c);
return count;
}
/* Perform history substitution, returning the next character afterwards. */
/**/
static int
histsubchar(int c)
{
int farg, evset = -1, larg, argc, cflag = 0, bflag = 0;
zlong ev;
static int marg = -1;
static zlong mev = -1;
char *buf, *ptr;
char *sline;
int lexraw_mark;
Histent ehist;
size_t buflen;
/*
* If accumulating raw input for use in command substitution,
* we don't want the history text, so mark it for later removal.
* It would be better to do this at a level above the history
* and below the lexer --- but there isn't one.
*
* Include the character we are attempting to substitute.
*/
lexraw_mark = zshlex_raw_mark(-1);
/* look, no goto's */
if (isfirstch && c == hatchar) {
int gbal = 0;
/* Line begins ^foo^bar */
isfirstch = 0;
inungetc(hatchar);
if (!(ehist = gethist(defev))
|| !(sline = getargs(ehist, 0, getargc(ehist))))
return -1;
if (getsubsargs(sline, &gbal, &cflag))
return substfailed();
if (!hsubl)
return -1;
if (subst(&sline, hsubl, hsubr, gbal))
return substfailed();
} else {
/* Line doesn't begin ^foo^bar */
if (c != ' ')
isfirstch = 0;
if (c == '\\') {
int g = ingetc();
if (g != bangchar)
safeinungetc(g);
else {
qbang = 1;
return bangchar;
}
}
if (c != bangchar)
return c;
*hptr = '\0';
if ((c = ingetc()) == '{') {
bflag = cflag = 1;
c = ingetc();
}
if (c == '\"') {
stophist = 1;
return ingetc();
}
if ((!cflag && inblank(c)) || c == '=' || c == '(' || lexstop) {
safeinungetc(c);
return bangchar;
}
cflag = 0;
ptr = buf = zhalloc(buflen = 265);
/* get event number */
queue_signals();
if (c == '?') {
for (;;) {
c = ingetc();
if (c == '?' || c == '\n' || lexstop)
break;
else {
*ptr++ = c;
if (ptr == buf + buflen) {
buf = hrealloc(buf, buflen, 2 * buflen);
ptr = buf + buflen;
buflen *= 2;
}
}
}
if (c != '\n' && !lexstop)
c = ingetc();
*ptr = '\0';
mev = ev = hconsearch(hsubl = ztrdup(buf), &marg);
evset = 0;
if (ev == -1) {
herrflush();
unqueue_signals();
zerr("no such event: %s", buf);
return -1;
}
} else {
zlong t0;
for (;;) {
if (inblank(c) || c == ';' || c == ':' || c == '^' ||
c == '$' || c == '*' || c == '%' || c == '}' ||
c == '\'' || c == '"' || c == '`' || lexstop)
break;
if (ptr != buf) {
if (c == '-')
break;
if ((idigit(buf[0]) || buf[0] == '-') && !idigit(c))
break;
}
*ptr++ = c;
if (ptr == buf + buflen) {
buf = hrealloc(buf, buflen, 2 * buflen);
ptr = buf + buflen;
buflen *= 2;
}
if (c == '#' || c == bangchar) {
c = ingetc();
break;
}
c = ingetc();
}
if (ptr == buf &&
(c == '}' || c == ';' || c == '\'' || c == '"' || c == '`')) {
/* Neither event nor word designator, no expansion */
safeinungetc(c);
unqueue_signals();
return bangchar;
}
*ptr = 0;
if (!*buf) {
if (c != '%') {
if (isset(CSHJUNKIEHISTORY))
ev = addhistnum(curhist,-1,HIST_FOREIGN);
else
ev = defev;
if (c == ':' && evset == -1)
evset = 0;
else
evset = 1;
} else {
if (marg != -1)
ev = mev;
else
ev = defev;
evset = 0;
}
} else if ((t0 = zstrtol(buf, NULL, 10))) {
ev = (t0 < 0) ? addhistnum(curhist,t0,HIST_FOREIGN) : t0;
evset = 1;
} else if ((unsigned)*buf == bangchar) {
ev = addhistnum(curhist,-1,HIST_FOREIGN);
evset = 1;
} else if (*buf == '#') {
ev = curhist;
evset = 1;
} else if ((ev = hcomsearch(buf)) == -1) {
herrflush();
unqueue_signals();
zerr("event not found: %s", buf);
return -1;
} else
evset = 1;
}
/* get the event */
if (!(ehist = gethist(defev = ev))) {
unqueue_signals();
return -1;
}
/* extract the relevant arguments */
argc = getargc(ehist);
if (c == ':') {
cflag = 1;
c = ingetc();
if (c == '%' && marg != -1) {
if (!evset) {
ehist = gethist(defev = mev);
argc = getargc(ehist);
} else {
herrflush();
unqueue_signals();
zerr("ambiguous history reference");
return -1;
}
}
}
if (c == '*') {
farg = 1;
larg = argc;
cflag = 0;
} else {
inungetc(c);
larg = farg = getargspec(argc, marg, evset);
if (larg == -2) {
unqueue_signals();
return -1;
}
if (farg != -1)
cflag = 0;
c = ingetc();
if (c == '*') {
cflag = 0;
larg = argc;
} else if (c == '-') {
cflag = 0;
larg = getargspec(argc, marg, evset);
if (larg == -2) {
unqueue_signals();
return -1;
}
if (larg == -1)
larg = argc - 1;
} else
inungetc(c);
}
if (farg == -1)
farg = 0;
if (larg == -1)
larg = argc;
if (!(sline = getargs(ehist, farg, larg))) {
unqueue_signals();
return -1;
}
unqueue_signals();
}
/* do the modifiers */
for (;;) {
c = (cflag) ? ':' : ingetc();
cflag = 0;
if (c == ':') {
int gbal = 0;
if ((c = ingetc()) == 'g') {
gbal = 1;
c = ingetc();
if (c != 's' && c != '&') {
zerr("'s' or '&' modifier expected after 'g'");
return -1;
}
}
switch (c) {
case 'p':
histdone = HISTFLAG_DONE | HISTFLAG_NOEXEC;
break;
case 'a':
if (!chabspath(&sline)) {
herrflush();
zerr("modifier failed: a");
return -1;
}
break;
case 'A':
if (!chrealpath(&sline, 'A', 1)) {
herrflush();
zerr("modifier failed: A");
return -1;
}
break;
case 'c':
if (!(sline = equalsubstr(sline, 0, 0))) {
herrflush();
zerr("modifier failed: c");
return -1;
}
break;
case 'h':
if (!remtpath(&sline, digitcount())) {
herrflush();
zerr("modifier failed: h");
return -1;
}
break;
case 'e':
if (!rembutext(&sline)) {
herrflush();
zerr("modifier failed: e");
return -1;
}
break;
case 'r':
if (!remtext(&sline)) {
herrflush();
zerr("modifier failed: r");
return -1;
}
break;
case 't':
if (!remlpaths(&sline, digitcount())) {
herrflush();
zerr("modifier failed: t");
return -1;
}
break;
case 's':
if (getsubsargs(sline, &gbal, &cflag))
return -1; /* fall through */
case '&':
if (hsubl && hsubr) {
if (subst(&sline, hsubl, hsubr, gbal))
return substfailed();
} else {
herrflush();
zerr("no previous substitution");
return -1;
}
break;
case 'q':
quote(&sline);
break;
case 'Q':
{
int one = noerrs, oef = errflag;
noerrs = 1;
parse_subst_string(sline);
noerrs = one;
errflag = oef | (errflag & ERRFLAG_INT);
remnulargs(sline);
untokenize(sline);
}
break;
case 'x':
quotebreak(&sline);
break;
case 'l':
sline = casemodify(sline, CASMOD_LOWER);
break;
case 'u':
sline = casemodify(sline, CASMOD_UPPER);
break;
case 'P':
if (*sline != '/') {
char *here = zgetcwd();
if (here[strlen(here)-1] != '/')
sline = zhtricat(metafy(here, -1, META_HEAPDUP), "/", sline);
else
sline = dyncat(here, sline);
}
sline = xsymlink(sline, 1);
break;
default:
herrflush();
zerr("illegal modifier: %c", c);
return -1;
}
} else {
if (c != '}' || !bflag)
inungetc(c);
if (c != '}' && bflag) {
zerr("'}' expected");
return -1;
}
break;
}
}
zshlex_raw_back_to_mark(lexraw_mark);
/*
* Push the expanded value onto the input stack,
* marking this as a history word for purposes of the alias stack.
*/
lexstop = 0;
/* this function is called only called from hgetc and only if *
* !(inbufflags & INP_ALIAS). History expansion should never be *
* done with INP_ALIAS (to prevent recursive history expansion and *
* histoty expansion of aliases). Escapes are not removed here. *
* This is now handled in hgetc. */
inpush(sline, INP_HIST, NULL); /* sline from heap, don't free */
histdone |= HISTFLAG_DONE;
if (isset(HISTVERIFY))
histdone |= HISTFLAG_NOEXEC | HISTFLAG_RECALL;
/* Don't try and re-expand line. */
return ingetc();
}
/* unget a char and remove it from chline. It can only be used *
* to unget a character returned by hgetc. */
static void
ihungetc(int c)
{
int doit = 1;
while (!lexstop && !errflag) {
if (hptr[-1] != (char) c && stophist < 4 &&
hptr > chline + 1 && hptr[-1] == '\n' && hptr[-2] == '\\' &&
!(histactive & HA_UNGET) &&
(inbufflags & (INP_ALIAS|INP_HIST)) != INP_ALIAS) {
histactive |= HA_UNGET;
hungetc('\n');
hungetc('\\');
histactive &= ~HA_UNGET;
}
if (expanding) {
zlemetacs--;
zlemetall--;
exlast++;
}
if ((inbufflags & (INP_ALIAS|INP_HIST)) != INP_ALIAS) {
DPUTS(hptr <= chline, "BUG: hungetc attempted at buffer start");
hptr--;
DPUTS(*hptr != (char) c, "BUG: wrong character in hungetc() ");
qbang = (c == bangchar && stophist < 2 &&
hptr > chline && hptr[-1] == '\\');