stty.c 43 KB

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