1
0

linenoise.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef __LINENOISE_H
  40. #define __LINENOISE_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #include <stddef.h> /* For size_t. */
  45. #include <stdlib.h>
  46. extern const char * linenoiseEditMore;
  47. /* The linenoiseState structure represents the state during line editing.
  48. * We pass this state to functions implementing specific editing
  49. * functionalities. */
  50. struct linenoiseState {
  51. int in_completion; /* The user pressed TAB and we are now in completion
  52. * mode, so input is handled by completeLine(). */
  53. size_t completion_idx; /* Index of next completion to propose. */
  54. int ifd; /* Terminal stdin file descriptor. */
  55. int ofd; /* Terminal stdout file descriptor. */
  56. char * buf; /* Edited line buffer. */
  57. size_t buflen; /* Edited line buffer size. */
  58. const char * prompt; /* Prompt to display. */
  59. size_t plen; /* Prompt length. */
  60. size_t pos; /* Current cursor position. */
  61. size_t oldcolpos; /* Previous refresh cursor column position. */
  62. size_t len; /* Current edited line length. */
  63. size_t cols; /* Number of columns in terminal. */
  64. size_t oldrows; /* Rows used by last refreshed line (multiline mode) */
  65. int history_index; /* The history index we are currently editing. */
  66. };
  67. struct linenoiseCompletions {
  68. size_t len = 0;
  69. char ** cvec = nullptr;
  70. bool to_free = true;
  71. ~linenoiseCompletions() {
  72. if (!to_free) {
  73. return;
  74. }
  75. for (size_t i = 0; i < len; ++i) {
  76. free(cvec[i]);
  77. }
  78. free(cvec);
  79. }
  80. };
  81. /* Non blocking API. */
  82. int linenoiseEditStart(struct linenoiseState * l, int stdin_fd, int stdout_fd, char * buf, size_t buflen,
  83. const char * prompt);
  84. const char * linenoiseEditFeed(struct linenoiseState * l);
  85. void linenoiseEditStop(struct linenoiseState * l);
  86. void linenoiseHide(struct linenoiseState * l);
  87. void linenoiseShow(struct linenoiseState * l);
  88. /* Blocking API. */
  89. const char * linenoise(const char * prompt);
  90. void linenoiseFree(void * ptr);
  91. /* Completion API. */
  92. typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
  93. typedef const char *(linenoiseHintsCallback) (const char *, int * color, int * bold);
  94. typedef void(linenoiseFreeHintsCallback)(const char *);
  95. void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
  96. void linenoiseSetHintsCallback(linenoiseHintsCallback *);
  97. void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *);
  98. void linenoiseAddCompletion(linenoiseCompletions *, const char *);
  99. /* History API. */
  100. int linenoiseHistoryAdd(const char * line);
  101. int linenoiseHistorySetMaxLen(int len);
  102. int linenoiseHistorySave(const char * filename);
  103. int linenoiseHistoryLoad(const char * filename);
  104. /* Other utilities. */
  105. void linenoiseClearScreen(void);
  106. void linenoiseSetMultiLine(int ml);
  107. void linenoisePrintKeyCodes(void);
  108. void linenoiseMaskModeEnable(void);
  109. void linenoiseMaskModeDisable(void);
  110. /* Encoding functions. */
  111. typedef size_t(linenoisePrevCharLen)(const char * buf, size_t buf_len, size_t pos, size_t * col_len);
  112. typedef size_t(linenoiseNextCharLen)(const char * buf, size_t buf_len, size_t pos, size_t * col_len);
  113. typedef size_t(linenoiseReadCode)(int fd, char * buf, size_t buf_len, int * c);
  114. void linenoiseSetEncodingFunctions(linenoisePrevCharLen * prevCharLenFunc, linenoiseNextCharLen * nextCharLenFunc,
  115. linenoiseReadCode * readCodeFunc);
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif /* __LINENOISE_H */