1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/* history.c
* Part of the Readline module for Tap. This is a simpler
* implementation of the GNU History library.
*/
/* Copyright (C) 1999 Jonathan duSaint
*
* This file is part of Tap, an interactive program for reading and
* writing data to the serial port to facilitate the reverse
* engineering of communication protocols.
*
* Tap is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* Tap is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License is generally kept in a file called
* COPYING or LICENSE. If you do not have a copy of the license, write
* to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "readline.h"
/* the history list itself
* The first node is not dynamically allocated. That way, no
* initialization function needs to be called.
*/
struct history_node the_history_list = { NULL, NULL, NULL, NULL };
/* pointers into the history list */
/* the last item in the history list */
struct history_node *last_history = &the_history_list;
/* the current item when the list is being traversed */
struct history_node *current_history = &the_history_list;
/* the most current line (which isn't itself on the history list) */
char *saved_line = NULL;
/* Get the next entry on the history list. If there isn't another node,
* return the saved line, or if that is the current line, return NULL.
*/
char *
next_history (void)
{
char *line_to_return;
/* already at the last entry */
if (current_history == last_history)
line_to_return = NULL;
else
{
current_history = current_history->next;
/* at the last entry, but still not on the most current line */
if (current_history == last_history)
{
line_to_return = saved_line;
saved_line = NULL;
}
else
{
/* (re)allocate scratch area if necessary */
if (current_history->saved_data == NULL)
{
current_history->saved_data =
xmalloc (ROUND_UP_SIZE (strlen (current_history->data)));
strcpy (current_history->saved_data, current_history->data);
}
line_to_return = current_history->saved_data;
}
}
return (line_to_return);
}
/* Get the previous line on the history list. If the current node is
* the first one, return NULL.
*/
char *
prev_history (void)
{
char *line_to_return;
/* at the oldest entry already */
if (current_history == &the_history_list)
line_to_return = NULL;
else
{
/* about to start traversing the list proper --
* need to save the most recent line
*/
if (current_history == last_history)
saved_line = current_line;
current_history = current_history->prev;
/* (re)allocate scratch area if necessary */
if (current_history->saved_data == NULL)
{
current_history->saved_data =
xmalloc (ROUND_UP_SIZE (strlen (current_history->data)));
strcpy (current_history->saved_data, current_history->data);
}
line_to_return = current_history->saved_data;
}
return (line_to_return);
}
/* Add an entry into the history list. Don't save empty lines or
* lines that are exactly the same as the line immediately above.
*/
void
add_history (char *line)
{
/* don't save empty lines */
if (strlen (line) == 0) return;
/* if this is a modification of an earlier line, restore the original */
if (current_history != last_history) current_history->saved_data = NULL;
if (last_history->prev != NULL)
{
/* don't save copies */
if (!strcmp (last_history->prev->data, line))
{
current_history = last_history;
return;
}
}
/* save the line in the list */
last_history->data = xmalloc (ROUND_UP_SIZE (strlen (line)));
strcpy (last_history->data, line);
/* create a new node ... */
last_history->next = xmalloc (sizeof (struct history_node));
/* ... and fill it in */
last_history->next->data = NULL;
last_history->next->saved_data = NULL;
last_history->next->prev = last_history;
last_history->next->next = NULL;
/* update all pointers */
last_history = last_history->next;
current_history = last_history;
}
/* Expand a path if it contains a tilde */
char *
tilde_expand (char *file)
{
char *expansion, *home;
home = getenv ("HOME");
if (file[0] == '~' && home != NULL)
{
expansion = xmalloc (strlen(file) + strlen(home));
sprintf (expansion, "%s%s", home, file + 1);
}
else
{
/* If there is no HOME or ~ is not in the string, just
* return a pointer which can be freed.
*/
expansion = xmalloc (strlen(file) + 1);
strcpy (expansion, file);
}
return (expansion);
}
/* save the history list to a file -- usually ~/.tap_history */
void
save_history_to_file (char *file)
{
char *path;
FILE *fd;
path = tilde_expand (file);
fd = fopen (path, "wt");
if (fd == NULL) return;
current_history = &the_history_list;
while (current_history != last_history)
{
fprintf (fd, "%s\n", current_history->data);
current_history = current_history->next;
}
fclose (fd);
xfree (path);
}
#define MAX_LINE_SIZE 1024
void
read_history_from_file (char *file)
{
FILE *fd;
char *line, *path;
line = xmalloc (MAX_LINE_SIZE);
path = tilde_expand (file);
fd = fopen (path, "rt");
if (fd == NULL) return;
while (fgets (line, MAX_LINE_SIZE, fd) != NULL)
{
if (line[strlen (line) - 1] == '\n') line[strlen (line) - 1] = '\0';
add_history (line);
}
fclose (fd);
xfree (line);
xfree (path);
}
|