使用了readline库,优化用户输入体验

修复:sakitsuki_asahi/RSA-Chat#2
This commit is contained in:
sakitsuki_asahi
2026-03-13 13:40:25 +09:00
parent 6d92a7c9bb
commit 8efbeaef0a
4 changed files with 30 additions and 16 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
all:
gcc -o RSA-Chat src/c/RSA-Chat.c -lcrypto
gcc -o RSA-Chat src/c/RSA-Chat.c -lcrypto -lreadline
BIN
View File
Binary file not shown.
+2 -1
View File
@@ -1,5 +1,6 @@
C version:
openssl 3.0 or later
openssl 3.0
readline 8.2-1.3
python version:
Python 3.8 or later
cryptography >= 38.0.0
+27 -14
View File
@@ -2,6 +2,9 @@
#include "text.c"
#include "file.c"
#include <readline/readline.h>
#include <readline/history.h>
int listen_mode=1;int send_mode=1;
int stop_flag=0;
@@ -27,6 +30,9 @@ void* receive_f(void* _i){
if(received<=0){
perror("receive_f(): recvfrom failed.");
}
rl_save_prompt();
printf("\033[2K\r");
fflush(stdout);
char* output = malloc(received);
int size = text_decrypt(input->key,buffer,output);
if(size < 0){
@@ -56,6 +62,8 @@ void* receive_f(void* _i){
printf("\033[32m%s\033[0m\n",output);
free(output);
}
rl_restore_prompt();
rl_forced_update_display();
free(header);
}
return NULL;
@@ -162,22 +170,29 @@ int main(int argn,char** argc){
pthread_t receive_t;struct receive_input receive_i = {sockfd, local_key, result};
pthread_create(&receive_t,NULL,receive_f,&receive_i);
char* buffer = malloc(BUFFER);
memset(buffer,0,BUFFER);
char* ioput = malloc(BUFFER);
char input[BUFFER]={0};
memset(ioput,0,BUFFER);
char* cmd = malloc(BUFFER);
memset(cmd,0,BUFFER);
char* argi = malloc(BUFFER);
memset(argi,0,BUFFER);
printf("初始化成功\n");
while(1){
memset(input,0,BUFFER);
scanf("%s",input);
switch(command(input)){
memset(cmd,0,BUFFER);
char* input = readline("");
strcpy(ioput,input);
free(input);
memcpy(cmd,ioput,strcspn(ioput," "));
strcpy(argi,ioput+strcspn(ioput," ")+1);
switch(command(cmd)){
case receive:
if(!listen_mode){
printf("接收模式已被禁用\n");
break;
}
printf("请粘贴Base64字符串:\n");
scanf("%s",ioput);
base64_decode(ioput,buffer,NULL);
memset(ioput,0,BUFFER);
base64_decode(argi,buffer,NULL);
memset(argi,0,BUFFER);
int size = text_decrypt(local_key,buffer,ioput);
memset(buffer,0,BUFFER);
if(size < 0){
@@ -192,25 +207,23 @@ int main(int argn,char** argc){
printf("发送模式已被禁用\n");
break;
}
printf("请输入要发送的文本或文件路径:\n");
scanf("%s",ioput);
FILE* send_file = fopen(ioput,"r");
if(strlen(ioput)>(peer_bits/8-80)){
perror("Text too long for key of peer.\n");
break;
}
if(send_file!=NULL){
int send_stat = send_file_f(ioput,sockfd,peer_key,result);
fclose(send_file);
int send_stat = send_file_f(argi,sockfd,peer_key,result);
if(send_stat!=0){
perror("文件发送失败");
}else{
printf("\033[32m文件发送成功: %s\033[0m\n",ioput);
printf("\033[32m文件发送成功: %s\033[0m\n",argi);
}
break;
}
size = text_encrypt(peer_key,ioput,buffer,strlen(ioput));
memset(ioput,0,BUFFER);
size = text_encrypt(peer_key,argi,buffer,strlen(argi));
memset(argi,0,BUFFER);
if(size < 0){
perror("加密失败");
}else{