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
;
56 static LPHIST_ENTRY Top
;
57 static LPHIST_ENTRY Bottom
;
60 static LPHIST_ENTRY curr_ptr
=0;
63 VOID
InitHistory(VOID
);
64 VOID
History_move_to_bottom(VOID
);
65 VOID
History (INT dir
, LPTSTR commandline
);
66 VOID
CleanHistory(VOID
);
67 VOID
History_del_current_entry(LPTSTR str
);
70 static VOID
del(LPHIST_ENTRY item
);
71 static VOID
add_at_bottom(LPTSTR string
);
72 /*VOID add_before_last(LPTSTR string);*/
73 VOID
set_size(INT new_size
);
77 INT
CommandHistory (LPTSTR param
)
84 tmp
=_tcschr(param
,_T('/'));
89 switch (_totupper(param
[1]))
91 case _T('F'):/*delete history*/
92 CleanHistory();InitHistory();
95 case _T('R'):/*read history from standard in*/
96 //hIn=GetStdHandle (STD_INPUT_HANDLE);
100 ConInString(szBuffer
,sizeof(szBuffer
)/sizeof(TCHAR
));
101 if (*szBuffer
!=_T('\0'))
108 case _T('A'):/*add an antry*/
112 case _T('S'):/*set history size*/
113 if ((tmp_int
=_ttoi(param
+2)))
123 for (h_tmp
= Top
->prev
; h_tmp
!= Bottom
; h_tmp
= h_tmp
->prev
)
124 ConErrPuts(h_tmp
->string
);
129 VOID
set_size(INT new_size
)
131 while (new_size
<size
)
138 VOID
InitHistory(VOID
)
142 Top
= cmd_alloc(sizeof(HIST_ENTRY
));
143 Bottom
= cmd_alloc(sizeof(HIST_ENTRY
));
151 Bottom
->string
= NULL
;
159 VOID
CleanHistory(VOID
)
161 while (Bottom
->next
!=Top
)
169 VOID
History_del_current_entry(LPTSTR str
)
176 if (curr_ptr
== Bottom
)
177 curr_ptr
=Bottom
->next
;
184 curr_ptr
= curr_ptr
->prev
;
191 VOID
del(LPHIST_ENTRY item
)
193 if (item
==NULL
|| item
==Top
|| item
==Bottom
)
195 TRACE ("del in " __FILE__
": returning\n"
196 "item is 0x%08x (Bottom is0x%08x)\n",
203 /*free string's mem*/
205 cmd_free(item
->string
);
207 /*set links in prev and next item*/
208 item
->next
->prev
=item
->prev
;
209 item
->prev
->next
=item
->next
;
217 VOID
add_at_bottom(LPTSTR string
)
224 /*delete first entry if maximum number of entries is reached*/
225 while(size
>=max_size
)
228 while (_istspace(*string
))
231 if (*string
==_T('\0'))
235 /*if new entry is the same than the last do not add it*/
237 if(_tcscmp(string
,Bottom
->next
->string
)==0)
241 /*fill bottom with string, it will become Bottom->next*/
242 Bottom
->string
=cmd_alloc((_tcslen(string
)+1)*sizeof(TCHAR
));
243 _tcscpy(Bottom
->string
,string
);
245 /*save Bottom value*/
249 /*create new void Bottom*/
250 Bottom
=cmd_alloc(sizeof(HIST_ENTRY
));
264 VOID
History_move_to_bottom(VOID
)
270 LPCTSTR
PeekHistory(INT dir
)
272 LPHIST_ENTRY entry
= curr_ptr
;
280 if (entry
->next
== Top
|| entry
== Top
)
293 if (entry
->next
== Bottom
|| entry
== Bottom
)
304 return entry
->string
;
307 VOID
History (INT dir
, LPTSTR commandline
)
312 add_at_bottom(commandline
);
319 commandline
[0]=_T('\0');
326 if (curr_ptr
->next
==Top
|| curr_ptr
==Top
)
332 commandline
[0]=_T('\0');
338 curr_ptr
= curr_ptr
->next
;
340 _tcscpy(commandline
,curr_ptr
->string
);
351 if (curr_ptr
->prev
==Bottom
|| curr_ptr
==Bottom
)
357 commandline
[0]=_T('\0');
362 curr_ptr
=curr_ptr
->prev
;
364 _tcscpy(commandline
,curr_ptr
->string
);
369 #endif //#if FEATURE_HISTORY