n_tty.c 50 KB

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