diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2015-09-04 22:09:43 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2015-09-04 22:09:43 -0700 |
commit | edbb612c40e3541a170aeaaf41ad8703c8b70cf9 (patch) | |
tree | dec1033f820f12de39ffea15f095e40aff706333 /linenoise/linenoise.h | |
parent | 875c3401a17f9264b83f5dd9c673cac1ca837e1a (diff) | |
download | txr-edbb612c40e3541a170aeaaf41ad8703c8b70cf9.tar.gz txr-edbb612c40e3541a170aeaaf41ad8703c8b70cf9.tar.bz2 txr-edbb612c40e3541a170aeaaf41ad8703c8b70cf9.zip |
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.
Diffstat (limited to 'linenoise/linenoise.h')
-rw-r--r-- | linenoise/linenoise.h | 25 |
1 files changed, 15 insertions, 10 deletions
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 *); |