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
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1982-2010 AT&T Intellectual Property *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.opensource.org/licenses/cpl1.0.txt *
* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* bash style history expansion
*
* Author:
* Karsten Fleischer
* Omnium Software Engineering
* An der Luisenburg 7
* D-51379 Leverkusen
* Germany
*
* <K.Fleischer@omnium.de>
*/
#include "defs.h"
#include "edit.h"
#if ! SHOPT_HISTEXPAND
NoN(hexpand)
#else
static char *modifiers = "htrepqxs&";
static int mod_flags[] = { 0, 0, 0, 0, HIST_PRINT, HIST_QUOTE, HIST_QUOTE|HIST_QUOTE_BR, 0, 0 };
#define DONE() {flag |= HIST_ERROR; cp = 0; stakseek(0); goto done;}
struct subst
{
char *str[2]; /* [0] is "old", [1] is "new" string */
};
/*
* parse an /old/new/ string, delimiter expected as first char.
* if "old" not specified, keep sb->str[0]
* if "new" not specified, set sb->str[1] to empty string
* read up to third delimeter char, \n or \0, whichever comes first.
* return adress is one past the last valid char in s:
* - the address containing \n or \0 or
* - one char beyond the third delimiter
*/
static char *parse_subst(const char *s, struct subst *sb)
{
char *cp,del;
int off,n = 0;
/* build the strings on the stack, mainly for '&' substition in "new" */
off = staktell();
/* init "new" with empty string */
if(sb->str[1])
free(sb->str[1]);
sb->str[1] = strdup("");
/* get delimiter */
del = *s;
cp = (char*) s + 1;
while(n < 2)
{
if(*cp == del || *cp == '\n' || *cp == '\0')
{
/* delimiter or EOL */
if(staktell() != off)
{
/* dupe string on stack and rewind stack */
stakputc('\0');
if(sb->str[n])
free(sb->str[n]);
sb->str[n] = strdup(stakptr(off));
stakseek(off);
}
n++;
/* if not delimiter, we've reached EOL. Get outta here. */
if(*cp != del)
break;
}
else if(*cp == '\\')
{
if(*(cp+1) == del) /* quote delimiter */
{
stakputc(del);
cp++;
}
else if(*(cp+1) == '&' && n == 1)
{ /* quote '&' only in "new" */
stakputc('&');
cp++;
}
else
stakputc('\\');
}
else if(*cp == '&' && n == 1 && sb->str[0])
/* substitute '&' with "old" in "new" */
stakputs(sb->str[0]);
else
stakputc(*cp);
cp++;
}
/* rewind stack */
stakseek(off);
return cp;
}
/*
* history expansion main routine
*/
int hist_expand(const char *ln, char **xp)
{
int off, /* stack offset */
q, /* quotation flags */
p, /* flag */
c, /* current char */
flag=0; /* HIST_* flags */
Sfoff_t n, /* history line number, counter, etc. */
i, /* counter */
w[2]; /* word range */
char *sp, /* stack pointer */
*cp, /* current char in ln */
*str, /* search string */
*evp, /* event/word designator string, for error msgs */
*cc=0, /* copy of current line up to cp; temp ptr */
hc[3], /* default histchars */
*qc="\'\"`"; /* quote characters */
Sfio_t *ref=0, /* line referenced by event designator */
*tmp=0, /* temporary line buffer */
*tmp2=0;/* temporary line buffer */
Histloc_t hl; /* history location */
static Namval_t *np = 0; /* histchars variable */
static struct subst sb = {0,0}; /* substition strings */
static Sfio_t *wm=0; /* word match from !?string? event designator */
if(!wm)
wm = sfopen(NULL, NULL, "swr");
hc[0] = '!';
hc[1] = '^';
hc[2] = 0;
if((np = nv_open("histchars",sh.var_tree,0)) && (cp = nv_getval(np)))
{
if(cp[0])
{
hc[0] = cp[0];
if(cp[1])
{
hc[1] = cp[1];
if(cp[2])
hc[2] = cp[2];
}
}
}
/* save shell stack */
if(off = staktell())
sp = stakfreeze(0);
cp = (char*)ln;
while(cp && *cp)
{
/* read until event/quick substitution/comment designator */
if((*cp != hc[0] && *cp != hc[1] && *cp != hc[2])
|| (*cp == hc[1] && cp != ln))
{
if(*cp == '\\') /* skip escaped designators */
stakputc(*cp++);
else if(*cp == '\'') /* skip quoted designators */
{
do
stakputc(*cp);
while(*++cp && *cp != '\'');
}
stakputc(*cp++);
continue;
}
if(hc[2] && *cp == hc[2]) /* history comment designator, skip rest of line */
{
stakputc(*cp++);
stakputs(cp);
DONE();
}
n = -1;
str = 0;
flag &= HIST_EVENT; /* save event flag for returning later */
evp = cp;
ref = 0;
if(*cp == hc[1]) /* shortcut substitution */
{
flag |= HIST_QUICKSUBST;
goto getline;
}
if(*cp == hc[0] && *(cp+1) == hc[0]) /* refer to line -1 */
{
cp += 2;
goto getline;
}
switch(c = *++cp) {
case ' ':
case '\t':
case '\n':
case '\0':
case '=':
case '(':
stakputc(hc[0]);
continue;
case '#': /* the line up to current position */
flag |= HIST_HASH;
cp++;
n = staktell(); /* terminate string and dup */
stakputc('\0');
cc = strdup(stakptr(0));
stakseek(n); /* remove null byte again */
ref = sfopen(ref, cc, "s"); /* open as file */
n = 0; /* skip history file referencing */
break;
case '-': /* back reference by number */
if(!isdigit(*(cp+1)))
goto string_event;
cp++;
/* FALLTHROUGH */
case '0': /* reference by number */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = 0;
while(isdigit(*cp))
n = n * 10 + (*cp++) - '0';
if(c == '-')
n = -n;
break;
case '$':
n = -1;
case ':':
break;
case '?':
cp++;
flag |= HIST_QUESTION;
/* FALLTHROUGH */
string_event:
default:
/* read until end of string or word designator/modifier */
str = cp;
while(*cp)
{
cp++;
if((!(flag&HIST_QUESTION) &&
(*cp == ':' || isspace(*cp)
|| *cp == '^' || *cp == '$'
|| *cp == '*' || *cp == '-'
|| *cp == '%')
)
|| ((flag&HIST_QUESTION) && (*cp == '?' || *cp == '\n')))
{
c = *cp;
*cp = '\0';
}
}
break;
}
getline:
flag |= HIST_EVENT;
if(str) /* !string or !?string? event designator */
{
/* search history for string */
hl = hist_find(sh.hist_ptr, str,
sh.hist_ptr->histind,
flag&HIST_QUESTION, -1);
if((n = hl.hist_command) == -1)
n = 0; /* not found */
}
if(n)
{
if(n < 0) /* determine index for backref */
n = sh.hist_ptr->histind + n;
/* search and use history file if found */
if(n > 0 && hist_seek(sh.hist_ptr, n) != -1)
ref = sh.hist_ptr->histfp;
}
if(!ref)
{
/* string not found or command # out of range */
c = *cp;
*cp = '\0';
errormsg(SH_DICT, ERROR_ERROR, "%s: event not found", evp);
*cp = c;
DONE();
}
if(str) /* string search: restore orig. line */
{
if(flag&HIST_QUESTION)
*cp++ = c; /* skip second question mark */
else
*cp = c;
}
/* colon introduces either word designators or modifiers */
if(*(evp = cp) == ':')
cp++;
w[0] = 0; /* -1 means last word, -2 means match from !?string? */
w[1] = -1; /* -1 means last word, -2 means suppress last word */
if(flag & HIST_QUICKSUBST) /* shortcut substitution */
goto getsel;
n = 0;
while(n < 2)
{
switch(c = *cp++) {
case '^': /* first word */
if(n == 0)
{
w[0] = w[1] = 1;
goto skip;
}
else
goto skip2;
case '$': /* last word */
w[n] = -1;
goto skip;
case '%': /* match from !?string? event designator */
if(n == 0)
{
if(!str)
{
w[0] = 0;
w[1] = -1;
ref = wm;
}
else
{
w[0] = -2;
w[1] = sftell(ref) + hl.hist_char;
}
sfseek(wm, 0, SEEK_SET);
goto skip;
}
/* FALLTHROUGH */
default:
skip2:
cp--;
n = 2;
break;
case '*': /* until last word */
if(n == 0)
w[0] = 1;
w[1] = -1;
skip:
flag |= HIST_WORDDSGN;
n = 2;
break;
case '-': /* until last word or specified index */
w[1] = -2;
flag |= HIST_WORDDSGN;
n = 1;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': /* specify index */
if((*evp == ':') || w[1] == -2)
{
w[n] = c - '0';
while(isdigit(c=*cp++))
w[n] = w[n] * 10 + c - '0';
flag |= HIST_WORDDSGN;
if(n == 0)
w[1] = w[0];
n++;
}
else
n = 2;
cp--;
break;
}
}
if(w[0] != -2 && w[1] > 0 && w[0] > w[1])
{
c = *cp;
*cp = '\0';
errormsg(SH_DICT, ERROR_ERROR, "%s: bad word specifier", evp);
*cp = c;
DONE();
}
/* no valid word designator after colon, rewind */
if(!(flag & HIST_WORDDSGN) && (*evp == ':'))
cp = evp;
getsel:
/* open temp buffer, let sfio do the (re)allocation */
tmp = sfopen(NULL, NULL, "swr");
/* push selected words into buffer, squash
whitespace into single blank or a newline */
n = i = q = 0;
while((c = sfgetc(ref)) > 0)
{
if(isspace(c))
{
flag |= (c == '\n' ? HIST_NEWLINE : 0);
continue;
}
if(n >= w[0] && ((w[0] != -2) ? (w[1] < 0 || n <= w[1]) : 1))
{
if(w[0] < 0)
sfseek(tmp, 0, SEEK_SET);
else
i = sftell(tmp);
if(i > 0)
sfputc(tmp, flag & HIST_NEWLINE ? '\n' : ' ');
flag &= ~HIST_NEWLINE;
p = 1;
}
else
p = 0;
do
{
cc = strchr(qc, c);
q ^= cc ? 1<<(int)(cc - qc) : 0;
if(p)
sfputc(tmp, c);
}
while((c = sfgetc(ref)) > 0 && (!isspace(c) || q));
if(w[0] == -2 && sftell(ref) > w[1])
break;
flag |= (c == '\n' ? HIST_NEWLINE : 0);
n++;
}
if(w[0] != -2 && w[1] >= 0 && w[1] >= n)
{
c = *cp;
*cp = '\0';
errormsg(SH_DICT, ERROR_ERROR, "%s: bad word specifier", evp);
*cp = c;
DONE();
}
else if(w[1] == -2) /* skip last word */
sfseek(tmp, i, SEEK_SET);
/* remove trailing newline */
if(sftell(tmp))
{
sfseek(tmp, -1, SEEK_CUR);
if(sfgetc(tmp) == '\n')
sfungetc(tmp, '\n');
}
sfputc(tmp, '\0');
if(str)
{
if(wm)
sfclose(wm);
wm = tmp;
}
if(cc && (flag&HIST_HASH))
{
/* close !# temp file */
sfclose(ref);
flag &= ~HIST_HASH;
free(cc);
cc = 0;
}
evp = cp;
/* selected line/words are now in buffer, now go for the modifiers */
while(*cp == ':' || (flag & HIST_QUICKSUBST))
{
if(flag & HIST_QUICKSUBST)
{
flag &= ~HIST_QUICKSUBST;
c = 's';
cp--;
}
else
c = *++cp;
sfseek(tmp, 0, SEEK_SET);
tmp2 = sfopen(tmp2, NULL, "swr");
if(c == 'g') /* global substitution */
{
flag |= HIST_GLOBALSUBST;
c = *++cp;
}
if(cc = strchr(modifiers, c))
flag |= mod_flags[cc - modifiers];
else
{
errormsg(SH_DICT, ERROR_ERROR, "%c: unrecognized history modifier", c);
DONE();
}
if(c == 'h' || c == 'r') /* head or base */
{
n = -1;
while((c = sfgetc(tmp)) > 0)
{ /* remember position of / or . */
if((c == '/' && *cp == 'h') || (c == '.' && *cp == 'r'))
n = sftell(tmp2);
sfputc(tmp2, c);
}
if(n > 0)
{ /* rewind to last / or . */
sfseek(tmp2, n, SEEK_SET);
/* end string there */
sfputc(tmp2, '\0');
}
}
else if(c == 't' || c == 'e') /* tail or suffix */
{
n = 0;
while((c = sfgetc(tmp)) > 0)
{ /* remember position of / or . */
if((c == '/' && *cp == 't') || (c == '.' && *cp == 'e'))
n = sftell(tmp);
}
/* rewind to last / or . */
sfseek(tmp, n, SEEK_SET);
/* copy from there on */
while((c = sfgetc(tmp)) > 0)
sfputc(tmp2, c);
}
else if(c == 's' || c == '&')
{
cp++;
if(c == 's')
{
/* preset old with match from !?string? */
if(!sb.str[0] && wm)
sb.str[0] = strdup(sfsetbuf(wm, (Void_t*)1, 0));
cp = parse_subst(cp, &sb);
}
if(!sb.str[0] || !sb.str[1])
{
c = *cp;
*cp = '\0';
errormsg(SH_DICT, ERROR_ERROR,
"%s%s: no previous substitution",
(flag & HIST_QUICKSUBST) ? ":s" : "",
evp);
*cp = c;
DONE();
}
/* need pointer for strstr() */
str = sfsetbuf(tmp, (Void_t*)1, 0);
flag |= HIST_SUBSTITUTE;
while(flag & HIST_SUBSTITUTE)
{
/* find string */
if(cc = strstr(str, sb.str[0]))
{ /* replace it */
c = *cc;
*cc = '\0';
sfputr(tmp2, str, -1);
sfputr(tmp2, sb.str[1], -1);
*cc = c;
str = cc + strlen(sb.str[0]);
}
else if(!sftell(tmp2))
{ /* not successfull */
c = *cp;
*cp = '\0';
errormsg(SH_DICT, ERROR_ERROR,
"%s%s: substitution failed",
(flag & HIST_QUICKSUBST) ? ":s" : "",
evp);
*cp = c;
DONE();
}
/* loop if g modifier specified */
if(!cc || !(flag & HIST_GLOBALSUBST))
flag &= ~HIST_SUBSTITUTE;
}
/* output rest of line */
sfputr(tmp2, str, -1);
if(*cp)
cp--;
}
if(sftell(tmp2))
{ /* if any substitions done, swap buffers */
if(wm != tmp)
sfclose(tmp);
tmp = tmp2;
tmp2 = 0;
}
cc = 0;
if(*cp)
cp++;
}
/* flush temporary buffer to stack */
if(tmp)
{
sfseek(tmp, 0, SEEK_SET);
if(flag & HIST_QUOTE)
stakputc('\'');
while((c = sfgetc(tmp)) > 0)
{
if(isspace(c))
{
flag = flag & ~HIST_NEWLINE;
/* squash white space to either a
blank or a newline */
do
flag |= (c == '\n' ? HIST_NEWLINE : 0);
while((c = sfgetc(tmp)) > 0 && isspace(c));
sfungetc(tmp, c);
c = (flag & HIST_NEWLINE) ? '\n' : ' ';
if(flag & HIST_QUOTE_BR)
{
stakputc('\'');
stakputc(c);
stakputc('\'');
}
else
stakputc(c);
}
else if((c == '\'') && (flag & HIST_QUOTE))
{
stakputc('\'');
stakputc('\\');
stakputc(c);
stakputc('\'');
}
else
stakputc(c);
}
if(flag & HIST_QUOTE)
stakputc('\'');
}
}
stakputc('\0');
done:
if(cc && (flag&HIST_HASH))
{
/* close !# temp file */
sfclose(ref);
free(cc);
cc = 0;
}
/* error? */
if(staktell() && !(flag & HIST_ERROR))
*xp = strdup(stakfreeze(1));
/* restore shell stack */
if(off)
stakset(sp,off);
else
stakseek(0);
/* drop temporary files */
if(tmp && tmp != wm)
sfclose(tmp);
if(tmp2)
sfclose(tmp2);
return (flag & HIST_ERROR ? HIST_ERROR : flag & HIST_FLAG_RETURN_MASK);
}
#endif
|