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

/App/win-bash_0_6/v01/bash-1.14.2/lib/readline/history.c

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

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