📄 history.c.bak
字号:
/*********************************************************************
********************************************************************/
#include <stdio.h>
#include <string.h>
#include "common.h"
/********************************************************************/
#define MAX_HISTORY_REC (5)
#define CTRL_U 0x15 /* command line previous */
#define CTRL_D 0x04 /* command line next */
#define CTRL_R 0x12 /* last command repeat */
typedef char HISTENT[MAX_LINE_LEN];
HISTENT History[MAX_HISTORY_REC+1];
/*******************************************************************/
void HistoryInit (void)
{
int index;
memset(History,MAX_HISTORY * MAX_LINE_LEN, 0);
}
/********************************************************************/
static int HistoryDown (int *CurrHist, char *Line)
{
*CurrHist -= 1;
if (*CurrHist < 0)
{
*CurrHist = -1;
Line[0] = '\0';
return 0;
}
strcpy(Line, History[*CurrHist]);
return strlen(Line);
}
/********************************************************************/
static int HistoryUp (int *CurrHist, char *Line)
{
if (*CurrHist == -1) *CurrHist = 0;
else {
*CurrHist ++;
if (*CurrHist >= MAX_HISTORY) *CurrHist = (MAX_HISTORY-1);
}
strcpy(Line, History[*CurrHist]);
return strlen(line);
}
/********************************************************************/
static int HistoryRepeat (char *Line)
{
strcpy(Line, History[0]);
return strlen(Line);
}
/********************************************************************/
static void HistoryAdd (char *Line)
{
int index;
for (index = (MAX_HISTORY-1); index > 0; --index)
if (!strcmp(History[index],Line)) break;
if (index == (MAX_HISTORY-1)) {
for (index = (MAX_HISTORY-1); index > 0; --index)
strcpy(History[index],History[index-1]);
strcpy(History[0], Line);
} else {
strcpy(History[MAX_HISTORY], History[0]);
strcpy(History[0],History[index]);
strcpy(History[index],History[MAX_HISTORY]);
}
}
/********************************************************************/
char *GetHistoryLine (char *userline)
{
char Line[MAX_LINE];
int Pos,CurrHist,Repeat;
int ch, i;
CurrHist = -1;
Repeat = FALSE;
Pos = 0;
ch = (int)Getchar();
while ( (ch != 0x0D /* CR */) &&
(ch != 0x0A /* LF/NL */) &&
(Pos < MAX_LINE))
{
switch (ch)
{
case 0x08: /* Backspace */
case 0x7F: /* Delete */
if (Pos > 0)
{
Pos -= 1;
putchar(0x08); /* backspace */
board_putchar(' ');
board_putchar(0x08); /* backspace */
}
break;
case CTRL_U:
case CTRL_D:
case CTRL_R:
for (i = 0; i < pos; ++i)
{
board_putchar(0x08); /* backspace */
board_putchar(' ');
board_putchar(0x08); /* backspace */
}
if (ch == CTRL_U)
{
pos = history_up(&curr_hist,line);
break;
}
if (ch == CTRL_D)
{
pos = history_down(&curr_hist,line);
break;
}
if (ch == CTRL_R)
{
pos = history_repeat(line);
repeat = TRUE;
}
break;
default:
if ((pos+1) < UIF_MAX_LINE)
{
/* only printable characters */
if ((ch > 0x1f) && (ch < 0x80))
{
line[pos++] = (char)ch;
board_putchar((char)ch);
}
}
break;
}
if (repeat)
{
break;
}
ch = (int)board_getchar();
}
line[pos] = '\0';
board_putchar(0x0D); /* CR */
board_putchar(0x0A); /* LF */
if ((strlen(line) != 0) && !repeat)
{
history_add(line);
}
strcpy(userline,line);
return userline;
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -