优化源码配置宏位置,方便个性化编译
This commit is contained in:
44
c/tem/ctl.c
44
c/tem/ctl.c
@ -35,7 +35,9 @@ int replace_chars(int start_pos, int old_len, const char *new_str) {
|
||||
// 3. 如果新内容比原内容短,删除剩余部分
|
||||
if (new_len < old_len) {
|
||||
write(STDOUT_FILENO, "\033[K", 3);
|
||||
return 0 ;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward)
|
||||
@ -45,12 +47,12 @@ int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward)
|
||||
if(*currant_index>0)
|
||||
(*currant_index)--;
|
||||
else
|
||||
*currant_index = HISTORY_BUF-1;
|
||||
*currant_index = TEM_HISTORY_BUF-1;
|
||||
|
||||
}
|
||||
else if(toward == 0)
|
||||
{
|
||||
if(*currant_index <HISTORY_BUF-1)
|
||||
if(*currant_index <TEM_HISTORY_BUF-1)
|
||||
(*currant_index)++;
|
||||
else
|
||||
*currant_index = 0;
|
||||
@ -60,8 +62,8 @@ int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward)
|
||||
*length = *length-2;
|
||||
return 0;
|
||||
}
|
||||
replace_chars(sizeof(PROMPT)-1,*length,self->history[*currant_index]);
|
||||
memcpy(buf,self->history[*currant_index],MAX_BUF);
|
||||
replace_chars(sizeof(TEM_PROMPT)-1,*length,self->history[*currant_index]);
|
||||
memcpy(buf,self->history[*currant_index],TEM_MAX_BUF);
|
||||
buf[strlen(buf)-1] = '\0';
|
||||
*length = strlen(buf);
|
||||
return 0;
|
||||
@ -71,7 +73,7 @@ int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward)
|
||||
|
||||
int del_char(int length, int index, char *buf)
|
||||
{
|
||||
int buf_idx = index - sizeof(PROMPT); // 待删字符在 buf 中的下标
|
||||
int buf_idx = index - sizeof(TEM_PROMPT); // 待删字符在 buf 中的下标
|
||||
|
||||
if (length == index) // 行尾退格
|
||||
{
|
||||
@ -86,7 +88,7 @@ int del_char(int length, int index, char *buf)
|
||||
goto_col(length - 2);
|
||||
write(STDOUT_FILENO, "\033[K", 3);
|
||||
goto_col(index - 1);
|
||||
char *restr = buf+index-sizeof(PROMPT)+1;
|
||||
char *restr = buf+index-sizeof(TEM_PROMPT)+1;
|
||||
memcpy(restr,new_str,str_len);
|
||||
free(new_str);
|
||||
return 1;
|
||||
@ -129,7 +131,7 @@ int read_line(char *buf,Ctl *self)
|
||||
char input_buf;
|
||||
int cursor_index = 0;
|
||||
int currant_index = self->index;
|
||||
while(read(0,&input_buf,1)==1&&length<MAX_BUF)
|
||||
while(read(0,&input_buf,1)==1&&length<TEM_MAX_BUF)
|
||||
{
|
||||
switch (input_buf) {
|
||||
case '\n':
|
||||
@ -145,7 +147,7 @@ int read_line(char *buf,Ctl *self)
|
||||
break;
|
||||
length--;
|
||||
get_cursor(&cursor_index);
|
||||
del_char(length+sizeof(PROMPT),cursor_index-1,buf);
|
||||
del_char(length+sizeof(TEM_PROMPT),cursor_index-1,buf);
|
||||
break;
|
||||
//方向键
|
||||
case 0x41: case 0x42: case 0x43: case 0x44:
|
||||
@ -165,7 +167,7 @@ int read_line(char *buf,Ctl *self)
|
||||
case 0x43:
|
||||
get_cursor(&cursor_index);
|
||||
length = length-2;
|
||||
if(cursor_index == sizeof(PROMPT)+length)
|
||||
if(cursor_index == sizeof(TEM_PROMPT)+length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -174,7 +176,7 @@ int read_line(char *buf,Ctl *self)
|
||||
case 0x44:
|
||||
get_cursor(&cursor_index);
|
||||
length = length -2;
|
||||
if(cursor_index == sizeof(PROMPT))
|
||||
if(cursor_index == sizeof(TEM_PROMPT))
|
||||
{
|
||||
break;
|
||||
}
|
||||
@ -192,24 +194,26 @@ int read_line(char *buf,Ctl *self)
|
||||
}
|
||||
|
||||
}
|
||||
if(length>=MAX_BUF)
|
||||
if(length>=TEM_MAX_BUF)
|
||||
{
|
||||
perror("SYS:input pass edge");
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int infifo(Ctl *self,const char *cmd)
|
||||
{
|
||||
if(self->history[self->index]!=NULL){
|
||||
memcpy(self->history[self->index],cmd,MAX_BUF);
|
||||
memcpy(self->history[self->index],cmd,TEM_MAX_BUF);
|
||||
}
|
||||
else{
|
||||
self->history[self->index] = (char*)malloc(MAX_BUF*sizeof(char));
|
||||
memcpy(self->history[self->index],cmd,MAX_BUF);
|
||||
self->history[self->index] = (char*)malloc(TEM_MAX_BUF*sizeof(char));
|
||||
memcpy(self->history[self->index],cmd,TEM_MAX_BUF);
|
||||
}
|
||||
//存储命令历史s
|
||||
if(self->index<HISTORY_BUF){
|
||||
if(self->index<TEM_HISTORY_BUF){
|
||||
self->index++;
|
||||
return 0;
|
||||
}
|
||||
@ -234,7 +238,7 @@ int free_history(Ctl *self)
|
||||
|
||||
int teml(Ctl *self,int fifo[2])
|
||||
{
|
||||
char input[MAX_BUF] = {'\0'};
|
||||
char input[TEM_MAX_BUF] = {'\0'};
|
||||
ctx *command = (ctx*)malloc(sizeof(ctx));
|
||||
Cmd cmd_dir[10];
|
||||
init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器
|
||||
@ -244,7 +248,7 @@ int teml(Ctl *self,int fifo[2])
|
||||
self->command = command;
|
||||
do
|
||||
{ //设置缓冲区,接收用户输入
|
||||
write(STDOUT_FILENO,PROMPT,sizeof(PROMPT));
|
||||
write(STDOUT_FILENO,TEM_PROMPT,sizeof(TEM_PROMPT));
|
||||
command->line = read_line(input,self);
|
||||
if(command->line == -1)
|
||||
perror("sys error");
|
||||
@ -256,7 +260,7 @@ int teml(Ctl *self,int fifo[2])
|
||||
memcpy(command->command,input,sizeof(input));
|
||||
interpret(SIG_MOD,command,cmd_dir);
|
||||
const char fexp[256] = {'\0'};
|
||||
memcpy(&input,&fexp,MAX_BUF);
|
||||
memcpy(&input,&fexp,TEM_MAX_BUF);
|
||||
}while(command->statue == 0);
|
||||
log_manager_stop(self->logmanager);
|
||||
pthread_join(self->logwathcher,NULL);
|
||||
@ -277,8 +281,8 @@ Ctl *init_tem(log_manager *logmanager)
|
||||
tem->infifo = infifo;
|
||||
tem->index = 0;
|
||||
tem->logmanager = logmanager;
|
||||
char *his_buf[HISTORY_BUF] = {NULL};
|
||||
memcpy(tem->history,his_buf,HISTORY_BUF);
|
||||
char *his_buf[TEM_HISTORY_BUF] = {NULL};
|
||||
memcpy(tem->history,his_buf,TEM_HISTORY_BUF);
|
||||
for(int i =0;i<6;i++)
|
||||
{
|
||||
tem->history[i] = NULL;
|
||||
|
||||
@ -5,10 +5,8 @@
|
||||
#include "tools/toml/toml.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "interpreter/interpreter.h"
|
||||
#include "config.h"
|
||||
|
||||
#define MAX_BUF 256
|
||||
#define HISTORY_BUF 210
|
||||
#define PROMPT "chatbot$$ "
|
||||
|
||||
|
||||
typedef struct Ctl
|
||||
@ -16,7 +14,7 @@ typedef struct Ctl
|
||||
int (*run)(struct Ctl*,int *);
|
||||
int (*infifo)(struct Ctl*,const char*);
|
||||
int index;
|
||||
char *history[HISTORY_BUF];
|
||||
char *history[TEM_HISTORY_BUF];
|
||||
pthread_t logwathcher;
|
||||
log_manager *logmanager;
|
||||
ctx *command;//解释器上下文
|
||||
|
||||
Reference in New Issue
Block a user