123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #undef __STRICT_ANSI__
- #include <stdlib.h>
- # include <unistd.h>
- # include <fcntl.h>
- #include "private.h"
- typedef struct {
- const char terminator;
- tinyrl_vt100_escape_t code;
- } vt100_decode_t;
- static vt100_decode_t cmds[] = {
- {'A', tinyrl_vt100_CURSOR_UP},
- {'B', tinyrl_vt100_CURSOR_DOWN},
- {'C', tinyrl_vt100_CURSOR_RIGHT},
- {'D', tinyrl_vt100_CURSOR_LEFT},
- };
- static void _tinyrl_vt100_setInputNonBlocking(const tinyrl_vt100_t * this)
- {
- #if defined(STDIN_FILENO)
- int flags = (fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
- fcntl(STDIN_FILENO, F_SETFL, flags);
- #endif
- }
- static void _tinyrl_vt100_setInputBlocking(const tinyrl_vt100_t * this)
- {
- #if defined(STDIN_FILENO)
- int flags = (fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
- fcntl(STDIN_FILENO, F_SETFL, flags);
- #endif
- }
- tinyrl_vt100_escape_t tinyrl_vt100_escape_decode(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_escape_t result = tinyrl_vt100_UNKNOWN;
- char sequence[10], *p = sequence;
- int c;
- unsigned i;
-
- _tinyrl_vt100_setInputNonBlocking(this);
-
- while (1) {
- c = getc(this->istream);
-
- if (-1 != c) {
- *p++ = (c & 0xFF);
- if ((c != '[') && (c > 63)) {
-
- result = tinyrl_vt100_CURSOR_UP;
- break;
- }
- } else {
- result = tinyrl_vt100_UNKNOWN;
- break;
- }
- }
-
- *p = '\0';
-
- _tinyrl_vt100_setInputBlocking(this);
- if (tinyrl_vt100_UNKNOWN != result) {
-
- for (i = 0; i < sizeof(cmds) / sizeof(vt100_decode_t); i++) {
- if (cmds[i].terminator == c) {
-
- result = cmds[i].code;
- break;
- }
- }
- }
- return result;
- }
- int tinyrl_vt100_printf(const tinyrl_vt100_t * this, const char *fmt, ...)
- {
- va_list args;
- int len;
- va_start(args, fmt);
- len = tinyrl_vt100_vprintf(this, fmt, args);
- va_end(args);
- return len;
- }
- int
- tinyrl_vt100_vprintf(const tinyrl_vt100_t * this, const char *fmt, va_list args)
- {
- return vfprintf(this->ostream, fmt, args);
- }
- int tinyrl_vt100_getchar(const tinyrl_vt100_t * this)
- {
- return getc(this->istream);
- }
- int tinyrl_vt100_oflush(const tinyrl_vt100_t * this)
- {
- return fflush(this->ostream);
- }
- int tinyrl_vt100_ierror(const tinyrl_vt100_t * this)
- {
- return ferror(this->istream);
- }
- int tinyrl_vt100_oerror(const tinyrl_vt100_t * this)
- {
- return ferror(this->ostream);
- }
- int tinyrl_vt100_ieof(const tinyrl_vt100_t * this)
- {
- return feof(this->istream);
- }
- int tinyrl_vt100_eof(const tinyrl_vt100_t * this)
- {
- return feof(this->istream);
- }
- unsigned tinyrl_vt100__get_width(const tinyrl_vt100_t * this)
- {
- this = this;
-
- return 80;
- }
- static void
- tinyrl_vt100_init(tinyrl_vt100_t * this, FILE * istream, FILE * ostream)
- {
- this->istream = istream;
- this->ostream = ostream;
- }
- static void tinyrl_vt100_fini(tinyrl_vt100_t * this)
- {
-
- this = this;
- }
- tinyrl_vt100_t *tinyrl_vt100_new(FILE * istream, FILE * ostream)
- {
- tinyrl_vt100_t *this = NULL;
- this = malloc(sizeof(tinyrl_vt100_t));
- if (NULL != this) {
- tinyrl_vt100_init(this, istream, ostream);
- }
- return this;
- }
- void tinyrl_vt100_delete(tinyrl_vt100_t * this)
- {
- tinyrl_vt100_fini(this);
-
- free(this);
- }
- void tinyrl_vt100_ding(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c", KEY_BEL);
- (void)tinyrl_vt100_oflush(this);
- }
- void tinyrl_vt100_attribute_reset(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[0m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_bright(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[1m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_dim(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[2m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_underscore(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[4m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_blink(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[5m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_reverse(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[7m", KEY_ESC);
- }
- void tinyrl_vt100_attribute_hidden(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[8m", KEY_ESC);
- }
- void tinyrl_vt100_erase_line(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[2K", KEY_ESC);
- }
- void tinyrl_vt100_clear_screen(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[2J", KEY_ESC);
- }
- void tinyrl_vt100_cursor_save(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c7", KEY_ESC);
- }
- void tinyrl_vt100_cursor_restore(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c8", KEY_ESC);
- }
- void tinyrl_vt100_cursor_forward(const tinyrl_vt100_t * this, unsigned count)
- {
- tinyrl_vt100_printf(this, "%c[%dC", KEY_ESC, count);
- }
- void tinyrl_vt100_cursor_back(const tinyrl_vt100_t * this, unsigned count)
- {
- tinyrl_vt100_printf(this, "%c[%dD", KEY_ESC, count);
- }
- void tinyrl_vt100_cursor_up(const tinyrl_vt100_t * this, unsigned count)
- {
- tinyrl_vt100_printf(this, "%c[%dA", KEY_ESC, count);
- }
- void tinyrl_vt100_cursor_down(const tinyrl_vt100_t * this, unsigned count)
- {
- tinyrl_vt100_printf(this, "%c[%dB", KEY_ESC, count);
- }
- void tinyrl_vt100_cursor_home(const tinyrl_vt100_t * this)
- {
- tinyrl_vt100_printf(this, "%c[H", KEY_ESC);
- }
- void tinyrl_vt100_erase(const tinyrl_vt100_t * this, unsigned count)
- {
- tinyrl_vt100_printf(this, "%c[%dP", KEY_ESC, count);
- }
- void tinyrl_vt100__set_istream(tinyrl_vt100_t * this, FILE * istream)
- {
- this->istream = istream;
- }
- FILE *tinyrl_vt100__get_istream(const tinyrl_vt100_t * this)
- {
- return this->istream;
- }
- FILE *tinyrl_vt100__get_ostream(const tinyrl_vt100_t * this)
- {
- return this->ostream;
- }
|