Skip to content

Commit

Permalink
nterm: remove outdated terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Jan 17, 2025
1 parent 130afbc commit d5eae51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion nterm/src/ansi.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void parseCSI() {

char buffer[64];
sprintf(buffer, "\e[%d;%dR", terminal.y+1, terminal.x+1);
write(terminal.master, buffer, strlen(buffer));
write(terminal.primary, buffer, strlen(buffer));
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions nterm/src/include/nterm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define BUFFER_SIZE 1024

typedef struct {
int master; // master file descriptor
int primary; // primary terminal descriptor
int width, height; // pixels
int wchar, hchar; // characters
int x, y; // cursor position
Expand All @@ -41,8 +41,8 @@ typedef struct {
uint16_t scancodes[BUFFER_SIZE]; // keyboard scan codes
uint8_t printableKeys[BUFFER_SIZE]; // readable key presses

int slaveCount;
uint8_t slaveOutput[BUFFER_SIZE]; // output of the slave
int outputCount;
uint8_t output[BUFFER_SIZE];
} TerminalStatus;

extern TerminalStatus terminal;
Expand Down
48 changes: 23 additions & 25 deletions nterm/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

TerminalStatus terminal;

int child(char *slavepty, int maxfd, int argc, char **argv) {
int child(char *secondarypty, int maxfd, int argc, char **argv) {
if(maxfd) {
for(int i = 0; i <= maxfd; i++) close(i);
}

// open the slave pty for stdin, out, err
// open the secondary pty for stdin, out, err
for(int i = 0; i < 3; i++) {
if(open(slavepty, O_RDWR) < 0) return -1;
if(open(secondarypty, O_RDWR) < 0) return -1;
}

tcsetpgrp(STDIN_FILENO, getpid());
Expand All @@ -55,17 +55,17 @@ int main(int argc, char **argv) {
// start by creating a terminal
memset(&terminal, 0, sizeof(TerminalStatus));

terminal.master = posix_openpt(O_RDWR | O_NONBLOCK | O_CLOEXEC);
if(terminal.master < 0) return -1;
terminal.primary = posix_openpt(O_RDWR | O_NONBLOCK | O_CLOEXEC);
if(terminal.primary < 0) return -1;

grantpt(terminal.master);
unlockpt(terminal.master);
grantpt(terminal.primary);
unlockpt(terminal.primary);

char *tempSlave = ptsname(terminal.master);
if(!tempSlave) return -1;
char *slaveName = malloc(strlen(tempSlave) + 1);
if(!slaveName) return -1;
strcpy(slaveName, tempSlave);
char *tempSecondary = ptsname(terminal.primary);
if(!tempSecondary) return -1;
char *secondaryName = malloc(strlen(tempSecondary) + 1);
if(!secondaryName) return -1;
strcpy(secondaryName, tempSecondary);

// open the frame buffer and keyboard
terminal.lfb = open("/dev/lfb0", O_RDWR | O_CLOEXEC);
Expand All @@ -86,8 +86,6 @@ int main(int argc, char **argv) {
terminal.cursor = 1;
terminal.bg = ttyColors[0];
terminal.fg = ttyColors[7];
terminal.keyCount = 0;
terminal.slaveCount = 0;

terminal.wchar = terminal.width / 8;
terminal.hchar = terminal.height / 16;
Expand All @@ -107,15 +105,15 @@ int main(int argc, char **argv) {

// let the pty driver know the terminal's size
struct winsize ws;
if(tcgetwinsize(terminal.master, &ws)) {
if(tcgetwinsize(terminal.primary, &ws)) {
ntermPuts("failed to request terminal window size: ");
ntermPuts(strerror(errno));
for(;;);
}

ws.ws_col = terminal.wchar;
ws.ws_row = terminal.hchar;
if(tcsetwinsize(terminal.master, &ws)) {
if(tcsetwinsize(terminal.primary, &ws)) {
ntermPuts("failed to set terminal window size: ");
ntermPuts(strerror(errno));
for(;;);
Expand All @@ -124,13 +122,13 @@ int main(int argc, char **argv) {
// fork and spawn a test process
pid_t pid = fork();
if(pid < 0) return -1;
if(!pid) return child(slaveName, terminal.kbd, argc, argv);
if(!pid) return child(secondaryName, terminal.kbd, argc, argv);

int shift = 0, control = 0;
int busy;

// idle loop where we read from the keyboard and write to the master pty,
// and then read from the master pty and draw to the screen
// idle loop where we read from the keyboard and write to the primary pty,
// and then read from the primary pty and draw to the screen
for(;;) {
busy = 0;

Expand Down Expand Up @@ -172,12 +170,12 @@ int main(int argc, char **argv) {
switch(c) {
case 'c': // Ctrl+C
c = 0x03; // end of text
write(terminal.master, &c, 1);
write(terminal.primary, &c, 1);
break;

case 'd': // Ctrl+D
c = 0x04; // end of transmission
write(terminal.master, &c, 1);
write(terminal.primary, &c, 1);
break;
}
} else {
Expand All @@ -189,18 +187,18 @@ int main(int argc, char **argv) {

// send the key presses to the children processes on this terminal
if(terminal.keyCount) {
write(terminal.master, terminal.printableKeys, terminal.keyCount);
write(terminal.primary, terminal.printableKeys, terminal.keyCount);
}
memset(terminal.printableKeys, 0, sizeof(terminal.printableKeys));
terminal.keyCount = 0;
}

// read from the terminal to draw on the screen
s = read(terminal.master, terminal.slaveOutput, BUFFER_SIZE);
s = read(terminal.primary, terminal.output, BUFFER_SIZE);
if(s > 0 && s <= BUFFER_SIZE) {
busy = 1;
terminal.slaveCount = s;
ntermPutcn((const char *)terminal.slaveOutput, terminal.slaveCount);
terminal.outputCount = s;
ntermPutcn((const char *)terminal.output, terminal.outputCount);
}

if(!busy) sched_yield();
Expand Down

0 comments on commit d5eae51

Please sign in to comment.