/
history.c
231 lines (198 loc) · 5.73 KB
/
history.c
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
/* HISTORY.C (c) Copyright Roger Bowler, 1999-2011 */
/* Hercules Command History Processes */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
// $Id$
#include "hstdinc.h"
#include "hercules.h"
#include "history.h"
#define HISTORY_MAX 10
#define CMD_SIZE 256 /* 32767 is way toooooo much */
typedef struct history {
int number;
char *cmdline;
struct history *prev;
struct history *next;
} HISTORY;
BYTE history_count; /* for line numbering */
HISTORY *history_lines; /* points to the beginning of history list (oldest command) */
HISTORY *history_lines_end; /* points to the end of history list (most recent command) */
HISTORY *history_ptr; /* points to last command retrieved by key press */
HISTORY *backup; /* used for backuping last removed command */
/* these 2 are used in panel.c to see if there was history command requested and
returns that command */
char *historyCmdLine;
int history_requested = 0;
void copy_to_historyCmdLine(char* cmdline)
{
if (historyCmdLine) free(historyCmdLine);
historyCmdLine = malloc(strlen(cmdline)+1);
strlcpy(historyCmdLine, cmdline, strlen(cmdline)+1 );
}
/* initialize environment */
int history_init() {
history_lines = NULL;
history_lines_end = NULL;
historyCmdLine = (char *) malloc(255);
history_requested = 0;
backup = NULL;
history_count = 0;
history_ptr = NULL;
return(0);
}
/* add commandline to history list */
int history_add(char *cmdline) {
HISTORY *tmp;
/* if there is some backup line remaining, remove it */
if (backup != NULL) {
free(backup->cmdline);
free(backup);
backup = NULL;
}
/* If last line is exactly the same as this line
ignore and return to caller */
if ( history_lines != NULL && !strcmp(cmdline,history_lines_end->cmdline) )
{
history_ptr = NULL;
return 0;
}
/* allocate space and copy string */
tmp = (HISTORY*) malloc(sizeof(HISTORY));
tmp->cmdline = (char*) malloc(strlen(cmdline) + 1);
strlcpy(tmp->cmdline, cmdline,strlen(cmdline)+1);
tmp->next = NULL;
tmp->prev = NULL;
tmp->number = ++history_count;
if (history_lines == NULL) {
/* first in list */
history_lines = tmp;
history_lines_end = tmp;
}
else {
tmp->prev = history_lines_end;
history_lines_end->next = tmp;
history_lines_end = tmp;
}
history_ptr = NULL;
if (history_count > HISTORY_MAX) {
/* if we are over maximum number of lines in history list, oldest one should be deleted
but we don't know whether history_remove will not be called, so oldest line is backuped and not removed */
backup = history_lines;
history_lines = history_lines->next;
backup->next = NULL;
history_lines->prev = NULL;
}
return(0);
}
/* show list of lines in history */
int history_show() {
HISTORY *tmp;
tmp = history_lines;
while (tmp != NULL) {
WRMSG(HHC02273, "I", tmp->number, tmp->cmdline);
tmp = tmp->next;
}
return(0);
}
/* remove last line from history list (called only when history command was invoked) */
int history_remove() {
HISTORY *tmp;
if (history_lines == NULL)
return(0);
if (history_lines == history_lines_end) {
ASSERT(history_lines->next == NULL);
ASSERT(history_count == 1);
free(history_lines->cmdline);
free(history_lines);
history_lines = NULL;
history_lines_end = NULL;
history_count--;
return(0);
}
tmp = history_lines_end->prev;
tmp->next = NULL;
free(history_lines_end->cmdline);
free(history_lines_end);
history_count--;
history_lines_end = tmp;
if (backup != NULL) {
backup->next = history_lines;
history_lines->prev = backup;
history_lines = backup;
backup = NULL;
}
return(0);
}
int history_relative_line(int x) {
HISTORY *tmp = history_lines_end;
char buf[80];
if (-x > HISTORY_MAX) {
MSGBUF(buf, "History limited to last %d commands", HISTORY_MAX);
WRMSG(HHC02293, "I", buf);
return (-1);
}
if (-x > history_count) {
MSGBUF(buf, "Only %d commands in history", history_count);
WRMSG(HHC02293, "I", buf);
return (-1);
}
while (x<-1) {
tmp = tmp->prev;
x++;
}
copy_to_historyCmdLine(tmp->cmdline);
history_ptr = NULL;
return(0);
}
int history_absolute_line(int x) {
HISTORY *tmp = history_lines_end;
int lowlimit;
char buf[80];
if (history_count == 0) {
WRMSG(HHC02293, "I", "History empty");
return -1;
}
lowlimit = history_count - HISTORY_MAX;
if (x > history_count || x <= lowlimit) {
MSGBUF(buf, "Only commands %d-%d are in history", lowlimit<0? 1 : lowlimit + 1, history_count);
WRMSG(HHC02293, "I", buf);
return (-1);
}
while (tmp->number != x)
tmp = tmp->prev;
copy_to_historyCmdLine(tmp->cmdline);
history_ptr = NULL;
return(0);
}
int history_next() {
if (history_ptr == NULL) {
history_ptr = history_lines_end;
if (history_ptr == NULL)
return(-1);
copy_to_historyCmdLine(history_ptr->cmdline);
return(0);
}
if (history_ptr->next == NULL)
history_ptr = history_lines;
else
history_ptr = history_ptr->next;
copy_to_historyCmdLine(history_ptr->cmdline);
return(0);
}
int history_prev() {
if (history_ptr == NULL) {
history_ptr = history_lines_end;
if (history_ptr == NULL)
return(-1);
copy_to_historyCmdLine(history_ptr->cmdline);
return(0);
}
if (history_ptr->prev == NULL)
history_ptr = history_lines_end;
else
history_ptr = history_ptr->prev;
copy_to_historyCmdLine(history_ptr->cmdline);
return(0);
}