n_tty.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021.12.07 linzhenxing first version
  9. */
  10. #include <stddef.h>
  11. #include <ctype.h>
  12. #include <tty.h>
  13. #include <lwp.h>
  14. #if defined(RT_USING_POSIX)
  15. #include <posix_termios.h>
  16. #endif
  17. #define DBG_TAG "N_TTY"
  18. #ifdef RT_TTY_DEBUG
  19. #define DBG_LVL DBG_LOG
  20. #else
  21. #define DBG_LVL DBG_INFO
  22. #endif /* RT_TTY_DEBUG */
  23. #include <rtdbg.h>
  24. /* number of characters left in xmit buffer before select has we have room */
  25. #define WAKEUP_CHARS 256
  26. /*
  27. * This defines the low- and high-watermarks for throttling and
  28. * unthrottling the TTY driver. These watermarks are used for
  29. * controlling the space in the read buffer.
  30. */
  31. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  32. #define TTY_THRESHOLD_UNTHROTTLE 128
  33. /*
  34. * Special byte codes used in the echo buffer to represent operations
  35. * or special handling of characters. Bytes in the echo buffer that
  36. * are not part of such special blocks are treated as normal character
  37. * codes.
  38. */
  39. #define ECHO_OP_START 0xff
  40. #define ECHO_OP_MOVE_BACK_COL 0x80
  41. #define ECHO_OP_SET_CANON_COL 0x81
  42. #define ECHO_OP_ERASE_TAB 0x82
  43. #define ECHO_COMMIT_WATERMARK 256
  44. #define ECHO_BLOCK 256
  45. #define ECHO_DISCARD_WATERMARK RT_TTY_BUF - (ECHO_BLOCK + 32)
  46. rt_inline void tty_sigaddset(lwp_sigset_t *set, int _sig)
  47. {
  48. unsigned long sig = _sig - 1;
  49. if (_LWP_NSIG_WORDS == 1)
  50. {
  51. set->sig[0] |= 1UL << sig;
  52. }
  53. else
  54. {
  55. set->sig[sig / _LWP_NSIG_BPW] |= 1UL << (sig % _LWP_NSIG_BPW);
  56. }
  57. }
  58. struct n_tty_data
  59. {
  60. /* producer-published */
  61. size_t read_head;
  62. size_t commit_head;
  63. size_t canon_head;
  64. size_t echo_head;
  65. size_t echo_commit;
  66. size_t echo_mark;
  67. unsigned long char_map[256];
  68. /* non-atomic */
  69. rt_bool_t no_room;
  70. /* must hold exclusive termios_rwsem to reset these */
  71. unsigned char lnext:1, erasing:1, raw:1, real_raw:1, icanon:1;
  72. unsigned char push:1;
  73. /* shared by producer and consumer */
  74. char read_buf[RT_TTY_BUF];
  75. unsigned long read_flags[RT_TTY_BUF];
  76. unsigned char echo_buf[RT_TTY_BUF];
  77. /* consumer-published */
  78. size_t read_tail;
  79. size_t line_start;
  80. /* protected by output lock */
  81. unsigned int column;
  82. unsigned int canon_column;
  83. size_t echo_tail;
  84. rt_mutex_t atomic_read_lock;
  85. rt_mutex_t output_lock;
  86. };
  87. static inline size_t read_cnt(struct n_tty_data *ldata)
  88. {
  89. return ldata->read_head - ldata->read_tail;
  90. }
  91. static inline char read_buf(struct n_tty_data *ldata, size_t i)
  92. {
  93. return ldata->read_buf[i & (RT_TTY_BUF - 1)];
  94. }
  95. static inline char *read_buf_addr(struct n_tty_data *ldata, size_t i)
  96. {
  97. return &ldata->read_buf[i & (RT_TTY_BUF - 1)];
  98. }
  99. static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
  100. {
  101. return ldata->echo_buf[i & (RT_TTY_BUF - 1)];
  102. }
  103. static inline unsigned char *echo_buf_addr(struct n_tty_data *ldata, size_t i)
  104. {
  105. return &ldata->echo_buf[i & (RT_TTY_BUF - 1)];
  106. }
  107. /**
  108. * put_tty_queue - add character to tty
  109. * @c: character
  110. * @ldata: n_tty data
  111. *
  112. * Add a character to the tty read_buf queue.
  113. *
  114. * n_tty_receive_buf()/producer path:
  115. * caller holds non-exclusive termios_rwsem
  116. */
  117. static inline void put_tty_queue(unsigned char c, struct n_tty_data *ldata)
  118. {
  119. *read_buf_addr(ldata, ldata->read_head) = c;
  120. ldata->read_head++;
  121. }
  122. /**
  123. * reset_buffer_flags - reset buffer state
  124. * @tty: terminal to reset
  125. *
  126. * Reset the read buffer counters and clear the flags.
  127. * Called from n_tty_open() and n_tty_flush_buffer().
  128. *
  129. * Locking: caller holds exclusive termios_rwsem
  130. * (or locking is not required)
  131. */
  132. static void reset_buffer_flags(struct n_tty_data *ldata)
  133. {
  134. ldata->read_head = ldata->canon_head = ldata->read_tail = 0;
  135. ldata->echo_head = ldata->echo_tail = ldata->echo_commit = 0;
  136. ldata->commit_head = 0;
  137. ldata->echo_mark = 0;
  138. ldata->line_start = 0;
  139. ldata->erasing = 0;
  140. rt_memset(ldata->read_flags, 0, RT_TTY_BUF);
  141. ldata->push = 0;
  142. }
  143. /**
  144. * add_echo_byte - add a byte to the echo buffer
  145. * @c: unicode byte to echo
  146. * @ldata: n_tty data
  147. *
  148. * Add a character or operation byte to the echo buffer.
  149. */
  150. static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
  151. {
  152. *echo_buf_addr(ldata, ldata->echo_head++) = c;
  153. }
  154. /**
  155. * echo_move_back_col - add operation to move back a column
  156. * @ldata: n_tty data
  157. *
  158. * Add an operation to the echo buffer to move back one column.
  159. */
  160. static void echo_move_back_col(struct n_tty_data *ldata)
  161. {
  162. add_echo_byte(ECHO_OP_START, ldata);
  163. add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
  164. }
  165. /**
  166. * echo_set_canon_col - add operation to set the canon column
  167. * @ldata: n_tty data
  168. *
  169. * Add an operation to the echo buffer to set the canon column
  170. * to the current column.
  171. */
  172. static void echo_set_canon_col(struct n_tty_data *ldata)
  173. {
  174. add_echo_byte(ECHO_OP_START, ldata);
  175. add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
  176. }
  177. /**
  178. * echo_erase_tab - add operation to erase a tab
  179. * @num_chars: number of character columns already used
  180. * @after_tab: true if num_chars starts after a previous tab
  181. * @ldata: n_tty data
  182. *
  183. * Add an operation to the echo buffer to erase a tab.
  184. *
  185. * Called by the eraser function, which knows how many character
  186. * columns have been used since either a previous tab or the start
  187. * of input. This information will be used later, along with
  188. * canon column (if applicable), to go back the correct number
  189. * of columns.
  190. */
  191. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  192. struct n_tty_data *ldata)
  193. {
  194. add_echo_byte(ECHO_OP_START, ldata);
  195. add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
  196. /* We only need to know this modulo 8 (tab spacing) */
  197. num_chars &= 7;
  198. /* Set the high bit as a flag if num_chars is after a previous tab */
  199. if (after_tab)
  200. {
  201. num_chars |= 0x80;
  202. }
  203. add_echo_byte(num_chars, ldata);
  204. }
  205. /**
  206. * echo_char_raw - echo a character raw
  207. * @c: unicode byte to echo
  208. * @tty: terminal device
  209. *
  210. * Echo user input back onto the screen. This must be called only when
  211. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  212. *
  213. * This variant does not treat control characters specially.
  214. */
  215. static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
  216. {
  217. if (c == ECHO_OP_START)
  218. {
  219. add_echo_byte(ECHO_OP_START, ldata);
  220. add_echo_byte(ECHO_OP_START, ldata);
  221. }
  222. else
  223. {
  224. add_echo_byte(c, ldata);
  225. }
  226. }
  227. /**
  228. * echo_char - echo a character
  229. * @c: unicode byte to echo
  230. * @tty: terminal device
  231. *
  232. * Echo user input back onto the screen. This must be called only when
  233. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  234. *
  235. * This variant tags control characters to be echoed as "^X"
  236. * (where X is the letter representing the control char).
  237. */
  238. static void echo_char(unsigned char c, struct tty_struct *tty)
  239. {
  240. struct n_tty_data *ldata = tty->disc_data;
  241. if (c == ECHO_OP_START)
  242. {
  243. add_echo_byte(ECHO_OP_START, ldata);
  244. add_echo_byte(ECHO_OP_START, ldata);
  245. }
  246. else
  247. {
  248. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  249. {
  250. add_echo_byte(ECHO_OP_START, ldata);
  251. }
  252. add_echo_byte(c, ldata);
  253. }
  254. }
  255. /**
  256. * finish_erasing - complete erase
  257. * @ldata: n_tty data
  258. */
  259. static inline void finish_erasing(struct n_tty_data *ldata)
  260. {
  261. if (ldata->erasing)
  262. {
  263. echo_char_raw('/', ldata);
  264. ldata->erasing = 0;
  265. }
  266. }
  267. /**
  268. * is_utf8_continuation - utf8 multibyte check
  269. * @c: byte to check
  270. *
  271. * Returns true if the utf8 character 'c' is a multibyte continuation
  272. * character. We use this to correctly compute the on screen size
  273. * of the character when printing
  274. */
  275. static inline int is_utf8_continuation(unsigned char c)
  276. {
  277. return (c & 0xc0) == 0x80;
  278. }
  279. /**
  280. * is_continuation - multibyte check
  281. * @c: byte to check
  282. *
  283. * Returns true if the utf8 character 'c' is a multibyte continuation
  284. * character and the terminal is in unicode mode.
  285. */
  286. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  287. {
  288. return I_IUTF8(tty) && is_utf8_continuation(c);
  289. }
  290. /**
  291. * eraser - handle erase function
  292. * @c: character input
  293. * @tty: terminal device
  294. *
  295. * Perform erase and necessary output when an erase character is
  296. * present in the stream from the driver layer. Handles the complexities
  297. * of UTF-8 multibyte symbols.
  298. *
  299. * n_tty_receive_buf()/producer path:
  300. * caller holds non-exclusive termios_rwsem
  301. */
  302. static void eraser(unsigned char c, struct tty_struct *tty)
  303. {
  304. struct n_tty_data *ldata = tty->disc_data;
  305. enum { KERASE, WERASE, KILL } kill_type;
  306. size_t head = 0;
  307. size_t cnt = 0;
  308. int seen_alnums = 0;
  309. if (ldata->read_head == ldata->canon_head)
  310. {
  311. /* process_output('\a', tty); */ /* what do you think? */
  312. return;
  313. }
  314. if (c == ERASE_CHAR(tty))
  315. {
  316. kill_type = KERASE;
  317. }
  318. else if (c == WERASE_CHAR(tty))
  319. {
  320. kill_type = WERASE;
  321. }
  322. else
  323. {
  324. if (!L_ECHO(tty))
  325. {
  326. ldata->read_head = ldata->canon_head;
  327. return;
  328. }
  329. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty))
  330. {
  331. ldata->read_head = ldata->canon_head;
  332. finish_erasing(ldata);
  333. echo_char(KILL_CHAR(tty), tty);
  334. /* Add a newline if ECHOK is on and ECHOKE is off. */
  335. if (L_ECHOK(tty))
  336. {
  337. echo_char_raw('\n', ldata);
  338. }
  339. return;
  340. }
  341. kill_type = KILL;
  342. }
  343. seen_alnums = 0;
  344. while (ldata->read_head != ldata->canon_head)
  345. {
  346. head = ldata->read_head;
  347. /* erase a single possibly multibyte character */
  348. do
  349. {
  350. head--;
  351. c = read_buf(ldata, head);
  352. } while (is_continuation(c, tty) && head != ldata->canon_head);
  353. /* do not partially erase */
  354. if (is_continuation(c, tty))
  355. {
  356. break;
  357. }
  358. if (kill_type == WERASE)
  359. {
  360. /* Equivalent to BSD's ALTWERASE. */
  361. if (isalnum(c) || c == '_')
  362. {
  363. seen_alnums++;
  364. }
  365. else if (seen_alnums)
  366. {
  367. break;
  368. }
  369. }
  370. cnt = ldata->read_head - head;
  371. ldata->read_head = head;
  372. if (L_ECHO(tty))
  373. {
  374. if (L_ECHOPRT(tty))
  375. {
  376. if (!ldata->erasing)
  377. {
  378. echo_char_raw('\\', ldata);
  379. ldata->erasing = 1;
  380. }
  381. /* if cnt > 1, output a multi-byte character */
  382. echo_char(c, tty);
  383. while (--cnt > 0)
  384. {
  385. head++;
  386. echo_char_raw(read_buf(ldata, head), ldata);
  387. echo_move_back_col(ldata);
  388. }
  389. }
  390. else if (kill_type == KERASE && !L_ECHOE(tty))
  391. {
  392. echo_char(ERASE_CHAR(tty), tty);
  393. }
  394. else if (c == '\t')
  395. {
  396. unsigned int num_chars = 0;
  397. int after_tab = 0;
  398. size_t tail = ldata->read_head;
  399. /*
  400. * Count the columns used for characters
  401. * since the start of input or after a
  402. * previous tab.
  403. * This info is used to go back the correct
  404. * number of columns.
  405. */
  406. while (tail != ldata->canon_head)
  407. {
  408. tail--;
  409. c = read_buf(ldata, tail);
  410. if (c == '\t')
  411. {
  412. after_tab = 1;
  413. break;
  414. }
  415. else if (iscntrl(c))
  416. {
  417. if (L_ECHOCTL(tty))
  418. {
  419. num_chars += 2;
  420. }
  421. }
  422. else if (!is_continuation(c, tty))
  423. {
  424. num_chars++;
  425. }
  426. }
  427. echo_erase_tab(num_chars, after_tab, ldata);
  428. }
  429. else
  430. {
  431. if (iscntrl(c) && L_ECHOCTL(tty))
  432. {
  433. echo_char_raw('\b', ldata);
  434. echo_char_raw(' ', ldata);
  435. echo_char_raw('\b', ldata);
  436. }
  437. if (!iscntrl(c) || L_ECHOCTL(tty))
  438. {
  439. echo_char_raw('\b', ldata);
  440. echo_char_raw(' ', ldata);
  441. echo_char_raw('\b', ldata);
  442. }
  443. }
  444. }
  445. if (kill_type == KERASE)
  446. {
  447. break;
  448. }
  449. }
  450. if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
  451. {
  452. finish_erasing(ldata);
  453. }
  454. }
  455. /**
  456. * isig - handle the ISIG optio
  457. * @sig: signal
  458. * @tty: terminal
  459. *
  460. * Called when a signal is being sent due to terminal input.
  461. * Called from the driver receive_buf path so serialized.
  462. *
  463. * Performs input and output flush if !NOFLSH. In this context, the echo
  464. * buffer is 'output'. The signal is processed first to alert any current
  465. * readers or writers to discontinue and exit their i/o loops.
  466. *
  467. * Locking: ctrl_lock
  468. */
  469. static void __isig(int sig, struct tty_struct *tty)
  470. {
  471. struct rt_lwp *lwp = tty->foreground;
  472. struct tty_ldisc *ld = RT_NULL;
  473. struct termios old_termios;
  474. struct termios *new_termios = get_old_termios();
  475. if (lwp)
  476. {
  477. if (sig == SIGTSTP)
  478. {
  479. rt_memcpy(&old_termios, &(tty->init_termios), sizeof(struct termios));
  480. tty->init_termios = *new_termios;
  481. ld = tty->ldisc;
  482. if (ld != RT_NULL)
  483. {
  484. if (ld->ops->set_termios)
  485. {
  486. ld->ops->set_termios(tty, &old_termios);
  487. }
  488. }
  489. tty_sigaddset(&lwp->signal_mask, SIGTTOU);
  490. tty->foreground = RT_NULL;
  491. }
  492. else
  493. {
  494. lwp_kill(lwp_to_pid(lwp), sig);
  495. }
  496. }
  497. }
  498. static void isig(int sig, struct tty_struct *tty)
  499. {
  500. struct n_tty_data *ldata = tty->disc_data;
  501. if (L_NOFLSH(tty))
  502. {
  503. /* signal only */
  504. __isig(sig, tty);
  505. }
  506. else
  507. { /* signal and flush */
  508. __isig(sig, tty);
  509. /* clear echo buffer */
  510. ldata->echo_head = ldata->echo_tail = 0;
  511. ldata->echo_mark = ldata->echo_commit = 0;
  512. /* clear input buffer */
  513. reset_buffer_flags(tty->disc_data);
  514. }
  515. }
  516. /**
  517. * do_output_char - output one character
  518. * @c: character (or partial unicode symbol)
  519. * @tty: terminal device
  520. * @space: space available in tty driver write buffer
  521. *
  522. * This is a helper function that handles one output character
  523. * (including special characters like TAB, CR, LF, etc.),
  524. * doing OPOST processing and putting the results in the
  525. * tty driver's write buffer.
  526. *
  527. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  528. * and NLDLY. They simply aren't relevant in the world today.
  529. * If you ever need them, add them here.
  530. *
  531. * Returns the number of bytes of buffer space used or -1 if
  532. * no space left.
  533. *
  534. * Locking: should be called under the output_lock to protect
  535. * the column state and space left in the buffer
  536. */
  537. static int do_output_char(unsigned char c, struct tty_struct *tty)
  538. {
  539. struct n_tty_data *ldata = tty->disc_data;
  540. int spaces = 0;
  541. char *ch = RT_NULL;
  542. switch (c)
  543. {
  544. case '\n':
  545. if (O_ONLRET(tty))
  546. {
  547. ldata->column = 0;
  548. }
  549. if (O_ONLCR(tty))
  550. {
  551. ldata->canon_column = ldata->column = 0;
  552. ch = "\r\n";
  553. rt_device_write((rt_device_t)tty, -1, ch, 2);
  554. return 2;
  555. }
  556. ldata->canon_column = ldata->column;
  557. break;
  558. case '\r':
  559. if (O_ONOCR(tty) && ldata->column == 0)
  560. {
  561. return 0;
  562. }
  563. if (O_OCRNL(tty))
  564. {
  565. c = '\n';
  566. if (O_ONLRET(tty))
  567. {
  568. ldata->canon_column = ldata->column = 0;
  569. }
  570. break;
  571. }
  572. ldata->canon_column = ldata->column = 0;
  573. break;
  574. case '\t':
  575. spaces = 8 - (ldata->column & 7);
  576. if (O_TABDLY(tty) == XTABS)
  577. {
  578. ldata->column += spaces;
  579. ch = " ";
  580. rt_device_write((rt_device_t)tty, -1, &ch, spaces);
  581. return spaces;
  582. }
  583. ldata->column += spaces;
  584. break;
  585. case '\b':
  586. if (ldata->column > 0)
  587. {
  588. ldata->column--;
  589. }
  590. ch = "\b \b";
  591. rt_device_write((rt_device_t)tty, -1, ch, strlen(ch));
  592. return 1;
  593. default:
  594. if (!iscntrl(c))
  595. {
  596. if (O_OLCUC(tty))
  597. {
  598. c = toupper(c);
  599. }
  600. if (!is_continuation(c, tty))
  601. {
  602. ldata->column++;
  603. }
  604. }
  605. break;
  606. }
  607. rt_device_write((rt_device_t)tty, -1, &c, 1);
  608. return 1;
  609. }
  610. /**
  611. * process_output - output post processor
  612. * @c: character (or partial unicode symbol)
  613. * @tty: terminal device
  614. *
  615. * Output one character with OPOST processing.
  616. * Returns -1 when the output device is full and the character
  617. * must be retried.
  618. *
  619. * Locking: output_lock to protect column state and space left
  620. * (also, this is called from n_tty_write under the
  621. * tty layer write lock)
  622. */
  623. static int process_output(unsigned char c, struct tty_struct *tty)
  624. {
  625. int retval = 0;
  626. retval = do_output_char(c, tty);
  627. if (retval < 0)
  628. {
  629. return -1;
  630. }
  631. else
  632. {
  633. return 0;
  634. }
  635. }
  636. /**
  637. * process_output_block - block post processor
  638. * @tty: terminal device
  639. * @buf: character buffer
  640. * @nr: number of bytes to output
  641. *
  642. * Output a block of characters with OPOST processing.
  643. * Returns the number of characters output.
  644. *
  645. * This path is used to speed up block console writes, among other
  646. * things when processing blocks of output data. It handles only
  647. * the simple cases normally found and helps to generate blocks of
  648. * symbols for the console driver and thus improve performance.
  649. *
  650. * Locking: output_lock to protect column state and space left
  651. * (also, this is called from n_tty_write under the
  652. * tty layer write lock)
  653. */
  654. static ssize_t process_output_block(struct tty_struct *tty,
  655. const char *buf, unsigned int nr)
  656. {
  657. struct n_tty_data *ldata = tty->disc_data;
  658. int i = 0;
  659. ssize_t size = 0;
  660. const char *cp = RT_NULL;
  661. for (i = 0, cp = buf; i < nr; i++, cp++)
  662. {
  663. char c = *cp;
  664. switch (c)
  665. {
  666. case '\n':
  667. if (O_ONLRET(tty))
  668. {
  669. ldata->column = 0;
  670. }
  671. if (O_ONLCR(tty))
  672. {
  673. goto break_out;
  674. }
  675. ldata->canon_column = ldata->column;
  676. break;
  677. case '\r':
  678. if (O_ONOCR(tty) && ldata->column == 0)
  679. {
  680. goto break_out;
  681. }
  682. if (O_OCRNL(tty))
  683. {
  684. goto break_out;
  685. }
  686. ldata->canon_column = ldata->column = 0;
  687. break;
  688. case '\t':
  689. goto break_out;
  690. case '\b':
  691. if (ldata->column > 0)
  692. {
  693. ldata->column--;
  694. }
  695. break;
  696. default:
  697. if (!iscntrl(c))
  698. {
  699. if (O_OLCUC(tty))
  700. {
  701. goto break_out;
  702. }
  703. if (!is_continuation(c, tty))
  704. {
  705. ldata->column++;
  706. }
  707. }
  708. break;
  709. }
  710. }
  711. break_out:
  712. size = rt_device_write((rt_device_t)tty, -1, buf, i);
  713. return size;
  714. }
  715. static size_t __process_echoes(struct tty_struct *tty)
  716. {
  717. struct n_tty_data *ldata = tty->disc_data;
  718. size_t tail = 0;
  719. unsigned char c = 0;
  720. char ch = 0;
  721. unsigned char num_chars = 0, num_bs = 0;
  722. tail = ldata->echo_tail;
  723. while (ldata->echo_commit != tail)
  724. {
  725. c = echo_buf(ldata, tail);
  726. if (c == ECHO_OP_START)
  727. {
  728. unsigned char op = 0;
  729. /*
  730. * If the buffer byte is the start of a multi-byte
  731. * operation, get the next byte, which is either the
  732. * op code or a control character value.
  733. */
  734. op = echo_buf(ldata, tail + 1);
  735. switch (op)
  736. {
  737. case ECHO_OP_ERASE_TAB:
  738. num_chars = echo_buf(ldata, tail + 2);
  739. /*
  740. * Determine how many columns to go back
  741. * in order to erase the tab.
  742. * This depends on the number of columns
  743. * used by other characters within the tab
  744. * area. If this (modulo 8) count is from
  745. * the start of input rather than from a
  746. * previous tab, we offset by canon column.
  747. * Otherwise, tab spacing is normal.
  748. */
  749. if (!(num_chars & 0x80))
  750. {
  751. num_chars += ldata->canon_column;
  752. }
  753. num_bs = 8 - (num_chars & 7);
  754. while (num_bs--)
  755. {
  756. ch = '\b';
  757. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  758. if (ldata->column > 0)
  759. {
  760. ldata->column--;
  761. }
  762. }
  763. tail += 3;
  764. break;
  765. case ECHO_OP_SET_CANON_COL:
  766. ldata->canon_column = ldata->column;
  767. tail += 2;
  768. break;
  769. case ECHO_OP_MOVE_BACK_COL:
  770. if (ldata->column > 0)
  771. {
  772. ldata->column--;
  773. }
  774. tail += 2;
  775. break;
  776. case ECHO_OP_START:
  777. ch = ECHO_OP_START;
  778. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  779. ldata->column++;
  780. tail += 2;
  781. break;
  782. default:
  783. /*
  784. * If the op is not a special byte code,
  785. * it is a ctrl char tagged to be echoed
  786. * as "^X" (where X is the letter
  787. * representing the control char).
  788. * Note that we must ensure there is
  789. * enough space for the whole ctrl pair.
  790. *
  791. */
  792. ch = '^';
  793. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  794. ch = op ^ 0100;
  795. rt_device_write((rt_device_t)tty, -1, &ch, 1);
  796. ldata->column += 2;
  797. tail += 2;
  798. }
  799. }
  800. else
  801. {
  802. if (O_OPOST(tty))
  803. {
  804. int retval = do_output_char(c, tty);
  805. if (retval < 0)
  806. {
  807. break;
  808. }
  809. }
  810. else
  811. {
  812. rt_device_write((rt_device_t)tty, -1, &c, 1);
  813. }
  814. tail += 1;
  815. }
  816. }
  817. /* If the echo buffer is nearly full (so that the possibility exists
  818. * of echo overrun before the next commit), then discard enough
  819. * data at the tail to prevent a subsequent overrun */
  820. while (ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK)
  821. {
  822. if (echo_buf(ldata, tail) == ECHO_OP_START)
  823. {
  824. if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
  825. {
  826. tail += 3;
  827. }
  828. else
  829. {
  830. tail += 2;
  831. }
  832. }
  833. else
  834. {
  835. tail++;
  836. }
  837. }
  838. ldata->echo_tail = tail;
  839. return 0;
  840. }
  841. static void commit_echoes(struct tty_struct *tty)
  842. {
  843. struct n_tty_data *ldata = tty->disc_data;
  844. size_t nr = 0, old = 0;
  845. size_t head = 0;
  846. head = ldata->echo_head;
  847. ldata->echo_mark = head;
  848. old = ldata->echo_commit - ldata->echo_tail;
  849. /* Process committed echoes if the accumulated # of bytes
  850. * is over the threshold (and try again each time another
  851. * block is accumulated) */
  852. nr = head - ldata->echo_tail;
  853. if (nr < ECHO_COMMIT_WATERMARK || (nr % ECHO_BLOCK > old % ECHO_BLOCK))
  854. {
  855. return;
  856. }
  857. ldata->echo_commit = head;
  858. __process_echoes(tty);
  859. }
  860. static void process_echoes(struct tty_struct *tty)
  861. {
  862. struct n_tty_data *ldata = tty->disc_data;
  863. if (ldata->echo_mark == ldata->echo_tail)
  864. {
  865. return;
  866. }
  867. ldata->echo_commit = ldata->echo_mark;
  868. __process_echoes(tty);
  869. }
  870. /* NB: echo_mark and echo_head should be equivalent here */
  871. static void flush_echoes(struct tty_struct *tty)
  872. {
  873. struct n_tty_data *ldata = tty->disc_data;
  874. if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
  875. ldata->echo_commit == ldata->echo_head)
  876. {
  877. return;
  878. }
  879. ldata->echo_commit = ldata->echo_head;
  880. __process_echoes(tty);
  881. }
  882. /**
  883. * n_tty_set_termios - termios data changed
  884. * @tty: terminal
  885. * @old: previous data
  886. *
  887. * Called by the tty layer when the user changes termios flags so
  888. * that the line discipline can plan ahead. This function cannot sleep
  889. * and is protected from re-entry by the tty layer. The user is
  890. * guaranteed that this function will not be re-entered or in progress
  891. * when the ldisc is closed.
  892. *
  893. * Locking: Caller holds tty->termios_rwsem
  894. */
  895. static void n_tty_set_termios(struct tty_struct *tty, struct termios *old)
  896. {
  897. struct n_tty_data *ldata = tty->disc_data;
  898. if (!old || (old->c_lflag ^ tty->init_termios.c_lflag) & (ICANON | EXTPROC))
  899. {
  900. rt_memset(ldata->read_flags, 0, RT_TTY_BUF);
  901. ldata->line_start = ldata->read_tail;
  902. if (!L_ICANON(tty) || !read_cnt(ldata))
  903. {
  904. ldata->canon_head = ldata->read_tail;
  905. ldata->push = 0;
  906. }
  907. else
  908. {
  909. set_bit((ldata->read_head - 1) & (RT_TTY_BUF - 1),(int *)ldata->read_flags);
  910. ldata->canon_head = ldata->read_head;
  911. ldata->push = 1;
  912. }
  913. ldata->commit_head = ldata->read_head;
  914. ldata->erasing = 0;
  915. ldata->lnext = 0;
  916. }
  917. ldata->icanon = (L_ICANON(tty) != 0);
  918. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  919. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  920. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  921. I_PARMRK(tty))
  922. {
  923. rt_memset(ldata->char_map, 0, 256);
  924. if (I_IGNCR(tty) || I_ICRNL(tty))
  925. {
  926. set_bit('\r', (int *)ldata->char_map);
  927. }
  928. if (I_INLCR(tty))
  929. {
  930. set_bit('\n', (int *)ldata->char_map);
  931. }
  932. if (L_ICANON(tty))
  933. {
  934. set_bit(ERASE_CHAR(tty), (int *)ldata->char_map);
  935. set_bit(KILL_CHAR(tty),(int *) ldata->char_map);
  936. set_bit(EOF_CHAR(tty), (int *)ldata->char_map);
  937. set_bit('\n',(int *) ldata->char_map);
  938. set_bit(EOL_CHAR(tty),(int *) ldata->char_map);
  939. if (L_IEXTEN(tty))
  940. {
  941. set_bit(WERASE_CHAR(tty), (int *)ldata->char_map);
  942. set_bit(LNEXT_CHAR(tty), (int *)ldata->char_map);
  943. set_bit(EOL2_CHAR(tty), (int *)ldata->char_map);
  944. if (L_ECHO(tty))
  945. {
  946. set_bit(REPRINT_CHAR(tty),
  947. (int *)ldata->char_map);
  948. }
  949. }
  950. }
  951. if (I_IXON(tty))
  952. {
  953. set_bit(START_CHAR(tty), (int *)ldata->char_map);
  954. set_bit(STOP_CHAR(tty), (int *)ldata->char_map);
  955. }
  956. if (L_ISIG(tty))
  957. {
  958. set_bit(INTR_CHAR(tty), (int *)ldata->char_map);
  959. set_bit(QUIT_CHAR(tty), (int *)ldata->char_map);
  960. set_bit(SUSP_CHAR(tty), (int *)ldata->char_map);
  961. }
  962. clear_bit(__DISABLED_CHAR, (int *)ldata->char_map);
  963. ldata->raw = 0;
  964. ldata->real_raw = 0;
  965. }
  966. else
  967. {
  968. ldata->raw = 1;
  969. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  970. (I_IGNPAR(tty) || !I_INPCK(tty))/* &&
  971. (tty->driver->flags & TTY_DRIVER_REAL_RAW)*/)
  972. {
  973. ldata->real_raw = 1;
  974. }
  975. else
  976. {
  977. ldata->real_raw = 0;
  978. }
  979. }
  980. }
  981. void console_ldata_init(struct tty_struct *tty)
  982. {
  983. struct n_tty_data *ldata = RT_NULL;
  984. ldata = rt_malloc(sizeof(struct n_tty_data));
  985. if (ldata == RT_NULL)
  986. {
  987. LOG_E("console_ldata_init ldata malloc fail");
  988. return;
  989. }
  990. tty->disc_data = ldata;
  991. reset_buffer_flags(ldata);
  992. ldata->column = 0;
  993. ldata->canon_column = 0;
  994. ldata->no_room = 0;
  995. ldata->lnext = 0;
  996. n_tty_set_termios(tty, RT_NULL);
  997. return;
  998. }
  999. static int n_tty_open(struct dfs_fd *fd)
  1000. {
  1001. int ret = 0;
  1002. struct n_tty_data *ldata = RT_NULL;
  1003. struct tty_struct *tty = (struct tty_struct*)fd->fnode->data;
  1004. ldata = rt_malloc(sizeof(struct n_tty_data));
  1005. if (ldata == RT_NULL)
  1006. {
  1007. LOG_E("n_tty_open ldata malloc fail");
  1008. return -1;
  1009. }
  1010. ldata->atomic_read_lock = rt_mutex_create("atomic_read_lock",RT_IPC_FLAG_FIFO);
  1011. ldata->output_lock = rt_mutex_create("output_lock",RT_IPC_FLAG_FIFO);
  1012. tty->disc_data = ldata;
  1013. reset_buffer_flags(ldata);
  1014. ldata->column = 0;
  1015. ldata->canon_column = 0;
  1016. ldata->lnext = 0;
  1017. n_tty_set_termios(tty, RT_NULL);
  1018. return ret;
  1019. }
  1020. static inline int input_available_p(struct tty_struct *tty, int poll)
  1021. {
  1022. struct n_tty_data *ldata = tty->disc_data;
  1023. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1024. if (ldata->icanon && !L_EXTPROC(tty))
  1025. {
  1026. return ldata->canon_head != ldata->read_tail;
  1027. }
  1028. else
  1029. {
  1030. return ldata->commit_head - ldata->read_tail >= amt;
  1031. }
  1032. }
  1033. /**
  1034. * copy_from_read_buf - copy read data directly
  1035. * @tty: terminal device
  1036. * @b: user data
  1037. * @nr: size of data
  1038. *
  1039. * Helper function to speed up n_tty_read. It is only called when
  1040. * ICANON is off; it copies characters straight from the tty queue to
  1041. * user space directly. It can be profitably called twice; once to
  1042. * drain the space from the tail pointer to the (physical) end of the
  1043. * buffer, and once to drain the space from the (physical) beginning of
  1044. * the buffer to head pointer.
  1045. *
  1046. * Called under the ldata->atomic_read_lock sem
  1047. *
  1048. * n_tty_read()/consumer path:
  1049. * caller holds non-exclusive termios_rwsem
  1050. * read_tail published
  1051. */
  1052. static int copy_from_read_buf(struct tty_struct *tty,char *b,size_t nr)
  1053. {
  1054. struct n_tty_data *ldata = tty->disc_data;
  1055. size_t n = 0;
  1056. rt_bool_t is_eof = 0;
  1057. size_t head = ldata->commit_head;
  1058. size_t tail = ldata->read_tail & (RT_TTY_BUF - 1);
  1059. n = min(head - ldata->read_tail, RT_TTY_BUF - tail);
  1060. n = min(nr, n);
  1061. if (n)
  1062. {
  1063. const char *from = read_buf_addr(ldata, tail);
  1064. rt_memcpy(b, from, n);
  1065. is_eof = n == 1 && *from == EOF_CHAR(tty);
  1066. ldata->read_tail += n;
  1067. /* Turn single EOF into zero-length read */
  1068. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1069. (head == ldata->read_tail))
  1070. {
  1071. n = 0;
  1072. }
  1073. }
  1074. return n;
  1075. }
  1076. /**
  1077. * canon_copy_from_read_buf - copy read data in canonical mode
  1078. * @tty: terminal device
  1079. * @b: user data
  1080. * @nr: size of data
  1081. *
  1082. * Helper function for n_tty_read. It is only called when ICANON is on;
  1083. * it copies one line of input up to and including the line-delimiting
  1084. * character into the user-space buffer.
  1085. *
  1086. * NB: When termios is changed from non-canonical to canonical mode and
  1087. * the read buffer contains data, n_tty_set_termios() simulates an EOF
  1088. * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
  1089. * This causes data already processed as input to be immediately available
  1090. * as input although a newline has not been received.
  1091. *
  1092. * Called under the atomic_read_lock mutex
  1093. *
  1094. * n_tty_read()/consumer path:
  1095. * caller holds non-exclusive termios_rwsem
  1096. * read_tail published
  1097. */
  1098. static int canon_copy_from_read_buf(struct tty_struct *tty, char *b, size_t nr)
  1099. {
  1100. struct n_tty_data *ldata = tty->disc_data;
  1101. size_t n = 0, size = 0, more = 0, c = 0;
  1102. size_t eol = 0;
  1103. size_t tail = 0;
  1104. int found = 0;
  1105. /* N.B. avoid overrun if nr == 0 */
  1106. if (nr == 0)
  1107. {
  1108. return 0;
  1109. }
  1110. n = min(nr + 1, ldata->canon_head - ldata->read_tail);
  1111. tail = ldata->read_tail & (RT_TTY_BUF - 1);
  1112. size = min(tail + n, RT_TTY_BUF);
  1113. eol = find_next_bit(ldata->read_flags, size, tail);
  1114. more = n - (size - tail);
  1115. if (eol == RT_TTY_BUF && more)
  1116. {
  1117. /* scan wrapped without finding set bit */
  1118. eol = find_next_bit(ldata->read_flags, more, 0);
  1119. found = eol != more;
  1120. }
  1121. else
  1122. {
  1123. found = eol != size;
  1124. }
  1125. n = eol - tail;
  1126. if (n > RT_TTY_BUF)
  1127. {
  1128. n += RT_TTY_BUF;
  1129. }
  1130. c = n + found;
  1131. if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
  1132. {
  1133. c = min(nr, c);
  1134. n = c;
  1135. }
  1136. size_t buf_size = RT_TTY_BUF - tail;
  1137. const void *from = read_buf_addr(ldata, tail);
  1138. if (n > buf_size)
  1139. {
  1140. rt_memcpy(b, from, buf_size);
  1141. b += buf_size;
  1142. n -= buf_size;
  1143. from = ldata->read_buf;
  1144. }
  1145. rt_memcpy(b, from, n);
  1146. if (found)
  1147. {
  1148. clear_bit(eol, (int *)ldata->read_flags);
  1149. }
  1150. ldata->read_tail = ldata->read_tail + c;
  1151. if (found)
  1152. {
  1153. if (!ldata->push)
  1154. {
  1155. ldata->line_start = ldata->read_tail;
  1156. }
  1157. else
  1158. {
  1159. ldata->push = 0;
  1160. }
  1161. }
  1162. return n;
  1163. }
  1164. static int n_tty_close(struct tty_struct *tty)
  1165. {
  1166. int ret = 0;
  1167. struct n_tty_data *ldata = RT_NULL;
  1168. struct tty_struct *o_tty = RT_NULL;
  1169. RT_ASSERT(tty != RT_NULL);
  1170. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1171. {
  1172. o_tty = tty->other_struct;
  1173. }
  1174. else
  1175. {
  1176. o_tty = tty;
  1177. }
  1178. ldata = o_tty->disc_data;
  1179. rt_free(ldata);
  1180. o_tty->disc_data = RT_NULL;
  1181. return ret;
  1182. }
  1183. static int n_tty_ioctl(struct dfs_fd *fd, int cmd, void *args)
  1184. {
  1185. int ret = 0;
  1186. struct tty_struct *real_tty = RT_NULL;
  1187. struct tty_struct *tty = RT_NULL;
  1188. tty = (struct tty_struct *)fd->fnode->data;
  1189. RT_ASSERT(tty != RT_NULL);
  1190. if (tty->type == TTY_DRIVER_TYPE_PTY && tty->subtype == PTY_TYPE_MASTER)
  1191. {
  1192. real_tty = tty->other_struct;
  1193. }
  1194. else
  1195. {
  1196. real_tty = tty;
  1197. }
  1198. switch(cmd)
  1199. {
  1200. default:
  1201. ret = n_tty_ioctl_extend(real_tty, cmd, args);
  1202. if (ret != -ENOIOCTLCMD)
  1203. {
  1204. return ret;
  1205. }
  1206. }
  1207. ret = rt_device_control((rt_device_t)real_tty, cmd, args);
  1208. if (ret != -ENOIOCTLCMD)
  1209. {
  1210. return ret;
  1211. }
  1212. return ret;
  1213. }
  1214. static void
  1215. n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
  1216. {
  1217. isig(signal, tty);
  1218. if (L_ECHO(tty))
  1219. {
  1220. echo_char(c, tty);
  1221. commit_echoes(tty);
  1222. }
  1223. else
  1224. {
  1225. process_echoes(tty);
  1226. }
  1227. return;
  1228. }
  1229. /**
  1230. * n_tty_receive_char - perform processing
  1231. * @tty: terminal device
  1232. * @c: character
  1233. *
  1234. * Process an individual character of input received from the driver.
  1235. * This is serialized with respect to itself by the rules for the
  1236. * driver above.
  1237. *
  1238. * n_tty_receive_buf()/producer path:
  1239. * caller holds non-exclusive termios_rwsem
  1240. * publishes canon_head if canonical mode is active
  1241. *
  1242. * Returns 1 if LNEXT was received, else returns 0
  1243. */
  1244. static int n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
  1245. {
  1246. struct n_tty_data *ldata = tty->disc_data;
  1247. if (I_IXON(tty))
  1248. {
  1249. if (c == START_CHAR(tty)) /*ctrl + p realize missing*/
  1250. {
  1251. process_echoes(tty);
  1252. return 0;
  1253. }
  1254. if (c == STOP_CHAR(tty)) /*ctrl + s realize missing*/
  1255. {
  1256. return 0;
  1257. }
  1258. }
  1259. if (L_ISIG(tty))
  1260. {
  1261. if (c == INTR_CHAR(tty)) /*ctrl + c realize missing*/
  1262. {
  1263. n_tty_receive_signal_char(tty, SIGINT, c);
  1264. return 0;
  1265. }
  1266. else if (c == QUIT_CHAR(tty)) /*ctrl + d realize missing*/
  1267. {
  1268. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1269. return 0;
  1270. }
  1271. else if (c == SUSP_CHAR(tty)) /*ctrl + z realize missing*/
  1272. {
  1273. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1274. return 0;
  1275. }
  1276. }
  1277. if (c == '\r')
  1278. {
  1279. if (I_IGNCR(tty))
  1280. {
  1281. return 0;
  1282. }
  1283. if (I_ICRNL(tty))
  1284. {
  1285. c = '\n';
  1286. }
  1287. }
  1288. else if (c == '\n' && I_INLCR(tty))
  1289. {
  1290. c = '\r';
  1291. }
  1292. if (ldata->icanon)
  1293. {
  1294. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1295. (c == WERASE_CHAR(tty) && L_IEXTEN(tty)))
  1296. {
  1297. eraser(c, tty);
  1298. commit_echoes(tty);
  1299. return 0;
  1300. }
  1301. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty))
  1302. {
  1303. ldata->lnext = 1;
  1304. if (L_ECHO(tty))
  1305. {
  1306. finish_erasing(ldata);
  1307. if (L_ECHOCTL(tty))
  1308. {
  1309. echo_char_raw('^', ldata);
  1310. echo_char_raw('\b', ldata);
  1311. commit_echoes(tty);
  1312. }
  1313. }
  1314. return 1;
  1315. }
  1316. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty))
  1317. {
  1318. size_t tail = ldata->canon_head;
  1319. finish_erasing(ldata);
  1320. echo_char(c, tty);
  1321. echo_char_raw('\n', ldata);
  1322. while (tail != ldata->read_head)
  1323. {
  1324. echo_char(read_buf(ldata, tail), tty);
  1325. tail++;
  1326. }
  1327. commit_echoes(tty);
  1328. return 0;
  1329. }
  1330. if (c == '\n')
  1331. {
  1332. if (L_ECHO(tty) || L_ECHONL(tty))
  1333. {
  1334. echo_char_raw('\n', ldata);
  1335. commit_echoes(tty);
  1336. }
  1337. goto handle_newline;
  1338. }
  1339. if (c == EOF_CHAR(tty))
  1340. {
  1341. c = __DISABLED_CHAR;
  1342. goto handle_newline;
  1343. }
  1344. if ((c == EOL_CHAR(tty)) ||
  1345. (c == EOL2_CHAR(tty) && L_IEXTEN(tty)))
  1346. {
  1347. /*
  1348. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1349. */
  1350. if (L_ECHO(tty))
  1351. {
  1352. /* Record the column of first canon char. */
  1353. if (ldata->canon_head == ldata->read_head)
  1354. {
  1355. echo_set_canon_col(ldata);
  1356. }
  1357. echo_char(c, tty);
  1358. commit_echoes(tty);
  1359. }
  1360. /*
  1361. * XXX does PARMRK doubling happen for
  1362. * EOL_CHAR and EOL2_CHAR?
  1363. */
  1364. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1365. {
  1366. put_tty_queue(c, ldata);
  1367. }
  1368. handle_newline:
  1369. set_bit(ldata->read_head & (RT_TTY_BUF - 1), (int *)ldata->read_flags);
  1370. put_tty_queue(c, ldata);
  1371. ldata->canon_head = ldata->read_head;
  1372. tty_wakeup_check(tty);
  1373. return 0;
  1374. }
  1375. }
  1376. if (L_ECHO(tty))
  1377. {
  1378. finish_erasing(ldata);
  1379. if (c == '\n')
  1380. {
  1381. echo_char_raw('\n', ldata);
  1382. }
  1383. else
  1384. {
  1385. /* Record the column of first canon char. */
  1386. if (ldata->canon_head == ldata->read_head)
  1387. {
  1388. echo_set_canon_col(ldata);
  1389. }
  1390. echo_char(c, tty);
  1391. }
  1392. commit_echoes(tty);
  1393. }
  1394. /* PARMRK doubling check */
  1395. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1396. {
  1397. put_tty_queue(c, ldata);
  1398. }
  1399. put_tty_queue(c, ldata);
  1400. return 0;
  1401. }
  1402. static inline void n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
  1403. {
  1404. struct n_tty_data *ldata = tty->disc_data;
  1405. if (L_ECHO(tty))
  1406. {
  1407. finish_erasing(ldata);
  1408. /* Record the column of first canon char. */
  1409. if (ldata->canon_head == ldata->read_head)
  1410. {
  1411. echo_set_canon_col(ldata);
  1412. }
  1413. echo_char(c, tty);
  1414. commit_echoes(tty);
  1415. }
  1416. /* PARMRK doubling check */
  1417. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1418. {
  1419. put_tty_queue(c, ldata);
  1420. }
  1421. put_tty_queue(c, ldata);
  1422. }
  1423. static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1424. {
  1425. n_tty_receive_char_inline(tty, c);
  1426. }
  1427. static void n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
  1428. {
  1429. struct n_tty_data *ldata = tty->disc_data;
  1430. ldata->lnext = 0;
  1431. if (flag == TTY_NORMAL)
  1432. {
  1433. if (I_ISTRIP(tty))
  1434. {
  1435. c &= 0x7f;
  1436. }
  1437. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1438. {
  1439. c = tolower(c);
  1440. }
  1441. n_tty_receive_char(tty, c);
  1442. }
  1443. else
  1444. {
  1445. //n_tty_receive_char_flagged(tty, c, flag);
  1446. }
  1447. }
  1448. static void n_tty_receive_buf_real_raw(struct tty_struct *tty, char *cp, int count)
  1449. {
  1450. struct n_tty_data *ldata = tty->disc_data;
  1451. size_t n = 0, head = 0;
  1452. head = ldata->read_head & (RT_TTY_BUF - 1);
  1453. n = min(count, RT_TTY_BUF - head);
  1454. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1455. ldata->read_head += n;
  1456. cp += n;
  1457. count -= n;
  1458. head = ldata->read_head & (RT_TTY_BUF - 1);
  1459. n = min(count, RT_TTY_BUF - head);
  1460. rt_memcpy(read_buf_addr(ldata, head), cp, n);
  1461. ldata->read_head += n;
  1462. }
  1463. static void n_tty_receive_buf_raw(struct tty_struct *tty, char *cp, int count)
  1464. {
  1465. struct n_tty_data *ldata = tty->disc_data;
  1466. char flag = TTY_NORMAL;
  1467. while (count--)
  1468. {
  1469. if (flag == TTY_NORMAL)
  1470. {
  1471. put_tty_queue(*cp++, ldata);
  1472. }
  1473. }
  1474. }
  1475. static void n_tty_receive_buf_standard(struct tty_struct *tty, char *cp, int count)
  1476. {
  1477. struct n_tty_data *ldata = tty->disc_data;
  1478. char flag = TTY_NORMAL;
  1479. while (count--)
  1480. {
  1481. char c = *cp++;
  1482. if (I_ISTRIP(tty))
  1483. {
  1484. c &= 0x7f;
  1485. }
  1486. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1487. {
  1488. c = tolower(c);
  1489. }
  1490. if (L_EXTPROC(tty))
  1491. {
  1492. put_tty_queue(c, ldata);
  1493. continue;
  1494. }
  1495. if (!test_bit(c, (int *)ldata->char_map))
  1496. {
  1497. n_tty_receive_char_inline(tty, c);
  1498. }
  1499. else if (n_tty_receive_char_special(tty, c) && count)
  1500. {
  1501. n_tty_receive_char_lnext(tty, *cp++, flag);
  1502. count--;
  1503. }
  1504. }
  1505. }
  1506. static inline void n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
  1507. {
  1508. struct n_tty_data *ldata = tty->disc_data;
  1509. if (L_ECHO(tty))
  1510. {
  1511. finish_erasing(ldata);
  1512. /* Record the column of first canon char. */
  1513. if (ldata->canon_head == ldata->read_head)
  1514. {
  1515. echo_set_canon_col(ldata);
  1516. }
  1517. echo_char(c, tty);
  1518. commit_echoes(tty);
  1519. }
  1520. put_tty_queue(c, ldata);
  1521. }
  1522. static void n_tty_receive_buf_fast(struct tty_struct *tty, char *cp, int count)
  1523. {
  1524. struct n_tty_data *ldata = tty->disc_data;
  1525. char flag = TTY_NORMAL;
  1526. while (count--)
  1527. {
  1528. unsigned char c = *cp++;
  1529. if (!test_bit(c, (int *)ldata->char_map))
  1530. {
  1531. n_tty_receive_char_fast(tty, c);
  1532. }
  1533. else if (n_tty_receive_char_special(tty, c) && count)
  1534. {
  1535. n_tty_receive_char_lnext(tty, *cp++, flag);
  1536. count--;
  1537. }
  1538. }
  1539. }
  1540. static void __receive_buf(struct tty_struct *tty, char *cp, int count)
  1541. {
  1542. struct n_tty_data *ldata = tty->disc_data;
  1543. rt_bool_t preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1544. if (ldata->real_raw)
  1545. {
  1546. n_tty_receive_buf_real_raw(tty, cp, count);
  1547. }
  1548. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1549. {
  1550. n_tty_receive_buf_raw(tty, cp, count);
  1551. }
  1552. else
  1553. {
  1554. if (!preops && !I_PARMRK(tty))
  1555. {
  1556. n_tty_receive_buf_fast(tty, cp, count);
  1557. }
  1558. else
  1559. {
  1560. n_tty_receive_buf_standard(tty, cp, count);
  1561. }
  1562. flush_echoes(tty);
  1563. }
  1564. if (ldata->icanon && !L_EXTPROC(tty))
  1565. return;
  1566. /* publish read_head to consumer */
  1567. ldata->commit_head = ldata->read_head;
  1568. if (read_cnt(ldata))
  1569. {
  1570. tty_wakeup_check(tty);
  1571. }
  1572. }
  1573. int n_tty_receive_buf(struct tty_struct *tty,char *cp, int count)
  1574. {
  1575. int size = 0;
  1576. struct n_tty_data *ldata = tty->disc_data;
  1577. int room = 0, n = 0, rcvd = 0, overflow = 0;
  1578. size = count;
  1579. while(1)
  1580. {
  1581. size_t tail = ldata->read_tail;
  1582. room = RT_TTY_BUF - (ldata->read_head - tail);
  1583. if (I_PARMRK(tty))
  1584. {
  1585. room = (room +2)/3;
  1586. }
  1587. room--;
  1588. if (room <= 0)
  1589. {
  1590. overflow = ldata->icanon && ldata->canon_head == tail;
  1591. if (overflow && room < 0)
  1592. {
  1593. ldata->read_head--;
  1594. }
  1595. room = overflow;
  1596. }
  1597. else
  1598. {
  1599. overflow = 0;
  1600. }
  1601. n = min(size, room);
  1602. if (!n)
  1603. {
  1604. break;
  1605. }
  1606. if (!overflow)
  1607. {
  1608. __receive_buf(tty, cp, n);
  1609. }
  1610. cp += n;
  1611. size -= n;
  1612. rcvd += n;
  1613. }
  1614. return count - size;
  1615. }
  1616. /**
  1617. * job_control - check job control
  1618. * @tty: tty
  1619. * @file: file handle
  1620. *
  1621. * Perform job control management checks on this file/tty descriptor
  1622. * and if appropriate send any needed signals and return a negative
  1623. * error code if action should be taken.
  1624. *
  1625. * Locking: redirected write test is safe
  1626. * current->signal->tty check is safe
  1627. * ctrl_lock to safely reference tty->pgrp
  1628. */
  1629. static int job_control(struct tty_struct *tty)
  1630. {
  1631. return __tty_check_change(tty, SIGTTIN);
  1632. }
  1633. static int n_tty_read(struct dfs_fd *fd, void *buf, size_t count)
  1634. {
  1635. int level = 0;
  1636. char *b = (char *)buf;
  1637. struct tty_struct *tty = RT_NULL;
  1638. struct rt_lwp *lwp = RT_NULL;
  1639. struct rt_wqueue *wq = RT_NULL;
  1640. int wait_ret = 0;
  1641. int retval = 0;
  1642. int c = 0;
  1643. level = rt_hw_interrupt_disable();
  1644. tty = (struct tty_struct *)fd->fnode->data;
  1645. RT_ASSERT(tty != RT_NULL);
  1646. c = job_control(tty);
  1647. if (c < 0)
  1648. {
  1649. rt_hw_interrupt_enable(level);
  1650. return c;
  1651. }
  1652. struct n_tty_data *ldata = tty->disc_data;
  1653. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  1654. wq = wait_queue_get(lwp, tty);
  1655. while(count)
  1656. {
  1657. if ((!input_available_p(tty, 0)))
  1658. {
  1659. if (fd->flags & O_NONBLOCK)
  1660. {
  1661. retval = -EAGAIN;
  1662. break;
  1663. }
  1664. wait_ret = rt_wqueue_wait_interruptible(wq, 0, RT_WAITING_FOREVER);
  1665. if (wait_ret != 0)
  1666. {
  1667. break;
  1668. }
  1669. }
  1670. if (ldata->icanon && !L_EXTPROC(tty))
  1671. {
  1672. retval = canon_copy_from_read_buf(tty, b, count);
  1673. }
  1674. else
  1675. {
  1676. retval = copy_from_read_buf(tty, b, count);
  1677. }
  1678. if (retval >= 1)
  1679. {
  1680. break;
  1681. }
  1682. }
  1683. rt_hw_interrupt_enable(level);
  1684. return retval;
  1685. }
  1686. static int n_tty_write(struct dfs_fd *fd, const void *buf, size_t count)
  1687. {
  1688. int retval = 0;
  1689. char *b = (char *)buf;
  1690. int c = 0;
  1691. struct tty_struct *tty = RT_NULL;
  1692. tty = (struct tty_struct *)fd->fnode->data;
  1693. RT_ASSERT(tty != RT_NULL);
  1694. retval = tty_check_change(tty);
  1695. if (retval)
  1696. {
  1697. return retval;
  1698. }
  1699. process_echoes(tty);
  1700. retval = count;
  1701. while(1)
  1702. {
  1703. if (O_OPOST(tty))
  1704. {
  1705. while (count > 0)
  1706. {
  1707. ssize_t num = process_output_block(tty, b, count);
  1708. if (num < 0)
  1709. {
  1710. if (num == -EAGAIN)
  1711. {
  1712. break;
  1713. }
  1714. retval = num;
  1715. goto break_out;
  1716. }
  1717. b += num;
  1718. count -= num;
  1719. if (count == 0)
  1720. {
  1721. break;
  1722. }
  1723. c = *b;
  1724. if (process_output(c, tty) < 0)
  1725. {
  1726. break;
  1727. }
  1728. b++;
  1729. count--;
  1730. }
  1731. retval -= count;
  1732. }
  1733. else
  1734. {
  1735. while (count > 0)
  1736. {
  1737. c = rt_device_write((rt_device_t)tty, -1, b, count);
  1738. if (c < 0)
  1739. {
  1740. retval = c;
  1741. goto break_out;
  1742. }
  1743. b += c;
  1744. count -= c;
  1745. }
  1746. retval -= count;
  1747. }
  1748. if (!count)
  1749. {
  1750. break;
  1751. }
  1752. if (fd->flags & O_NONBLOCK)
  1753. {
  1754. break;
  1755. }
  1756. }
  1757. break_out:
  1758. return retval;
  1759. }
  1760. static int n_tty_flush(struct dfs_fd *fd)
  1761. {
  1762. return 0;
  1763. }
  1764. static int n_tty_lseek(struct dfs_fd *fd, off_t offset)
  1765. {
  1766. return 0;
  1767. }
  1768. static int n_tty_getdents(struct dfs_fd *fd, struct dirent *dirp, uint32_t count)
  1769. {
  1770. return 0;
  1771. }
  1772. static int n_tty_poll(struct dfs_fd *fd, struct rt_pollreq *req)
  1773. {
  1774. rt_base_t level = 0;
  1775. int mask = POLLOUT;
  1776. struct tty_struct *tty = RT_NULL;
  1777. struct rt_wqueue *wq = RT_NULL;
  1778. struct rt_lwp *lwp = RT_NULL;
  1779. tty = (struct tty_struct *)fd->fnode->data;
  1780. RT_ASSERT(tty != RT_NULL);
  1781. RT_ASSERT(tty->init_flag == TTY_INIT_FLAG_INITED);
  1782. lwp = (struct rt_lwp *)(rt_thread_self()->lwp);
  1783. wq = wait_queue_get(lwp, tty);
  1784. rt_poll_add(wq, req);
  1785. level = rt_hw_interrupt_disable();
  1786. if (input_available_p(tty, 1))
  1787. {
  1788. mask |= POLLIN;
  1789. }
  1790. rt_hw_interrupt_enable(level);
  1791. return mask;
  1792. }
  1793. static struct tty_ldisc_ops n_tty_ops = {
  1794. "n_tty",
  1795. 0,
  1796. n_tty_open,
  1797. n_tty_close,
  1798. n_tty_ioctl,
  1799. n_tty_read,
  1800. n_tty_write,
  1801. n_tty_flush,
  1802. n_tty_lseek,
  1803. n_tty_getdents,
  1804. n_tty_poll,
  1805. n_tty_set_termios,
  1806. n_tty_receive_buf,
  1807. 0,
  1808. };
  1809. void n_tty_init(void)
  1810. {
  1811. tty_register_ldisc(N_TTY, &n_tty_ops);
  1812. }