stty.c 42 KB

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