n_tty.c 50 KB

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