feat(shell): import readline, and add simple history
This commit is contained in:
parent
5c8ed315f7
commit
87eb989d79
3 changed files with 19 additions and 48 deletions
5
Makefile
5
Makefile
|
@ -1,6 +1,7 @@
|
||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
CC = gcc
|
CC = gcc
|
||||||
SRC = main.c loop.c exec.c line.c builtin.c
|
SRC = main.c loop.c exec.c line.c builtin.c
|
||||||
|
LINK= -lreadline
|
||||||
OBJ = $(SRC:.c=.ro)
|
OBJ = $(SRC:.c=.ro)
|
||||||
DBGOBJ = $(SRC:.c=.do)
|
DBGOBJ = $(SRC:.c=.do)
|
||||||
EXE = dish
|
EXE = dish
|
||||||
|
@ -11,10 +12,10 @@ debug: CFLAGS = -g -O0 -DDEBUG
|
||||||
release: CFLAGS = -Wall -O3
|
release: CFLAGS = -Wall -O3
|
||||||
|
|
||||||
release: $(OBJ)
|
release: $(OBJ)
|
||||||
$(CC) $(CFLAGS) -o $(EXE) $(OBJ)
|
$(CC) $(CFLAGS) -o $(EXE) $(OBJ) $(LINK)
|
||||||
|
|
||||||
debug: $(DBGOBJ)
|
debug: $(DBGOBJ)
|
||||||
$(CC) $(CFLAGS) -o $(DBGEXE) $(DBGOBJ)
|
$(CC) $(CFLAGS) -o $(DBGEXE) $(DBGOBJ) $(LINK)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o *.do *.ro
|
rm -f *.o *.do *.ro
|
||||||
|
|
40
line.c
40
line.c
|
@ -4,45 +4,13 @@
|
||||||
* @Last Modified by: 1ridic
|
* @Last Modified by: 1ridic
|
||||||
* @Last Modified time: 2022-09-18 14:14:05
|
* @Last Modified time: 2022-09-18 14:14:05
|
||||||
*/
|
*/
|
||||||
#include "line.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
char *readLine(void) {
|
#include "line.h"
|
||||||
int bufsize = LINE_BUF_SIZE;
|
|
||||||
int position = 0;
|
|
||||||
char *buffer = malloc(sizeof(char) * bufsize);
|
|
||||||
char c;
|
|
||||||
|
|
||||||
if (!buffer) {
|
|
||||||
fprintf(stderr, "dish: allocation error\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
// read a character
|
|
||||||
c = getchar();
|
|
||||||
// repleace EOF with a null character and return
|
|
||||||
if (c == EOF || c == '\n') {
|
|
||||||
buffer[position] = '\0';
|
|
||||||
return buffer;
|
|
||||||
} else {
|
|
||||||
buffer[position] = c;
|
|
||||||
}
|
|
||||||
position++;
|
|
||||||
|
|
||||||
/* if the buffer is full, reallocate */
|
|
||||||
if (position >= bufsize) {
|
|
||||||
bufsize += LINE_BUF_SIZE;
|
|
||||||
buffer = realloc(buffer, bufsize);
|
|
||||||
if (!buffer) {
|
|
||||||
fprintf(stderr, "dish: allocation error\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char **splitLine(char *line) {
|
char **splitLine(char *line) {
|
||||||
int bufsize = LINE_BUF_SIZE, position = 0;
|
int bufsize = LINE_BUF_SIZE, position = 0;
|
||||||
|
|
12
loop.c
12
loop.c
|
@ -4,12 +4,14 @@
|
||||||
* @Last Modified by: 1ridic
|
* @Last Modified by: 1ridic
|
||||||
* @Last Modified time: 2022-09-18 23:32:02
|
* @Last Modified time: 2022-09-18 23:32:02
|
||||||
*/
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include "loop.h"
|
#include "loop.h"
|
||||||
#include "exec.h"
|
#include "exec.h"
|
||||||
#include "line.h"
|
#include "line.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
@ -17,8 +19,8 @@ int loop() {
|
||||||
|
|
||||||
char *line;
|
char *line;
|
||||||
char **args;
|
char **args;
|
||||||
printf(ANSI_COLOR_GREEN"> "ANSI_COLOR_RESET);
|
line = readline(ANSI_COLOR_GREEN"> "ANSI_COLOR_RESET);
|
||||||
line = readLine();
|
add_history(line);
|
||||||
args = splitLine(line);
|
args = splitLine(line);
|
||||||
status = commandExec(args);
|
status = commandExec(args);
|
||||||
free(line);
|
free(line);
|
||||||
|
|
Loading…
Reference in a new issue