histedit.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* $NetBSD: histedit.h,v 1.61 2022/02/08 21:13:22 rillig Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Christos Zoulas of Cornell University.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)histedit.h 8.2 (Berkeley) 1/3/94
  34. */
  35. /*
  36. * histedit.h: Line editor and history interface.
  37. */
  38. #ifndef _HISTEDIT_H_
  39. #define _HISTEDIT_H_
  40. #define LIBEDIT_MAJOR 2
  41. #define LIBEDIT_MINOR 11
  42. #include <sys/types.h>
  43. #include <stdio.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /*
  48. * ==== Editing ====
  49. */
  50. typedef struct editline EditLine;
  51. /*
  52. * For user-defined function interface
  53. */
  54. typedef struct lineinfo {
  55. const char *buffer;
  56. const char *cursor;
  57. const char *lastchar;
  58. } LineInfo;
  59. /*
  60. * EditLine editor function return codes.
  61. * For user-defined function interface
  62. */
  63. #define CC_NORM 0
  64. #define CC_NEWLINE 1
  65. #define CC_EOF 2
  66. #define CC_ARGHACK 3
  67. #define CC_REFRESH 4
  68. #define CC_CURSOR 5
  69. #define CC_ERROR 6
  70. #define CC_FATAL 7
  71. #define CC_REDISPLAY 8
  72. #define CC_REFRESH_BEEP 9
  73. /*
  74. * Initialization, cleanup, and resetting
  75. */
  76. EditLine *el_init(const char *, FILE *, FILE *, FILE *);
  77. EditLine *el_init_fd(const char *, FILE *, FILE *, FILE *,
  78. int, int, int);
  79. void el_end(EditLine *);
  80. void el_reset(EditLine *);
  81. /*
  82. * Get a line, a character or push a string back in the input queue
  83. */
  84. const char *el_gets(EditLine *, int *);
  85. int el_getc(EditLine *, char *);
  86. void el_push(EditLine *, const char *);
  87. /*
  88. * Beep!
  89. */
  90. void el_beep(EditLine *);
  91. /*
  92. * High level function internals control
  93. * Parses argc, argv array and executes builtin editline commands
  94. */
  95. int el_parse(EditLine *, int, const char **);
  96. /*
  97. * Low level editline access functions
  98. */
  99. int el_set(EditLine *, int, ...);
  100. int el_get(EditLine *, int, ...);
  101. unsigned char _el_fn_complete(EditLine *, int);
  102. /*
  103. * el_set/el_get parameters
  104. *
  105. * When using el_wset/el_wget (as opposed to el_set/el_get):
  106. * Char is wchar_t, otherwise it is char.
  107. * prompt_func is el_wpfunc_t, otherwise it is el_pfunc_t .
  108. * Prompt function prototypes are:
  109. * typedef char *(*el_pfunct_t) (EditLine *);
  110. * typedef wchar_t *(*el_wpfunct_t) (EditLine *);
  111. *
  112. * For operations that support set or set/get, the argument types listed are for
  113. * the "set" operation. For "get", each listed type must be a pointer.
  114. * E.g. EL_EDITMODE takes an int when set, but an int* when get.
  115. *
  116. * Operations that only support "get" have the correct argument types listed.
  117. */
  118. #define EL_PROMPT 0 /* , prompt_func); set/get */
  119. #define EL_TERMINAL 1 /* , const char *); set/get */
  120. #define EL_EDITOR 2 /* , const Char *); set/get */
  121. #define EL_SIGNAL 3 /* , int); set/get */
  122. #define EL_BIND 4 /* , const Char *, ..., NULL); set */
  123. #define EL_TELLTC 5 /* , const Char *, ..., NULL); set */
  124. #define EL_SETTC 6 /* , const Char *, ..., NULL); set */
  125. #define EL_ECHOTC 7 /* , const Char *, ..., NULL); set */
  126. #define EL_SETTY 8 /* , const Char *, ..., NULL); set */
  127. #define EL_ADDFN 9 /* , const Char *, const Char, set */
  128. /* el_func_t); */
  129. #define EL_HIST 10 /* , hist_fun_t, const void *); set */
  130. #define EL_EDITMODE 11 /* , int); set/get */
  131. #define EL_RPROMPT 12 /* , prompt_func); set/get */
  132. #define EL_GETCFN 13 /* , el_rfunc_t); set/get */
  133. #define EL_CLIENTDATA 14 /* , void *); set/get */
  134. #define EL_UNBUFFERED 15 /* , int); set/get */
  135. #define EL_PREP_TERM 16 /* , int); set */
  136. #define EL_GETTC 17 /* , const Char *, ..., NULL); get */
  137. #define EL_GETFP 18 /* , int, FILE **); get */
  138. #define EL_SETFP 19 /* , int, FILE *); set */
  139. #define EL_REFRESH 20 /* , void); set */
  140. #define EL_PROMPT_ESC 21 /* , prompt_func, Char); set/get */
  141. #define EL_RPROMPT_ESC 22 /* , prompt_func, Char); set/get */
  142. #define EL_RESIZE 23 /* , el_zfunc_t, void *); set */
  143. #define EL_ALIAS_TEXT 24 /* , el_afunc_t, void *); set */
  144. #define EL_SAFEREAD 25 /* , int); set/get */
  145. #define EL_BUILTIN_GETCFN (NULL)
  146. /*
  147. * Source named file or $PWD/.editrc or $HOME/.editrc
  148. */
  149. int el_source(EditLine *, const char *);
  150. /*
  151. * Must be called when the terminal changes size; If EL_SIGNAL
  152. * is set this is done automatically otherwise it is the responsibility
  153. * of the application
  154. */
  155. void el_resize(EditLine *);
  156. /*
  157. * User-defined function interface.
  158. */
  159. const LineInfo *el_line(EditLine *);
  160. int el_insertstr(EditLine *, const char *);
  161. void el_deletestr(EditLine *, int);
  162. int el_replacestr(EditLine *, const char *);
  163. int el_deletestr1(EditLine *, int, int);
  164. /*
  165. * ==== History ====
  166. */
  167. typedef struct history History;
  168. typedef struct HistEvent {
  169. int num;
  170. const char *str;
  171. } HistEvent;
  172. /*
  173. * History access functions.
  174. */
  175. History * history_init(void);
  176. void history_end(History *);
  177. int history(History *, HistEvent *, int, ...);
  178. #define H_FUNC 0 /* , UTSL */
  179. #define H_SETSIZE 1 /* , const int); */
  180. #define H_GETSIZE 2 /* , void); */
  181. #define H_FIRST 3 /* , void); */
  182. #define H_LAST 4 /* , void); */
  183. #define H_PREV 5 /* , void); */
  184. #define H_NEXT 6 /* , void); */
  185. #define H_CURR 8 /* , const int); */
  186. #define H_SET 7 /* , int); */
  187. #define H_ADD 9 /* , const wchar_t *); */
  188. #define H_ENTER 10 /* , const wchar_t *); */
  189. #define H_APPEND 11 /* , const wchar_t *); */
  190. #define H_END 12 /* , void); */
  191. #define H_NEXT_STR 13 /* , const wchar_t *); */
  192. #define H_PREV_STR 14 /* , const wchar_t *); */
  193. #define H_NEXT_EVENT 15 /* , const int); */
  194. #define H_PREV_EVENT 16 /* , const int); */
  195. #define H_LOAD 17 /* , const char *); */
  196. #define H_SAVE 18 /* , const char *); */
  197. #define H_CLEAR 19 /* , void); */
  198. #define H_SETUNIQUE 20 /* , int); */
  199. #define H_GETUNIQUE 21 /* , void); */
  200. #define H_DEL 22 /* , int); */
  201. #define H_NEXT_EVDATA 23 /* , const int, histdata_t *); */
  202. #define H_DELDATA 24 /* , int, histdata_t *);*/
  203. #define H_REPLACE 25 /* , const char *, histdata_t); */
  204. #define H_SAVE_FP 26 /* , FILE *); */
  205. #define H_NSAVE_FP 27 /* , size_t, FILE *); */
  206. /*
  207. * ==== Tokenization ====
  208. */
  209. typedef struct tokenizer Tokenizer;
  210. /*
  211. * String tokenization functions, using simplified sh(1) quoting rules
  212. */
  213. Tokenizer *tok_init(const char *);
  214. void tok_end(Tokenizer *);
  215. void tok_reset(Tokenizer *);
  216. int tok_line(Tokenizer *, const LineInfo *,
  217. int *, const char ***, int *, int *);
  218. int tok_str(Tokenizer *, const char *,
  219. int *, const char ***);
  220. /*
  221. * Begin Wide Character Support
  222. */
  223. #include <wchar.h>
  224. #include <wctype.h>
  225. #ifndef HAVE_WCSDUP
  226. wchar_t * wcsdup(const wchar_t *str);
  227. #endif
  228. /*
  229. * ==== Editing ====
  230. */
  231. typedef struct lineinfow {
  232. const wchar_t *buffer;
  233. const wchar_t *cursor;
  234. const wchar_t *lastchar;
  235. } LineInfoW;
  236. typedef int (*el_rfunc_t)(EditLine *, wchar_t *);
  237. const wchar_t *el_wgets(EditLine *, int *);
  238. int el_wgetc(EditLine *, wchar_t *);
  239. void el_wpush(EditLine *, const wchar_t *);
  240. int el_wparse(EditLine *, int, const wchar_t **);
  241. int el_wset(EditLine *, int, ...);
  242. int el_wget(EditLine *, int, ...);
  243. int el_cursor(EditLine *, int);
  244. const LineInfoW *el_wline(EditLine *);
  245. int el_winsertstr(EditLine *, const wchar_t *);
  246. #define el_wdeletestr el_deletestr
  247. int el_wreplacestr(EditLine *, const wchar_t *);
  248. /*
  249. * ==== History ====
  250. */
  251. typedef struct histeventW {
  252. int num;
  253. const wchar_t *str;
  254. } HistEventW;
  255. typedef struct historyW HistoryW;
  256. HistoryW * history_winit(void);
  257. void history_wend(HistoryW *);
  258. int history_w(HistoryW *, HistEventW *, int, ...);
  259. /*
  260. * ==== Tokenization ====
  261. */
  262. typedef struct tokenizerW TokenizerW;
  263. /* Wide character tokenizer support */
  264. TokenizerW *tok_winit(const wchar_t *);
  265. void tok_wend(TokenizerW *);
  266. void tok_wreset(TokenizerW *);
  267. int tok_wline(TokenizerW *, const LineInfoW *,
  268. int *, const wchar_t ***, int *, int *);
  269. int tok_wstr(TokenizerW *, const wchar_t *,
  270. int *, const wchar_t ***);
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif /* _HISTEDIT_H_ */