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 <ekohl@abo.rhein-zeitung.de>)
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 cmd
, 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
= malloc(sizeof(HIST_ENTRY
));
143 Bottom
= malloc(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
)
196 DebugPrintf(_T("del in " __FILE__
": retrning\n"
197 "item is 0x%08x (Bottom is0x%08x)\n"),
205 /*free string's mem*/
209 /*set links in prev and next item*/
210 item
->next
->prev
=item
->prev
;
211 item
->prev
->next
=item
->next
;
221 VOID
add_before_last(LPTSTR string
)
223 LPHIST_ENTRY tmp
,before
,after
;
225 /*delete first entry if maximum number of entries is reached*/
226 while(size
>=max_size
)
229 while (_istspace(*string
))
232 if (*string
==_T('\0'))
235 /*allocte entry and string*/
236 tmp
=malloc(sizeof(HIST_ENTRY
));
237 tmp
->string
=malloc((_tcslen(string
)+1)*sizeof(TCHAR
));
238 _tcscpy(tmp
->string
,string
);
258 VOID
add_at_bottom(LPTSTR string
)
265 /*delete first entry if maximum number of entries is reached*/
266 while(size
>=max_size
)
269 while (_istspace(*string
))
272 if (*string
==_T('\0'))
276 /*if new entry is the same than the last do not add it*/
278 if(_tcscmp(string
,Bottom
->next
->string
)==0)
282 /*fill bottom with string, it will become Bottom->next*/
283 Bottom
->string
=malloc((_tcslen(string
)+1)*sizeof(TCHAR
));
284 _tcscpy(Bottom
->string
,string
);
286 /*save Bottom value*/
290 /*create new void Bottom*/
291 Bottom
=malloc(sizeof(HIST_ENTRY
));
305 VOID
History_move_to_bottom(VOID
)
312 VOID
History (INT dir
, LPTSTR commandline
)
317 add_at_bottom(commandline
);
324 commandline
[0]=_T('\0');
331 if (curr_ptr
->next
==Top
|| curr_ptr
==Top
)
337 commandline
[0]=_T('\0');
343 curr_ptr
= curr_ptr
->next
;
345 _tcscpy(commandline
,curr_ptr
->string
);
356 if (curr_ptr
->prev
==Bottom
|| curr_ptr
==Bottom
)
362 commandline
[0]=_T('\0');
367 curr_ptr
=curr_ptr
->prev
;
369 _tcscpy(commandline
,curr_ptr
->string
);
381 LPTSTR history
= NULL
; /*buffer to sotre all the lines*/
382 LPTSTR lines
[MAXLINES
]; /*array of pointers to each line(entry)*/
383 /*located in history buffer*/
385 INT curline
= 0; /*the last line recalled by user*/
386 INT numlines
= 0; /*number of entries, included the last*/
389 INT maxpos
= 0; /*index of last byte of last entry*/
393 VOID
History (INT dir
, LPTSTR commandline
)
396 INT count
; /*used in for loops*/
397 INT length
; /*used in the same loops of count*/
398 /*both to make room when is full
399 either history or lines*/
401 /*first time History is called allocate mem*/
404 history
= malloc (history_size
* sizeof (TCHAR
));
412 if (curline
< numlines
)
417 if (curline
== numlines
)
423 _tcscpy (commandline
, lines
[curline
]);
434 _tcscpy (commandline
, lines
[curline
]);
439 /* remove oldest string until there's enough room for next one */
440 /* strlen (commandline) must be less than history_size! */
441 while ((maxpos
+ (INT
)_tcslen (commandline
) + 1 > history_size
) || (numlines
>= MAXLINES
))
443 length
= _tcslen (lines
[0]) + 1;
445 for (count
= 0; count
< maxpos
&& count
+ (lines
[1] - lines
[0]) < history_size
; count
++)
447 history
[count
] = history
[count
+ length
];
452 for (count
= 0; count
<= numlines
&& count
< MAXLINES
; count
++)
454 lines
[count
] = lines
[count
+ 1] - length
;
459 ConOutPrintf (_T("Reduced size: %ld lines\n"), numlines
);
461 for (count
= 0; count
< numlines
; count
++)
463 ConOutPrintf (_T("%d: %s\n"), count
, lines
[count
]);
468 /*copy entry in the history bufer*/
469 _tcscpy (lines
[numlines
], commandline
);
472 /*set last lines[numlines] pointer next the end of last, valid,
473 just setted entry (the two lines above)*/
474 lines
[numlines
] = lines
[numlines
- 1] + _tcslen (commandline
) + 1;
475 maxpos
+= _tcslen (commandline
) + 1;
476 /* last line, empty */