vi_utils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <posix/string.h>
  14. #include <unistd.h>
  15. #include <poll.h>
  16. #include <sys/types.h>
  17. #include <sys/errno.h>
  18. #include <sys/stat.h>
  19. #define DBG_TAG "vi"
  20. #define DBG_LVL DBG_INFO
  21. #include <rtdbg.h>
  22. #define BB_VER "latest: 2021-08-29"
  23. #define BB_BT "Busybox vi for RT-Thread"
  24. //config:config FEATURE_VI_MAX_LEN
  25. //config: int "Maximum screen width in vi"
  26. //config: range 256 16384
  27. //config: default 4096
  28. //config: depends on VI
  29. //config: help
  30. //config: Contrary to what you may think, this is not eating much.
  31. //config: Make it smaller than 4k only if you are very limited on memory.
  32. #ifdef VI_MAX_LEN
  33. #define CONFIG_FEATURE_VI_MAX_LEN VI_MAX_LEN
  34. #else
  35. #define CONFIG_FEATURE_VI_MAX_LEN 4096
  36. #endif
  37. //config:config FEATURE_VI_ASK_TERMINAL
  38. //config: bool "Use 'tell me cursor position' ESC sequence to measure window"
  39. //config: default y
  40. //config: depends on VI
  41. //config: help
  42. //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
  43. //config: this option makes vi perform a last-ditch effort to find it:
  44. //config: position cursor to 999,999 and ask terminal to report real
  45. //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
  46. //config:
  47. //config: This is not clean but helps a lot on serial lines and such.
  48. #ifdef VI_ENABLE_VI_ASK_TERMINAL
  49. #define ENABLE_FEATURE_VI_ASK_TERMINAL 1
  50. #define IF_FEATURE_VI_ASK_TERMINAL(...) __VA_ARGS__
  51. #else
  52. #define ENABLE_FEATURE_VI_ASK_TERMINAL 0
  53. #define IF_FEATURE_VI_ASK_TERMINAL(...)
  54. #endif
  55. //config:config FEATURE_VI_COLON
  56. //config: bool "Enable \":\" colon commands (no \"ex\" mode)"
  57. //config: default y
  58. //config: depends on VI
  59. //config: help
  60. //config: Enable a limited set of colon commands for vi. This does not
  61. //config: provide an "ex" mode.
  62. #ifdef VI_ENABLE_COLON
  63. #define ENABLE_FEATURE_VI_COLON 1
  64. #define IF_FEATURE_VI_COLON(...) __VA_ARGS__
  65. #else
  66. #define ENABLE_FEATURE_VI_COLON 0
  67. #define IF_FEATURE_VI_COLON(...)
  68. #endif
  69. //config:config FEATURE_VI_COLON_EXPAND
  70. //config: bool "Expand \"%\" and \"#\" in colon commands"
  71. //config: default y
  72. //config: depends on FEATURE_VI_COLON
  73. //config: help
  74. //config: Expand the special characters \"%\" (current filename)
  75. //config: and \"#\" (alternate filename) in colon commands.
  76. #ifdef VI_ENABLE_COLON_EXPAND
  77. #define ENABLE_FEATURE_VI_COLON_EXPAND 1
  78. #else
  79. #define ENABLE_FEATURE_VI_COLON_EXPAND 0
  80. #endif
  81. //config:config FEATURE_VI_SEARCH
  82. //config: bool "Enable search and replace cmds"
  83. //config: default y
  84. //config: depends on VI
  85. //config: help
  86. //config: Select this if you wish to be able to do search and replace in
  87. //config: busybox vi.
  88. #ifdef VI_ENABLE_SEARCH
  89. #define ENABLE_FEATURE_VI_SEARCH 1
  90. #define IF_FEATURE_VI_SEARCH(...) __VA_ARGS__
  91. #else
  92. #define ENABLE_FEATURE_VI_SEARCH 0
  93. #define IF_FEATURE_VI_SEARCH(...)
  94. #endif
  95. //config:config FEATURE_VI_READONLY
  96. //config: bool "Enable -R option and \"view\" mode"
  97. //config: default y
  98. //config: depends on VI
  99. //config: help
  100. //config: Enable the read-only command line option, which allows the user to
  101. //config: open a file in read-only mode.
  102. #ifdef VI_ENABLE_READONLY
  103. #define ENABLE_FEATURE_VI_READONLY 1
  104. #define IF_FEATURE_VI_READONLY(...) __VA_ARGS__
  105. #else
  106. #define ENABLE_FEATURE_VI_READONLY 0
  107. #define IF_FEATURE_VI_READONLY(...)
  108. #endif
  109. //config:config FEATURE_VI_SET
  110. //config: bool "Support for :set"
  111. //config: default y
  112. //config: depends on VI
  113. //config: help
  114. //config: Support for ":set".
  115. #ifdef VI_ENABLE_SET
  116. #define ENABLE_FEATURE_VI_SET 1
  117. #define IF_FEATURE_VI_SET(...) __VA_ARGS__
  118. #else
  119. #define ENABLE_FEATURE_VI_SET 0
  120. #define IF_FEATURE_VI_SET(...)
  121. #endif
  122. //config:config FEATURE_VI_SETOPTS
  123. //config: bool "Enable set-able options, ai ic showmatch"
  124. //config: default y
  125. //config: depends on VI
  126. //config: help
  127. //config: Enable the editor to set some (ai, ic, showmatch) options.
  128. #ifdef VI_ENABLE_SETOPTS
  129. #define ENABLE_FEATURE_VI_SETOPTS 1
  130. #define IF_FEATURE_VI_SETOPTS(...) __VA_ARGS__
  131. #else
  132. #define ENABLE_FEATURE_VI_SETOPTS 0
  133. #define IF_FEATURE_VI_SETOPTS(...)
  134. #endif
  135. //config:
  136. //config:config FEATURE_VI_WIN_RESIZE
  137. //config: bool "Handle window resize"
  138. //config: default y
  139. //config: depends on VI
  140. //config: help
  141. //config: Make busybox vi behave nicely with terminals that get resized.
  142. #ifdef VI_ENABLE_WIN_RESIZE
  143. #define ENABLE_FEATURE_VI_WIN_RESIZE 1
  144. #define IF_FEATURE_VI_WIN_RESIZE(...) __VA_ARGS__
  145. #else
  146. #define ENABLE_FEATURE_VI_WIN_RESIZE 0
  147. #define IF_FEATURE_VI_WIN_RESIZE(...)
  148. #endif
  149. //config:config FEATURE_VI_YANKMARK
  150. //config: bool "Enable yank/put commands and mark cmds"
  151. //config: default y
  152. //config: depends on VI
  153. //config: help
  154. //config: This will enable you to use yank and put, as well as mark in
  155. //config: busybox vi.
  156. //config:
  157. #ifdef VI_ENABLE_YANKMARK
  158. #define ENABLE_FEATURE_VI_YANKMARK 1
  159. #define IF_FEATURE_VI_YANKMARK(...) __VA_ARGS__
  160. #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
  161. #else
  162. #define ENABLE_FEATURE_VI_YANKMARK 0
  163. #define IF_FEATURE_VI_YANKMARK(...)
  164. #endif
  165. //config:config FEATURE_VI_DOT_CMD
  166. //config: bool "Remember previous cmd and \".\" cmd"
  167. //config: default y
  168. //config: depends on VI
  169. //config: help
  170. //config: Make busybox vi remember the last command and be able to repeat it.
  171. #ifdef VI_ENABLE_DOT_CMD
  172. #define ENABLE_FEATURE_VI_DOT_CMD 1
  173. #define IF_FEATURE_VI_DOT_CMD(...) __VA_ARGS__
  174. #else
  175. #define ENABLE_FEATURE_VI_DOT_CMD 0
  176. #define IF_FEATURE_VI_DOT_CMD(...)
  177. #endif
  178. //config:config FEATURE_VI_UNDO
  179. //config: bool "Support undo command 'u'"
  180. //config: default y
  181. //config: depends on VI
  182. //config: help
  183. //config: Support the 'u' command to undo insertion, deletion, and replacement
  184. //config: of text.
  185. #ifdef VI_ENABLE_UNDO
  186. #define ENABLE_FEATURE_VI_UNDO 1
  187. #define IF_FEATURE_VI_UNDO(...) __VA_ARGS__
  188. #else
  189. #define ENABLE_FEATURE_VI_UNDO 0
  190. #define IF_FEATURE_VI_UNDO(...)
  191. #endif
  192. //config:config FEATURE_VI_UNDO_QUEUE
  193. //config: bool "Enable undo operation queuing"
  194. //config: default y
  195. //config: depends on FEATURE_VI_UNDO
  196. //config: help
  197. //config: The vi undo functions can use an intermediate queue to greatly lower
  198. //config: malloc() calls and overhead. When the maximum size of this queue is
  199. //config: reached, the contents of the queue are committed to the undo stack.
  200. //config: This increases the size of the undo code and allows some undo
  201. //config: operations (especially un-typing/backspacing) to be far more useful.
  202. //config:config FEATURE_VI_UNDO_QUEUE_MAX
  203. //config: int "Maximum undo character queue size"
  204. //config: default 256
  205. //config: range 32 65536
  206. //config: depends on FEATURE_VI_UNDO_QUEUE
  207. //config: help
  208. //config: This option sets the number of bytes used at runtime for the queue.
  209. //config: Smaller values will create more undo objects and reduce the amount
  210. //config: of typed or backspaced characters that are grouped into one undo
  211. //config: operation; larger values increase the potential size of each undo
  212. //config: and will generally malloc() larger objects and less frequently.
  213. //config: Unless you want more (or less) frequent "undo points" while typing,
  214. //config: you should probably leave this unchanged.
  215. #ifdef VI_ENABLE_UNDO_QUEUE
  216. #define ENABLE_FEATURE_VI_UNDO_QUEUE 1
  217. #define IF_FEATURE_VI_UNDO_QUEUE(...) __VA_ARGS__
  218. #define CONFIG_FEATURE_VI_UNDO_QUEUE_MAX VI_UNDO_QUEUE_MAX
  219. #else
  220. #define ENABLE_FEATURE_VI_UNDO_QUEUE 0
  221. #define IF_FEATURE_VI_UNDO_QUEUE(...)
  222. #endif
  223. //config:config FEATURE_VI_VERBOSE_STATUS
  224. //config: bool "Enable verbose status reporting"
  225. //config: default y
  226. //config: depends on VI
  227. //config: help
  228. //config: Enable more verbose reporting of the results of yank, change,
  229. //config: delete, undo and substitution commands.
  230. #ifdef VI_ENABLE_VERBOSE_STATUS
  231. #define ENABLE_FEATURE_VI_VERBOSE_STATUS 1
  232. #else
  233. #define ENABLE_FEATURE_VI_VERBOSE_STATUS 0
  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. #ifndef F_OK
  312. #define F_OK 0 /* Tests whether the file exists. */
  313. #define R_OK 4 /* Tests whether the file can be accessed for reading. */
  314. #define W_OK 2 /* Tests whether the file can be accessed for writing. */
  315. #define X_OK 1 /* Tests whether the file can be accessed for execution. */
  316. #endif
  317. #if !defined(__GNUC__)
  318. #define ALIGN1
  319. #define barrier()
  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. unsigned char vi_mem_init(void);
  327. void vi_mem_release(void);
  328. void *vi_malloc(rt_size_t size);
  329. void *vi_realloc(void *rmem, rt_size_t newsize);
  330. void vi_free(void *ptr);
  331. void* vi_zalloc(size_t size);
  332. char *vi_strdup(const char *s);
  333. char *vi_strndup(const char *s, size_t n);
  334. int vi_putchar(int c);
  335. void vi_puts(const char *s);
  336. void vi_write(const void *buffer, uint32_t size);
  337. int64_t read_key(int fd, char *buffer, int timeout);
  338. char* xasprintf(const char *format, ...);
  339. #ifdef VI_ENABLE_COLON
  340. char* last_char_is(const char *s, int c);
  341. #endif
  342. #ifdef VI_ENABLE_SETOPTS
  343. char* skip_whitespace(const char *s);
  344. char* skip_non_whitespace(const char *s);
  345. #endif
  346. int index_in_strings(const char *strings, const char *key);
  347. ssize_t safe_read(int fd, void *buf, size_t count);
  348. int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout);
  349. ssize_t full_write(int fd, const void *buf, size_t len);
  350. ssize_t full_read(int fd, void *buf, size_t len);
  351. #ifdef RT_USING_POSIX_TERMIOS
  352. #include <termios.h>
  353. #define TERMIOS_CLEAR_ISIG (1 << 0)
  354. #define TERMIOS_RAW_CRNL_INPUT (1 << 1)
  355. #define TERMIOS_RAW_CRNL_OUTPUT (1 << 2)
  356. #define TERMIOS_RAW_CRNL (TERMIOS_RAW_CRNL_INPUT|TERMIOS_RAW_CRNL_OUTPUT)
  357. #define TERMIOS_RAW_INPUT (1 << 3)
  358. int tcsetattr_stdin_TCSANOW(const struct termios *tp);
  359. int get_terminal_width_height(int fd, unsigned *width, unsigned *height);
  360. int set_termios_to_raw(int fd, struct termios *oldterm, int flags);
  361. #endif
  362. //config: TODO for RT-Thread
  363. //config:config FEATURE_VI_USE_SIGNALS
  364. //config: bool "Catch signals"
  365. //config: default y
  366. //config: depends on VI
  367. //config: help
  368. //config: Selecting this option will make busybox vi signal aware. This will
  369. //config: make busybox vi support SIGWINCH to deal with Window Changes, catch
  370. //config: Ctrl-Z and Ctrl-C and alarms.
  371. #endif