stty.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  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() 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 set_window_size(const int rows, const int cols)
  685. {
  686. struct winsize win = { 0, 0, 0, 0 };
  687. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
  688. if (errno != EINVAL) {
  689. goto bail;
  690. }
  691. memset(&win, 0, sizeof(win));
  692. }
  693. if (rows >= 0)
  694. win.ws_row = rows;
  695. if (cols >= 0)
  696. win.ws_col = cols;
  697. if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
  698. bail:
  699. perror_on_device("%s");
  700. }
  701. static void display_window_size(const int fancy)
  702. {
  703. const char *fmt_str = "%s\0%s: no size information for this device";
  704. unsigned width, height;
  705. if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
  706. if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
  707. perror_on_device(fmt_str);
  708. }
  709. } else {
  710. wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
  711. height, width);
  712. }
  713. }
  714. static const struct suffix_mult stty_suffixes[] = {
  715. { "b", 512 },
  716. { "k", 1024 },
  717. { "B", 1024 },
  718. { }
  719. };
  720. static const struct mode_info *find_mode(const char *name)
  721. {
  722. int i = index_in_strings(mode_name, name);
  723. return i >= 0 ? &mode_info[i] : NULL;
  724. }
  725. static const struct control_info *find_control(const char *name)
  726. {
  727. int i = index_in_strings(control_name, name);
  728. return i >= 0 ? &control_info[i] : NULL;
  729. }
  730. enum {
  731. param_need_arg = 0x80,
  732. param_line = 1 | 0x80,
  733. param_rows = 2 | 0x80,
  734. param_cols = 3 | 0x80,
  735. param_columns = 4 | 0x80,
  736. param_size = 5,
  737. param_speed = 6,
  738. param_ispeed = 7 | 0x80,
  739. param_ospeed = 8 | 0x80,
  740. };
  741. static int find_param(const char *name)
  742. {
  743. static const char params[] ALIGN1 =
  744. "line\0" /* 1 */
  745. "rows\0" /* 2 */
  746. "cols\0" /* 3 */
  747. "columns\0" /* 4 */
  748. "size\0" /* 5 */
  749. "speed\0" /* 6 */
  750. "ispeed\0"
  751. "ospeed\0";
  752. int i = index_in_strings(params, name) + 1;
  753. if (i == 0)
  754. return 0;
  755. if (i != 5 && i != 6)
  756. i |= 0x80;
  757. return i;
  758. }
  759. static int recover_mode(const char *arg, struct termios *mode)
  760. {
  761. int i, n;
  762. unsigned chr;
  763. unsigned long iflag, oflag, cflag, lflag;
  764. /* Scan into temporaries since it is too much trouble to figure out
  765. the right format for 'tcflag_t' */
  766. if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
  767. &iflag, &oflag, &cflag, &lflag, &n) != 4)
  768. return 0;
  769. mode->c_iflag = iflag;
  770. mode->c_oflag = oflag;
  771. mode->c_cflag = cflag;
  772. mode->c_lflag = lflag;
  773. arg += n;
  774. for (i = 0; i < NCCS; ++i) {
  775. if (sscanf(arg, ":%x%n", &chr, &n) != 1)
  776. return 0;
  777. mode->c_cc[i] = chr;
  778. arg += n;
  779. }
  780. /* Fail if there are too many fields */
  781. if (*arg != '\0')
  782. return 0;
  783. return 1;
  784. }
  785. static void display_recoverable(const struct termios *mode,
  786. int UNUSED_PARAM dummy)
  787. {
  788. int i;
  789. printf("%lx:%lx:%lx:%lx",
  790. (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
  791. (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
  792. for (i = 0; i < NCCS; ++i)
  793. printf(":%x", (unsigned int) mode->c_cc[i]);
  794. bb_putchar('\n');
  795. }
  796. static void display_speed(const struct termios *mode, int fancy)
  797. {
  798. //01234567 8 9
  799. const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
  800. unsigned long ispeed, ospeed;
  801. ospeed = ispeed = cfgetispeed(mode);
  802. if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
  803. ispeed = ospeed; /* in case ispeed was 0 */
  804. //0123 4 5 6 7 8 9
  805. fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
  806. }
  807. if (fancy) fmt_str += 9;
  808. wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
  809. }
  810. static void do_display(const struct termios *mode, const int all)
  811. {
  812. int i;
  813. tcflag_t *bitsp;
  814. unsigned long mask;
  815. int prev_type = control;
  816. display_speed(mode, 1);
  817. if (all)
  818. display_window_size(1);
  819. #ifdef HAVE_C_LINE
  820. wrapf("line = %d;\n", mode->c_line);
  821. #else
  822. wrapf("\n");
  823. #endif
  824. for (i = 0; i != CIDX_min; ++i) {
  825. /* If swtch is the same as susp, don't print both */
  826. #if VSWTCH == VSUSP
  827. if (i == CIDX_swtch)
  828. continue;
  829. #endif
  830. /* If eof uses the same slot as min, only print whichever applies */
  831. #if VEOF == VMIN
  832. if ((mode->c_lflag & ICANON) == 0
  833. && (i == CIDX_eof || i == CIDX_eol)
  834. ) {
  835. continue;
  836. }
  837. #endif
  838. wrapf("%s = %s;", nth_string(control_name, i),
  839. visible(mode->c_cc[control_info[i].offset]));
  840. }
  841. #if VEOF == VMIN
  842. if ((mode->c_lflag & ICANON) == 0)
  843. #endif
  844. wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
  845. if (G.current_col) wrapf("\n");
  846. for (i = 0; i < NUM_mode_info; ++i) {
  847. if (mode_info[i].flags & OMIT)
  848. continue;
  849. if (mode_info[i].type != prev_type) {
  850. /* wrapf("\n"); */
  851. if (G.current_col) wrapf("\n");
  852. prev_type = mode_info[i].type;
  853. }
  854. bitsp = mode_type_flag(mode_info[i].type, mode);
  855. mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
  856. if ((*bitsp & mask) == mode_info[i].bits) {
  857. if (all || (mode_info[i].flags & SANE_UNSET))
  858. wrapf("-%s"+1, nth_string(mode_name, i));
  859. } else {
  860. if ((all && mode_info[i].flags & REV)
  861. || (!all && (mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV))
  862. ) {
  863. wrapf("-%s", nth_string(mode_name, i));
  864. }
  865. }
  866. }
  867. if (G.current_col) wrapf("\n");
  868. }
  869. static void sane_mode(struct termios *mode)
  870. {
  871. int i;
  872. tcflag_t *bitsp;
  873. for (i = 0; i < NUM_control_info; ++i) {
  874. #if VMIN == VEOF
  875. if (i == CIDX_min)
  876. break;
  877. #endif
  878. mode->c_cc[control_info[i].offset] = control_info[i].saneval;
  879. }
  880. for (i = 0; i < NUM_mode_info; ++i) {
  881. if (mode_info[i].flags & SANE_SET) {
  882. bitsp = mode_type_flag(mode_info[i].type, mode);
  883. *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
  884. | mode_info[i].bits;
  885. } else if (mode_info[i].flags & SANE_UNSET) {
  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. }
  890. }
  891. }
  892. /* Save set_mode from #ifdef forest plague */
  893. #ifndef ONLCR
  894. #define ONLCR 0
  895. #endif
  896. #ifndef OCRNL
  897. #define OCRNL 0
  898. #endif
  899. #ifndef ONLRET
  900. #define ONLRET 0
  901. #endif
  902. #ifndef XCASE
  903. #define XCASE 0
  904. #endif
  905. #ifndef IXANY
  906. #define IXANY 0
  907. #endif
  908. #ifndef TABDLY
  909. #define TABDLY 0
  910. #endif
  911. #ifndef OXTABS
  912. #define OXTABS 0
  913. #endif
  914. #ifndef IUCLC
  915. #define IUCLC 0
  916. #endif
  917. #ifndef OLCUC
  918. #define OLCUC 0
  919. #endif
  920. #ifndef ECHOCTL
  921. #define ECHOCTL 0
  922. #endif
  923. #ifndef ECHOKE
  924. #define ECHOKE 0
  925. #endif
  926. static void set_mode(const struct mode_info *info, int reversed,
  927. struct termios *mode)
  928. {
  929. tcflag_t *bitsp;
  930. bitsp = mode_type_flag(info->type, mode);
  931. if (bitsp) {
  932. if (reversed)
  933. *bitsp = *bitsp & ~info->mask & ~info->bits;
  934. else
  935. *bitsp = (*bitsp & ~info->mask) | info->bits;
  936. return;
  937. }
  938. /* Combination mode */
  939. if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
  940. if (reversed)
  941. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  942. else
  943. mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
  944. } else if (info == &mode_info[IDX_oddp]) {
  945. if (reversed)
  946. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  947. else
  948. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
  949. } else if (info == &mode_info[IDX_nl]) {
  950. if (reversed) {
  951. mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
  952. mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
  953. } else {
  954. mode->c_iflag = mode->c_iflag & ~ICRNL;
  955. if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
  956. }
  957. } else if (info == &mode_info[IDX_ek]) {
  958. mode->c_cc[VERASE] = CERASE;
  959. mode->c_cc[VKILL] = CKILL;
  960. } else if (info == &mode_info[IDX_sane]) {
  961. sane_mode(mode);
  962. } else if (info == &mode_info[IDX_cbreak]) {
  963. if (reversed)
  964. mode->c_lflag |= ICANON;
  965. else
  966. mode->c_lflag &= ~ICANON;
  967. } else if (info == &mode_info[IDX_pass8]) {
  968. if (reversed) {
  969. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  970. mode->c_iflag |= ISTRIP;
  971. } else {
  972. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  973. mode->c_iflag &= ~ISTRIP;
  974. }
  975. } else if (info == &mode_info[IDX_litout]) {
  976. if (reversed) {
  977. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  978. mode->c_iflag |= ISTRIP;
  979. mode->c_oflag |= OPOST;
  980. } else {
  981. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  982. mode->c_iflag &= ~ISTRIP;
  983. mode->c_oflag &= ~OPOST;
  984. }
  985. } else if (info == &mode_info[IDX_raw] || info == &mode_info[IDX_cooked]) {
  986. if ((info == &mode_info[IDX_raw] && reversed)
  987. || (info == &mode_info[IDX_cooked] && !reversed)
  988. ) {
  989. /* Cooked mode */
  990. mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
  991. mode->c_oflag |= OPOST;
  992. mode->c_lflag |= ISIG | ICANON;
  993. #if VMIN == VEOF
  994. mode->c_cc[VEOF] = CEOF;
  995. #endif
  996. #if VTIME == VEOL
  997. mode->c_cc[VEOL] = CEOL;
  998. #endif
  999. } else {
  1000. /* Raw mode */
  1001. mode->c_iflag = 0;
  1002. mode->c_oflag &= ~OPOST;
  1003. mode->c_lflag &= ~(ISIG | ICANON | XCASE);
  1004. mode->c_cc[VMIN] = 1;
  1005. mode->c_cc[VTIME] = 0;
  1006. }
  1007. }
  1008. else if (IXANY && info == &mode_info[IDX_decctlq]) {
  1009. if (reversed)
  1010. mode->c_iflag |= IXANY;
  1011. else
  1012. mode->c_iflag &= ~IXANY;
  1013. }
  1014. else if (TABDLY && info == &mode_info[IDX_tabs]) {
  1015. if (reversed)
  1016. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
  1017. else
  1018. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
  1019. }
  1020. else if (OXTABS && info == &mode_info[IDX_tabs]) {
  1021. if (reversed)
  1022. mode->c_oflag |= OXTABS;
  1023. else
  1024. mode->c_oflag &= ~OXTABS;
  1025. } else
  1026. if (XCASE && IUCLC && OLCUC
  1027. && (info == &mode_info[IDX_lcase] || info == &mode_info[IDX_LCASE])
  1028. ) {
  1029. if (reversed) {
  1030. mode->c_lflag &= ~XCASE;
  1031. mode->c_iflag &= ~IUCLC;
  1032. mode->c_oflag &= ~OLCUC;
  1033. } else {
  1034. mode->c_lflag |= XCASE;
  1035. mode->c_iflag |= IUCLC;
  1036. mode->c_oflag |= OLCUC;
  1037. }
  1038. } else if (info == &mode_info[IDX_crt]) {
  1039. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1040. } else if (info == &mode_info[IDX_dec]) {
  1041. mode->c_cc[VINTR] = 3; /* ^C */
  1042. mode->c_cc[VERASE] = 127; /* DEL */
  1043. mode->c_cc[VKILL] = 21; /* ^U */
  1044. mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
  1045. if (IXANY) mode->c_iflag &= ~IXANY;
  1046. }
  1047. }
  1048. static void set_control_char_or_die(const struct control_info *info,
  1049. const char *arg, struct termios *mode)
  1050. {
  1051. unsigned char value;
  1052. if (info == &control_info[CIDX_min] || info == &control_info[CIDX_time])
  1053. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1054. else if (arg[0] == '\0' || arg[1] == '\0')
  1055. value = arg[0];
  1056. else if (!strcmp(arg, "^-") || !strcmp(arg, "undef"))
  1057. value = _POSIX_VDISABLE;
  1058. else if (arg[0] == '^') { /* Ignore any trailing junk (^Cjunk) */
  1059. value = arg[1] & 0x1f; /* Non-letters get weird results */
  1060. if (arg[1] == '?')
  1061. value = 127;
  1062. } else
  1063. value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
  1064. mode->c_cc[info->offset] = value;
  1065. }
  1066. #define STTY_require_set_attr (1 << 0)
  1067. #define STTY_speed_was_set (1 << 1)
  1068. #define STTY_verbose_output (1 << 2)
  1069. #define STTY_recoverable_output (1 << 3)
  1070. #define STTY_noargs (1 << 4)
  1071. int stty_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1072. int stty_main(int argc, char **argv)
  1073. {
  1074. struct termios mode;
  1075. void (*output_func)(const struct termios *, const int);
  1076. const char *file_name = NULL;
  1077. int display_all = 0;
  1078. int stty_state;
  1079. int k;
  1080. INIT_G();
  1081. stty_state = STTY_noargs;
  1082. output_func = do_display;
  1083. /* First pass: only parse/verify command line params */
  1084. k = 0;
  1085. while (argv[++k]) {
  1086. const struct mode_info *mp;
  1087. const struct control_info *cp;
  1088. const char *arg = argv[k];
  1089. const char *argnext = argv[k+1];
  1090. int param;
  1091. if (arg[0] == '-') {
  1092. int i;
  1093. mp = find_mode(arg+1);
  1094. if (mp) {
  1095. if (!(mp->flags & REV))
  1096. goto invalid_argument;
  1097. stty_state &= ~STTY_noargs;
  1098. continue;
  1099. }
  1100. /* It is an option - parse it */
  1101. i = 0;
  1102. while (arg[++i]) {
  1103. switch (arg[i]) {
  1104. case 'a':
  1105. stty_state |= STTY_verbose_output;
  1106. output_func = do_display;
  1107. display_all = 1;
  1108. break;
  1109. case 'g':
  1110. stty_state |= STTY_recoverable_output;
  1111. output_func = display_recoverable;
  1112. break;
  1113. case 'F':
  1114. if (file_name)
  1115. bb_error_msg_and_die("only one device may be specified");
  1116. file_name = &arg[i+1]; /* "-Fdevice" ? */
  1117. if (!file_name[0]) { /* nope, "-F device" */
  1118. int p = k+1; /* argv[p] is argnext */
  1119. file_name = argnext;
  1120. if (!file_name)
  1121. bb_error_msg_and_die(bb_msg_requires_arg, "-F");
  1122. /* remove -F param from arg[vc] */
  1123. --argc;
  1124. while (argv[p]) { argv[p] = argv[p+1]; ++p; }
  1125. }
  1126. goto end_option;
  1127. default:
  1128. goto invalid_argument;
  1129. }
  1130. }
  1131. end_option:
  1132. continue;
  1133. }
  1134. mp = find_mode(arg);
  1135. if (mp) {
  1136. stty_state &= ~STTY_noargs;
  1137. continue;
  1138. }
  1139. cp = find_control(arg);
  1140. if (cp) {
  1141. if (!argnext)
  1142. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1143. /* called for the side effect of xfunc death only */
  1144. set_control_char_or_die(cp, argnext, &mode);
  1145. stty_state &= ~STTY_noargs;
  1146. ++k;
  1147. continue;
  1148. }
  1149. param = find_param(arg);
  1150. if (param & param_need_arg) {
  1151. if (!argnext)
  1152. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  1153. ++k;
  1154. }
  1155. switch (param) {
  1156. #ifdef HAVE_C_LINE
  1157. case param_line:
  1158. # ifndef TIOCGWINSZ
  1159. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1160. break;
  1161. # endif /* else fall-through */
  1162. #endif
  1163. #ifdef TIOCGWINSZ
  1164. case param_rows:
  1165. case param_cols:
  1166. case param_columns:
  1167. xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
  1168. break;
  1169. case param_size:
  1170. #endif
  1171. case param_speed:
  1172. break;
  1173. case param_ispeed:
  1174. /* called for the side effect of xfunc death only */
  1175. set_speed_or_die(input_speed, argnext, &mode);
  1176. break;
  1177. case param_ospeed:
  1178. /* called for the side effect of xfunc death only */
  1179. set_speed_or_die(output_speed, argnext, &mode);
  1180. break;
  1181. default:
  1182. if (recover_mode(arg, &mode) == 1) break;
  1183. if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) break;
  1184. invalid_argument:
  1185. bb_error_msg_and_die("invalid argument '%s'", arg);
  1186. }
  1187. stty_state &= ~STTY_noargs;
  1188. }
  1189. /* Specifying both -a and -g is an error */
  1190. if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
  1191. (STTY_verbose_output | STTY_recoverable_output))
  1192. bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
  1193. /* Specifying -a or -g with non-options is an error */
  1194. if (!(stty_state & STTY_noargs) &&
  1195. (stty_state & (STTY_verbose_output | STTY_recoverable_output)))
  1196. bb_error_msg_and_die("modes may not be set when specifying an output style");
  1197. /* Now it is safe to start doing things */
  1198. if (file_name) {
  1199. int fd, fdflags;
  1200. G.device_name = file_name;
  1201. fd = xopen(G.device_name, O_RDONLY | O_NONBLOCK);
  1202. if (fd != STDIN_FILENO) {
  1203. dup2(fd, STDIN_FILENO);
  1204. close(fd);
  1205. }
  1206. fdflags = fcntl(STDIN_FILENO, F_GETFL);
  1207. if (fdflags < 0 ||
  1208. fcntl(STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
  1209. perror_on_device_and_die("%s: cannot reset non-blocking mode");
  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. set_window_size(-1, xatoul_sfx(argnext, stty_suffixes));
  1265. break;
  1266. case param_size:
  1267. display_window_size(0);
  1268. break;
  1269. case param_rows:
  1270. set_window_size(xatoul_sfx(argnext, stty_suffixes), -1);
  1271. break;
  1272. #endif
  1273. case param_speed:
  1274. display_speed(&mode, 0);
  1275. break;
  1276. case param_ispeed:
  1277. set_speed_or_die(input_speed, argnext, &mode);
  1278. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1279. break;
  1280. case param_ospeed:
  1281. set_speed_or_die(output_speed, argnext, &mode);
  1282. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1283. break;
  1284. default:
  1285. if (recover_mode(arg, &mode) == 1)
  1286. stty_state |= STTY_require_set_attr;
  1287. else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{
  1288. set_speed_or_die(both_speeds, arg, &mode);
  1289. stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
  1290. } /* else - impossible (caught in the first pass):
  1291. bb_error_msg_and_die("invalid argument '%s'", arg); */
  1292. }
  1293. }
  1294. if (stty_state & STTY_require_set_attr) {
  1295. struct termios new_mode;
  1296. if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
  1297. perror_on_device_and_die("%s");
  1298. /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
  1299. it performs *any* of the requested operations. This means it
  1300. can report 'success' when it has actually failed to perform
  1301. some proper subset of the requested operations. To detect
  1302. this partial failure, get the current terminal attributes and
  1303. compare them to the requested ones */
  1304. /* Initialize to all zeroes so there is no risk memcmp will report a
  1305. spurious difference in an uninitialized portion of the structure */
  1306. memset(&new_mode, 0, sizeof(new_mode));
  1307. if (tcgetattr(STDIN_FILENO, &new_mode))
  1308. perror_on_device_and_die("%s");
  1309. if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
  1310. #ifdef CIBAUD
  1311. /* SunOS 4.1.3 (at least) has the problem that after this sequence,
  1312. tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
  1313. sometimes (m1 != m2). The only difference is in the four bits
  1314. of the c_cflag field corresponding to the baud rate. To save
  1315. Sun users a little confusion, don't report an error if this
  1316. happens. But suppress the error only if we haven't tried to
  1317. set the baud rate explicitly -- otherwise we'd never give an
  1318. error for a true failure to set the baud rate */
  1319. new_mode.c_cflag &= (~CIBAUD);
  1320. if ((stty_state & STTY_speed_was_set)
  1321. || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
  1322. #endif
  1323. perror_on_device_and_die("%s: cannot perform all requested operations");
  1324. }
  1325. }
  1326. return EXIT_SUCCESS;
  1327. }