Progress I guess

This commit is contained in:
Joseph Ferano 2023-11-15 17:31:59 +07:00
parent 2b3735b90b
commit f15014d84e
2 changed files with 62 additions and 36 deletions

View File

@ -1,6 +1,7 @@
P=bt
CC=gcc
CFLAGS=-g -Wall -Wextra -pedantic -O0
CFLAGS=-fsanitize=address -g -Wall -Wextra -pedantic -O0
# CFLAGS=-Wall -Wextra -pedantic -O2
LDLIBS=-lSDL2 -lSDL2_ttf
RM=rm -vf

95
bt.c
View File

@ -59,24 +59,31 @@ die(const char *errstr, ...)
exit(1);
}
void spawn(FileDescriptors *fds) {
openpty(&fds->parent, &fds->child, NULL, NULL, NULL);
if (openpty(&fds->parent, &fds->child, NULL, NULL, NULL)) {
fprintf(stderr, "openpty() failed: %s\n", strerror(errno));
exit(1);
}
// pid_t p = forkpty(&fds->parent, NULL, NULL, NULL);
// int _stdout = dup(STDOUT_FILENO);
// dup2(fds->child, STDOUT_FILENO);
pid_t p = fork();
if (p == 0) {
close(fds->parent);
setsid();
if (ioctl(fds->child, TIOCSCTTY, NULL))
die("ioctl TIOCSCTTY failed: %s\n", strerror(errno));
dup2(fds->child, 0);
dup2(fds->child, 1);
dup2(fds->child, 2);
if (fds->child > 2) {
close(fds->child);
}
dup2(fds->child, STDIN_FILENO);
dup2(fds->child, STDOUT_FILENO);
dup2(fds->child, STDERR_FILENO);
close(fds->parent);
close(fds->child);
// execle("/usr/bin/zsh", "-/usr/bin/zsh", (char *)NULL, (char *[]){ "TERM=dumb", NULL });
execle("/usr/bin/dash", "-/usr/bin/dash", (char *)NULL, (char *)NULL);
// execle("/usr/bin/dash", "-/usr/bin/dash", (char *)NULL, (char *)NULL);
// execle("/usr/bin/bash", "-/usr/bin/bash", (char *)NULL, (char *)NULL);
char **args = NULL;
execvp("/usr/bin/bash", args);
// execvp("/usr/bin/dash", args);
} else {
fds->child = p;
close(fds->child);
@ -115,25 +122,26 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) {
// printf("%c\n", readbuf[i]);
}
// int read length =
printf("Reading %ld\n", nread);
// 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);
// if (readbuf[i] == '\n') {
// // printf("*");
if (readbuf[i] == '\r') {
// printf("<CR>");
readbuf[i] = '\n';
}
// fflush(stdout);
// printf("%d\n", readbuf[i]);
// printf("%c - %d\n", readbuf[i], i);
// }
// fflush(stdout);
}
// printf("\n");
// }
if (readbuf[nread - 1] == '\r') {
// 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);
@ -142,9 +150,15 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) {
// strcat(sb->buf, readbuf);
memcpy(sb->buf + sb->length, readbuf, nread);
sb->length += nread;
// sb->buf[sb->length] = '\0';
sb->buf[sb->length+1] = '\0';
// printf("After Scrollback:\n%s\n", sb->buf);
// printf("Read Buf:\n %s\n", readbuf);
// printf("Scrollback:\n %s\n", sb->buf);
for (int i = 0; i < sb->length; i++) {
printf("%d ", sb->buf[i]);
fflush(stdout);
}
printf("\n");
// printf("------------------\n");
return nread;
}
@ -167,25 +181,31 @@ void render_scrollback(Context* ctx) {
// 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
// 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) {
if (sb.buf[c] == '\n' && ncol > 0 || c == sb.length - 1) {
// } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0') {
// row_buf[ncol] = '\0';
// ncol++;
row_buf[ncol] = '\0';
// TODO: Render new line or something?
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
SDL_Surface* surface =
TTF_RenderText_Blended(ctx->font, row_buf, WHITE);
if (surface == NULL) {
fprintf(stderr, "DONT PRINT THERES AN ERROR");
return;
}
// now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface);
SDL_Rect rect;
rect.x = 0;
rect.y = nrow * 20;
rect.w = surface->w;
rect.h = surface->h;
@ -342,16 +362,19 @@ int main(void) {
if (key == SDL_SCANCODE_F1) {
printf("Debug Printing Scrollback Length %d:\n", sb.length);
for (int i = 0; i < sb.length; i++) {
if (sb.buf[i] == '\r') {
// printf("\r");
} else if (sb.buf[i] == ' ') {
// printf("_");
} else {
// printf("%c - %d\n", readbuf[i], i);
}
printf("%d-%c\n", i, sb.buf[i]);
// fflush(stdout);
// if (sb.buf[i] == '\r') {
// // printf("\r");
// } else if (sb.buf[i] == ' ') {
// // printf("_");
// } else {
// // printf("%c - %d\n", readbuf[i], i);
// }
// printf("(%d-%c)", i, sb.buf[i]);
}
printf("%s\n", sb.buf);
fflush(stdout);
// printf("\n");
printf("%p", sb.buf);
printf("\n");
printf("============\n");
} else if ((key >= 32) && (key <= 125)) {
@ -396,7 +419,7 @@ int main(void) {
}
new_char = true;
}
}
}
// float scroll_speed = 35.5f;
@ -426,7 +449,9 @@ int main(void) {
new_read = true;
}
render_scrollback(&ctx);
if (nread > 0) {
render_scrollback(&ctx);
}
}
free(sb.buf);