3
0

stty.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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;
  587. /* The width of the screen, for output wrapping */
  588. unsigned max_col;
  589. /* Current position, to know when to wrap */
  590. unsigned current_col;
  591. char buf[10];
  592. } FIX_ALIASING;
  593. #define G (*(struct globals*)&bb_common_bufsiz1)
  594. #define INIT_G() do { \
  595. G.device_name = bb_msg_standard_input; \
  596. G.max_col = 80; \
  597. } while (0)
  598. /* Return a string that is the printable representation of character CH */
  599. /* Adapted from 'cat' by Torbjorn Granlund */
  600. static const char *visible(unsigned ch)
  601. {
  602. char *bpout = G.buf;
  603. if (ch == _POSIX_VDISABLE)
  604. return "<undef>";
  605. if (ch >= 128) {
  606. ch -= 128;
  607. *bpout++ = 'M';
  608. *bpout++ = '-';
  609. }
  610. if (ch < 32) {
  611. *bpout++ = '^';
  612. *bpout++ = ch + 64;
  613. } else if (ch < 127) {
  614. *bpout++ = ch;
  615. } else {
  616. *bpout++ = '^';
  617. *bpout++ = '?';
  618. }
  619. *bpout = '\0';
  620. return G.buf;
  621. }
  622. static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
  623. {
  624. static const uint8_t tcflag_offsets[] ALIGN1 = {
  625. offsetof(struct termios, c_cflag), /* control */
  626. offsetof(struct termios, c_iflag), /* input */
  627. offsetof(struct termios, c_oflag), /* output */
  628. offsetof(struct termios, c_lflag) /* local */
  629. };
  630. if (type <= local) {
  631. return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
  632. }
  633. return NULL;
  634. }
  635. static void set_speed_or_die(enum speed_setting type, const char *arg,
  636. struct termios *mode)
  637. {
  638. speed_t baud;
  639. baud = tty_value_to_baud(xatou(arg));
  640. if (type != output_speed) { /* either input or both */
  641. cfsetispeed(mode, baud);
  642. }
  643. if (type != input_speed) { /* either output or both */
  644. cfsetospeed(mode, baud);
  645. }
  646. }
  647. static NORETURN void perror_on_device_and_die(const char *fmt)
  648. {
  649. bb_perror_msg_and_die(fmt, G.device_name);
  650. }
  651. static void perror_on_device(const char *fmt)
  652. {
  653. bb_perror_msg(fmt, G.device_name);
  654. }
  655. /* Print format string MESSAGE and optional args.
  656. Wrap to next line first if it won't fit.
  657. Print a space first unless MESSAGE will start a new line */
  658. static void wrapf(const char *message, ...)
  659. {
  660. char buf[128];
  661. va_list args;
  662. unsigned buflen;
  663. va_start(args, message);
  664. buflen = vsnprintf(buf, sizeof(buf), message, args);
  665. va_end(args);
  666. /* We seem to be called only with suitable lengths, but check if
  667. somebody failed to adhere to this assumption just to be sure. */
  668. if (!buflen || buflen >= sizeof(buf)) return;
  669. if (G.current_col > 0) {
  670. G.current_col++;
  671. if (buf[0] != '\n') {
  672. if (G.current_col + buflen >= G.max_col) {
  673. bb_putchar('\n');
  674. G.current_col = 0;
  675. } else
  676. bb_putchar(' ');
  677. }
  678. }
  679. fputs(buf, stdout);
  680. G.current_col += buflen;
  681. if (buf[buflen-1] == '\n')
  682. G.current_col = 0;
  683. }
  684. static void newline(void)
  685. {
  686. if (G.current_col != 0)
  687. wrapf("\n");
  688. }
  689. static void set_window_size(int rows, int cols)
  690. {
  691. struct winsize win = { 0, 0, 0, 0 };
  692. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
  693. if (errno != EINVAL) {
  694. goto bail;
  695. }
  696. memset(&win, 0, sizeof(win));
  697. }
  698. if (rows >= 0)
  699. win.ws_row = rows;
  700. if (cols >= 0)
  701. win.ws_col = cols;
  702. if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
  703. bail:
  704. perror_on_device("%s");
  705. }
  706. static void display_window_size(int fancy)
  707. {
  708. const char *fmt_str = "%s\0%s: no size information for this device";
  709. unsigned width, height;
  710. if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
  711. if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
  712. perror_on_device(fmt_str);
  713. }
  714. } else {
  715. wrapf(fancy ? "rows %u; columns %u;" : "%u %u\n",
  716. height, width);
  717. }
  718. }
  719. static const struct suffix_mult stty_suffixes[] = {
  720. { "b", 512 },
  721. { "k", 1024 },
  722. { "B", 1024 },
  723. { "", 0 }
  724. };
  725. static const struct mode_info *find_mode(const char *name)
  726. {
  727. int i = index_in_strings(mode_name, name);
  728. return i >= 0 ? &mode_info[i] : NULL;
  729. }
  730. static const struct control_info *find_control(const char *name)
  731. {
  732. int i = index_in_strings(control_name, name);
  733. return i >= 0 ? &control_info[i] : NULL;
  734. }
  735. enum {
  736. param_need_arg = 0x80,
  737. param_line = 1 | 0x80,
  738. param_rows = 2 | 0x80,
  739. param_cols = 3 | 0x80,
  740. param_columns = 4 | 0x80,
  741. param_size = 5,
  742. param_speed = 6,
  743. param_ispeed = 7 | 0x80,
  744. param_ospeed = 8 | 0x80,
  745. };
  746. static int find_param(const char *name)
  747. {
  748. static const char params[] ALIGN1 =
  749. "line\0" /* 1 */
  750. "rows\0" /* 2 */
  751. "cols\0" /* 3 */
  752. "columns\0" /* 4 */
  753. "size\0" /* 5 */
  754. "speed\0" /* 6 */
  755. "ispeed\0"
  756. "ospeed\0";
  757. int i = index_in_strings(params, name) + 1;
  758. if (i == 0)
  759. return 0;
  760. if (i != 5 && i != 6)
  761. i |= 0x80;
  762. return i;
  763. }
  764. static int recover_mode(const char *arg, struct termios *mode)
  765. {
  766. int i, n;
  767. unsigned chr;
  768. unsigned long iflag, oflag, cflag, lflag;
  769. /* Scan into temporaries since it is too much trouble to figure out
  770. the right format for 'tcflag_t' */
  771. if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
  772. &iflag, &oflag, &cflag, &lflag, &n) != 4)
  773. return 0;
  774. mode->c_iflag = iflag;
  775. mode->c_oflag = oflag;
  776. mode->c_cflag = cflag;
  777. mode->c_lflag = lflag;
  778. arg += n;
  779. for (i = 0; i < NCCS; ++i) {
  780. if (sscanf(arg, ":%x%n", &chr, &n) != 1)
  781. return 0;
  782. mode->c_cc[i] = chr;
  783. arg += n;
  784. }
  785. /* Fail if there are too many fields */
  786. if (*arg != '\0')
  787. return 0;
  788. return 1;
  789. }
  790. static void display_recoverable(const struct termios *mode,
  791. int UNUSED_PARAM dummy)
  792. {
  793. int i;
  794. printf("%lx:%lx:%lx:%lx",
  795. (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
  796. (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
  797. for (i = 0; i < NCCS; ++i)
  798. printf(":%x", (unsigned int) mode->c_cc[i]);
  799. bb_putchar('\n');
  800. }
  801. static void display_speed(const struct termios *mode, int fancy)
  802. {
  803. //____________________ 01234567 8 9
  804. const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
  805. unsigned long ispeed, ospeed;
  806. ospeed = ispeed = cfgetispeed(mode);
  807. if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
  808. ispeed = ospeed; /* in case ispeed was 0 */
  809. //________ 0123 4 5 6 7 8 9
  810. fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
  811. }
  812. if (fancy) fmt_str += 9;
  813. wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
  814. }
  815. static void do_display(const struct termios *mode, int all)
  816. {
  817. int i;
  818. tcflag_t *bitsp;
  819. unsigned long mask;
  820. int prev_type = control;
  821. display_speed(mode, 1);
  822. if (all)
  823. display_window_size(1);
  824. #ifdef HAVE_C_LINE
  825. wrapf("line = %u;\n", mode->c_line);
  826. #else
  827. newline();
  828. #endif
  829. for (i = 0; i != CIDX_min; ++i) {
  830. /* If swtch is the same as susp, don't print both */
  831. #if VSWTCH == VSUSP
  832. if (i == CIDX_swtch)
  833. continue;
  834. #endif
  835. /* If eof uses the same slot as min, only print whichever applies */
  836. #if VEOF == VMIN
  837. if (!(mode->c_lflag & ICANON)
  838. && (i == CIDX_eof || i == CIDX_eol)
  839. ) {
  840. continue;
  841. }
  842. #endif
  843. wrapf("%s = %s;", nth_string(control_name, i),
  844. visible(mode->c_cc[control_info[i].offset]));
  845. }
  846. #if VEOF == VMIN
  847. if ((mode->c_lflag & ICANON) == 0)
  848. #endif
  849. wrapf("min = %u; time = %u;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
  850. newline();
  851. for (i = 0; i < NUM_mode_info; ++i) {
  852. if (mode_info[i].flags & OMIT)
  853. continue;
  854. if (mode_info[i].type != prev_type) {
  855. newline();
  856. prev_type = mode_info[i].type;
  857. }
  858. bitsp = mode_type_flag(mode_info[i].type, mode);
  859. mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
  860. if ((*bitsp & mask) == mode_info[i].bits) {
  861. if (all || (mode_info[i].flags & SANE_UNSET))
  862. wrapf("-%s"+1, nth_string(mode_name, i));
  863. } else {
  864. if ((all && mode_info[i].flags & REV)
  865. || (!all && (mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV))
  866. ) {
  867. wrapf("-%s", nth_string(mode_name, i));
  868. }
  869. }
  870. }
  871. newline();
  872. }
  873. static void sane_mode(struct termios *mode)
  874. {
  875. int i;
  876. tcflag_t *bitsp;
  877. for (i = 0; i < NUM_control_info; ++i) {
  878. #if VMIN == VEOF
  879. if (i == CIDX_min)
  880. break;
  881. #endif
  882. mode->c_cc[control_info[i].offset] = control_info[i].saneval;
  883. }
  884. for (i = 0; i < NUM_mode_info; ++i) {
  885. if (mode_info[i].flags & SANE_SET) {
  886. bitsp = mode_type_flag(mode_info[i].type, mode);
  887. *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
  888. | mode_info[i].bits;
  889. } else if (mode_info[i].flags & SANE_UNSET) {
  890. bitsp = mode_type_flag(mode_info[i].type, mode);
  891. *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
  892. & ~mode_info[i].bits;
  893. }
  894. }
  895. }
  896. /* Save set_mode from #ifdef forest plague */
  897. #ifndef ONLCR
  898. #define ONLCR 0
  899. #endif
  900. #ifndef OCRNL
  901. #define OCRNL 0
  902. #endif
  903. #ifndef ONLRET
  904. #define ONLRET 0
  905. #endif
  906. #ifndef XCASE
  907. #define XCASE 0
  908. #endif
  909. #ifndef IXANY
  910. #define IXANY 0
  911. #endif
  912. #ifndef TABDLY
  913. #define TABDLY 0
  914. #endif
  915. #ifndef OXTABS
  916. #define OXTABS 0
  917. #endif
  918. #ifndef IUCLC
  919. #define IUCLC 0
  920. #endif
  921. #ifndef OLCUC
  922. #define OLCUC 0
  923. #endif
  924. #ifndef ECHOCTL
  925. #define ECHOCTL 0
  926. #endif
  927. #ifndef ECHOKE
  928. #define ECHOKE 0
  929. #endif
  930. static void set_mode(const struct mode_info *info, int reversed,
  931. struct termios *mode)
  932. {
  933. tcflag_t *bitsp;
  934. bitsp = mode_type_flag(info->type, mode);
  935. if (bitsp) {
  936. if (reversed)
  937. *bitsp = *bitsp & ~info->mask & ~info->bits;
  938. else
  939. *bitsp = (*bitsp & ~info->mask) | info->bits;
  940. return;
  941. }
  942. /* Combination mode */
  943. if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
  944. if (reversed)
  945. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  946. else
  947. mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
  948. } else if (info == &mode_info[IDX_oddp]) {
  949. if (reversed)
  950. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  951. else
  952. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
  953. } else if (info == &mode_info[IDX_nl]) {
  954. if (reversed) {
  955. mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
  956. mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
  957. } else {
  958. mode->c_iflag = mode->c_iflag & ~ICRNL;
  959. if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
  960. }
  961. } else if (info == &mode_info[IDX_ek]) {
  962. mode->c_cc[VERASE] = CERASE;
  963. mode->c_cc[VKILL] = CKILL;
  964. } else if (info == &mode_info[IDX_sane]) {
  965. sane_mode(mode);
  966. } else if (info == &mode_info[IDX_cbreak]) {
  967. if (reversed)
  968. mode->c_lflag |= ICANON;
  969. else
  970. mode->c_lflag &= ~ICANON;
  971. } else if (info == &mode_info[IDX_pass8]) {
  972. if (reversed) {
  973. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  974. mode->c_iflag |= ISTRIP;
  975. } else {
  976. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  977. mode->c_iflag &= ~ISTRIP;
  978. }
  979. } else if (info == &mode_info[IDX_litout]) {
  980. if (reversed) {
  981. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  982. mode->c_iflag |= ISTRIP;
  983. mode->c_oflag |= OPOST;
  984. } else {
  985. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  986. mode->c_iflag &= ~ISTRIP;
  987. mode->c_oflag &= ~OPOST;
  988. }
  989. } else if (info == &mode_info[IDX_raw] || info == &mode_info[IDX_cooked]) {
  990. if ((info == &mode_info[IDX_raw] && reversed)
  991. || (info == &mode_info[IDX_cooked] && !reversed)
  992. ) {
  993. /* Cooked mode */
  994. mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
  995. mode->c_oflag |= OPOST;
  996. mode->c_lflag |= ISIG | ICANON;
  997. #if VMIN == VEOF
  998. mode->c_cc[VEOF] = CEOF;
  999. #endif
  1000. #if VTIME == VEOL
  1001. mode->c_cc[VEOL] = CEOL;
  1002. #endif
  1003. } else {
  1004. /* Raw mode */
  1005. mode->c_iflag = 0;
  1006. mode->c_oflag &= ~OPOST;
  1007. mode->c_lflag &= ~(ISIG | ICANON | XCASE);
  1008. mode->c_cc[VMIN] = 1;
  1009. mode->c_cc[VTIME] = 0;
  1010. }
  1011. }
  1012. else if (IXANY && info == &mode_info[IDX_decctlq]) {
  1013. if (reversed)
  1014. mode->c_iflag |= IXANY;
  1015. else
  1016. mode->c_iflag &= ~IXANY;
  1017. }
  1018. else if (TABDLY && info == &mode_info[IDX_tabs]) {
  1019. if (reversed)
  1020. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
  1021. else
  1022. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
  1023. }
  1024. else if (OXTABS && info == &mode_info[IDX_tabs]) {
  1025. if (reversed)
  1026. mode->c_oflag |= OXTABS;
  1027. else
  1028. mode->c_oflag &= ~OXTABS;
  1029. } else
  1030. if (XCASE && IUCLC && OLCUC
  1031. && (info == &mode_info[IDX_lcase] || info == &mode_info[IDX_LCASE])
  1032. ) {
  1033. if (reversed) {
  1034. mode->c_lflag &= ~XCASE;
  1035. mode->c_iflag &= ~IUCLC;
  1036. mode->c_oflag &= ~OLCUC;
  1037. } else {
  1038. mode->c_lflag |= XCASE;
  1039. mode->c_iflag |= IUCLC;
  1040. mode->c_oflag |= OLCUC;
  1041. }
  1042. } else if (info == &mode_info[IDX_crt]) {
  1043. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1044. } else if (info == &mode_info[IDX_dec]) {
  1045. mode->c_cc[VINTR] = 3; /* ^C */
  1046. mode->c_cc[VERASE] = 127; /* DEL */
  1047. mode->c_cc[VKILL] = 21; /* ^U */
  1048. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1049. if (IXANY) mode->c_iflag &= ~IXANY;
  1050. }
  1051. }
  1052. static void set_control_char_or_die(const struct control_info *info,
  1053. const char *arg, struct termios *mode)
  1054. {
  1055. unsigned char value;
  1056. if (info == &control_info[CIDX_min] || info == &control_info[CIDX_time])
  1057. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1058. else if (arg[0] == '\0' || arg[1] == '\0')
  1059. value = arg[0];
  1060. else if (strcmp(arg, "^-") == 0 || strcmp(arg, "undef") == 0)
  1061. value = _POSIX_VDISABLE;
  1062. else if (arg[0] == '^') { /* Ignore any trailing junk (^Cjunk) */
  1063. value = arg[1] & 0x1f; /* Non-letters get weird results */
  1064. if (arg[1] == '?')
  1065. value = 127;
  1066. } else
  1067. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1068. mode->c_cc[info->offset] = value;
  1069. }
  1070. #define STTY_require_set_attr (1 << 0)
  1071. #define STTY_speed_was_set (1 << 1)
  1072. #define STTY_verbose_output (1 << 2)
  1073. #define STTY_recoverable_output (1 << 3)
  1074. #define STTY_noargs (1 << 4)
  1075. int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1076. int stty_main(int argc UNUSED_PARAM, char **argv)
  1077. {
  1078. struct termios mode;
  1079. void (*output_func)(const struct termios *, int);
  1080. const char *file_name = NULL;
  1081. int display_all = 0;
  1082. int stty_state;
  1083. int k;
  1084. INIT_G();
  1085. stty_state = STTY_noargs;
  1086. output_func = do_display;
  1087. /* First pass: only parse/verify command line params */
  1088. k = 0;
  1089. while (argv[++k]) {
  1090. const struct mode_info *mp;
  1091. const struct control_info *cp;
  1092. const char *arg = argv[k];
  1093. const char *argnext = argv[k+1];
  1094. int param;
  1095. if (arg[0] == '-') {
  1096. int i;
  1097. mp = find_mode(arg+1);
  1098. if (mp) {
  1099. if (!(mp->flags & REV))
  1100. goto invalid_argument;
  1101. stty_state &= ~STTY_noargs;
  1102. continue;
  1103. }
  1104. /* It is an option - parse it */
  1105. i = 0;
  1106. while (arg[++i]) {
  1107. switch (arg[i]) {
  1108. case 'a':
  1109. stty_state |= STTY_verbose_output;
  1110. output_func = do_display;
  1111. display_all = 1;
  1112. break;
  1113. case 'g':
  1114. stty_state |= STTY_recoverable_output;
  1115. output_func = display_recoverable;
  1116. break;
  1117. case 'F':
  1118. if (file_name)
  1119. bb_error_msg_and_die("only one device may be specified");
  1120. file_name = &arg[i+1]; /* "-Fdevice" ? */
  1121. if (!file_name[0]) { /* nope, "-F device" */
  1122. int p = k+1; /* argv[p] is argnext */
  1123. file_name = argnext;
  1124. if (!file_name)
  1125. bb_error_msg_and_die(bb_msg_requires_arg, "-F");
  1126. /* remove -F param from arg[vc] */
  1127. while (argv[p]) {
  1128. argv[p] = argv[p+1];
  1129. ++p;
  1130. }
  1131. }
  1132. goto end_option;
  1133. default:
  1134. goto invalid_argument;
  1135. }
  1136. }
  1137. end_option:
  1138. continue;
  1139. }
  1140. mp = find_mode(arg);
  1141. if (mp) {
  1142. stty_state &= ~STTY_noargs;
  1143. continue;
  1144. }
  1145. cp = find_control(arg);
  1146. if (cp) {
  1147. if (!argnext)
  1148. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1149. /* called for the side effect of xfunc death only */
  1150. set_control_char_or_die(cp, argnext, &mode);
  1151. stty_state &= ~STTY_noargs;
  1152. ++k;
  1153. continue;
  1154. }
  1155. param = find_param(arg);
  1156. if (param & param_need_arg) {
  1157. if (!argnext)
  1158. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1159. ++k;
  1160. }
  1161. switch (param) {
  1162. #ifdef HAVE_C_LINE
  1163. case param_line:
  1164. # ifndef TIOCGWINSZ
  1165. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1166. break;
  1167. # endif /* else fall-through */
  1168. #endif
  1169. #ifdef TIOCGWINSZ
  1170. case param_rows:
  1171. case param_cols:
  1172. case param_columns:
  1173. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1174. break;
  1175. case param_size:
  1176. #endif
  1177. case param_speed:
  1178. break;
  1179. case param_ispeed:
  1180. /* called for the side effect of xfunc death only */
  1181. set_speed_or_die(input_speed, argnext, &mode);
  1182. break;
  1183. case param_ospeed:
  1184. /* called for the side effect of xfunc death only */
  1185. set_speed_or_die(output_speed, argnext, &mode);
  1186. break;
  1187. default:
  1188. if (recover_mode(arg, &mode) == 1) break;
  1189. if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) break;
  1190. invalid_argument:
  1191. bb_error_msg_and_die("invalid argument '%s'", arg);
  1192. }
  1193. stty_state &= ~STTY_noargs;
  1194. }
  1195. /* Specifying both -a and -g is an error */
  1196. if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
  1197. (STTY_verbose_output | STTY_recoverable_output))
  1198. bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
  1199. /* Specifying -a or -g with non-options is an error */
  1200. if (!(stty_state & STTY_noargs)
  1201. && (stty_state & (STTY_verbose_output | STTY_recoverable_output))
  1202. ) {
  1203. bb_error_msg_and_die("modes may not be set when specifying an output style");
  1204. }
  1205. /* Now it is safe to start doing things */
  1206. if (file_name) {
  1207. G.device_name = file_name;
  1208. xmove_fd(xopen_nonblocking(G.device_name), STDIN_FILENO);
  1209. ndelay_off(STDIN_FILENO);
  1210. }
  1211. /* Initialize to all zeroes so there is no risk memcmp will report a
  1212. spurious difference in an uninitialized portion of the structure */
  1213. memset(&mode, 0, sizeof(mode));
  1214. if (tcgetattr(STDIN_FILENO, &mode))
  1215. perror_on_device_and_die("%s");
  1216. if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
  1217. get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL);
  1218. output_func(&mode, display_all);
  1219. return EXIT_SUCCESS;
  1220. }
  1221. /* Second pass: perform actions */
  1222. k = 0;
  1223. while (argv[++k]) {
  1224. const struct mode_info *mp;
  1225. const struct control_info *cp;
  1226. const char *arg = argv[k];
  1227. const char *argnext = argv[k+1];
  1228. int param;
  1229. if (arg[0] == '-') {
  1230. mp = find_mode(arg+1);
  1231. if (mp) {
  1232. set_mode(mp, 1 /* reversed */, &mode);
  1233. stty_state |= STTY_require_set_attr;
  1234. }
  1235. /* It is an option - already parsed. Skip it */
  1236. continue;
  1237. }
  1238. mp = find_mode(arg);
  1239. if (mp) {
  1240. set_mode(mp, 0 /* non-reversed */, &mode);
  1241. stty_state |= STTY_require_set_attr;
  1242. continue;
  1243. }
  1244. cp = find_control(arg);
  1245. if (cp) {
  1246. ++k;
  1247. set_control_char_or_die(cp, argnext, &mode);
  1248. stty_state |= STTY_require_set_attr;
  1249. continue;
  1250. }
  1251. param = find_param(arg);
  1252. if (param & param_need_arg) {
  1253. ++k;
  1254. }
  1255. switch (param) {
  1256. #ifdef HAVE_C_LINE
  1257. case param_line:
  1258. mode.c_line = xatoul_sfx(argnext, stty_suffixes);
  1259. stty_state |= STTY_require_set_attr;
  1260. break;
  1261. #endif
  1262. #ifdef TIOCGWINSZ
  1263. case param_cols:
  1264. case param_columns:
  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. }