PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/reference/bash-1.14.7/lib/readline/history.c

http://github.com/cardmagic/lucash
C | 2218 lines | 1746 code | 231 blank | 241 comment | 250 complexity | 16505aa109c6a232bd0ffeacabfb9d5d MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /* History.c -- standalone history library */
  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 1, 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. 675 Mass Ave, Cambridge, MA 02139, USA. */
  17. /* The goal is to make the implementation transparent, so that you
  18. don't have to know what data types are used, just what functions
  19. you can call. I think I have done that. */
  20. #define READLINE_LIBRARY
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/file.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #if defined (HAVE_STDLIB_H)
  27. # include <stdlib.h>
  28. #else
  29. # include "ansi_stdlib.h"
  30. #endif /* HAVE_STDLIB_H */
  31. #if defined (HAVE_UNISTD_H)
  32. # include <unistd.h>
  33. #endif
  34. #if defined (HAVE_STRING_H)
  35. # include <string.h>
  36. #else
  37. # include <strings.h>
  38. #endif /* !HAVE_STRING_H */
  39. #include <errno.h>
  40. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  41. #if !defined (errno)
  42. extern int errno;
  43. #endif /* !errno */
  44. #include "memalloc.h"
  45. #include "history.h"
  46. #if defined (STATIC_MALLOC)
  47. static char *xmalloc (), *xrealloc ();
  48. #else
  49. extern char *xmalloc (), *xrealloc ();
  50. #endif /* STATIC_MALLOC */
  51. #define STREQ(a, b) (((a)[0] == (b)[0]) && (strcmp ((a), (b)) == 0))
  52. #define STREQN(a, b, n) (((a)[0] == (b)[0]) && (strncmp ((a), (b), (n)) == 0))
  53. #ifndef savestring
  54. # ifndef strcpy
  55. extern char *strcpy ();
  56. # endif
  57. #define savestring(x) strcpy (xmalloc (1 + strlen (x)), (x))
  58. #endif
  59. #ifndef whitespace
  60. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  61. #endif
  62. #ifndef digit_p
  63. #define digit_p(c) ((c) >= '0' && (c) <= '9')
  64. #endif
  65. #ifndef digit_value
  66. #define digit_value(c) ((c) - '0')
  67. #endif
  68. #ifndef member
  69. # ifndef strchr
  70. extern char *strchr ();
  71. # endif
  72. #define member(c, s) ((c) ? ((char *)strchr ((s), (c)) != (char *)NULL) : 0)
  73. #endif
  74. /* Possible history errors passed to hist_error. */
  75. #define EVENT_NOT_FOUND 0
  76. #define BAD_WORD_SPEC 1
  77. #define SUBST_FAILED 2
  78. #define BAD_MODIFIER 3
  79. static char error_pointer;
  80. static char *subst_lhs;
  81. static char *subst_rhs;
  82. static int subst_lhs_len = 0;
  83. static int subst_rhs_len = 0;
  84. static char *get_history_word_specifier ();
  85. static char *history_find_word ();
  86. #if defined (SHELL)
  87. extern char *single_quote ();
  88. #endif
  89. /* **************************************************************** */
  90. /* */
  91. /* History Functions */
  92. /* */
  93. /* **************************************************************** */
  94. /* An array of HIST_ENTRY. This is where we store the history. */
  95. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  96. /* Non-zero means that we have enforced a limit on the amount of
  97. history that we save. */
  98. static int history_stifled = 0;
  99. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  100. entries to remember. */
  101. int max_input_history;
  102. /* The current location of the interactive history pointer. Just makes
  103. life easier for outside callers. */
  104. static int history_offset = 0;
  105. /* The number of strings currently stored in the input_history list. */
  106. int history_length = 0;
  107. /* The current number of slots allocated to the input_history. */
  108. static int history_size = 0;
  109. /* The number of slots to increase the_history by. */
  110. #define DEFAULT_HISTORY_GROW_SIZE 50
  111. /* The character that represents the start of a history expansion
  112. request. This is usually `!'. */
  113. char history_expansion_char = '!';
  114. /* The character that invokes word substitution if found at the start of
  115. a line. This is usually `^'. */
  116. char history_subst_char = '^';
  117. /* During tokenization, if this character is seen as the first character
  118. of a word, then it, and all subsequent characters upto a newline are
  119. ignored. For a Bourne shell, this should be '#'. Bash special cases
  120. the interactive comment character to not be a comment delimiter. */
  121. char history_comment_char = '\0';
  122. /* The list of characters which inhibit the expansion of text if found
  123. immediately following history_expansion_char. */
  124. char *history_no_expand_chars = " \t\n\r=";
  125. /* The logical `base' of the history array. It defaults to 1. */
  126. int history_base = 1;
  127. /* Return the current HISTORY_STATE of the history. */
  128. HISTORY_STATE *
  129. history_get_history_state ()
  130. {
  131. HISTORY_STATE *state;
  132. state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  133. state->entries = the_history;
  134. state->offset = history_offset;
  135. state->length = history_length;
  136. state->size = history_size;
  137. state->flags = 0;
  138. if (history_stifled)
  139. state->flags |= HS_STIFLED;
  140. return (state);
  141. }
  142. /* Set the state of the current history array to STATE. */
  143. void
  144. history_set_history_state (state)
  145. HISTORY_STATE *state;
  146. {
  147. the_history = state->entries;
  148. history_offset = state->offset;
  149. history_length = state->length;
  150. history_size = state->size;
  151. if (state->flags & HS_STIFLED)
  152. history_stifled = 1;
  153. }
  154. /* Begin a session in which the history functions might be used. This
  155. initializes interactive variables. */
  156. void
  157. using_history ()
  158. {
  159. history_offset = history_length;
  160. }
  161. /* Return the number of bytes that the primary history entries are using.
  162. This just adds up the lengths of the_history->lines. */
  163. int
  164. history_total_bytes ()
  165. {
  166. register int i, result;
  167. result = 0;
  168. for (i = 0; the_history && the_history[i]; i++)
  169. result += strlen (the_history[i]->line);
  170. return (result);
  171. }
  172. /* Place STRING at the end of the history list. The data field
  173. is set to NULL. */
  174. void
  175. add_history (string)
  176. char *string;
  177. {
  178. HIST_ENTRY *temp;
  179. if (history_stifled && (history_length == max_input_history))
  180. {
  181. register int i;
  182. /* If the history is stifled, and history_length is zero,
  183. and it equals max_input_history, we don't save items. */
  184. if (history_length == 0)
  185. return;
  186. /* If there is something in the slot, then remove it. */
  187. if (the_history[0])
  188. {
  189. free (the_history[0]->line);
  190. free (the_history[0]);
  191. }
  192. /* Copy the rest of the entries, moving down one slot. */
  193. for (i = 0; i < history_length; i++)
  194. the_history[i] = the_history[i + 1];
  195. history_base++;
  196. }
  197. else
  198. {
  199. if (!history_size)
  200. {
  201. history_size = DEFAULT_HISTORY_GROW_SIZE;
  202. the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
  203. history_length = 1;
  204. }
  205. else
  206. {
  207. if (history_length == (history_size - 1))
  208. {
  209. history_size += DEFAULT_HISTORY_GROW_SIZE;
  210. the_history = (HIST_ENTRY **)
  211. xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
  212. }
  213. history_length++;
  214. }
  215. }
  216. temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  217. temp->line = savestring (string);
  218. temp->data = (char *)NULL;
  219. the_history[history_length] = (HIST_ENTRY *)NULL;
  220. the_history[history_length - 1] = temp;
  221. }
  222. /* Make the history entry at WHICH have LINE and DATA. This returns
  223. the old entry so you can dispose of the data. In the case of an
  224. invalid WHICH, a NULL pointer is returned. */
  225. HIST_ENTRY *
  226. replace_history_entry (which, line, data)
  227. int which;
  228. char *line;
  229. char *data;
  230. {
  231. HIST_ENTRY *temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  232. HIST_ENTRY *old_value;
  233. if (which >= history_length)
  234. return ((HIST_ENTRY *)NULL);
  235. old_value = the_history[which];
  236. temp->line = savestring (line);
  237. temp->data = data;
  238. the_history[which] = temp;
  239. return (old_value);
  240. }
  241. /* Returns the magic number which says what history element we are
  242. looking at now. In this implementation, it returns history_offset. */
  243. int
  244. where_history ()
  245. {
  246. return (history_offset);
  247. }
  248. /* Search the history for STRING, starting at history_offset.
  249. If DIRECTION < 0, then the search is through previous entries, else
  250. through subsequent. If ANCHORED is non-zero, the string must
  251. appear at the beginning of a history line, otherwise, the string
  252. may appear anywhere in the line. If the string is found, then
  253. current_history () is the history entry, and the value of this
  254. function is the offset in the line of that history entry that the
  255. string was found in. Otherwise, nothing is changed, and a -1 is
  256. returned. */
  257. #define ANCHORED_SEARCH 1
  258. #define NON_ANCHORED_SEARCH 0
  259. static int
  260. history_search_internal (string, direction, anchored)
  261. char *string;
  262. int direction, anchored;
  263. {
  264. register int i, reverse;
  265. register char *line;
  266. register int line_index;
  267. int string_len;
  268. i = history_offset;
  269. reverse = (direction < 0);
  270. /* Take care of trivial cases first. */
  271. if (string == 0 || *string == '\0')
  272. return (-1);
  273. if (!history_length || ((i == history_length) && !reverse))
  274. return (-1);
  275. if (reverse && (i == history_length))
  276. i--;
  277. #define NEXT_LINE() do { if (reverse) i--; else i++; } while (0)
  278. string_len = strlen (string);
  279. while (1)
  280. {
  281. /* Search each line in the history list for STRING. */
  282. /* At limit for direction? */
  283. if ((reverse && i < 0) || (!reverse && i == history_length))
  284. return (-1);
  285. line = the_history[i]->line;
  286. line_index = strlen (line);
  287. /* If STRING is longer than line, no match. */
  288. if (string_len > line_index)
  289. {
  290. NEXT_LINE ();
  291. continue;
  292. }
  293. /* Handle anchored searches first. */
  294. if (anchored == ANCHORED_SEARCH)
  295. {
  296. if (STREQN (string, line, string_len))
  297. {
  298. history_offset = i;
  299. return (0);
  300. }
  301. NEXT_LINE ();
  302. continue;
  303. }
  304. /* Do substring search. */
  305. if (reverse)
  306. {
  307. line_index -= string_len;
  308. while (line_index >= 0)
  309. {
  310. if (STREQN (string, line + line_index, string_len))
  311. {
  312. history_offset = i;
  313. return (line_index);
  314. }
  315. line_index--;
  316. }
  317. }
  318. else
  319. {
  320. register int limit = line_index - string_len + 1;
  321. line_index = 0;
  322. while (line_index < limit)
  323. {
  324. if (STREQN (string, line + line_index, string_len))
  325. {
  326. history_offset = i;
  327. return (line_index);
  328. }
  329. line_index++;
  330. }
  331. }
  332. NEXT_LINE ();
  333. }
  334. }
  335. /* Do a non-anchored search for STRING through the history in DIRECTION. */
  336. int
  337. history_search (string, direction)
  338. char *string;
  339. int direction;
  340. {
  341. return (history_search_internal (string, direction, NON_ANCHORED_SEARCH));
  342. }
  343. /* Do an anchored search for string through the history in DIRECTION. */
  344. int
  345. history_search_prefix (string, direction)
  346. char *string;
  347. int direction;
  348. {
  349. return (history_search_internal (string, direction, ANCHORED_SEARCH));
  350. }
  351. /* Remove history element WHICH from the history. The removed
  352. element is returned to you so you can free the line, data,
  353. and containing structure. */
  354. HIST_ENTRY *
  355. remove_history (which)
  356. int which;
  357. {
  358. HIST_ENTRY *return_value;
  359. if (which >= history_length || !history_length)
  360. return_value = (HIST_ENTRY *)NULL;
  361. else
  362. {
  363. register int i;
  364. return_value = the_history[which];
  365. for (i = which; i < history_length; i++)
  366. the_history[i] = the_history[i + 1];
  367. history_length--;
  368. }
  369. return (return_value);
  370. }
  371. /* Stifle the history list, remembering only MAX number of lines. */
  372. void
  373. stifle_history (max)
  374. int max;
  375. {
  376. if (max < 0)
  377. max = 0;
  378. if (history_length > max)
  379. {
  380. register int i, j;
  381. /* This loses because we cannot free the data. */
  382. for (i = 0; i < (history_length - max); i++)
  383. {
  384. free (the_history[i]->line);
  385. free (the_history[i]);
  386. }
  387. history_base = i;
  388. for (j = 0, i = history_length - max; j < max; i++, j++)
  389. the_history[j] = the_history[i];
  390. the_history[j] = (HIST_ENTRY *)NULL;
  391. history_length = j;
  392. }
  393. history_stifled = 1;
  394. max_input_history = max;
  395. }
  396. /* Stop stifling the history. This returns the previous amount the history
  397. was stifled by. The value is positive if the history was stifled, negative
  398. if it wasn't. */
  399. int
  400. unstifle_history ()
  401. {
  402. int result = max_input_history;
  403. if (history_stifled)
  404. {
  405. result = -result;
  406. history_stifled = 0;
  407. }
  408. return (result);
  409. }
  410. int
  411. history_is_stifled ()
  412. {
  413. return (history_stifled);
  414. }
  415. /* Return the string that should be used in the place of this
  416. filename. This only matters when you don't specify the
  417. filename to read_history (), or write_history (). */
  418. static char *
  419. history_filename (filename)
  420. char *filename;
  421. {
  422. char *return_val = filename ? savestring (filename) : (char *)NULL;
  423. if (!return_val)
  424. {
  425. char *home;
  426. int home_len;
  427. home = getenv ("HOME");
  428. if (!home)
  429. home = ".";
  430. home_len = strlen (home);
  431. /* strlen(".history") == 8 */
  432. return_val = xmalloc (2 + home_len + 8);
  433. strcpy (return_val, home);
  434. return_val[home_len] = '/';
  435. strcpy (return_val + home_len + 1, ".history");
  436. }
  437. return (return_val);
  438. }
  439. /* Add the contents of FILENAME to the history list, a line at a time.
  440. If FILENAME is NULL, then read from ~/.history. Returns 0 if
  441. successful, or errno if not. */
  442. int
  443. read_history (filename)
  444. char *filename;
  445. {
  446. return (read_history_range (filename, 0, -1));
  447. }
  448. /* Read a range of lines from FILENAME, adding them to the history list.
  449. Start reading at the FROM'th line and end at the TO'th. If FROM
  450. is zero, start at the beginning. If TO is less than FROM, read
  451. until the end of the file. If FILENAME is NULL, then read from
  452. ~/.history. Returns 0 if successful, or errno if not. */
  453. int
  454. read_history_range (filename, from, to)
  455. char *filename;
  456. int from, to;
  457. {
  458. register int line_start, line_end;
  459. char *input, *buffer = (char *)NULL;
  460. int file, current_line;
  461. struct stat finfo;
  462. input = history_filename (filename);
  463. file = open (input, O_RDONLY, 0666);
  464. if ((file < 0) || (fstat (file, &finfo) == -1))
  465. goto error_and_exit;
  466. buffer = xmalloc ((int)finfo.st_size + 1);
  467. if (read (file, buffer, finfo.st_size) != finfo.st_size)
  468. {
  469. error_and_exit:
  470. if (file >= 0)
  471. close (file);
  472. if (input)
  473. free (input);
  474. if (buffer)
  475. free (buffer);
  476. return (errno);
  477. }
  478. close (file);
  479. /* Set TO to larger than end of file if negative. */
  480. if (to < 0)
  481. to = finfo.st_size;
  482. /* Start at beginning of file, work to end. */
  483. line_start = line_end = current_line = 0;
  484. /* Skip lines until we are at FROM. */
  485. while (line_start < finfo.st_size && current_line < from)
  486. {
  487. for (line_end = line_start; line_end < finfo.st_size; line_end++)
  488. if (buffer[line_end] == '\n')
  489. {
  490. current_line++;
  491. line_start = line_end + 1;
  492. if (current_line == from)
  493. break;
  494. }
  495. }
  496. /* If there are lines left to gobble, then gobble them now. */
  497. for (line_end = line_start; line_end < finfo.st_size; line_end++)
  498. if (buffer[line_end] == '\n')
  499. {
  500. buffer[line_end] = '\0';
  501. if (buffer[line_start])
  502. add_history (buffer + line_start);
  503. current_line++;
  504. if (current_line >= to)
  505. break;
  506. line_start = line_end + 1;
  507. }
  508. if (input)
  509. free (input);
  510. if (buffer)
  511. free (buffer);
  512. return (0);
  513. }
  514. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  515. If FNAME is NULL, then use ~/.history. */
  516. int
  517. history_truncate_file (fname, lines)
  518. char *fname;
  519. register int lines;
  520. {
  521. register int i;
  522. int file, chars_read;
  523. char *buffer = (char *)NULL, *filename;
  524. struct stat finfo;
  525. filename = history_filename (fname);
  526. file = open (filename, O_RDONLY, 0666);
  527. if (file == -1 || fstat (file, &finfo) == -1)
  528. goto truncate_exit;
  529. buffer = xmalloc ((int)finfo.st_size + 1);
  530. chars_read = read (file, buffer, finfo.st_size);
  531. close (file);
  532. if (chars_read <= 0)
  533. goto truncate_exit;
  534. /* Count backwards from the end of buffer until we have passed
  535. LINES lines. */
  536. for (i = chars_read - 1; lines && i; i--)
  537. {
  538. if (buffer[i] == '\n')
  539. lines--;
  540. }
  541. /* If this is the first line, then the file contains exactly the
  542. number of lines we want to truncate to, so we don't need to do
  543. anything. It's the first line if we don't find a newline between
  544. the current value of i and 0. Otherwise, write from the start of
  545. this line until the end of the buffer. */
  546. for ( ; i; i--)
  547. if (buffer[i] == '\n')
  548. {
  549. i++;
  550. break;
  551. }
  552. /* Write only if there are more lines in the file than we want to
  553. truncate to. */
  554. if (i && ((file = open (filename, O_WRONLY|O_TRUNC, 0666)) != -1))
  555. {
  556. write (file, buffer + i, finfo.st_size - i);
  557. close (file);
  558. }
  559. truncate_exit:
  560. if (buffer)
  561. free (buffer);
  562. free (filename);
  563. return 0;
  564. }
  565. #define HISTORY_APPEND 0
  566. #define HISTORY_OVERWRITE 1
  567. /* Workhorse function for writing history. Writes NELEMENT entries
  568. from the history list to FILENAME. OVERWRITE is non-zero if you
  569. wish to replace FILENAME with the entries. */
  570. static int
  571. history_do_write (filename, nelements, overwrite)
  572. char *filename;
  573. int nelements, overwrite;
  574. {
  575. register int i;
  576. char *output = history_filename (filename);
  577. int file, mode;
  578. mode = overwrite ? O_WRONLY | O_CREAT | O_TRUNC : O_WRONLY | O_APPEND;
  579. if ((file = open (output, mode, 0666)) == -1)
  580. {
  581. if (output)
  582. free (output);
  583. return (errno);
  584. }
  585. if (nelements > history_length)
  586. nelements = history_length;
  587. /* Build a buffer of all the lines to write, and write them in one syscall.
  588. Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
  589. {
  590. register int j = 0;
  591. int buffer_size = 0;
  592. char *buffer;
  593. /* Calculate the total number of bytes to write. */
  594. for (i = history_length - nelements; i < history_length; i++)
  595. buffer_size += 1 + strlen (the_history[i]->line);
  596. /* Allocate the buffer, and fill it. */
  597. buffer = xmalloc (buffer_size);
  598. for (i = history_length - nelements; i < history_length; i++)
  599. {
  600. strcpy (buffer + j, the_history[i]->line);
  601. j += strlen (the_history[i]->line);
  602. buffer[j++] = '\n';
  603. }
  604. write (file, buffer, buffer_size);
  605. free (buffer);
  606. }
  607. close (file);
  608. if (output)
  609. free (output);
  610. return (0);
  611. }
  612. /* Append NELEMENT entries to FILENAME. The entries appended are from
  613. the end of the list minus NELEMENTs up to the end of the list. */
  614. int
  615. append_history (nelements, filename)
  616. int nelements;
  617. char *filename;
  618. {
  619. return (history_do_write (filename, nelements, HISTORY_APPEND));
  620. }
  621. /* Overwrite FILENAME with the current history. If FILENAME is NULL,
  622. then write the history list to ~/.history. Values returned
  623. are as in read_history ().*/
  624. int
  625. write_history (filename)
  626. char *filename;
  627. {
  628. return (history_do_write (filename, history_length, HISTORY_OVERWRITE));
  629. }
  630. /* Return the history entry at the current position, as determined by
  631. history_offset. If there is no entry there, return a NULL pointer. */
  632. HIST_ENTRY *
  633. current_history ()
  634. {
  635. if ((history_offset == history_length) || !the_history)
  636. return ((HIST_ENTRY *)NULL);
  637. else
  638. return (the_history[history_offset]);
  639. }
  640. /* Back up history_offset to the previous history entry, and return
  641. a pointer to that entry. If there is no previous entry then return
  642. a NULL pointer. */
  643. HIST_ENTRY *
  644. previous_history ()
  645. {
  646. if (!history_offset)
  647. return ((HIST_ENTRY *)NULL);
  648. else
  649. return (the_history[--history_offset]);
  650. }
  651. /* Move history_offset forward to the next history entry, and return
  652. a pointer to that entry. If there is no next entry then return a
  653. NULL pointer. */
  654. HIST_ENTRY *
  655. next_history ()
  656. {
  657. if (history_offset == history_length)
  658. return ((HIST_ENTRY *)NULL);
  659. else
  660. return (the_history[++history_offset]);
  661. }
  662. /* Return the current history array. The caller has to be carefull, since this
  663. is the actual array of data, and could be bashed or made corrupt easily.
  664. The array is terminated with a NULL pointer. */
  665. HIST_ENTRY **
  666. history_list ()
  667. {
  668. return (the_history);
  669. }
  670. /* Return the history entry which is logically at OFFSET in the history array.
  671. OFFSET is relative to history_base. */
  672. HIST_ENTRY *
  673. history_get (offset)
  674. int offset;
  675. {
  676. int local_index = offset - history_base;
  677. if (local_index >= history_length ||
  678. local_index < 0 ||
  679. !the_history)
  680. return ((HIST_ENTRY *)NULL);
  681. return (the_history[local_index]);
  682. }
  683. /* Search for STRING in the history list. DIR is < 0 for searching
  684. backwards. POS is an absolute index into the history list at
  685. which point to begin searching. */
  686. int
  687. history_search_pos (string, dir, pos)
  688. char *string;
  689. int dir, pos;
  690. {
  691. int ret, old = where_history ();
  692. history_set_pos (pos);
  693. if (history_search (string, dir) == -1)
  694. {
  695. history_set_pos (old);
  696. return (-1);
  697. }
  698. ret = where_history ();
  699. history_set_pos (old);
  700. return ret;
  701. }
  702. /* Make the current history item be the one at POS, an absolute index.
  703. Returns zero if POS is out of range, else non-zero. */
  704. int
  705. history_set_pos (pos)
  706. int pos;
  707. {
  708. if (pos > history_length || pos < 0 || !the_history)
  709. return (0);
  710. history_offset = pos;
  711. return (1);
  712. }
  713. /* **************************************************************** */
  714. /* */
  715. /* History Expansion */
  716. /* */
  717. /* **************************************************************** */
  718. /* Hairy history expansion on text, not tokens. This is of general
  719. use, and thus belongs in this library. */
  720. /* The last string searched for in a !?string? search. */
  721. static char *search_string = (char *)NULL;
  722. /* The last string matched by a !?string? search. */
  723. static char *search_match = (char *)NULL;
  724. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  725. point to after the event specifier. Just a pointer to the history
  726. line is returned; NULL is returned in the event of a bad specifier.
  727. You pass STRING with *INDEX equal to the history_expansion_char that
  728. begins this specification.
  729. DELIMITING_QUOTE is a character that is allowed to end the string
  730. specification for what to search for in addition to the normal
  731. characters `:', ` ', `\t', `\n', and sometimes `?'.
  732. So you might call this function like:
  733. line = get_history_event ("!echo:p", &index, 0); */
  734. char *
  735. get_history_event (string, caller_index, delimiting_quote)
  736. char *string;
  737. int *caller_index;
  738. int delimiting_quote;
  739. {
  740. register int i = *caller_index;
  741. register char c;
  742. HIST_ENTRY *entry;
  743. int which, sign = 1;
  744. int local_index, search_mode, substring_okay = 0;
  745. char *temp;
  746. /* The event can be specified in a number of ways.
  747. !! the previous command
  748. !n command line N
  749. !-n current command-line minus N
  750. !str the most recent command starting with STR
  751. !?str[?]
  752. the most recent command containing STR
  753. All values N are determined via HISTORY_BASE. */
  754. if (string[i] != history_expansion_char)
  755. return ((char *)NULL);
  756. /* Move on to the specification. */
  757. i++;
  758. #define RETURN_ENTRY(e, w) \
  759. return ((e = history_get (w)) ? e->line : (char *)NULL)
  760. /* Handle !! case. */
  761. if (string[i] == history_expansion_char)
  762. {
  763. i++;
  764. which = history_base + (history_length - 1);
  765. *caller_index = i;
  766. RETURN_ENTRY (entry, which);
  767. }
  768. /* Hack case of numeric line specification. */
  769. if (string[i] == '-')
  770. {
  771. sign = -1;
  772. i++;
  773. }
  774. if (digit_p (string[i]))
  775. {
  776. /* Get the extent of the digits and compute the value. */
  777. for (which = 0; digit_p (string[i]); i++)
  778. which = (which * 10) + digit_value (string[i]);
  779. *caller_index = i;
  780. if (sign < 0)
  781. which = (history_length + history_base) - which;
  782. RETURN_ENTRY (entry, which);
  783. }
  784. /* This must be something to search for. If the spec begins with
  785. a '?', then the string may be anywhere on the line. Otherwise,
  786. the string must be found at the start of a line. */
  787. if (string[i] == '?')
  788. {
  789. substring_okay++;
  790. i++;
  791. }
  792. /* Only a closing `?' or a newline delimit a substring search string. */
  793. for (local_index = i; c = string[i]; i++)
  794. if ((!substring_okay && (whitespace (c) || c == ':' ||
  795. #if defined (SHELL)
  796. member (c, ";&()|<>") ||
  797. #endif /* SHELL */
  798. string[i] == delimiting_quote)) ||
  799. string[i] == '\n' ||
  800. (substring_okay && string[i] == '?'))
  801. break;
  802. temp = xmalloc (1 + (i - local_index));
  803. strncpy (temp, &string[local_index], (i - local_index));
  804. temp[i - local_index] = '\0';
  805. if (substring_okay && string[i] == '?')
  806. i++;
  807. *caller_index = i;
  808. #define FAIL_SEARCH() \
  809. do { history_offset = history_length; free (temp) ; return (char *)NULL; } while (0)
  810. search_mode = substring_okay ? NON_ANCHORED_SEARCH : ANCHORED_SEARCH;
  811. while (1)
  812. {
  813. local_index = history_search_internal (temp, -1, search_mode);
  814. if (local_index < 0)
  815. FAIL_SEARCH ();
  816. if (local_index == 0 || substring_okay)
  817. {
  818. entry = current_history ();
  819. history_offset = history_length;
  820. /* If this was a substring search, then remember the
  821. string that we matched for word substitution. */
  822. if (substring_okay)
  823. {
  824. if (search_string)
  825. free (search_string);
  826. search_string = temp;
  827. if (search_match)
  828. free (search_match);
  829. search_match = history_find_word (entry->line, local_index);
  830. }
  831. else
  832. free (temp);
  833. return (entry->line);
  834. }
  835. if (history_offset)
  836. history_offset--;
  837. else
  838. FAIL_SEARCH ();
  839. }
  840. #undef FAIL_SEARCH
  841. #undef RETURN_ENTRY
  842. }
  843. #if defined (SHELL)
  844. /* Function for extracting single-quoted strings. Used for inhibiting
  845. history expansion within single quotes. */
  846. /* Extract the contents of STRING as if it is enclosed in single quotes.
  847. SINDEX, when passed in, is the offset of the character immediately
  848. following the opening single quote; on exit, SINDEX is left pointing
  849. to the closing single quote. */
  850. static void
  851. rl_string_extract_single_quoted (string, sindex)
  852. char *string;
  853. int *sindex;
  854. {
  855. register int i = *sindex;
  856. while (string[i] && string[i] != '\'')
  857. i++;
  858. *sindex = i;
  859. }
  860. static char *
  861. quote_breaks (s)
  862. char *s;
  863. {
  864. register char *p, *r;
  865. char *ret;
  866. int len = 3;
  867. for (p = s; p && *p; p++, len++)
  868. {
  869. if (*p == '\'')
  870. len += 3;
  871. else if (whitespace (*p) || *p == '\n')
  872. len += 2;
  873. }
  874. r = ret = xmalloc (len);
  875. *r++ = '\'';
  876. for (p = s; p && *p; )
  877. {
  878. if (*p == '\'')
  879. {
  880. *r++ = '\'';
  881. *r++ = '\\';
  882. *r++ = '\'';
  883. *r++ = '\'';
  884. p++;
  885. }
  886. else if (whitespace (*p) || *p == '\n')
  887. {
  888. *r++ = '\'';
  889. *r++ = *p++;
  890. *r++ = '\'';
  891. }
  892. else
  893. *r++ = *p++;
  894. }
  895. *r++ = '\'';
  896. *r = '\0';
  897. return ret;
  898. }
  899. #endif /* SHELL */
  900. static char *
  901. hist_error(s, start, current, errtype)
  902. char *s;
  903. int start, current, errtype;
  904. {
  905. char *temp, *emsg;
  906. int ll, elen;
  907. ll = current - start;
  908. switch (errtype)
  909. {
  910. case EVENT_NOT_FOUND:
  911. emsg = "event not found";
  912. elen = 15;
  913. break;
  914. case BAD_WORD_SPEC:
  915. emsg = "bad word specifier";
  916. elen = 18;
  917. break;
  918. case SUBST_FAILED:
  919. emsg = "substitution failed";
  920. elen = 19;
  921. break;
  922. case BAD_MODIFIER:
  923. emsg = "unrecognized history modifier";
  924. elen = 29;
  925. break;
  926. default:
  927. emsg = "unknown expansion error";
  928. elen = 23;
  929. break;
  930. }
  931. temp = xmalloc (ll + elen + 3);
  932. strncpy (temp, s + start, ll);
  933. temp[ll] = ':';
  934. temp[ll + 1] = ' ';
  935. strcpy (temp + ll + 2, emsg);
  936. return (temp);
  937. }
  938. /* Get a history substitution string from STR starting at *IPTR
  939. and return it. The length is returned in LENPTR.
  940. A backslash can quote the delimiter. If the string is the
  941. empty string, the previous pattern is used. If there is
  942. no previous pattern for the lhs, the last history search
  943. string is used.
  944. If IS_RHS is 1, we ignore empty strings and set the pattern
  945. to "" anyway. subst_lhs is not changed if the lhs is empty;
  946. subst_rhs is allowed to be set to the empty string. */
  947. static char *
  948. get_subst_pattern (str, iptr, delimiter, is_rhs, lenptr)
  949. char *str;
  950. int *iptr, delimiter, is_rhs, *lenptr;
  951. {
  952. register int si, i, j, k;
  953. char *s = (char *) NULL;
  954. i = *iptr;
  955. for (si = i; str[si] && str[si] != delimiter; si++)
  956. if (str[si] == '\\' && str[si + 1] == delimiter)
  957. si++;
  958. if (si > i || is_rhs)
  959. {
  960. s = xmalloc (si - i + 1);
  961. for (j = 0, k = i; k < si; j++, k++)
  962. {
  963. /* Remove a backslash quoting the search string delimiter. */
  964. if (str[k] == '\\' && str[k + 1] == delimiter)
  965. k++;
  966. s[j] = str[k];
  967. }
  968. s[j] = '\0';
  969. if (lenptr)
  970. *lenptr = j;
  971. }
  972. i = si;
  973. if (str[i])
  974. i++;
  975. *iptr = i;
  976. return s;
  977. }
  978. static void
  979. postproc_subst_rhs ()
  980. {
  981. char *new;
  982. int i, j, new_size;
  983. new = xmalloc (new_size = subst_rhs_len + subst_lhs_len);
  984. for (i = j = 0; i < subst_rhs_len; i++)
  985. {
  986. if (subst_rhs[i] == '&')
  987. {
  988. if (j + subst_lhs_len >= new_size)
  989. new = xrealloc (new, (new_size = new_size * 2 + subst_lhs_len));
  990. strcpy (new + j, subst_lhs);
  991. j += subst_lhs_len;
  992. }
  993. else
  994. {
  995. /* a single backslash protects the `&' from lhs interpolation */
  996. if (subst_rhs[i] == '\\' && subst_rhs[i + 1] == '&')
  997. i++;
  998. if (j >= new_size)
  999. new = xrealloc (new, new_size *= 2);
  1000. new[j++] = subst_rhs[i];
  1001. }
  1002. }
  1003. new[j] = '\0';
  1004. free (subst_rhs);
  1005. subst_rhs = new;
  1006. subst_rhs_len = j;
  1007. }
  1008. /* Expand the bulk of a history specifier starting at STRING[START].
  1009. Returns 0 if everything is OK, -1 if an error occurred, and 1
  1010. if the `p' modifier was supplied and the caller should just print
  1011. the returned string. Returns the new index into string in
  1012. *END_INDEX_PTR, and the expanded specifier in *RET_STRING. */
  1013. static int
  1014. history_expand_internal (string, start, end_index_ptr, ret_string, current_line)
  1015. char *string;
  1016. int start, *end_index_ptr;
  1017. char **ret_string;
  1018. char *current_line; /* for !# */
  1019. {
  1020. int i, n, starting_index;
  1021. int substitute_globally, want_quotes, print_only;
  1022. char *event, *temp, *result, *tstr, *t, c, *word_spec;
  1023. int result_len;
  1024. result = xmalloc (result_len = 128);
  1025. i = start;
  1026. /* If it is followed by something that starts a word specifier,
  1027. then !! is implied as the event specifier. */
  1028. if (member (string[i + 1], ":$*%^"))
  1029. {
  1030. char fake_s[3];
  1031. int fake_i = 0;
  1032. i++;
  1033. fake_s[0] = fake_s[1] = history_expansion_char;
  1034. fake_s[2] = '\0';
  1035. event = get_history_event (fake_s, &fake_i, 0);
  1036. }
  1037. else if (string[i + 1] == '#')
  1038. {
  1039. i += 2;
  1040. event = current_line;
  1041. }
  1042. else
  1043. {
  1044. int quoted_search_delimiter = 0;
  1045. /* If the character before this `!' is a double or single
  1046. quote, then this expansion takes place inside of the
  1047. quoted string. If we have to search for some text ("!foo"),
  1048. allow the delimiter to end the search string. */
  1049. if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  1050. quoted_search_delimiter = string[i - 1];
  1051. event = get_history_event (string, &i, quoted_search_delimiter);
  1052. }
  1053. if (!event)
  1054. {
  1055. *ret_string = hist_error (string, start, i, EVENT_NOT_FOUND);
  1056. free (result);
  1057. return (-1);
  1058. }
  1059. /* If a word specifier is found, then do what that requires. */
  1060. starting_index = i;
  1061. word_spec = get_history_word_specifier (string, event, &i);
  1062. /* There is no such thing as a `malformed word specifier'. However,
  1063. it is possible for a specifier that has no match. In that case,
  1064. we complain. */
  1065. if (word_spec == (char *)&error_pointer)
  1066. {
  1067. *ret_string = hist_error (string, starting_index, i, BAD_WORD_SPEC);
  1068. free (result);
  1069. return (-1);
  1070. }
  1071. /* If no word specifier, than the thing of interest was the event. */
  1072. if (!word_spec)
  1073. temp = savestring (event);
  1074. else
  1075. {
  1076. temp = savestring (word_spec);
  1077. free (word_spec);
  1078. }
  1079. /* Perhaps there are other modifiers involved. Do what they say. */
  1080. want_quotes = substitute_globally = print_only = 0;
  1081. starting_index = i;
  1082. while (string[i] == ':')
  1083. {
  1084. c = string[i + 1];
  1085. if (c == 'g')
  1086. {
  1087. substitute_globally = 1;
  1088. i++;
  1089. c = string[i + 1];
  1090. }
  1091. switch (c)
  1092. {
  1093. default:
  1094. *ret_string = hist_error (string, i+1, i+2, BAD_MODIFIER);
  1095. free (result);
  1096. free (temp);
  1097. return -1;
  1098. #if defined (SHELL)
  1099. case 'q':
  1100. want_quotes = 'q';
  1101. break;
  1102. case 'x':
  1103. want_quotes = 'x';
  1104. break;
  1105. #endif /* SHELL */
  1106. /* :p means make this the last executed line. So we
  1107. return an error state after adding this line to the
  1108. history. */
  1109. case 'p':
  1110. print_only++;
  1111. break;
  1112. /* :t discards all but the last part of the pathname. */
  1113. case 't':
  1114. tstr = strrchr (temp, '/');
  1115. if (tstr)
  1116. {
  1117. tstr++;
  1118. t = savestring (tstr);
  1119. free (temp);
  1120. temp = t;
  1121. }
  1122. break;
  1123. /* :h discards the last part of a pathname. */
  1124. case 'h':
  1125. tstr = strrchr (temp, '/');
  1126. if (tstr)
  1127. *tstr = '\0';
  1128. break;
  1129. /* :r discards the suffix. */
  1130. case 'r':
  1131. tstr = strrchr (temp, '.');
  1132. if (tstr)
  1133. *tstr = '\0';
  1134. break;
  1135. /* :e discards everything but the suffix. */
  1136. case 'e':
  1137. tstr = strrchr (temp, '.');
  1138. if (tstr)
  1139. {
  1140. t = savestring (tstr);
  1141. free (temp);
  1142. temp = t;
  1143. }
  1144. break;
  1145. /* :s/this/that substitutes `that' for the first
  1146. occurrence of `this'. :gs/this/that substitutes `that'
  1147. for each occurrence of `this'. :& repeats the last
  1148. substitution. :g& repeats the last substitution
  1149. globally. */
  1150. case '&':
  1151. case 's':
  1152. {
  1153. char *new_event, *t;
  1154. int delimiter, failed, si, l_temp;
  1155. if (c == 's')
  1156. {
  1157. if (i + 2 < (int)strlen (string))
  1158. delimiter = string[i + 2];
  1159. else
  1160. break; /* no search delimiter */
  1161. i += 3;
  1162. t = get_subst_pattern (string, &i, delimiter, 0, &subst_lhs_len);
  1163. /* An empty substitution lhs with no previous substitution
  1164. uses the last search string as the lhs. */
  1165. if (t)
  1166. {
  1167. if (subst_lhs)
  1168. free (subst_lhs);
  1169. subst_lhs = t;
  1170. }
  1171. else if (!subst_lhs)
  1172. {
  1173. if (search_string && *search_string)
  1174. {
  1175. subst_lhs = savestring (search_string);
  1176. subst_lhs_len = strlen (subst_lhs);
  1177. }
  1178. else
  1179. {
  1180. subst_lhs = (char *) NULL;
  1181. subst_lhs_len = 0;
  1182. }
  1183. }
  1184. /* If there is no lhs, the substitution can't succeed. */
  1185. if (subst_lhs_len == 0)
  1186. {
  1187. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  1188. free (result);
  1189. free (temp);
  1190. return -1;
  1191. }
  1192. if (subst_rhs)
  1193. free (subst_rhs);
  1194. subst_rhs = get_subst_pattern (string, &i, delimiter, 1, &subst_rhs_len);
  1195. /* If `&' appears in the rhs, it's supposed to be replaced
  1196. with the lhs. */
  1197. if (member ('&', subst_rhs))
  1198. postproc_subst_rhs ();
  1199. }
  1200. else
  1201. i += 2;
  1202. l_temp = strlen (temp);
  1203. /* Ignore impossible cases. */
  1204. if (subst_lhs_len > l_temp)
  1205. {
  1206. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  1207. free (result);
  1208. free (temp);
  1209. return (-1);
  1210. }
  1211. /* Find the first occurrence of THIS in TEMP. */
  1212. si = 0;
  1213. for (failed = 1; (si + subst_lhs_len) <= l_temp; si++)
  1214. if (STREQN (temp+si, subst_lhs, subst_lhs_len))
  1215. {
  1216. int len = subst_rhs_len - subst_lhs_len + l_temp;
  1217. new_event = xmalloc (1 + len);
  1218. strncpy (new_event, temp, si);
  1219. strncpy (new_event + si, subst_rhs, subst_rhs_len);
  1220. strncpy (new_event + si + subst_rhs_len,
  1221. temp + si + subst_lhs_len,
  1222. l_temp - (si + subst_lhs_len));
  1223. new_event[len] = '\0';
  1224. free (temp);
  1225. temp = new_event;
  1226. failed = 0;
  1227. if (substitute_globally)
  1228. {
  1229. si += subst_rhs_len;
  1230. l_temp = strlen (temp);
  1231. substitute_globally++;
  1232. continue;
  1233. }
  1234. else
  1235. break;
  1236. }
  1237. if (substitute_globally > 1)
  1238. {
  1239. substitute_globally = 0;
  1240. continue; /* don't want to increment i */
  1241. }
  1242. if (failed == 0)
  1243. continue; /* don't want to increment i */
  1244. *ret_string = hist_error (string, starting_index, i, SUBST_FAILED);
  1245. free (result);
  1246. free (temp);
  1247. return (-1);
  1248. }
  1249. }
  1250. i += 2;
  1251. }
  1252. /* Done with modfiers. */
  1253. /* Believe it or not, we have to back the pointer up by one. */
  1254. --i;
  1255. #if defined (SHELL)
  1256. if (want_quotes)
  1257. {
  1258. char *x;
  1259. if (want_quotes == 'q')
  1260. x = single_quote (temp);
  1261. else if (want_quotes == 'x')
  1262. x = quote_breaks (temp);
  1263. else
  1264. x = savestring (temp);
  1265. free (temp);
  1266. temp = x;
  1267. }
  1268. #endif /* SHELL */
  1269. n = strlen (temp);
  1270. if (n > result_len)
  1271. result = xrealloc (result, n + 2);
  1272. strcpy (result, temp);
  1273. free (temp);
  1274. *end_index_ptr = i;
  1275. *ret_string = result;
  1276. return (print_only);
  1277. }
  1278. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  1279. to a string. Returns:
  1280. -1) If there was an error in expansion.
  1281. 0) If no expansions took place (or, if the only change in
  1282. the text was the de-slashifying of the history expansion
  1283. character)
  1284. 1) If expansions did take place
  1285. 2) If the `p' modifier was given and the caller should print the result
  1286. If an error ocurred in expansion, then OUTPUT contains a descriptive
  1287. error message. */
  1288. #define ADD_STRING(s) \
  1289. do \
  1290. { \
  1291. int sl = strlen (s); \
  1292. j += sl; \
  1293. if (j >= result_len) \
  1294. { \
  1295. while (j >= result_len) \
  1296. result_len += 128; \
  1297. result = xrealloc (result, result_len); \
  1298. } \
  1299. strcpy (result + j - sl, s); \
  1300. } \
  1301. while (0)
  1302. #define ADD_CHAR(c) \
  1303. do \
  1304. { \
  1305. if (j >= result_len - 1) \
  1306. result = xrealloc (result, result_len += 64); \
  1307. result[j++] = c; \
  1308. result[j] = '\0'; \
  1309. } \
  1310. while (0)
  1311. int
  1312. history_expand (hstring, output)
  1313. char *hstring;
  1314. char **output;
  1315. {
  1316. register int j;
  1317. int i, r, l, passc, cc, modified, eindex, only_printing;
  1318. char *string;
  1319. /* The output string, and its length. */
  1320. int result_len;
  1321. char *result;
  1322. /* Used when adding the string. */
  1323. char *temp;
  1324. /* Setting the history expansion character to 0 inhibits all
  1325. history expansion. */
  1326. if (history_expansion_char == 0)
  1327. {
  1328. *output = savestring (hstring);
  1329. return (0);
  1330. }
  1331. /* Prepare the buffer for printing error messages. */
  1332. result = xmalloc (result_len = 256);
  1333. result[0] = '\0';
  1334. only_printing = modified = 0;
  1335. l = strlen (hstring);
  1336. /* Grovel the string. Only backslash can quote the history escape
  1337. character. We also handle arg specifiers. */
  1338. /* Before we grovel forever, see if the history_expansion_char appears
  1339. anywhere within the text. */
  1340. /* The quick substitution character is a history expansion all right. That
  1341. is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  1342. that is the substitution that we do. */
  1343. if (hstring[0] == history_subst_char)
  1344. {
  1345. string = xmalloc (l + 5);
  1346. string[0] = string[1] = history_expansion_char;
  1347. string[2] = ':';
  1348. string[3] = 's';
  1349. strcpy (string + 4, hstring);
  1350. l += 4;
  1351. }
  1352. else
  1353. {
  1354. string = hstring;
  1355. /* If not quick substitution, still maybe have to do expansion. */
  1356. /* `!' followed by one of the characters in history_no_expand_chars
  1357. is NOT an expansion. */
  1358. for (i = 0; string[i]; i++)
  1359. {
  1360. cc = string[i + 1];
  1361. if (string[i] == history_expansion_char)
  1362. {
  1363. if (!cc || member (cc, history_no_expand_chars))
  1364. continue;
  1365. #if defined (SHELL)
  1366. /* The shell uses ! as a pattern negation character
  1367. in globbing [...] expressions, so let those pass
  1368. without expansion. */
  1369. else if (i > 0 && (string[i - 1] == '[') &&
  1370. member (']', string + i + 1))
  1371. continue;
  1372. #endif /* SHELL */
  1373. else
  1374. break;
  1375. }
  1376. #if defined (SHELL)
  1377. else if (string[i] == '\'')
  1378. {
  1379. /* If this is bash, single quotes inhibit history expansion. */
  1380. i++;
  1381. rl_string_extract_single_quoted (string, &i);
  1382. }
  1383. else if (string[i] == '\\')
  1384. {
  1385. /* If this is bash, allow backslashes to quote single
  1386. quotes and
  1387. the history expansion character. */
  1388. if (cc == '\'' || cc == history_expansion_char)
  1389. i++;
  1390. }
  1391. #endif /* SHELL */
  1392. }
  1393. if (string[i] != history_expansion_char)
  1394. {
  1395. free (result);
  1396. *output = savestring (string);
  1397. return (0);
  1398. }
  1399. }
  1400. /* Extract and perform the substitution. */
  1401. for (passc = i = j = 0; i < l; i++)
  1402. {
  1403. int tchar = string[i];
  1404. if (passc)
  1405. {
  1406. passc = 0;
  1407. ADD_CHAR (tchar);
  1408. continue;
  1409. }
  1410. if (tchar == history_expansion_char)
  1411. tchar = -3;
  1412. switch (tchar)
  1413. {
  1414. default:
  1415. ADD_CHAR (string[i]);
  1416. break;
  1417. case '\\':
  1418. passc++;
  1419. ADD_CHAR (tchar);
  1420. break;
  1421. #if defined (SHELL)
  1422. case '\'':
  1423. {
  1424. /* If this is bash, single quotes inhibit history expansion. */
  1425. int quote, slen;
  1426. quote = i++;
  1427. rl_string_extract_single_quoted (string, &i);
  1428. slen = i - quote + 2;
  1429. temp = xmalloc (slen);
  1430. strncpy (temp, string + quote, slen);
  1431. temp[slen - 1] = '\0';
  1432. ADD_STRING (temp);
  1433. free (temp);
  1434. break;
  1435. }
  1436. #endif /* SHELL */
  1437. case -3: /* history_expansion_char */
  1438. cc = string[i + 1];
  1439. /* If the history_expansion_char is followed by one of the
  1440. characters in history_no_expand_chars, then it is not a
  1441. candidate for expansion of any kind. */
  1442. if (member (cc, history_no_expand_chars))
  1443. {
  1444. ADD_CHAR (string[i]);
  1445. break;
  1446. }
  1447. #if defined (NO_BANG_HASH_MODIFIERS)
  1448. /* There is something that is listed as a `word specifier' in csh
  1449. documentation which means `the expanded text to this point'.
  1450. That is not a word specifier, it is an event specifier. If we
  1451. don't want to allow modifiers with `!#', just stick the current
  1452. output line in again. */
  1453. if (cc == '#')
  1454. {
  1455. if (result)
  1456. {
  1457. temp = xmalloc (1 + strlen (result));
  1458. strcpy (temp, result);
  1459. ADD_STRING (temp);
  1460. free (temp);
  1461. }
  1462. i++;
  1463. break;
  1464. }
  1465. #endif
  1466. r = history_expand_internal (string, i, &eindex, &temp, result);
  1467. if (r < 0)
  1468. {
  1469. *output = temp;
  1470. free (result);
  1471. if (string != hstring)
  1472. free (string);
  1473. return -1;
  1474. }
  1475. else
  1476. {
  1477. if (temp)
  1478. {
  1479. modified++;
  1480. if (*temp)
  1481. ADD_STRING (temp);
  1482. free (temp);
  1483. }
  1484. only_printing = r == 1;
  1485. i = eindex;
  1486. }
  1487. break;
  1488. }
  1489. }
  1490. *output = result;
  1491. if (string != hstring)
  1492. free (string);
  1493. if (only_printing)
  1494. {
  1495. add_history (result);
  1496. return (2);
  1497. }
  1498. return (modified != 0);
  1499. }
  1500. /* Return a consed string which is the word specified in SPEC, and found
  1501. in FROM. NULL is returned if there is no spec. The address of
  1502. ERROR_POINTER is returned if the word specified cannot be found.
  1503. CALLER_INDEX is the offset in SPEC to start looking; it is updated
  1504. to point to just after the last character parsed. */
  1505. static char *
  1506. get_history_word_specifier (spec, from, caller_index)
  1507. char *spec, *from;
  1508. int *caller_index;
  1509. {
  1510. register int i = *caller_index;
  1511. int first, last;
  1512. int expecting_word_spec = 0;
  1513. char *result;
  1514. /* The range of words to return doesn't exist yet. */
  1515. first = last = 0;
  1516. result = (char *)NULL;
  1517. /* If we found a colon, then this *must* be a word specification. If
  1518. it isn't, then it is an error. */
  1519. if (spec[i] == ':')
  1520. {
  1521. i++;
  1522. expecting_word_spec++;
  1523. }
  1524. /* Handle special cases first. */
  1525. /* `%' is the word last searched for. */
  1526. if (spec[i] == '%')
  1527. {
  1528. *caller_index = i + 1;
  1529. return (search_match ? savestring (search_match) : savestring (""));
  1530. }
  1531. /* `*' matches all of the arguments, but not the command. */
  1532. if (spec[i] == '*')
  1533. {
  1534. *caller_index = i + 1;
  1535. result = history_arg_extract (1, '$', from);
  1536. return (result ? result : savestring (""));
  1537. }
  1538. /* `$' is last arg. */
  1539. if (spec[i] == '$')
  1540. {
  1541. *caller_index = i + 1;
  1542. return (history_arg_extract ('$', '$', from));
  1543. }
  1544. /* Try to get FIRST and LAST figured out. */
  1545. if (spec[i] == '-')
  1546. first = 0;
  1547. else if (spec[i] == '^')
  1548. first = 1;
  1549. else if (digit_p (spec[i]) && expecting_word_spec)
  1550. {
  1551. for (first = 0; digit_p (spec[i]); i++)
  1552. first = (first * 10) + digit_value (spec[i]);
  1553. }
  1554. else
  1555. return ((char *)NULL); /* no valid `first' for word specifier */
  1556. if (spec[i] == '^' || spec[i] == '*')
  1557. {
  1558. last = (spec[i] == '^') ? 1 : '$'; /* x* abbreviates x-$ */
  1559. i++;
  1560. }
  1561. else if (spec[i] != '-')
  1562. last = first;
  1563. else
  1564. {
  1565. i++;
  1566. if (digit_p (spec[i]))
  1567. {
  1568. for (last = 0; digit_p (spec[i]); i++)
  1569. last = (last * 10) + digit_value (spec[i]);
  1570. }
  1571. else if (spec[i] == '$')
  1572. {
  1573. i++;
  1574. last = '$';
  1575. }
  1576. else if (!spec[i] || spec[i] == ':') /* could be modifier separator */
  1577. last = -1; /* x- abbreviates x-$ omitting word `$' */
  1578. }
  1579. *caller_index = i;
  1580. if (last >= first || last == '$' || last < 0)
  1581. result = history_arg_extract (first, last, from);
  1582. return (result ? result : (char *)&error_pointer);
  1583. }
  1584. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1585. The args are taken from STRING. If either FIRST or LAST is < 0,
  1586. then make that arg count from the right (subtract from the number of
  1587. tokens, so that FIRST = -1 means the next to last token on the line).
  1588. If LAST is `$' the last arg from STRING is used. */
  1589. char *
  1590. history_arg_extract (first, last, string)
  1591. int first, last;
  1592. char *string;
  1593. {
  1594. register int i, len;
  1595. char *result = (char *)NULL;
  1596. int size = 0, offset = 0;
  1597. char **list;
  1598. /* XXX - think about making history_tokenize return a struct array,
  1599. each struct in array being a string and a length to avoid the
  1600. calls to strlen below. */
  1601. if ((list = history_tokenize (string)) == NULL)
  1602. return ((char *)NULL);
  1603. for (len = 0; list[len]; len++)
  1604. ;
  1605. if (last < 0)
  1606. last = len + last - 1;
  1607. if (first < 0)
  1608. first = len + first - 1;
  1609. if (last == '$')
  1610. last = len - 1;
  1611. if (first == '$')
  1612. first = len - 1;
  1613. last++;
  1614. if (first >= len || last > len || first < 0 || last < 0 || first > last)
  1615. result = ((char *)NULL);
  1616. else
  1617. {
  1618. for (size = 0, i = first; i < last; i++)
  1619. size += strlen (list[i]) + 1;
  1620. result = xmalloc (size + 1);
  1621. result[0] = '\0';
  1622. for (i = first; i < last; i++)
  1623. {
  1624. strcpy (result + offset, list[i]);
  1625. offset += strlen (list[i]);
  1626. if (i + 1 < last)
  1627. {
  1628. result[offset++] = ' ';
  1629. result[offset] = 0;
  1630. }
  1631. }
  1632. }
  1633. for (i = 0; i < len; i++)
  1634. free (list[i]);
  1635. free (list);
  1636. return (result);
  1637. }
  1638. #define slashify_in_quotes "\\`\"$"
  1639. /* Parse STRING into tokens and return an array of strings. If WIND is
  1640. not -1 and INDP is not null, we also want the word surrounding index
  1641. WIND. The position in the returned array of strings is returned in
  1642. *INDP. */
  1643. static char **
  1644. history_tokenize_internal (string, wind, indp)
  1645. char *string;
  1646. int wind, *indp;
  1647. {
  1648. char **result = (char **)NULL;
  1649. register int i, start, result_index, size;
  1650. int len;
  1651. i = result_index = size = 0;
  1652. /* Get a token, and stuff it into RESULT. The tokens are split
  1653. exactly where the shell would split them. */
  1654. while (string[i])
  1655. {
  1656. int delimiter = 0;
  1657. /* Skip leading whitespace. */
  1658. for (; string[i] && whitespace (string[i]); i++)
  1659. ;
  1660. if (!string[i] || string[i] == history_comment_char)
  1661. return (result);
  1662. start = i;
  1663. if (member (string[i], "()\n"))
  1664. {
  1665. i++;
  1666. goto got_token;
  1667. }
  1668. if (member (string[i], "<>;&|$"))
  1669. {
  1670. int peek = string[i + 1];
  1671. if (peek == string[i] && peek != '$')
  1672. {
  1673. if (peek == '<' && string[i + 2] == '-')
  1674. i++;
  1675. i += 2;
  1676. goto got_token;
  1677. }
  1678. else
  1679. {
  1680. if ((peek == '&' && (string[i] == '>' || string[i] == '<')) ||
  1681. ((peek == '>') && (string[i] == '&')) ||
  1682. ((peek == '(') && (string[i] == '$')))
  1683. {
  1684. i += 2;
  1685. goto got_token;
  1686. }
  1687. }
  1688. if (string[i] != '$')
  1689. {
  1690. i++;
  1691. goto got_token;
  1692. }
  1693. }
  1694. /* Get word from string + i; */
  1695. if (member (string[i], "\"'`"))
  1696. delimiter = string[i++];
  1697. for (; string[i]; i++)
  1698. {
  1699. if (string[i] == '\\' && string[i + 1] == '\n')
  1700. {
  1701. i++;
  1702. continue;
  1703. }
  1704. if (string[i] == '\\' && delimiter != '\'' &&
  1705. (delimiter != '"' || member (string[i], slashify_in_quotes)))
  1706. {
  1707. i++;
  1708. continue;
  1709. }
  1710. if (delimiter && string[i] == delimiter)
  1711. {
  1712. delimiter = 0;
  1713. continue;
  1714. }
  1715. if (!delimiter && (member (string[i], " \t\n;&()|<>")))
  1716. break;
  1717. if (!delimiter && member (string[i], "\"'`"))
  1718. delimiter = string[i];
  1719. }
  1720. got_token:
  1721. /* If we are looking for the word in which the character at a
  1722. particular index falls, remember it. */
  1723. if (indp && wind >= 0 && wind >= start && wind < i)
  1724. *indp = result_index;
  1725. len = i - start;
  1726. if (result_index + 2 >= size)
  1727. result = (char **)xrealloc (result, ((size += 10) * sizeof (char *)));
  1728. result[result_index] = xmalloc (1 + len);
  1729. strncpy (result[result_index], string + start, len);
  1730. result[result_index][len] = '\0';
  1731. result[++result_index] = (char *)NULL;
  1732. }
  1733. return (result);
  1734. }
  1735. /* Return an array of tokens, much as the shell might. The tokens are
  1736. parsed out of STRING. */
  1737. char **
  1738. history_tokenize (string)
  1739. char *string;
  1740. {
  1741. return (history_tokenize_internal (string, -1, (int *)NULL));
  1742. }
  1743. /* Find and return the word wh…

Large files files are truncated, but you can click here to view the full file