vi_utils.h 13 KB

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