histfile.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /* histfile.c - functions to manipulate the history file. */
  2. /* Copyright (C) 1989-2010 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 (__TANDEM)
  21. # include <floss.h>
  22. #endif
  23. #if defined (HAVE_CONFIG_H)
  24. # include <config.h>
  25. #endif
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #if ! defined (_MINIX) && defined (HAVE_SYS_FILE_H)
  29. # include <sys/file.h>
  30. #endif
  31. #include "posixstat.h"
  32. #include <fcntl.h>
  33. #if defined (HAVE_STDLIB_H)
  34. # include <stdlib.h>
  35. #else
  36. # include "ansi_stdlib.h"
  37. #endif /* HAVE_STDLIB_H */
  38. #if defined (HAVE_UNISTD_H)
  39. # include <unistd.h>
  40. #endif
  41. #include <ctype.h>
  42. #if defined (__EMX__)
  43. # undef HAVE_MMAP
  44. #endif
  45. #ifdef HISTORY_USE_MMAP
  46. # include <sys/mman.h>
  47. # ifdef MAP_FILE
  48. # define MAP_RFLAGS (MAP_FILE|MAP_PRIVATE)
  49. # define MAP_WFLAGS (MAP_FILE|MAP_SHARED)
  50. # else
  51. # define MAP_RFLAGS MAP_PRIVATE
  52. # define MAP_WFLAGS MAP_SHARED
  53. # endif
  54. # ifndef MAP_FAILED
  55. # define MAP_FAILED ((void *)-1)
  56. # endif
  57. #endif /* HISTORY_USE_MMAP */
  58. /* If we're compiling for __EMX__ (OS/2) or __CYGWIN__ (cygwin32 environment
  59. on win 95/98/nt), we want to open files with O_BINARY mode so that there
  60. is no \n -> \r\n conversion performed. On other systems, we don't want to
  61. mess around with O_BINARY at all, so we ensure that it's defined to 0. */
  62. #if defined (__EMX__) || defined (__CYGWIN__)
  63. # ifndef O_BINARY
  64. # define O_BINARY 0
  65. # endif
  66. #else /* !__EMX__ && !__CYGWIN__ */
  67. # undef O_BINARY
  68. # define O_BINARY 0
  69. #endif /* !__EMX__ && !__CYGWIN__ */
  70. #include <errno.h>
  71. #if !defined (errno)
  72. extern int errno;
  73. #endif /* !errno */
  74. #include "history.h"
  75. #include "histlib.h"
  76. #include "rlshell.h"
  77. #include "xmalloc.h"
  78. /* If non-zero, we write timestamps to the history file in history_do_write() */
  79. int history_write_timestamps = 0;
  80. /* Does S look like the beginning of a history timestamp entry? Placeholder
  81. for more extensive tests. */
  82. #define HIST_TIMESTAMP_START(s) (*(s) == history_comment_char && isdigit ((s)[1]) )
  83. /* Return the string that should be used in the place of this
  84. filename. This only matters when you don't specify the
  85. filename to read_history (), or write_history (). */
  86. static char *
  87. history_filename (filename)
  88. const char *filename;
  89. {
  90. char *return_val;
  91. const char *home;
  92. int home_len;
  93. return_val = filename ? savestring (filename) : (char *)NULL;
  94. if (return_val)
  95. return (return_val);
  96. home = sh_get_env_value ("HOME");
  97. if (home == 0)
  98. {
  99. #if 0
  100. home = ".";
  101. home_len = 1;
  102. #else
  103. return (NULL);
  104. #endif
  105. }
  106. else
  107. home_len = strlen (home);
  108. return_val = (char *)xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */
  109. strcpy (return_val, home);
  110. return_val[home_len] = '/';
  111. #if defined (__MSDOS__)
  112. strcpy (return_val + home_len + 1, "_history");
  113. #else
  114. strcpy (return_val + home_len + 1, ".history");
  115. #endif
  116. return (return_val);
  117. }
  118. /* Add the contents of FILENAME to the history list, a line at a time.
  119. If FILENAME is NULL, then read from ~/.history. Returns 0 if
  120. successful, or errno if not. */
  121. int
  122. read_history (filename)
  123. const char *filename;
  124. {
  125. return (read_history_range (filename, 0, -1));
  126. }
  127. /* Read a range of lines from FILENAME, adding them to the history list.
  128. Start reading at the FROM'th line and end at the TO'th. If FROM
  129. is zero, start at the beginning. If TO is less than FROM, read
  130. until the end of the file. If FILENAME is NULL, then read from
  131. ~/.history. Returns 0 if successful, or errno if not. */
  132. int
  133. read_history_range (filename, from, to)
  134. const char *filename;
  135. int from, to;
  136. {
  137. register char *line_start, *line_end, *p;
  138. char *input, *buffer, *bufend, *last_ts;
  139. int file, current_line, chars_read;
  140. struct stat finfo;
  141. size_t file_size;
  142. #if defined (EFBIG)
  143. int overflow_errno = EFBIG;
  144. #elif defined (EOVERFLOW)
  145. int overflow_errno = EOVERFLOW;
  146. #else
  147. int overflow_errno = EIO;
  148. #endif
  149. buffer = last_ts = (char *)NULL;
  150. input = history_filename (filename);
  151. file = input ? open (input, O_RDONLY|O_BINARY, 0666) : -1;
  152. if ((file < 0) || (fstat (file, &finfo) == -1))
  153. goto error_and_exit;
  154. file_size = (size_t)finfo.st_size;
  155. /* check for overflow on very large files */
  156. if (file_size != finfo.st_size || file_size + 1 < file_size)
  157. {
  158. errno = overflow_errno;
  159. goto error_and_exit;
  160. }
  161. #ifdef HISTORY_USE_MMAP
  162. /* We map read/write and private so we can change newlines to NULs without
  163. affecting the underlying object. */
  164. buffer = (char *)mmap (0, file_size, PROT_READ|PROT_WRITE, MAP_RFLAGS, file, 0);
  165. if ((void *)buffer == MAP_FAILED)
  166. {
  167. errno = overflow_errno;
  168. goto error_and_exit;
  169. }
  170. chars_read = file_size;
  171. #else
  172. buffer = (char *)malloc (file_size + 1);
  173. if (buffer == 0)
  174. {
  175. errno = overflow_errno;
  176. goto error_and_exit;
  177. }
  178. chars_read = read (file, buffer, file_size);
  179. #endif
  180. if (chars_read < 0)
  181. {
  182. error_and_exit:
  183. if (errno != 0)
  184. chars_read = errno;
  185. else
  186. chars_read = EIO;
  187. if (file >= 0)
  188. close (file);
  189. FREE (input);
  190. #ifndef HISTORY_USE_MMAP
  191. FREE (buffer);
  192. #endif
  193. return (chars_read);
  194. }
  195. close (file);
  196. /* Set TO to larger than end of file if negative. */
  197. if (to < 0)
  198. to = chars_read;
  199. /* Start at beginning of file, work to end. */
  200. bufend = buffer + chars_read;
  201. current_line = 0;
  202. /* Skip lines until we are at FROM. */
  203. for (line_start = line_end = buffer; line_end < bufend && current_line < from; line_end++)
  204. if (*line_end == '\n')
  205. {
  206. p = line_end + 1;
  207. /* If we see something we think is a timestamp, continue with this
  208. line. We should check more extensively here... */
  209. if (HIST_TIMESTAMP_START(p) == 0)
  210. current_line++;
  211. line_start = p;
  212. }
  213. /* If there are lines left to gobble, then gobble them now. */
  214. for (line_end = line_start; line_end < bufend; line_end++)
  215. if (*line_end == '\n')
  216. {
  217. /* Change to allow Windows-like \r\n end of line delimiter. */
  218. if (line_end > line_start && line_end[-1] == '\r')
  219. line_end[-1] = '\0';
  220. else
  221. *line_end = '\0';
  222. if (*line_start)
  223. {
  224. if (HIST_TIMESTAMP_START(line_start) == 0)
  225. {
  226. add_history (line_start);
  227. if (last_ts)
  228. {
  229. add_history_time (last_ts);
  230. last_ts = NULL;
  231. }
  232. }
  233. else
  234. {
  235. last_ts = line_start;
  236. current_line--;
  237. }
  238. }
  239. current_line++;
  240. if (current_line >= to)
  241. break;
  242. line_start = line_end + 1;
  243. }
  244. FREE (input);
  245. #ifndef HISTORY_USE_MMAP
  246. FREE (buffer);
  247. #else
  248. munmap (buffer, file_size);
  249. #endif
  250. return (0);
  251. }
  252. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  253. If FNAME is NULL, then use ~/.history. Returns 0 on success, errno
  254. on failure. */
  255. int
  256. history_truncate_file (fname, lines)
  257. const char *fname;
  258. int lines;
  259. {
  260. char *buffer, *filename, *bp, *bp1; /* bp1 == bp+1 */
  261. int file, chars_read, rv;
  262. struct stat finfo;
  263. size_t file_size;
  264. buffer = (char *)NULL;
  265. filename = history_filename (fname);
  266. file = filename ? open (filename, O_RDONLY|O_BINARY, 0666) : -1;
  267. rv = 0;
  268. /* Don't try to truncate non-regular files. */
  269. if (file == -1 || fstat (file, &finfo) == -1)
  270. {
  271. rv = errno;
  272. if (file != -1)
  273. close (file);
  274. goto truncate_exit;
  275. }
  276. if (S_ISREG (finfo.st_mode) == 0)
  277. {
  278. close (file);
  279. #ifdef EFTYPE
  280. rv = EFTYPE;
  281. #else
  282. rv = EINVAL;
  283. #endif
  284. goto truncate_exit;
  285. }
  286. file_size = (size_t)finfo.st_size;
  287. /* check for overflow on very large files */
  288. if (file_size != finfo.st_size || file_size + 1 < file_size)
  289. {
  290. close (file);
  291. #if defined (EFBIG)
  292. rv = errno = EFBIG;
  293. #elif defined (EOVERFLOW)
  294. rv = errno = EOVERFLOW;
  295. #else
  296. rv = errno = EINVAL;
  297. #endif
  298. goto truncate_exit;
  299. }
  300. buffer = (char *)malloc (file_size + 1);
  301. if (buffer == 0)
  302. {
  303. close (file);
  304. goto truncate_exit;
  305. }
  306. chars_read = read (file, buffer, file_size);
  307. close (file);
  308. if (chars_read <= 0)
  309. {
  310. rv = (chars_read < 0) ? errno : 0;
  311. goto truncate_exit;
  312. }
  313. /* Count backwards from the end of buffer until we have passed
  314. LINES lines. bp1 is set funny initially. But since bp[1] can't
  315. be a comment character (since it's off the end) and *bp can't be
  316. both a newline and the history comment character, it should be OK. */
  317. for (bp1 = bp = buffer + chars_read - 1; lines && bp > buffer; bp--)
  318. {
  319. if (*bp == '\n' && HIST_TIMESTAMP_START(bp1) == 0)
  320. lines--;
  321. bp1 = bp;
  322. }
  323. /* If this is the first line, then the file contains exactly the
  324. number of lines we want to truncate to, so we don't need to do
  325. anything. It's the first line if we don't find a newline between
  326. the current value of i and 0. Otherwise, write from the start of
  327. this line until the end of the buffer. */
  328. for ( ; bp > buffer; bp--)
  329. {
  330. if (*bp == '\n' && HIST_TIMESTAMP_START(bp1) == 0)
  331. {
  332. bp++;
  333. break;
  334. }
  335. bp1 = bp;
  336. }
  337. /* Write only if there are more lines in the file than we want to
  338. truncate to. */
  339. if (bp > buffer && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0600)) != -1))
  340. {
  341. write (file, bp, chars_read - (bp - buffer));
  342. #if defined (__BEOS__)
  343. /* BeOS ignores O_TRUNC. */
  344. ftruncate (file, chars_read - (bp - buffer));
  345. #endif
  346. close (file);
  347. }
  348. truncate_exit:
  349. FREE (buffer);
  350. xfree (filename);
  351. return rv;
  352. }
  353. /* Workhorse function for writing history. Writes NELEMENT entries
  354. from the history list to FILENAME. OVERWRITE is non-zero if you
  355. wish to replace FILENAME with the entries. */
  356. static int
  357. history_do_write (filename, nelements, overwrite)
  358. const char *filename;
  359. int nelements, overwrite;
  360. {
  361. register int i;
  362. char *output;
  363. int file, mode, rv;
  364. #ifdef HISTORY_USE_MMAP
  365. size_t cursize;
  366. mode = overwrite ? O_RDWR|O_CREAT|O_TRUNC|O_BINARY : O_RDWR|O_APPEND|O_BINARY;
  367. #else
  368. mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
  369. #endif
  370. output = history_filename (filename);
  371. file = output ? open (output, mode, 0600) : -1;
  372. rv = 0;
  373. if (file == -1)
  374. {
  375. FREE (output);
  376. return (errno);
  377. }
  378. #ifdef HISTORY_USE_MMAP
  379. cursize = overwrite ? 0 : lseek (file, 0, SEEK_END);
  380. #endif
  381. if (nelements > history_length)
  382. nelements = history_length;
  383. /* Build a buffer of all the lines to write, and write them in one syscall.
  384. Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
  385. {
  386. HIST_ENTRY **the_history; /* local */
  387. register int j;
  388. int buffer_size;
  389. char *buffer;
  390. the_history = history_list ();
  391. /* Calculate the total number of bytes to write. */
  392. for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
  393. #if 0
  394. buffer_size += 2 + HISTENT_BYTES (the_history[i]);
  395. #else
  396. {
  397. if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
  398. buffer_size += strlen (the_history[i]->timestamp) + 1;
  399. buffer_size += strlen (the_history[i]->line) + 1;
  400. }
  401. #endif
  402. /* Allocate the buffer, and fill it. */
  403. #ifdef HISTORY_USE_MMAP
  404. if (ftruncate (file, buffer_size+cursize) == -1)
  405. goto mmap_error;
  406. buffer = (char *)mmap (0, buffer_size, PROT_READ|PROT_WRITE, MAP_WFLAGS, file, cursize);
  407. if ((void *)buffer == MAP_FAILED)
  408. {
  409. mmap_error:
  410. rv = errno;
  411. FREE (output);
  412. close (file);
  413. return rv;
  414. }
  415. #else
  416. buffer = (char *)malloc (buffer_size);
  417. if (buffer == 0)
  418. {
  419. rv = errno;
  420. FREE (output);
  421. close (file);
  422. return rv;
  423. }
  424. #endif
  425. for (j = 0, i = history_length - nelements; i < history_length; i++)
  426. {
  427. if (history_write_timestamps && the_history[i]->timestamp && the_history[i]->timestamp[0])
  428. {
  429. strcpy (buffer + j, the_history[i]->timestamp);
  430. j += strlen (the_history[i]->timestamp);
  431. buffer[j++] = '\n';
  432. }
  433. strcpy (buffer + j, the_history[i]->line);
  434. j += strlen (the_history[i]->line);
  435. buffer[j++] = '\n';
  436. }
  437. #ifdef HISTORY_USE_MMAP
  438. if (msync (buffer, buffer_size, 0) != 0 || munmap (buffer, buffer_size) != 0)
  439. rv = errno;
  440. #else
  441. if (write (file, buffer, buffer_size) < 0)
  442. rv = errno;
  443. xfree (buffer);
  444. #endif
  445. }
  446. close (file);
  447. FREE (output);
  448. return (rv);
  449. }
  450. /* Append NELEMENT entries to FILENAME. The entries appended are from
  451. the end of the list minus NELEMENTs up to the end of the list. */
  452. int
  453. append_history (nelements, filename)
  454. int nelements;
  455. const char *filename;
  456. {
  457. return (history_do_write (filename, nelements, HISTORY_APPEND));
  458. }
  459. /* Overwrite FILENAME with the current history. If FILENAME is NULL,
  460. then write the history list to ~/.history. Values returned
  461. are as in read_history ().*/
  462. int
  463. write_history (filename)
  464. const char *filename;
  465. {
  466. return (history_do_write (filename, history_length, HISTORY_OVERWRITE));
  467. }