优化源码配置宏位置,方便个性化编译

This commit is contained in:
2025-12-16 09:13:30 +08:00
parent 08589dfe79
commit cd75b098f5
9 changed files with 62 additions and 36 deletions

15
c/config.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef SEVERCONFG
#define SEVERCONFG
#define MAX_LOG 256
#define TEM_MAX_BUF 256
#define TEM_HISTORY_BUF 210
#define TEM_PROMPT "chatbot$$ "
#define NET_MAX_POOL 10
#define NET_MAX_MESSAGE_BUF 1024
#define INTER_MAX_BUF 256
#endif

View File

@ -174,6 +174,8 @@ int exce(const int command,ctx *all_ctx)
all_ctx->statue = -1; all_ctx->statue = -1;
write(all_ctx->fifofd[1],"q",1); write(all_ctx->fifofd[1],"q",1);
return 1; return 1;
default :
return -1;
} }
} }
@ -186,7 +188,7 @@ int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic)
split(all_ctx->command,all_ctx); split(all_ctx->command,all_ctx);
get_args(all_ctx); get_args(all_ctx);
char *cmd_buf = malloc(MAX_BUF); char *cmd_buf = malloc(INTER_MAX_BUF);
int len; int len;
if(all_ctx->space_index[0]==0) if(all_ctx->space_index[0]==0)
{ {
@ -214,5 +216,8 @@ int interpret(int mod, ctx *all_ctx,Cmd *cmd_dic)
if (mod == FILE_MOD) if (mod == FILE_MOD)
{ {
//todo 读取命令脚本并执行
return -1;
} }
return -1;
} }

View File

@ -1,7 +1,7 @@
#ifndef INTERPRETER #ifndef INTERPRETER
#define INTERPRETER #define INTERPRETER
#define MAX_BUF 256 #include "config.h"
#define SIG_MOD 0 #define SIG_MOD 0
#define FILE_MOD 1 #define FILE_MOD 1
@ -37,7 +37,7 @@ typedef struct ctx
int line;//当前行长度 int line;//当前行长度
int word;//当前解释词位置 int word;//当前解释词位置
args *arg;//当前环境下参数链表 args *arg;//当前环境下参数链表
char command[MAX_BUF];//当前行缓存 char command[INTER_MAX_BUF];//当前行缓存
int statue;//当前状态 int statue;//当前状态
int fifofd[2]; int fifofd[2];
log_manager *log_manager; log_manager *log_manager;

View File

