splitting things out

This commit is contained in:
Kyle Isom 2020-02-07 23:07:59 -08:00
parent 42658b8d98
commit 38a85b745e
6 changed files with 137 additions and 46 deletions

View File

@ -1,5 +1,5 @@
BIN := ke BIN := ke
OBJS := main.o OBJS := main.o terminal.o util.o
LDFLAGS := -static LDFLAGS := -static
CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99 CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99
@ -16,6 +16,7 @@ clean:
.PHONY: run .PHONY: run
run: $(BIN) run: $(BIN)
reset
./$(BIN) ./$(BIN)
%.o: %.c %.o: %.c

View File

@ -1,68 +1,62 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include "terminal.h"
/* entry_term contains a snapshot of the termios settings at startup. */ #include "util.h"
struct termios entry_term;
/* void
* A text editor needs the terminal to be in raw mode; but the default loop()
* is to be in canonical (cooked) mode, which is a buffered input mode.
*/
static void
enable_termraw()
{ {
struct termios raw; char c;
int command = 0;
/* Read the current terminal parameters for standard input. */ while (1) {
tcgetattr(STDIN_FILENO, &raw); c = '\0';
/*
* Note that Cygwin, apparently, will treat a read time-
* out as an error with errno == EAGAIN. I don't really
* have any interest in supporting code for Cygwin.
*/
if ((read(STDIN_FILENO, &c, 1) == -1)) {
die("failed to read from the terminal");
}
/* if (c == 0x00) {
* Turn off the local ECHO mode, which we need to do in raw mode continue;
* because what gets displayed is going to need extra control. }
*
* NOTE(kyle): look into cfmakeraw, which will require
* snapshotting.
*/
raw.c_lflag &= ~(ECHO|ICANON);
/* if (command && ((c == 'q') || (c == 0x11))) {
* Now write the terminal parameters to the current terminal, break;
* after flushing any waiting input out. }
*/
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
if (c == 0x0b) {
if (!command) {
command = 1;
continue;
}
} else {
command = 0;
}
static void if (iscntrl(c)) {
disable_termraw() printf("%02x\r\n", c);
{ } else {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &entry_term); printf("%02x ('%c')\r\n", c, c);
}
}
} }
int int
main() main()
{ {
char c; setup_terminal();
loop();
/* prepare the terminal */
tcgetattr(STDIN_FILENO, &entry_term);
atexit(disable_termraw);
enable_termraw();
while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') {
if (iscntrl(c)) {
printf("%02x\n", c);
} else {
printf("%02x ('%c')\n", c, c);
}
}
return 0; return 0;
} }

70
ke/terminal.c Normal file
View File

@ -0,0 +1,70 @@
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "util.h"
static struct termios entry_term;
/*
* A text editor needs the terminal to be in raw mode; but the default
* is to be in canonical (cooked) mode, which is a buffered input mode.
*/
static void
enable_termraw()
{
struct termios raw;
/* Read the current terminal parameters for standard input. */
if (tcgetattr(STDIN_FILENO, &raw) == -1) {
die("tcgetattr while enabling raw mode");
}
/*
* Put the terminal into raw mode.
*/
cfmakeraw(&raw);
/*
* Set timeout for read(2).
*
* VMIN: what is the minimum number of bytes required for read
* to return?
*
* VTIME: max time before read(2) returns in hundreds of milli-
* seconds.
*/
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
/*
* Now write the terminal parameters to the current terminal,
* after flushing any waiting input out.
*/
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) {
die("tcsetattr while enabling raw mode");
}
}
static void
disable_termraw()
{
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &entry_term) == -1) {
die("couldn't disable terminal raw mode");
}
}
void
setup_terminal()
{
if (tcgetattr(STDIN_FILENO, &entry_term) == -1) {
die("can't snapshot terminal settings");
}
atexit(disable_termraw);
enable_termraw();
}

8
ke/terminal.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef KE_TERMINAL_H
#define KE_TERMINAL_H
void setup_terminal();
#endif /* KE_TERMINAL_H */

10
ke/util.c Normal file
View File

@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
void
die(const char *s)
{
perror(s);
exit(1);
}

8
ke/util.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef KE_UTIL_H
#define KE_UTIL_H
void die(const char *s);
#endif /* KE_UTIL_H */