1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306 |
- #undef __STRICT_ANSI__
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <errno.h>
- #include <unistd.h>
- #include "lub/string.h"
- #include "private.h"
- static void utf8_point_left(tinyrl_t * this)
- {
- if (!this->utf8)
- return;
- while (this->point &&
- (UTF8_10 == (this->line[this->point] & UTF8_MASK)))
- this->point--;
- }
- static void utf8_point_right(tinyrl_t * this)
- {
- if (!this->utf8)
- return;
- while ((this->point < this->end) &&
- (UTF8_10 == (this->line[this->point] & UTF8_MASK)))
- this->point++;
- }
- static unsigned utf8_nsyms(tinyrl_t * this, const char *str, unsigned num)
- {
- unsigned nsym = 0;
- unsigned i;
- if (!this->utf8)
- return num;
- for (i = 0; i < num; i++) {
- if ('\0' == str[i])
- break;
- if (UTF8_10 == (str[i] & UTF8_MASK))
- continue;
- nsym++;
- }
- return nsym;
- }
- static void tty_set_raw_mode(tinyrl_t * this)
- {
- struct termios new_termios;
- int fd = fileno(tinyrl_vt100__get_istream(this->term));
- int status;
- status = tcgetattr(fd, &this->default_termios);
- if (-1 != status) {
- status = tcgetattr(fd, &new_termios);
- assert(-1 != status);
- new_termios.c_iflag = 0;
- new_termios.c_oflag = OPOST | ONLCR;
- new_termios.c_lflag = 0;
- new_termios.c_cc[VMIN] = 1;
- new_termios.c_cc[VTIME] = 0;
-
- status = tcsetattr(fd, TCSAFLUSH, &new_termios);
- assert(-1 != status);
- }
- }
- static void tty_restore_mode(const tinyrl_t * this)
- {
- int fd = fileno(tinyrl_vt100__get_istream(this->term));
-
- (void)tcsetattr(fd, TCSAFLUSH, &this->default_termios);
- }
- static void changed_line(tinyrl_t * this)
- {
-
- if (this->line != this->buffer) {
-
- free(this->buffer);
- this->line = this->buffer = lub_string_dup(this->line);
- this->buffer_size = strlen(this->buffer);
- assert(this->line);
- }
- }
- static bool_t tinyrl_key_default(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (key > 31) {
- char tmp[2];
- tmp[0] = (key & 0xFF), tmp[1] = '\0';
-
- result = tinyrl_insert_text(this, tmp);
- } else {
- char tmp[10];
- sprintf(tmp, "~%d", key);
-
- result = tinyrl_insert_text(this, tmp);
- }
- return result;
- }
- static bool_t tinyrl_key_interrupt(tinyrl_t * this, int key)
- {
- tinyrl_delete_text(this, 0, this->end);
- this->done = BOOL_TRUE;
-
- key = key;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_start_of_line(tinyrl_t * this, int key)
- {
-
- this->point = 0;
-
- key = key;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_end_of_line(tinyrl_t * this, int key)
- {
-
- this->point = this->end;
-
- key = key;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_kill(tinyrl_t * this, int key)
- {
-
- lub_string_free(this->kill_string);
-
- this->kill_string = lub_string_dup(&this->buffer[this->point]);
-
- tinyrl_delete_text(this, this->point, this->end);
-
- key = key;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_yank(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->kill_string) {
-
- result = tinyrl_insert_text(this, this->kill_string);
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_crlf(tinyrl_t * this, int key)
- {
- tinyrl_crlf(this);
- this->done = BOOL_TRUE;
-
- key = key;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_up(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- tinyrl_history_entry_t *entry = NULL;
- if (this->line == this->buffer) {
-
- entry = tinyrl_history_getlast(this->history, &this->hist_iter);
- } else {
-
- entry = tinyrl_history_getprevious(&this->hist_iter);
- }
- if (entry) {
-
- this->line = tinyrl_history_entry__get_line(entry);
- this->point = this->end = strlen(this->line);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_down(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->line != this->buffer) {
-
-
- tinyrl_history_entry_t *entry =
- tinyrl_history_getnext(&this->hist_iter);
- if (!entry) {
-
- this->line = this->buffer;
- } else {
- this->line = tinyrl_history_entry__get_line(entry);
- }
-
- this->point = this->end = strlen(this->line);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_left(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->point > 0) {
- this->point--;
- utf8_point_left(this);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_right(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->point < this->end) {
- this->point++;
- utf8_point_right(this);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_backspace(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->point) {
- unsigned end = --this->point;
- utf8_point_left(this);
- tinyrl_delete_text(this, this->point, end);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_delete(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- if (this->point < this->end) {
- unsigned end = this->point;
- utf8_point_left(this);
- tinyrl_delete_text(this, this->point, end);
- result = BOOL_TRUE;
- }
-
- key = key;
- return result;
- }
- static bool_t tinyrl_key_clear_screen(tinyrl_t * this, int key)
- {
- tinyrl_vt100_clear_screen(this->term);
- tinyrl_vt100_cursor_home(this->term);
- tinyrl_reset_line_state(this);
-
- key = key;
- this = this;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_erase_line(tinyrl_t * this, int key)
- {
- tinyrl_delete_text(this, 0, this->point);
- this->point = 0;
-
- key = key;
- this = this;
- return BOOL_TRUE;
- }
- static bool_t tinyrl_key_escape(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- switch (tinyrl_vt100_escape_decode(this->term)) {
- case tinyrl_vt100_CURSOR_UP:
- result = tinyrl_key_up(this, key);
- break;
- case tinyrl_vt100_CURSOR_DOWN:
- result = tinyrl_key_down(this, key);
- break;
- case tinyrl_vt100_CURSOR_LEFT:
- result = tinyrl_key_left(this, key);
- break;
- case tinyrl_vt100_CURSOR_RIGHT:
- result = tinyrl_key_right(this, key);
- break;
- case tinyrl_vt100_HOME:
- result = tinyrl_key_start_of_line(this,key);
- break;
- case tinyrl_vt100_END:
- result = tinyrl_key_end_of_line(this,key);
- break;
- case tinyrl_vt100_DELETE:
- result = tinyrl_key_delete(this,key);
- break;
- case tinyrl_vt100_INSERT:
- case tinyrl_vt100_PGDOWN:
- case tinyrl_vt100_PGUP:
- case tinyrl_vt100_UNKNOWN:
- break;
- }
- return result;
- }
- static bool_t tinyrl_key_tab(tinyrl_t * this, int key)
- {
- bool_t result = BOOL_FALSE;
- tinyrl_match_e status = tinyrl_complete_with_extensions(this);
- switch (status) {
- case TINYRL_COMPLETED_MATCH:
- case TINYRL_MATCH:
-
- result = tinyrl_insert_text(this, " ");
- break;
- case TINYRL_NO_MATCH:
- case TINYRL_MATCH_WITH_EXTENSIONS:
- case TINYRL_AMBIGUOUS:
- case TINYRL_COMPLETED_AMBIGUOUS:
-
- break;
- }
-
- key = key;
- return result;
- }
- static void tinyrl_fini(tinyrl_t * this)
- {
-
- tinyrl_history_delete(this->history);
-
- tinyrl_vt100_delete(this->term);
-
- lub_string_free(this->buffer);
- lub_string_free(this->kill_string);
- lub_string_free(this->last_buffer);
- lub_string_free(this->prompt);
- }
- static void
- tinyrl_init(tinyrl_t * this,
- FILE * instream, FILE * outstream,
- unsigned stifle, tinyrl_completion_func_t * complete_fn)
- {
- int i;
- for (i = 0; i < NUM_HANDLERS; i++) {
- this->handlers[i] = tinyrl_key_default;
- }
-
- this->handlers[KEY_CR] = tinyrl_key_crlf;
- this->handlers[KEY_LF] = tinyrl_key_crlf;
- this->handlers[KEY_ETX] = tinyrl_key_interrupt;
- this->handlers[KEY_DEL] = tinyrl_key_backspace;
- this->handlers[KEY_BS] = tinyrl_key_backspace;
- this->handlers[KEY_EOT] = tinyrl_key_delete;
- this->handlers[KEY_ESC] = tinyrl_key_escape;
- this->handlers[KEY_FF] = tinyrl_key_clear_screen;
- this->handlers[KEY_NAK] = tinyrl_key_erase_line;
- this->handlers[KEY_SOH] = tinyrl_key_start_of_line;
- this->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
- this->handlers[KEY_VT] = tinyrl_key_kill;
- this->handlers[KEY_EM] = tinyrl_key_yank;
- this->handlers[KEY_HT] = tinyrl_key_tab;
- this->line = NULL;
- this->max_line_length = 0;
- this->prompt = NULL;
- this->prompt_size = 0;
- this->buffer = NULL;
- this->buffer_size = 0;
- this->done = BOOL_FALSE;
- this->completion_over = BOOL_FALSE;
- this->point = 0;
- this->end = 0;
- this->attempted_completion_function = complete_fn;
- this->state = 0;
- this->kill_string = NULL;
- this->echo_char = '\0';
- this->echo_enabled = BOOL_TRUE;
- if (instream)
- this->isatty = isatty(fileno(instream)) ?
- BOOL_TRUE : BOOL_FALSE;
- else
- this->isatty = BOOL_FALSE;
- this->last_buffer = NULL;
- this->last_point = 0;
- this->utf8 = BOOL_FALSE;
-
- this->term = tinyrl_vt100_new(instream, outstream);
-
- this->history = tinyrl_history_new(stifle);
- }
- int tinyrl_printf(const tinyrl_t * this, const char *fmt, ...)
- {
- va_list args;
- int len;
- va_start(args, fmt);
- len = tinyrl_vt100_vprintf(this->term, fmt, args);
- va_end(args);
- return len;
- }
- void tinyrl_delete(tinyrl_t * this)
- {
- assert(this);
- if (this) {
-
- tinyrl_fini(this);
-
- free(this);
- }
- }
- int tinyrl_getchar(const tinyrl_t * this)
- {
- return tinyrl_vt100_getchar(this->term);
- }
- static void tinyrl_internal_print(const tinyrl_t * this, const char *text)
- {
- if (this->echo_enabled) {
-
- tinyrl_vt100_printf(this->term, "%s", text);
- } else {
-
- if (this->echo_char) {
- unsigned i = strlen(text);
- while (i--) {
- tinyrl_vt100_printf(this->term, "%c",
- this->echo_char);
- }
- }
- }
- }
- static void tinyrl_internal_position(tinyrl_t *this, size_t prompt_len,
- size_t line_len, unsigned int width)
- {
- int rows, cols;
- rows = ((line_len + prompt_len) / width) - (prompt_len / width);
- cols = ((line_len + prompt_len) % width) - (prompt_len % width);
- if (cols > 0)
- tinyrl_vt100_cursor_back(this->term, cols);
- else if (cols < 0)
- tinyrl_vt100_cursor_forward(this->term, -cols);
- if (rows > 0)
- tinyrl_vt100_cursor_up(this->term, rows);
- }
- void tinyrl_redisplay(tinyrl_t * this)
- {
- unsigned int count;
- unsigned int line_size = strlen(this->line);
- unsigned int line_len = utf8_nsyms(this, this->line, line_size);
- unsigned int width = tinyrl_vt100__get_width(this->term);
- int cols;
-
- if (this->last_buffer) {
- count = utf8_nsyms(this, this->last_buffer, this->last_point);
- tinyrl_internal_position(this, this->prompt_len, count, width);
- } else
- tinyrl_vt100_printf(this->term, "%s", this->prompt);
-
- tinyrl_internal_print(this, this->line);
- cols = (this->prompt_len + line_len) % width;
- if (!cols && line_size)
- tinyrl_vt100_next_line(this->term);
- tinyrl_vt100_erase_down(this->term);
-
- if (this->point < line_size) {
- unsigned int pre_len = utf8_nsyms(this,
- this->line, this->point);
- count = utf8_nsyms(this, this->line + this->point,
- line_size - this->point);
- tinyrl_internal_position(this, this->prompt_len + pre_len,
- count, width);
- }
-
- (void)tinyrl_vt100_oflush(this->term);
-
- lub_string_free(this->last_buffer);
- this->last_buffer = lub_string_dup(this->line);
- this->last_point = this->point;
- }
- tinyrl_t *tinyrl_new(FILE * instream, FILE * outstream,
- unsigned stifle, tinyrl_completion_func_t * complete_fn)
- {
- tinyrl_t *this = NULL;
- this = malloc(sizeof(tinyrl_t));
- if (this)
- tinyrl_init(this, instream, outstream, stifle, complete_fn);
- return this;
- }
- static char *internal_insertline(tinyrl_t * this, char *buffer)
- {
- char *p;
- char *s = buffer;
-
- p = strchr(buffer, '\r');
- if (!p)
- p = strchr(buffer, '\n');
- if (p)
- *p = '\0';
-
- if (0 == this->point) {
- while (*s && isspace(*s))
- s++;
- }
- if (*s) {
-
- (void)tinyrl_insert_text(this, s);
-
- tinyrl_redisplay(this);
- }
- return s;
- }
- static char *internal_readline(tinyrl_t * this,
- const char *prompt, void *context, const char *str)
- {
- FILE *istream = tinyrl_vt100__get_istream(this->term);
- int crlf = 1;
- char *result = NULL;
- int lerrno = 0;
-
- this->done = BOOL_FALSE;
- this->point = 0;
- this->end = 0;
- this->buffer = lub_string_dup("");
- this->buffer_size = strlen(this->buffer);
- this->line = this->buffer;
- tinyrl__set_prompt(this, prompt);
- this->context = context;
- if (this->isatty && !str) {
-
- tty_set_raw_mode(this);
- tinyrl_reset_line_state(this);
- while (!this->done) {
- int key;
-
- tinyrl_redisplay(this);
-
- key = tinyrl_getchar(this);
-
- if (EOF != key) {
-
- if (!this->handlers[key](this, key))
- tinyrl_ding(this);
- if (this->done) {
-
- if (this->end &&
- isspace(this->line[this->end - 1]))
- tinyrl_delete_text(this,
- this->end - 1,
- this->end);
- }
- } else {
-
- this->done = BOOL_TRUE;
- this->line = NULL;
- lerrno = ENOENT;
- }
- }
-
- tty_restore_mode(this);
- } else {
-
- char *s = NULL, buffer[80];
- size_t len = sizeof(buffer);
- char *tmp = NULL;
-
- lub_string_free(this->last_buffer);
- this->last_buffer = NULL;
- if (str) {
- tmp = lub_string_dup(str);
- s = internal_insertline(this, tmp);
- } else {
- while ((sizeof(buffer) == len) &&
- (s = fgets(buffer, sizeof(buffer), istream))) {
- s = internal_insertline(this, buffer);
- len = strlen(buffer) + 1;
- }
- if (!s || (this->line[0] == '\0' && feof(istream))) {
-
- crlf = 0;
- this->line = NULL;
- lerrno = ENOENT;
- }
- }
-
- if (this->line) {
- if (this->line[0] == '\0') {
- tinyrl_reset_line_state(this);
- } else if (!this->handlers[KEY_LF](this, KEY_LF)) {
-
- tinyrl_ding(this);
- this->line = NULL;
- lerrno = EBADMSG;
- }
- }
- if (str)
- lub_string_free(tmp);
- }
-
- result = this->line ? lub_string_dup(this->line) : NULL;
-
- free(this->buffer);
- this->buffer = NULL;
-
- if (crlf && (!result || ('\0' == *result)))
- tinyrl_crlf(this);
- if (!result)
- errno = lerrno;
- return result;
- }
- char *tinyrl_readline(tinyrl_t * this, const char *prompt, void *context)
- {
- return internal_readline(this, prompt, context, NULL);
- }
- char *tinyrl_forceline(tinyrl_t * this,
- const char *prompt, void *context, const char *line)
- {
- return internal_readline(this, prompt, context, line);
- }
- bool_t tinyrl_extend_line_buffer(tinyrl_t * this, unsigned len)
- {
- bool_t result = BOOL_TRUE;
- char *new_buffer;
- size_t new_len = len;
- if (this->buffer_size >= len)
- return result;
-
- if (this->max_line_length == 0) {
-
- if (new_len < this->buffer_size + 10)
- new_len = this->buffer_size + 10;
-
- new_buffer = realloc(this->buffer, new_len + 1);
- if (!new_buffer) {
- tinyrl_ding(this);
- result = BOOL_FALSE;
- } else {
- this->buffer_size = new_len;
- this->line = this->buffer = new_buffer;
- }
- } else {
- if (new_len < this->max_line_length) {
-
- new_buffer = realloc(this->buffer,
- this->max_line_length);
- if (!new_buffer) {
- tinyrl_ding(this);
- result = BOOL_FALSE;
- } else {
- this->buffer_size =
- this->max_line_length - 1;
- this->line = this->buffer = new_buffer;
- }
- } else {
- tinyrl_ding(this);
- result = BOOL_FALSE;
- }
- }
- return result;
- }
- bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
- {
- unsigned delta = strlen(text);
-
- changed_line(this);
- if ((delta + this->end) > (this->buffer_size)) {
-
- if (BOOL_FALSE ==
- tinyrl_extend_line_buffer(this, this->end + delta))
- return BOOL_FALSE;
- }
- if (this->point < this->end) {
-
- memmove(&this->buffer[this->point + delta],
- &this->buffer[this->point],
- (this->end - this->point) + 1);
- } else {
-
- this->buffer[this->end + delta] = '\0';
- }
-
- strncpy(&this->buffer[this->point], text, delta);
-
- this->point += delta;
- this->end += delta;
- return BOOL_TRUE;
- }
- void
- tinyrl_display_matches(const tinyrl_t * this,
- char *const *matches, unsigned len, size_t max)
- {
- unsigned r, c;
- unsigned width = tinyrl_vt100__get_width(this->term);
- unsigned cols = width / (max + 1);
- unsigned rows = len / cols + 1;
- assert(matches);
- if (matches) {
- len--, matches++;
-
- for (r = 0; r < rows && len; r++) {
- for (c = 0; c < cols && len; c++) {
- const char *match = *matches++;
- len--;
- tinyrl_vt100_printf(this->term, "%-*s ", max,
- match);
- }
- tinyrl_crlf(this);
- }
- }
- }
- void tinyrl_delete_text(tinyrl_t * this, unsigned start, unsigned end)
- {
- unsigned delta;
-
- changed_line(this);
-
- if (start > end) {
- unsigned tmp = end;
- start = end;
- end = tmp;
- }
- if (end > this->end)
- end = this->end;
- delta = (end - start) + 1;
-
- memmove(&this->buffer[start],
- &this->buffer[start + delta], this->end - end);
-
- if (this->point >= start) {
- if (this->point > end) {
-
- this->point -= delta;
- } else {
-
- this->point = start;
- }
- }
- if (this->end > end)
- this->end -= delta;
- else
- this->end = start;
-
- this->buffer[this->end] = '\0';
- }
- bool_t tinyrl_bind_key(tinyrl_t * this, int key, tinyrl_key_func_t * fn)
- {
- bool_t result = BOOL_FALSE;
- if ((key >= 0) && (key < 256)) {
-
- this->handlers[key] = fn;
- result = BOOL_TRUE;
- }
- return result;
- }
- char **tinyrl_completion(tinyrl_t * this,
- const char *line, unsigned start, unsigned end,
- tinyrl_compentry_func_t * entry_func)
- {
- unsigned state = 0;
- size_t size = 1;
- unsigned offset = 1;
- char **matches = NULL;
- char *match;
-
- char *text = lub_string_dupn(line, end);
-
- while ((match = entry_func(this, text, start, state++))) {
- if (size == offset) {
-
- size += 10;
- matches =
- realloc(matches, (sizeof(char *) * (size + 1)));
- }
-
- if (!matches)
- break;
- matches[offset] = match;
-
- if (1 == offset) {
-
- matches[0] = lub_string_dup(match);
- } else {
- char *p = matches[0];
- size_t match_len = strlen(p);
-
- while ((tolower(*p) == tolower(*match)) && match_len--) {
- p++, match++;
- }
-
- *p = '\0';
- }
- offset++;
- }
-
- lub_string_free(text);
- if (matches)
- matches[offset] = NULL;
- return matches;
- }
- void tinyrl_delete_matches(char **this)
- {
- char **matches = this;
- while (*matches) {
-
- free(*matches++);
- }
-
- free(this);
- }
- void tinyrl_crlf(const tinyrl_t * this)
- {
- tinyrl_vt100_printf(this->term, "\n");
- }
- void tinyrl_ding(const tinyrl_t * this)
- {
- tinyrl_vt100_ding(this->term);
- }
- void tinyrl_reset_line_state(tinyrl_t * this)
- {
-
- lub_string_free(this->last_buffer);
- this->last_buffer = NULL;
- tinyrl_redisplay(this);
- }
- void tinyrl_replace_line(tinyrl_t * this, const char *text, int clear_undo)
- {
- size_t new_len = strlen(text);
-
- clear_undo = clear_undo;
-
- if (tinyrl_extend_line_buffer(this, new_len)) {
-
- strcpy(this->buffer, text);
-
- this->point = this->end = new_len;
- }
- tinyrl_redisplay(this);
- }
- static tinyrl_match_e
- tinyrl_do_complete(tinyrl_t * this, bool_t with_extensions)
- {
- tinyrl_match_e result = TINYRL_NO_MATCH;
- char **matches = NULL;
- unsigned start, end;
- bool_t completion = BOOL_FALSE;
- bool_t prefix = BOOL_FALSE;
- int i = 0;
-
- start = end = this->point;
- while (start && !isspace(this->line[start - 1]))
- start--;
- if (this->attempted_completion_function) {
- this->completion_over = BOOL_FALSE;
- this->completion_error_over = BOOL_FALSE;
-
- matches = this->attempted_completion_function(this,
- this->line, start, end);
- }
- if (!matches && (BOOL_FALSE == this->completion_over)) {
-
- }
- if (!matches)
- return result;
-
- if (0 != strncmp(matches[0], &this->line[start],
- strlen(matches[0]))) {
-
- if (this->end != end)
- end--;
- tinyrl_delete_text(this, start, end);
- if (BOOL_FALSE == tinyrl_insert_text(this, matches[0]))
- return TINYRL_NO_MATCH;
- completion = BOOL_TRUE;
- }
- for (i = 1; matches[i]; i++) {
-
- if (0 == lub_string_nocasecmp(matches[0], matches[i]))
- prefix = BOOL_TRUE;
- }
-
- if (matches[2]) {
- char **tmp = matches;
- unsigned max, len;
- max = len = 0;
- while (*tmp) {
- size_t size = strlen(*tmp++);
- len++;
- if (size > max)
- max = size;
- }
- if (completion)
- result = TINYRL_COMPLETED_AMBIGUOUS;
- else if (prefix)
- result = TINYRL_MATCH_WITH_EXTENSIONS;
- else
- result = TINYRL_AMBIGUOUS;
- if (with_extensions || !prefix) {
-
- tinyrl_crlf(this);
- tinyrl_display_matches(this, matches, len, max);
- tinyrl_reset_line_state(this);
- }
- } else {
- result = completion ?
- TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
- }
-
- tinyrl_delete_matches(matches);
-
- tinyrl_redisplay(this);
- return result;
- }
- tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * this)
- {
- return tinyrl_do_complete(this, BOOL_TRUE);
- }
- tinyrl_match_e tinyrl_complete(tinyrl_t * this)
- {
- return tinyrl_do_complete(this, BOOL_FALSE);
- }
- void *tinyrl__get_context(const tinyrl_t * this)
- {
- return this->context;
- }
- const char *tinyrl__get_line(const tinyrl_t * this)
- {
- return this->line;
- }
- tinyrl_history_t *tinyrl__get_history(const tinyrl_t * this)
- {
- return this->history;
- }
- void tinyrl_completion_over(tinyrl_t * this)
- {
- this->completion_over = BOOL_TRUE;
- }
- void tinyrl_completion_error_over(tinyrl_t * this)
- {
- this->completion_error_over = BOOL_TRUE;
- }
- bool_t tinyrl_is_completion_error_over(const tinyrl_t * this)
- {
- return this->completion_error_over;
- }
- void tinyrl_done(tinyrl_t * this)
- {
- this->done = BOOL_TRUE;
- }
- void tinyrl_enable_echo(tinyrl_t * this)
- {
- this->echo_enabled = BOOL_TRUE;
- }
- void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
- {
- this->echo_enabled = BOOL_FALSE;
- this->echo_char = echo_char;
- }
- void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
- {
- tinyrl_vt100__set_istream(this->term, istream);
- this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
- }
- bool_t tinyrl__get_isatty(const tinyrl_t * this)
- {
- return this->isatty;
- }
- FILE *tinyrl__get_istream(const tinyrl_t * this)
- {
- return tinyrl_vt100__get_istream(this->term);
- }
- FILE *tinyrl__get_ostream(const tinyrl_t * this)
- {
- return tinyrl_vt100__get_ostream(this->term);
- }
- const char *tinyrl__get_prompt(const tinyrl_t * this)
- {
- return this->prompt;
- }
- void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
- {
- if (this->prompt) {
- lub_string_free(this->prompt);
- this->prompt_size = 0;
- this->prompt_len = 0;
- }
- this->prompt = lub_string_dup(prompt);
- if (this->prompt) {
- this->prompt_size = strlen(this->prompt);
- this->prompt_len = utf8_nsyms(this, this->prompt,
- this->prompt_size);
- }
- }
- bool_t tinyrl__get_utf8(const tinyrl_t * this)
- {
- return this->utf8;
- }
- void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
- {
- this->utf8 = utf8;
- }
- bool_t tinyrl_is_quoting(const tinyrl_t * this)
- {
- bool_t result = BOOL_FALSE;
-
- unsigned i = 0;
- while (i < this->point) {
- if (this->line[i++] == '"') {
- result = result ? BOOL_FALSE : BOOL_TRUE;
- }
- }
- return result;
- }
- bool_t tinyrl_is_empty(const tinyrl_t *this)
- {
- return (this->point == 0) ? BOOL_TRUE : BOOL_FALSE;
- }
- void tinyrl_limit_line_length(tinyrl_t * this, unsigned length)
- {
- this->max_line_length = length;
- }
|