dish/loop.c

52 lines
1000 B
C
Raw Normal View History

2022-09-18 06:46:23 +00:00
/*
* @Author: 1ridic
* @Date: 2022-09-18 14:13:53
* @Last Modified by: 1ridic
* @Last Modified time: 2022-09-20 22:26:59
2022-09-18 06:46:23 +00:00
*/
#include "loop.h"
#include "exec.h"
#include "line.h"
2023-03-30 10:12:29 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <readline/history.h>
#include <readline/readline.h>
int status;
2022-09-18 05:14:30 +00:00
void SIGINT_Handler(int dummy) {
#ifdef DEBUG
2023-03-30 10:12:29 +00:00
fprintf(stdout, "\ndebug: SIGINT\n");
#endif
/* Move to a new line */
2023-03-30 10:12:29 +00:00
fprintf(stdout, "\n");
/* Regenerate the prompt on a newline */
rl_on_new_line();
/* Clear the previous text */
rl_replace_line("", 0);
rl_redisplay();
return;
}
2022-09-18 05:14:30 +00:00
int loop() {
2022-09-18 05:14:30 +00:00
char *line;
char **args;
2023-03-30 10:12:29 +00:00
line = readline(ANSI_COLOR_GREEN "> " ANSI_COLOR_RESET);
add_history(line);
2022-09-18 05:14:30 +00:00
args = splitLine(line);
2023-03-30 10:12:29 +00:00
if (args == NULL) {
free(line);
free(args);
return 1;
}
status = commandExec(args);
2022-09-18 05:14:30 +00:00
free(line);
2023-03-30 10:12:29 +00:00
/*free all array in args*/
for (int i = 0; args[i] != NULL; i++) {
free(args[i]);
}
2022-09-18 05:14:30 +00:00
free(args);
return 0;
}