vi_utils.h 13 KB

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