-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecuicmhist.c
181 lines (155 loc) · 4.32 KB
/
ecuicmhist.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
/*+-------------------------------------------------------------------------
ecuicmhist.c - ECU interactive command history handler
wht@wht.net
Defined functions:
icmd_history_add(icmd_buf)
icmd_history_manager(func, newicmd, icmdsize)
I met this girl in Macy's. She was shopping, and I was putting
slinkeys on the escalator. -- Steven Wright
--------------------------------------------------------------------------*/
/*+:EDITS:*/
/*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
/*:01-08-2000-14:08-wht@menlo-strdup can be a macro */
/*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
/*:09-11-1996-20:00-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
/*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
/*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
/*:01-12-1995-15:19-wht@n4hgf-apply Andrew Chernov 8-bit clean+FreeBSD patch */
/*:05-04-1994-04:38-wht@n4hgf-ECU release 3.30 */
/*:09-13-1992-02:05-wht@n4hgf-redisplay escape prompt on error exit */
/*:09-10-1992-13:58-wht@n4hgf-ECU release 3.20 */
/*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
/*:08-28-1991-14:07-wht@n4hgf2-SVR4 cleanup by aega84!lh */
/*:08-11-1991-14:58-wht@n4hgf-new ttygets botched command history handler */
/*:07-25-1991-12:56-wht@n4hgf-ECU release 3.10 */
/*:07-14-1991-18:18-wht@n4hgf-new ttygets functions */
/*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
#include <string.h>
#include "ecucurses.h"
#define STDIO_H_INCLUDED
#define OMIT_TERMIO_REFERENCES
#include "ecu.h"
#include "pc_scr.h"
#include "ecukey.h"
#include "ecuxkey.h"
#include "ecutty.h"
#define ICMDH_MAXCNT 50
#define ICMDH_MAXLEN 72
typedef struct icmd_hist
{
struct icmd_hist *prev;
struct icmd_hist *next;
uchar *icmd;
} ICMDH;
ICMDH *icmdh_head = (ICMDH *) 0;
ICMDH *icmdh_tail = (ICMDH *) 0;
int icmdh_count = 0;
/*+-------------------------------------------------------------------------
icmd_history_add(icmd_buf)
--------------------------------------------------------------------------*/
void
icmd_history_add(icmd_buf)
char *icmd_buf;
{
ICMDH *icmdh = (ICMDH *) malloc(sizeof(ICMDH));
if (!icmdh)
return;
if (!(icmdh->icmd = (uchar *) strdup(icmd_buf)))
{
free((char *)icmdh);
return;
}
if (strlen((char *)icmdh->icmd) > (unsigned)ICMDH_MAXLEN)
icmdh->icmd[ICMDH_MAXLEN] = 0;
if (icmdh_tail)
{
icmdh_tail->next = icmdh;
icmdh->prev = icmdh_tail;
icmdh->next = (ICMDH *) 0;
icmdh_tail = icmdh;
}
else
{
icmdh->prev = (ICMDH *) 0;
icmdh->next = (ICMDH *) 0;
icmdh_head = icmdh;
icmdh_tail = icmdh;
}
if (++icmdh_count > ICMDH_MAXCNT)
{
icmdh = icmdh_head;
icmdh_head = icmdh->next;
icmdh_head->prev = (ICMDH *) 0;
free((char *)icmdh->icmd);
free((char *)icmdh);
icmdh_count--;
}
} /* end of icmd_history_add */
/*+-------------------------------------------------------------------------
icmd_history_manager(func,newicmd,icmdsize) - entered by Home Xkey
return new icmd string to execute
returns 0 if ok to exce new cmd, else 1 if not
(returns 0 if null or ESC, so caller can handle exit condition)
--------------------------------------------------------------------------*/
/*ARGSUSED*/
int
icmd_history_manager(func, newicmd, icmdsize)
uchar func;
uchar *newicmd;
int icmdsize;
{
int itmp;
ICMDH *icmdh = icmdh_tail;
UINT delim;
if (!icmdh)
{
ff(se, " no interactive commands saved\r\n");
show_escape_prompt();
return (1);
}
while (1)
{
strncpy((char *)newicmd, (char *)icmdh->icmd, icmdsize - 1);
*(newicmd + icmdsize - 1) = 0;
ttygets(newicmd, icmdsize, TG_XDELIM | TG_EDIT, &delim, (int *)0);
if (!newicmd[0])
return (0);
switch (delim)
{
case ESC:
*newicmd = 0;
return (0);
case XFhome:
icmdh = icmdh_head;
break;
case XFend:
icmdh = icmdh_tail;
break;
case XFpgup:
case XFpgdn:
ring_bell();
break;
case XFcurup:
if (icmdh->prev)
icmdh = icmdh->prev;
break;
case XFcurdn:
if (icmdh->next)
icmdh = icmdh->next;
break;
default:
return (0);
}
itmp = strlen((char *)newicmd);
while (itmp--)
fputc(BS, se);
itmp = strlen((char *)newicmd);
while (itmp--)
fputc(' ', se);
itmp = strlen((char *)newicmd);
while (itmp--)
fputc(BS, se);
}
} /* end of icmd_history_manager */
/* vi: set tabstop=4 shiftwidth=4: */
/* end of ecuicmhist.c */