started using readline library for user input

This commit is contained in:
Stephen Toth 2023-09-23 13:56:05 -04:00
parent 32e65cbf05
commit 1d6bc23705
4 changed files with 60 additions and 5 deletions

19
.vscode/launch.json vendored
View File

@ -4,6 +4,25 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/bin/cterm",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
"name": "colorwrap",
"type": "cppdbg",

View File

@ -6,13 +6,14 @@ TARGET := bin/cterm
SRCEXT := c
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -Wall
LIB := -L lib
CFLAGS := -Wall -lreadline
LIB := -L lib -lreadline
INC := -I include
$(TARGET): $(OBJECTS)
@echo " Linking..."
@echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) -O3
@echo " Done!"
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo " Building..."
@ -28,5 +29,7 @@ test:
trace:
strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh
tracef:
strace -fo $(TESTDIR)/trace.lp $(TARGET) /bin/sh
.PHONY: clean

View File

@ -8,6 +8,9 @@ Project image/gif
Requirements
RHEL-like:
`sudo dnf install readline-devel`
## Installation
`make`
@ -16,6 +19,13 @@ Requirements
`bin/run`
## Considerations and Caveats
Since this program buffers input by line, It will not handle TUI-based commands
Examples include less and vim
If you need to use these kinds of applications, do not use this program to wrap your shell.
## Example
`bin/run`

View File

@ -6,8 +6,10 @@
#include <sys/wait.h>
#include <sys/types.h>
#include <pty.h>
#include <fcntl.h>
#include <sysexits.h>
#include <fcntl.h>
#include <readline/readline.h>
#include <readline/history.h>
#define BUFCHUNKSIZE 4096
#define BUFSAFEPAD 8
@ -25,6 +27,25 @@ void child_process(char *argv[]){
}
size_t getuserinput(char* input_buffer, size_t max_buffer_size, int fd) {
int in_char;
size_t total_read_length;
ssize_t read_result;
//read_result = read(fd, &in_char, 1);
while (1) {
printf("reading char\n");
in_char = fgetc(stdin);
printf("read char\n");
}
//if (inchar < 0){
// //error condition
// exit(1);
//}
return 1;
}
int main(int argc, char *argv[]) {
int master;
pid_t child_pid;
@ -58,22 +79,24 @@ int main(int argc, char *argv[]) {
// ideally I would want to read from the pipe and buffer the output, rewriting the line on change
// whenever it sees a newline, it should send it to the processor and print to stdout, with that newline
//
memset(childoutbuffer, 0, BUFCHUNKSIZE);
while(1){
ssize_t count = read(master, childoutbuffer, BUFCHUNKSIZE);
if (count == -1){
if(errno == EAGAIN){
break;
} else exit(1);
} else exit(0);
} else if (count == 0 ){
exit(0);
}
write(STDOUT_FILENO, childoutbuffer, count);
}
memset(childoutbuffer, 0, BUFCHUNKSIZE);
fgets(childoutbuffer, BUFCHUNKSIZE, stdin);
strncpy(childoutbuffer, readline("~>"), BUFCHUNKSIZE-1);
//printf("%s\n", childoutbuffer);
write(master, childoutbuffer, strlen(childoutbuffer));
write(master, "\n", 1);
//memset(childoutbuffer, 0, BUFCHUNKSIZE);
//ssize_t count = strlen(childoutbuffer);