Initial import, starting with kyle's editor.

This commit is contained in:
Kyle Isom 2020-02-07 20:46:43 -08:00
commit 42658b8d98
4 changed files with 128 additions and 0 deletions

1
.hgignore Normal file
View File

@ -0,0 +1 @@
.o$

22
ke/Makefile Normal file
View File

@ -0,0 +1,22 @@
BIN := ke
OBJS := main.o
LDFLAGS := -static
CFLAGS := -pedantic -Wall -Werror -Wextra -O2 -std=c99
.PHONY: all
all: $(BIN) run
$(BIN): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS)
.PHONY: clean
clean:
rm -f $(BIN) $(OBJS)
.PHONY: run
run: $(BIN)
./$(BIN)
%.o: %.c
$(CC) $(CFLAGS) -c $@ $<

37
ke/keys.txt Normal file
View File

@ -0,0 +1,37 @@
An Exploration of the Keys
==========================
ESC: 1b
Function keys: 1b followed by
F1 F2 F3 F4 F5 F6 F7 F8
4f O 4f O 4f O 4f O 5b 5b 5b 5b
50 P 51 Q 52 R 54 S 31 1 31 1 31 1 31 1
35 5 37 7 38 8 39 9
7e ~ 7e ~ 7e ~ 7e ~
F9 F10 F11 F12
5b [ 5b [ 5b [ 5b [
32 2 32 2 32 2 32 2
30 0 31 1 33 3 34 4
7e ~ 7e ~ 7e ~ 7e ~
DEL 7f - but it should be a 4-byte escape sequence?
BKSP 7f
Arrow keys: 1b followed by
UP DN LF RT
5b [ 5b [ 5b [ 5b [
41 A 42 B 44 D 43 C
Other keys: 1b followed by
HOME END PGUP PGDN INS
5b [ 5b [ 5b [ 5b [ 5b [
48 H 46 F 35 5 36 6 32 2
7e ~ 7e ~ 7e ~
Notes: any time you see escape, you must process another key to figure
out what to do.

68
ke/main.c Normal file
View File

@ -0,0 +1,68 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
/* entry_term contains a snapshot of the termios settings at startup. */
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. */
tcgetattr(STDIN_FILENO, &raw);
/*
* Turn off the local ECHO mode, which we need to do in raw mode
* because what gets displayed is going to need extra control.
*
* NOTE(kyle): look into cfmakeraw, which will require
* snapshotting.
*/
raw.c_lflag &= ~(ECHO|ICANON);
/*
* Now write the terminal parameters to the current terminal,
* after flushing any waiting input out.
*/
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
static void
disable_termraw()
{
tcsetattr(STDIN_FILENO, TCSAFLUSH, &entry_term);
}
int
main()
{
char c;
/* 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;
}