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/linenoise.h | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'linenoise/linenoise.h') diff --git a/linenoise/linenoise.h b/linenoise/linenoise.h index 5eb92c4f..acf9ffe2 100644 --- a/linenoise/linenoise.h +++ b/linenoise/linenoise.h @@ -36,20 +36,25 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +typedef struct lino_state lino_t; + typedef struct lino_completions { size_t len; char **cvec; } lino_completions_t; -typedef void (lino_compl_cb_t)(const char *, lino_completions_t *); -void lino_set_completion_cb(lino_compl_cb_t *); +typedef void lino_compl_cb_t(const char *, lino_completions_t *, void *ctx); +void lino_set_completion_cb(lino_t *, lino_compl_cb_t *, void *ctx); void lino_add_completion(lino_completions_t *, const char *); -char *linenoise(const char *prompt); -int lino_hist_add(const char *line); -int lino_hist_set_max_len(int len); -int lino_hist_save(const char *filename); -int lino_hist_load(const char *filename); -void lino_clear_screen(void); -void lino_set_multiline(int ml); -void lino_print_keycodes(void); +lino_t *lino_make(int ifd, int ofd); +void lino_free(lino_t *); + +char *linenoise(lino_t *, const char *prompt); +int lino_hist_add(lino_t *, const char *line); +int lino_hist_set_max_len(lino_t *, int len); +int lino_hist_save(lino_t *, const char *filename); +int lino_hist_load(lino_t *, const char *filename); +int lino_clear_screen(lino_t *); +void lino_set_multiline(lino_t *, int ml); +void lino_print_keycodes(lino_t *); -- cgit v1.2.3