2 * HISTORY.C - command line history.
7 * 14/01/95 (Tim Norman)
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
14 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
15 * added config.h include
17 * 25-Jan-1999 (Eric Kohl)
19 * Unicode and redirection safe!
21 * 25-Jan-1999 (Paolo Pantaleo <paolopan@freemail.it>)
22 * Added lots of comments (beginning studying the source)
23 * Added command.com's F3 support (see cmdinput.c)
30 * HISTORY.C - command line history. Second version
35 * 06/12/99 (Paolo Pantaleo <paolopan@freemail.it>)
42 #ifdef FEATURE_HISTORY
44 typedef struct tagHISTORY
46 struct tagHISTORY
*prev
;
47 struct tagHISTORY
*next
;
49 } HIST_ENTRY
, * LPHIST_ENTRY
;
51 static INT size
, max_size
=100;
53 static LPHIST_ENTRY Top
;
54 static LPHIST_ENTRY Bottom
;
56 static LPHIST_ENTRY curr_ptr
=0;
58 VOID
InitHistory(VOID
);
59 VOID
History_move_to_bottom(VOID
);
60 VOID
History (INT dir
, LPTSTR commandline
);
61 VOID
CleanHistory(VOID
);
62 VOID
History_del_current_entry(LPTSTR str
);
65 static VOID
del(LPHIST_ENTRY item
);
66 static VOID
add_at_bottom(LPTSTR string
);
67 /*VOID add_before_last(LPTSTR string);*/
68 VOID
set_size(INT new_size
);
71 INT
CommandHistory (LPTSTR param
)
78 tmp
=_tcschr(param
,_T('/'));
83 switch (_totupper(param
[1]))
85 case _T('F'):/*delete history*/
86 CleanHistory();InitHistory();
89 case _T('R'):/*read history from standard in*/
92 ConInString(szBuffer
,sizeof(szBuffer
)/sizeof(TCHAR
));
93 if (*szBuffer
!=_T('\0'))
100 case _T('A'):/*add an antry*/
104 case _T('S'):/*set history size*/
105 if ((tmp_int
=_ttoi(param
+2)))
115 for (h_tmp
= Top
->prev
; h_tmp
!= Bottom
; h_tmp
= h_tmp
->prev
)
116 ConErrPuts(h_tmp
->string
);
121 VOID
set_size(INT new_size
)
123 while (new_size
<size
)
130 VOID
InitHistory(VOID
)
134 Top
= cmd_alloc(sizeof(HIST_ENTRY
));
135 Bottom
= cmd_alloc(sizeof(HIST_ENTRY
));
143 Bottom
->string
= NULL
;
149 VOID
CleanHistory(VOID
)
151 while (Bottom
->next
!=Top
)
159 VOID
History_del_current_entry(LPTSTR str
)
166 if (curr_ptr
== Bottom
)
167 curr_ptr
=Bottom
->next
;
174 curr_ptr
= curr_ptr
->prev
;
181 VOID
del(LPHIST_ENTRY item
)
183 if (item
==NULL
|| item
==Top
|| item
==Bottom
)
185 TRACE ("del in " __FILE__
": returning\n"
186 "item is 0x%08x (Bottom is0x%08x)\n",
191 /*free string's mem*/
193 cmd_free(item
->string
);
195 /*set links in prev and next item*/
196 item
->next
->prev
=item
->prev
;
197 item
->prev
->next
=item
->next
;
205 VOID
add_at_bottom(LPTSTR string
)
209 /*delete first entry if maximum number of entries is reached*/
210 while(size
>=max_size
)
213 while (_istspace(*string
))
216 if (*string
==_T('\0'))
219 /*if new entry is the same than the last do not add it*/
221 if (_tcscmp(string
,Bottom
->next
->string
)==0)
224 /*fill bottom with string, it will become Bottom->next*/
225 Bottom
->string
=cmd_alloc((_tcslen(string
)+1)*sizeof(TCHAR
));
226 _tcscpy(Bottom
->string
,string
);
228 /*save Bottom value*/
231 /*create new void Bottom*/
232 Bottom
=cmd_alloc(sizeof(HIST_ENTRY
));
244 VOID
History_move_to_bottom(VOID
)
249 LPCTSTR
PeekHistory(INT dir
)
251 LPHIST_ENTRY entry
= curr_ptr
;
259 if (entry
->next
== Top
|| entry
== Top
)
272 if (entry
->next
== Bottom
|| entry
== Bottom
)
283 return entry
->string
;
286 VOID
History (INT dir
, LPTSTR commandline
)
290 add_at_bottom(commandline
);
297 commandline
[0]=_T('\0');
303 if (curr_ptr
->next
==Top
|| curr_ptr
==Top
)
309 commandline
[0]=_T('\0');
314 curr_ptr
= curr_ptr
->next
;
315 if (curr_ptr
->string
)
316 _tcscpy(commandline
,curr_ptr
->string
);
321 if (curr_ptr
->prev
==Bottom
|| curr_ptr
==Bottom
)
327 commandline
[0]=_T('\0');
332 curr_ptr
=curr_ptr
->prev
;
333 if (curr_ptr
->string
)
334 _tcscpy(commandline
,curr_ptr
->string
);
338 #endif //#if FEATURE_HISTORY