Clean up this mess

This commit is contained in:
Joseph Ferano 2023-11-15 18:19:05 +07:00
parent f15014d84e
commit 2a9e327fdf

135
bt.c
View File

@ -78,9 +78,6 @@ void spawn(FileDescriptors *fds) {
close(fds->parent); close(fds->parent);
close(fds->child); 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/bash", "-/usr/bin/bash", (char *)NULL, (char *)NULL);
char **args = NULL; char **args = NULL;
execvp("/usr/bin/bash", args); execvp("/usr/bin/bash", args);
// execvp("/usr/bin/dash", args); // execvp("/usr/bin/dash", args);
@ -115,51 +112,17 @@ int read_pty(FileDescriptors *fds, Scrollback *sb) {
printf("Reallocated scrollback buffer\n"); printf("Reallocated scrollback buffer\n");
} }
if (readbuf[nread - 1] == '\n') { if (readbuf[nread - 1] == '\n') {
// printf("are you firing when you shouldnt?\n"); printf("are you firing when you shouldnt?\n");
nread--; nread--;
} }
for (int i = 0; i < 24; i++) {
// 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++) { for (int i = 0; i < nread; i++) {
// if (readbuf[i] == '\n') {
// // printf("*");
if (readbuf[i] == '\r') { if (readbuf[i] == '\r') {
// printf("<CR>");
readbuf[i] = '\n'; readbuf[i] = '\n';
} }
// printf("%d\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); memcpy(sb->buf + sb->length, readbuf, nread);
sb->length += nread; sb->length += nread;
sb->buf[sb->length+1] = '\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; return nread;
} }
} }
@ -172,26 +135,11 @@ void render_scrollback(Context* ctx) {
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 last_xpos = 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++) { if ((sb.buf[c] == '\n' && ncol > 0) || c == sb.length - 1) {
// if (sb.buf[c] == '\r') {
// nrow++;
// 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] == '\n' && ncol > 0 || c == sb.length - 1) {
// } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0') {
// ncol++;
row_buf[ncol] = '\0'; 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 = SDL_Surface* surface =
TTF_RenderText_Blended(ctx->font, row_buf, WHITE); TTF_RenderText_Blended(ctx->font, row_buf, WHITE);
@ -199,6 +147,7 @@ void render_scrollback(Context* ctx) {
fprintf(stderr, "DONT PRINT THERES AN ERROR"); fprintf(stderr, "DONT PRINT THERES AN ERROR");
return; return;
} }
// now you can convert it into a texture // now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface); SDL_Texture* texture = SDL_CreateTextureFromSurface(ctx->renderer, surface);
@ -209,14 +158,17 @@ void render_scrollback(Context* ctx) {
rect.w = surface->w; rect.w = surface->w;
rect.h = surface->h; rect.h = surface->h;
last_xpos = surface->w;
SDL_RenderCopy(ctx->renderer, texture, NULL, &rect); SDL_RenderCopy(ctx->renderer, texture, NULL, &rect);
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
if (c < sb.length - 1) {
nrow++; nrow++;
ncol = 0; ncol = 0;
}
// Control sequence // Control sequence
} else if (sb.buf[c] == '\x1b') { } else if (sb.buf[c] == '\x1b') {
@ -275,11 +227,39 @@ void render_scrollback(Context* ctx) {
break; break;
} }
} }
} else { } else if (sb.buf[c] != '\n') {
row_buf[ncol++] = sb.buf[c]; row_buf[ncol++] = sb.buf[c];
} }
} }
// We drew the scrollback buffer, now draw the user's prompt
if (ctx->rline_len > 0) {
SDL_Surface* surface =
TTF_RenderText_Blended(ctx->font, ctx->readline, 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 = last_xpos + 10;
rect.y = nrow * 20;
rect.w = surface->w;
rect.h = surface->h;
SDL_RenderCopy(ctx->renderer, texture, NULL, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
SDL_RenderPresent(ctx->renderer); SDL_RenderPresent(ctx->renderer);
} }
@ -352,7 +332,6 @@ 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();
@ -361,61 +340,25 @@ int main(void) {
// new_char = true; // new_char = true;
if (key == SDL_SCANCODE_F1) { if (key == SDL_SCANCODE_F1) {
printf("Debug Printing Scrollback Length %d:\n", sb.length); 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)", i, sb.buf[i]);
}
printf("%s\n", sb.buf); printf("%s\n", sb.buf);
fflush(stdout);
// printf("\n");
printf("%p", sb.buf);
printf("\n");
printf("============\n"); printf("============\n");
} else if ((key >= 32) && (key <= 125)) { } else if ((key >= 32) && (key <= 125)) {
ctx.readline[ctx.rline_len] = (char)key; ctx.readline[ctx.rline_len] = (char)key;
ctx.rline_len++; ctx.rline_len++;
// printf("%d\n", sb.length); ctx.readline[ctx.rline_len] = '\0';
// sb.buf[sb.length] = (char)key;
// sb.length++;
// sb.buf[sb.length] = '\0';
} }
if (key == SDLK_RETURN) { 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.readline[ctx.rline_len] = '\r';
ctx.rline_len++; ctx.rline_len++;
// printf("%s\n", buf);
write(fds.parent, ctx.readline, ctx.rline_len); 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; ctx.rline_len = 0;
new_char = true; new_char = true;
} }
if (key == SDLK_BACKSPACE) { 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) { if (ctx.rline_len > 0) {
ctx.rline_len--; ctx.rline_len--;
ctx.readline[ctx.rline_len] = '\0'; ctx.readline[ctx.rline_len] = '\0';
sb.length--;
sb.buf[sb.length] = '\0';
} }
new_char = true; new_char = true;
} }
@ -449,9 +392,9 @@ int main(void) {
new_read = true; new_read = true;
} }
if (nread > 0) { // if (nread > 0) {
render_scrollback(&ctx); render_scrollback(&ctx);
} // }
} }
free(sb.buf); free(sb.buf);