KBaba7/llama.cpp
0
1/* linenoise.h -- VERSION 1.02 *3 * Guerrilla line editing library against the idea that a line editing lib4 * 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 without17 * modification, are permitted provided that the following conditions are18 * met:19 *20 * * Redistributions of source code must retain the above copyright21 * notice, this list of conditions and the following disclaimer.22 *23 * * Redistributions in binary form must reproduce the above copyright24 * notice, this list of conditions and the following disclaimer in the25 * documentation and/or other materials provided with the distribution.26 *27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT31 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.38 */39 40#ifndef __LINENOISE_H41#define __LINENOISE_H42 43#ifdef __cplusplus44extern "C" {45#endif46 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 editing54 * functionalities. */55struct linenoiseState {56 int in_completion; /* The user pressed TAB and we are now in completion57 * 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 oldpos; /* Previous refresh cursor 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 refrehsed 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(cvec[i]);85 }86 87 free(cvec);88 }89};90 91/* Non blocking API. */92int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt);93const char *linenoiseEditFeed(struct linenoiseState *l);94void linenoiseEditStop(struct linenoiseState *l);95void linenoiseHide(struct linenoiseState *l);96void linenoiseShow(struct linenoiseState *l);97 98/* Blocking API. */99const char *linenoise(const char *prompt);100void linenoiseFree(void *ptr);101 102/* Completion API. */103typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);104typedef const char*(linenoiseHintsCallback)(const char *, int *color, int *bold);105typedef void(linenoiseFreeHintsCallback)(const char *);106void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);107void linenoiseSetHintsCallback(linenoiseHintsCallback *);108void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);109void linenoiseAddCompletion(linenoiseCompletions *, const char *);110 111/* History API. */112int linenoiseHistoryAdd(const char *line);113int linenoiseHistorySetMaxLen(int len);114int linenoiseHistorySave(const char *filename);115int linenoiseHistoryLoad(const char *filename);116 117/* Other utilities. */118void linenoiseClearScreen(void);119void linenoiseSetMultiLine(int ml);120void linenoisePrintKeyCodes(void);121void linenoiseMaskModeEnable(void);122void linenoiseMaskModeDisable(void);123 124#ifdef __cplusplus125}126#endif127 128#endif /* __LINENOISE_H */129 