From 425ef692da7e74112f88f0b368f3286dba84f846 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 18 Jun 2020 19:45:40 -0700 Subject: feat: working parser for rc shell language --- sys/libterm/term.h | 365 +++++++++++------------------------------------------ 1 file changed, 73 insertions(+), 292 deletions(-) (limited to 'sys/libterm/term.h') diff --git a/sys/libterm/term.h b/sys/libterm/term.h index acae95f..6bd2f6b 100644 --- a/sys/libterm/term.h +++ b/sys/libterm/term.h @@ -7,165 +7,11 @@ #include #define iota(x) 1 << (x) -/* - * obtained from: - * https://invisible-island.net/ncurses/man/curs_add_wch.3x.html - */ -#define ACSRUNES \ - /* name utf8 ascii acsc*/ \ - ACSRUNE("block", 0x25ae, '#', '0') \ - ACSRUNE("board", 0x2592, '#', 'h') \ - ACSRUNE("btee", 0x2534, '+', 'v') \ - ACSRUNE("bullet", 0x00b7, 'o', '~') \ - ACSRUNE("ckboard", 0x2592, ':', 'a') \ - ACSRUNE("darrow", 0x2193, 'v', '.') \ - ACSRUNE("degree", 0x00b0, '\'','f') \ - ACSRUNE("diamond", 0x25c6, '+', '`') \ - ACSRUNE("gequal", 0x2265, '>', '>') \ - ACSRUNE("hline", 0x2500, '-', 'q') \ - ACSRUNE("antern", 0x2603, '#', 'i') \ - ACSRUNE("larrow", 0x2190, '<', ',') \ - ACSRUNE("lequal", 0x2264, '<', 'y') \ - ACSRUNE("llcorner", 0x2514, '+', 'm') \ - ACSRUNE("lrcorner", 0x2518, '+', 'j') \ - ACSRUNE("ltee", 0x2524, '+', 't') \ - ACSRUNE("nequal", 0x2260, '!', '|') \ - ACSRUNE("pi", 0x03c0, '*', '{') \ - ACSRUNE("plminus", 0x00b1, '#', 'g') \ - ACSRUNE("plus", 0x253c, '+', 'n') \ - ACSRUNE("rarrow", 0x2192, '>', '+') \ - ACSRUNE("rtee", 0x251c, '+', 'u') \ - ACSRUNE("s1", 0x23ba, '-', 'o') \ - ACSRUNE("s3", 0x23bb, '-', 'p') \ - ACSRUNE("s7", 0x23bc, '-', 'r') \ - ACSRUNE("s9", 0x23bd, '_', 's') \ - ACSRUNE("sterling", 0x00a3, 'f', '}') \ - ACSRUNE("ttee", 0x252c, '+', 'w') \ - ACSRUNE("uarrow", 0x2191, '^', '-') \ - ACSRUNE("ulcorner", 0x250c, '+', 'l') \ - ACSRUNE("urcorner", 0x2510, '+', 'k') \ - ACSRUNE("vline", 0x2502, '|', 'x') \ - /* thick versions */ \ - ACSRUNE("t_btee", 0x253b, '+', 'V') \ - ACSRUNE("t_hline", 0x2501, '-', 'Q') \ - ACSRUNE("t_llcorner", 0x2517, '+', 'M') \ - ACSRUNE("t_lrcorner", 0x251b, '+', 'J') \ - ACSRUNE("t_ltee", 0x252b, '+', 'T') \ - ACSRUNE("t_plus", 0x254b, '+', 'N') \ - ACSRUNE("t_rtee", 0x2523, '+', 'U') \ - ACSRUNE("t_ttee", 0x2533, '+', 'W') \ - ACSRUNE("t_ulcorner", 0x250f, '+', 'L') \ - ACSRUNE("t_urcorner", 0x2513, '+', 'K') \ - ACSRUNE("t_vline", 0x2503, '|', 'X') \ - /* double version */ \ - ACSRUNE("d_btee", 0x2569, '+', 'H') \ - ACSRUNE("d_hline", 0x2550, '-', 'R') \ - ACSRUNE("d_llcorner", 0x255a, '+', 'D') \ - ACSRUNE("d_lrcorner", 0x255d, '+', 'A') \ - ACSRUNE("d_ltee", 0x2560, '+', 'F') \ - ACSRUNE("d_plus", 0x256c, '+', 'E') \ - ACSRUNE("d_rtee", 0x2563, '+', 'G') \ - ACSRUNE("d_ttee", 0x2566, '+', 'I') \ - ACSRUNE("d_ulcorner", 0x2554, '+', 'C') \ - ACSRUNE("d_urcorner", 0x2557, '+', 'B') \ - ACSRUNE("d_vline", 0x2551, '|', 'Y') - -/* enums */ - -/* key symbols */ -enum -{ - SymUnknown = -1, - SymNone = 0, - - /* special names in c0 */ - SymBackspace, SymTab, SymEnter, SymEscape, - - /* special names in g0 */ - SymSpace, SymDel, - - /* special keys */ - SymUp, SymDown, SymLeft, SymRight, SymBegin, SymFind, SymInsert, - SymDelete, SymSelect, SymPageup, SymPagedown, SymHome, SymEnd, - - /* special keys from terminfo */ - SymCancel, SymClear, SymClose, SymCommand, SymCopy, SymExit, - SymHelp, SymMark, SymMessage, SymMove, SymOpen, SymOptions, - SymPrint, SymRedo, SymReference, SymRefresh, SymReplace, - SymRestart, SymResume, SymSave, SymSuspend, SymUndo, - - /* numeric keypad special keys */ - SymKp0, SymKp1, SymKp2, SymKp3, SymKp4, SymKp5, SymKp6, SymKp7, SymKp8, - SymKp9, SymKpenter, SymKpplus, SymKpminus, SymKpmult, SymKpdiv, SymKpcomma, - SymKpperiod, SymKpequals, - - /* et cetera ad nauseum */ - NumSyms -}; - -/* key type */ -enum -{ - KeyUnicode, - KeyFunc, - KeySym, - KeyMouse, - KeyPosition, - KeyModeReport, - KeyDCS, - KeyOSC, - /* add other recognised types here */ - - KeyUnknownCSI = -1 -}; - -/* key events */ -enum KeyEvent -{ - EvNil, - EvKey, - EvEOF, - EvAgain, - EvErr, -}; - -enum MouseEvent -{ - MouseNil, - MousePress, - MouseDrag, - MouseRelease, -}; - -enum -{ - ModShift = iota(0), - ModAlt = iota(1), - ModCtrl = iota(2), -}; - -enum -{ - FlagNoInterpret = iota(0), - FlagConvertKP = iota(1), - FlagRaw = iota(2), - FlagUTF8 = iota(3), - FlagNoTermIOS = iota(4), - FlagSpaceSymbol = iota(5), - FlagCtrlC = iota(6), - FlagEintr = iota(7), -}; - -enum -{ - HarmonizeSpace = iota(0), - HarmonizeDelBS = iota(1), -}; -/* types */ typedef struct RGB8 RGB8; typedef struct Pen Pen; +typedef struct Dot Dot; typedef struct Cell Cell; typedef struct Row Row; typedef struct Buffer Buffer; @@ -194,7 +40,7 @@ enum PenUnderline = iota(6), PenBlink = iota(7), /* ... */ - PenTrueClr = iota(15), + PenRGB = iota(15), }; struct Pen @@ -213,38 +59,29 @@ struct Pen }; /* outputs */ -struct Cell { - rune r; +struct Cell +{ + rune txt; Pen pen; }; -struct Row { +struct Row +{ Cell *cells; - uint dirty:1; + uint dirty : 1; }; -/* Buffer holding the current window content (as an array) as well - * as the scroll back buffer content (as a circular/ring buffer). - * - * If new content is added to terminal the view port slides down and the - * previously top most line is moved into the scroll back buffer at postion - * scroll.index. This index will eventually wrap around and thus overwrite - * the oldest lines. - * - * In the scenerio below a scroll up has been performed. That is 'scroll.above' - * lines still lie above the current view port. Further scrolling up will show - * them. Similarly 'scroll.below' is the amount of lines below the current - * viewport. - * - * The function buffer_boundary sets the row pointers to the start/end range - * of the section delimiting the region before/after the viewport. The functions - * buffer_row_{first,last} return the first/last logical row. And - * buffer_row_{next,prev} allows to iterate over the logical lines in either - * direction. +struct Dot +{ + int row, col; +}; + +/* + * scroll.top & scroll.bot are pointers into the viewport. * * scroll back buffer * - * scroll_buf->+----------------+-----+ + * scroll.buf->+----------------+-----+ * | | | ^ \ * | before | | | | * current terminal content | viewport | | | | @@ -253,39 +90,48 @@ struct Row { * ^ | | i | \ | | i | c | * | | | n | \ | | n | r | * | | v | \ | | v | o | - * r | | i | \ | | i | l / - * o | viewport | s | >|<- scroll.index | s | l \ - * w | | i | / | | i | | - * s | | b | / | after | b | s > scroll.below + * | | i | \ | | i | l / + * | buffer | s | >|<- scroll.index | s | l \ + * h | | i | / | | i | | + * | | b | / | after | b | s > scroll.below * | | l | / | viewport | l | i | * v | | e | / | | e | z / * +----------------+-----+/ | unused | | e - * <- maxcols -> | scroll back | | - * <- cols -> | buffer | | | + * <- maxw -> | scroll back | | + * <- w -> | buffer | | | * | | | | * | | | v - * roll_buf + scroll.size->+----------------+-----+ - * <- maxcols -> - * <- cols -> + * scroll.buf + scroll.size->+----------------+-----+ + * <- maxw -> + * <- w -> */ -struct Buffer { - Row *row; /* array of Row pointers of size 'rows' */ - Row *crow; /* row on which the cursor currently resides */ - bool *tabs; /* a boolean flag for each column whether it is a tab */ +struct Buffer +{ + int w, h; /* dimension of buffer */ + Pen pen; /* default attributes */ + int maxw; /* allocated cells (maximal cols over time) */ + Row *row; /* array of row pointers of size 'h' */ struct { - Row *buf; /* a ring buffer holding the scroll back content */ - Row *top; /* row in lines where scrolling region starts */ - Row *bot; /* row in lines where scrolling region ends */ - int size; /* maximal capacity of scroll back buffer (in lines) */ - int index; /* current index into the ring buffer */ - int above; /* number of lines above current viewport */ - int below; /* number of lines below current viewport */ + Row *buf; + Row *top; + Row *bot; + int size; + int index; + int above; + int below; } scroll; - Pen pen, spen; - int nrow, ncol; /* current dimension of buffer */ - int maxcols; /* allocated cells (maximal cols over time) */ - int srow, scol; /* saved cursor row/column (zero based) */ + Dot cur, save; /* cursor position within buffer */ +}; + +struct Window +{ + struct Buffer; + int top, left; + uchar curvis : 1; + uchar blink : 2; + + Window *parent, *child, *link; }; /* input */ @@ -310,61 +156,57 @@ struct KeyInfo int modset; }; -/* make opaque? */ struct Input { int fd; int flags; - int hflag; int wait; /* in ms */ + /* modifiers */ + uchar closed : 1; + uchar started : 1; + uchar hasold : 1; + struct termios oldterm; /* buffer */ struct { long off; uchar *b, *c, *e, bytes[256]; - } buf; - - /* modifiers */ - char closed : 1; - char started : 1; - char hasold : 1; + } rbuf; + struct { + uchar *s, bytes[256]; + } ebuf; /* key data */ Node *keys; struct KeyInfo c0[32]; - - char **keynm; - int nkeynm; - - int nsavedcsi; - char *savedcsi; }; + struct Term { /* meta data */ char *name; unibi_term *info; struct { - uint altscreen : 1; - uint cursorvis : 1; - uint mouse : 1; + uchar altscreen : 1; + uchar cursorvis : 1; + uchar mouse : 1; } mode; struct { - uint bce : 1; - int colors; + uchar bce : 1; + int colors; } cap; /* input capture */ - Input *input; + Input input; /* output display */ Window *root; Pen pen; - /* output raw text */ + /* raw text to pty */ int fd; struct { char *c, b[512]; @@ -416,74 +258,13 @@ struct Term } ext; } esc; - /* basic shapes */ - struct { - rune block; - rune board; - rune hline; - rune vline; - rune plus; - rune ltee; - rune rtee; - rune ttee; - rune btee; - rune ulcorner; - rune urcorner; - rune llcorner; - rune lrcorner; - } acs; + Term *link; }; -/* - * exported functions - */ +/* functions */ +void tresize(Term *t); -#if 0 -/* buffer.c */ -void zero(Row *row, int start, int len); -void roll(Row *start, Row *end, int count); - -void bclear(Buffer *b); -void bfree(Buffer *b); -void bscroll(Buffer *b, int s); -void bresize(Buffer *b, int rows, int cols); -bool binit(Buffer *b, int rows, int cols, int size); -void brender(Buffer *b, Term *t); -void bboundary(Buffer *b, Row **bs, Row **be, Row **as, Row **ae) ; -Row *browfirst(Buffer *b); -Row *browlast(Buffer *b); -Row *brownext(Buffer *b, Row *row); -Row *bprevrow(Buffer *b, Row *row); - -/* input.c */ -Input *makeinput(int fd, int flags, unibi_term *info); -void freeinput(Input *in); -int startrecord(Input *in); -int stoprecord(Input *in); -char *keyname(Input *in, int sym); - -enum KeyEvent term·waitkey(Input *in, Key *key); /* block until next keypress */ -enum KeyEvent term·getkey(Input *in, Key *key); /* grab key if we can */ -enum KeyEvent term·demandkey(Input *in, Key *key); /* grab now and interpret as best we can */ - -/* unpack key event into useful data */ -enum KeyEvent decodemouse(Input *in, Key *key, enum MouseEvent *ev, int *button, int *row, int *col); -enum KeyEvent decodepos(Input *in, Key *key, int *row, int *col); -enum KeyEvent decodemode(Input *in, Key *key, int *init, int *mode, int *val); -#endif - -/* term.c */ -void term·init(Term *t); -void term·fini(Term *t); -void term·flush(Term *t); -void term·write(Term *t, long len, char *s); -int term·goto(Term *t, int row, int col); -void term·jump(Term *t, int down, int right); -void term·del(Term *t, int num); -void term·setpen(Term *t, Pen pen); -void term·clear(Term *t); - -/* input.c */ -enum KeyEvent term·waitkey(Term *t, Key *key); /* block until next keypress */ -enum KeyEvent term·getkey(Term *t, Key *key); /* grab key if we can */ -enum KeyEvent term·demandkey(Term *t, Key *key); /* grab now and interpret as best we can */ +Window *wmake(Window *root, int top, int left, int w, int h, int scroll); +void wresize(Window *root, int w, int h); +void wputrune(Window *win, rune r); +void wscroll(Window *win, int s); -- cgit v1.2.1