vi_utils.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  3. */
  4. #include "vi_utils.h"
  5. int index_in_strings(const char *strings, const char *key)
  6. {
  7. int j, idx = 0;
  8. while (*strings) {
  9. /* Do we see "key\0" at current position in strings? */
  10. for (j = 0; *strings == key[j]; ++j) {
  11. if (*strings++ == '\0') {
  12. //bb_error_msg("found:'%s' i:%u", key, idx);
  13. return idx; /* yes */
  14. }
  15. }
  16. /* No. Move to the start of the next string. */
  17. while (*strings++ != '\0')
  18. continue;
  19. idx++;
  20. }
  21. return -1;
  22. }
  23. #ifdef VI_ENABLE_COLON
  24. /* Find out if the last character of a string matches the one given */
  25. char* last_char_is(const char *s, int c)
  26. {
  27. if (!s[0])
  28. return NULL;
  29. while (s[1])
  30. s++;
  31. return (*s == (char)c) ? (char *) s : NULL;
  32. }
  33. #endif
  34. #if defined(_MSC_VER) || defined(__CC_ARM)
  35. void *memrchr(const void* ptr, int ch, size_t pos)
  36. {
  37. char *end = (char *)ptr+pos-1;
  38. while (end != ptr)
  39. {
  40. if (*end == ch)
  41. return end;
  42. end--;
  43. }
  44. return (*end == ch)?(end):(NULL);
  45. }
  46. int isblank(int ch)
  47. {
  48. if (ch == ' ' || ch == '\t')
  49. return 1;
  50. return 0;
  51. }
  52. #endif
  53. #ifdef VI_ENABLE_SETOPTS
  54. char* skip_whitespace(const char *s)
  55. {
  56. /* In POSIX/C locale (the only locale we care about: do we REALLY want
  57. * to allow Unicode whitespace in, say, .conf files? nuts!)
  58. * isspace is only these chars: "\t\n\v\f\r" and space.
  59. * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
  60. * Use that.
  61. */
  62. while (*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9))
  63. s++;
  64. return (char *) s;
  65. }
  66. char* skip_non_whitespace(const char *s)
  67. {
  68. while (*s != '\0' && *s != ' ' && (unsigned char)(*s - 9) > (13 - 9))
  69. s++;
  70. return (char *) s;
  71. }
  72. #endif
  73. ssize_t safe_read(int fd, void *buf, size_t count)
  74. {
  75. ssize_t n;
  76. for (;;) {
  77. n = read(fd, buf, count);
  78. if (n >= 0 || errno != EINTR)
  79. break;
  80. /* Some callers set errno=0, are upset when they see EINTR.
  81. * Returning EINTR is wrong since we retry read(),
  82. * the "error" was transient.
  83. */
  84. errno = 0;
  85. /* repeat the read() */
  86. }
  87. return n;
  88. }
  89. /*
  90. * Read all of the supplied buffer from a file.
  91. * This does multiple reads as necessary.
  92. * Returns the amount read, or -1 on an error.
  93. * A short read is returned on an end of file.
  94. */
  95. ssize_t full_read(int fd, void *buf, size_t len)
  96. {
  97. ssize_t cc;
  98. ssize_t total;
  99. total = 0;
  100. while (len) {
  101. cc = safe_read(fd, buf, len);
  102. if (cc < 0) {
  103. if (total) {
  104. /* we already have some! */
  105. /* user can do another read to know the error code */
  106. return total;
  107. }
  108. return cc; /* read() returns -1 on failure. */
  109. }
  110. if (cc == 0)
  111. break;
  112. buf = ((char *)buf) + cc;
  113. total += cc;
  114. len -= cc;
  115. }
  116. return total;
  117. }
  118. ssize_t safe_write(int fd, const void *buf, size_t count)
  119. {
  120. ssize_t n;
  121. for (;;) {
  122. n = write(fd, buf, count);
  123. if (n >= 0 || errno != EINTR)
  124. break;
  125. /* Some callers set errno=0, are upset when they see EINTR.
  126. * Returning EINTR is wrong since we retry write(),
  127. * the "error" was transient.
  128. */
  129. errno = 0;
  130. /* repeat the write() */
  131. }
  132. return n;
  133. }
  134. /*
  135. * Write all of the supplied buffer out to a file.
  136. * This does multiple writes as necessary.
  137. * Returns the amount written, or -1 if error was seen
  138. * on the very first write.
  139. */
  140. ssize_t full_write(int fd, const void *buf, size_t len)
  141. {
  142. ssize_t cc;
  143. ssize_t total;
  144. total = 0;
  145. while (len) {
  146. cc = safe_write(fd, buf, len);
  147. if (cc < 0) {
  148. if (total) {
  149. /* we already wrote some! */
  150. /* user can do another write to know the error code */
  151. return total;
  152. }
  153. return cc; /* write() returns -1 on failure. */
  154. }
  155. total += cc;
  156. buf = ((const char *)buf) + cc;
  157. len -= cc;
  158. }
  159. return total;
  160. }
  161. /* Wrapper which restarts poll on EINTR or ENOMEM.
  162. * On other errors does perror("poll") and returns.
  163. * Warning! May take longer than timeout_ms to return! */
  164. int safe_poll(struct pollfd *ufds, nfds_t nfds, int timeout)
  165. {
  166. while (1) {
  167. int n = poll(ufds, nfds, timeout);
  168. if (n >= 0)
  169. return n;
  170. /* Make sure we inch towards completion */
  171. if (timeout > 0)
  172. timeout--;
  173. /* E.g. strace causes poll to return this */
  174. if (errno == EINTR)
  175. continue;
  176. /* Kernel is very low on memory. Retry. */
  177. /* I doubt many callers would handle this correctly! */
  178. if (errno == ENOMEM)
  179. continue;
  180. bb_simple_perror_msg("poll");
  181. return n;
  182. }
  183. }
  184. static mem_sandbox_t vi_sandbox = RT_NULL;
  185. unsigned char vi_mem_init(void)
  186. {
  187. vi_sandbox = mem_sandbox_create(1024 *20); /* sandbox size is 20KB */
  188. if(vi_sandbox == RT_NULL)
  189. {
  190. return 0;
  191. }
  192. else
  193. {
  194. return 1;
  195. }
  196. }
  197. void vi_mem_release(void)
  198. {
  199. mem_sandbox_delete(vi_sandbox);
  200. }
  201. void *xmalloc(rt_size_t size)
  202. {
  203. return mem_sandbox_malloc(vi_sandbox, size);
  204. }
  205. void *xrealloc(void *rmem, rt_size_t newsize)
  206. {
  207. return mem_sandbox_realloc(vi_sandbox, rmem, newsize);
  208. }
  209. void xfree(void *ptr)
  210. {
  211. mem_sandbox_free(vi_sandbox, ptr);
  212. }
  213. void* xzalloc(size_t size)
  214. {
  215. void *ptr = xmalloc(size);
  216. rt_memset(ptr, 0, size);
  217. return ptr;
  218. }
  219. char *xstrdup(const char *s)
  220. {
  221. return mem_sandbox_strdup(vi_sandbox, s);
  222. }
  223. char *xstrndup(const char *s, size_t n)
  224. {
  225. return mem_sandbox_strndup(vi_sandbox, s, n);
  226. }
  227. void bb_show_usage(void)
  228. {
  229. printf("Usage: vi [FILE]\n");
  230. }
  231. int64_t read_key(int fd, char *buffer, int timeout)
  232. {
  233. struct pollfd pfd;
  234. const char *seq;
  235. int n;
  236. /* Known escape sequences for cursor and function keys.
  237. * See "Xterm Control Sequences"
  238. * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  239. */
  240. static const char esccmds[] ALIGN1 = {
  241. 'O','A' |0x80, (char) KEYCODE_UP ,
  242. 'O','B' |0x80, (char) KEYCODE_DOWN ,
  243. 'O','C' |0x80, (char) KEYCODE_RIGHT ,
  244. 'O','D' |0x80, (char) KEYCODE_LEFT ,
  245. 'O','H' |0x80, (char) KEYCODE_HOME ,
  246. 'O','F' |0x80, (char) KEYCODE_END ,
  247. #if 0
  248. 'O','P' |0x80, (char) KEYCODE_FUN1 ,
  249. /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
  250. /* ESC [ O 1 ; 2 P - Shift-F1 */
  251. /* ESC [ O 1 ; 3 P - Alt-F1 */
  252. /* ESC [ O 1 ; 4 P - Alt-Shift-F1 */
  253. /* ESC [ O 1 ; 5 P - Ctrl-F1 */
  254. /* ESC [ O 1 ; 6 P - Ctrl-Shift-F1 */
  255. 'O','Q' |0x80, (char) KEYCODE_FUN2 ,
  256. 'O','R' |0x80, (char) KEYCODE_FUN3 ,
  257. 'O','S' |0x80, (char) KEYCODE_FUN4 ,
  258. #endif
  259. '[','A' |0x80, (char) KEYCODE_UP ,
  260. '[','B' |0x80, (char) KEYCODE_DOWN ,
  261. '[','C' |0x80, (char) KEYCODE_RIGHT ,
  262. '[','D' |0x80, (char) KEYCODE_LEFT ,
  263. /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
  264. /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below */
  265. /* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */
  266. /* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */
  267. /* ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow> */
  268. /* ESC [ 1 ; 7 x, where x = A/B/C/D: Ctrl-Alt-<arrow> */
  269. /* ESC [ 1 ; 8 x, where x = A/B/C/D: Ctrl-Alt-Shift-<arrow> */
  270. '[','H' |0x80, (char) KEYCODE_HOME , /* xterm */
  271. '[','F' |0x80, (char) KEYCODE_END , /* xterm */
  272. /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */
  273. /* '[','Z' |0x80, (char) KEYCODE_SHIFT_TAB, */
  274. '[','1','~' |0x80, (char) KEYCODE_HOME , /* vt100? linux vt? or what? */
  275. '[','2','~' |0x80, (char) KEYCODE_INSERT ,
  276. /* ESC [ 2 ; 3 ~ - Alt-Insert */
  277. '[','3','~' |0x80, (char) KEYCODE_DELETE ,
  278. /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
  279. /* ESC [ 3 ; 3 ~ - Alt-Delete */
  280. /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
  281. '[','4','~' |0x80, (char) KEYCODE_END , /* vt100? linux vt? or what? */
  282. '[','5','~' |0x80, (char) KEYCODE_PAGEUP ,
  283. /* ESC [ 5 ; 3 ~ - Alt-PgUp */
  284. /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
  285. /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
  286. '[','6','~' |0x80, (char) KEYCODE_PAGEDOWN,
  287. '[','7','~' |0x80, (char) KEYCODE_HOME , /* vt100? linux vt? or what? */
  288. '[','8','~' |0x80, (char) KEYCODE_END , /* vt100? linux vt? or what? */
  289. #if 0
  290. '[','1','1','~'|0x80, (char) KEYCODE_FUN1 , /* old xterm, deprecated by ESC O P */
  291. '[','1','2','~'|0x80, (char) KEYCODE_FUN2 , /* old xterm... */
  292. '[','1','3','~'|0x80, (char) KEYCODE_FUN3 , /* old xterm... */
  293. '[','1','4','~'|0x80, (char) KEYCODE_FUN4 , /* old xterm... */
  294. '[','1','5','~'|0x80, (char) KEYCODE_FUN5 ,
  295. /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
  296. '[','1','7','~'|0x80, (char) KEYCODE_FUN6 ,
  297. '[','1','8','~'|0x80, (char) KEYCODE_FUN7 ,
  298. '[','1','9','~'|0x80, (char) KEYCODE_FUN8 ,
  299. '[','2','0','~'|0x80, (char) KEYCODE_FUN9 ,
  300. '[','2','1','~'|0x80, (char) KEYCODE_FUN10 ,
  301. '[','2','3','~'|0x80, (char) KEYCODE_FUN11 ,
  302. '[','2','4','~'|0x80, (char) KEYCODE_FUN12 ,
  303. /* ESC [ 2 4 ; 2 ~ - Shift-F12 */
  304. /* ESC [ 2 4 ; 3 ~ - Alt-F12 */
  305. /* ESC [ 2 4 ; 4 ~ - Alt-Shift-F12 */
  306. /* ESC [ 2 4 ; 5 ~ - Ctrl-F12 */
  307. /* ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12 */
  308. #endif
  309. /* '[','1',';','5','A' |0x80, (char) KEYCODE_CTRL_UP , - unused */
  310. /* '[','1',';','5','B' |0x80, (char) KEYCODE_CTRL_DOWN , - unused */
  311. '[','1',';','5','C' |0x80, (char) KEYCODE_CTRL_RIGHT,
  312. '[','1',';','5','D' |0x80, (char) KEYCODE_CTRL_LEFT ,
  313. /* '[','1',';','3','A' |0x80, (char) KEYCODE_ALT_UP , - unused */
  314. /* '[','1',';','3','B' |0x80, (char) KEYCODE_ALT_DOWN , - unused */
  315. '[','1',';','3','C' |0x80, (char) KEYCODE_ALT_RIGHT,
  316. '[','1',';','3','D' |0x80, (char) KEYCODE_ALT_LEFT ,
  317. /* '[','3',';','3','~' |0x80, (char) KEYCODE_ALT_DELETE, - unused */
  318. 0
  319. };
  320. pfd.fd = fd;
  321. pfd.events = POLLIN;
  322. buffer++; /* saved chars counter is in buffer[-1] now */
  323. start_over:
  324. errno = 0;
  325. n = (unsigned char)buffer[-1];
  326. if (n == 0) {
  327. /* If no data, wait for input.
  328. * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful
  329. * if fd can be in non-blocking mode.
  330. */
  331. if (timeout >= -1) {
  332. if (safe_poll(&pfd, 1, timeout) == 0) {
  333. /* Timed out */
  334. errno = EAGAIN;
  335. return -1;
  336. }
  337. }
  338. /* It is tempting to read more than one byte here,
  339. * but it breaks pasting. Example: at shell prompt,
  340. * user presses "c","a","t" and then pastes "\nline\n".
  341. * When we were reading 3 bytes here, we were eating
  342. * "li" too, and cat was getting wrong input.
  343. */
  344. n = safe_read(fd, buffer, 1);
  345. if (n <= 0)
  346. return -1;
  347. }
  348. {
  349. unsigned char c = buffer[0];
  350. n--;
  351. if (n)
  352. memmove(buffer, buffer + 1, n);
  353. /* Only ESC starts ESC sequences */
  354. if (c != 27) {
  355. buffer[-1] = n;
  356. return c;
  357. }
  358. }
  359. /* Loop through known ESC sequences */
  360. seq = esccmds;
  361. while (*seq != '\0') {
  362. /* n - position in sequence we did not read yet */
  363. int i = 0; /* position in sequence to compare */
  364. /* Loop through chars in this sequence */
  365. while (1) {
  366. /* So far escape sequence matched up to [i-1] */
  367. if (n <= i) {
  368. int read_num;
  369. /* Need more chars, read another one if it wouldn't block.
  370. * Note that escape sequences come in as a unit,
  371. * so if we block for long it's not really an escape sequence.
  372. * Timeout is needed to reconnect escape sequences
  373. * split up by transmission over a serial console. */
  374. if (safe_poll(&pfd, 1, 50) == 0) {
  375. /* No more data!
  376. * Array is sorted from shortest to longest,
  377. * we can't match anything later in array -
  378. * anything later is longer than this seq.
  379. * Break out of both loops. */
  380. goto got_all;
  381. }
  382. errno = 0;
  383. read_num = safe_read(fd, buffer + n, 1);
  384. if (read_num <= 0) {
  385. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  386. * in fact, there is no data. */
  387. if (errno != EAGAIN) {
  388. /* otherwise: it's EOF/error */
  389. buffer[-1] = 0;
  390. return -1;
  391. }
  392. goto got_all;
  393. }
  394. n++;
  395. }
  396. if (buffer[i] != (seq[i] & 0x7f)) {
  397. /* This seq doesn't match, go to next */
  398. seq += i;
  399. /* Forward to last char */
  400. while (!(*seq & 0x80))
  401. seq++;
  402. /* Skip it and the keycode which follows */
  403. seq += 2;
  404. break;
  405. }
  406. if (seq[i] & 0x80) {
  407. /* Entire seq matched */
  408. n = 0;
  409. /* n -= i; memmove(...);
  410. * would be more correct,
  411. * but we never read ahead that much,
  412. * and n == i here. */
  413. buffer[-1] = 0;
  414. return (signed char)seq[i+1];
  415. }
  416. i++;
  417. }
  418. }
  419. /* We did not find matching sequence.
  420. * We possibly read and stored more input in buffer[] by now.
  421. * n = bytes read. Try to read more until we time out.
  422. */
  423. while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
  424. int read_num;
  425. if (safe_poll(&pfd, 1, 50) == 0) {
  426. /* No more data! */
  427. break;
  428. }
  429. errno = 0;
  430. read_num = safe_read(fd, buffer + n, 1);
  431. if (read_num <= 0) {
  432. /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
  433. * in fact, there is no data. */
  434. if (errno != EAGAIN) {
  435. /* otherwise: it's EOF/error */
  436. buffer[-1] = 0;
  437. return -1;
  438. }
  439. break;
  440. }
  441. n++;
  442. /* Try to decipher "ESC [ NNN ; NNN R" sequence */
  443. if ((ENABLE_FEATURE_VI_ASK_TERMINAL)
  444. && n >= 5
  445. && buffer[0] == '['
  446. && buffer[n-1] == 'R'
  447. && isdigit((unsigned char)buffer[1])
  448. ) {
  449. char *end;
  450. unsigned long row, col;
  451. row = strtoul(buffer + 1, &end, 10);
  452. if (*end != ';' || !isdigit((unsigned char)end[1]))
  453. continue;
  454. col = strtoul(end + 1, &end, 10);
  455. if (*end != 'R')
  456. continue;
  457. if (row < 1 || col < 1 || (row | col) > 0x7fff)
  458. continue;
  459. buffer[-1] = 0;
  460. /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
  461. row |= ((unsigned)(-1) << 15);
  462. col |= (row << 16);
  463. /* Return it in high-order word */
  464. return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
  465. }
  466. }
  467. got_all:
  468. if (n <= 1) {
  469. /* Alt-x is usually returned as ESC x.
  470. * Report ESC, x is remembered for the next call.
  471. */
  472. buffer[-1] = n;
  473. return 27;
  474. }
  475. /* We were doing "buffer[-1] = n; return c;" here, but this results
  476. * in unknown key sequences being interpreted as ESC + garbage.
  477. * This was not useful. Pretend there was no key pressed,
  478. * go and wait for a new keypress:
  479. */
  480. buffer[-1] = 0;
  481. goto start_over;
  482. }
  483. #ifdef RT_USING_POSIX_TERMIOS
  484. static int wh_helper(int value, int def_val, const char *env_name, int *err)
  485. {
  486. /* Envvars override even if "value" from ioctl is valid (>0).
  487. * Rationale: it's impossible to guess what user wants.
  488. * For example: "man CMD | ...": should "man" format output
  489. * to stdout's width? stdin's width? /dev/tty's width? 80 chars?
  490. * We _cant_ know it. If "..." saves text for e.g. email,
  491. * then it's probably 80 chars.
  492. * If "..." is, say, "grep -v DISCARD | $PAGER", then user
  493. * would prefer his tty's width to be used!
  494. *
  495. * Since we don't know, at least allow user to do this:
  496. * "COLUMNS=80 man CMD | ..."
  497. */
  498. char *s = getenv(env_name);
  499. if (s) {
  500. value = atoi(s);
  501. /* If LINES/COLUMNS are set, pretend that there is
  502. * no error getting w/h, this prevents some ugly
  503. * cursor tricks by our callers */
  504. *err = 0;
  505. }
  506. if (value <= 1 || value >= 30000)
  507. value = def_val;
  508. return value;
  509. }
  510. int get_terminal_width_height(int fd, unsigned *width, unsigned *height)
  511. {
  512. struct winsize win;
  513. int err;
  514. int close_me = -1;
  515. if (fd == -1) {
  516. if (isatty(STDOUT_FILENO))
  517. fd = STDOUT_FILENO;
  518. else
  519. if (isatty(STDERR_FILENO))
  520. fd = STDERR_FILENO;
  521. else
  522. if (isatty(STDIN_FILENO))
  523. fd = STDIN_FILENO;
  524. else
  525. close_me = fd = open("/dev/tty", O_RDONLY);
  526. }
  527. win.ws_row = 0;
  528. win.ws_col = 0;
  529. /* I've seen ioctl returning 0, but row/col is (still?) 0.
  530. * We treat that as an error too. */
  531. err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0;
  532. if (height)
  533. *height = wh_helper(win.ws_row, 24, "LINES", &err);
  534. if (width)
  535. *width = wh_helper(win.ws_col, 80, "COLUMNS", &err);
  536. if (close_me >= 0)
  537. close(close_me);
  538. return err;
  539. }
  540. int tcsetattr_stdin_TCSANOW(const struct termios *tp)
  541. {
  542. return tcsetattr(STDIN_FILENO, TCSANOW, tp);
  543. }
  544. static int get_termios_and_make_raw(int fd, struct termios *newterm, struct termios *oldterm, int flags)
  545. {
  546. //TODO: slattach, shell read might be adapted to use this too: grep for "tcsetattr", "[VTIME] = 0"
  547. int r;
  548. memset(oldterm, 0, sizeof(*oldterm)); /* paranoia */
  549. r = tcgetattr(fd, oldterm);
  550. *newterm = *oldterm;
  551. /* Turn off buffered input (ICANON)
  552. * Turn off echoing (ECHO)
  553. * and separate echoing of newline (ECHONL, normally off anyway)
  554. */
  555. newterm->c_lflag &= ~(ICANON | ECHO | ECHONL);
  556. if (flags & TERMIOS_CLEAR_ISIG) {
  557. /* dont recognize INT/QUIT/SUSP chars */
  558. newterm->c_lflag &= ~ISIG;
  559. }
  560. /* reads will block only if < 1 char is available */
  561. newterm->c_cc[VMIN] = 1;
  562. /* no timeout (reads block forever) */
  563. newterm->c_cc[VTIME] = 0;
  564. /* IXON, IXOFF, and IXANY:
  565. * IXOFF=1: sw flow control is enabled on input queue:
  566. * tty transmits a STOP char when input queue is close to full
  567. * and transmits a START char when input queue is nearly empty.
  568. * IXON=1: sw flow control is enabled on output queue:
  569. * tty will stop sending if STOP char is received,
  570. * and resume sending if START is received, or if any char
  571. * is received and IXANY=1.
  572. */
  573. if (flags & TERMIOS_RAW_CRNL_INPUT) {
  574. /* IXON=0: XON/XOFF chars are treated as normal chars (why we do this?) */
  575. /* dont convert CR to NL on input */
  576. newterm->c_iflag &= ~(IXON | ICRNL);
  577. }
  578. if (flags & TERMIOS_RAW_CRNL_OUTPUT) {
  579. /* dont convert NL to CR+NL on output */
  580. newterm->c_oflag &= ~(ONLCR);
  581. /* Maybe clear more c_oflag bits? Usually, only OPOST and ONLCR are set.
  582. * OPOST Enable output processing (reqd for OLCUC and *NL* bits to work)
  583. * OLCUC Map lowercase characters to uppercase on output.
  584. * OCRNL Map CR to NL on output.
  585. * ONOCR Don't output CR at column 0.
  586. * ONLRET Don't output CR.
  587. */
  588. }
  589. if (flags & TERMIOS_RAW_INPUT) {
  590. #ifndef IMAXBEL
  591. # define IMAXBEL 0
  592. #endif
  593. #ifndef IUCLC
  594. # define IUCLC 0
  595. #endif
  596. #ifndef IXANY
  597. # define IXANY 0
  598. #endif
  599. /* IXOFF=0: disable sending XON/XOFF if input buf is full
  600. * IXON=0: input XON/XOFF chars are not special
  601. * BRKINT=0: dont send SIGINT on break
  602. * IMAXBEL=0: dont echo BEL on input line too long
  603. * INLCR,ICRNL,IUCLC: dont convert anything on input
  604. */
  605. newterm->c_iflag &= ~(IXOFF|IXON|IXANY|BRKINT|INLCR|ICRNL|IUCLC|IMAXBEL);
  606. }
  607. return r;
  608. }
  609. int set_termios_to_raw(int fd, struct termios *oldterm, int flags)
  610. {
  611. struct termios newterm;
  612. get_termios_and_make_raw(fd, &newterm, oldterm, flags);
  613. return tcsetattr(fd, TCSANOW, &newterm);
  614. }
  615. #endif /* RT_USING_POSIX_TERMIOS */
  616. #if defined(VI_ENABLE_SEARCH) && !defined(__GNUC__)
  617. char* strchrnul(const char *s, int c)
  618. {
  619. while (*s != '\0' && *s != c)
  620. s++;
  621. return (char*)s;
  622. }
  623. #endif