From cd75b098f56bc5d0d6ee093bb84e430256dfa10a Mon Sep 17 00:00:00 2001 From: jianf <2198972886@qq.com> Date: Tue, 16 Dec 2025 09:13:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=BA=90=E7=A0=81=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E5=AE=8F=E4=BD=8D=E7=BD=AE=EF=BC=8C=E6=96=B9=E4=BE=BF?= =?UTF-8?q?=E4=B8=AA=E6=80=A7=E5=8C=96=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/config.h | 15 +++++++++++++ c/interpreter/interpreter.c | 7 +++++- c/interpreter/interpreter.h | 4 ++-- c/network/network.c | 10 ++++----- c/network/swap.h | 1 + c/tem/ctl.c | 44 ++++++++++++++++++++----------------- c/tem/ctl.h | 6 ++--- c/tools/log/log.h | 2 +- c/tools/quit/quit.c | 9 +++++--- 9 files changed, 62 insertions(+), 36 deletions(-) create mode 100644 c/config.h diff --git a/c/config.h b/c/config.h new file mode 100644 index 0000000..14871b8 --- /dev/null +++ b/c/config.h @@ -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 \ No newline at end of file diff --git a/c/interpreter/interpreter.c b/c/interpreter/interpreter.c index 1cc45af..ee822f1 100644 --- a/c/interpreter/interpreter.c +++ b/c/interpreter/interpreter.c @@ -174,6 +174,8 @@ int exce(const int command,ctx *all_ctx) all_ctx->statue = -1; write(all_ctx->fifofd[1],"q",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); get_args(all_ctx); - char *cmd_buf = malloc(MAX_BUF); + char *cmd_buf = malloc(INTER_MAX_BUF); int len; 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) { + //todo 读取命令脚本并执行 + return -1; } + return -1; } \ No newline at end of file diff --git a/c/interpreter/interpreter.h b/c/interpreter/interpreter.h index 1f95a38..c7740f0 100644 --- a/c/interpreter/interpreter.h +++ b/c/interpreter/interpreter.h @@ -1,7 +1,7 @@ #ifndef INTERPRETER #define INTERPRETER -#define MAX_BUF 256 +#include "config.h" #define SIG_MOD 0 #define FILE_MOD 1 @@ -37,7 +37,7 @@ typedef struct ctx int line;//当前行长度 int word;//当前解释词位置 args *arg;//当前环境下参数链表 - char command[MAX_BUF];//当前行缓存 + char command[INTER_MAX_BUF];//当前行缓存 int statue;//当前状态 int fifofd[2]; log_manager *log_manager; diff --git a/c/network/network.c b/c/network/network.c index a201547..9fc50eb 100644 --- a/c/network/network.c +++ b/c/network/network.c @@ -83,7 +83,7 @@ int rbt_parse_json(const char *json_text, rbt_msg *out) ssize_t read_req(int fd, void *buf) { // TODO 修改读取任务函数 - ssize_t n = read(fd, buf, MAX_MESSAGE_BUF); + ssize_t n = read(fd, buf, NET_MAX_MESSAGE_BUF); if (n == 0) /* 写端已关闭,管道永不会再有数据 */ return 0; return (n > 0) ? n : -1; @@ -137,7 +137,7 @@ int iss_work(netm *self,char *command) //查询空闲线程 while(atomic_load(&(self->pool[i].status)) ==0) { - if(ififo_fd[0],(void*)req); //管道关闭时退出; @@ -203,7 +203,7 @@ void *pth_module(void *args_p) int start_pool(netm *self) { - for(int i = 0;ipool[i].fifo_fd); @@ -219,7 +219,7 @@ int start_pool(netm *self) int shutdown_pool(netm *self) { - for(int i = 0;ipool[i].status == -1) continue; diff --git a/c/network/swap.h b/c/network/swap.h index bc7e5bf..9abba42 100644 --- a/c/network/swap.h +++ b/c/network/swap.h @@ -1,6 +1,7 @@ #ifndef SWAP #define SWAP +#include "config.h" #define QUITPLG 0 #define NEWMSG 1 #define FREE 2 diff --git a/c/tem/ctl.c b/c/tem/ctl.c index dbd9d58..3e9685f 100644 --- a/c/tem/ctl.c +++ b/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[*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) + 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->indexindexindex++; 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; diff --git a/c/tem/ctl.h b/c/tem/ctl.h index f9f6717..2905f49 100644 --- a/c/tem/ctl.h +++ b/c/tem/ctl.h @@ -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;//解释器上下文 diff --git a/c/tools/log/log.h b/c/tools/log/log.h index a618b72..16c8e9a 100644 --- a/c/tools/log/log.h +++ b/c/tools/log/log.h @@ -1,10 +1,10 @@ #ifndef LOG #define LOG +#include "config.h" #include #include -#define MAX_LOG 256 typedef struct logs { diff --git a/c/tools/quit/quit.c b/c/tools/quit/quit.c index 8eac777..9689fc9 100644 --- a/c/tools/quit/quit.c +++ b/c/tools/quit/quit.c @@ -24,17 +24,20 @@ int quit_server(netm *self) //关闭epoll监听 if(self->http_fd != -1) { - close(self->http_fd); + if(close(self->http_fd)==-1) + return -1; self->http_fd =-1; } //关闭socket监听 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; } //关闭管道监听 - + free(self->err_indictor); + return 0; } void quit_all(int status,void *self_p)