From edbb612c40e3541a170aeaaf41ad8703c8b70cf9 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Fri, 4 Sep 2015 22:09:43 -0700 Subject: linenoise: get rid of globals; everything in struct. * linenoise/linenoise.c (completion_callback, orig_termios, rawmode, mlmode, history_max_len, history_len, history): Global variables removed; moved into struct lino_state. (struct lino_state): New members next, prev. New members completion_callback, orig_termios, rawmode, mlmode, history_max_len, history_len, history. (lino_list): New static variable: dummy node for circular list of struct lino_state structures. (lino_set_multiline): Operate on structure rather than global variable. (enable_raw_mode, disable_raw_mode): Operate on structure. Use file descriptors from structures rather than inconsistent hard-coded use of STDIN_FILENO and the argument fd, which is gone. (lino_clear_screen): Obtain file descriptor from structure, rather than global. (complete_line): Operate on structure rather than globals. Pass context to completion callback. (lino_set_completion_cb): Store callback in structure rather than global. Store new context argument also. (reresh_line): Take mode from structure rather than global. (edit_insert, edit_move_end): Operate on struture rather than globals. (edit): Do not accept file descriptor arguments. Do not update ifd and ofd members of structure; just rely on these values to already be there, since the lino_make constructor puts them there. (lino_print_keycodes, go_raw): Operate on structure. (lino_make, lino_free): New functions. (lino_cleanup): New static function. (linenoise, free_hist): Operate on structure. (atexit_handler): Loop over all structures in global list, and clean up each one. (lino_hist_add, lino_list_set_max_len, lino_hist_save, lino_hist_load): Operate on structure. * linenoise/linenoise.h (lino_t): New typedef. (lino_compl_cb_t): Function type takes new context argument. (lino_set_completion_cb, linenoise, lino_hist_add, lino_hist_set_max, lino_his_save, lino_hist_load, lino_clear_screen, lino_set_multiline, lino_print_keycodes): Declarations updated. * linenoise/example.c (completion): Take new context argument, and ignore it. (main): Create linenoise context with lino_make, giving it the input and output file descriptor, and pass it to all functions. --- linenoise/example.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'linenoise/example.c') diff --git a/linenoise/example.c b/linenoise/example.c index a5046fa3..f884c962 100644 --- a/linenoise/example.c +++ b/linenoise/example.c @@ -15,7 +15,8 @@ mem_t *chk_realloc(mem_t *old, size_t size) return realloc(old, size); } -void completion(const char *buf, lino_completions_t *lc) { +void completion(const char *buf, lino_completions_t *lc, void *ctx) { + (void) ctx; if (buf[0] == 'h') { lino_add_completion(lc, "hello"); lino_add_completion(lc, "hello there"); @@ -25,16 +26,17 @@ void completion(const char *buf, lino_completions_t *lc) { int main(int argc, char **argv) { char *line; char *prgname = argv[0]; + lino_t *ls = lino_make(0, 1); /* Parse options, with --multiline we enable multi line editing. */ while(argc > 1) { argc--; argv++; if (!strcmp(*argv,"--multiline")) { - lino_set_multiline(1); + lino_set_multiline(ls, 1); printf("Multi-line mode enabled.\n"); } else if (!strcmp(*argv,"--keycodes")) { - lino_print_keycodes(); + lino_print_keycodes(ls); exit(0); } else { fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname); @@ -44,11 +46,11 @@ int main(int argc, char **argv) { /* Set the completion callback. This will be called every time the * user uses the key. */ - lino_set_completion_cb(completion); + lino_set_completion_cb(ls, completion, 0); /* Load history from file. The history file is just a plain text file * where entries are separated by newlines. */ - lino_hist_load("history.txt"); /* Load the history at startup */ + lino_hist_load(ls, "history.txt"); /* Load the history at startup */ /* Now this is the main loop of the typical linenoise-based application. * The call to linenoise() will block as long as the user types something @@ -56,16 +58,16 @@ int main(int argc, char **argv) { * * The typed string is returned as a malloc() allocated string by * linenoise, so the user needs to free() it. */ - while((line = linenoise("hello> ")) != NULL) { + while((line = linenoise(ls, "hello> ")) != NULL) { /* Do something with the string. */ if ((1 || line[0] != '\0') && line[0] != '/') { printf("echo: '%s'\n", line); - lino_hist_add(line); /* Add to the history. */ - lino_hist_save("history.txt"); /* Save the history on disk. */ + lino_hist_add(ls, line); /* Add to the history. */ + lino_hist_save(ls, "history.txt"); /* Save the history on disk. */ } else if (!strncmp(line,"/historylen",11)) { /* The "/historylen" command will change the history len. */ int len = atoi(line+11); - lino_hist_set_max_len(len); + lino_hist_set_max_len(ls, len); } else if (line[0] == '/') { printf("Unreconized command: %s\n", line); } -- cgit v1.2.3