PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/cmd-line-utils/readline/histexpand.c

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