1/* Copyright (C) 1987-2002 Free Software Foundation, Inc. 2 3 This file is part of the GNU Readline Library, a library for 4 reading lines of text with interactive input and history editing. 5 6 The GNU Readline Library is free software; you can redistribute it 7 and/or modify it under the terms of the GNU General Public License 8 as published by the Free Software Foundation; either version 2, or 9 (at your option) any later version. 10 11 The GNU Readline Library is distributed in the hope that it will be 12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 The GNU General Public License is often shipped with GNU software, and 17 is generally kept in a file called COPYING or LICENSE. If you do not 18 have a copy of the license, write to the Free Software Foundation, 19 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 20 21#include <stdio.h> 22 23#ifdef READLINE_LIBRARY 24# include "history.h" 25#else 26# include <readline/history.h> 27#endif 28
| 1/* Copyright (C) 1987-2002 Free Software Foundation, Inc. 2 3 This file is part of the GNU Readline Library, a library for 4 reading lines of text with interactive input and history editing. 5 6 The GNU Readline Library is free software; you can redistribute it 7 and/or modify it under the terms of the GNU General Public License 8 as published by the Free Software Foundation; either version 2, or 9 (at your option) any later version. 10 11 The GNU Readline Library is distributed in the hope that it will be 12 useful, but WITHOUT ANY WARRANTY; without even the implied warranty 13 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 The GNU General Public License is often shipped with GNU software, and 17 is generally kept in a file called COPYING or LICENSE. If you do not 18 have a copy of the license, write to the Free Software Foundation, 19 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ 20 21#include <stdio.h> 22 23#ifdef READLINE_LIBRARY 24# include "history.h" 25#else 26# include <readline/history.h> 27#endif 28
|
29main (argc, argv) 30 int argc; 31 char **argv; 32{ 33 char line[1024], *t; 34 int len, done; 35 36 line[0] = 0; 37 done = 0; 38 39 using_history (); 40 while (!done) 41 { 42 printf ("history$ "); 43 fflush (stdout); 44 t = fgets (line, sizeof (line) - 1, stdin); 45 if (t && *t) 46 { 47 len = strlen (t); 48 if (t[len - 1] == '\n') 49 t[len - 1] = '\0'; 50 } 51 52 if (!t) 53 strcpy (line, "quit"); 54 55 if (line[0]) 56 { 57 char *expansion; 58 int result; 59 60 using_history (); 61 62 result = history_expand (line, &expansion); 63 if (result) 64 fprintf (stderr, "%s\n", expansion); 65 66 if (result < 0 || result == 2) 67 { 68 free (expansion); 69 continue; 70 } 71 72 add_history (expansion); 73 strncpy (line, expansion, sizeof (line) - 1); 74 free (expansion); 75 } 76 77 if (strcmp (line, "quit") == 0) 78 done = 1; 79 else if (strcmp (line, "save") == 0) 80 write_history ("history_file"); 81 else if (strcmp (line, "read") == 0) 82 read_history ("history_file"); 83 else if (strcmp (line, "list") == 0) 84 { 85 register HIST_ENTRY **the_list; 86 register int i; 87 time_t tt; 88 char timestr[128]; 89 90 the_list = history_list (); 91 if (the_list) 92 for (i = 0; the_list[i]; i++) 93 { 94 tt = history_get_time (the_list[i]); 95 if (tt) 96 strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt)); 97 else 98 strcpy (timestr, "??"); 99 printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line); 100 } 101 } 102 else if (strncmp (line, "delete", 6) == 0) 103 { 104 int which; 105 if ((sscanf (line + 6, "%d", &which)) == 1) 106 { 107 HIST_ENTRY *entry = remove_history (which); 108 if (!entry) 109 fprintf (stderr, "No such entry %d\n", which); 110 else 111 { 112 free (entry->line); 113 free (entry); 114 } 115 } 116 else 117 { 118 fprintf (stderr, "non-numeric arg given to `delete'\n"); 119 } 120 } 121 } 122}
| 31main (argc, argv) 32 int argc; 33 char **argv; 34{ 35 char line[1024], *t; 36 int len, done; 37 38 line[0] = 0; 39 done = 0; 40 41 using_history (); 42 while (!done) 43 { 44 printf ("history$ "); 45 fflush (stdout); 46 t = fgets (line, sizeof (line) - 1, stdin); 47 if (t && *t) 48 { 49 len = strlen (t); 50 if (t[len - 1] == '\n') 51 t[len - 1] = '\0'; 52 } 53 54 if (!t) 55 strcpy (line, "quit"); 56 57 if (line[0]) 58 { 59 char *expansion; 60 int result; 61 62 using_history (); 63 64 result = history_expand (line, &expansion); 65 if (result) 66 fprintf (stderr, "%s\n", expansion); 67 68 if (result < 0 || result == 2) 69 { 70 free (expansion); 71 continue; 72 } 73 74 add_history (expansion); 75 strncpy (line, expansion, sizeof (line) - 1); 76 free (expansion); 77 } 78 79 if (strcmp (line, "quit") == 0) 80 done = 1; 81 else if (strcmp (line, "save") == 0) 82 write_history ("history_file"); 83 else if (strcmp (line, "read") == 0) 84 read_history ("history_file"); 85 else if (strcmp (line, "list") == 0) 86 { 87 register HIST_ENTRY **the_list; 88 register int i; 89 time_t tt; 90 char timestr[128]; 91 92 the_list = history_list (); 93 if (the_list) 94 for (i = 0; the_list[i]; i++) 95 { 96 tt = history_get_time (the_list[i]); 97 if (tt) 98 strftime (timestr, sizeof (timestr), "%a %R", localtime(&tt)); 99 else 100 strcpy (timestr, "??"); 101 printf ("%d: %s: %s\n", i + history_base, timestr, the_list[i]->line); 102 } 103 } 104 else if (strncmp (line, "delete", 6) == 0) 105 { 106 int which; 107 if ((sscanf (line + 6, "%d", &which)) == 1) 108 { 109 HIST_ENTRY *entry = remove_history (which); 110 if (!entry) 111 fprintf (stderr, "No such entry %d\n", which); 112 else 113 { 114 free (entry->line); 115 free (entry); 116 } 117 } 118 else 119 { 120 fprintf (stderr, "non-numeric arg given to `delete'\n"); 121 } 122 } 123 } 124}
|