vi_utils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  3. */
  4. #ifndef __VI_UTILS_H__
  5. #define __VI_UTILS_H__
  6. #include <ctype.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <stdarg.h>
  14. #include <stddef.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <dfs_posix.h>
  18. #include <dfs_poll.h>
  19. #include <sys/types.h>
  20. #include <mem_sandbox.h>
  21. #define BB_VER "latest: 2021-04-20"
  22. #define BB_BT "busybox vi"
  23. //config:config FEATURE_VI_MAX_LEN
  24. //config: int "Maximum screen width in vi"
  25. //config: range 256 16384
  26. //config: default 4096
  27. //config: depends on VI
  28. //config: help
  29. //config: Contrary to what you may think, this is not eating much.
  30. //config: Make it smaller than 4k only if you are very limited on memory.
  31. #ifdef VI_MAX_LEN
  32. #define CONFIG_FEATURE_VI_MAX_LEN VI_MAX_LEN
  33. #else
  34. #define CONFIG_FEATURE_VI_MAX_LEN 4096
  35. #endif
  36. //config:config FEATURE_VI_ASK_TERMINAL
  37. //config: bool "Use 'tell me cursor position' ESC sequence to measure window"
  38. //config: default y
  39. //config: depends on VI
  40. //config: help
  41. //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
  42. //config: this option makes vi perform a last-ditch effort to find it:
  43. //config: position cursor to 999,999 and ask terminal to report real
  44. //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
  45. //config:
  46. //config: This is not clean but helps a lot on serial lines and such.
  47. #ifdef VI_ENABLE_VI_ASK_TERMINAL
  48. #define ENABLE_FEATURE_VI_ASK_TERMINAL 1
  49. #define IF_FEATURE_VI_ASK_TERMINAL(...) __VA_ARGS__
  50. #else
  51. #define ENABLE_FEATURE_VI_ASK_TERMINAL 0
  52. #define IF_FEATURE_VI_ASK_TERMINAL(...)
  53. #endif
  54. //config:config FEATURE_VI_COLON
  55. //config: bool "Enable \":\" colon commands (no \"ex\" mode)"
  56. //config: default y
  57. //config: depends on VI
  58. //config: help
  59. //config: Enable a limited set of colon commands for vi. This does not
  60. //config: provide an "ex" mode.
  61. #ifdef VI_ENABLE_COLON
  62. #define ENABLE_FEATURE_VI_COLON 1
  63. #define IF_FEATURE_VI_COLON(...) __VA_ARGS__
  64. #else
  65. #define ENABLE_FEATURE_VI_COLON 0
  66. #define IF_FEATURE_VI_COLON(...)
  67. #endif
  68. //config:config FEATURE_VI_SEARCH
  69. //config: bool "Enable search and replace cmds"
  70. //config: default y
  71. //config: depends on VI
  72. //config: help
  73. //config: Select this if you wish to be able to do search and replace in
  74. //config: busybox vi.
  75. #ifdef VI_ENABLE_SEARCH
  76. #define ENABLE_FEATURE_VI_SEARCH 1
  77. #define IF_FEATURE_VI_SEARCH(...) __VA_ARGS__
  78. #else
  79. #define ENABLE_FEATURE_VI_SEARCH 0
  80. #define IF_FEATURE_VI_SEARCH(...)
  81. #endif
  82. //config:config FEATURE_VI_READONLY
  83. //config: bool "Enable -R option and \"view\" mode"
  84. //config: default y
  85. //config: depends on VI
  86. //config: help
  87. //config: Enable the read-only command line option, which allows the user to
  88. //config: open a file in read-only mode.
  89. #ifdef VI_ENABLE_READONLY
  90. #define ENABLE_FEATURE_VI_READONLY 1
  91. #define IF_FEATURE_VI_READONLY(...) __VA_ARGS__
  92. #else
  93. #define ENABLE_FEATURE_VI_READONLY 0
  94. #define IF_FEATURE_VI_READONLY(...)
  95. #endif
  96. //config:config FEATURE_VI_SET
  97. //config: bool "Support for :set"
  98. //config: default y
  99. //config: depends on VI
  100. //config: help
  101. //config: Support for ":set".
  102. #ifdef VI_ENABLE_SET
  103. #define ENABLE_FEATURE_VI_SET 1
  104. #define IF_FEATURE_VI_SET(...) __VA_ARGS__
  105. #else
  106. #define ENABLE_FEATURE_VI_SET 0
  107. #define IF_FEATURE_VI_SET(...)
  108. #endif
  109. //config:config FEATURE_VI_SETOPTS
  110. //config: bool "Enable set-able options, ai ic showmatch"
  111. //config: default y
  112. //config: depends on VI
  113. //config: help
  114. //config: Enable the editor to set some (ai, ic, showmatch) options.
  115. #ifdef VI_ENABLE_SETOPTS
  116. #define ENABLE_FEATURE_VI_SETOPTS 1
  117. #define IF_FEATURE_VI_SETOPTS(...) __VA_ARGS__
  118. #else
  119. #define ENABLE_FEATURE_VI_SETOPTS 0
  120. #define IF_FEATURE_VI_SETOPTS(...)
  121. #endif
  122. //config:
  123. //config:config FEATURE_VI_WIN_RESIZE
  124. //config: bool "Handle window resize"
  125. //config: default y
  126. //config: depends on VI
  127. //config: help
  128. //config: Make busybox vi behave nicely with terminals that get resized.
  129. #ifdef VI_ENABLE_WIN_RESIZE
  130. #define ENABLE_FEATURE_VI_WIN_RESIZE 1
  131. #define IF_FEATURE_VI_WIN_RESIZE(...) __VA_ARGS__
  132. #else
  133. #define ENABLE_FEATURE_VI_WIN_RESIZE 0
  134. #define IF_FEATURE_VI_WIN_RESIZE(...)
  135. #endif
  136. //config:config FEATURE_VI_YANKMARK
  137. //config: bool "Enable yank/put commands and mark cmds"
  138. //config: default y
  139. //config: depends on VI
  140. //config: help
  141. //config: This will enable you to use yank and put, as well as mark in
  142. //config: busybox vi.
  143. //config:
  144. #ifdef VI_ENABLE_YANKMARK
  145. #define ENABLE_FEATURE_VI_YANKMARK 1
  146. #define IF_FEATURE_VI_YANKMARK(...) __VA_ARGS__
  147. #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  148. #else
  149. #define ENABLE_FEATURE_VI_YANKMARK 0
  150. #define IF_FEATURE_VI_YANKMARK(...)
  151. #endif
  152. //config:config FEATURE_VI_DOT_CMD
  153. //config: bool "Remember previous cmd and \".\" cmd"
  154. //config: default y
  155. //config: depends on VI
  156. //config: help
  157. //config: Make busybox vi remember the last command and be able to repeat it.
  158. #ifdef VI_ENABLE_DOT_CMD
  159. #define ENABLE_FEATURE_VI_DOT_CMD 1
  160. #define IF_FEATURE_VI_DOT_CMD(...) __VA_ARGS__
  161. #else
  162. #define ENABLE_FEATURE_VI_DOT_CMD 0
  163. #define IF_FEATURE_VI_DOT_CMD(...)
  164. #endif
  165. //config:config FEATURE_VI_UNDO
  166. //config: bool "Support undo command 'u'"
  167. //config: default y
  168. //config: depends on VI
  169. //config: help
  170. //config: Support the 'u' command to undo insertion, deletion, and replacement
  171. //config: of text.
  172. #ifdef VI_ENABLE_UNDO
  173. #define ENABLE_FEATURE_VI_UNDO 1
  174. #define IF_FEATURE_VI_UNDO(...) __VA_ARGS__
  175. #else
  176. #define ENABLE_FEATURE_VI_UNDO 0
  177. #define IF_FEATURE_VI_UNDO(...)
  178. #endif
  179. //config:config FEATURE_VI_UNDO_QUEUE
  180. //config: bool "Enable undo operation queuing"
  181. //config: default y
  182. //config: depends on FEATURE_VI_UNDO
  183. //config: help
  184. //config: The vi undo functions can use an intermediate queue to greatly lower
  185. //config: malloc() calls and overhead. When the maximum size of this queue is
  186. //config: reached, the contents of the queue are committed to the undo stack.
  187. //config: This increases the size of the undo code and allows some undo
  188. //config: operations (especially un-typing/backspacing) to be far more useful.
  189. //config:config FEATURE_VI_UNDO_QUEUE_MAX
  190. //config: int "Maximum undo character queue size"
  191. //config: default 256
  192. //config: range 32 65536
  193. //config: depends on FEATURE_VI_UNDO_QUEUE
  194. //config: help
  195. //config: This option sets the number of bytes used at runtime for the queue.
  196. //config: Smaller values will create more undo objects and reduce the amount
  197. //config: of typed or backspaced characters that are grouped into one undo
  198. //config: operation; larger values increase the potential size of each undo
  199. //config: and will generally malloc() larger objects and less frequently.
  200. //config: Unless you want more (or less) frequent "undo points" while typing,
  201. //config: you should probably leave this unchanged.
  202. #ifdef VI_ENABLE_UNDO_QUEUE
  203. #define ENABLE_FEATURE_VI_UNDO_QUEUE 1
  204. #define IF_FEATURE_VI_UNDO_QUEUE(...) __VA_ARGS__
  205. #define CONFIG_FEATURE_VI_UNDO_QUEUE_MAX VI_UNDO_QUEUE_MAX
  206. #else
  207. #define ENABLE_FEATURE_VI_UNDO_QUEUE 0
  208. #define IF_FEATURE_VI_UNDO_QUEUE(...)
  209. #endif
  210. //config:config FEATURE_VI_VERBOSE_STATUS
  211. //config: bool "Enable verbose status reporting"
  212. //config: default y
  213. //config: depends on VI
  214. //config: help
  215. //config: Enable more verbose reporting of the results of yank, change,
  216. //config: delete, undo and substitution commands.
  217. #ifdef VI_ENABLE_VERBOSE_STATUS
  218. #define ENABLE_FEATURE_VI_VERBOSE_STATUS 1
  219. #else
  220. #define ENABLE_FEATURE_VI_VERBOSE_STATUS 0
  221. #endif
  222. //config:config FEATURE_VI_REGEX_SEARCH
  223. //config: bool "Enable regex in search and replace"
  224. //config: default n # Uses GNU regex, which may be unavailable. FIXME
  225. //config: depends on FEATURE_VI_SEARCH
  226. //config: help
  227. //config: Use extended regex search.
  228. #ifdef VI_ENABLE_REGEX_SEARCH
  229. #define ENABLE_FEATURE_VI_REGEX_SEARCH 1
  230. #define IF_FEATURE_VI_REGEX_SEARCH(...) __VA_ARGS__
  231. #else
  232. #define ENABLE_FEATURE_VI_REGEX_SEARCH 0
  233. #define IF_FEATURE_VI_REGEX_SEARCH(...)
  234. #endif
  235. //config:config FEATURE_VI_8BIT
  236. //config: bool "Allow vi to display 8-bit chars (otherwise shows dots)"
  237. //config: default n
  238. //config: depends on VI
  239. //config: help
  240. //config: If your terminal can display characters with high bit set,
  241. //config: you may want to enable this. Note: vi is not Unicode-capable.
  242. //config: If your terminal combines several 8-bit bytes into one character
  243. //config: (as in Unicode mode), this will not work properly.
  244. #ifdef VI_ENABLE_8BIT
  245. #define ENABLE_FEATURE_VI_8BIT 1
  246. #define IF_FEATURE_VI_8BIT(...) __VA_ARGS__
  247. #else
  248. #define ENABLE_FEATURE_VI_8BIT 0
  249. #define IF_FEATURE_VI_8BIT(...)
  250. #endif
  251. /*----------------------------------------------------------------*/
  252. #define SET_PTR_TO_GLOBALS(x) do { \
  253. (*(struct globals**)&ptr_to_globals) = (void*)(x); \
  254. barrier(); \
  255. } while (0)
  256. /* "Keycodes" that report an escape sequence.
  257. * We use something which fits into signed char,
  258. * yet doesn't represent any valid Unicode character.
  259. * Also, -1 is reserved for error indication and we don't use it. */
  260. enum {
  261. KEYCODE_UP = -2,
  262. KEYCODE_DOWN = -3,
  263. KEYCODE_RIGHT = -4,
  264. KEYCODE_LEFT = -5,
  265. KEYCODE_HOME = -6,
  266. KEYCODE_END = -7,
  267. KEYCODE_INSERT = -8,
  268. KEYCODE_DELETE = -9,
  269. KEYCODE_PAGEUP = -10,
  270. KEYCODE_PAGEDOWN = -11,
  271. // -12 is reserved for Alt/Ctrl/Shift-TAB
  272. #if 0
  273. KEYCODE_FUN1 = -13,
  274. KEYCODE_FUN2 = -14,
  275. KEYCODE_FUN3 = -15,
  276. KEYCODE_FUN4 = -16,
  277. KEYCODE_FUN5 = -17,
  278. KEYCODE_FUN6 = -18,
  279. KEYCODE_FUN7 = -19,
  280. KEYCODE_FUN8 = -20,
  281. KEYCODE_FUN9 = -21,
  282. KEYCODE_FUN10 = -22,
  283. KEYCODE_FUN11 = -23,
  284. KEYCODE_FUN12 = -24,
  285. #endif
  286. /* Be sure that last defined value is small enough
  287. * to not interfere with Alt/Ctrl/Shift bits.
  288. * So far we do not exceed -31 (0xfff..fffe1),
  289. * which gives us three upper bits in LSB to play with.
  290. */
  291. //KEYCODE_SHIFT_TAB = (-12) & ~0x80,
  292. //KEYCODE_SHIFT_... = KEYCODE_... & ~0x80,
  293. //KEYCODE_CTRL_UP = KEYCODE_UP & ~0x40,
  294. //KEYCODE_CTRL_DOWN = KEYCODE_DOWN & ~0x40,
  295. KEYCODE_CTRL_RIGHT = KEYCODE_RIGHT & ~0x40,
  296. KEYCODE_CTRL_LEFT = KEYCODE_LEFT & ~0x40,
  297. //KEYCODE_ALT_UP = KEYCODE_UP & ~0x20,
  298. //KEYCODE_ALT_DOWN = KEYCODE_DOWN & ~0x20,
  299. KEYCODE_ALT_RIGHT = KEYCODE_RIGHT & ~0x20,
  300. KEYCODE_ALT_LEFT = KEYCODE_LEFT & ~0x20,
  301. KEYCODE_CURSOR_POS = -0x100, /* 0xfff..fff00 */
  302. /* How long is the longest ESC sequence we know?
  303. * We want it big enough to be able to contain
  304. * cursor position sequence "ESC [ 9999 ; 9999 R"
  305. */
  306. KEYCODE_BUFFER_SIZE = 16
  307. };
  308. typedef enum {FALSE = 0, TRUE = !FALSE} bool;
  309. typedef int smallint;
  310. typedef unsigned smalluint;
  311. #if defined(_MSC_VER) || defined(__CC_ARM) || defined(__ICCARM__)
  312. #define ALIGN1
  313. #define barrier()
  314. #define F_OK 0
  315. #define R_OK 4
  316. #define W_OK 2
  317. #define X_OK 1
  318. int isblank(int ch);
  319. int isatty (int fd);
  320. #else
  321. #define ALIGN1 __attribute__((aligned(1)))
  322. /* At least gcc 3.4.6 on mipsel system needs optimization barrier */
  323. #define barrier() __asm__ __volatile__("":::"memory")
  324. #endif
  325. #define vi_strtou strtoul
  326. #define fflush_all() fflush(NULL)
  327. unsigned char vi_mem_init(void);
  328. void vi_mem_release(void);
  329. void *vi_malloc(rt_size_t size);
  330. void *vi_realloc(void *rmem, rt_size_t newsize);
  331. void vi_free(void *ptr);
  332. void* vi_zalloc(size_t size);
  333. char *vi_strdup(const char *s);
  334. char *vi_strndup(const char *s, size_t n);
  335. int64_t read_key(int fd, char *buffer, int timeout);
  336. void *memrchr(const void* ptr, int ch, size_t pos);
  337. char* xasprintf(const char *format, ...);
  338. #ifdef VI_ENABLE_SEARCH
  339. char* strchrnul(const char *s, int c);
  340. #endif
  341. #ifdef VI_ENABLE_COLON
  342. char* last_char_is(const char *s, int c);
  343. #endif
  344. #ifdef VI_ENABLE_SETOPTS
  345. char* skip_whitespace(const char *s);
  346. char* skip_non_whitespace(const char *s);
  347. #endif
  348. int index_in_strings(const char *strings, const char *key);
  349. int safe_read(int fd, void *buf, size_t count);
  350. int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout);
  351. ssize_t full_write(int fd, const void *buf, size_t len);
  352. ssize_t full_read(int fd, void *buf, size_t len);
  353. #ifdef RT_USING_POSIX_TERMIOS
  354. #include <termios.h>
  355. #define TERMIOS_CLEAR_ISIG (1 << 0)
  356. #define TERMIOS_RAW_CRNL_INPUT (1 << 1)
  357. #define TERMIOS_RAW_CRNL_OUTPUT (1 << 2)
  358. #define TERMIOS_RAW_CRNL (TERMIOS_RAW_CRNL_INPUT|TERMIOS_RAW_CRNL_OUTPUT)
  359. #define TERMIOS_RAW_INPUT (1 << 3)
  360. int tcsetattr_stdin_TCSANOW(const struct termios *tp);
  361. int get_terminal_width_height(int fd, unsigned *width, unsigned *height);
  362. int set_termios_to_raw(int fd, struct termios *oldterm, int flags);
  363. #endif
  364. //config: TODO for RT-Thread
  365. //config:config FEATURE_VI_USE_SIGNALS
  366. //config: bool "Catch signals"
  367. //config: default y
  368. //config: depends on VI
  369. //config: help
  370. //config: Selecting this option will make busybox vi signal aware. This will
  371. //config: make busybox vi support SIGWINCH to deal with Window Changes, catch
  372. //config: Ctrl-Z and Ctrl-C and alarms.
  373. #endif