stty.c 34 KB

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