history.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* history.c -- standalone history library */
  2. /* Copyright (C) 1989-2009 Free Software Foundation, Inc.
  3. This file contains the GNU History Library (History), a set of
  4. routines for managing the text of previously typed lines.
  5. History is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. History is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with History. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /* The goal is to make the implementation transparent, so that you
  17. don't have to know what data types are used, just what functions
  18. you can call. I think I have done that. */
  19. #define READLINE_LIBRARY
  20. #if defined (HAVE_CONFIG_H)
  21. # include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #if defined (HAVE_STDLIB_H)
  25. # include <stdlib.h>
  26. #else
  27. # include "ansi_stdlib.h"
  28. #endif /* HAVE_STDLIB_H */
  29. #if defined (HAVE_UNISTD_H)
  30. # ifdef _MINIX
  31. # include <sys/types.h>
  32. # endif
  33. # include <unistd.h>
  34. #endif
  35. #include "history.h"
  36. #include "histlib.h"
  37. #include "xmalloc.h"
  38. /* The number of slots to increase the_history by. */
  39. #define DEFAULT_HISTORY_GROW_SIZE 50
  40. static char *hist_inittime PARAMS((void));
  41. /* **************************************************************** */
  42. /* */
  43. /* History Functions */
  44. /* */
  45. /* **************************************************************** */
  46. /* An array of HIST_ENTRY. This is where we store the history. */
  47. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  48. /* Non-zero means that we have enforced a limit on the amount of
  49. history that we save. */
  50. static int history_stifled;
  51. /* The current number of slots allocated to the input_history. */
  52. static int history_size;
  53. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  54. entries to remember. */
  55. int history_max_entries;
  56. int max_input_history; /* backwards compatibility */
  57. /* The current location of the interactive history pointer. Just makes
  58. life easier for outside callers. */
  59. int history_offset;
  60. /* The number of strings currently stored in the history list. */
  61. int history_length;
  62. /* The logical `base' of the history array. It defaults to 1. */
  63. int history_base = 1;
  64. /* Return the current HISTORY_STATE of the history. */
  65. HISTORY_STATE *
  66. history_get_history_state ()
  67. {
  68. HISTORY_STATE *state;
  69. state = (HISTORY_STATE *)xmalloc (sizeof (HISTORY_STATE));
  70. state->entries = the_history;
  71. state->offset = history_offset;
  72. state->length = history_length;
  73. state->size = history_size;
  74. state->flags = 0;
  75. if (history_stifled)
  76. state->flags |= HS_STIFLED;
  77. return (state);
  78. }
  79. /* Set the state of the current history array to STATE. */
  80. void
  81. history_set_history_state (state)
  82. HISTORY_STATE *state;
  83. {
  84. the_history = state->entries;
  85. history_offset = state->offset;
  86. history_length = state->length;
  87. history_size = state->size;
  88. if (state->flags & HS_STIFLED)
  89. history_stifled = 1;
  90. }
  91. /* Begin a session in which the history functions might be used. This
  92. initializes interactive variables. */
  93. void
  94. using_history ()
  95. {
  96. history_offset = history_length;
  97. }
  98. /* Return the number of bytes that the primary history entries are using.
  99. This just adds up the lengths of the_history->lines and the associated
  100. timestamps. */
  101. int
  102. history_total_bytes ()
  103. {
  104. register int i, result;
  105. for (i = result = 0; the_history && the_history[i]; i++)
  106. result += HISTENT_BYTES (the_history[i]);
  107. return (result);
  108. }
  109. /* Returns the magic number which says what history element we are
  110. looking at now. In this implementation, it returns history_offset. */
  111. int
  112. where_history ()
  113. {
  114. return (history_offset);
  115. }
  116. /* Make the current history item be the one at POS, an absolute index.
  117. Returns zero if POS is out of range, else non-zero. */
  118. int
  119. history_set_pos (pos)
  120. int pos;
  121. {
  122. if (pos > history_length || pos < 0 || !the_history)
  123. return (0);
  124. history_offset = pos;
  125. return (1);
  126. }
  127. /* Return the current history array. The caller has to be careful, since this
  128. is the actual array of data, and could be bashed or made corrupt easily.
  129. The array is terminated with a NULL pointer. */
  130. HIST_ENTRY **
  131. history_list ()
  132. {
  133. return (the_history);
  134. }
  135. /* Return the history entry at the current position, as determined by
  136. history_offset. If there is no entry there, return a NULL pointer. */
  137. HIST_ENTRY *
  138. current_history ()
  139. {
  140. return ((history_offset == history_length) || the_history == 0)
  141. ? (HIST_ENTRY *)NULL
  142. : the_history[history_offset];
  143. }
  144. /* Back up history_offset to the previous history entry, and return
  145. a pointer to that entry. If there is no previous entry then return
  146. a NULL pointer. */
  147. HIST_ENTRY *
  148. previous_history ()
  149. {
  150. return history_offset ? the_history[--history_offset] : (HIST_ENTRY *)NULL;
  151. }
  152. /* Move history_offset forward to the next history entry, and return
  153. a pointer to that entry. If there is no next entry then return a
  154. NULL pointer. */
  155. HIST_ENTRY *
  156. next_history ()
  157. {
  158. return (history_offset == history_length) ? (HIST_ENTRY *)NULL : the_history[++history_offset];
  159. }
  160. /* Return the history entry which is logically at OFFSET in the history array.
  161. OFFSET is relative to history_base. */
  162. HIST_ENTRY *
  163. history_get (offset)
  164. int offset;
  165. {
  166. int local_index;
  167. local_index = offset - history_base;
  168. return (local_index >= history_length || local_index < 0 || the_history == 0)
  169. ? (HIST_ENTRY *)NULL
  170. : the_history[local_index];
  171. }
  172. HIST_ENTRY *
  173. alloc_history_entry (string, ts)
  174. char *string;
  175. char *ts;
  176. {
  177. HIST_ENTRY *temp;
  178. temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  179. temp->line = string ? savestring (string) : string;
  180. temp->data = (char *)NULL;
  181. temp->timestamp = ts;
  182. return temp;
  183. }
  184. time_t
  185. history_get_time (hist)
  186. HIST_ENTRY *hist;
  187. {
  188. char *ts;
  189. time_t t;
  190. if (hist == 0 || hist->timestamp == 0)
  191. return 0;
  192. ts = hist->timestamp;
  193. if (ts[0] != history_comment_char)
  194. return 0;
  195. t = (time_t) atol (ts + 1); /* XXX - should use strtol() here */
  196. return t;
  197. }
  198. static char *
  199. hist_inittime ()
  200. {
  201. time_t t;
  202. char ts[64], *ret;
  203. t = (time_t) time ((time_t *)0);
  204. #if defined (HAVE_VSNPRINTF) /* assume snprintf if vsnprintf exists */
  205. snprintf (ts, sizeof (ts) - 1, "X%lu", (unsigned long) t);
  206. #else
  207. sprintf (ts, "X%lu", (unsigned long) t);
  208. #endif
  209. ret = savestring (ts);
  210. ret[0] = history_comment_char;
  211. return ret;
  212. }
  213. /* Place STRING at the end of the history list. The data field
  214. is set to NULL. */
  215. void
  216. add_history (string)
  217. const char *string;
  218. {
  219. HIST_ENTRY *temp;
  220. if (history_stifled && (history_length == history_max_entries))
  221. {
  222. register int i;
  223. /* If the history is stifled, and history_length is zero,
  224. and it equals history_max_entries, we don't save items. */
  225. if (history_length == 0)
  226. return;
  227. /* If there is something in the slot, then remove it. */
  228. if (the_history[0])
  229. (void) free_history_entry (the_history[0]);
  230. /* Copy the rest of the entries, moving down one slot. */
  231. for (i = 0; i < history_length; i++)
  232. the_history[i] = the_history[i + 1];
  233. history_base++;
  234. }
  235. else
  236. {
  237. if (history_size == 0)
  238. {
  239. history_size = DEFAULT_HISTORY_GROW_SIZE;
  240. the_history = (HIST_ENTRY **)xmalloc (history_size * sizeof (HIST_ENTRY *));
  241. history_length = 1;
  242. }
  243. else
  244. {
  245. if (history_length == (history_size - 1))
  246. {
  247. history_size += DEFAULT_HISTORY_GROW_SIZE;
  248. the_history = (HIST_ENTRY **)
  249. xrealloc (the_history, history_size * sizeof (HIST_ENTRY *));
  250. }
  251. history_length++;
  252. }
  253. }
  254. temp = alloc_history_entry (string, hist_inittime ());
  255. the_history[history_length] = (HIST_ENTRY *)NULL;
  256. the_history[history_length - 1] = temp;
  257. }
  258. /* Change the time stamp of the most recent history entry to STRING. */
  259. void
  260. add_history_time (string)
  261. const char *string;
  262. {
  263. HIST_ENTRY *hs;
  264. if (string == 0)
  265. return;
  266. hs = the_history[history_length - 1];
  267. FREE (hs->timestamp);
  268. hs->timestamp = savestring (string);
  269. }
  270. /* Free HIST and return the data so the calling application can free it
  271. if necessary and desired. */
  272. histdata_t
  273. free_history_entry (hist)
  274. HIST_ENTRY *hist;
  275. {
  276. histdata_t x;
  277. if (hist == 0)
  278. return ((histdata_t) 0);
  279. FREE (hist->line);
  280. FREE (hist->timestamp);
  281. x = hist->data;
  282. xfree (hist);
  283. return (x);
  284. }
  285. HIST_ENTRY *
  286. copy_history_entry (hist)
  287. HIST_ENTRY *hist;
  288. {
  289. HIST_ENTRY *ret;
  290. char *ts;
  291. if (hist == 0)
  292. return hist;
  293. ret = alloc_history_entry (hist->line, (char *)NULL);
  294. ts = hist->timestamp ? savestring (hist->timestamp) : hist->timestamp;
  295. ret->timestamp = ts;
  296. ret->data = hist->data;
  297. return ret;
  298. }
  299. /* Make the history entry at WHICH have LINE and DATA. This returns
  300. the old entry so you can dispose of the data. In the case of an
  301. invalid WHICH, a NULL pointer is returned. */
  302. HIST_ENTRY *
  303. replace_history_entry (which, line, data)
  304. int which;
  305. const char *line;
  306. histdata_t data;
  307. {
  308. HIST_ENTRY *temp, *old_value;
  309. if (which < 0 || which >= history_length)
  310. return ((HIST_ENTRY *)NULL);
  311. temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  312. old_value = the_history[which];
  313. temp->line = savestring (line);
  314. temp->data = data;
  315. temp->timestamp = savestring (old_value->timestamp);
  316. the_history[which] = temp;
  317. return (old_value);
  318. }
  319. /* Replace the DATA in the specified history entries, replacing OLD with
  320. NEW. WHICH says which one(s) to replace: WHICH == -1 means to replace
  321. all of the history entries where entry->data == OLD; WHICH == -2 means
  322. to replace the `newest' history entry where entry->data == OLD; and
  323. WHICH >= 0 means to replace that particular history entry's data, as
  324. long as it matches OLD. */
  325. void
  326. replace_history_data (which,old, new)
  327. int which;
  328. histdata_t *old, *new;
  329. {
  330. HIST_ENTRY *entry;
  331. register int i, last;
  332. if (which < -2 || which >= history_length || history_length == 0 || the_history == 0)
  333. return;
  334. if (which >= 0)
  335. {
  336. entry = the_history[which];
  337. if (entry && entry->data == old)
  338. entry->data = new;
  339. return;
  340. }
  341. last = -1;
  342. for (i = 0; i < history_length; i++)
  343. {
  344. entry = the_history[i];
  345. if (entry == 0)
  346. continue;
  347. if (entry->data == old)
  348. {
  349. last = i;
  350. if (which == -1)
  351. entry->data = new;
  352. }
  353. }
  354. if (which == -2 && last >= 0)
  355. {
  356. entry = the_history[last];
  357. entry->data = new; /* XXX - we don't check entry->old */
  358. }
  359. }
  360. /* Remove history element WHICH from the history. The removed
  361. element is returned to you so you can free the line, data,
  362. and containing structure. */
  363. HIST_ENTRY *
  364. remove_history (which)
  365. int which;
  366. {
  367. HIST_ENTRY *return_value;
  368. register int i;
  369. if (which < 0 || which >= history_length || history_length == 0 || the_history == 0)
  370. return ((HIST_ENTRY *)NULL);
  371. return_value = the_history[which];
  372. for (i = which; i < history_length; i++)
  373. the_history[i] = the_history[i + 1];
  374. history_length--;
  375. return (return_value);
  376. }
  377. /* Stifle the history list, remembering only MAX number of lines. */
  378. void
  379. stifle_history (max)
  380. int max;
  381. {
  382. register int i, j;
  383. if (max < 0)
  384. max = 0;
  385. if (history_length > max)
  386. {
  387. /* This loses because we cannot free the data. */
  388. for (i = 0, j = history_length - max; i < j; i++)
  389. free_history_entry (the_history[i]);
  390. history_base = i;
  391. for (j = 0, i = history_length - max; j < max; i++, j++)
  392. the_history[j] = the_history[i];
  393. the_history[j] = (HIST_ENTRY *)NULL;
  394. history_length = j;
  395. }
  396. history_stifled = 1;
  397. max_input_history = history_max_entries = max;
  398. }
  399. /* Stop stifling the history. This returns the previous maximum
  400. number of history entries. The value is positive if the history
  401. was stifled, negative if it wasn't. */
  402. int
  403. unstifle_history ()
  404. {
  405. if (history_stifled)
  406. {
  407. history_stifled = 0;
  408. return (history_max_entries);
  409. }
  410. else
  411. return (-history_max_entries);
  412. }
  413. int
  414. history_is_stifled ()
  415. {
  416. return (history_stifled);
  417. }
  418. void
  419. clear_history ()
  420. {
  421. register int i;
  422. /* This loses because we cannot free the data. */
  423. for (i = 0; i < history_length; i++)
  424. {
  425. free_history_entry (the_history[i]);
  426. the_history[i] = (HIST_ENTRY *)NULL;
  427. }
  428. history_offset = history_length = 0;
  429. }