histexamp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* histexamp.c - history library example program. */
  2. /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
  3. This file is part of the GNU Readline Library (Readline), a library for
  4. reading lines of text with interactive input and history editing.
  5. Readline 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. Readline 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 Readline. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdio.h>
  17. #ifdef READLINE_LIBRARY
  18. # include "history.h"
  19. #else
  20. # include <readline/history.h>
  21. #endif
  22. #include <string.h>
  23. main (argc, argv)
  24. int argc;
  25. char **argv;
  26. {
  27. char line[1024], *t;
  28. int len, done;
  29. line[0] = 0;
  30. done = 0;
  31. using_history ();
  32. while (!done)
  33. {
  34. printf ("history$ ");
  35. fflush (stdout);
  36. t = fgets (line, sizeof (line) - 1, stdin);
  37. if (t && *t)
  38. {
  39. len = strlen (t);
  40. if (t[len - 1] == '\n')
  41. t[len - 1] = '\0';
  42. }
  43. if (!t)
  44. strcpy (line, "quit");
  45. if (line[0])
  46. {
  47. char *expansion;
  48. int result;
  49. using_history ();
  50. result = history_expand (line, &expansion);
  51. if (result)
  52. fprintf (stderr, "%s\n", expansion);
  53. if (result < 0 || result == 2)
  54. {
  55. free (expansion);
  56. continue;
  57. }
  58. add_history (expansion);
  59. strncpy (line, expansion, sizeof (line) - 1);
  60. free (expansion);
  61. }
  62. if (strcmp (line, "quit") == 0)
  63. done = 1;
  64. else if (strcmp (line, "save") == 0)
  65. write_history ("history_file");
  66. else if (strcmp (line, "read") == 0)
  67. read_history ("history_file");
  68. else if (strcmp (line, "list") == 0)
  69. {
  70. register HIST_ENTRY **the_list;
  71. register int i;
  72. time_t tt;
  73. char timestr[128];
  74. the_list = history_list ();
  75. if (the_list)
  76. for (i = 0; the_list[i]; i++)
  77. {
  78. tt = history_get_time (the_list[i]);
  79. if (tt)
  80. strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt));
  81. else
  82. strcpy (timestr, "??");
  83. printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line);
  84. }
  85. }
  86. else if (strncmp (line, "delete", 6) == 0)
  87. {
  88. int which;
  89. if ((sscanf (line + 6, "%d", &which)) == 1)
  90. {
  91. HIST_ENTRY *entry = remove_history (which);
  92. if (!entry)
  93. fprintf (stderr, "No such entry %d\n", which);
  94. else
  95. {
  96. free (entry->line);
  97. free (entry);
  98. }
  99. }
  100. else
  101. {
  102. fprintf (stderr, "non-numeric arg given to `delete'\n");
  103. }
  104. }
  105. }
  106. }