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*/
90 //hIn=GetStdHandle (STD_INPUT_HANDLE);
94 ConInString(szBuffer
,sizeof(szBuffer
)/sizeof(TCHAR
));
95 if (*szBuffer
!=_T('\0'))
102 case _T('A'):/*add an antry*/
106 case _T('S'):/*set history size*/
107 if ((tmp_int
=_ttoi(param
+2)))
117 for (h_tmp
= Top
->prev
; h_tmp
!= Bottom
; h_tmp
= h_tmp
->prev
)
118 ConErrPuts(h_tmp
->string
);
123 VOID
set_size(INT new_size
)
125 while (new_size
<size
)
132 VOID
InitHistory(VOID
)
136 Top
= cmd_alloc(sizeof(HIST_ENTRY
));
137 Bottom
= cmd_alloc(sizeof(HIST_ENTRY
));
145 Bottom
->string
= NULL
;
151 VOID
CleanHistory(VOID
)
153 while (Bottom
->next
!=Top
)
161 VOID
History_del_current_entry(LPTSTR str
)
168 if (curr_ptr
== Bottom
)
169 curr_ptr
=Bottom
->next
;
176 curr_ptr
= curr_ptr
->prev
;
183 VOID
del(LPHIST_ENTRY item
)
185 if (item
==NULL
|| item
==Top
|| item
==Bottom
)
187 TRACE ("del in " __FILE__
": returning\n"
188 "item is 0x%08x (Bottom is0x%08x)\n",
193 /*free string's mem*/
195 cmd_free(item
->string
);
197 /*set links in prev and next item*/
198 item
->next
->prev
=item
->prev
;
199 item
->prev
->next
=item
->next
;
207 VOID
add_at_bottom(LPTSTR string
)
211 /*delete first entry if maximum number of entries is reached*/
212 while(size
>=max_size
)
215 while (_istspace(*string
))
218 if (*string
==_T('\0'))
221 /*if new entry is the same than the last do not add it*/
223 if (_tcscmp(string
,Bottom
->next
->string
)==0)
226 /*fill bottom with string, it will become Bottom->next*/
227 Bottom
->string
=cmd_alloc((_tcslen(string
)+1)*sizeof(TCHAR
));
228 _tcscpy(Bottom
->string
,string
);
230 /*save Bottom value*/
233 /*create new void Bottom*/
234 Bottom
=cmd_alloc(sizeof(HIST_ENTRY
));
246 VOID
History_move_to_bottom(VOID
)
251 LPCTSTR
PeekHistory(INT dir
)
253 LPHIST_ENTRY entry
= curr_ptr
;
261 if (entry
->next
== Top
|| entry
== Top
)
274 if (entry
->next
== Bottom
|| entry
== Bottom
)
285 return entry
->string
;
288 VOID
History (INT dir
, LPTSTR commandline
)
292 add_at_bottom(commandline
);
299 commandline
[0]=_T('\0');
305 if (curr_ptr
->next
==Top
|| curr_ptr
==Top
)
311 commandline
[0]=_T('\0');
316 curr_ptr
= curr_ptr
->next
;
317 if (curr_ptr
->string
)
318 _tcscpy(commandline
,curr_ptr
->string
);
323 if (curr_ptr
->prev
==Bottom
|| curr_ptr
==Bottom
)
329 commandline
[0]=_T('\0');
334 curr_ptr
=curr_ptr
->prev
;
335 if (curr_ptr
->string
)
336 _tcscpy(commandline
,curr_ptr
->string
);
340 #endif //#if FEATURE_HISTORY