history command added and some minor changes to history interface

svn path=/trunk/; revision=976
This commit is contained in:
Paolo Pantaleo 2000-02-01 18:29:11 +00:00
parent 33258c166f
commit 16ef6ef2af
4 changed files with 395 additions and 40 deletions

View file

@ -1,4 +1,4 @@
/* $Id: cmd.h,v 1.18 1999/12/28 23:06:35 ekohl Exp $ /* $Id: cmd.h,v 1.19 2000/02/01 18:29:11 paolopan Exp $
* *
* CMD.H - header file for the modules in CMD.EXE * CMD.H - header file for the modules in CMD.EXE
* *
@ -252,6 +252,7 @@ VOID History_move_to_bottom(VOID);/*F3*/
VOID InitHistory(VOID); VOID InitHistory(VOID);
VOID CleanHistory(VOID); VOID CleanHistory(VOID);
VOID History_del_current_entry(LPTSTR str);/*CTRL-D*/ VOID History_del_current_entry(LPTSTR str);/*CTRL-D*/
INT CommandHistory (LPTSTR cmd, LPTSTR param);
#endif #endif

View file

@ -139,6 +139,7 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
INPUT_RECORD ir; INPUT_RECORD ir;
WORD wLastKey = 0; WORD wLastKey = 0;
TCHAR ch; TCHAR ch;
BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/
/* get screen size */ /* get screen size */
GetScreenSize (&maxx, &maxy); GetScreenSize (&maxx, &maxy);
@ -157,6 +158,57 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
{ {
ConInKey (&ir); ConInKey (&ir);
if (ir.Event.KeyEvent.dwControlKeyState &
(RIGHT_ALT_PRESSED|RIGHT_ALT_PRESSED|
RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) )
{
switch (ir.Event.KeyEvent.wVirtualKeyCode)
{
#ifdef FEATURE_HISTORY
case 'K':
/*add the current command line to the history*/
if (ir.Event.KeyEvent.dwControlKeyState &
(LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED))
{
if (str[0])
History(0,str);
ClearCommandLine (str, maxlen, orgx, orgy);
current = charcount = 0;
bContinue=TRUE;
break;
}
case 'D':
/*delete current history entry*/
if (ir.Event.KeyEvent.dwControlKeyState &
(LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED))
{
ClearCommandLine (str, maxlen, orgx, orgy);
History_del_current_entry(str);
current = charcount = _tcslen (str);
ConOutPrintf (_T("%s"), str);
bContinue=TRUE;
break;
}
#endif/*FEATURE_HISTORY*/
}
}
//if (bContinue)
// continue;
switch (ir.Event.KeyEvent.wVirtualKeyCode) switch (ir.Event.KeyEvent.wVirtualKeyCode)
{ {
case VK_BACK: case VK_BACK:
@ -303,8 +355,8 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
current = charcount = 0; current = charcount = 0;
break; break;
case VK_F3:
#ifdef FEATURE_HISTORY #ifdef FEATURE_HISTORY
case VK_F3:
History_move_to_bottom(); History_move_to_bottom();
#endif #endif
case VK_UP: case VK_UP:
@ -359,8 +411,11 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
} }
break; break;
#if 0
#ifdef FEATURE_HISTORY #ifdef FEATURE_HISTORY
/*!!!WARNING!!!*/ /*!!!WARNING!!!*/
/*this will only work as long as the two if statement /*this will only work as long as the two if statement
evaluates the same expression and a break is included evaluates the same expression and a break is included
@ -368,7 +423,6 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
This can be used for any combination using CTRL. This can be used for any combination using CTRL.
For other combinations is needed another system*/ For other combinations is needed another system*/
//case VK_K:
case 'K': case 'K':
/*add the current command line to the history*/ /*add the current command line to the history*/
if (ir.Event.KeyEvent.dwControlKeyState & if (ir.Event.KeyEvent.dwControlKeyState &
@ -394,7 +448,9 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
break; break;
} }
#endif #endif/*FEATURE_HISTORY*/
#endif/*0*/
default: default:
#ifdef _UNICODE #ifdef _UNICODE
ch = ir.Event.KeyEvent.uChar.UnicodeChar; ch = ir.Event.KeyEvent.uChar.UnicodeChar;

View file

@ -115,6 +115,10 @@ COMMAND cmds[] =
{_T("goto"), CMD_BATCHONLY, cmd_goto}, {_T("goto"), CMD_BATCHONLY, cmd_goto},
#ifdef FEATURE_HISTORY
{_T("history"), 0, CommandHistory},
#endif
{_T("if"), 0, cmd_if}, {_T("if"), 0, cmd_if},
#ifdef INCLUDE_CMD_LABEL #ifdef INCLUDE_CMD_LABEL

View file

@ -1,5 +1,4 @@
/* $Id: history.c,v 1.8 1999/12/24 17:19:20 ekohl Exp $ /*
*
* HISTORY.C - command line history. * HISTORY.C - command line history.
* *
* *
@ -25,6 +24,8 @@
* *
*/ */
/* /*
* HISTORY.C - command line history. Second version * HISTORY.C - command line history. Second version
* *
@ -36,9 +37,16 @@
* *
*/ */
#include "config.h" #include "config.h"
#ifdef FEATURE_HISTORY #ifdef FEATURE_HISTORY
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
#include <string.h> #include <string.h>
@ -54,29 +62,111 @@ typedef struct tagHISTORY
LPTSTR string; LPTSTR string;
} HIST_ENTRY, * LPHIST_ENTRY; } HIST_ENTRY, * LPHIST_ENTRY;
static INT size; static INT size,
static INT max_size=10; /* for now not configurable */ max_size=100;
static LPHIST_ENTRY Top; static LPHIST_ENTRY Top;
static LPHIST_ENTRY Bottom; static LPHIST_ENTRY Bottom;
static LPHIST_ENTRY curr_ptr=0; static LPHIST_ENTRY curr_ptr=0;
/* service functions */
static VOID del (LPHIST_ENTRY item); VOID InitHistory(VOID);
static VOID add_at_bottom (LPTSTR string); VOID History_move_to_bottom(VOID);
VOID History (INT dir, LPTSTR commandline);
VOID CleanHistory(VOID);
VOID History_del_current_entry(LPTSTR str);
/*service functions*/
VOID del(LPHIST_ENTRY item);
VOID add_at_bottom(LPTSTR string);
/*VOID add_before_last(LPTSTR string);*/
VOID set_size(INT new_size);
VOID InitHistory (VOID)
INT CommandHistory (LPTSTR cmd, LPTSTR param)
{
LPTSTR tmp;
INT tmp_int;
LPHIST_ENTRY h_tmp;
TCHAR szBuffer[2048];
tmp=_tcschr(param,_T('/'));
if (tmp)
{
param=tmp;
switch (_totupper(param[1]))
{
case _T('F'):/*delete history*/
CleanHistory();InitHistory();
break;
case _T('R'):/*read history from standard in*/
//hIn=GetStdHandle (STD_INPUT_HANDLE);
for(;;)
{
ConInString(szBuffer,sizeof(szBuffer)/sizeof(TCHAR));
if (*szBuffer!=_T('\0'))
History(0,szBuffer);
else
break;
}
break;
case _T('A'):/*add an antry*/
History(0,param+2);
break;
case _T('S'):/*set history size*/
if (tmp_int=_ttoi(param+2))
set_size(tmp_int);
break;
default:
return 1;
}
}
else
{
for(h_tmp=Top->prev;h_tmp!=Bottom;h_tmp=h_tmp->prev)
ConErrPuts(h_tmp->string);
}
}
VOID set_size(INT new_size)
{
while(new_size<size)
del(Top->prev);
max_size=new_size;
}
VOID InitHistory(VOID)
{ {
size=0; size=0;
Top = malloc(sizeof(HIST_ENTRY)); Top = malloc(sizeof(HIST_ENTRY));
Bottom = malloc(sizeof(HIST_ENTRY)); Bottom = malloc(sizeof(HIST_ENTRY));
Top->prev = Bottom; Top->prev = Bottom;
Top->next = NULL; Top->next = NULL;
Top->string = NULL; Top->string = NULL;
Bottom->prev = NULL; Bottom->prev = NULL;
Bottom->next = Top; Bottom->next = Top;
Bottom->string = NULL; Bottom->string = NULL;
@ -85,13 +175,17 @@ VOID InitHistory (VOID)
} }
VOID CleanHistory (VOID)
{
while (Bottom->next!=Top)
del (Bottom->next);
free (Top);
free (Bottom); VOID CleanHistory(VOID)
{
while (Bottom->next!=Top)
del(Bottom->next);
free(Top);
free(Bottom);
} }
@ -108,30 +202,40 @@ VOID History_del_current_entry(LPTSTR str)
if(curr_ptr==Top) if(curr_ptr==Top)
curr_ptr=Top->prev; curr_ptr=Top->prev;
tmp=curr_ptr; tmp=curr_ptr;
curr_ptr=curr_ptr->prev; curr_ptr=curr_ptr->prev;
del(tmp); del(tmp);
History(-1,str); History(-1,str);
} }
static static
VOID del(LPHIST_ENTRY item) VOID del(LPHIST_ENTRY item)
{ {
if( item==NULL || item==Top || item==Bottom ) if( item==NULL || item==Top || item==Bottom )
{ {
#ifdef _DEBUG #ifdef _DEBUG
DebugPrintf("del in " __FILE__ ": retrning\n" DebugPrintf("del in " __FILE__ ": retrning\n"
"item is 0x%08x (Bottom is0x%08x)\n", "item is 0x%08x (Bottom is0x%08x)\n",
item, Bottom); item, Bottom);
#endif #endif
return; return;
} }
/*free string's mem*/ /*free string's mem*/
if (item->string) if (item->string)
free(item->string); free(item->string);
/*set links in prev and next item*/ /*set links in prev and next item*/
item->next->prev=item->prev; item->next->prev=item->prev;
item->prev->next=item->next; item->prev->next=item->next;
@ -139,25 +243,91 @@ VOID del(LPHIST_ENTRY item)
free(item); free(item);
size--; size--;
} }
#if 0
static
VOID add_before_last(LPTSTR string)
{
LPHIST_ENTRY tmp,before,after;
/*delete first entry if maximum number of entries is reached*/
while(size>=max_size)
del(Top->prev);
while (_istspace(*string))
string++;
if (*string==_T('\0'))
return;
/*allocte entry and string*/
tmp=malloc(sizeof(HIST_ENTRY));
tmp->string=malloc(_tcslen(string)+1);
_tcscpy(tmp->string,string);
/*set links*/
before=Bottom->next;
after=before->next;
tmp->prev=before;
tmp->next=after;
after->prev=tmp;
before->next=tmp;
/*set new size*/
size++;
}
#endif/*0*/
static static
VOID add_at_bottom(LPTSTR string) VOID add_at_bottom(LPTSTR string)
{ {
LPHIST_ENTRY tmp; LPHIST_ENTRY tmp;
/*delete first entry if maximum number of entries is reached*/ /*delete first entry if maximum number of entries is reached*/
if(size==max_size) while(size>=max_size)
del(Top->prev); del(Top->prev);
/*fill bottom with string*/ while (_istspace(*string))
string++;
if (*string==_T('\0'))
return;
/*if new entry is the same than the last do not add it*/
if(size)
if(_tcscmp(string,Bottom->next->string)==0)
return;
/*fill bottom with string, it will become Bottom->next*/
Bottom->string=malloc(_tcslen(string)+1); Bottom->string=malloc(_tcslen(string)+1);
_tcscpy(Bottom->string,string); _tcscpy(Bottom->string,string);
/*save Bottom value*/ /*save Bottom value*/
tmp=Bottom; tmp=Bottom;
/*create new void Bottom*/ /*create new void Bottom*/
Bottom=malloc(sizeof(HIST_ENTRY)); Bottom=malloc(sizeof(HIST_ENTRY));
Bottom->next=tmp; Bottom->next=tmp;
@ -168,17 +338,21 @@ VOID add_at_bottom(LPTSTR string)
/*set new size*/ /*set new size*/
size++; size++;
} }
VOID History_move_to_bottom(VOID) VOID History_move_to_bottom(VOID)
{ {
curr_ptr=Bottom; curr_ptr=Bottom;
} }
VOID History (INT dir, LPTSTR commandline) VOID History (INT dir, LPTSTR commandline)
{ {
if(dir==0) if(dir==0)
{ {
add_at_bottom(commandline); add_at_bottom(commandline);
@ -192,6 +366,7 @@ VOID History (INT dir, LPTSTR commandline)
return; return;
} }
if(dir<0)/*key up*/ if(dir<0)/*key up*/
{ {
if (curr_ptr->next==Top || curr_ptr==Top) if (curr_ptr->next==Top || curr_ptr==Top)
@ -205,13 +380,20 @@ VOID History (INT dir, LPTSTR commandline)
#endif #endif
} }
curr_ptr = curr_ptr->next; curr_ptr = curr_ptr->next;
if(curr_ptr->string) if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string); _tcscpy(commandline,curr_ptr->string);
} }
if(dir>0) if(dir>0)
{ {
if (curr_ptr->prev==Bottom || curr_ptr==Bottom) if (curr_ptr->prev==Bottom || curr_ptr==Bottom)
{ {
#ifdef WRAP_HISTORY #ifdef WRAP_HISTORY
@ -226,8 +408,120 @@ VOID History (INT dir, LPTSTR commandline)
curr_ptr=curr_ptr->prev; curr_ptr=curr_ptr->prev;
if(curr_ptr->string) if(curr_ptr->string)
_tcscpy(commandline,curr_ptr->string); _tcscpy(commandline,curr_ptr->string);
} }
} }
#endif /* FEATURE_HISTORY */
/* EOF */
#if 0
LPTSTR history = NULL; /*buffer to sotre all the lines*/
LPTSTR lines[MAXLINES]; /*array of pointers to each line(entry)*/
/*located in history buffer*/
INT curline = 0; /*the last line recalled by user*/
INT numlines = 0; /*number of entries, included the last*/
/*empty one*/
INT maxpos = 0; /*index of last byte of last entry*/
VOID History (INT dir, LPTSTR commandline)
{
INT count; /*used in for loops*/
INT length; /*used in the same loops of count*/
/*both to make room when is full
either history or lines*/
/*first time History is called allocate mem*/
if (!history)
{
history = malloc (history_size * sizeof (TCHAR));
lines[0] = history;
history[0] = 0;
}
if (dir > 0)
{
/* next command */
if (curline < numlines)
{
curline++;
}
if (curline == numlines)
{
commandline[0] = 0;
}
else
{
_tcscpy (commandline, lines[curline]);
}
}
else if (dir < 0)
{
/* prev command */
if (curline > 0)
{
curline--;
}
_tcscpy (commandline, lines[curline]);
}
else
{
/* add to history */
/* remove oldest string until there's enough room for next one */
/* strlen (commandline) must be less than history_size! */
while ((maxpos + (INT)_tcslen (commandline) + 1 > history_size) || (numlines >= MAXLINES))
{
length = _tcslen (lines[0]) + 1;
for (count = 0; count < maxpos && count + (lines[1] - lines[0]) < history_size; count++)
{
history[count] = history[count + length];
}
maxpos -= length;
for (count = 0; count <= numlines && count < MAXLINES; count++)
{
lines[count] = lines[count + 1] - length;
}
numlines--;
#ifdef DEBUG
ConOutPrintf (_T("Reduced size: %ld lines\n"), numlines);
for (count = 0; count < numlines; count++)
{
ConOutPrintf (_T("%d: %s\n"), count, lines[count]);
}
#endif
}
/*copy entry in the history bufer*/
_tcscpy (lines[numlines], commandline);
numlines++;
/*set last lines[numlines] pointer next the end of last, valid,
just setted entry (the two lines above)*/
lines[numlines] = lines[numlines - 1] + _tcslen (commandline) + 1;
maxpos += _tcslen (commandline) + 1;
/* last line, empty */
curline = numlines;
}
return;
}
#endif
#endif //#if 0