started using readline library for user input
This commit is contained in:
parent
32e65cbf05
commit
1d6bc23705
|
|
@ -4,6 +4,25 @@
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"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",
|
"name": "colorwrap",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
|
|
|
||||||
7
Makefile
7
Makefile
|
|
@ -6,13 +6,14 @@ TARGET := bin/cterm
|
||||||
SRCEXT := c
|
SRCEXT := c
|
||||||
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
|
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
|
||||||
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
|
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
|
||||||
CFLAGS := -Wall
|
CFLAGS := -Wall -lreadline
|
||||||
LIB := -L lib
|
LIB := -L lib -lreadline
|
||||||
INC := -I include
|
INC := -I include
|
||||||
|
|
||||||
$(TARGET): $(OBJECTS)
|
$(TARGET): $(OBJECTS)
|
||||||
@echo " Linking..."
|
@echo " Linking..."
|
||||||
@echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) -O3
|
@echo " $(CC) $^ -o $(TARGET) $(LIB)"; $(CC) $^ -o $(TARGET) $(LIB) -O3
|
||||||
|
@echo " Done!"
|
||||||
|
|
||||||
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
|
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
|
||||||
@echo " Building..."
|
@echo " Building..."
|
||||||
|
|
@ -28,5 +29,7 @@ test:
|
||||||
|
|
||||||
trace:
|
trace:
|
||||||
strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh
|
strace -o $(TESTDIR)/trace.lp $(TARGET) /bin/sh
|
||||||
|
tracef:
|
||||||
|
strace -fo $(TESTDIR)/trace.lp $(TARGET) /bin/sh
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
|
|
|
||||||
10
README.md
10
README.md
|
|
@ -8,6 +8,9 @@ Project image/gif
|
||||||
|
|
||||||
Requirements
|
Requirements
|
||||||
|
|
||||||
|
RHEL-like:
|
||||||
|
`sudo dnf install readline-devel`
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
`make`
|
`make`
|
||||||
|
|
@ -16,6 +19,13 @@ Requirements
|
||||||
|
|
||||||
`bin/run`
|
`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
|
## Example
|
||||||
|
|
||||||
`bin/run`
|
`bin/run`
|
||||||
|
|
|
||||||
29
src/main.c
29
src/main.c
|
|
@ -6,8 +6,10 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <pty.h>
|
#include <pty.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <sysexits.h>
|
#include <sysexits.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
|
#include <readline/history.h>
|
||||||
|
|
||||||
#define BUFCHUNKSIZE 4096
|
#define BUFCHUNKSIZE 4096
|
||||||
#define BUFSAFEPAD 8
|
#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 main(int argc, char *argv[]) {
|
||||||
int master;
|
int master;
|
||||||
pid_t child_pid;
|
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
|
// 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
|
// 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){
|
while(1){
|
||||||
ssize_t count = read(master, childoutbuffer, BUFCHUNKSIZE);
|
ssize_t count = read(master, childoutbuffer, BUFCHUNKSIZE);
|
||||||
if (count == -1){
|
if (count == -1){
|
||||||
if(errno == EAGAIN){
|
if(errno == EAGAIN){
|
||||||
break;
|
break;
|
||||||
} else exit(1);
|
} else exit(0);
|
||||||
} else if (count == 0 ){
|
} else if (count == 0 ){
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
write(STDOUT_FILENO, childoutbuffer, count);
|
write(STDOUT_FILENO, childoutbuffer, count);
|
||||||
}
|
}
|
||||||
memset(childoutbuffer, 0, BUFCHUNKSIZE);
|
memset(childoutbuffer, 0, BUFCHUNKSIZE);
|
||||||
fgets(childoutbuffer, BUFCHUNKSIZE, stdin);
|
strncpy(childoutbuffer, readline("~>"), BUFCHUNKSIZE-1);
|
||||||
|
|
||||||
//printf("%s\n", childoutbuffer);
|
//printf("%s\n", childoutbuffer);
|
||||||
write(master, childoutbuffer, strlen(childoutbuffer));
|
write(master, childoutbuffer, strlen(childoutbuffer));
|
||||||
|
write(master, "\n", 1);
|
||||||
|
|
||||||
//memset(childoutbuffer, 0, BUFCHUNKSIZE);
|
//memset(childoutbuffer, 0, BUFCHUNKSIZE);
|
||||||
//ssize_t count = strlen(childoutbuffer);
|
//ssize_t count = strlen(childoutbuffer);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue