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