histfile.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:9k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* histfile.c - functions to manipulate the history file. */
  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. #if defined (HAVE_CONFIG_H)
  22. #  include <config.h>
  23. #endif
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #ifndef _MINIX
  27. #  include <sys/file.h>
  28. #endif
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #if defined (HAVE_STDLIB_H)
  32. #  include <stdlib.h>
  33. #else
  34. #  include "ansi_stdlib.h"
  35. #endif /* HAVE_STDLIB_H */
  36. #if defined (HAVE_UNISTD_H)
  37. #  include <unistd.h>
  38. #endif
  39. #if defined (HAVE_STRING_H)
  40. #  include <string.h>
  41. #else
  42. #  include <strings.h>
  43. #endif /* !HAVE_STRING_H */
  44. #if defined (__EMX__)
  45. #  ifndef O_BINARY
  46. #    define O_BINARY 0
  47. #  endif
  48. #else /* !__EMX__ */
  49.    /* If we're not compiling for __EMX__, we don't want this at all.  Ever. */
  50. #  undef O_BINARY
  51. #  define O_BINARY 0
  52. #endif /* !__EMX__ */
  53. #include <errno.h>
  54. #if !defined (errno)
  55. extern int errno;
  56. #endif /* !errno */
  57. #include "history.h"
  58. #include "histlib.h"
  59. /* Functions imported from shell.c */
  60. extern char *get_env_value ();
  61. extern char *xmalloc (), *xrealloc ();
  62. /* Return the string that should be used in the place of this
  63.    filename.  This only matters when you don't specify the
  64.    filename to read_history (), or write_history (). */
  65. static char *
  66. history_filename (filename)
  67.      char *filename;
  68. {
  69.   char *return_val, *home;
  70.   int home_len;
  71.   return_val = filename ? savestring (filename) : (char *)NULL;
  72.   if (return_val)
  73.     return (return_val);
  74.   
  75.   home = get_env_value ("HOME");
  76.   if (home == 0)
  77.     {
  78.       home = ".";
  79.       home_len = 1;
  80.     }
  81.   else
  82.     home_len = strlen (home);
  83.   return_val = xmalloc (2 + home_len + 8); /* strlen(".history") == 8 */
  84.   strcpy (return_val, home);
  85.   return_val[home_len] = '/';
  86.   strcpy (return_val + home_len + 1, ".history");
  87.   return (return_val);
  88. }
  89. /* Add the contents of FILENAME to the history list, a line at a time.
  90.    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
  91.    successful, or errno if not. */
  92. int
  93. read_history (filename)
  94.      char *filename;
  95. {
  96.   return (read_history_range (filename, 0, -1));
  97. }
  98. /* Read a range of lines from FILENAME, adding them to the history list.
  99.    Start reading at the FROM'th line and end at the TO'th.  If FROM
  100.    is zero, start at the beginning.  If TO is less than FROM, read
  101.    until the end of the file.  If FILENAME is NULL, then read from
  102.    ~/.history.  Returns 0 if successful, or errno if not. */
  103. int
  104. read_history_range (filename, from, to)
  105.      char *filename;
  106.      int from, to;
  107. {
  108.   register int line_start, line_end;
  109.   char *input, *buffer;
  110.   int file, current_line;
  111.   struct stat finfo;
  112.   size_t file_size;
  113.   buffer = (char *)NULL;
  114.   input = history_filename (filename);
  115.   file = open (input, O_RDONLY|O_BINARY, 0666);
  116.   if ((file < 0) || (fstat (file, &finfo) == -1))
  117.     goto error_and_exit;
  118.   file_size = (size_t)finfo.st_size;
  119.   /* check for overflow on very large files */
  120.   if (file_size != finfo.st_size || file_size + 1 < file_size)
  121.     {
  122. #if defined (EFBIG)
  123.       errno = EFBIG;
  124. #endif
  125.       goto error_and_exit;
  126.     }
  127.   buffer = xmalloc (file_size + 1);
  128. #if 0
  129.   if (read (file, buffer, file_size) != file_size)
  130. #else
  131.   if (read (file, buffer, file_size) < 0)
  132. #endif
  133.     {
  134.   error_and_exit:
  135.       if (file >= 0)
  136. close (file);
  137.       FREE (input);
  138.       FREE (buffer);
  139.       return (errno);
  140.     }
  141.   close (file);
  142.   /* Set TO to larger than end of file if negative. */
  143.   if (to < 0)
  144.     to = file_size;
  145.   /* Start at beginning of file, work to end. */
  146.   line_start = line_end = current_line = 0;
  147.   /* Skip lines until we are at FROM. */
  148.   while (line_start < file_size && current_line < from)
  149.     {
  150.       for (line_end = line_start; line_end < file_size; line_end++)
  151. if (buffer[line_end] == 'n')
  152.   {
  153.     current_line++;
  154.     line_start = line_end + 1;
  155.     if (current_line == from)
  156.       break;
  157.   }
  158.     }
  159.   /* If there are lines left to gobble, then gobble them now. */
  160.   for (line_end = line_start; line_end < file_size; line_end++)
  161.     if (buffer[line_end] == 'n')
  162.       {
  163. buffer[line_end] = '';
  164. if (buffer[line_start])
  165.   add_history (buffer + line_start);
  166. current_line++;
  167. if (current_line >= to)
  168.   break;
  169. line_start = line_end + 1;
  170.       }
  171.   FREE (input);
  172.   FREE (buffer);
  173.   return (0);
  174. }
  175. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  176.    If FNAME is NULL, then use ~/.history. */
  177. int
  178. history_truncate_file (fname, lines)
  179.      char *fname;
  180.      int lines;
  181. {
  182.   register int i;
  183.   int file, chars_read;
  184.   char *buffer, *filename;
  185.   struct stat finfo;
  186.   size_t file_size;
  187.   buffer = (char *)NULL;
  188.   filename = history_filename (fname);
  189.   file = open (filename, O_RDONLY|O_BINARY, 0666);
  190.   if (file == -1 || fstat (file, &finfo) == -1)
  191.     goto truncate_exit;
  192.   file_size = (size_t)finfo.st_size;
  193.   /* check for overflow on very large files */
  194.   if (file_size != finfo.st_size || file_size + 1 < file_size)
  195.     {
  196.       close (file);
  197. #if defined (EFBIG)
  198.       errno = EFBIG;
  199. #endif
  200.       goto truncate_exit;
  201.     }
  202.   buffer = xmalloc (file_size + 1);
  203.   chars_read = read (file, buffer, file_size);
  204.   close (file);
  205.   if (chars_read <= 0)
  206.     goto truncate_exit;
  207.   /* Count backwards from the end of buffer until we have passed
  208.      LINES lines. */
  209.   for (i = chars_read - 1; lines && i; i--)
  210.     {
  211.       if (buffer[i] == 'n')
  212. lines--;
  213.     }
  214.   /* If this is the first line, then the file contains exactly the
  215.      number of lines we want to truncate to, so we don't need to do
  216.      anything.  It's the first line if we don't find a newline between
  217.      the current value of i and 0.  Otherwise, write from the start of
  218.      this line until the end of the buffer. */
  219.   for ( ; i; i--)
  220.     if (buffer[i] == 'n')
  221.       {
  222. i++;
  223. break;
  224.       }
  225.   /* Write only if there are more lines in the file than we want to
  226.      truncate to. */
  227.   if (i && ((file = open (filename, O_WRONLY|O_TRUNC|O_BINARY, 0600)) != -1))
  228.     {
  229.       write (file, buffer + i, file_size - i);
  230. #if defined (__BEOS__)
  231.       /* BeOS ignores O_TRUNC. */
  232.       ftruncate (file, file_size - i);
  233. #endif
  234.       close (file);
  235.     }
  236.  truncate_exit:
  237.   FREE (buffer);
  238.   free (filename);
  239.   return 0;
  240. }
  241. /* Workhorse function for writing history.  Writes NELEMENT entries
  242.    from the history list to FILENAME.  OVERWRITE is non-zero if you
  243.    wish to replace FILENAME with the entries. */
  244. static int
  245. history_do_write (filename, nelements, overwrite)
  246.      char *filename;
  247.      int nelements, overwrite;
  248. {
  249.   register int i;
  250.   char *output;
  251.   int file, mode;
  252.   mode = overwrite ? O_WRONLY|O_CREAT|O_TRUNC|O_BINARY : O_WRONLY|O_APPEND|O_BINARY;
  253.   output = history_filename (filename);
  254.   if ((file = open (output, mode, 0600)) == -1)
  255.     {
  256.       FREE (output);
  257.       return (errno);
  258.     }
  259.   if (nelements > history_length)
  260.     nelements = history_length;
  261.   /* Build a buffer of all the lines to write, and write them in one syscall.
  262.      Suggested by Peter Ho (peter@robosts.oxford.ac.uk). */
  263.   {
  264.     HIST_ENTRY **the_history; /* local */
  265.     register int j;
  266.     int buffer_size;
  267.     char *buffer;
  268.     the_history = history_list ();
  269.     /* Calculate the total number of bytes to write. */
  270.     for (buffer_size = 0, i = history_length - nelements; i < history_length; i++)
  271.       buffer_size += 1 + strlen (the_history[i]->line);
  272.     /* Allocate the buffer, and fill it. */
  273.     buffer = xmalloc (buffer_size);
  274.     for (j = 0, i = history_length - nelements; i < history_length; i++)
  275.       {
  276. strcpy (buffer + j, the_history[i]->line);
  277. j += strlen (the_history[i]->line);
  278. buffer[j++] = 'n';
  279.       }
  280.     write (file, buffer, buffer_size);
  281.     free (buffer);
  282.   }
  283.   close (file);
  284.   FREE (output);
  285.   return (0);
  286. }
  287. /* Append NELEMENT entries to FILENAME.  The entries appended are from
  288.    the end of the list minus NELEMENTs up to the end of the list. */
  289. int
  290. append_history (nelements, filename)
  291.      int nelements;
  292.      char *filename;
  293. {
  294.   return (history_do_write (filename, nelements, HISTORY_APPEND));
  295. }
  296. /* Overwrite FILENAME with the current history.  If FILENAME is NULL,
  297.    then write the history list to ~/.history.  Values returned
  298.    are as in read_history ().*/
  299. int
  300. write_history (filename)
  301.      char *filename;
  302. {
  303.   return (history_do_write (filename, history_length, HISTORY_OVERWRITE));
  304. }