@ -83,7 +83,7 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
ssize_t read_req(int fd, void *buf) ssize_t read_req(int fd, void *buf)
{ {
// TODO 修改读取任务函数 // TODO 修改读取任务函数
ssize_t n = read(fd, buf, MAX_MESSAGE_BUF); ssize_t n = read(fd, buf, NET_MAX_MESSAGE_BUF);
if (n == 0) /* 写端已关闭,管道永不会再有数据 */ if (n == 0) /* 写端已关闭,管道永不会再有数据 */
return 0; return 0;
return (n > 0) ? n : -1; return (n > 0) ? n : -1;
@ -137,7 +137,7 @@ int iss_work(netm *self,char *command)
//查询空闲线程 //查询空闲线程
while(atomic_load(&(self->pool[i].status)) ==0) while(atomic_load(&(self->pool[i].status)) ==0)
{ {
if(i<MAX_POOL) if(i<NET_MAX_POOL)
i++; i++;
else{ else{
i=0; i=0;
@ -182,7 +182,7 @@ void *pth_module(void *args_p)
for(;;){ for(;;){
//线程池中,单个线程模型 //线程池中,单个线程模型
char *req = (char*)malloc(MAX_MESSAGE_BUF); char *req = (char*)malloc(NET_MAX_MESSAGE_BUF);
//从管道中读取请求,并解析,无内容时休眠 //从管道中读取请求,并解析,无内容时休眠
int n = read_req(pmd->fifo_fd[0],(void*)req); int n = read_req(pmd->fifo_fd[0],(void*)req);
//管道关闭时退出; //管道关闭时退出;
@ -203,7 +203,7 @@ void *pth_module(void *args_p)
int start_pool(netm *self) int start_pool(netm *self)
{ {
for(int i = 0;i<MAX_POOL;i++) for(int i = 0;i<NET_MAX_POOL;i++)
{ {
//为线程开辟管道 //为线程开辟管道
pipe(self->pool[i].fifo_fd); pipe(self->pool[i].fifo_fd);
@ -219,7 +219,7 @@ int start_pool(netm *self)
int shutdown_pool(netm *self) int shutdown_pool(netm *self)
{ {
for(int i = 0;i<MAX_POOL;i++) for(int i = 0;i<NET_MAX_POOL;i++)
{ {
if(self->pool[i].status == -1) if(self->pool[i].status == -1)
continue; continue;

View File

@ -1,6 +1,7 @@
#ifndef SWAP #ifndef SWAP
#define SWAP #define SWAP
#include "config.h"
#define QUITPLG 0 #define QUITPLG 0
#define NEWMSG 1 #define NEWMSG 1
#define FREE 2 #define FREE 2

View File

@ -35,7 +35,9 @@ int replace_chars(int start_pos, int old_len, const char *new_str) {
// 3. 如果新内容比原内容短,删除剩余部分 // 3. 如果新内容比原内容短,删除剩余部分
if (new_len < old_len) { if (new_len < old_len) {
write(STDOUT_FILENO, "\033[K", 3); write(STDOUT_FILENO, "\033[K", 3);
return 0 ;
} }
return 0;
} }
int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward) 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) if(*currant_index>0)
(*currant_index)--; (*currant_index)--;
else else
*currant_index = HISTORY_BUF-1; *currant_index = TEM_HISTORY_BUF-1;
} }
else if(toward == 0) else if(toward == 0)
{ {
if(*currant_index <HISTORY_BUF-1) if(*currant_index <TEM_HISTORY_BUF-1)
(*currant_index)++; (*currant_index)++;
else else
*currant_index = 0; *currant_index = 0;
@ -60,8 +62,8 @@ int take_history(Ctl *self,int *currant_index,int *length,char *buf,int toward)
*length = *length-2; *length = *length-2;
return 0; return 0;
} }
replace_chars(sizeof(PROMPT)-1,*length,self->history[*currant_index]); replace_chars(sizeof(TEM_PROMPT)-1,*length,self->history[*currant_index]);
memcpy(buf,self->history[*currant_index],MAX_BUF); memcpy(buf,self->history[*currant_index],TEM_MAX_BUF);
buf[strlen(buf)-1] = '\0'; buf[strlen(buf)-1] = '\0';
*length = strlen(buf); *length = strlen(buf);
return 0; 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 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) // 行尾退格 if (length == index) // 行尾退格
{ {
@ -86,7 +88,7 @@ int del_char(int length, int index, char *buf)
goto_col(length - 2); goto_col(length - 2);
write(STDOUT_FILENO, "\033[K", 3); write(STDOUT_FILENO, "\033[K", 3);
goto_col(index - 1); 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); memcpy(restr,new_str,str_len);
free(new_str); free(new_str);
return 1; return 1;
@ -129,7 +131,7 @@ int read_line(char *buf,Ctl *self)
char input_buf; char input_buf;
int cursor_index = 0; int cursor_index = 0;
int currant_index = self->index; 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) { switch (input_buf) {
case '\n': case '\n':
@ -145,7 +147,7 @@ int read_line(char *buf,Ctl *self)
break; break;
length--; length--;
get_cursor(&cursor_index); get_cursor(&cursor_index);
del_char(length+sizeof(PROMPT),cursor_index-1,buf); del_char(length+sizeof(TEM_PROMPT),cursor_index-1,buf);
break; break;
//方向键 //方向键
case 0x41: case 0x42: case 0x43: case 0x44: case 0x41: case 0x42: case 0x43: case 0x44:
@ -165,7 +167,7 @@ int read_line(char *buf,Ctl *self)
case 0x43: case 0x43:
get_cursor(&cursor_index); get_cursor(&cursor_index);
length = length-2; length = length-2;
if(cursor_index == sizeof(PROMPT)+length) if(cursor_index == sizeof(TEM_PROMPT)+length)
{ {
break; break;
} }
@ -174,7 +176,7 @@ int read_line(char *buf,Ctl *self)
case 0x44: case 0x44:
get_cursor(&cursor_index); get_cursor(&cursor_index);
length = length -2; length = length -2;
if(cursor_index == sizeof(PROMPT)) if(cursor_index == sizeof(TEM_PROMPT))
{ {
break; 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"); perror("SYS:input pass edge");
return 0;
} }
return 0;
} }
int infifo(Ctl *self,const char *cmd) int infifo(Ctl *self,const char *cmd)
{ {
if(self->history[self->index]!=NULL){ if(self->history[self->index]!=NULL){
memcpy(self->history[self->index],cmd,MAX_BUF); memcpy(self->history[self->index],cmd,TEM_MAX_BUF);
} }
else{ else{
self->history[self->index] = (char*)malloc(MAX_BUF*sizeof(char)); self->history[self->index] = (char*)malloc(TEM_MAX_BUF*sizeof(char));
memcpy(self->history[self->index],cmd,MAX_BUF); memcpy(self->history[self->index],cmd,TEM_MAX_BUF);
} }
//存储命令历史s //存储命令历史s
if(self->index<HISTORY_BUF){ if(self->index<TEM_HISTORY_BUF){
self->index++; self->index++;
return 0; return 0;
} }
@ -234,7 +238,7 @@ int free_history(Ctl *self)
int teml(Ctl *self,int fifo[2]) int teml(Ctl *self,int fifo[2])
{ {
char input[MAX_BUF] = {'\0'}; char input[TEM_MAX_BUF] = {'\0'};
ctx *command = (ctx*)malloc(sizeof(ctx)); ctx *command = (ctx*)malloc(sizeof(ctx));
Cmd cmd_dir[10]; Cmd cmd_dir[10];
init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器 init_interpreter(cmd_dir,command,fifo,self->logmanager);//初始化解释器
@ -244,7 +248,7 @@ int teml(Ctl *self,int fifo[2])
self->command = command; self->command = command;
do do
{ //设置缓冲区,接收用户输入 { //设置缓冲区,接收用户输入
write(STDOUT_FILENO,PROMPT,sizeof(PROMPT)); write(STDOUT_FILENO,TEM_PROMPT,sizeof(TEM_PROMPT));
command->line = read_line(input,self); command->line = read_line(input,self);
if(command->line == -1) if(command->line == -1)
perror("sys error"); perror("sys error");
@ -256,7 +260,7 @@ int teml(Ctl *self,int fifo[2])
memcpy(command->command,input,sizeof(input)); memcpy(command->command,input,sizeof(input));
interpret(SIG_MOD,command,cmd_dir); interpret(SIG_MOD,command,cmd_dir);
const char fexp[256] = {'\0'}; const char fexp[256] = {'\0'};
memcpy(&input,&fexp,MAX_BUF); memcpy(&input,&fexp,TEM_MAX_BUF);
}while(command->statue == 0); }while(command->statue == 0);
log_manager_stop(self->logmanager); log_manager_stop(self->logmanager);
pthread_join(self->logwathcher,NULL); pthread_join(self->logwathcher,NULL);
@ -277,8 +281,8 @@ Ctl *init_tem(log_manager *logmanager)
tem->infifo = infifo; tem->infifo = infifo;
tem->index = 0; tem->index = 0;
tem->logmanager = logmanager; tem->logmanager = logmanager;
char *his_buf[HISTORY_BUF] = {NULL}; char *his_buf[TEM_HISTORY_BUF] = {NULL};
memcpy(tem->history,his_buf,HISTORY_BUF); memcpy(tem->history,his_buf,TEM_HISTORY_BUF);
for(int i =0;i<6;i++) for(int i =0;i<6;i++)
{ {
tem->history[i] = NULL; tem->history[i] = NULL;

View File

@ -5,10 +5,8 @@
#include "tools/toml/toml.h" #include "tools/toml/toml.h"
#include "tools/log/log.h" #include "tools/log/log.h"
#include "interpreter/interpreter.h" #include "interpreter/interpreter.h"
#include "config.h"
#define MAX_BUF 256
#define HISTORY_BUF 210
#define PROMPT "chatbot$$ "
typedef struct Ctl typedef struct Ctl
@ -16,7 +14,7 @@ typedef struct Ctl
int (*run)(struct Ctl*,int *); int (*run)(struct Ctl*,int *);
int (*infifo)(struct Ctl*,const char*); int (*infifo)(struct Ctl*,const char*);
int index; int index;
char *history[HISTORY_BUF]; char *history[TEM_HISTORY_BUF];
pthread_t logwathcher; pthread_t logwathcher;
log_manager *logmanager; log_manager *logmanager;
ctx *command;//解释器上下文 ctx *command;//解释器上下文

View File

@ -1,10 +1,10 @@
#ifndef LOG #ifndef LOG
#define LOG #define LOG
#include "config.h"
#include <semaphore.h> #include <semaphore.h>
#include <pthread.h> #include <pthread.h>
#define MAX_LOG 256
typedef struct logs typedef struct logs
{ {

View File

@ -24,17 +24,20 @@ int quit_server(netm *self)
//关闭epoll监听 //关闭epoll监听
if(self->http_fd != -1) if(self->http_fd != -1)
{ {
close(self->http_fd); if(close(self->http_fd)==-1)
return -1;
self->http_fd =-1; self->http_fd =-1;
} }
//关闭socket监听 //关闭socket监听
if(self->fifo_fd[1] != -1) if(self->fifo_fd[1] != -1)
{ {
close(self->fifo_fd[1]); if(close(self->fifo_fd[1])==-1)
return -1;
self->fifo_fd[1] = -1; self->fifo_fd[1] = -1;
} }
//关闭管道监听 //关闭管道监听
free(self->err_indictor);
return 0;
} }
void quit_all(int status,void *self_p) void quit_all(int status,void *self_p)