PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/src/bin/bash/lib/readline/histexpand.c

http://github.com/Barrett17/Haiku-services-branch
C | 1611 lines | 1224 code | 171 blank | 216 comment | 263 complexity | 705f9bb6f9bebe542376211ef6a84955 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-2.0, LGPL-2.1, BSD-2-Clause, ISC, Apache-2.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, Unlicense, BSD-3-Clause, LGPL-3.0
  1. /* histexpand.c -- history expansion. */
  2. /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
  3. This file contains the GNU History Library (History), a set of
  4. routines for managing the text of previously typed lines.
  5. History is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. History is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with History. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define READLINE_LIBRARY
  17. #if defined (HAVE_CONFIG_H)
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #if defined (HAVE_STDLIB_H)
  22. # include <stdlib.h>
  23. #else
  24. # include "ansi_stdlib.h"
  25. #endif /* HAVE_STDLIB_H */
  26. #if defined (HAVE_UNISTD_H)
  27. # ifndef _MINIX
  28. # include <sys/types.h>
  29. # endif
  30. # include <unistd.h>
  31. #endif
  32. #include "rlmbutil.h"
  33. #include "history.h"
  34. #include "histlib.h"
  35. #include "rlshell.h"
  36. #include "xmalloc.h"
  37. #define HISTORY_WORD_DELIMITERS " \t\n;&()|<>"
  38. #define HISTORY_QUOTE_CHARACTERS "\"'`"
  39. #define slashify_in_quotes "\\`\"$"
  40. typedef int _hist_search_func_t PARAMS((const char *, int));
  41. static char error_pointer;
  42. static char *subst_lhs;
  43. static char *subst_rhs;
  44. static int subst_lhs_len;
  45. static int subst_rhs_len;
  46. static char *get_history_word_specifier PARAMS((char *, char *, int *));
  47. static int history_tokenize_word PARAMS((const char *, int));
  48. static char **history_tokenize_internal PARAMS((const char *, int, int *));
  49. static char *history_substring PARAMS((const char *, int, int));
  50. static void freewords PARAMS((char **, int));
  51. static char *history_find_word PARAMS((char *, int));
  52. static char *quote_breaks PARAMS((char *));
  53. /* Variables exported by this file. */
  54. /* The character that represents the start of a history expansion
  55. request. This is usually `!'. */
  56. char history_expansion_char = '!';
  57. /* The character that invokes word substitution if found at the start of
  58. a line. This is usually `^'. */
  59. char history_subst_char = '^';
  60. /* During tokenization, if this character is seen as the first character
  61. of a word, then it, and all subsequent characters upto a newline are
  62. ignored. For a Bourne shell, this should be '#'. Bash special cases
  63. the interactive comment character to not be a comment delimiter. */
  64. char history_comment_char = '\0';
  65. /* The list of characters which inhibit the expansion of text if found
  66. immediately following history_expansion_char. */
  67. char *history_no_expand_chars = " \t\n\r=";
  68. /* If set to a non-zero value, single quotes inhibit history expansion.
  69. The default is 0. */
  70. int history_quotes_inhibit_expansion = 0;
  71. /* Used to split words by history_tokenize_internal. */
  72. char *history_word_delimiters = HISTORY_WORD_DELIMITERS;
  73. /* If set, this points to a function that is called to verify that a
  74. particular history expansion should be performed. */
  75. rl_linebuf_func_t *history_inhibit_expansion_function;
  76. /* **************************************************************** */
  77. /* */
  78. /* History Expansion */
  79. /* */
  80. /* **************************************************************** */
  81. /* Hairy history expansion on text, not tokens. This is of general
  82. use, and thus belongs in this library. */
  83. /* The last string searched for by a !?string? search. */
  84. static char *search_string;
  85. /* The last string matched by a !?string? search. */
  86. static char *search_match;
  87. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  88. point to after the event specifier. Just a pointer to the history
  89. line is returned; NULL is returned in the event of a bad specifier.
  90. You pass STRING with *INDEX equal to the history_expansion_char that
  91. begins this specification.
  92. DELIMITING_QUOTE is a character that is allowed to end the string
  93. specification for what to search for in addition to the normal
  94. characters `:', ` ', `\t', `\n', and sometimes `?'.
  95. So you might call this function like:
  96. line = get_history_event ("!echo:p", &index, 0); */
  97. char *
  98. get_history_event (string, caller_index, delimiting_quote)
  99. const char *string;
  100. int *caller_index;
  101. int delimiting_quote;
  102. {
  103. register int i;
  104. register char c;
  105. HIST_ENTRY *entry;
  106. int which, sign, local_index, substring_okay;
  107. _hist_search_func_t *search_func;
  108. char *temp;
  109. /* The event can be specified in a number of ways.
  110. !! the previous command
  111. !n command line N
  112. !-n current command-line minus N
  113. !str the most recent command starting with STR
  114. !?str[?]
  115. the most recent command containing STR
  116. All values N are determined via HISTORY_BASE. */
  117. i = *caller_index;
  118. if (string[i] != history_expansion_char)
  119. return ((char *)NULL);
  120. /* Move on to the specification. */
  121. i++;
  122. sign = 1;
  123. substring_okay = 0;
  124. #define RETURN_ENTRY(e, w) \
  125. return ((e = history_get (w)) ? e->line : (char *)NULL)
  126. /* Handle !! case. */
  127. if (string[i] == history_expansion_char)
  128. {
  129. i++;
  130. which = history_base + (history_length - 1);
  131. *caller_index = i;
  132. RETURN_ENTRY (entry, which);
  133. }
  134. /* Hack case of numeric line specification. */
  135. if (string[i] == '-')
  136. {
  137. sign = -1;
  138. i++;
  139. }
  140. if (_rl_digit_p (string[i]))
  141. {
  142. /* Get the extent of the digits and compute the value. */
  143. for (which = 0; _rl_digit_p (string[i]); i++)
  144. which = (which * 10) + _rl_digit_value (string[i]);
  145. *caller_index = i;
  146. if (sign < 0)
  147. which = (history_length + history_base) - which;
  148. RETURN_ENTRY (entry, which);
  149. }
  150. /* This must be something to search for. If the spec begins with
  151. a '?', then the string may be anywhere on the line. Otherwise,
  152. the string must be found at the start of a line. */
  153. if (string[i] == '?')
  154. {
  155. substring_okay++;
  156. i++;
  157. }
  158. /* Only a closing `?' or a newline delimit a substring search string. */
  159. for (local_index = i; c = string[i]; i++)
  160. {
  161. #if defined (HANDLE_MULTIBYTE)
  162. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  163. {
  164. int v;
  165. mbstate_t ps;
  166. memset (&ps, 0, sizeof (mbstate_t));
  167. /* These produce warnings because we're passing a const string to a
  168. function that takes a non-const string. */
  169. _rl_adjust_point ((char *)string, i, &ps);
  170. if ((v = _rl_get_char_len ((char *)string + i, &ps)) > 1)
  171. {
  172. i += v - 1;
  173. continue;
  174. }
  175. }
  176. #endif /* HANDLE_MULTIBYTE */
  177. if ((!substring_okay && (whitespace (c) || c == ':' ||
  178. (history_search_delimiter_chars && member (c, history_search_delimiter_chars)) ||
  179. string[i] == delimiting_quote)) ||
  180. string[i] == '\n' ||
  181. (substring_okay && string[i] == '?'))
  182. break;
  183. }
  184. which = i - local_index;
  185. temp = (char *)xmalloc (1 + which);
  186. if (which)
  187. strncpy (temp, string + local_index, which);
  188. temp[which] = '\0';
  189. if (substring_okay && string[i] == '?')
  190. i++;
  191. *caller_index = i;
  192. #define FAIL_SEARCH() \
  193. do { \
  194. history_offset = history_length; free (temp) ; return (char *)NULL; \
  195. } while (0)
  196. /* If there is no search string, try to use the previous search string,
  197. if one exists. If not, fail immediately. */
  198. if (*temp == '\0' && substring_okay)
  199. {
  200. if (search_string)
  201. {
  202. free (temp);
  203. temp = savestring (search_string);
  204. }
  205. else
  206. FAIL_SEARCH ();
  207. }
  208. search_func = substring_okay ? history_search : history_search_prefix;
  209. while (1)
  210. {
  211. local_index = (*search_func) (temp, -1);
  212. if (local_index < 0)
  213. FAIL_SEARCH ();
  214. if (local_index == 0 || substring_okay)
  215. {
  216. entry = current_history ();
  217. history_offset = history_length;
  218. /* If this was a substring search, then remember the
  219. string that we matched for word substitution. */
  220. if (substring_okay)
  221. {
  222. FREE (search_string);
  223. search_string = temp;
  224. FREE (search_match);
  225. search_match = history_find_word (entry->line, local_index);
  226. }
  227. else
  228. free (temp);
  229. return (entry->line);
  230. }
  231. if (history_offset)
  232. history_offset--;
  233. else
  234. FAIL_SEARCH ();
  235. }
  236. #undef FAIL_SEARCH
  237. #undef RETURN_ENTRY
  238. }
  239. /* Function for extracting single-quoted strings. Used for inhibiting
  240. history expansion within single quotes. */
  241. /* Extract the contents of STRING as if it is enclosed in single quotes.
  242. SINDEX, when passed in, is the offset of the character immediately
  243. following the opening single quote; on exit, SINDEX is left pointing
  244. to the closing single quote. */
  245. static void
  246. hist_string_extract_single_quoted (string, sindex)
  247. char *string;
  248. int *sindex;
  249. {
  250. register int i;
  251. for (i = *sindex; string[i] && string[i] != '\''; i++)
  252. ;
  253. *sindex = i;
  254. }
  255. static char *
  256. quote_breaks (s)
  257. char *s;
  258. {
  259. register char *p, *r;
  260. char *ret;
  261. int len = 3;
  262. for (p = s; p && *p; p++, len++)
  263. {
  264. if (*p == '\'')
  265. len += 3;
  266. else if (whitespace (*p) || *p == '\n')
  267. len += 2;
  268. }
  269. r = ret = (char *)xmalloc (len);
  270. *r++ = '\'';
  271. for (p = s; p && *p; )
  272. {
  273. if (*p == '\'')
  274. {
  275. *r++ = '\'';
  276. *r++ = '\\';
  277. *r++ = '\'';
  278. *r++ = '\'';
  279. p++;
  280. }
  281. else if (whitespace (*p) || *p == '\n')
  282. {
  283. *r++ = '\'';
  284. *r++ = *p++;
  285. *r++ = '\'';
  286. }
  287. else
  288. *r++ = *p++;
  289. }
  290. *r++ = '\'';
  291. *r = '\0';
  292. return ret;
  293. }
  294. static char *
  295. hist_error(s, start, current, errtype)
  296. char *s;
  297. int start, current, errtype;
  298. {
  299. char *temp;
  300. const char *emsg;
  301. int ll, elen;
  302. ll = current - start;
  303. switch (errtype)
  304. {
  305. case EVENT_NOT_FOUND:
  306. emsg = "event not found";
  307. elen = 15;
  308. break;
  309. case BAD_WORD_SPEC:
  310. emsg = "bad word specifier";
  311. elen = 18;
  312. break;
  313. case SUBST_FAILED:
  314. emsg = "substitution failed";
  315. elen = 19;
  316. break;
  317. case BAD_MODIFIER:
  318. emsg = "unrecognized history modifier";
  319. elen = 29;
  320. break;
  321. case NO_PREV_SUBST:
  322. emsg = "no previous substitution";
  323. elen = 24;
  324. break;
  325. default:
  326. emsg = "unknown expansion error";
  327. elen = 23;
  328. break;
  329. }
  330. temp = (char *)xmalloc (ll + elen + 3);
  331. strncpy (temp, s + start, ll);
  332. temp[ll] = ':';
  333. temp[ll + 1] = ' ';
  334. strcpy (temp + ll + 2, emsg);
  335. return (temp);
  336. }
  337. /* Get a history substitution string from STR starting at *IPTR
  338. and return it. The length is returned in LENPTR.
  339. A backslash can quote the delimiter. If the string is the
  340. empty string, the previous pattern is used. If there is
  341. no previous pattern for the lhs, the last history search
  342. string is used.
  343. If IS_RHS is 1, we ignore empty strings and set the pattern
  344. to "" anyway. subst_lhs is not changed if the lhs is empty;
  345. subst_rhs is allowed to be set to the empty string. */
  346. static char *
  347. get_subst_pattern (str, iptr, delimiter, is_rhs, lenptr)
  348. char *str;
  349. int *iptr, delimiter, is_rhs, *lenptr;
  350. {
  351. register int si, i, j, k;
  352. char *s;
  353. #if defined (HANDLE_MULTIBYTE)
  354. mbstate_t ps;
  355. #endif
  356. s = (char *)NULL;
  357. i = *iptr;
  358. #if defined (HANDLE_MULTIBYTE)
  359. memset (&ps, 0, sizeof (mbstate_t));
  360. _rl_adjust_point (str, i, &ps);
  361. #endif
  362. for (si = i; str[si] && str[si] != delimiter; si++)
  363. #if defined (HANDLE_MULTIBYTE)
  364. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  365. {
  366. int v;
  367. if ((v = _rl_get_char_len (str + si, &ps)) > 1)
  368. si += v - 1;
  369. else if (str[si] == '\\' && str[si + 1] == delimiter)
  370. si++;
  371. }
  372. else
  373. #endif /* HANDLE_MULTIBYTE */
  374. if (str[si] == '\\' && str[si + 1] == delimiter)
  375. si++;
  376. if (si > i || is_rhs)
  377. {
  378. s = (char *)xmalloc (si - i + 1);
  379. for (j = 0, k = i; k < si; j++, k++)
  380. {
  381. /* Remove a backslash quoting the search string delimiter. */
  382. if (str[k] == '\\' && str[k + 1] == delimiter)
  383. k++;
  384. s[j] = str[k];
  385. }
  386. s[j] = '\0';
  387. if (lenptr)
  388. *lenptr = j;
  389. }
  390. i = si;
  391. if (str[i])
  392. i++;
  393. *iptr = i;
  394. return s;
  395. }
  396. static void
  397. postproc_subst_rhs ()
  398. {
  399. char *new;
  400. int i, j, new_size;
  401. new = (char *)xmalloc (new_size = subst_rhs_len + subst_lhs_len);
  402. for (i = j = 0; i < subst_rhs_len; i++)
  403. {
  404. if (subst_rhs[i] == '&')
  405. {
  406. if (j + subst_lhs_len >= new_size)
  407. new = (char *)xrealloc (new, (new_size = new_size * 2 + subst_lhs_len));
  408. strcpy (new + j, subst_lhs);
  409. j += subst_lhs_len;
  410. }
  411. else
  412. {
  413. /* a single backslash protects the `&' from lhs interpolation */
  414. if (subst_rhs[i] == '\\' && subst_rhs[i + 1] == '&')
  415. i++;
  416. if (j >= new_size)
  417. new = (char *)xrealloc (new, new_size *= 2);
  418. new[j++] = subst_rhs[i];
  419. }
  420. }
  421. new[j] = '\0';
  422. free (subst_rhs);
  423. subst_rhs = new;
  424. subst_rhs_len = j;
  425. }
  426. /* Expand the bulk of a history specifier starting at STRING[START].
  427. Returns 0 if everything is OK, -1 if an error occurred, and 1
  428. if the `p' modifier was supplied and the caller should just print
  429. the returned string. Returns the new index into string in
  430. *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
  431. static int
  432. history_expand_internal (string, start, end_index_ptr, ret_string, current_line)
  433. char *string;
  434. int start, *end_index_ptr;
  435. char **ret_string;
  436. char *current_line; /* for !# */
  437. {
  438. int i, n, starting_index;
  439. int substitute_globally, subst_bywords, want_quotes, print_only;
  440. char *event, *temp, *result, *tstr, *t, c, *word_spec;
  441. int result_len;
  442. #if defined (HANDLE_MULTIBYTE)
  443. mbstate_t ps;
  444. memset (&ps, 0, sizeof (mbstate_t));
  445. #endif
  446. result = (char *)xmalloc (result_len = 128);
  447. i = start;
  448. /* If it is followed by something that starts a word specifier,
  449. then !! is implied as the event specifier. */
  450. if (member (string[i + 1], ":$*%^"))
  451. {
  452. char fake_s[3];
  453. int fake_i = 0;
  454. i++;
  455. fake_s[0] = fake_s[1] = history_expansion_char;
  456. fake_s[2] = '\0';
  457. event = get_history_event (fake_s, &fake_i, 0);
  458. }
  459. else if (string[i + 1] == '#')
  460. {
  461. i += 2;
  462. event = current_line;
  463. }
  464. else
  465. {
  466. int quoted_search_delimiter = 0;
  467. /* If the character before this `!' is a double or single
  468. quote, then this expansion takes place inside of the
  469. quoted string. If we have to search for some text ("!foo"),
  470. allow the delimiter to end the search string. */
  471. #if defined (HANDLE_MULTIBYTE)
  472. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  473. {
  474. int ch, l;
  475. l = _rl_find_prev_mbchar (string, i, MB_FIND_ANY);
  476. ch = string[l];
  477. /* XXX - original patch had i - 1 ??? If i == 0 it would fail. */
  478. if (i && (ch == '\'' || ch == '"'))
  479. quoted_search_delimiter = ch;
  480. }
  481. else
  482. #endif /* HANDLE_MULTIBYTE */
  483. if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  484. quoted_search_delimiter = string[i - 1];
  485. event = get_history_event (string, &i, quoted_search_delimiter);
  486. }
  487. if (event == 0)
  488. {
  489. *ret_string = hist_error (string, start, i, EVENT_NOT_FOUND);
  490. free (result);
  491. return (-1);
  492. }
  493. /* If a word specifier is found, then do what that requires. */
  494. starting_index = i;
  495. word_spec = get_history_word_specifier (string, event, &i);
  496. /* There is no such thing as a `malformed word specifier'. However,
  497. it is possible for a specifier that has no match. In that case,
  498. we complain. */
  499. if (word_spec == (char *)&error_pointer)
  500. {
  501. *ret_string = hist_error (string, starting_index, i, BAD_WORD_SPEC);
  502. free (result);
  503. return (-1);
  504. }
  505. /* If no word specifier, than the thing of interest was the event. */
  506. temp = word_spec ? savestring (word_spec) : savestring (event);
  507. FREE (word_spec);
  508. /* Perhaps there are other modifiers involved. Do what they say. */
  509. want_quotes = substitute_globally = subst_bywords = print_only = 0;
  510. starting_index = i;
  511. while (string[i] == ':')
  512. {
  513. c = string[i + 1];
  514. if (c == 'g' || c == 'a')
  515. {
  516. substitute_globally = 1;
  517. i++;
  518. c = string[i + 1];
  519. }
  520. else if (c == 'G')
  521. {
  522. subst_bywords = 1;
  523. i++;
  524. c = string[i + 1];
  525. }
  526. switch (c)
  527. {
  528. default:
  529. *ret_string = hist_error (string, i+1, i+2, BAD_MODIFIER);
  530. free (result);
  531. free (temp);
  532. return -1;
  533. case 'q':
  534. want_quotes = 'q';
  535. break;
  536. case 'x':
  537. want_quotes = 'x';
  538. break;
  539. /* :p means make this the last executed line. So we
  540. return an error state after adding this line to the
  541. history. */
  542. case 'p':
  543. print_only++;
  544. break;
  545. /* :t discards all but the last part of the pathname. */
  546. case 't':
  547. tstr = strrchr (temp, '/');
  548. if (tstr)
  549. {
  550. tstr++;
  551. t = savestring (tstr);
  552. free (temp);
  553. temp = t;
  554. }
  555. break;
  556. /* :h discards the last part of a pathname. */
  557. case 'h':
  558. tstr = strrchr (temp, '/');
  559. if (tstr)
  560. *tstr = '\0';
  561. break;
  562. /* :r discards the suffix. */
  563. case 'r':
  564. tstr = strrchr (temp, '.');
  565. if (tstr)
  566. *tstr = '\0';
  567. break;
  568. /* :e discards everything but the suffix. */
  569. case 'e':
  570. tstr = strrchr (temp, '.');
  571. if (tstr)
  572. {
  573. t = savestring (tstr);
  574. free (temp);
  575. temp = t;
  576. }
  577. break;
  578. /* :s/this/that substitutes `that' for the first
  579. occurrence of `this'. :gs/this/that substitutes `that'
  580. for each occurrence of `this'. :& repeats the last
  581. substitution. :g& repeats the last substitution
  582. globally. */
  583. case '&':
  584. case 's':
  585. {
  586. char *new_event;
  587. int delimiter, failed, si, l_temp, ws, we;
  588. if (c == 's')
  589. {
  590. if (i + 2 < (int)strlen (string))
  591. {
  592. #if defined (HANDLE_MULTIBYTE)
  593. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  594. {
  595. _rl_adjust_point (string, i + 2, &ps);
  596. if (_rl_get_char_len (string + i + 2, &ps) > 1)
  597. delimiter = 0;
  598. else
  599. delimiter = string[i + 2];
  600. }
  601. else
  602. #endif /* HANDLE_MULTIBYTE */
  603. delimiter = string[i + 2];
  604. }
  605. else
  606. break; /* no search delimiter */
  607. i += 3;
  608. t = get_subst_pattern (string, &i, delimiter, 0, &subst_lhs_len);
  609. /* An empty substitution lhs with no previous substitution
  610. uses the last search string as the lhs. */
  611. if (t)
  612. {
  613. FREE (subst_lhs);
  614. subst_lhs = t;
  615. }
  616. else if (!subst_lhs)
  617. {
  618. if (search_string && *search_string)
  619. {
  620. subst_lhs = savestring (search_string);
  621. subst_lhs_len = strlen (subst_lhs);
  622. }
  623. else
  624. {
  625. subst_lhs = (char *) NULL;
  626. subst_lhs_len = 0;
  627. }
  628. }
  629. FREE (subst_rhs);
  630. subst_rhs = get_subst_pattern (string, &i, delimiter, 1, &subst_rhs_len);
  631. /* If `&' appears in the rhs, it's supposed to be replaced
  632. with the lhs. */
  633. if (member ('&', subst_rhs))
  634. postproc_subst_rhs ();
  635. }
  636. else
  637. i += 2;
  638. /* If there is no lhs, the substitution can't succeed. */
  639. if (subst_lhs_len == 0)
  640. {
  641. *ret_string = hist_error (string, starting_index, i, NO_PREV_SUBST);
  642. free (result);
  643. free (temp);
  644. return -1;
  645. }
  646. l_temp = strlen (temp);
  647. /* Ignore impossible cases. */
  648. if (subst_lhs_len > l_temp)
  649. {
  650. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  651. free (result);
  652. free (temp);
  653. return (-1);
  654. }
  655. /* Find the first occurrence of THIS in TEMP. */
  656. /* Substitute SUBST_RHS for SUBST_LHS in TEMP. There are three
  657. cases to consider:
  658. 1. substitute_globally == subst_bywords == 0
  659. 2. substitute_globally == 1 && subst_bywords == 0
  660. 3. substitute_globally == 0 && subst_bywords == 1
  661. In the first case, we substitute for the first occurrence only.
  662. In the second case, we substitute for every occurrence.
  663. In the third case, we tokenize into words and substitute the
  664. first occurrence of each word. */
  665. si = we = 0;
  666. for (failed = 1; (si + subst_lhs_len) <= l_temp; si++)
  667. {
  668. /* First skip whitespace and find word boundaries if
  669. we're past the end of the word boundary we found
  670. the last time. */
  671. if (subst_bywords && si > we)
  672. {
  673. for (; temp[si] && whitespace (temp[si]); si++)
  674. ;
  675. ws = si;
  676. we = history_tokenize_word (temp, si);
  677. }
  678. if (STREQN (temp+si, subst_lhs, subst_lhs_len))
  679. {
  680. int len = subst_rhs_len - subst_lhs_len + l_temp;
  681. new_event = (char *)xmalloc (1 + len);
  682. strncpy (new_event, temp, si);
  683. strncpy (new_event + si, subst_rhs, subst_rhs_len);
  684. strncpy (new_event + si + subst_rhs_len,
  685. temp + si + subst_lhs_len,
  686. l_temp - (si + subst_lhs_len));
  687. new_event[len] = '\0';
  688. free (temp);
  689. temp = new_event;
  690. failed = 0;
  691. if (substitute_globally)
  692. {
  693. /* Reported to fix a bug that causes it to skip every
  694. other match when matching a single character. Was
  695. si += subst_rhs_len previously. */
  696. si += subst_rhs_len - 1;
  697. l_temp = strlen (temp);
  698. substitute_globally++;
  699. continue;
  700. }
  701. else if (subst_bywords)
  702. {
  703. si = we;
  704. l_temp = strlen (temp);
  705. continue;
  706. }
  707. else
  708. break;
  709. }
  710. }
  711. if (substitute_globally > 1)
  712. {
  713. substitute_globally = 0;
  714. continue; /* don't want to increment i */
  715. }
  716. if (failed == 0)
  717. continue; /* don't want to increment i */
  718. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  719. free (result);
  720. free (temp);
  721. return (-1);
  722. }
  723. }
  724. i += 2;
  725. }
  726. /* Done with modfiers. */
  727. /* Believe it or not, we have to back the pointer up by one. */
  728. --i;
  729. if (want_quotes)
  730. {
  731. char *x;
  732. if (want_quotes == 'q')
  733. x = sh_single_quote (temp);
  734. else if (want_quotes == 'x')
  735. x = quote_breaks (temp);
  736. else
  737. x = savestring (temp);
  738. free (temp);
  739. temp = x;
  740. }
  741. n = strlen (temp);
  742. if (n >= result_len)
  743. result = (char *)xrealloc (result, n + 2);
  744. strcpy (result, temp);
  745. free (temp);
  746. *end_index_ptr = i;
  747. *ret_string = result;
  748. return (print_only);
  749. }
  750. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  751. to a string. Returns:
  752. -1) If there was an error in expansion.
  753. 0) If no expansions took place (or, if the only change in
  754. the text was the de-slashifying of the history expansion
  755. character)
  756. 1) If expansions did take place
  757. 2) If the `p' modifier was given and the caller should print the result
  758. If an error ocurred in expansion, then OUTPUT contains a descriptive
  759. error message. */
  760. #define ADD_STRING(s) \
  761. do \
  762. { \
  763. int sl = strlen (s); \
  764. j += sl; \
  765. if (j >= result_len) \
  766. { \
  767. while (j >= result_len) \
  768. result_len += 128; \
  769. result = (char *)xrealloc (result, result_len); \
  770. } \
  771. strcpy (result + j - sl, s); \
  772. } \
  773. while (0)
  774. #define ADD_CHAR(c) \
  775. do \
  776. { \
  777. if (j >= result_len - 1) \
  778. result = (char *)xrealloc (result, result_len += 64); \
  779. result[j++] = c; \
  780. result[j] = '\0'; \
  781. } \
  782. while (0)
  783. int
  784. history_expand (hstring, output)
  785. char *hstring;
  786. char **output;
  787. {
  788. register int j;
  789. int i, r, l, passc, cc, modified, eindex, only_printing, dquote;
  790. char *string;
  791. /* The output string, and its length. */
  792. int result_len;
  793. char *result;
  794. #if defined (HANDLE_MULTIBYTE)
  795. char mb[MB_LEN_MAX];
  796. mbstate_t ps;
  797. #endif
  798. /* Used when adding the string. */
  799. char *temp;
  800. if (output == 0)
  801. return 0;
  802. /* Setting the history expansion character to 0 inhibits all
  803. history expansion. */
  804. if (history_expansion_char == 0)
  805. {
  806. *output = savestring (hstring);
  807. return (0);
  808. }
  809. /* Prepare the buffer for printing error messages. */
  810. result = (char *)xmalloc (result_len = 256);
  811. result[0] = '\0';
  812. only_printing = modified = 0;
  813. l = strlen (hstring);
  814. /* Grovel the string. Only backslash and single quotes can quote the
  815. history escape character. We also handle arg specifiers. */
  816. /* Before we grovel forever, see if the history_expansion_char appears
  817. anywhere within the text. */
  818. /* The quick substitution character is a history expansion all right. That
  819. is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  820. that is the substitution that we do. */
  821. if (hstring[0] == history_subst_char)
  822. {
  823. string = (char *)xmalloc (l + 5);
  824. string[0] = string[1] = history_expansion_char;
  825. string[2] = ':';
  826. string[3] = 's';
  827. strcpy (string + 4, hstring);
  828. l += 4;
  829. }
  830. else
  831. {
  832. #if defined (HANDLE_MULTIBYTE)
  833. memset (&ps, 0, sizeof (mbstate_t));
  834. #endif
  835. string = hstring;
  836. /* If not quick substitution, still maybe have to do expansion. */
  837. /* `!' followed by one of the characters in history_no_expand_chars
  838. is NOT an expansion. */
  839. for (i = dquote = 0; string[i]; i++)
  840. {
  841. #if defined (HANDLE_MULTIBYTE)
  842. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  843. {
  844. int v;
  845. v = _rl_get_char_len (string + i, &ps);
  846. if (v > 1)
  847. {
  848. i += v - 1;
  849. continue;
  850. }
  851. }
  852. #endif /* HANDLE_MULTIBYTE */
  853. cc = string[i + 1];
  854. /* The history_comment_char, if set, appearing at the beginning
  855. of a word signifies that the rest of the line should not have
  856. history expansion performed on it.
  857. Skip the rest of the line and break out of the loop. */
  858. if (history_comment_char && string[i] == history_comment_char &&
  859. (i == 0 || member (string[i - 1], history_word_delimiters)))
  860. {
  861. while (string[i])
  862. i++;
  863. break;
  864. }
  865. else if (string[i] == history_expansion_char)
  866. {
  867. if (cc == 0 || member (cc, history_no_expand_chars))
  868. continue;
  869. /* If the calling application has set
  870. history_inhibit_expansion_function to a function that checks
  871. for special cases that should not be history expanded,
  872. call the function and skip the expansion if it returns a
  873. non-zero value. */
  874. else if (history_inhibit_expansion_function &&
  875. (*history_inhibit_expansion_function) (string, i))
  876. continue;
  877. else
  878. break;
  879. }
  880. /* Shell-like quoting: allow backslashes to quote double quotes
  881. inside a double-quoted string. */
  882. else if (dquote && string[i] == '\\' && cc == '"')
  883. i++;
  884. /* More shell-like quoting: if we're paying attention to single
  885. quotes and letting them quote the history expansion character,
  886. then we need to pay attention to double quotes, because single
  887. quotes are not special inside double-quoted strings. */
  888. else if (history_quotes_inhibit_expansion && string[i] == '"')
  889. {
  890. dquote = 1 - dquote;
  891. }
  892. else if (dquote == 0 && history_quotes_inhibit_expansion && string[i] == '\'')
  893. {
  894. /* If this is bash, single quotes inhibit history expansion. */
  895. i++;
  896. hist_string_extract_single_quoted (string, &i);
  897. }
  898. else if (history_quotes_inhibit_expansion && string[i] == '\\')
  899. {
  900. /* If this is bash, allow backslashes to quote single
  901. quotes and the history expansion character. */
  902. if (cc == '\'' || cc == history_expansion_char)
  903. i++;
  904. }
  905. }
  906. if (string[i] != history_expansion_char)
  907. {
  908. free (result);
  909. *output = savestring (string);
  910. return (0);
  911. }
  912. }
  913. /* Extract and perform the substitution. */
  914. for (passc = dquote = i = j = 0; i < l; i++)
  915. {
  916. int tchar = string[i];
  917. if (passc)
  918. {
  919. passc = 0;
  920. ADD_CHAR (tchar);
  921. continue;
  922. }
  923. #if defined (HANDLE_MULTIBYTE)
  924. if (MB_CUR_MAX > 1 && rl_byte_oriented == 0)
  925. {
  926. int k, c;
  927. c = tchar;
  928. memset (mb, 0, sizeof (mb));
  929. for (k = 0; k < MB_LEN_MAX; k++)
  930. {
  931. mb[k] = (char)c;
  932. memset (&ps, 0, sizeof (mbstate_t));
  933. if (_rl_get_char_len (mb, &ps) == -2)
  934. c = string[++i];
  935. else
  936. break;
  937. }
  938. if (strlen (mb) > 1)
  939. {
  940. ADD_STRING (mb);
  941. break;
  942. }
  943. }
  944. #endif /* HANDLE_MULTIBYTE */
  945. if (tchar == history_expansion_char)
  946. tchar = -3;
  947. else if (tchar == history_comment_char)
  948. tchar = -2;
  949. switch (tchar)
  950. {
  951. default:
  952. ADD_CHAR (string[i]);
  953. break;
  954. case '\\':
  955. passc++;
  956. ADD_CHAR (tchar);
  957. break;
  958. case '"':
  959. dquote = 1 - dquote;
  960. ADD_CHAR (tchar);
  961. break;
  962. case '\'':
  963. {
  964. /* If history_quotes_inhibit_expansion is set, single quotes
  965. inhibit history expansion. */
  966. if (dquote == 0 && history_quotes_inhibit_expansion)
  967. {
  968. int quote, slen;
  969. quote = i++;
  970. hist_string_extract_single_quoted (string, &i);
  971. slen = i - quote + 2;
  972. temp = (char *)xmalloc (slen);
  973. strncpy (temp, string + quote, slen);
  974. temp[slen - 1] = '\0';
  975. ADD_STRING (temp);
  976. free (temp);
  977. }
  978. else
  979. ADD_CHAR (string[i]);
  980. break;
  981. }
  982. case -2: /* history_comment_char */
  983. if (i == 0 || member (string[i - 1], history_word_delimiters))
  984. {
  985. temp = (char *)xmalloc (l - i + 1);
  986. strcpy (temp, string + i);
  987. ADD_STRING (temp);
  988. free (temp);
  989. i = l;
  990. }
  991. else
  992. ADD_CHAR (string[i]);
  993. break;
  994. case -3: /* history_expansion_char */
  995. cc = string[i + 1];
  996. /* If the history_expansion_char is followed by one of the
  997. characters in history_no_expand_chars, then it is not a
  998. candidate for expansion of any kind. */
  999. if (cc == 0 || member (cc, history_no_expand_chars) ||
  1000. (history_inhibit_expansion_function && (*history_inhibit_expansion_function) (string, i)))
  1001. {
  1002. ADD_CHAR (string[i]);
  1003. break;
  1004. }
  1005. #if defined (NO_BANG_HASH_MODIFIERS)
  1006. /* There is something that is listed as a `word specifier' in csh
  1007. documentation which means `the expanded text to this point'.
  1008. That is not a word specifier, it is an event specifier. If we
  1009. don't want to allow modifiers with `!#', just stick the current
  1010. output line in again. */
  1011. if (cc == '#')
  1012. {
  1013. if (result)
  1014. {
  1015. temp = (char *)xmalloc (1 + strlen (result));
  1016. strcpy (temp, result);
  1017. ADD_STRING (temp);
  1018. free (temp);
  1019. }
  1020. i++;
  1021. break;
  1022. }
  1023. #endif
  1024. r = history_expand_internal (string, i, &eindex, &temp, result);
  1025. if (r < 0)
  1026. {
  1027. *output = temp;
  1028. free (result);
  1029. if (string != hstring)
  1030. free (string);
  1031. return -1;
  1032. }
  1033. else
  1034. {
  1035. if (temp)
  1036. {
  1037. modified++;
  1038. if (*temp)
  1039. ADD_STRING (temp);
  1040. free (temp);
  1041. }
  1042. only_printing = r == 1;
  1043. i = eindex;
  1044. }
  1045. break;
  1046. }
  1047. }
  1048. *output = result;
  1049. if (string != hstring)
  1050. free (string);
  1051. if (only_printing)
  1052. {
  1053. #if 0
  1054. add_history (result);
  1055. #endif
  1056. return (2);
  1057. }
  1058. return (modified != 0);
  1059. }
  1060. /* Return a consed string which is the word specified in SPEC, and found
  1061. in FROM. NULL is returned if there is no spec. The address of
  1062. ERROR_POINTER is returned if the word specified cannot be found.
  1063. CALLER_INDEX is the offset in SPEC to start looking; it is updated
  1064. to point to just after the last character parsed. */
  1065. static char *
  1066. get_history_word_specifier (spec, from, caller_index)
  1067. char *spec, *from;
  1068. int *caller_index;
  1069. {
  1070. register int i = *caller_index;
  1071. int first, last;
  1072. int expecting_word_spec = 0;
  1073. char *result;
  1074. /* The range of words to return doesn't exist yet. */
  1075. first = last = 0;
  1076. result = (char *)NULL;
  1077. /* If we found a colon, then this *must* be a word specification. If
  1078. it isn't, then it is an error. */
  1079. if (spec[i] == ':')
  1080. {
  1081. i++;
  1082. expecting_word_spec++;
  1083. }
  1084. /* Handle special cases first. */
  1085. /* `%' is the word last searched for. */
  1086. if (spec[i] == '%')
  1087. {
  1088. *caller_index = i + 1;
  1089. return (search_match ? savestring (search_match) : savestring (""));
  1090. }
  1091. /* `*' matches all of the arguments, but not the command. */
  1092. if (spec[i] == '*')
  1093. {
  1094. *caller_index = i + 1;
  1095. result = history_arg_extract (1, '$', from);
  1096. return (result ? result : savestring (""));
  1097. }
  1098. /* `$' is last arg. */
  1099. if (spec[i] == '$')
  1100. {
  1101. *caller_index = i + 1;
  1102. return (history_arg_extract ('$', '$', from));
  1103. }
  1104. /* Try to get FIRST and LAST figured out. */
  1105. if (spec[i] == '-')
  1106. first = 0;
  1107. else if (spec[i] == '^')
  1108. {
  1109. first = 1;
  1110. i++;
  1111. }
  1112. else if (_rl_digit_p (spec[i]) && expecting_word_spec)
  1113. {
  1114. for (first = 0; _rl_digit_p (spec[i]); i++)
  1115. first = (first * 10) + _rl_digit_value (spec[i]);
  1116. }
  1117. else
  1118. return ((char *)NULL); /* no valid `first' for word specifier */
  1119. if (spec[i] == '^' || spec[i] == '*')
  1120. {
  1121. last = (spec[i] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
  1122. i++;
  1123. }
  1124. else if (spec[i] != '-')
  1125. last = first;
  1126. else
  1127. {
  1128. i++;
  1129. if (_rl_digit_p (spec[i]))
  1130. {
  1131. for (last = 0; _rl_digit_p (spec[i]); i++)
  1132. last = (last * 10) + _rl_digit_value (spec[i]);
  1133. }
  1134. else if (spec[i] == '$')
  1135. {
  1136. i++;
  1137. last = '$';
  1138. }
  1139. #if 0
  1140. else if (!spec[i] || spec[i] == ':')
  1141. /* check against `:' because there could be a modifier separator */
  1142. #else
  1143. else
  1144. /* csh seems to allow anything to terminate the word spec here,
  1145. leaving it as an abbreviation. */
  1146. #endif
  1147. last = -1; /* x- abbreviates x-$ omitting word `$' */
  1148. }
  1149. *caller_index = i;
  1150. if (last >= first || last == '$' || last < 0)
  1151. result = history_arg_extract (first, last, from);
  1152. return (result ? result : (char *)&error_pointer);
  1153. }
  1154. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1155. The args are taken from STRING. If either FIRST or LAST is < 0,
  1156. then make that arg count from the right (subtract from the number of
  1157. tokens, so that FIRST = -1 means the next to last token on the line).
  1158. If LAST is `$' the last arg from STRING is used. */
  1159. char *
  1160. history_arg_extract (first, last, string)
  1161. int first, last;
  1162. const char *string;
  1163. {
  1164. register int i, len;
  1165. char *result;
  1166. int size, offset;
  1167. char **list;
  1168. /* XXX - think about making history_tokenize return a struct array,
  1169. each struct in array being a string and a length to avoid the
  1170. calls to strlen below. */
  1171. if ((list = history_tokenize (string)) == NULL)
  1172. return ((char *)NULL);
  1173. for (len = 0; list[len]; len++)
  1174. ;
  1175. if (last < 0)
  1176. last = len + last - 1;
  1177. if (first < 0)
  1178. first = len + first - 1;
  1179. if (last == '$')
  1180. last = len - 1;
  1181. if (first == '$')
  1182. first = len - 1;
  1183. last++;
  1184. if (first >= len || last > len || first < 0 || last < 0 || first > last)
  1185. result = ((char *)NULL);
  1186. else
  1187. {
  1188. for (size = 0, i = first; i < last; i++)
  1189. size += strlen (list[i]) + 1;
  1190. result = (char *)xmalloc (size + 1);
  1191. result[0] = '\0';
  1192. for (i = first, offset = 0; i < last; i++)
  1193. {
  1194. strcpy (result + offset, list[i]);
  1195. offset += strlen (list[i]);
  1196. if (i + 1 < last)
  1197. {
  1198. result[offset++] = ' ';
  1199. result[offset] = 0;
  1200. }
  1201. }
  1202. }
  1203. for (i = 0; i < len; i++)
  1204. free (list[i]);
  1205. free (list);
  1206. return (result);
  1207. }
  1208. static int
  1209. history_tokenize_word (string, ind)
  1210. const char *string;
  1211. int ind;
  1212. {
  1213. register int i;
  1214. int delimiter;
  1215. i = ind;
  1216. delimiter = 0;
  1217. if (member (string[i], "()\n"))
  1218. {
  1219. i++;
  1220. return i;
  1221. }
  1222. if (member (string[i], "<>;&|$"))
  1223. {
  1224. int peek = string[i + 1];
  1225. if (peek == string[i] && peek != '$')
  1226. {
  1227. if (peek == '<' && string[i + 2] == '-')
  1228. i++;
  1229. else if (peek == '<' && string[i + 2] == '<')
  1230. i++;
  1231. i += 2;
  1232. return i;
  1233. }
  1234. else
  1235. {
  1236. if ((peek == '&' && (string[i] == '>' || string[i] == '<')) ||
  1237. (peek == '>' && string[i] == '&') ||
  1238. (peek == '(' && (string[i] == '>' || string[i] == '<')) || /* ) */
  1239. (peek == '(' && string[i] == '$')) /* ) */
  1240. {
  1241. i += 2;
  1242. return i;
  1243. }
  1244. }
  1245. if (string[i] != '$')
  1246. {
  1247. i++;
  1248. return i;
  1249. }
  1250. }
  1251. /* Get word from string + i; */
  1252. if (member (string[i], HISTORY_QUOTE_CHARACTERS))
  1253. delimiter = string[i++];
  1254. for (; string[i]; i++)
  1255. {
  1256. if (string[i] == '\\' && string[i + 1] == '\n')
  1257. {
  1258. i++;
  1259. continue;
  1260. }
  1261. if (string[i] == '\\' && delimiter != '\'' &&
  1262. (delimiter != '"' || member (string[i], slashify_in_quotes)))
  1263. {
  1264. i++;
  1265. continue;
  1266. }
  1267. if (delimiter && string[i] == delimiter)
  1268. {
  1269. delimiter = 0;
  1270. continue;
  1271. }
  1272. if (!delimiter && (member (string[i], history_word_delimiters)))
  1273. break;
  1274. if (!delimiter && member (string[i], HISTORY_QUOTE_CHARACTERS))
  1275. delimiter = string[i];
  1276. }
  1277. return i;
  1278. }
  1279. static char *
  1280. history_substring (string, start, end)
  1281. const char *string;
  1282. int start, end;
  1283. {
  1284. register int len;
  1285. register char *result;
  1286. len = end - start;
  1287. result = (char *)xmalloc (len + 1);
  1288. strncpy (result, string + start, len);
  1289. result[len] = '\0';
  1290. return result;
  1291. }
  1292. /* Parse STRING into tokens and return an array of strings. If WIND is
  1293. not -1 and INDP is not null, we also want the word surrounding index
  1294. WIND. The position in the returned array of strings is returned in
  1295. *INDP. */
  1296. static char **
  1297. history_tokenize_internal (string, wind, indp)
  1298. const char *string;
  1299. int wind, *indp;
  1300. {
  1301. char **result;
  1302. register int i, start, result_index, size;
  1303. /* If we're searching for a string that's not part of a word (e.g., " "),
  1304. make sure we set *INDP to a reasonable value. */
  1305. if (indp && wind != -1)
  1306. *indp = -1;
  1307. /* Get a token, and stuff it into RESULT. The tokens are split
  1308. exactly where the shell would split them. */
  1309. for (i = result_index = size = 0, result = (char **)NULL; string[i]; )
  1310. {
  1311. /* Skip leading whitespace. */
  1312. for (; string[i] && whitespace (string[i]); i++)
  1313. ;
  1314. if (string[i] == 0 || string[i] == history_comment_char)
  1315. return (result);
  1316. start = i;
  1317. i = history_tokenize_word (string, start);
  1318. /* If we have a non-whitespace delimiter character (which would not be
  1319. skipped by the loop above), use it and any adjacent delimiters to
  1320. make a separate field. Any adjacent white space will be skipped the
  1321. next time through the loop. */
  1322. if (i == start && history_word_delimiters)
  1323. {
  1324. i++;
  1325. while (string[i] && member (string[i], history_word_delimiters))
  1326. i++;
  1327. }
  1328. /* If we are looking for the word in which the character at a
  1329. particular index falls, remember it. */
  1330. if (indp && wind != -1 && wind >= start && wind < i)
  1331. *indp = result_index;
  1332. if (result_index + 2 >= size)
  1333. result = (char **)xrealloc (result, ((size += 10) * sizeof (char *)));
  1334. result[result_index++] = history_substring (string, start, i);
  1335. result[result_index] = (char *)NULL;
  1336. }
  1337. return (result);
  1338. }
  1339. /* Return an array of tokens, much as the shell might. The tokens are
  1340. parsed out of STRING. */
  1341. char **
  1342. history_tokenize (string)
  1343. const char *string;
  1344. {
  1345. return (history_tokenize_internal (string, -1, (int *)NULL));
  1346. }
  1347. /* Free members of WORDS from START to an empty string */
  1348. static void
  1349. freewords (words, start)
  1350. char **words;
  1351. int start;
  1352. {
  1353. register int i;
  1354. for (i = start; words[i]; i++)
  1355. free (words[i]);
  1356. }
  1357. /* Find and return the word which contains the character at index IND
  1358. in the history line LINE. Used to save the word matched by the
  1359. last history !?string? search. */
  1360. static char *
  1361. history_find_word (line, ind)
  1362. char *line;
  1363. int ind;
  1364. {
  1365. char **words, *s;
  1366. int i, wind;
  1367. words = history_tokenize_internal (line, ind, &wind);
  1368. if (wind == -1 || words == 0)
  1369. {
  1370. if (words)
  1371. freewords (words, 0);
  1372. FREE (words);
  1373. return ((char *)NULL);
  1374. }
  1375. s = words[wind];
  1376. for (i = 0; i < wind; i++)
  1377. free (words[i]);
  1378. freewords (words, wind + 1);
  1379. free (words);
  1380. return s;
  1381. }