1 /*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kenneth Almquist.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #include <dirent.h>
44 #include <limits.h>
45 #include <paths.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 /*
50 * Editline and history functions (and glue).
51 */
52 #include "shell.h"
53 #include "parser.h"
54 #include "var.h"
55 #include "options.h"
56 #include "main.h"
57 #include "output.h"
58 #include "mystring.h"
59 #include "builtins.h"
60 #ifndef NO_HISTORY
61 #include "myhistedit.h"
62 #include "error.h"
63 #include "eval.h"
64 #include "memalloc.h"
65
66 #define MAXHISTLOOPS 4 /* max recursions through fc */
67 #define DEFEDITOR "ed" /* default editor *should* be $EDITOR */
68
69 History *hist; /* history cookie */
70 EditLine *el; /* editline cookie */
71 int displayhist;
72 static FILE *el_in, *el_out;
73
74 static char *fc_replace(const char *, char *, char *);
75 static int not_fcnumber(const char *);
76 static int str_to_event(const char *, int);
77 static int comparator(const void *, const void *, void *);
78 static char **sh_matches(const char *, int, int);
79 static unsigned char sh_complete(EditLine *, int);
80
81 /*
82 * Set history and editing status. Called whenever the status may
83 * have changed (figures out what to do).
84 */
85 void
histedit(void)86 histedit(void)
87 {
88
89 #define editing (Eflag || Vflag)
90
91 if (iflag) {
92 if (!hist) {
93 /*
94 * turn history on
95 */
96 INTOFF;
97 hist = history_init();
98 INTON;
99
100 if (hist != NULL)
101 sethistsize(histsizeval());
102 else
103 out2fmt_flush("sh: can't initialize history\n");
104 }
105 if (editing && !el && isatty(0)) { /* && isatty(2) ??? */
106 /*
107 * turn editing on
108 */
109 char *term;
110
111 INTOFF;
112 if (el_in == NULL)
113 el_in = fdopen(0, "r");
114 if (el_out == NULL)
115 el_out = fdopen(2, "w");
116 if (el_in == NULL || el_out == NULL)
117 goto bad;
118 term = lookupvar("TERM");
119 if (term)
120 setenv("TERM", term, 1);
121 else
122 unsetenv("TERM");
123 el = el_init(arg0, el_in, el_out, el_out);
124 if (el != NULL) {
125 if (hist)
126 el_set(el, EL_HIST, history, hist);
127 el_set(el, EL_PROMPT, getprompt);
128 el_set(el, EL_ADDFN, "sh-complete",
129 "Filename completion",
130 sh_complete);
131 } else {
132 bad:
133 out2fmt_flush("sh: can't initialize editing\n");
134 }
135 INTON;
136 } else if (!editing && el) {
137 INTOFF;
138 el_end(el);
139 el = NULL;
140 INTON;
141 }
142 if (el) {
143 if (Vflag)
144 el_set(el, EL_EDITOR, "vi");
145 else if (Eflag) {
146 el_set(el, EL_EDITOR, "emacs");
147 el_set(el, EL_BIND, "^R", "em-inc-search-prev", NULL);
148 }
149 el_set(el, EL_BIND, "^I", "sh-complete", NULL);
150 el_source(el, NULL);
151 }
152 } else {
153 INTOFF;
154 if (el) { /* no editing if not interactive */
155 el_end(el);
156 el = NULL;
157 }
158 if (hist) {
159 history_end(hist);
160 hist = NULL;
161 }
162 INTON;
163 }
164 }
165
166
167 void
sethistsize(const char * hs)168 sethistsize(const char *hs)
169 {
170 int histsize;
171 HistEvent he;
172
173 if (hist != NULL) {
174 if (hs == NULL || !is_number(hs))
175 histsize = 100;
176 else
177 histsize = atoi(hs);
178 history(hist, &he, H_SETSIZE, histsize);
179 history(hist, &he, H_SETUNIQUE, 1);
180 }
181 }
182
183 void
setterm(const char * term)184 setterm(const char *term)
185 {
186 if (rootshell && el != NULL && term != NULL)
187 el_set(el, EL_TERMINAL, term);
188 }
189
190 int
histcmd(int argc,char ** argv __unused)191 histcmd(int argc, char **argv __unused)
192 {
193 int ch;
194 const char *editor = NULL;
195 HistEvent he;
196 int lflg = 0, nflg = 0, rflg = 0, sflg = 0;
197 int i, retval;
198 const char *firststr, *laststr;
199 int first, last, direction;
200 char *pat = NULL, *repl = NULL;
201 static int active = 0;
202 struct jmploc jmploc;
203 struct jmploc *savehandler;
204 char editfilestr[PATH_MAX];
205 char *volatile editfile;
206 FILE *efp = NULL;
207 int oldhistnum;
208
209 if (hist == NULL)
210 error("history not active");
211
212 if (argc == 1)
213 error("missing history argument");
214
215 while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
216 switch ((char)ch) {
217 case 'e':
218 editor = shoptarg;
219 break;
220 case 'l':
221 lflg = 1;
222 break;
223 case 'n':
224 nflg = 1;
225 break;
226 case 'r':
227 rflg = 1;
228 break;
229 case 's':
230 sflg = 1;
231 break;
232 }
233
234 savehandler = handler;
235 /*
236 * If executing...
237 */
238 if (lflg == 0 || editor || sflg) {
239 lflg = 0; /* ignore */
240 editfile = NULL;
241 /*
242 * Catch interrupts to reset active counter and
243 * cleanup temp files.
244 */
245 if (setjmp(jmploc.loc)) {
246 active = 0;
247 if (editfile)
248 unlink(editfile);
249 handler = savehandler;
250 longjmp(handler->loc, 1);
251 }
252 handler = &jmploc;
253 if (++active > MAXHISTLOOPS) {
254 active = 0;
255 displayhist = 0;
256 error("called recursively too many times");
257 }
258 /*
259 * Set editor.
260 */
261 if (sflg == 0) {
262 if (editor == NULL &&
263 (editor = bltinlookup("FCEDIT", 1)) == NULL &&
264 (editor = bltinlookup("EDITOR", 1)) == NULL)
265 editor = DEFEDITOR;
266 if (editor[0] == '-' && editor[1] == '\0') {
267 sflg = 1; /* no edit */
268 editor = NULL;
269 }
270 }
271 }
272
273 /*
274 * If executing, parse [old=new] now
275 */
276 if (lflg == 0 && *argptr != NULL &&
277 ((repl = strchr(*argptr, '=')) != NULL)) {
278 pat = *argptr;
279 *repl++ = '\0';
280 argptr++;
281 }
282 /*
283 * determine [first] and [last]
284 */
285 if (*argptr == NULL) {
286 firststr = lflg ? "-16" : "-1";
287 laststr = "-1";
288 } else if (argptr[1] == NULL) {
289 firststr = argptr[0];
290 laststr = lflg ? "-1" : argptr[0];
291 } else if (argptr[2] == NULL) {
292 firststr = argptr[0];
293 laststr = argptr[1];
294 } else
295 error("too many arguments");
296 /*
297 * Turn into event numbers.
298 */
299 first = str_to_event(firststr, 0);
300 last = str_to_event(laststr, 1);
301
302 if (rflg) {
303 i = last;
304 last = first;
305 first = i;
306 }
307 /*
308 * XXX - this should not depend on the event numbers
309 * always increasing. Add sequence numbers or offset
310 * to the history element in next (diskbased) release.
311 */
312 direction = first < last ? H_PREV : H_NEXT;
313
314 /*
315 * If editing, grab a temp file.
316 */
317 if (editor) {
318 int fd;
319 INTOFF; /* easier */
320 sprintf(editfilestr, "%s/_shXXXXXX", _PATH_TMP);
321 if ((fd = mkstemp(editfilestr)) < 0)
322 error("can't create temporary file %s", editfile);
323 editfile = editfilestr;
324 if ((efp = fdopen(fd, "w")) == NULL) {
325 close(fd);
326 error("Out of space");
327 }
328 }
329
330 /*
331 * Loop through selected history events. If listing or executing,
332 * do it now. Otherwise, put into temp file and call the editor
333 * after.
334 *
335 * The history interface needs rethinking, as the following
336 * convolutions will demonstrate.
337 */
338 history(hist, &he, H_FIRST);
339 retval = history(hist, &he, H_NEXT_EVENT, first);
340 for (;retval != -1; retval = history(hist, &he, direction)) {
341 if (lflg) {
342 if (!nflg)
343 out1fmt("%5d ", he.num);
344 out1str(he.str);
345 } else {
346 const char *s = pat ?
347 fc_replace(he.str, pat, repl) : he.str;
348
349 if (sflg) {
350 if (displayhist) {
351 out2str(s);
352 flushout(out2);
353 }
354 evalstring(s, 0);
355 if (displayhist && hist) {
356 /*
357 * XXX what about recursive and
358 * relative histnums.
359 */
360 oldhistnum = he.num;
361 history(hist, &he, H_ENTER, s);
362 /*
363 * XXX H_ENTER moves the internal
364 * cursor, set it back to the current
365 * entry.
366 */
367 history(hist, &he,
368 H_NEXT_EVENT, oldhistnum);
369 }
370 } else
371 fputs(s, efp);
372 }
373 /*
374 * At end? (if we were to lose last, we'd sure be
375 * messed up).
376 */
377 if (he.num == last)
378 break;
379 }
380 if (editor) {
381 char *editcmd;
382
383 fclose(efp);
384 INTON;
385 editcmd = stalloc(strlen(editor) + strlen(editfile) + 2);
386 sprintf(editcmd, "%s %s", editor, editfile);
387 evalstring(editcmd, 0); /* XXX - should use no JC command */
388 readcmdfile(editfile); /* XXX - should read back - quick tst */
389 unlink(editfile);
390 }
391
392 if (lflg == 0 && active > 0)
393 --active;
394 if (displayhist)
395 displayhist = 0;
396 handler = savehandler;
397 return 0;
398 }
399
400 static char *
fc_replace(const char * s,char * p,char * r)401 fc_replace(const char *s, char *p, char *r)
402 {
403 char *dest;
404 int plen = strlen(p);
405
406 STARTSTACKSTR(dest);
407 while (*s) {
408 if (*s == *p && strncmp(s, p, plen) == 0) {
409 STPUTS(r, dest);
410 s += plen;
411 *p = '\0'; /* so no more matches */
412 } else
413 STPUTC(*s++, dest);
414 }
415 STPUTC('\0', dest);
416 dest = grabstackstr(dest);
417
418 return (dest);
419 }
420
421 static int
not_fcnumber(const char * s)422 not_fcnumber(const char *s)
423 {
424 if (s == NULL)
425 return (0);
426 if (*s == '-')
427 s++;
428 return (!is_number(s));
429 }
430
431 static int
str_to_event(const char * str,int last)432 str_to_event(const char *str, int last)
433 {
434 HistEvent he;
435 const char *s = str;
436 int relative = 0;
437 int i, retval;
438
439 retval = history(hist, &he, H_FIRST);
440 switch (*s) {
441 case '-':
442 relative = 1;
443 /*FALLTHROUGH*/
444 case '+':
445 s++;
446 }
447 if (is_number(s)) {
448 i = atoi(s);
449 if (relative) {
450 while (retval != -1 && i--) {
451 retval = history(hist, &he, H_NEXT);
452 }
453 if (retval == -1)
454 retval = history(hist, &he, H_LAST);
455 } else {
456 retval = history(hist, &he, H_NEXT_EVENT, i);
457 if (retval == -1) {
458 /*
459 * the notion of first and last is
460 * backwards to that of the history package
461 */
462 retval = history(hist, &he, last ? H_FIRST : H_LAST);
463 }
464 }
465 if (retval == -1)
466 error("history number %s not found (internal error)",
467 str);
468 } else {
469 /*
470 * pattern
471 */
472 retval = history(hist, &he, H_PREV_STR, str);
473 if (retval == -1)
474 error("history pattern not found: %s", str);
475 }
476 return (he.num);
477 }
478
479 int
bindcmd(int argc,char ** argv)480 bindcmd(int argc, char **argv)
481 {
482 int ret;
483 FILE *old;
484 FILE *out;
485
486 if (el == NULL)
487 error("line editing is disabled");
488
489 INTOFF;
490
491 out = out1fp();
492 if (out == NULL)
493 error("Out of space");
494
495 el_get(el, EL_GETFP, 1, &old);
496 el_set(el, EL_SETFP, 1, out);
497
498 ret = el_parse(el, argc, __DECONST(const char **, argv));
499
500 el_set(el, EL_SETFP, 1, old);
501
502 fclose(out);
503
504 if (argc > 1 && argv[1][0] == '-' &&
505 memchr("ve", argv[1][1], 2) != NULL) {
506 Vflag = argv[1][1] == 'v';
507 Eflag = !Vflag;
508 histedit();
509 }
510
511 INTON;
512
513 return ret;
514 }
515
516 /*
517 * Comparator function for qsort(). The use of curpos here is to skip
518 * characters that we already know to compare equal (common prefix).
519 */
520 static int
comparator(const void * a,const void * b,void * thunk)521 comparator(const void *a, const void *b, void *thunk)
522 {
523 size_t curpos = (intptr_t)thunk;
524 return (strcmp(*(char *const *)a + curpos,
525 *(char *const *)b + curpos));
526 }
527
528 /*
529 * This function is passed to libedit's fn_complete2(). The library will
530 * use it instead of its standard function that finds matching files in
531 * current directory. If we're at the start of the line, we want to look
532 * for available commands from all paths in $PATH.
533 */
534 static char
sh_matches(const char * text,int start,int end)535 **sh_matches(const char *text, int start, int end)
536 {
537 char *free_path = NULL, *path;
538 const char *dirname;
539 char **matches = NULL;
540 size_t i = 0, size = 16, uniq;
541 size_t curpos = end - start, lcstring = -1;
542
543 if (start > 0 || memchr("/.~", text[0], 3) != NULL)
544 return (NULL);
545 if ((free_path = path = strdup(pathval())) == NULL)
546 goto out;
547 if ((matches = malloc(size * sizeof(matches[0]))) == NULL)
548 goto out;
549 while ((dirname = strsep(&path, ":")) != NULL) {
550 struct dirent *entry;
551 DIR *dir;
552 int dfd;
553
554 dir = opendir(dirname[0] == '\0' ? "." : dirname);
555 if (dir == NULL)
556 continue;
557 if ((dfd = dirfd(dir)) == -1) {
558 closedir(dir);
559 continue;
560 }
561 while ((entry = readdir(dir)) != NULL) {
562 struct stat statb;
563 char **rmatches;
564
565 if (strncmp(entry->d_name, text, curpos) != 0)
566 continue;
567 if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
568 if (fstatat(dfd, entry->d_name, &statb, 0) == -1)
569 continue;
570 if (!S_ISREG(statb.st_mode))
571 continue;
572 } else if (entry->d_type != DT_REG)
573 continue;
574 matches[++i] = strdup(entry->d_name);
575 if (i < size - 1)
576 continue;
577 size *= 2;
578 rmatches = reallocarray(matches, size, sizeof(matches[0]));
579 if (rmatches == NULL) {
580 closedir(dir);
581 goto out;
582 }
583 matches = rmatches;
584 }
585 closedir(dir);
586 }
587 out:
588 free(free_path);
589 if (i == 0) {
590 free(matches);
591 return (NULL);
592 }
593 uniq = 1;
594 if (i > 1) {
595 qsort_s(matches + 1, i, sizeof(matches[0]), comparator,
596 (void *)(intptr_t)curpos);
597 for (size_t k = 2; k <= i; k++) {
598 const char *l = matches[uniq] + curpos;
599 const char *r = matches[k] + curpos;
600 size_t common = 0;
601
602 while (*l != '\0' && *r != '\0' && *l == *r)
603 (void)l++, r++, common++;
604 if (common < lcstring)
605 lcstring = common;
606 if (*l == *r)
607 free(matches[k]);
608 else
609 matches[++uniq] = matches[k];
610 }
611 }
612 matches[uniq + 1] = NULL;
613 /*
614 * matches[0] is special: it's not a real matching file name but a common
615 * prefix for all matching names. It can't be null, unlike any other
616 * element of the array. When strings matches[0] and matches[1] compare
617 * equal and matches[2] is null that means to libedit that there is only
618 * a single match. It will then replace user input with possibly escaped
619 * string in matches[0] which is the reason to copy the full name of the
620 * only match.
621 */
622 if (uniq == 1)
623 matches[0] = strdup(matches[1]);
624 else if (lcstring != (size_t)-1)
625 matches[0] = strndup(matches[1], curpos + lcstring);
626 else
627 matches[0] = strdup(text);
628 if (matches[0] == NULL) {
629 for (size_t k = 1; k <= uniq; k++)
630 free(matches[k]);
631 free(matches);
632 return (NULL);
633 }
634 return (matches);
635 }
636
637 /*
638 * This is passed to el_set(el, EL_ADDFN, ...) so that it's possible to
639 * bind a key (tab by default) to execute the function.
640 */
641 unsigned char
sh_complete(EditLine * sel,int ch __unused)642 sh_complete(EditLine *sel, int ch __unused)
643 {
644 return (unsigned char)fn_complete2(sel, NULL, sh_matches,
645 L" \t\n\"\\'`@$><=;|&{(", NULL, NULL, (size_t)100,
646 NULL, &((int) {0}), NULL, NULL, FN_QUOTE_MATCH);
647 }
648
649 #else
650 #include "error.h"
651
652 int
histcmd(int argc __unused,char ** argv __unused)653 histcmd(int argc __unused, char **argv __unused)
654 {
655
656 error("not compiled with history support");
657 /*NOTREACHED*/
658 return (0);
659 }
660
661 int
bindcmd(int argc __unused,char ** argv __unused)662 bindcmd(int argc __unused, char **argv __unused)
663 {
664
665 error("not compiled with line editing support");
666 return (0);
667 }
668 #endif
669