TEMP COMMIT
This commit is contained in:
parent
8117e9250a
commit
2b3735b90b
325
bt.c
325
bt.c
@ -36,38 +36,57 @@ typedef struct {
|
|||||||
} Scrollback;
|
} Scrollback;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int master;
|
int parent;
|
||||||
int child;
|
int child;
|
||||||
} FileDescriptors;
|
} FileDescriptors;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Scrollback* sb;
|
Scrollback* sb;
|
||||||
|
char *readline;
|
||||||
|
int rline_len;
|
||||||
TTF_Font* font;
|
TTF_Font* font;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
} Context;
|
} Context;
|
||||||
|
|
||||||
|
void
|
||||||
|
die(const char *errstr, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, errstr);
|
||||||
|
vfprintf(stderr, errstr, ap);
|
||||||
|
va_end(ap);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
void spawn(FileDescriptors *fds) {
|
void spawn(FileDescriptors *fds) {
|
||||||
openpty(&fds->master, &fds->child, NULL, NULL, NULL);
|
openpty(&fds->parent, &fds->child, NULL, NULL, NULL);
|
||||||
|
// pid_t p = forkpty(&fds->parent, NULL, NULL, NULL);
|
||||||
pid_t p = fork();
|
pid_t p = fork();
|
||||||
if (p == 0) {
|
if (p == 0) {
|
||||||
close(fds->master);
|
close(fds->parent);
|
||||||
|
|
||||||
setsid();
|
setsid();
|
||||||
ioctl(fds->child, TIOCSCTTY, NULL);
|
if (ioctl(fds->child, TIOCSCTTY, NULL))
|
||||||
|
die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
|
||||||
dup2(fds->child, 0);
|
dup2(fds->child, 0);
|
||||||
dup2(fds->child, 1);
|
dup2(fds->child, 1);
|
||||||
dup2(fds->child, 2);
|
dup2(fds->child, 2);
|
||||||
|
if (fds->child > 2) {
|
||||||
|
close(fds->child);
|
||||||
|
}
|
||||||
|
|
||||||
execle("/bin/bash", "-/bin/bash", (char *)NULL, (char *[]){ "TERM=dumb", NULL });
|
// execle("/usr/bin/zsh", "-/usr/bin/zsh", (char *)NULL, (char *[]){ "TERM=dumb", NULL });
|
||||||
|
execle("/usr/bin/dash", "-/usr/bin/dash", (char *)NULL, (char *)NULL);
|
||||||
} else {
|
} else {
|
||||||
|
fds->child = p;
|
||||||
close(fds->child);
|
close(fds->child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int read_pty(FileDescriptors *fds, Scrollback *sb) {
|
int read_pty(FileDescriptors *fds, Scrollback *sb) {
|
||||||
ssize_t nread;
|
char readbuf[512];
|
||||||
char readbuf[256];
|
ssize_t nread = read(fds->parent, readbuf, sizeof(readbuf));
|
||||||
switch (nread = read(fds->master, readbuf, sizeof(readbuf))) {
|
switch (nread) {
|
||||||
case -1:
|
case -1:
|
||||||
if (errno == EAGAIN) {
|
if (errno == EAGAIN) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -80,13 +99,53 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) {
|
|||||||
default:
|
default:
|
||||||
if (sb->length + nread > sb->capacity) {
|
if (sb->length + nread > sb->capacity) {
|
||||||
sb->capacity *= 2;
|
sb->capacity *= 2;
|
||||||
sb->buf = realloc(sb->buf, sb->capacity);
|
void *buf = realloc(sb->buf, sb->capacity);
|
||||||
|
if (buf == NULL) {
|
||||||
|
fprintf(stderr, "We couldn't get anymore memory");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
sb->buf = buf;
|
||||||
|
printf("Reallocated scrollback buffer\n");
|
||||||
}
|
}
|
||||||
if (readbuf[nread - 1] == '\n') {
|
if (readbuf[nread - 1] == '\n') {
|
||||||
|
// printf("are you firing when you shouldnt?\n");
|
||||||
nread--;
|
nread--;
|
||||||
}
|
}
|
||||||
readbuf[nread] = '\0';
|
for (int i = 0; i < 24; i++) {
|
||||||
strcat(sb->buf, readbuf);
|
// printf("%c\n", readbuf[i]);
|
||||||
|
}
|
||||||
|
// int read length =
|
||||||
|
printf("Reading %ld\n", nread);
|
||||||
|
// if ((char)readbuf[0] == '\b') {
|
||||||
|
// if (readbuf[0] == 'l') {
|
||||||
|
for (int i = 0; i < nread; i++) {
|
||||||
|
if (readbuf[i] == '\n') {
|
||||||
|
// printf("*");
|
||||||
|
} else if (readbuf[i] == '\r') {
|
||||||
|
// printf("@");
|
||||||
|
} else {
|
||||||
|
printf("%c\n", readbuf[i]);
|
||||||
|
// printf("%c - %d\n", readbuf[i], i);
|
||||||
|
}
|
||||||
|
// fflush(stdout);
|
||||||
|
}
|
||||||
|
// printf("\n");
|
||||||
|
// }
|
||||||
|
if (readbuf[nread - 1] == '\r') {
|
||||||
|
// readbuf[nread - 1] = '\n';
|
||||||
|
}
|
||||||
|
// TODO: This is bad, what if we read all 256 bytes
|
||||||
|
// printf("Read %li bytes, length = %d\n", nread, sb->length);
|
||||||
|
// printf("readbuf:\n %s\n", readbuf);
|
||||||
|
// readbuf[nread] = '\0';
|
||||||
|
// sb->length += nread - 1;
|
||||||
|
// strcat(sb->buf, readbuf);
|
||||||
|
memcpy(sb->buf + sb->length, readbuf, nread);
|
||||||
|
sb->length += nread;
|
||||||
|
// sb->buf[sb->length] = '\0';
|
||||||
|
// printf("After Scrollback:\n%s\n", sb->buf);
|
||||||
|
// printf("Scrollback:\n %s\n", sb->buf);
|
||||||
|
// printf("------------------\n");
|
||||||
return nread;
|
return nread;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,20 +153,27 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) {
|
|||||||
void render_scrollback(Context* ctx) {
|
void render_scrollback(Context* ctx) {
|
||||||
SDL_RenderClear(ctx->renderer);
|
SDL_RenderClear(ctx->renderer);
|
||||||
|
|
||||||
SDL_Color current_color = WHITE;
|
// SDL_Color current_color = WHITE;
|
||||||
int col_max = 80;
|
int col_max = 120;
|
||||||
char row_buf[col_max];
|
char row_buf[col_max];
|
||||||
int nrow = 0;
|
int nrow = 0;
|
||||||
int ncol = 0;
|
int ncol = 0;
|
||||||
int row_height = 18;
|
// int row_height = 18;
|
||||||
float row_posx = 0;
|
|
||||||
Scrollback sb = *ctx->sb;
|
Scrollback sb = *ctx->sb;
|
||||||
for (int c = 0; c <= sb.length; c++) {
|
for (int c = 0; c < sb.length; c++) {
|
||||||
// for (int c = 0; c <= -1; c++) {
|
// for (int c = 0; c <= -1; c++) {
|
||||||
if (sb.buf[c] == '\r') {
|
// if (sb.buf[c] == '\r') {
|
||||||
continue;
|
// nrow++;
|
||||||
} else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) {
|
// ncol = 0;
|
||||||
row_buf[ncol] = '\0';
|
// continue;
|
||||||
|
// TODO: In order for us to do the ncol >= col_max thing (meaning we're wrapping)
|
||||||
|
// We have to make sure we handle subsequent newlines
|
||||||
|
// } else if (sb.buf[c] == '\0' || ncol >= col_max) {
|
||||||
|
// } else if (sb.buf[c] == '\0' || ncol >= col_max) {
|
||||||
|
// if (sb.buf[c] == '\r' || sb.buf[c] == '\0' || ncol >= col_max) {
|
||||||
|
if (sb.buf[c] == '\r' || c == sb.length - 1 || ncol >= col_max) {
|
||||||
|
// } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0') {
|
||||||
|
// row_buf[ncol] = '\0';
|
||||||
// TODO: Render new line or something?
|
// TODO: Render new line or something?
|
||||||
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
|
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
|
||||||
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
|
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
|
||||||
@ -119,7 +185,7 @@ void render_scrollback(Context* ctx) {
|
|||||||
|
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
rect.x = 0;
|
rect.x = 0;
|
||||||
rect.y = nrow * 15;
|
rect.y = nrow * 20;
|
||||||
rect.w = surface->w;
|
rect.w = surface->w;
|
||||||
rect.h = surface->h;
|
rect.h = surface->h;
|
||||||
|
|
||||||
@ -131,61 +197,61 @@ void render_scrollback(Context* ctx) {
|
|||||||
|
|
||||||
nrow++;
|
nrow++;
|
||||||
ncol = 0;
|
ncol = 0;
|
||||||
row_posx = 0;
|
|
||||||
// Control sequence
|
// Control sequence
|
||||||
} else if (sb.buf[c] == '\x1b') {
|
} else if (sb.buf[c] == '\x1b') {
|
||||||
int c2 = c + 1;
|
int c2 = c + 1;
|
||||||
// Control Sequence Introducer
|
// Control Sequence Introducer
|
||||||
int csi_args[16];
|
// int csi_args[16];
|
||||||
char csi_code;
|
// char csi_code;
|
||||||
int nargs = 0;
|
// int nargs = 0;
|
||||||
if (sb.buf[c2] == '[') {
|
if (sb.buf[c2] == '[') {
|
||||||
char *esc_buf = sb.buf + c2 + 1;
|
// char *esc_buf = sb.buf + c2 + 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
char *substr;
|
// char *substr;
|
||||||
long num = strtol(esc_buf, &substr, 10);
|
// // long num = strtol(esc_buf, &substr, 10);
|
||||||
if (esc_buf != substr) {
|
// if (esc_buf != substr) {
|
||||||
csi_args[nargs++] = num;
|
// // csi_args[nargs++] = num;
|
||||||
esc_buf = substr;
|
// esc_buf = substr;
|
||||||
if (substr[0] == ';') {
|
// if (substr[0] == ';') {
|
||||||
esc_buf++;
|
// esc_buf++;
|
||||||
}
|
// }
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
csi_code = substr[0];
|
// csi_code = substr[0];
|
||||||
SDL_Color new_color = WHITE;
|
// SDL_Color new_color = WHITE;
|
||||||
switch (csi_code) {
|
// switch (csi_code) {
|
||||||
case 'm':
|
// case 'm':
|
||||||
for (int i = 0; i < nargs; i++) {
|
// for (int i = 0; i < nargs; i++) {
|
||||||
if (csi_args[i] == 31) {
|
// if (csi_args[i] == 31) {
|
||||||
new_color = RED;
|
// new_color = RED;
|
||||||
} else if (csi_args[i] == 32) {
|
// } else if (csi_args[i] == 32) {
|
||||||
new_color = GREEN;
|
// new_color = GREEN;
|
||||||
} else if (csi_args[i] == 33) {
|
// } else if (csi_args[i] == 33) {
|
||||||
new_color = YELLOW;
|
// new_color = YELLOW;
|
||||||
} else if (csi_args[i] == 34) {
|
// } else if (csi_args[i] == 34) {
|
||||||
new_color = BLUE;
|
// new_color = BLUE;
|
||||||
} else if (csi_args[i] == 35) {
|
// } else if (csi_args[i] == 35) {
|
||||||
new_color = MAGENTA;
|
// new_color = MAGENTA;
|
||||||
} else if (csi_args[i] == 36) {
|
// } else if (csi_args[i] == 36) {
|
||||||
new_color = SKYBLUE;
|
// new_color = SKYBLUE;
|
||||||
} else if (csi_args[i] == 0) {
|
// } else if (csi_args[i] == 0) {
|
||||||
new_color = RAYWHITE;
|
// new_color = RAYWHITE;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
row_buf[ncol] = '\0';
|
// row_buf[ncol] = '\0';
|
||||||
// TODO: This looks like the actual place where we
|
// // TODO: This looks like the actual place where we
|
||||||
// draw the text at a line
|
// // draw the text at a line
|
||||||
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
|
// // Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
|
||||||
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
|
// // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
|
||||||
current_color = new_color;
|
// // current_color = new_color;
|
||||||
ncol = 0;
|
// ncol = 0;
|
||||||
c += (substr) - (sb.buf + c);
|
// c += (substr) - (sb.buf + c);
|
||||||
// TODO: Get the text height
|
// // TODO: Get the text height
|
||||||
// int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
|
// // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
|
||||||
// row_posx += width + 0.85;
|
// // row_posx += width + 0.85;
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -226,10 +292,12 @@ int main(void) {
|
|||||||
TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16);
|
TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16);
|
||||||
|
|
||||||
struct winsize sz;
|
struct winsize sz;
|
||||||
int result = ioctl(fds.master, TIOCGWINSZ, &sz);
|
// int result = ioctl(fds.parent, TIOCGWINSZ, &sz);
|
||||||
|
ioctl(fds.parent, TIOCGWINSZ, &sz);
|
||||||
sz.ws_col = 200;
|
sz.ws_col = 200;
|
||||||
sz.ws_row = 120;
|
sz.ws_row = 120;
|
||||||
result = ioctl(fds.master, TIOCSWINSZ, &sz);
|
// result = ioctl(fds.parent, TIOCSWINSZ, &sz);
|
||||||
|
ioctl(fds.parent, TIOCSWINSZ, &sz);
|
||||||
|
|
||||||
// TODO: It would be nice to figure out how to do this in SDL
|
// TODO: It would be nice to figure out how to do this in SDL
|
||||||
// SetTargetFPS(60);
|
// SetTargetFPS(60);
|
||||||
@ -238,22 +306,22 @@ int main(void) {
|
|||||||
sb.buf = malloc(sb.capacity);
|
sb.buf = malloc(sb.capacity);
|
||||||
sb.buf[0] = '\0';
|
sb.buf[0] = '\0';
|
||||||
sb.length = 0;
|
sb.length = 0;
|
||||||
char buf[128];
|
|
||||||
int buf_len = 0;
|
|
||||||
|
|
||||||
int flags = fcntl(fds.master, F_GETFL);
|
int flags = fcntl(fds.parent, F_GETFL);
|
||||||
flags |= O_NONBLOCK;
|
flags |= O_NONBLOCK;
|
||||||
fcntl(fds.master, F_SETFL, flags);
|
fcntl(fds.parent, F_SETFL, flags);
|
||||||
fd_set rset;
|
fd_set rset;
|
||||||
FD_ZERO(&rset);
|
FD_ZERO(&rset);
|
||||||
FD_SET(fds.master, &rset);
|
FD_SET(fds.parent, &rset);
|
||||||
|
|
||||||
sb.ypos = 0;
|
sb.ypos = 0;
|
||||||
|
|
||||||
Context ctx = {
|
Context ctx = {
|
||||||
.sb = &sb,
|
.sb = &sb,
|
||||||
.font = font,
|
.font = font,
|
||||||
.renderer = renderer
|
.renderer = renderer,
|
||||||
|
.readline = malloc(256),
|
||||||
|
.rline_len = 0
|
||||||
};
|
};
|
||||||
bool new_read = false;
|
bool new_read = false;
|
||||||
bool new_char = false;
|
bool new_char = false;
|
||||||
@ -264,43 +332,74 @@ int main(void) {
|
|||||||
if (e.type == SDL_QUIT) {
|
if (e.type == SDL_QUIT) {
|
||||||
quit = true;
|
quit = true;
|
||||||
} else if (e.type == SDL_KEYDOWN) {
|
} else if (e.type == SDL_KEYDOWN) {
|
||||||
|
// printf("Nothing is going on right?\n");
|
||||||
}
|
char key = e.key.keysym.sym;
|
||||||
char key = e.key.keysym.sym;
|
// TODO: Translate this all to SDL2
|
||||||
// TODO: Translate this all to SDL2
|
// int key = GetCharPressed();
|
||||||
// int key = GetCharPressed();
|
// TODO: Don't remember why I was doing this
|
||||||
// TODO: Don't remember why I was doing this
|
// key = GetCharPressed();
|
||||||
// key = GetCharPressed();
|
// new_char = true;
|
||||||
// new_char = true;
|
if (key == SDL_SCANCODE_F1) {
|
||||||
if ((key >= 32) && (key <= 125)) {
|
printf("Debug Printing Scrollback Length %d:\n", sb.length);
|
||||||
buf[buf_len] = (char)key;
|
for (int i = 0; i < sb.length; i++) {
|
||||||
buf[buf_len + 1] = '\0';
|
if (sb.buf[i] == '\r') {
|
||||||
buf_len++;
|
// printf("\r");
|
||||||
sb.buf[sb.length] = (char)key;
|
} else if (sb.buf[i] == ' ') {
|
||||||
sb.buf[sb.length + 1] = '\0';
|
// printf("_");
|
||||||
sb.length++;
|
} else {
|
||||||
}
|
// printf("%c - %d\n", readbuf[i], i);
|
||||||
|
}
|
||||||
if (key == SDLK_RETURN) {
|
printf("%d-%c\n", i, sb.buf[i]);
|
||||||
sb.length -= buf_len;
|
// fflush(stdout);
|
||||||
sb.buf[sb.length] = '\0';
|
}
|
||||||
write(fds.master, buf, buf_len);
|
printf("\n");
|
||||||
write(fds.master, "\r", 1);
|
printf("============\n");
|
||||||
buf[0] = '\0';
|
} else if ((key >= 32) && (key <= 125)) {
|
||||||
buf_len = 0;
|
ctx.readline[ctx.rline_len] = (char)key;
|
||||||
new_char = true;
|
ctx.rline_len++;
|
||||||
}
|
// printf("%d\n", sb.length);
|
||||||
if (key == SDLK_BACKSPACE) {
|
// sb.buf[sb.length] = (char)key;
|
||||||
if (buf_len > 0) {
|
// sb.length++;
|
||||||
buf_len--;
|
// sb.buf[sb.length] = '\0';
|
||||||
buf[buf_len] = '\0';
|
|
||||||
sb.length--;
|
|
||||||
sb.buf[sb.length] = '\0';
|
|
||||||
}
|
}
|
||||||
new_char = true;
|
|
||||||
|
if (key == SDLK_RETURN) {
|
||||||
|
// buf[buf_len] = '\0';
|
||||||
|
// buf_len++;
|
||||||
|
// ctx.readline[ctx.rline_len] = '\n';
|
||||||
|
// ctx.rline_len++;
|
||||||
|
ctx.readline[ctx.rline_len] = '\r';
|
||||||
|
ctx.rline_len++;
|
||||||
|
// printf("%s\n", buf);
|
||||||
|
write(fds.parent, ctx.readline, ctx.rline_len);
|
||||||
|
// write(fds.parent, "\r", 1);
|
||||||
|
// buf[0] = '\0';
|
||||||
|
// sb.length -= buf_len-1;
|
||||||
|
// sb.length++;
|
||||||
|
// sb.buf[sb.length] = '\n';
|
||||||
|
// sb.buf[sb.length] = '\0';
|
||||||
|
// sb.buf[sb.length++] = '\0';
|
||||||
|
ctx.rline_len = 0;
|
||||||
|
new_char = true;
|
||||||
|
}
|
||||||
|
if (key == SDLK_BACKSPACE) {
|
||||||
|
// printf("Writing %d\n", key);
|
||||||
|
// char bs = 'c';
|
||||||
|
// char huh[] = {'b'};
|
||||||
|
// write(fds.parent, huh, 1);
|
||||||
|
// write(fds.parent, &key, 1);
|
||||||
|
if (ctx.rline_len > 0) {
|
||||||
|
ctx.rline_len--;
|
||||||
|
ctx.readline[ctx.rline_len] = '\0';
|
||||||
|
sb.length--;
|
||||||
|
sb.buf[sb.length] = '\0';
|
||||||
|
}
|
||||||
|
new_char = true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
float scroll_speed = 35.5f;
|
// float scroll_speed = 35.5f;
|
||||||
// TODO: We have to convert both these lines to SDL
|
// TODO: We have to convert both these lines to SDL
|
||||||
// sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
|
// sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
|
||||||
// sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
|
// sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
|
||||||
@ -324,7 +423,6 @@ int main(void) {
|
|||||||
|
|
||||||
int nread = read_pty(&fds, &sb);
|
int nread = read_pty(&fds, &sb);
|
||||||
if (nread > 0) {
|
if (nread > 0) {
|
||||||
sb.length += nread;
|
|
||||||
new_read = true;
|
new_read = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,6 +430,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(sb.buf);
|
free(sb.buf);
|
||||||
|
free(ctx.readline);
|
||||||
TTF_CloseFont(font);
|
TTF_CloseFont(font);
|
||||||
TTF_Quit();
|
TTF_Quit();
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user