this repo has no description
1/* linenoise.h -- VERSION 1.0
2 *
3 * Guerrilla line editing library against the idea that a line editing lib
4 * needs to be 20,000 lines of C code.
5 *
6 * See linenoise.c for more information.
7 *
8 * ------------------------------------------------------------------------
9 *
10 * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
11 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
12 *
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are
17 * met:
18 *
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * * Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#ifndef __LINENOISE_H
40#define __LINENOISE_H
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46#include <stddef.h> /* For size_t. */
47
48extern int linenoiseWasInterrupted; /* boolean signalling if last call was ctrl-c */
49extern char *linenoiseEditMore;
50
51/* The linenoiseState structure represents the state during line editing.
52 * We pass this state to functions implementing specific editing
53 * functionalities. */
54struct linenoiseState {
55 int in_completion; /* The user pressed TAB and we are now in completion
56 * mode, so input is handled by completeLine(). */
57 size_t completion_idx; /* Index of next completion to propose. */
58 int ifd; /* Terminal stdin file descriptor. */
59 int ofd; /* Terminal stdout file descriptor. */
60 char *buf; /* Edited line buffer. */
61 size_t buflen; /* Edited line buffer size. */
62 const char *prompt; /* Prompt to display. */
63 size_t plen; /* Prompt length. */
64 size_t pos; /* Current cursor position. */
65 size_t oldcolpos; /* Previous refresh cursor column position. */
66 size_t len; /* Current edited line length. */
67 size_t cols; /* Number of columns in terminal. */
68 size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */
69 int history_index; /* The history index we are currently editing. */
70};
71
72typedef struct linenoiseCompletions {
73 size_t len;
74 char **cvec;
75} linenoiseCompletions;
76
77/* Non blocking API. */
78int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);
79char *linenoiseEditFeed(struct linenoiseState *l);
80void linenoiseEditStop(struct linenoiseState *l);
81void linenoiseHide(struct linenoiseState *l);
82void linenoiseShow(struct linenoiseState *l);
83
84/* Blocking API. */
85char *linenoise(const char *prompt);
86void linenoiseFree(void *ptr);
87
88/* Completion API. */
89typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
90typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold);
91typedef void(linenoiseFreeHintsCallback)(void *);
92void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
93void linenoiseSetHintsCallback(linenoiseHintsCallback *);
94void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
95void linenoiseAddCompletion(linenoiseCompletions *, const char *);
96
97/* History API. */
98int linenoiseHistoryAdd(const char *line);
99int linenoiseHistorySetMaxLen(int len);
100int linenoiseHistorySave(const char *filename);
101int linenoiseHistoryLoad(const char *filename);
102
103/* Other utilities. */
104void linenoiseClearScreen(void);
105void linenoiseSetMultiLine(int ml);
106void linenoisePrintKeyCodes(void);
107void linenoiseMaskModeEnable(void);
108void linenoiseMaskModeDisable(void);
109
110typedef size_t (linenoisePrevCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len);
111typedef size_t (linenoiseNextCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len);
112typedef size_t (linenoiseReadCode)(int fd, char *buf, size_t buf_len, int* c);
113
114void linenoiseSetEncodingFunctions(
115 linenoisePrevCharLen *prevCharLenFunc,
116 linenoiseNextCharLen *nextCharLenFunc,
117 linenoiseReadCode *readCodeFunc);
118
119#ifdef __cplusplus
120}
121#endif
122
123#endif /* __LINENOISE_H */