stty.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /* vi: set sw=4 ts=4: */
  2. /* stty -- change and print terminal line settings
  3. Copyright (C) 1990-1999 Free Software Foundation, Inc.
  4. Licensed under GPLv2 or later, see file LICENSE in this source tree.
  5. */
  6. /* Usage: stty [-ag] [-F device] [setting...]
  7. Options:
  8. -a Write all current settings to stdout in human-readable form.
  9. -g Write all current settings to stdout in stty-readable form.
  10. -F Open and use the specified device instead of stdin
  11. If no args are given, write to stdout the baud rate and settings that
  12. have been changed from their defaults. Mode reading and changes
  13. are done on the specified device, or stdin if none was specified.
  14. David MacKenzie <djm@gnu.ai.mit.edu>
  15. Special for busybox ported by Vladimir Oleynik <dzo@simtreas.ru> 2001
  16. */
  17. #include "libbb.h"
  18. #ifndef _POSIX_VDISABLE
  19. # define _POSIX_VDISABLE ((unsigned char) 0)
  20. #endif
  21. #define Control(c) ((c) & 0x1f)
  22. /* Canonical values for control characters */
  23. #ifndef CINTR
  24. # define CINTR Control('c')
  25. #endif
  26. #ifndef CQUIT
  27. # define CQUIT 28
  28. #endif
  29. #ifndef CERASE
  30. # define CERASE 127
  31. #endif
  32. #ifndef CKILL
  33. # define CKILL Control('u')
  34. #endif
  35. #ifndef CEOF
  36. # define CEOF Control('d')
  37. #endif
  38. #ifndef CEOL
  39. # define CEOL _POSIX_VDISABLE
  40. #endif
  41. #ifndef CSTART
  42. # define CSTART Control('q')
  43. #endif
  44. #ifndef CSTOP
  45. # define CSTOP Control('s')
  46. #endif
  47. #ifndef CSUSP
  48. # define CSUSP Control('z')
  49. #endif
  50. #if defined(VEOL2) && !defined(CEOL2)
  51. # define CEOL2 _POSIX_VDISABLE
  52. #endif
  53. /* ISC renamed swtch to susp for termios, but we'll accept either name */
  54. #if defined(VSUSP) && !defined(VSWTCH)
  55. # define VSWTCH VSUSP
  56. # define CSWTCH CSUSP
  57. #endif
  58. #if defined(VSWTCH) && !defined(CSWTCH)
  59. # define CSWTCH _POSIX_VDISABLE
  60. #endif
  61. /* SunOS 5.3 loses (^Z doesn't work) if 'swtch' is the same as 'susp'.
  62. So the default is to disable 'swtch.' */
  63. #if defined(__sparc__) && defined(__svr4__)
  64. # undef CSWTCH
  65. # define CSWTCH _POSIX_VDISABLE
  66. #endif
  67. #if defined(VWERSE) && !defined(VWERASE) /* AIX-3.2.5 */
  68. # define VWERASE VWERSE
  69. #endif
  70. #if defined(VDSUSP) && !defined(CDSUSP)
  71. # define CDSUSP Control('y')
  72. #endif
  73. #if !defined(VREPRINT) && defined(VRPRNT) /* Irix 4.0.5 */
  74. # define VREPRINT VRPRNT
  75. #endif
  76. #if defined(VREPRINT) && !defined(CRPRNT)
  77. # define CRPRNT Control('r')
  78. #endif
  79. #if defined(VWERASE) && !defined(CWERASE)
  80. # define CWERASE Control('w')
  81. #endif
  82. #if defined(VLNEXT) && !defined(CLNEXT)
  83. # define CLNEXT Control('v')
  84. #endif
  85. #if defined(VDISCARD) && !defined(VFLUSHO)
  86. # define VFLUSHO VDISCARD
  87. #endif
  88. #if defined(VFLUSH) && !defined(VFLUSHO) /* Ultrix 4.2 */
  89. # define VFLUSHO VFLUSH
  90. #endif
  91. #if defined(CTLECH) && !defined(ECHOCTL) /* Ultrix 4.3 */
  92. # define ECHOCTL CTLECH
  93. #endif
  94. #if defined(TCTLECH) && !defined(ECHOCTL) /* Ultrix 4.2 */
  95. # define ECHOCTL TCTLECH
  96. #endif
  97. #if defined(CRTKIL) && !defined(ECHOKE) /* Ultrix 4.2 and 4.3 */
  98. # define ECHOKE CRTKIL
  99. #endif
  100. #if defined(VFLUSHO) && !defined(CFLUSHO)
  101. # define CFLUSHO Control('o')
  102. #endif
  103. #if defined(VSTATUS) && !defined(CSTATUS)
  104. # define CSTATUS Control('t')
  105. #endif
  106. /* Save us from #ifdef forest plague */
  107. #ifndef BSDLY
  108. # define BSDLY 0
  109. #endif
  110. #ifndef CIBAUD
  111. # define CIBAUD 0
  112. #endif
  113. #ifndef CRDLY
  114. # define CRDLY 0
  115. #endif
  116. #ifndef CRTSCTS
  117. # define CRTSCTS 0
  118. #endif
  119. #ifndef ECHOCTL
  120. # define ECHOCTL 0
  121. #endif
  122. #ifndef ECHOKE
  123. # define ECHOKE 0
  124. #endif
  125. #ifndef ECHOPRT
  126. # define ECHOPRT 0
  127. #endif
  128. #ifndef FFDLY
  129. # define FFDLY 0
  130. #endif
  131. #ifndef IEXTEN
  132. # define IEXTEN 0
  133. #endif
  134. #ifndef IMAXBEL
  135. # define IMAXBEL 0
  136. #endif
  137. #ifndef IUCLC
  138. # define IUCLC 0
  139. #endif
  140. #ifndef IXANY
  141. # define IXANY 0
  142. #endif
  143. #ifndef NLDLY
  144. # define NLDLY 0
  145. #endif
  146. #ifndef OCRNL
  147. # define OCRNL 0
  148. #endif
  149. #ifndef OFDEL
  150. # define OFDEL 0
  151. #endif
  152. #ifndef OFILL
  153. # define OFILL 0
  154. #endif
  155. #ifndef OLCUC
  156. # define OLCUC 0
  157. #endif
  158. #ifndef ONLCR
  159. # define ONLCR 0
  160. #endif
  161. #ifndef ONLRET
  162. # define ONLRET 0
  163. #endif
  164. #ifndef ONOCR
  165. # define ONOCR 0
  166. #endif
  167. #ifndef OXTABS
  168. # define OXTABS 0
  169. #endif
  170. #ifndef TABDLY
  171. # define TABDLY 0
  172. #endif
  173. #ifndef TAB1
  174. # define TAB1 0
  175. #endif
  176. #ifndef TAB2
  177. # define TAB2 0
  178. #endif
  179. #ifndef TOSTOP
  180. # define TOSTOP 0
  181. #endif
  182. #ifndef VDSUSP
  183. # define VDSUSP 0
  184. #endif
  185. #ifndef VEOL2
  186. # define VEOL2 0
  187. #endif
  188. #ifndef VFLUSHO
  189. # define VFLUSHO 0
  190. #endif
  191. #ifndef VLNEXT
  192. # define VLNEXT 0
  193. #endif
  194. #ifndef VREPRINT
  195. # define VREPRINT 0
  196. #endif
  197. #ifndef VSTATUS
  198. # define VSTATUS 0
  199. #endif
  200. #ifndef VSWTCH
  201. # define VSWTCH 0
  202. #endif
  203. #ifndef VTDLY
  204. # define VTDLY 0
  205. #endif
  206. #ifndef VWERASE
  207. # define VWERASE 0
  208. #endif
  209. #ifndef XCASE
  210. # define XCASE 0
  211. #endif
  212. /* Which speeds to set */
  213. enum speed_setting {
  214. input_speed, output_speed, both_speeds
  215. };
  216. /* Which member(s) of 'struct termios' a mode uses */
  217. enum {
  218. /* Do NOT change the order or values, as mode_type_flag()
  219. * depends on them */
  220. control, input, output, local, combination
  221. };
  222. /* Flags for 'struct mode_info' */
  223. #define SANE_SET 1 /* Set in 'sane' mode */
  224. #define SANE_UNSET 2 /* Unset in 'sane' mode */
  225. #define REV 4 /* Can be turned off by prepending '-' */
  226. #define OMIT 8 /* Don't display value */
  227. /* Each mode.
  228. * This structure should be kept as small as humanly possible.
  229. */
  230. struct mode_info {
  231. const uint8_t type; /* Which structure element to change */
  232. const uint8_t flags; /* Setting and display options */
  233. /* only these values are ever used, so... */
  234. #if (CSIZE | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY) < 0x100
  235. const uint8_t mask;
  236. #elif (CSIZE | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY) < 0x10000
  237. const uint16_t mask;
  238. #else
  239. const tcflag_t mask; /* Other bits to turn off for this mode */
  240. #endif
  241. /* was using short here, but ppc32 was unhappy */
  242. const tcflag_t bits; /* Bits to set for this mode */
  243. };
  244. enum {
  245. /* Must match mode_name[] and mode_info[] order! */
  246. IDX_evenp = 0,
  247. IDX_parity,
  248. IDX_oddp,
  249. IDX_nl,
  250. IDX_ek,
  251. IDX_sane,
  252. IDX_cooked,
  253. IDX_raw,
  254. IDX_pass8,
  255. IDX_litout,
  256. IDX_cbreak,
  257. IDX_crt,
  258. IDX_dec,
  259. #if IXANY
  260. IDX_decctlq,
  261. #endif
  262. #if TABDLY || OXTABS
  263. IDX_tabs,
  264. #endif
  265. #if XCASE && IUCLC && OLCUC
  266. IDX_lcase,
  267. IDX_LCASE,
  268. #endif
  269. };
  270. #define MI_ENTRY(N,T,F,B,M) N "\0"
  271. /* Mode names given on command line */
  272. static const char mode_name[] =
  273. MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 )
  274. MI_ENTRY("parity", combination, REV | OMIT, 0, 0 )
  275. MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 )
  276. MI_ENTRY("nl", combination, REV | OMIT, 0, 0 )
  277. MI_ENTRY("ek", combination, OMIT, 0, 0 )
  278. MI_ENTRY("sane", combination, OMIT, 0, 0 )
  279. MI_ENTRY("cooked", combination, REV | OMIT, 0, 0 )
  280. MI_ENTRY("raw", combination, REV | OMIT, 0, 0 )
  281. MI_ENTRY("pass8", combination, REV | OMIT, 0, 0 )
  282. MI_ENTRY("litout", combination, REV | OMIT, 0, 0 )
  283. MI_ENTRY("cbreak", combination, REV | OMIT, 0, 0 )
  284. MI_ENTRY("crt", combination, OMIT, 0, 0 )
  285. MI_ENTRY("dec", combination, OMIT, 0, 0 )
  286. #if IXANY
  287. MI_ENTRY("decctlq", combination, REV | OMIT, 0, 0 )
  288. #endif
  289. #if TABDLY || OXTABS
  290. MI_ENTRY("tabs", combination, REV | OMIT, 0, 0 )
  291. #endif
  292. #if XCASE && IUCLC && OLCUC
  293. MI_ENTRY("lcase", combination, REV | OMIT, 0, 0 )
  294. MI_ENTRY("LCASE", combination, REV | OMIT, 0, 0 )
  295. #endif
  296. MI_ENTRY("parenb", control, REV, PARENB, 0 )
  297. MI_ENTRY("parodd", control, REV, PARODD, 0 )
  298. MI_ENTRY("cs5", control, 0, CS5, CSIZE)
  299. MI_ENTRY("cs6", control, 0, CS6, CSIZE)
  300. MI_ENTRY("cs7", control, 0, CS7, CSIZE)
  301. MI_ENTRY("cs8", control, 0, CS8, CSIZE)
  302. MI_ENTRY("hupcl", control, REV, HUPCL, 0 )
  303. MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 )
  304. MI_ENTRY("cstopb", control, REV, CSTOPB, 0 )
  305. MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 )
  306. MI_ENTRY("clocal", control, REV, CLOCAL, 0 )
  307. #if CRTSCTS
  308. MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 )
  309. #endif
  310. MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 )
  311. MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 )
  312. MI_ENTRY("ignpar", input, REV, IGNPAR, 0 )
  313. MI_ENTRY("parmrk", input, REV, PARMRK, 0 )
  314. MI_ENTRY("inpck", input, REV, INPCK, 0 )
  315. MI_ENTRY("istrip", input, REV, ISTRIP, 0 )
  316. MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 )
  317. MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 )
  318. MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 )
  319. MI_ENTRY("ixon", input, REV, IXON, 0 )
  320. MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 )
  321. MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 )
  322. #if IUCLC
  323. MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
  324. #endif
  325. #if IXANY
  326. MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 )
  327. #endif
  328. #if IMAXBEL
  329. MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
  330. #endif
  331. MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
  332. #if OLCUC
  333. MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 )
  334. #endif
  335. #if OCRNL
  336. MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 )
  337. #endif
  338. #if ONLCR
  339. MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 )
  340. #endif
  341. #if ONOCR
  342. MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 )
  343. #endif
  344. #if ONLRET
  345. MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 )
  346. #endif
  347. #if OFILL
  348. MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 )
  349. #endif
  350. #if OFDEL
  351. MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 )
  352. #endif
  353. #if NLDLY
  354. MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY)
  355. MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY)
  356. #endif
  357. #if CRDLY
  358. MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY)
  359. MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY)
  360. MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY)
  361. MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY)
  362. #endif
  363. #if TABDLY
  364. MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY)
  365. # if TAB2
  366. MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY)
  367. # endif
  368. # if TAB1
  369. MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY)
  370. # endif
  371. MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY)
  372. #else
  373. # if OXTABS
  374. MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 )
  375. # endif
  376. #endif
  377. #if BSDLY
  378. MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY)
  379. MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY)
  380. #endif
  381. #if VTDLY
  382. MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY)
  383. MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY)
  384. #endif
  385. #if FFDLY
  386. MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY)
  387. MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY)
  388. #endif
  389. MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 )
  390. MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 )
  391. #if IEXTEN
  392. MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 )
  393. #endif
  394. MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 )
  395. MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 )
  396. MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 )
  397. MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 )
  398. MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 )
  399. MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 )
  400. #if XCASE
  401. MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 )
  402. #endif
  403. #if TOSTOP
  404. MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 )
  405. #endif
  406. #if ECHOPRT
  407. MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
  408. MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
  409. #endif
  410. #if ECHOCTL
  411. MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
  412. MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
  413. #endif
  414. #if ECHOKE
  415. MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
  416. MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
  417. #endif
  418. ;
  419. #undef MI_ENTRY
  420. #define MI_ENTRY(N,T,F,B,M) { T, F, M, B },
  421. static const struct mode_info mode_info[] = {
  422. /* This should be verbatim cut-n-paste copy of the above MI_ENTRYs */
  423. MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 )
  424. MI_ENTRY("parity", combination, REV | OMIT, 0, 0 )
  425. MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 )
  426. MI_ENTRY("nl", combination, REV | OMIT, 0, 0 )
  427. MI_ENTRY("ek", combination, OMIT, 0, 0 )
  428. MI_ENTRY("sane", combination, OMIT, 0, 0 )
  429. MI_ENTRY("cooked", combination, REV | OMIT, 0, 0 )
  430. MI_ENTRY("raw", combination, REV | OMIT, 0, 0 )
  431. MI_ENTRY("pass8", combination, REV | OMIT, 0, 0 )
  432. MI_ENTRY("litout", combination, REV | OMIT, 0, 0 )
  433. MI_ENTRY("cbreak", combination, REV | OMIT, 0, 0 )
  434. MI_ENTRY("crt", combination, OMIT, 0, 0 )
  435. MI_ENTRY("dec", combination, OMIT, 0, 0 )
  436. #if IXANY
  437. MI_ENTRY("decctlq", combination, REV | OMIT, 0, 0 )
  438. #endif
  439. #if TABDLY || OXTABS
  440. MI_ENTRY("tabs", combination, REV | OMIT, 0, 0 )
  441. #endif
  442. #if XCASE && IUCLC && OLCUC
  443. MI_ENTRY("lcase", combination, REV | OMIT, 0, 0 )
  444. MI_ENTRY("LCASE", combination, REV | OMIT, 0, 0 )
  445. #endif
  446. MI_ENTRY("parenb", control, REV, PARENB, 0 )
  447. MI_ENTRY("parodd", control, REV, PARODD, 0 )
  448. MI_ENTRY("cs5", control, 0, CS5, CSIZE)
  449. MI_ENTRY("cs6", control, 0, CS6, CSIZE)
  450. MI_ENTRY("cs7", control, 0, CS7, CSIZE)
  451. MI_ENTRY("cs8", control, 0, CS8, CSIZE)
  452. MI_ENTRY("hupcl", control, REV, HUPCL, 0 )
  453. MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 )
  454. MI_ENTRY("cstopb", control, REV, CSTOPB, 0 )
  455. MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 )
  456. MI_ENTRY("clocal", control, REV, CLOCAL, 0 )
  457. #if CRTSCTS
  458. MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 )
  459. #endif
  460. MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 )
  461. MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 )
  462. MI_ENTRY("ignpar", input, REV, IGNPAR, 0 )
  463. MI_ENTRY("parmrk", input, REV, PARMRK, 0 )
  464. MI_ENTRY("inpck", input, REV, INPCK, 0 )
  465. MI_ENTRY("istrip", input, REV, ISTRIP, 0 )
  466. MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 )
  467. MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 )
  468. MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 )
  469. MI_ENTRY("ixon", input, REV, IXON, 0 )
  470. MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 )
  471. MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 )
  472. #if IUCLC
  473. MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
  474. #endif
  475. #if IXANY
  476. MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 )
  477. #endif
  478. #if IMAXBEL
  479. MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
  480. #endif
  481. MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
  482. #if OLCUC
  483. MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 )
  484. #endif
  485. #if OCRNL
  486. MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 )
  487. #endif
  488. #if ONLCR
  489. MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 )
  490. #endif
  491. #if ONOCR
  492. MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 )
  493. #endif
  494. #if ONLRET
  495. MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 )
  496. #endif
  497. #if OFILL
  498. MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 )
  499. #endif
  500. #if OFDEL
  501. MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 )
  502. #endif
  503. #if NLDLY
  504. MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY)
  505. MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY)
  506. #endif
  507. #if CRDLY
  508. MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY)
  509. MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY)
  510. MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY)
  511. MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY)
  512. #endif
  513. #if TABDLY
  514. MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY)
  515. # if TAB2
  516. MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY)
  517. # endif
  518. # if TAB1
  519. MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY)
  520. # endif
  521. MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY)
  522. #else
  523. # if OXTABS
  524. MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 )
  525. # endif
  526. #endif
  527. #if BSDLY
  528. MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY)
  529. MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY)
  530. #endif
  531. #if VTDLY
  532. MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY)
  533. MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY)
  534. #endif
  535. #if FFDLY
  536. MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY)
  537. MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY)
  538. #endif
  539. MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 )
  540. MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 )
  541. #if IEXTEN
  542. MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 )
  543. #endif
  544. MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 )
  545. MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 )
  546. MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 )
  547. MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 )
  548. MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 )
  549. MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 )
  550. #if XCASE
  551. MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 )
  552. #endif
  553. #if TOSTOP
  554. MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 )
  555. #endif
  556. #if ECHOPRT
  557. MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
  558. MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
  559. #endif
  560. #if ECHOCTL
  561. MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
  562. MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
  563. #endif
  564. #if ECHOKE
  565. MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
  566. MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
  567. #endif
  568. };
  569. enum {
  570. NUM_mode_info = ARRAY_SIZE(mode_info)
  571. };
  572. /* Control characters */
  573. struct control_info {
  574. const uint8_t saneval; /* Value to set for 'stty sane' */
  575. const uint8_t offset; /* Offset in c_cc */
  576. };
  577. enum {
  578. /* Must match control_name[] and control_info[] order! */
  579. CIDX_intr = 0,
  580. CIDX_quit,
  581. CIDX_erase,
  582. CIDX_kill,
  583. CIDX_eof,
  584. CIDX_eol,
  585. #if VEOL2
  586. CIDX_eol2,
  587. #endif
  588. #if VSWTCH
  589. CIDX_swtch,
  590. #endif
  591. CIDX_start,
  592. CIDX_stop,
  593. CIDX_susp,
  594. #if VDSUSP
  595. CIDX_dsusp,
  596. #endif
  597. #if VREPRINT
  598. CIDX_rprnt,
  599. #endif
  600. #if VWERASE
  601. CIDX_werase,
  602. #endif
  603. #if VLNEXT
  604. CIDX_lnext,
  605. #endif
  606. #if VFLUSHO
  607. CIDX_flush,
  608. #endif
  609. #if VSTATUS
  610. CIDX_status,
  611. #endif
  612. CIDX_min,
  613. CIDX_time,
  614. };
  615. #define CI_ENTRY(n,s,o) n "\0"
  616. /* Name given on command line */
  617. static const char control_name[] =
  618. CI_ENTRY("intr", CINTR, VINTR )
  619. CI_ENTRY("quit", CQUIT, VQUIT )
  620. CI_ENTRY("erase", CERASE, VERASE )
  621. CI_ENTRY("kill", CKILL, VKILL )
  622. CI_ENTRY("eof", CEOF, VEOF )
  623. CI_ENTRY("eol", CEOL, VEOL )
  624. #if VEOL2
  625. CI_ENTRY("eol2", CEOL2, VEOL2 )
  626. #endif
  627. #if VSWTCH
  628. CI_ENTRY("swtch", CSWTCH, VSWTCH )
  629. #endif
  630. CI_ENTRY("start", CSTART, VSTART )
  631. CI_ENTRY("stop", CSTOP, VSTOP )
  632. CI_ENTRY("susp", CSUSP, VSUSP )
  633. #if VDSUSP
  634. CI_ENTRY("dsusp", CDSUSP, VDSUSP )
  635. #endif
  636. #if VREPRINT
  637. CI_ENTRY("rprnt", CRPRNT, VREPRINT)
  638. #endif
  639. #if VWERASE
  640. CI_ENTRY("werase", CWERASE, VWERASE )
  641. #endif
  642. #if VLNEXT
  643. CI_ENTRY("lnext", CLNEXT, VLNEXT )
  644. #endif
  645. #if VFLUSHO
  646. CI_ENTRY("flush", CFLUSHO, VFLUSHO )
  647. #endif
  648. #if VSTATUS
  649. CI_ENTRY("status", CSTATUS, VSTATUS )
  650. #endif
  651. /* These must be last because of the display routines */
  652. CI_ENTRY("min", 1, VMIN )
  653. CI_ENTRY("time", 0, VTIME )
  654. ;
  655. #undef CI_ENTRY
  656. #define CI_ENTRY(n,s,o) { s, o },
  657. static const struct control_info control_info[] = {
  658. /* This should be verbatim cut-n-paste copy of the above CI_ENTRYs */
  659. CI_ENTRY("intr", CINTR, VINTR )
  660. CI_ENTRY("quit", CQUIT, VQUIT )
  661. CI_ENTRY("erase", CERASE, VERASE )
  662. CI_ENTRY("kill", CKILL, VKILL )
  663. CI_ENTRY("eof", CEOF, VEOF )
  664. CI_ENTRY("eol", CEOL, VEOL )
  665. #if VEOL2
  666. CI_ENTRY("eol2", CEOL2, VEOL2 )
  667. #endif
  668. #if VSWTCH
  669. CI_ENTRY("swtch", CSWTCH, VSWTCH )
  670. #endif
  671. CI_ENTRY("start", CSTART, VSTART )
  672. CI_ENTRY("stop", CSTOP, VSTOP )
  673. CI_ENTRY("susp", CSUSP, VSUSP )
  674. #if VDSUSP
  675. CI_ENTRY("dsusp", CDSUSP, VDSUSP )
  676. #endif
  677. #if VREPRINT
  678. CI_ENTRY("rprnt", CRPRNT, VREPRINT)
  679. #endif
  680. #if VWERASE
  681. CI_ENTRY("werase", CWERASE, VWERASE )
  682. #endif
  683. #if VLNEXT
  684. CI_ENTRY("lnext", CLNEXT, VLNEXT )
  685. #endif
  686. #if VFLUSHO
  687. CI_ENTRY("flush", CFLUSHO, VFLUSHO )
  688. #endif
  689. #if VSTATUS
  690. CI_ENTRY("status", CSTATUS, VSTATUS )
  691. #endif
  692. /* These must be last because of the display routines */
  693. CI_ENTRY("min", 1, VMIN )
  694. CI_ENTRY("time", 0, VTIME )
  695. };
  696. enum {
  697. NUM_control_info = ARRAY_SIZE(control_info)
  698. };
  699. struct globals {
  700. const char *device_name;
  701. /* The width of the screen, for output wrapping */
  702. unsigned max_col;
  703. /* Current position, to know when to wrap */
  704. unsigned current_col;
  705. char buf[10];
  706. } FIX_ALIASING;
  707. #define G (*(struct globals*)&bb_common_bufsiz1)
  708. #define INIT_G() do { \
  709. G.device_name = bb_msg_standard_input; \
  710. G.max_col = 80; \
  711. } while (0)
  712. /* Return a string that is the printable representation of character CH */
  713. /* Adapted from 'cat' by Torbjorn Granlund */
  714. static const char *visible(unsigned ch)
  715. {
  716. char *bpout = G.buf;
  717. if (ch == _POSIX_VDISABLE)
  718. return "<undef>";
  719. if (ch >= 128) {
  720. ch -= 128;
  721. *bpout++ = 'M';
  722. *bpout++ = '-';
  723. }
  724. if (ch < 32) {
  725. *bpout++ = '^';
  726. *bpout++ = ch + 64;
  727. } else if (ch < 127) {
  728. *bpout++ = ch;
  729. } else {
  730. *bpout++ = '^';
  731. *bpout++ = '?';
  732. }
  733. *bpout = '\0';
  734. return G.buf;
  735. }
  736. static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
  737. {
  738. static const uint8_t tcflag_offsets[] ALIGN1 = {
  739. offsetof(struct termios, c_cflag), /* control */
  740. offsetof(struct termios, c_iflag), /* input */
  741. offsetof(struct termios, c_oflag), /* output */
  742. offsetof(struct termios, c_lflag) /* local */
  743. };
  744. if (type <= local) {
  745. return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
  746. }
  747. return NULL;
  748. }
  749. static void set_speed_or_die(enum speed_setting type, const char *arg,
  750. struct termios *mode)
  751. {
  752. speed_t baud;
  753. baud = tty_value_to_baud(xatou(arg));
  754. if (type != output_speed) { /* either input or both */
  755. cfsetispeed(mode, baud);
  756. }
  757. if (type != input_speed) { /* either output or both */
  758. cfsetospeed(mode, baud);
  759. }
  760. }
  761. static NORETURN void perror_on_device_and_die(const char *fmt)
  762. {
  763. bb_perror_msg_and_die(fmt, G.device_name);
  764. }
  765. static void perror_on_device(const char *fmt)
  766. {
  767. bb_perror_msg(fmt, G.device_name);
  768. }
  769. /* Print format string MESSAGE and optional args.
  770. Wrap to next line first if it won't fit.
  771. Print a space first unless MESSAGE will start a new line */
  772. static void wrapf(const char *message, ...)
  773. {
  774. char buf[128];
  775. va_list args;
  776. unsigned buflen;
  777. va_start(args, message);
  778. buflen = vsnprintf(buf, sizeof(buf), message, args);
  779. va_end(args);
  780. /* We seem to be called only with suitable lengths, but check if
  781. somebody failed to adhere to this assumption just to be sure. */
  782. if (!buflen || buflen >= sizeof(buf)) return;
  783. if (G.current_col > 0) {
  784. G.current_col++;
  785. if (buf[0] != '\n') {
  786. if (G.current_col + buflen >= G.max_col) {
  787. bb_putchar('\n');
  788. G.current_col = 0;
  789. } else
  790. bb_putchar(' ');
  791. }
  792. }
  793. fputs(buf, stdout);
  794. G.current_col += buflen;
  795. if (buf[buflen-1] == '\n')
  796. G.current_col = 0;
  797. }
  798. static void newline(void)
  799. {
  800. if (G.current_col != 0)
  801. wrapf("\n");
  802. }
  803. #ifdef TIOCGWINSZ
  804. static void set_window_size(int rows, int cols)
  805. {
  806. struct winsize win = { 0, 0, 0, 0 };
  807. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
  808. if (errno != EINVAL) {
  809. goto bail;
  810. }
  811. memset(&win, 0, sizeof(win));
  812. }
  813. if (rows >= 0)
  814. win.ws_row = rows;
  815. if (cols >= 0)
  816. win.ws_col = cols;
  817. if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
  818. bail:
  819. perror_on_device("%s");
  820. }
  821. #endif
  822. static void display_window_size(int fancy)
  823. {
  824. const char *fmt_str = "%s\0%s: no size information for this device";
  825. unsigned width, height;
  826. if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
  827. if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
  828. perror_on_device(fmt_str);
  829. }
  830. } else {
  831. wrapf(fancy ? "rows %u; columns %u;" : "%u %u\n",
  832. height, width);
  833. }
  834. }
  835. static const struct suffix_mult stty_suffixes[] = {
  836. { "b", 512 },
  837. { "k", 1024 },
  838. { "B", 1024 },
  839. { "", 0 }
  840. };
  841. static const struct mode_info *find_mode(const char *name)
  842. {
  843. int i = index_in_strings(mode_name, name);
  844. return i >= 0 ? &mode_info[i] : NULL;
  845. }
  846. static const struct control_info *find_control(const char *name)
  847. {
  848. int i = index_in_strings(control_name, name);
  849. return i >= 0 ? &control_info[i] : NULL;
  850. }
  851. enum {
  852. param_need_arg = 0x80,
  853. param_line = 1 | 0x80,
  854. param_rows = 2 | 0x80,
  855. param_cols = 3 | 0x80,
  856. param_columns = 4 | 0x80,
  857. param_size = 5,
  858. param_speed = 6,
  859. param_ispeed = 7 | 0x80,
  860. param_ospeed = 8 | 0x80,
  861. };
  862. static int find_param(const char *name)
  863. {
  864. static const char params[] ALIGN1 =
  865. "line\0" /* 1 */
  866. "rows\0" /* 2 */
  867. "cols\0" /* 3 */
  868. "columns\0" /* 4 */
  869. "size\0" /* 5 */
  870. "speed\0" /* 6 */
  871. "ispeed\0"
  872. "ospeed\0";
  873. int i = index_in_strings(params, name) + 1;
  874. if (i == 0)
  875. return 0;
  876. if (i != 5 && i != 6)
  877. i |= 0x80;
  878. return i;
  879. }
  880. static int recover_mode(const char *arg, struct termios *mode)
  881. {
  882. int i, n;
  883. unsigned chr;
  884. unsigned long iflag, oflag, cflag, lflag;
  885. /* Scan into temporaries since it is too much trouble to figure out
  886. the right format for 'tcflag_t' */
  887. if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
  888. &iflag, &oflag, &cflag, &lflag, &n) != 4)
  889. return 0;
  890. mode->c_iflag = iflag;
  891. mode->c_oflag = oflag;
  892. mode->c_cflag = cflag;
  893. mode->c_lflag = lflag;
  894. arg += n;
  895. for (i = 0; i < NCCS; ++i) {
  896. if (sscanf(arg, ":%x%n", &chr, &n) != 1)
  897. return 0;
  898. mode->c_cc[i] = chr;
  899. arg += n;
  900. }
  901. /* Fail if there are too many fields */
  902. if (*arg != '\0')
  903. return 0;
  904. return 1;
  905. }
  906. static void display_recoverable(const struct termios *mode,
  907. int UNUSED_PARAM dummy)
  908. {
  909. int i;
  910. printf("%lx:%lx:%lx:%lx",
  911. (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
  912. (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
  913. for (i = 0; i < NCCS; ++i)
  914. printf(":%x", (unsigned int) mode->c_cc[i]);
  915. bb_putchar('\n');
  916. }
  917. static void display_speed(const struct termios *mode, int fancy)
  918. {
  919. //____________________ 01234567 8 9
  920. const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
  921. unsigned long ispeed, ospeed;
  922. ospeed = ispeed = cfgetispeed(mode);
  923. if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
  924. ispeed = ospeed; /* in case ispeed was 0 */
  925. //________ 0123 4 5 6 7 8 9
  926. fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
  927. }
  928. if (fancy) fmt_str += 9;
  929. wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
  930. }
  931. static void do_display(const struct termios *mode, int all)
  932. {
  933. int i;
  934. tcflag_t *bitsp;
  935. unsigned long mask;
  936. int prev_type = control;
  937. display_speed(mode, 1);
  938. if (all)
  939. display_window_size(1);
  940. #ifdef HAVE_C_LINE
  941. wrapf("line = %u;\n", mode->c_line);
  942. #else
  943. newline();
  944. #endif
  945. for (i = 0; i != CIDX_min; ++i) {
  946. /* If swtch is the same as susp, don't print both */
  947. #if VSWTCH == VSUSP
  948. if (i == CIDX_swtch)
  949. continue;
  950. #endif
  951. /* If eof uses the same slot as min, only print whichever applies */
  952. #if VEOF == VMIN
  953. if (!(mode->c_lflag & ICANON)
  954. && (i == CIDX_eof || i == CIDX_eol)
  955. ) {
  956. continue;
  957. }
  958. #endif
  959. wrapf("%s = %s;", nth_string(control_name, i),
  960. visible(mode->c_cc[control_info[i].offset]));
  961. }
  962. #if VEOF == VMIN
  963. if ((mode->c_lflag & ICANON) == 0)
  964. #endif
  965. wrapf("min = %u; time = %u;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
  966. newline();
  967. for (i = 0; i < NUM_mode_info; ++i) {
  968. if (mode_info[i].flags & OMIT)
  969. continue;
  970. if (mode_info[i].type != prev_type) {
  971. newline();
  972. prev_type = mode_info[i].type;
  973. }
  974. bitsp = mode_type_flag(mode_info[i].type, mode);
  975. mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
  976. if ((*bitsp & mask) == mode_info[i].bits) {
  977. if (all || (mode_info[i].flags & SANE_UNSET))
  978. wrapf("-%s"+1, nth_string(mode_name, i));
  979. } else {
  980. if ((all && mode_info[i].flags & REV)
  981. || (!all && (mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV))
  982. ) {
  983. wrapf("-%s", nth_string(mode_name, i));
  984. }
  985. }
  986. }
  987. newline();
  988. }
  989. static void sane_mode(struct termios *mode)
  990. {
  991. int i;
  992. tcflag_t *bitsp;
  993. for (i = 0; i < NUM_control_info; ++i) {
  994. #if VMIN == VEOF
  995. if (i == CIDX_min)
  996. break;
  997. #endif
  998. mode->c_cc[control_info[i].offset] = control_info[i].saneval;
  999. }
  1000. for (i = 0; i < NUM_mode_info; ++i) {
  1001. if (mode_info[i].flags & SANE_SET) {
  1002. bitsp = mode_type_flag(mode_info[i].type, mode);
  1003. *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
  1004. | mode_info[i].bits;
  1005. } else if (mode_info[i].flags & SANE_UNSET) {
  1006. bitsp = mode_type_flag(mode_info[i].type, mode);
  1007. *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
  1008. & ~mode_info[i].bits;
  1009. }
  1010. }
  1011. }
  1012. static void set_mode(const struct mode_info *info, int reversed,
  1013. struct termios *mode)
  1014. {
  1015. tcflag_t *bitsp;
  1016. bitsp = mode_type_flag(info->type, mode);
  1017. if (bitsp) {
  1018. if (reversed)
  1019. *bitsp = *bitsp & ~info->mask & ~info->bits;
  1020. else
  1021. *bitsp = (*bitsp & ~info->mask) | info->bits;
  1022. return;
  1023. }
  1024. /* Combination mode */
  1025. if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
  1026. if (reversed)
  1027. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  1028. else
  1029. mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
  1030. } else if (info == &mode_info[IDX_oddp]) {
  1031. if (reversed)
  1032. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  1033. else
  1034. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
  1035. } else if (info == &mode_info[IDX_nl]) {
  1036. if (reversed) {
  1037. mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
  1038. mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
  1039. } else {
  1040. mode->c_iflag = mode->c_iflag & ~ICRNL;
  1041. if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
  1042. }
  1043. } else if (info == &mode_info[IDX_ek]) {
  1044. mode->c_cc[VERASE] = CERASE;
  1045. mode->c_cc[VKILL] = CKILL;
  1046. } else if (info == &mode_info[IDX_sane]) {
  1047. sane_mode(mode);
  1048. } else if (info == &mode_info[IDX_cbreak]) {
  1049. if (reversed)
  1050. mode->c_lflag |= ICANON;
  1051. else
  1052. mode->c_lflag &= ~ICANON;
  1053. } else if (info == &mode_info[IDX_pass8]) {
  1054. if (reversed) {
  1055. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  1056. mode->c_iflag |= ISTRIP;
  1057. } else {
  1058. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  1059. mode->c_iflag &= ~ISTRIP;
  1060. }
  1061. } else if (info == &mode_info[IDX_litout]) {
  1062. if (reversed) {
  1063. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  1064. mode->c_iflag |= ISTRIP;
  1065. mode->c_oflag |= OPOST;
  1066. } else {
  1067. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  1068. mode->c_iflag &= ~ISTRIP;
  1069. mode->c_oflag &= ~OPOST;
  1070. }
  1071. } else if (info == &mode_info[IDX_raw] || info == &mode_info[IDX_cooked]) {
  1072. if ((info == &mode_info[IDX_raw] && reversed)
  1073. || (info == &mode_info[IDX_cooked] && !reversed)
  1074. ) {
  1075. /* Cooked mode */
  1076. mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
  1077. mode->c_oflag |= OPOST;
  1078. mode->c_lflag |= ISIG | ICANON;
  1079. #if VMIN == VEOF
  1080. mode->c_cc[VEOF] = CEOF;
  1081. #endif
  1082. #if VTIME == VEOL
  1083. mode->c_cc[VEOL] = CEOL;
  1084. #endif
  1085. } else {
  1086. /* Raw mode */
  1087. mode->c_iflag = 0;
  1088. mode->c_oflag &= ~OPOST;
  1089. mode->c_lflag &= ~(ISIG | ICANON | XCASE);
  1090. mode->c_cc[VMIN] = 1;
  1091. mode->c_cc[VTIME] = 0;
  1092. }
  1093. }
  1094. #if IXANY
  1095. else if (info == &mode_info[IDX_decctlq]) {
  1096. if (reversed)
  1097. mode->c_iflag |= IXANY;
  1098. else
  1099. mode->c_iflag &= ~IXANY;
  1100. }
  1101. #endif
  1102. #if TABDLY
  1103. else if (info == &mode_info[IDX_tabs]) {
  1104. if (reversed)
  1105. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
  1106. else
  1107. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
  1108. }
  1109. #endif
  1110. #if OXTABS
  1111. else if (info == &mode_info[IDX_tabs]) {
  1112. if (reversed)
  1113. mode->c_oflag |= OXTABS;
  1114. else
  1115. mode->c_oflag &= ~OXTABS;
  1116. }
  1117. #endif
  1118. #if XCASE && IUCLC && OLCUC
  1119. else if (info==&mode_info[IDX_lcase] || info==&mode_info[IDX_LCASE]) {
  1120. if (reversed) {
  1121. mode->c_lflag &= ~XCASE;
  1122. mode->c_iflag &= ~IUCLC;
  1123. mode->c_oflag &= ~OLCUC;
  1124. } else {
  1125. mode->c_lflag |= XCASE;
  1126. mode->c_iflag |= IUCLC;
  1127. mode->c_oflag |= OLCUC;
  1128. }
  1129. }
  1130. #endif
  1131. else if (info == &mode_info[IDX_crt]) {
  1132. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1133. } else if (info == &mode_info[IDX_dec]) {
  1134. mode->c_cc[VINTR] = 3; /* ^C */
  1135. mode->c_cc[VERASE] = 127; /* DEL */
  1136. mode->c_cc[VKILL] = 21; /* ^U */
  1137. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1138. if (IXANY) mode->c_iflag &= ~IXANY;
  1139. }
  1140. }
  1141. static void set_control_char_or_die(const struct control_info *info,
  1142. const char *arg, struct termios *mode)
  1143. {
  1144. unsigned char value;
  1145. if (info == &control_info[CIDX_min] || info == &control_info[CIDX_time])
  1146. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1147. else if (arg[0] == '\0' || arg[1] == '\0')
  1148. value = arg[0];
  1149. else if (strcmp(arg, "^-") == 0 || strcmp(arg, "undef") == 0)
  1150. value = _POSIX_VDISABLE;
  1151. else if (arg[0] == '^') { /* Ignore any trailing junk (^Cjunk) */
  1152. value = arg[1] & 0x1f; /* Non-letters get weird results */
  1153. if (arg[1] == '?')
  1154. value = 127;
  1155. } else
  1156. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1157. mode->c_cc[info->offset] = value;
  1158. }
  1159. #define STTY_require_set_attr (1 << 0)
  1160. #define STTY_speed_was_set (1 << 1)
  1161. #define STTY_verbose_output (1 << 2)
  1162. #define STTY_recoverable_output (1 << 3)
  1163. #define STTY_noargs (1 << 4)
  1164. int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1165. int stty_main(int argc UNUSED_PARAM, char **argv)
  1166. {
  1167. struct termios mode;
  1168. void (*output_func)(const struct termios *, int);
  1169. const char *file_name = NULL;
  1170. int display_all = 0;
  1171. int stty_state;
  1172. int k;
  1173. INIT_G();
  1174. stty_state = STTY_noargs;
  1175. output_func = do_display;
  1176. /* First pass: only parse/verify command line params */
  1177. k = 0;
  1178. while (argv[++k]) {
  1179. const struct mode_info *mp;
  1180. const struct control_info *cp;
  1181. const char *arg = argv[k];
  1182. const char *argnext = argv[k+1];
  1183. int param;
  1184. if (arg[0] == '-') {
  1185. int i;
  1186. mp = find_mode(arg+1);
  1187. if (mp) {
  1188. if (!(mp->flags & REV))
  1189. goto invalid_argument;
  1190. stty_state &= ~STTY_noargs;
  1191. continue;
  1192. }
  1193. /* It is an option - parse it */
  1194. i = 0;
  1195. while (arg[++i]) {
  1196. switch (arg[i]) {
  1197. case 'a':
  1198. stty_state |= STTY_verbose_output;
  1199. output_func = do_display;
  1200. display_all = 1;
  1201. break;
  1202. case 'g':
  1203. stty_state |= STTY_recoverable_output;
  1204. output_func = display_recoverable;
  1205. break;
  1206. case 'F':
  1207. if (file_name)
  1208. bb_error_msg_and_die("only one device may be specified");
  1209. file_name = &arg[i+1]; /* "-Fdevice" ? */
  1210. if (!file_name[0]) { /* nope, "-F device" */
  1211. int p = k+1; /* argv[p] is argnext */
  1212. file_name = argnext;
  1213. if (!file_name)
  1214. bb_error_msg_and_die(bb_msg_requires_arg, "-F");
  1215. /* remove -F param from arg[vc] */
  1216. while (argv[p]) {
  1217. argv[p] = argv[p+1];
  1218. ++p;
  1219. }
  1220. }
  1221. goto end_option;
  1222. default:
  1223. goto invalid_argument;
  1224. }
  1225. }
  1226. end_option:
  1227. continue;
  1228. }
  1229. mp = find_mode(arg);
  1230. if (mp) {
  1231. stty_state &= ~STTY_noargs;
  1232. continue;
  1233. }
  1234. cp = find_control(arg);
  1235. if (cp) {
  1236. if (!argnext)
  1237. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1238. /* called for the side effect of xfunc death only */
  1239. set_control_char_or_die(cp, argnext, &mode);
  1240. stty_state &= ~STTY_noargs;
  1241. ++k;
  1242. continue;
  1243. }
  1244. param = find_param(arg);
  1245. if (param & param_need_arg) {
  1246. if (!argnext)
  1247. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1248. ++k;
  1249. }
  1250. switch (param) {
  1251. #ifdef HAVE_C_LINE
  1252. case param_line:
  1253. # ifndef TIOCGWINSZ
  1254. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1255. break;
  1256. # endif /* else fall-through */
  1257. #endif
  1258. #ifdef TIOCGWINSZ
  1259. case param_rows:
  1260. case param_cols:
  1261. case param_columns:
  1262. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1263. break;
  1264. case param_size:
  1265. #endif
  1266. case param_speed:
  1267. break;
  1268. case param_ispeed:
  1269. /* called for the side effect of xfunc death only */
  1270. set_speed_or_die(input_speed, argnext, &mode);
  1271. break;
  1272. case param_ospeed:
  1273. /* called for the side effect of xfunc death only */
  1274. set_speed_or_die(output_speed, argnext, &mode);
  1275. break;
  1276. default:
  1277. if (recover_mode(arg, &mode) == 1) break;
  1278. if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) break;
  1279. invalid_argument:
  1280. bb_error_msg_and_die("invalid argument '%s'", arg);
  1281. }
  1282. stty_state &= ~STTY_noargs;
  1283. }
  1284. /* Specifying both -a and -g is an error */
  1285. if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
  1286. (STTY_verbose_output | STTY_recoverable_output))
  1287. bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
  1288. /* Specifying -a or -g with non-options is an error */
  1289. if (!(stty_state & STTY_noargs)
  1290. && (stty_state & (STTY_verbose_output | STTY_recoverable_output))
  1291. ) {
  1292. bb_error_msg_and_die("modes may not be set when specifying an output style");
  1293. }
  1294. /* Now it is safe to start doing things */
  1295. if (file_name) {
  1296. G.device_name = file_name;
  1297. xmove_fd(xopen_nonblocking(G.device_name), STDIN_FILENO);
  1298. ndelay_off(STDIN_FILENO);
  1299. }
  1300. /* Initialize to all zeroes so there is no risk memcmp will report a
  1301. spurious difference in an uninitialized portion of the structure */
  1302. memset(&mode, 0, sizeof(mode));
  1303. if (tcgetattr(STDIN_FILENO, &mode))
  1304. perror_on_device_and_die("%s");
  1305. if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
  1306. get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL);
  1307. output_func(&mode, display_all);
  1308. return EXIT_SUCCESS;
  1309. }
  1310. /* Second pass: perform actions */
  1311. k = 0;
  1312. while (argv[++k]) {
  1313. const struct mode_info *mp;
  1314. const struct control_info *cp;
  1315. const char *arg = argv[k];
  1316. const char *argnext = argv[k+1];
  1317. int param;
  1318. if (arg[0] == '-') {
  1319. mp = find_mode(arg+1);
  1320. if (mp) {
  1321. set_mode(mp, 1 /* reversed */, &mode);
  1322. stty_state |= STTY_require_set_attr;
  1323. }
  1324. /* It is an option - already parsed. Skip it */
  1325. continue;
  1326. }
  1327. mp = find_mode(arg);
  1328. if (mp) {
  1329. set_mode(mp, 0 /* non-reversed */, &mode);
  1330. stty_state |= STTY_require_set_attr;
  1331. continue;
  1332. }
  1333. cp = find_control(arg);
  1334. if (cp) {
  1335. ++k;
  1336. set_control_char_or_die(cp, argnext, &mode);
  1337. stty_state |= STTY_require_set_attr;
  1338. continue;
  1339. }
  1340. param = find_param(arg);
  1341. if (param & param_need_arg) {
  1342. ++k;
  1343. }
  1344. switch (param) {
  1345. #ifdef HAVE_C_LINE
  1346. case param_line:
  1347. mode.c_line = xatoul_sfx(argnext, stty_suffixes);
  1348. stty_state |= STTY_require_set_attr;
  1349. break;
  1350. #endif
  1351. #ifdef TIOCGWINSZ
  1352. case param_cols:
  1353. case param_columns:
  1354. set_window_size(-1, xatoul_sfx(argnext, stty_suffixes));
  1355. break;
  1356. case param_size:
  1357. display_window_size(0);
  1358. break;
  1359. case param_rows:
  1360. set_window_size(xatoul_sfx(argnext, stty_suffixes), -1);
  1361. break;
  1362. #endif
  1363. case param_speed:
  1364. display_speed(&mode, 0);
  1365. break;
  1366. case param_ispeed:
  1367. set_speed_or_die(input_speed, argnext, &mode);
  1368. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1369. break;
  1370. case param_ospeed:
  1371. set_speed_or_die(output_speed, argnext, &mode);
  1372. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1373. break;
  1374. default:
  1375. if (recover_mode(arg, &mode) == 1)
  1376. stty_state |= STTY_require_set_attr;
  1377. else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{
  1378. set_speed_or_die(both_speeds, arg, &mode);
  1379. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1380. } /* else - impossible (caught in the first pass):
  1381. bb_error_msg_and_die("invalid argument '%s'", arg); */
  1382. }
  1383. }
  1384. if (stty_state & STTY_require_set_attr) {
  1385. struct termios new_mode;
  1386. if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
  1387. perror_on_device_and_die("%s");
  1388. /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
  1389. it performs *any* of the requested operations. This means it
  1390. can report 'success' when it has actually failed to perform
  1391. some proper subset of the requested operations. To detect
  1392. this partial failure, get the current terminal attributes and
  1393. compare them to the requested ones */
  1394. /* Initialize to all zeroes so there is no risk memcmp will report a
  1395. spurious difference in an uninitialized portion of the structure */
  1396. memset(&new_mode, 0, sizeof(new_mode));
  1397. if (tcgetattr(STDIN_FILENO, &new_mode))
  1398. perror_on_device_and_die("%s");
  1399. if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
  1400. #if CIBAUD
  1401. /* SunOS 4.1.3 (at least) has the problem that after this sequence,
  1402. tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
  1403. sometimes (m1 != m2). The only difference is in the four bits
  1404. of the c_cflag field corresponding to the baud rate. To save
  1405. Sun users a little confusion, don't report an error if this
  1406. happens. But suppress the error only if we haven't tried to
  1407. set the baud rate explicitly -- otherwise we'd never give an
  1408. error for a true failure to set the baud rate */
  1409. new_mode.c_cflag &= (~CIBAUD);
  1410. if ((stty_state & STTY_speed_was_set)
  1411. || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
  1412. #endif
  1413. perror_on_device_and_die("%s: cannot perform all requested operations");
  1414. }
  1415. }
  1416. return EXIT_SUCCESS;
  1417. }