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

/src/bin/gdb/readline/histexpand.c

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