tty_ttydisc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-13 Shell init ver.
  9. */
  10. #include "../bsd_porting.h"
  11. #include "../tty_config.h"
  12. #include "../tty_internal.h"
  13. #include "../terminal.h"
  14. #include <rtdef.h>
  15. /*-
  16. * SPDX-License-Identifier: BSD-2-Clause
  17. *
  18. * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
  19. * All rights reserved.
  20. *
  21. * Portions of this software were developed under sponsorship from Snow
  22. * B.V., the Netherlands.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. * 1. Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions and the following disclaimer.
  29. * 2. Redistributions in binary form must reproduce the above copyright
  30. * notice, this list of conditions and the following disclaimer in the
  31. * documentation and/or other materials provided with the distribution.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  34. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  35. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  37. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  38. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  39. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  43. * SUCH DAMAGE.
  44. */
  45. /*
  46. * Standard TTYDISC `termios' line discipline.
  47. */
  48. /* Statistics. */
  49. static rt_atomic_t tty_nin = 0;
  50. #ifdef USING_BSD_SYSCTL
  51. SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD, &tty_nin, 0,
  52. "Total amount of bytes received");
  53. #endif
  54. static rt_atomic_t tty_nout = 0;
  55. #ifdef USING_BSD_SYSCTL
  56. SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, &tty_nout, 0,
  57. "Total amount of bytes transmitted");
  58. #endif
  59. /* termios comparison macro's. */
  60. #define CMP_CC(v, c) \
  61. (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && tp->t_termios.c_cc[v] == (c))
  62. #define CMP_FLAG(field, opt) (tp->t_termios.c_##field##flag & (opt))
  63. /* Characters that cannot be modified through c_cc. */
  64. #define CTAB '\t'
  65. #define CNL '\n'
  66. #define CCR '\r'
  67. /* Character is a control character. */
  68. #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20)
  69. /* Control character should be processed on echo. */
  70. #define CTL_ECHO(c, q) \
  71. (!(q) && ((c) == CERASE2 || (c) == CTAB || (c) == CNL || (c) == CCR))
  72. /* Control character should be printed using ^X notation. */
  73. #define CTL_PRINT(c, q) \
  74. ((c) == 0x7f || \
  75. ((unsigned char)(c) < 0x20 && ((q) || ((c) != CTAB && (c) != CNL))))
  76. /* Character is whitespace. */
  77. #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB)
  78. /* Character is alphanumeric. */
  79. #define CTL_ALNUM(c) \
  80. (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'z') || \
  81. ((c) >= 'A' && (c) <= 'Z'))
  82. /* Character is UTF8-encoded. */
  83. #define CTL_UTF8(c) (!!((c)&0x80))
  84. /* Character is a UTF8 continuation byte. */
  85. #define CTL_UTF8_CONT(c) (((c)&0xc0) == 0x80)
  86. #define TTY_STACKBUF 256
  87. #define UTF8_STACKBUF 4
  88. void ttydisc_open(struct lwp_tty *tp)
  89. {
  90. ttydisc_optimize(tp);
  91. }
  92. void ttydisc_close(struct lwp_tty *tp)
  93. {
  94. /* Clean up our flags when leaving the discipline. */
  95. tp->t_flags &= ~(TF_STOPPED | TF_HIWAT | TF_ZOMBIE);
  96. tp->t_termios.c_lflag &= ~FLUSHO;
  97. /*
  98. * POSIX states that we must drain output and flush input on
  99. * last close. Draining has already been done if possible.
  100. */
  101. tty_flush(tp, FREAD | FWRITE);
  102. #ifdef USING_BSD_HOOK
  103. if (ttyhook_hashook(tp, close))
  104. ttyhook_close(tp);
  105. #endif
  106. }
  107. static int ttydisc_read_canonical(struct lwp_tty *tp, struct uio *uio,
  108. int ioflag)
  109. {
  110. char breakc[4] = {CNL}; /* enough to hold \n, VEOF and VEOL. */
  111. int error;
  112. size_t clen, flen = 0, n = 1;
  113. unsigned char lastc = _POSIX_VDISABLE;
  114. #define BREAK_ADD(c) \
  115. do \
  116. { \
  117. if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
  118. breakc[n++] = tp->t_termios.c_cc[c]; \
  119. } while (0)
  120. /* Determine which characters we should trigger on. */
  121. BREAK_ADD(VEOF);
  122. BREAK_ADD(VEOL);
  123. #undef BREAK_ADD
  124. breakc[n] = '\0';
  125. do
  126. {
  127. error = tty_wait_background(tp, curthread, SIGTTIN);
  128. if (error)
  129. return error;
  130. /*
  131. * Quite a tricky case: unlike the old TTY
  132. * implementation, this implementation copies data back
  133. * to userspace in large chunks. Unfortunately, we can't
  134. * calculate the line length on beforehand if it crosses
  135. * ttyinq_block boundaries, because multiple reads could
  136. * then make this code read beyond the newline.
  137. *
  138. * This is why we limit the read to:
  139. * - The size the user has requested
  140. * - The blocksize (done in tty_inq.c)
  141. * - The amount of bytes until the newline
  142. *
  143. * This causes the line length to be recalculated after
  144. * each block has been copied to userspace. This will
  145. * cause the TTY layer to return data in chunks using
  146. * the blocksize (except the first and last blocks).
  147. */
  148. clen =
  149. ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid, (char *)&lastc);
  150. /* No more data. */
  151. if (clen == 0)
  152. {
  153. if (tp->t_flags & TF_ZOMBIE)
  154. return 0;
  155. else if (ioflag & IO_NDELAY)
  156. return -EWOULDBLOCK;
  157. error = tty_wait(tp, &tp->t_inwait);
  158. if (error)
  159. return error;
  160. continue;
  161. }
  162. /* Don't send the EOF char back to userspace. */
  163. if (CMP_CC(VEOF, lastc))
  164. flen = 1;
  165. MPASS(flen <= clen);
  166. /* Read and throw away the EOF character. */
  167. error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
  168. if (error)
  169. return error;
  170. /** Notes: proceed when read buf frees and not reaching breaks */
  171. } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
  172. return 0;
  173. }
  174. static int ttydisc_read_raw_no_timer(struct lwp_tty *tp, struct uio *uio,
  175. int ioflag)
  176. {
  177. size_t vmin = tp->t_termios.c_cc[VMIN];
  178. ssize_t oresid = uio->uio_resid;
  179. int error;
  180. MPASS(tp->t_termios.c_cc[VTIME] == 0);
  181. /*
  182. * This routine implements the easy cases of read()s while in
  183. * non-canonical mode, namely case B and D, where we don't have
  184. * any timers at all.
  185. */
  186. for (;;)
  187. {
  188. error = tty_wait_background(tp, curthread, SIGTTIN);
  189. if (error)
  190. return error;
  191. error = ttyinq_read_uio(&tp->t_inq, tp, uio, uio->uio_resid, 0);
  192. if (error)
  193. return error;
  194. if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
  195. return 0;
  196. /* We have to wait for more. */
  197. if (tp->t_flags & TF_ZOMBIE)
  198. return 0;
  199. else if (ioflag & IO_NDELAY)
  200. return -EWOULDBLOCK;
  201. error = tty_wait(tp, &tp->t_inwait);
  202. if (error)
  203. return error;
  204. }
  205. }
  206. static int ttydisc_read_raw_read_timer(struct lwp_tty *tp, struct uio *uio,
  207. int ioflag, int oresid)
  208. {
  209. size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
  210. unsigned int vtime = tp->t_termios.c_cc[VTIME];
  211. struct timeval end, now, left;
  212. int error, hz;
  213. MPASS(tp->t_termios.c_cc[VTIME] != 0);
  214. /* Determine when the read should be expired. */
  215. end.tv_sec = vtime / 10;
  216. end.tv_usec = (vtime % 10) * 100000;
  217. getmicrotime(&now);
  218. timevaladd(&end, &now);
  219. for (;;)
  220. {
  221. error = tty_wait_background(tp, curthread, SIGTTIN);
  222. if (error)
  223. return error;
  224. error = ttyinq_read_uio(&tp->t_inq, tp, uio, uio->uio_resid, 0);
  225. if (error)
  226. return error;
  227. if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
  228. return 0;
  229. /* Calculate how long we should wait. */
  230. getmicrotime(&now);
  231. if (timevalcmp(&now, &end, >))
  232. return 0;
  233. left = end;
  234. timevalsub(&left, &now);
  235. hz = tvtohz(&left);
  236. /*
  237. * We have to wait for more. If the timer expires, we
  238. * should return a 0-byte read.
  239. */
  240. if (tp->t_flags & TF_ZOMBIE)
  241. return 0;
  242. else if (ioflag & IO_NDELAY)
  243. return -EWOULDBLOCK;
  244. error = tty_timedwait(tp, &tp->t_inwait, hz);
  245. if (error)
  246. return (error == -EWOULDBLOCK ? 0 : error);
  247. }
  248. return 0;
  249. }
  250. static int ttydisc_read_raw_interbyte_timer(struct lwp_tty *tp, struct uio *uio,
  251. int ioflag)
  252. {
  253. size_t vmin = tp->t_termios.c_cc[VMIN];
  254. ssize_t oresid = uio->uio_resid;
  255. int error;
  256. MPASS(tp->t_termios.c_cc[VMIN] != 0);
  257. MPASS(tp->t_termios.c_cc[VTIME] != 0);
  258. /*
  259. * When using the interbyte timer, the timer should be started
  260. * after the first byte has been received. We just call into the
  261. * generic read timer code after we've received the first byte.
  262. */
  263. for (;;)
  264. {
  265. error = tty_wait_background(tp, curthread, SIGTTIN);
  266. if (error)
  267. return error;
  268. error = ttyinq_read_uio(&tp->t_inq, tp, uio, uio->uio_resid, 0);
  269. if (error)
  270. return error;
  271. if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
  272. return 0;
  273. /*
  274. * Not enough data, but we did receive some, which means
  275. * we'll now start using the interbyte timer.
  276. */
  277. if (oresid != uio->uio_resid)
  278. break;
  279. /* We have to wait for more. */
  280. if (tp->t_flags & TF_ZOMBIE)
  281. return 0;
  282. else if (ioflag & IO_NDELAY)
  283. return -EWOULDBLOCK;
  284. error = tty_wait(tp, &tp->t_inwait);
  285. if (error)
  286. return error;
  287. }
  288. return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
  289. }
  290. int ttydisc_read(struct lwp_tty *tp, struct uio *uio, int ioflag)
  291. {
  292. int error;
  293. tty_assert_locked(tp);
  294. if (uio->uio_resid == 0)
  295. return 0;
  296. if (CMP_FLAG(l, ICANON))
  297. error = ttydisc_read_canonical(tp, uio, ioflag);
  298. else if (tp->t_termios.c_cc[VTIME] == 0)
  299. error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
  300. else if (tp->t_termios.c_cc[VMIN] == 0)
  301. error = ttydisc_read_raw_read_timer(tp, uio, ioflag, uio->uio_resid);
  302. else
  303. error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
  304. if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
  305. ttyinq_bytescanonicalized(&tp->t_inq) == 0)
  306. {
  307. /* Unset the input watermark when we've got enough space. */
  308. tty_hiwat_in_unblock(tp);
  309. }
  310. return error;
  311. }
  312. /* return the offset to special character from string start */
  313. rt_inline unsigned int ttydisc_findchar(const char *obstart, unsigned int oblen)
  314. {
  315. const char *c = obstart;
  316. while (oblen--)
  317. {
  318. if (CTL_VALID(*c))
  319. break;
  320. c++;
  321. }
  322. return (c - obstart);
  323. }
  324. static int ttydisc_write_oproc(struct lwp_tty *tp, char c)
  325. {
  326. unsigned int scnt, error;
  327. MPASS(CMP_FLAG(o, OPOST));
  328. MPASS(CTL_VALID(c));
  329. #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
  330. switch (c)
  331. {
  332. case CEOF:
  333. /* End-of-text dropping. */
  334. if (CMP_FLAG(o, ONOEOT))
  335. return 0;
  336. return PRINT_NORMAL();
  337. case CERASE2:
  338. /* Handle backspace to fix tab expansion. */
  339. if (PRINT_NORMAL() != 0)
  340. return (-1);
  341. if (tp->t_column > 0)
  342. tp->t_column--;
  343. return 0;
  344. case CTAB:
  345. /* Tab expansion. */
  346. scnt = 8 - (tp->t_column & 7);
  347. if (CMP_FLAG(o, TAB3))
  348. {
  349. error = ttyoutq_write_nofrag(&tp->t_outq, " ", scnt);
  350. }
  351. else
  352. {
  353. error = PRINT_NORMAL();
  354. }
  355. if (error)
  356. return (-1);
  357. tp->t_column += scnt;
  358. MPASS((tp->t_column % 8) == 0);
  359. return 0;
  360. case CNL:
  361. /* Newline conversion. */
  362. if (CMP_FLAG(o, ONLCR))
  363. {
  364. /* Convert \n to \r\n. */
  365. error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
  366. }
  367. else
  368. {
  369. error = PRINT_NORMAL();
  370. }
  371. if (error)
  372. return (-1);
  373. if (CMP_FLAG(o, ONLCR | ONLRET))
  374. {
  375. tp->t_column = tp->t_writepos = 0;
  376. ttyinq_reprintpos_set(&tp->t_inq);
  377. }
  378. return 0;
  379. case CCR:
  380. /* Carriage return to newline conversion. */
  381. if (CMP_FLAG(o, OCRNL))
  382. c = CNL;
  383. /* Omit carriage returns on column 0. */
  384. if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
  385. return 0;
  386. if (PRINT_NORMAL() != 0)
  387. return (-1);
  388. tp->t_column = tp->t_writepos = 0;
  389. ttyinq_reprintpos_set(&tp->t_inq);
  390. return 0;
  391. }
  392. /*
  393. * Invisible control character. Print it, but don't
  394. * increase the column count.
  395. */
  396. return PRINT_NORMAL();
  397. #undef PRINT_NORMAL
  398. }
  399. /*
  400. * Just like the old TTY implementation, we need to copy data in chunks
  401. * into a temporary buffer. One of the reasons why we need to do this,
  402. * is because output processing (only TAB3 though) may allow the buffer
  403. * to grow eight times.
  404. */
  405. int ttydisc_write(struct lwp_tty *tp, struct uio *uio, int ioflag)
  406. {
  407. char ob[TTY_STACKBUF];
  408. char *obstart;
  409. int error = 0;
  410. unsigned int oblen = 0;
  411. tty_assert_locked(tp);
  412. if (tp->t_flags & TF_ZOMBIE)
  413. return -EIO;
  414. /*
  415. * We don't need to check whether the process is the foreground
  416. * process group or if we have a carrier. This is already done
  417. * in ttydev_write().
  418. */
  419. while (uio->uio_resid > 0)
  420. {
  421. unsigned int nlen;
  422. MPASS(oblen == 0);
  423. if (CMP_FLAG(l, FLUSHO))
  424. {
  425. uio->uio_offset += uio->uio_resid;
  426. uio->uio_resid = 0;
  427. return 0;
  428. }
  429. /* Step 1: read data. */
  430. obstart = ob;
  431. nlen = MIN(uio->uio_resid, sizeof ob);
  432. tty_unlock(tp);
  433. error = uiomove(ob, nlen, uio);
  434. tty_lock(tp);
  435. if (error != 0)
  436. break;
  437. oblen = nlen;
  438. if (tty_gone(tp))
  439. {
  440. error = -ENXIO;
  441. break;
  442. }
  443. MPASS(oblen > 0);
  444. /* Step 2: process data. */
  445. do
  446. {
  447. unsigned int plen, wlen;
  448. if (CMP_FLAG(l, FLUSHO))
  449. {
  450. uio->uio_offset += uio->uio_resid;
  451. uio->uio_resid = 0;
  452. return 0;
  453. }
  454. /* Search for special characters for post processing. */
  455. if (CMP_FLAG(o, OPOST))
  456. {
  457. plen = ttydisc_findchar(obstart, oblen);
  458. }
  459. else
  460. {
  461. plen = oblen;
  462. }
  463. if (plen == 0)
  464. {
  465. /*
  466. * We're going to process a character
  467. * that needs processing
  468. */
  469. if (ttydisc_write_oproc(tp, *obstart) == 0)
  470. {
  471. obstart++;
  472. oblen--;
  473. tp->t_writepos = tp->t_column;
  474. ttyinq_reprintpos_set(&tp->t_inq);
  475. continue;
  476. }
  477. }
  478. else
  479. {
  480. /* We're going to write regular data. */
  481. wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
  482. obstart += wlen;
  483. oblen -= wlen;
  484. tp->t_column += wlen;
  485. tp->t_writepos = tp->t_column;
  486. ttyinq_reprintpos_set(&tp->t_inq);
  487. if (wlen == plen)
  488. continue;
  489. }
  490. /* Watermark reached. Try to sleep. */
  491. tp->t_flags |= TF_HIWAT_OUT;
  492. if (ioflag & IO_NDELAY)
  493. {
  494. error = EWOULDBLOCK;
  495. goto done;
  496. }
  497. /*
  498. * The driver may write back the data
  499. * synchronously. Be sure to check the high
  500. * water mark before going to sleep.
  501. */
  502. ttydevsw_outwakeup(tp);
  503. if ((tp->t_flags & TF_HIWAT_OUT) == 0)
  504. continue;
  505. error = tty_wait(tp, &tp->t_outwait);
  506. if (error)
  507. goto done;
  508. if (tp->t_flags & TF_ZOMBIE)
  509. {
  510. error = EIO;
  511. goto done;
  512. }
  513. } while (oblen > 0);
  514. }
  515. done:
  516. if (!tty_gone(tp))
  517. ttydevsw_outwakeup(tp);
  518. /*
  519. * Add the amount of bytes that we didn't process back to the
  520. * uio counters. We need to do this to make sure write() doesn't
  521. * count the bytes we didn't store in the queue.
  522. */
  523. uio->uio_resid += oblen;
  524. return error;
  525. }
  526. /* set optimize flags if possible */
  527. void ttydisc_optimize(struct lwp_tty *tp)
  528. {
  529. tty_assert_locked(tp);
  530. if (ttyhook_hashook(tp, rint_bypass))
  531. {
  532. tp->t_flags |= TF_BYPASS;
  533. }
  534. else if (ttyhook_hashook(tp, rint))
  535. {
  536. tp->t_flags &= ~TF_BYPASS;
  537. }
  538. else if (!CMP_FLAG(i, ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON) &&
  539. (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
  540. (!CMP_FLAG(i, PARMRK) ||
  541. CMP_FLAG(i, IGNPAR | IGNBRK) == (IGNPAR | IGNBRK)) &&
  542. !CMP_FLAG(l, ECHO | ICANON | IEXTEN | ISIG | PENDIN))
  543. {
  544. tp->t_flags |= TF_BYPASS;
  545. }
  546. else
  547. {
  548. tp->t_flags &= ~TF_BYPASS;
  549. }
  550. }
  551. void ttydisc_modem(struct lwp_tty *tp, int open)
  552. {
  553. tty_assert_locked(tp);
  554. if (open)
  555. cv_broadcast(&tp->t_dcdwait);
  556. /*
  557. * Ignore modem status lines when CLOCAL is turned on, but don't
  558. * enter the zombie state when the TTY isn't opened, because
  559. * that would cause the TTY to be in zombie state after being
  560. * opened.
  561. */
  562. if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
  563. return;
  564. if (open == 0)
  565. {
  566. /*
  567. * Lost carrier.
  568. */
  569. tp->t_flags |= TF_ZOMBIE;
  570. lwp_tty_signal_sessleader(tp, SIGHUP);
  571. tty_flush(tp, FREAD | FWRITE);
  572. }
  573. else
  574. {
  575. /*
  576. * Carrier is back again.
  577. */
  578. /* XXX: what should we do here? */
  579. }
  580. }
  581. static int ttydisc_echo_force(struct lwp_tty *tp, char c, int quote)
  582. {
  583. if (CMP_FLAG(l, FLUSHO))
  584. return 0;
  585. if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote))
  586. {
  587. /*
  588. * Only perform postprocessing when OPOST is turned on
  589. * and the character is an unquoted BS/TB/NL/CR.
  590. */
  591. return ttydisc_write_oproc(tp, c);
  592. }
  593. else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote))
  594. {
  595. /*
  596. * Only use ^X notation when ECHOCTL is turned on and
  597. * we've got an quoted control character.
  598. *
  599. * Print backspaces when echoing an end-of-file.
  600. */
  601. char ob[4] = "^?\b\b";
  602. /* Print ^X notation. */
  603. if (c != 0x7f)
  604. ob[1] = c + 'A' - 1;
  605. if (!quote && CMP_CC(VEOF, c))
  606. {
  607. return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
  608. }
  609. else
  610. {
  611. tp->t_column += 2;
  612. return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
  613. }
  614. }
  615. else
  616. {
  617. /* Can just be printed. */
  618. tp->t_column++;
  619. return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
  620. }
  621. }
  622. static int ttydisc_echo(struct lwp_tty *tp, char c, int quote)
  623. {
  624. /*
  625. * Only echo characters when ECHO is turned on, or ECHONL when
  626. * the character is an unquoted newline.
  627. */
  628. if (!CMP_FLAG(l, ECHO) && (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
  629. return 0;
  630. return ttydisc_echo_force(tp, c, quote);
  631. }
  632. static void ttydisc_reprint_char(void *d, char c, int quote)
  633. {
  634. struct lwp_tty *tp = d;
  635. ttydisc_echo(tp, c, quote);
  636. }
  637. static void ttydisc_reprint(struct lwp_tty *tp)
  638. {
  639. cc_t c;
  640. /* Print ^R\n, followed by the line. */
  641. c = tp->t_termios.c_cc[VREPRINT];
  642. if (c != _POSIX_VDISABLE)
  643. ttydisc_echo(tp, c, 0);
  644. ttydisc_echo(tp, CNL, 0);
  645. ttyinq_reprintpos_reset(&tp->t_inq);
  646. ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
  647. }
  648. struct ttydisc_recalc_length
  649. {
  650. struct lwp_tty *tp;
  651. unsigned int curlen;
  652. };
  653. static void ttydisc_recalc_charlength(void *d, char c, int quote)
  654. {
  655. struct ttydisc_recalc_length *data = d;
  656. struct lwp_tty *tp = data->tp;
  657. if (CTL_PRINT(c, quote))
  658. {
  659. if (CMP_FLAG(l, ECHOCTL))
  660. data->curlen += 2;
  661. }
  662. else if (c == CTAB)
  663. {
  664. data->curlen += 8 - (data->curlen & 7);
  665. }
  666. else
  667. {
  668. data->curlen++;
  669. }
  670. }
  671. static unsigned int ttydisc_recalc_linelength(struct lwp_tty *tp)
  672. {
  673. struct ttydisc_recalc_length data = {tp, tp->t_writepos};
  674. ttyinq_line_iterate_from_reprintpos(&tp->t_inq, ttydisc_recalc_charlength,
  675. &data);
  676. return (data.curlen);
  677. }
  678. static int ttydisc_rubchar(struct lwp_tty *tp)
  679. {
  680. char c;
  681. int quote;
  682. unsigned int prevpos, tablen;
  683. if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
  684. return (-1);
  685. ttyinq_unputchar(&tp->t_inq);
  686. if (CMP_FLAG(l, ECHO))
  687. {
  688. /*
  689. * Remove the character from the screen. This is even
  690. * safe for characters that span multiple characters
  691. * (tabs, quoted, etc).
  692. */
  693. if (tp->t_writepos >= tp->t_column)
  694. {
  695. /* Retype the sentence. */
  696. ttydisc_reprint(tp);
  697. }
  698. else if (CMP_FLAG(l, ECHOE))
  699. {
  700. if (CTL_PRINT(c, quote))
  701. {
  702. /* Remove ^X formatted chars. */
  703. if (CMP_FLAG(l, ECHOCTL))
  704. {
  705. tp->t_column -= 2;
  706. ttyoutq_write_nofrag(&tp->t_outq, "\b\b \b\b", 6);
  707. }
  708. }
  709. else if (c == ' ')
  710. {
  711. /* Space character needs no rubbing. */
  712. tp->t_column -= 1;
  713. ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
  714. }
  715. else if (c == CTAB)
  716. {
  717. /*
  718. * Making backspace work with tabs is
  719. * quite hard. Recalculate the length of
  720. * this character and remove it.
  721. *
  722. * Because terminal settings could be
  723. * changed while the line is being
  724. * inserted, the calculations don't have
  725. * to be correct. Make sure we keep the
  726. * tab length within proper bounds.
  727. */
  728. prevpos = ttydisc_recalc_linelength(tp);
  729. if (prevpos >= tp->t_column)
  730. tablen = 1;
  731. else
  732. tablen = tp->t_column - prevpos;
  733. if (tablen > 8)
  734. tablen = 8;
  735. tp->t_column = prevpos;
  736. ttyoutq_write_nofrag(&tp->t_outq, "\b\b\b\b\b\b\b\b", tablen);
  737. return 0;
  738. }
  739. #ifdef USING_BSD_UTF8
  740. else if ((tp->t_termios.c_iflag & IUTF8) != 0 && CTL_UTF8(c))
  741. {
  742. uint8_t bytes[UTF8_STACKBUF] = {0};
  743. int curidx = UTF8_STACKBUF - 1, cwidth = 1, nb = 0;
  744. teken_char_t codepoint;
  745. /* Save current byte. */
  746. bytes[curidx] = c;
  747. curidx--;
  748. nb++;
  749. /* Loop back through inq until we hit the
  750. * leading byte. */
  751. while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF)
  752. {
  753. /*
  754. * Check if we've reached the beginning
  755. * of the line.
  756. */
  757. if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
  758. break;
  759. ttyinq_unputchar(&tp->t_inq);
  760. bytes[curidx] = c;
  761. curidx--;
  762. nb++;
  763. }
  764. /*
  765. * Shift array so that the leading
  766. * byte ends up at idx 0.
  767. */
  768. if (nb < UTF8_STACKBUF)
  769. memmove(&bytes[0], &bytes[curidx + 1],
  770. nb * sizeof(uint8_t));
  771. /* Check for malformed UTF8 characters. */
  772. if (nb == UTF8_STACKBUF && CTL_UTF8_CONT(bytes[0]))
  773. {
  774. /*
  775. * Place all bytes back into the inq and
  776. * delete the last byte only.
  777. */
  778. ttyinq_write(&tp->t_inq, bytes, UTF8_STACKBUF, 0);
  779. ttyinq_unputchar(&tp->t_inq);
  780. }
  781. else
  782. {
  783. /* Find codepoint and width. */
  784. codepoint = teken_utf8_bytes_to_codepoint(bytes, nb);
  785. if (codepoint == TEKEN_UTF8_INVALID_CODEPOINT ||
  786. (cwidth = teken_wcwidth(codepoint)) == -1)
  787. {
  788. /*
  789. * Place all bytes back into the
  790. * inq and fall back to
  791. * default behaviour.
  792. */
  793. cwidth = 1;
  794. ttyinq_write(&tp->t_inq, bytes, nb, 0);
  795. ttyinq_unputchar(&tp->t_inq);
  796. }
  797. }
  798. tp->t_column -= cwidth;
  799. /*
  800. * Delete character by punching
  801. * 'cwidth' spaces over it.
  802. */
  803. if (cwidth == 1)
  804. ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
  805. else if (cwidth == 2)
  806. ttyoutq_write_nofrag(&tp->t_outq, "\b\b \b\b", 6);
  807. }
  808. #endif
  809. else
  810. {
  811. /*
  812. * Remove a regular character by
  813. * punching a space over it.
  814. */
  815. tp->t_column -= 1;
  816. ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
  817. }
  818. }
  819. else
  820. {
  821. /* Don't print spaces. */
  822. ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
  823. }
  824. }
  825. return 0;
  826. }
  827. static void ttydisc_rubword(struct lwp_tty *tp)
  828. {
  829. char c;
  830. int quote;
  831. #ifdef ALTWERASE
  832. int alnum;
  833. #endif
  834. /* Strip whitespace first. */
  835. for (;;)
  836. {
  837. if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
  838. return;
  839. if (!CTL_WHITE(c))
  840. break;
  841. ttydisc_rubchar(tp);
  842. }
  843. /*
  844. * Record whether the last character from the previous iteration
  845. * was alphanumeric or not. We need this to implement ALTWERASE.
  846. */
  847. #ifdef ALTWERASE
  848. alnum = CTL_ALNUM(c);
  849. #endif
  850. for (;;)
  851. {
  852. ttydisc_rubchar(tp);
  853. if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
  854. return;
  855. if (CTL_WHITE(c))
  856. return;
  857. #ifdef ALTWERASE
  858. if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
  859. return;
  860. #endif
  861. }
  862. }
  863. int ttydisc_rint(struct lwp_tty *tp, char c, int flags)
  864. {
  865. int signal, quote = 0;
  866. char ob[3] = {0xff, 0x00};
  867. size_t ol;
  868. tty_assert_locked(tp);
  869. rt_atomic_add(&tty_nin, 1);
  870. #ifdef USING_BSD_HOOK
  871. if (ttyhook_hashook(tp, rint))
  872. return ttyhook_rint(tp, c, flags);
  873. #endif
  874. if (tp->t_flags & TF_BYPASS)
  875. goto processed;
  876. if (flags)
  877. {
  878. if (flags & TRE_BREAK)
  879. {
  880. if (CMP_FLAG(i, IGNBRK))
  881. {
  882. /* Ignore break characters. */
  883. return 0;
  884. }
  885. else if (CMP_FLAG(i, BRKINT))
  886. {
  887. /* Generate SIGINT on break. */
  888. tty_flush(tp, FREAD | FWRITE);
  889. lwp_tty_signal_pgrp(tp, SIGINT);
  890. return 0;
  891. }
  892. else
  893. {
  894. /* Just print it. */
  895. goto parmrk;
  896. }
  897. }
  898. else if (flags & TRE_FRAMING ||
  899. (flags & TRE_PARITY && CMP_FLAG(i, INPCK)))
  900. {
  901. if (CMP_FLAG(i, IGNPAR))
  902. {
  903. /* Ignore bad characters. */
  904. return 0;
  905. }
  906. else
  907. {
  908. /* Just print it. */
  909. goto parmrk;
  910. }
  911. }
  912. }
  913. /* Allow any character to perform a wakeup. */
  914. if (CMP_FLAG(i, IXANY))
  915. {
  916. tp->t_flags &= ~TF_STOPPED;
  917. tp->t_termios.c_lflag &= ~FLUSHO; /* FLUSHO: Output is being flushed */
  918. }
  919. /* Remove the top bit. */
  920. if (CMP_FLAG(i, ISTRIP))
  921. c &= ~0x80;
  922. /* Skip input processing when we want to print it literally. */
  923. if (tp->t_flags & TF_LITERAL)
  924. {
  925. tp->t_flags &= ~TF_LITERAL;
  926. quote = 1;
  927. goto processed;
  928. }
  929. /* Special control characters that are implementation dependent. */
  930. if (CMP_FLAG(l, IEXTEN))
  931. {
  932. /* Accept the next character as literal. */
  933. if (CMP_CC(VLNEXT, c))
  934. {
  935. if (CMP_FLAG(l, ECHO))
  936. {
  937. if (CMP_FLAG(l, ECHOE))
  938. ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
  939. else
  940. ttydisc_echo(tp, c, 0);
  941. }
  942. tp->t_flags |= TF_LITERAL;
  943. return 0;
  944. }
  945. /* Discard processing */
  946. if (CMP_CC(VDISCARD, c))
  947. {
  948. if (CMP_FLAG(l, FLUSHO))
  949. {
  950. tp->t_termios.c_lflag &= ~FLUSHO;
  951. }
  952. else
  953. {
  954. tty_flush(tp, FWRITE);
  955. ttydisc_echo(tp, c, 0);
  956. if (tp->t_inq.ti_end > 0)
  957. ttydisc_reprint(tp);
  958. tp->t_termios.c_lflag |= FLUSHO;
  959. }
  960. }
  961. }
  962. /*
  963. * Handle signal processing.
  964. */
  965. if (CMP_FLAG(l, ISIG))
  966. {
  967. #ifdef USING_BSD_SIGINFO
  968. if (CMP_FLAG(l, ICANON | IEXTEN) == (ICANON | IEXTEN))
  969. {
  970. if (CMP_CC(VSTATUS, c))
  971. {
  972. tty_signal_pgrp(tp, SIGINFO);
  973. return 0;
  974. }
  975. }
  976. #endif /* USING_BSD_SIGINFO */
  977. /*
  978. * When compared to the old implementation, this
  979. * implementation also flushes the output queue. POSIX
  980. * is really brief about this, but does makes us assume
  981. * we have to do so.
  982. */
  983. signal = 0;
  984. if (CMP_CC(VINTR, c))
  985. {
  986. signal = SIGINT;
  987. }
  988. else if (CMP_CC(VQUIT, c))
  989. {
  990. signal = SIGQUIT;
  991. }
  992. else if (CMP_CC(VSUSP, c))
  993. {
  994. signal = SIGTSTP;
  995. }
  996. if (signal != 0)
  997. {
  998. /*
  999. * Echo the character before signalling the
  1000. * processes.
  1001. */
  1002. if (!CMP_FLAG(l, NOFLSH))
  1003. tty_flush(tp, FREAD | FWRITE);
  1004. ttydisc_echo(tp, c, 0);
  1005. lwp_tty_signal_pgrp(tp, signal);
  1006. return 0;
  1007. }
  1008. }
  1009. /*
  1010. * Handle start/stop characters.
  1011. */
  1012. if (CMP_FLAG(i, IXON))
  1013. {
  1014. if (CMP_CC(VSTOP, c))
  1015. {
  1016. /* Stop it if we aren't stopped yet. */
  1017. if ((tp->t_flags & TF_STOPPED) == 0)
  1018. {
  1019. tp->t_flags |= TF_STOPPED;
  1020. return 0;
  1021. }
  1022. /*
  1023. * Fallthrough:
  1024. * When VSTART == VSTOP, we should make this key
  1025. * toggle it.
  1026. */
  1027. if (!CMP_CC(VSTART, c))
  1028. return 0;
  1029. }
  1030. if (CMP_CC(VSTART, c))
  1031. {
  1032. tp->t_flags &= ~TF_STOPPED;
  1033. return 0;
  1034. }
  1035. }
  1036. /* Conversion of CR and NL. */
  1037. switch (c)
  1038. {
  1039. case CCR:
  1040. if (CMP_FLAG(i, IGNCR))
  1041. return 0;
  1042. if (CMP_FLAG(i, ICRNL))
  1043. c = CNL;
  1044. break;
  1045. case CNL:
  1046. if (CMP_FLAG(i, INLCR))
  1047. c = CCR;
  1048. break;
  1049. }
  1050. /* Canonical line editing. */
  1051. if (CMP_FLAG(l, ICANON))
  1052. {
  1053. if (CMP_CC(VERASE, c)
  1054. #ifdef VERASE2
  1055. || CMP_CC(VERASE2, c)
  1056. #endif
  1057. )
  1058. {
  1059. ttydisc_rubchar(tp);
  1060. return 0;
  1061. }
  1062. else if (CMP_CC(VKILL, c))
  1063. {
  1064. while (ttydisc_rubchar(tp) == 0)
  1065. ;
  1066. return 0;
  1067. }
  1068. else if (CMP_FLAG(l, IEXTEN))
  1069. {
  1070. if (CMP_CC(VWERASE, c))
  1071. {
  1072. ttydisc_rubword(tp);
  1073. return 0;
  1074. }
  1075. else if (CMP_CC(VREPRINT, c))
  1076. {
  1077. ttydisc_reprint(tp);
  1078. return 0;
  1079. }
  1080. }
  1081. }
  1082. processed:
  1083. if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff)
  1084. {
  1085. /* Print 0xff 0xff. */
  1086. ob[1] = 0xff;
  1087. ol = 2;
  1088. quote = 1;
  1089. }
  1090. else
  1091. {
  1092. ob[0] = c;
  1093. ol = 1;
  1094. }
  1095. goto print;
  1096. parmrk:
  1097. if (CMP_FLAG(i, PARMRK))
  1098. {
  1099. /* Prepend 0xff 0x00 0x.. */
  1100. ob[2] = c;
  1101. ol = 3;
  1102. quote = 1;
  1103. }
  1104. else
  1105. {
  1106. ob[0] = c;
  1107. ol = 1;
  1108. }
  1109. print:
  1110. /* See if we can store this on the input queue. */
  1111. if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0)
  1112. {
  1113. if (CMP_FLAG(i, IMAXBEL))
  1114. ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
  1115. /*
  1116. * Prevent a deadlock here. It may be possible that a
  1117. * user has entered so much data, there is no data
  1118. * available to read(), but the buffers are full anyway.
  1119. *
  1120. * Only enter the high watermark if the device driver
  1121. * can actually transmit something.
  1122. */
  1123. if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
  1124. return 0;
  1125. tty_hiwat_in_block(tp);
  1126. return (-1);
  1127. }
  1128. /*
  1129. * In raw mode, we canonicalize after receiving a single
  1130. * character. Otherwise, we canonicalize when we receive a
  1131. * newline, VEOL or VEOF, but only when it isn't quoted.
  1132. */
  1133. if (!CMP_FLAG(l, ICANON) ||
  1134. (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c))))
  1135. {
  1136. ttyinq_canonicalize(&tp->t_inq);
  1137. }
  1138. ttydisc_echo(tp, c, quote);
  1139. return 0;
  1140. }
  1141. size_t ttydisc_rint_simple(struct lwp_tty *tp, const void *buf, size_t len)
  1142. {
  1143. const char *cbuf;
  1144. if (ttydisc_can_bypass(tp))
  1145. return (ttydisc_rint_bypass(tp, buf, len));
  1146. for (cbuf = buf; len-- > 0; cbuf++)
  1147. {
  1148. if (ttydisc_rint(tp, *cbuf, 0) != 0)
  1149. break;
  1150. }
  1151. return (cbuf - (const char *)buf);
  1152. }
  1153. size_t ttydisc_rint_bypass(struct lwp_tty *tp, const void *buf, size_t len)
  1154. {
  1155. size_t ret;
  1156. tty_assert_locked(tp);
  1157. MPASS(tp->t_flags & TF_BYPASS);
  1158. rt_atomic_add(&tty_nin, len);
  1159. #ifdef USING_BSD_HOOK
  1160. if (ttyhook_hashook(tp, rint_bypass))
  1161. return ttyhook_rint_bypass(tp, buf, len);
  1162. #endif
  1163. ret = ttyinq_write(&tp->t_inq, buf, len, 0);
  1164. ttyinq_canonicalize(&tp->t_inq);
  1165. if (ret < len)
  1166. tty_hiwat_in_block(tp);
  1167. return (ret);
  1168. }
  1169. void ttydisc_rint_done(struct lwp_tty *tp)
  1170. {
  1171. tty_assert_locked(tp);
  1172. #ifdef USING_BSD_HOOK
  1173. if (ttyhook_hashook(tp, rint_done))
  1174. ttyhook_rint_done(tp);
  1175. #endif
  1176. /* Wake up readers. */
  1177. tty_wakeup(tp, FREAD);
  1178. /* Wake up driver for echo. */
  1179. ttydevsw_outwakeup(tp);
  1180. }
  1181. size_t ttydisc_rint_poll(struct lwp_tty *tp)
  1182. {
  1183. size_t l;
  1184. tty_assert_locked(tp);
  1185. #ifdef USING_BSD_HOOK
  1186. if (ttyhook_hashook(tp, rint_poll))
  1187. return ttyhook_rint_poll(tp);
  1188. #endif
  1189. /*
  1190. * XXX: Still allow character input when there's no space in the
  1191. * buffers, but we haven't entered the high watermark. This is
  1192. * to allow backspace characters to be inserted when in
  1193. * canonical mode.
  1194. */
  1195. l = ttyinq_bytesleft(&tp->t_inq);
  1196. if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
  1197. return (1);
  1198. return (l);
  1199. }
  1200. static void ttydisc_wakeup_watermark(struct lwp_tty *tp)
  1201. {
  1202. size_t c;
  1203. c = ttyoutq_bytesleft(&tp->t_outq);
  1204. if (tp->t_flags & TF_HIWAT_OUT)
  1205. {
  1206. /* Only allow us to run when we're below the watermark. */
  1207. if (c < tp->t_outlow)
  1208. return;
  1209. /* Reset the watermark. */
  1210. tp->t_flags &= ~TF_HIWAT_OUT;
  1211. }
  1212. else
  1213. {
  1214. /* Only run when we have data at all. */
  1215. if (c == 0)
  1216. return;
  1217. }
  1218. tty_wakeup(tp, FWRITE);
  1219. }
  1220. size_t ttydisc_getc(struct lwp_tty *tp, void *buf, size_t len)
  1221. {
  1222. tty_assert_locked(tp);
  1223. if (tp->t_flags & TF_STOPPED)
  1224. return 0;
  1225. #ifdef USING_BSD_HOOK
  1226. if (ttyhook_hashook(tp, getc_inject))
  1227. return ttyhook_getc_inject(tp, buf, len);
  1228. #endif
  1229. len = ttyoutq_read(&tp->t_outq, buf, len);
  1230. #ifdef USING_BSD_HOOK
  1231. if (ttyhook_hashook(tp, getc_capture))
  1232. ttyhook_getc_capture(tp, buf, len);
  1233. #endif
  1234. ttydisc_wakeup_watermark(tp);
  1235. rt_atomic_add(&tty_nout, len);
  1236. return (len);
  1237. }
  1238. int ttydisc_getc_uio(struct lwp_tty *tp, struct uio *uio)
  1239. {
  1240. int error = 0;
  1241. ssize_t obytes = uio->uio_resid;
  1242. size_t len;
  1243. char buf[TTY_STACKBUF];
  1244. tty_assert_locked(tp);
  1245. if (tp->t_flags & TF_STOPPED)
  1246. return 0;
  1247. /*
  1248. * When a TTY hook is attached, we cannot perform unbuffered
  1249. * copying to userspace. Just call ttydisc_getc() and
  1250. * temporarily store data in a shadow buffer.
  1251. */
  1252. if (ttyhook_hashook(tp, getc_capture) || ttyhook_hashook(tp, getc_inject))
  1253. {
  1254. while (uio->uio_resid > 0)
  1255. {
  1256. /* Read to shadow buffer. */
  1257. len = ttydisc_getc(tp, buf, MIN(uio->uio_resid, sizeof buf));
  1258. if (len == 0)
  1259. break;
  1260. /* Copy to userspace. */
  1261. tty_unlock(tp);
  1262. error = uiomove(buf, len, uio);
  1263. tty_lock(tp);
  1264. if (error != 0)
  1265. break;
  1266. }
  1267. }
  1268. else
  1269. {
  1270. error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
  1271. ttydisc_wakeup_watermark(tp);
  1272. rt_atomic_add(&tty_nout, obytes - uio->uio_resid);
  1273. }
  1274. return error;
  1275. }
  1276. size_t ttydisc_getc_poll(struct lwp_tty *tp)
  1277. {
  1278. tty_assert_locked(tp);
  1279. if (tp->t_flags & TF_STOPPED)
  1280. return 0;
  1281. #if USING_BSD_HOOK
  1282. if (ttyhook_hashook(tp, getc_poll))
  1283. return ttyhook_getc_poll(tp);
  1284. #endif
  1285. return ttyoutq_bytesused(&tp->t_outq);
  1286. }