stty.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. You should have received a copy of the GNU General Public License
  9. along with this program; if not, write to the Free Software Foundation,
  10. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  11. /* Usage: stty [-ag] [-F device] [setting...]
  12. Options:
  13. -a Write all current settings to stdout in human-readable form.
  14. -g Write all current settings to stdout in stty-readable form.
  15. -F Open and use the specified device instead of stdin
  16. If no args are given, write to stdout the baud rate and settings that
  17. have been changed from their defaults. Mode reading and changes
  18. are done on the specified device, or stdin if none was specified.
  19. David MacKenzie <djm@gnu.ai.mit.edu>
  20. Special for busybox ported by Vladimir Oleynik <dzo@simtreas.ru> 2001
  21. */
  22. //#define TEST
  23. #include <stddef.h>
  24. #include <termios.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/param.h>
  27. #include <unistd.h>
  28. #ifndef STDIN_FILENO
  29. # define STDIN_FILENO 0
  30. #endif
  31. #ifndef STDOUT_FILENO
  32. # define STDOUT_FILENO 1
  33. #endif
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <assert.h>
  37. #include <ctype.h>
  38. #include <errno.h>
  39. #include <limits.h>
  40. #include <memory.h>
  41. #include <fcntl.h>
  42. #include "busybox.h"
  43. #define STREQ(a, b) (strcmp ((a), (b)) == 0)
  44. #ifndef _POSIX_VDISABLE
  45. # define _POSIX_VDISABLE ((unsigned char) 0)
  46. #endif
  47. #define Control(c) ((c) & 0x1f)
  48. /* Canonical values for control characters. */
  49. #ifndef CINTR
  50. # define CINTR Control ('c')
  51. #endif
  52. #ifndef CQUIT
  53. # define CQUIT 28
  54. #endif
  55. #ifndef CERASE
  56. # define CERASE 127
  57. #endif
  58. #ifndef CKILL
  59. # define CKILL Control ('u')
  60. #endif
  61. #ifndef CEOF
  62. # define CEOF Control ('d')
  63. #endif
  64. #ifndef CEOL
  65. # define CEOL _POSIX_VDISABLE
  66. #endif
  67. #ifndef CSTART
  68. # define CSTART Control ('q')
  69. #endif
  70. #ifndef CSTOP
  71. # define CSTOP Control ('s')
  72. #endif
  73. #ifndef CSUSP
  74. # define CSUSP Control ('z')
  75. #endif
  76. #if defined(VEOL2) && !defined(CEOL2)
  77. # define CEOL2 _POSIX_VDISABLE
  78. #endif
  79. /* ISC renamed swtch to susp for termios, but we'll accept either name. */
  80. #if defined(VSUSP) && !defined(VSWTCH)
  81. # define VSWTCH VSUSP
  82. # define CSWTCH CSUSP
  83. #endif
  84. #if defined(VSWTCH) && !defined(CSWTCH)
  85. # define CSWTCH _POSIX_VDISABLE
  86. #endif
  87. /* SunOS 5.3 loses (^Z doesn't work) if `swtch' is the same as `susp'.
  88. So the default is to disable `swtch.' */
  89. #if defined (__sparc__) && defined (__svr4__)
  90. # undef CSWTCH
  91. # define CSWTCH _POSIX_VDISABLE
  92. #endif
  93. #if defined(VWERSE) && !defined (VWERASE) /* AIX-3.2.5 */
  94. # define VWERASE VWERSE
  95. #endif
  96. #if defined(VDSUSP) && !defined (CDSUSP)
  97. # define CDSUSP Control ('y')
  98. #endif
  99. #if !defined(VREPRINT) && defined(VRPRNT) /* Irix 4.0.5 */
  100. # define VREPRINT VRPRNT
  101. #endif
  102. #if defined(VREPRINT) && !defined(CRPRNT)
  103. # define CRPRNT Control ('r')
  104. #endif
  105. #if defined(VWERASE) && !defined(CWERASE)
  106. # define CWERASE Control ('w')
  107. #endif
  108. #if defined(VLNEXT) && !defined(CLNEXT)
  109. # define CLNEXT Control ('v')
  110. #endif
  111. #if defined(VDISCARD) && !defined(VFLUSHO)
  112. # define VFLUSHO VDISCARD
  113. #endif
  114. #if defined(VFLUSH) && !defined(VFLUSHO) /* Ultrix 4.2 */
  115. # define VFLUSHO VFLUSH
  116. #endif
  117. #if defined(CTLECH) && !defined(ECHOCTL) /* Ultrix 4.3 */
  118. # define ECHOCTL CTLECH
  119. #endif
  120. #if defined(TCTLECH) && !defined(ECHOCTL) /* Ultrix 4.2 */
  121. # define ECHOCTL TCTLECH
  122. #endif
  123. #if defined(CRTKIL) && !defined(ECHOKE) /* Ultrix 4.2 and 4.3 */
  124. # define ECHOKE CRTKIL
  125. #endif
  126. #if defined(VFLUSHO) && !defined(CFLUSHO)
  127. # define CFLUSHO Control ('o')
  128. #endif
  129. #if defined(VSTATUS) && !defined(CSTATUS)
  130. # define CSTATUS Control ('t')
  131. #endif
  132. /* Which speeds to set. */
  133. enum speed_setting {
  134. input_speed, output_speed, both_speeds
  135. };
  136. /* Which member(s) of `struct termios' a mode uses. */
  137. enum mode_type {
  138. /* Do NOT change the order or values, as mode_type_flag()
  139. * depends on them. */
  140. control, input, output, local, combination
  141. };
  142. static const char evenp [] = "evenp";
  143. static const char raw [] = "raw";
  144. static const char stty_min [] = "min";
  145. static const char stty_time [] = "time";
  146. static const char stty_swtch[] = "swtch";
  147. static const char stty_eol [] = "eol";
  148. static const char stty_eof [] = "eof";
  149. static const char parity [] = "parity";
  150. static const char stty_oddp [] = "oddp";
  151. static const char stty_nl [] = "nl";
  152. static const char stty_ek [] = "ek";
  153. static const char stty_sane [] = "sane";
  154. static const char cbreak [] = "cbreak";
  155. static const char stty_pass8[] = "pass8";
  156. static const char litout [] = "litout";
  157. static const char cooked [] = "cooked";
  158. static const char decctlq [] = "decctlq";
  159. static const char stty_tabs [] = "tabs";
  160. static const char stty_lcase[] = "lcase";
  161. static const char stty_LCASE[] = "LCASE";
  162. static const char stty_crt [] = "crt";
  163. static const char stty_dec [] = "dec";
  164. /* Flags for `struct mode_info'. */
  165. #define SANE_SET 1 /* Set in `sane' mode. */
  166. #define SANE_UNSET 2 /* Unset in `sane' mode. */
  167. #define REV 4 /* Can be turned off by prepending `-'. */
  168. #define OMIT 8 /* Don't display value. */
  169. /* Each mode. */
  170. struct mode_info {
  171. const char *name; /* Name given on command line. */
  172. /* enum mode_type type; */
  173. char type; /* Which structure element to change. */
  174. char flags; /* Setting and display options. */
  175. unsigned short mask; /* Other bits to turn off for this mode. */
  176. unsigned long bits; /* Bits to set for this mode. */
  177. };
  178. #define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B }
  179. static const struct mode_info mode_info[] = {
  180. MI_ENTRY("parenb", control, REV, PARENB, 0 ),
  181. MI_ENTRY("parodd", control, REV, PARODD, 0 ),
  182. MI_ENTRY("cs5", control, 0, CS5, CSIZE),
  183. MI_ENTRY("cs6", control, 0, CS6, CSIZE),
  184. MI_ENTRY("cs7", control, 0, CS7, CSIZE),
  185. MI_ENTRY("cs8", control, 0, CS8, CSIZE),
  186. MI_ENTRY("hupcl", control, REV, HUPCL, 0 ),
  187. MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 ),
  188. MI_ENTRY("cstopb", control, REV, CSTOPB, 0 ),
  189. MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 ),
  190. MI_ENTRY("clocal", control, REV, CLOCAL, 0 ),
  191. #ifdef CRTSCTS
  192. MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 ),
  193. #endif
  194. MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 ),
  195. MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 ),
  196. MI_ENTRY("ignpar", input, REV, IGNPAR, 0 ),
  197. MI_ENTRY("parmrk", input, REV, PARMRK, 0 ),
  198. MI_ENTRY("inpck", input, REV, INPCK, 0 ),
  199. MI_ENTRY("istrip", input, REV, ISTRIP, 0 ),
  200. MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 ),
  201. MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 ),
  202. MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 ),
  203. MI_ENTRY("ixon", input, REV, IXON, 0 ),
  204. MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 ),
  205. MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 ),
  206. #ifdef IUCLC
  207. MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 ),
  208. #endif
  209. #ifdef IXANY
  210. MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 ),
  211. #endif
  212. #ifdef IMAXBEL
  213. MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 ),
  214. #endif
  215. MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 ),
  216. #ifdef OLCUC
  217. MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 ),
  218. #endif
  219. #ifdef OCRNL
  220. MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 ),
  221. #endif
  222. #ifdef ONLCR
  223. MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 ),
  224. #endif
  225. #ifdef ONOCR
  226. MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 ),
  227. #endif
  228. #ifdef ONLRET
  229. MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 ),
  230. #endif
  231. #ifdef OFILL
  232. MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 ),
  233. #endif
  234. #ifdef OFDEL
  235. MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 ),
  236. #endif
  237. #ifdef NLDLY
  238. MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY),
  239. MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY),
  240. #endif
  241. #ifdef CRDLY
  242. MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY),
  243. MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY),
  244. MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY),
  245. MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY),
  246. #endif
  247. #ifdef TABDLY
  248. MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY),
  249. MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY),
  250. MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY),
  251. MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY),
  252. #else
  253. # ifdef OXTABS
  254. MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 ),
  255. # endif
  256. #endif
  257. #ifdef BSDLY
  258. MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY),
  259. MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY),
  260. #endif
  261. #ifdef VTDLY
  262. MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY),
  263. MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY),
  264. #endif
  265. #ifdef FFDLY
  266. MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY),
  267. MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY),
  268. #endif
  269. MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 ),
  270. MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 ),
  271. #ifdef IEXTEN
  272. MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 ),
  273. #endif
  274. MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 ),
  275. MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 ),
  276. MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 ),
  277. MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 ),
  278. MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 ),
  279. MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 ),
  280. #ifdef XCASE
  281. MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 ),
  282. #endif
  283. #ifdef TOSTOP
  284. MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 ),
  285. #endif
  286. #ifdef ECHOPRT
  287. MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 ),
  288. MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 ),
  289. #endif
  290. #ifdef ECHOCTL
  291. MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 ),
  292. MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 ),
  293. #endif
  294. #ifdef ECHOKE
  295. MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 ),
  296. MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 ),
  297. #endif
  298. MI_ENTRY(evenp, combination, REV | OMIT, 0, 0 ),
  299. MI_ENTRY(parity, combination, REV | OMIT, 0, 0 ),
  300. MI_ENTRY(stty_oddp, combination, REV | OMIT, 0, 0 ),
  301. MI_ENTRY(stty_nl, combination, REV | OMIT, 0, 0 ),
  302. MI_ENTRY(stty_ek, combination, OMIT, 0, 0 ),
  303. MI_ENTRY(stty_sane, combination, OMIT, 0, 0 ),
  304. MI_ENTRY(cooked, combination, REV | OMIT, 0, 0 ),
  305. MI_ENTRY(raw, combination, REV | OMIT, 0, 0 ),
  306. MI_ENTRY(stty_pass8, combination, REV | OMIT, 0, 0 ),
  307. MI_ENTRY(litout, combination, REV | OMIT, 0, 0 ),
  308. MI_ENTRY(cbreak, combination, REV | OMIT, 0, 0 ),
  309. #ifdef IXANY
  310. MI_ENTRY(decctlq, combination, REV | OMIT, 0, 0 ),
  311. #endif
  312. #if defined (TABDLY) || defined (OXTABS)
  313. MI_ENTRY(stty_tabs, combination, REV | OMIT, 0, 0 ),
  314. #endif
  315. #if defined(XCASE) && defined(IUCLC) && defined(OLCUC)
  316. MI_ENTRY(stty_lcase, combination, REV | OMIT, 0, 0 ),
  317. MI_ENTRY(stty_LCASE, combination, REV | OMIT, 0, 0 ),
  318. #endif
  319. MI_ENTRY(stty_crt, combination, OMIT, 0, 0 ),
  320. MI_ENTRY(stty_dec, combination, OMIT, 0, 0 ),
  321. };
  322. static const int NUM_mode_info =
  323. (sizeof(mode_info) / sizeof(struct mode_info));
  324. /* Control character settings. */
  325. struct control_info {
  326. const char *name; /* Name given on command line. */
  327. unsigned char saneval; /* Value to set for `stty sane'. */
  328. unsigned char offset; /* Offset in c_cc. */
  329. };
  330. /* Control characters. */
  331. static const struct control_info control_info[] = {
  332. {"intr", CINTR, VINTR},
  333. {"quit", CQUIT, VQUIT},
  334. {"erase", CERASE, VERASE},
  335. {"kill", CKILL, VKILL},
  336. {stty_eof, CEOF, VEOF},
  337. {stty_eol, CEOL, VEOL},
  338. #ifdef VEOL2
  339. {"eol2", CEOL2, VEOL2},
  340. #endif
  341. #ifdef VSWTCH
  342. {stty_swtch, CSWTCH, VSWTCH},
  343. #endif
  344. {"start", CSTART, VSTART},
  345. {"stop", CSTOP, VSTOP},
  346. {"susp", CSUSP, VSUSP},
  347. #ifdef VDSUSP
  348. {"dsusp", CDSUSP, VDSUSP},
  349. #endif
  350. #ifdef VREPRINT
  351. {"rprnt", CRPRNT, VREPRINT},
  352. #endif
  353. #ifdef VWERASE
  354. {"werase", CWERASE, VWERASE},
  355. #endif
  356. #ifdef VLNEXT
  357. {"lnext", CLNEXT, VLNEXT},
  358. #endif
  359. #ifdef VFLUSHO
  360. {"flush", CFLUSHO, VFLUSHO},
  361. #endif
  362. #ifdef VSTATUS
  363. {"status", CSTATUS, VSTATUS},
  364. #endif
  365. /* These must be last because of the display routines. */
  366. {stty_min, 1, VMIN},
  367. {stty_time, 0, VTIME},
  368. };
  369. static const int NUM_control_info =
  370. (sizeof(control_info) / sizeof(struct control_info));
  371. static const char * visible(unsigned int ch);
  372. static int recover_mode(char *arg, struct termios *mode);
  373. static int screen_columns(void);
  374. static int set_mode(const struct mode_info *info,
  375. int reversed, struct termios *mode);
  376. static speed_t string_to_baud(const char *arg);
  377. static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode);
  378. static void display_all(struct termios *mode, int fd);
  379. static void display_changed(struct termios *mode, int fd);
  380. static void display_recoverable(struct termios *mode, int fd);
  381. static void display_speed(struct termios *mode, int fancy);
  382. static void display_window_size(int fancy, int fd);
  383. static void sane_mode(struct termios *mode);
  384. static void set_control_char(const struct control_info *info,
  385. const char *arg, struct termios *mode);
  386. static void set_speed(enum speed_setting type,
  387. const char *arg, struct termios *mode);
  388. static void set_window_size(int rows, int cols, int fd);
  389. static const char *device_name;
  390. static __attribute__ ((noreturn)) void perror_on_device(const char *fmt)
  391. {
  392. bb_perror_msg_and_die(fmt, device_name);
  393. }
  394. /* The width of the screen, for output wrapping. */
  395. static int max_col;
  396. /* Current position, to know when to wrap. */
  397. static int current_col;
  398. /* Print format string MESSAGE and optional args.
  399. Wrap to next line first if it won't fit.
  400. Print a space first unless MESSAGE will start a new line. */
  401. static void wrapf(const char *message, ...)
  402. {
  403. va_list args;
  404. char buf[1024]; /* Plenty long for our needs. */
  405. int buflen;
  406. va_start(args, message);
  407. vsprintf(buf, message, args);
  408. va_end(args);
  409. buflen = strlen(buf);
  410. if (current_col + (current_col > 0) + buflen >= max_col) {
  411. putchar('\n');
  412. current_col = 0;
  413. }
  414. if (current_col > 0) {
  415. putchar(' ');
  416. current_col++;
  417. }
  418. fputs(buf, stdout);
  419. current_col += buflen;
  420. }
  421. static const struct suffix_mult stty_suffixes[] = {
  422. {"b", 512 },
  423. {"k", 1024},
  424. {"B", 1024},
  425. {NULL, 0 }
  426. };
  427. #ifndef TEST
  428. extern int stty_main(int argc, char **argv)
  429. #else
  430. extern int main(int argc, char **argv)
  431. #endif
  432. {
  433. struct termios mode;
  434. void (*output_func)(struct termios *, int);
  435. int optc;
  436. int require_set_attr;
  437. int speed_was_set;
  438. int verbose_output;
  439. int recoverable_output;
  440. int k;
  441. int noargs = 1;
  442. char * file_name = NULL;
  443. int fd;
  444. output_func = display_changed;
  445. verbose_output = 0;
  446. recoverable_output = 0;
  447. /* Don't print error messages for unrecognized options. */
  448. opterr = 0;
  449. while ((optc = getopt(argc, argv, "agF:")) != -1) {
  450. switch (optc) {
  451. case 'a':
  452. verbose_output = 1;
  453. output_func = display_all;
  454. break;
  455. case 'g':
  456. recoverable_output = 1;
  457. output_func = display_recoverable;
  458. break;
  459. case 'F':
  460. if (file_name)
  461. bb_error_msg_and_die("only one device may be specified");
  462. file_name = optarg;
  463. break;
  464. default: /* unrecognized option */
  465. noargs = 0;
  466. break;
  467. }
  468. if (noargs == 0)
  469. break;
  470. }
  471. if (optind < argc)
  472. noargs = 0;
  473. /* Specifying both -a and -g gets an error. */
  474. if (verbose_output & recoverable_output)
  475. bb_error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive");
  476. /* Specifying any other arguments with -a or -g gets an error. */
  477. if (~noargs & (verbose_output | recoverable_output))
  478. bb_error_msg_and_die ("modes may not be set when specifying an output style");
  479. /* FIXME: it'd be better not to open the file until we've verified
  480. that all arguments are valid. Otherwise, we could end up doing
  481. only some of the requested operations and then failing, probably
  482. leaving things in an undesirable state. */
  483. if (file_name) {
  484. int fdflags;
  485. device_name = file_name;
  486. fd = bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
  487. if ((fdflags = fcntl(fd, F_GETFL)) == -1
  488. || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
  489. perror_on_device("%s: couldn't reset non-blocking mode");
  490. } else {
  491. fd = 0;
  492. device_name = bb_msg_standard_input;
  493. }
  494. /* Initialize to all zeroes so there is no risk memcmp will report a
  495. spurious difference in an uninitialized portion of the structure. */
  496. memset(&mode, 0, sizeof(mode));
  497. if (tcgetattr(fd, &mode))
  498. perror_on_device("%s");
  499. if (verbose_output | recoverable_output | noargs) {
  500. max_col = screen_columns();
  501. current_col = 0;
  502. output_func(&mode, fd);
  503. return EXIT_SUCCESS;
  504. }
  505. speed_was_set = 0;
  506. require_set_attr = 0;
  507. k = 0;
  508. while (++k < argc) {
  509. int match_found = 0;
  510. int reversed = 0;
  511. int i;
  512. if (argv[k][0] == '-') {
  513. char *find_dev_opt;
  514. ++argv[k];
  515. /* Handle "-a", "-ag", "-aF/dev/foo", "-aF /dev/foo", etc.
  516. Find the options that have been parsed. This is really
  517. gross, but it's needed because stty SETTINGS look like options to
  518. getopt(), so we need to work around things in a really horrible
  519. way. If any new options are ever added to stty, the short option
  520. MUST NOT be a letter which is the first letter of one of the
  521. possible stty settings.
  522. */
  523. find_dev_opt = strchr(argv[k], 'F'); /* find -*F* */
  524. if(find_dev_opt) {
  525. if(find_dev_opt[1]==0) /* -*F /dev/foo */
  526. k++; /* skip /dev/foo */
  527. continue; /* else -*F/dev/foo - no skip */
  528. }
  529. if(argv[k][0]=='a' || argv[k][0]=='g')
  530. continue;
  531. /* Is not options - is reverse params */
  532. reversed = 1;
  533. }
  534. for (i = 0; i < NUM_mode_info; ++i)
  535. if (STREQ(argv[k], mode_info[i].name)) {
  536. match_found = set_mode(&mode_info[i], reversed, &mode);
  537. require_set_attr = 1;
  538. break;
  539. }
  540. if (match_found == 0 && reversed)
  541. bb_error_msg_and_die("invalid argument `%s'", --argv[k]);
  542. if (match_found == 0)
  543. for (i = 0; i < NUM_control_info; ++i)
  544. if (STREQ(argv[k], control_info[i].name)) {
  545. if (k == argc - 1)
  546. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  547. match_found = 1;
  548. ++k;
  549. set_control_char(&control_info[i], argv[k], &mode);
  550. require_set_attr = 1;
  551. break;
  552. }
  553. if (match_found == 0) {
  554. if (STREQ(argv[k], "ispeed")) {
  555. if (k == argc - 1)
  556. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  557. ++k;
  558. set_speed(input_speed, argv[k], &mode);
  559. speed_was_set = 1;
  560. require_set_attr = 1;
  561. } else if (STREQ(argv[k], "ospeed")) {
  562. if (k == argc - 1)
  563. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  564. ++k;
  565. set_speed(output_speed, argv[k], &mode);
  566. speed_was_set = 1;
  567. require_set_attr = 1;
  568. }
  569. #ifdef TIOCGWINSZ
  570. else if (STREQ(argv[k], "rows")) {
  571. if (k == argc - 1)
  572. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  573. ++k;
  574. set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
  575. -1, fd);
  576. } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
  577. if (k == argc - 1)
  578. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  579. ++k;
  580. set_window_size(-1,
  581. (int) bb_xparse_number(argv[k], stty_suffixes),
  582. fd);
  583. } else if (STREQ(argv[k], "size")) {
  584. max_col = screen_columns();
  585. current_col = 0;
  586. display_window_size(0, fd);
  587. }
  588. #endif
  589. #ifdef HAVE_C_LINE
  590. else if (STREQ(argv[k], "line")) {
  591. if (k == argc - 1)
  592. bb_error_msg_and_die("missing argument to `%s'", argv[k]);
  593. ++k;
  594. mode.c_line = bb_xparse_number(argv[k], stty_suffixes);
  595. require_set_attr = 1;
  596. }
  597. #endif
  598. else if (STREQ(argv[k], "speed")) {
  599. max_col = screen_columns();
  600. display_speed(&mode, 0);
  601. } else if (recover_mode(argv[k], &mode) == 1)
  602. require_set_attr = 1;
  603. else if (string_to_baud(argv[k]) != (speed_t) - 1) {
  604. set_speed(both_speeds, argv[k], &mode);
  605. speed_was_set = 1;
  606. require_set_attr = 1;
  607. } else
  608. bb_error_msg_and_die("invalid argument `%s'", argv[k]);
  609. }
  610. }
  611. if (require_set_attr) {
  612. struct termios new_mode;
  613. if (tcsetattr(fd, TCSADRAIN, &mode))
  614. perror_on_device("%s");
  615. /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
  616. it performs *any* of the requested operations. This means it
  617. can report `success' when it has actually failed to perform
  618. some proper subset of the requested operations. To detect
  619. this partial failure, get the current terminal attributes and
  620. compare them to the requested ones. */
  621. /* Initialize to all zeroes so there is no risk memcmp will report a
  622. spurious difference in an uninitialized portion of the structure. */
  623. memset(&new_mode, 0, sizeof(new_mode));
  624. if (tcgetattr(fd, &new_mode))
  625. perror_on_device("%s");
  626. /* Normally, one shouldn't use memcmp to compare structures that
  627. may have `holes' containing uninitialized data, but we have been
  628. careful to initialize the storage of these two variables to all
  629. zeroes. One might think it more efficient simply to compare the
  630. modified fields, but that would require enumerating those fields --
  631. and not all systems have the same fields in this structure. */
  632. if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
  633. #ifdef CIBAUD
  634. /* SunOS 4.1.3 (at least) has the problem that after this sequence,
  635. tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
  636. sometimes (m1 != m2). The only difference is in the four bits
  637. of the c_cflag field corresponding to the baud rate. To save
  638. Sun users a little confusion, don't report an error if this
  639. happens. But suppress the error only if we haven't tried to
  640. set the baud rate explicitly -- otherwise we'd never give an
  641. error for a true failure to set the baud rate. */
  642. new_mode.c_cflag &= (~CIBAUD);
  643. if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
  644. #endif
  645. perror_on_device ("%s: unable to perform all requested operations");
  646. }
  647. }
  648. return EXIT_SUCCESS;
  649. }
  650. /* Return 0 if not applied because not reversible; otherwise return 1. */
  651. static int
  652. set_mode(const struct mode_info *info, int reversed, struct termios *mode)
  653. {
  654. tcflag_t *bitsp;
  655. if (reversed && (info->flags & REV) == 0)
  656. return 0;
  657. bitsp = mode_type_flag(info->type, mode);
  658. if (bitsp == NULL) {
  659. /* Combination mode. */
  660. if (info->name == evenp || info->name == parity) {
  661. if (reversed)
  662. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  663. else
  664. mode->c_cflag =
  665. (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
  666. } else if (info->name == stty_oddp) {
  667. if (reversed)
  668. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  669. else
  670. mode->c_cflag =
  671. (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
  672. } else if (info->name == stty_nl) {
  673. if (reversed) {
  674. mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
  675. mode->c_oflag = (mode->c_oflag
  676. #ifdef ONLCR
  677. | ONLCR
  678. #endif
  679. )
  680. #ifdef OCRNL
  681. & ~OCRNL
  682. #endif
  683. #ifdef ONLRET
  684. & ~ONLRET
  685. #endif
  686. ;
  687. } else {
  688. mode->c_iflag = mode->c_iflag & ~ICRNL;
  689. #ifdef ONLCR
  690. mode->c_oflag = mode->c_oflag & ~ONLCR;
  691. #endif
  692. }
  693. } else if (info->name == stty_ek) {
  694. mode->c_cc[VERASE] = CERASE;
  695. mode->c_cc[VKILL] = CKILL;
  696. } else if (info->name == stty_sane)
  697. sane_mode(mode);
  698. else if (info->name == cbreak) {
  699. if (reversed)
  700. mode->c_lflag |= ICANON;
  701. else
  702. mode->c_lflag &= ~ICANON;
  703. } else if (info->name == stty_pass8) {
  704. if (reversed) {
  705. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  706. mode->c_iflag |= ISTRIP;
  707. } else {
  708. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  709. mode->c_iflag &= ~ISTRIP;
  710. }
  711. } else if (info->name == litout) {
  712. if (reversed) {
  713. mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
  714. mode->c_iflag |= ISTRIP;
  715. mode->c_oflag |= OPOST;
  716. } else {
  717. mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
  718. mode->c_iflag &= ~ISTRIP;
  719. mode->c_oflag &= ~OPOST;
  720. }
  721. } else if (info->name == raw || info->name == cooked) {
  722. if ((info->name[0] == 'r' && reversed)
  723. || (info->name[0] == 'c' && !reversed)) {
  724. /* Cooked mode. */
  725. mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
  726. mode->c_oflag |= OPOST;
  727. mode->c_lflag |= ISIG | ICANON;
  728. #if VMIN == VEOF
  729. mode->c_cc[VEOF] = CEOF;
  730. #endif
  731. #if VTIME == VEOL
  732. mode->c_cc[VEOL] = CEOL;
  733. #endif
  734. } else {
  735. /* Raw mode. */
  736. mode->c_iflag = 0;
  737. mode->c_oflag &= ~OPOST;
  738. mode->c_lflag &= ~(ISIG | ICANON
  739. #ifdef XCASE
  740. | XCASE
  741. #endif
  742. );
  743. mode->c_cc[VMIN] = 1;
  744. mode->c_cc[VTIME] = 0;
  745. }
  746. }
  747. #ifdef IXANY
  748. else if (info->name == decctlq) {
  749. if (reversed)
  750. mode->c_iflag |= IXANY;
  751. else
  752. mode->c_iflag &= ~IXANY;
  753. }
  754. #endif
  755. #ifdef TABDLY
  756. else if (info->name == stty_tabs) {
  757. if (reversed)
  758. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
  759. else
  760. mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
  761. }
  762. #else
  763. # ifdef OXTABS
  764. else if (info->name == stty_tabs) {
  765. if (reversed)
  766. mode->c_oflag = mode->c_oflag | OXTABS;
  767. else
  768. mode->c_oflag = mode->c_oflag & ~OXTABS;
  769. }
  770. # endif
  771. #endif
  772. #if defined(XCASE) && defined(IUCLC) && defined(OLCUC)
  773. else if (info->name == stty_lcase || info->name == stty_LCASE) {
  774. if (reversed) {
  775. mode->c_lflag &= ~XCASE;
  776. mode->c_iflag &= ~IUCLC;
  777. mode->c_oflag &= ~OLCUC;
  778. } else {
  779. mode->c_lflag |= XCASE;
  780. mode->c_iflag |= IUCLC;
  781. mode->c_oflag |= OLCUC;
  782. }
  783. }
  784. #endif
  785. else if (info->name == stty_crt)
  786. mode->c_lflag |= ECHOE
  787. #ifdef ECHOCTL
  788. | ECHOCTL
  789. #endif
  790. #ifdef ECHOKE
  791. | ECHOKE
  792. #endif
  793. ;
  794. else if (info->name == stty_dec) {
  795. mode->c_cc[VINTR] = 3; /* ^C */
  796. mode->c_cc[VERASE] = 127; /* DEL */
  797. mode->c_cc[VKILL] = 21; /* ^U */
  798. mode->c_lflag |= ECHOE
  799. #ifdef ECHOCTL
  800. | ECHOCTL
  801. #endif
  802. #ifdef ECHOKE
  803. | ECHOKE
  804. #endif
  805. ;
  806. #ifdef IXANY
  807. mode->c_iflag &= ~IXANY;
  808. #endif
  809. }
  810. } else if (reversed)
  811. *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits;
  812. else
  813. *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits;
  814. return 1;
  815. }
  816. static void
  817. set_control_char(const struct control_info *info, const char *arg,
  818. struct termios *mode)
  819. {
  820. unsigned char value;
  821. if (info->name == stty_min || info->name == stty_time)
  822. value = bb_xparse_number(arg, stty_suffixes);
  823. else if (arg[0] == '\0' || arg[1] == '\0')
  824. value = arg[0];
  825. else if (STREQ(arg, "^-") || STREQ(arg, "undef"))
  826. value = _POSIX_VDISABLE;
  827. else if (arg[0] == '^' && arg[1] != '\0') { /* Ignore any trailing junk. */
  828. if (arg[1] == '?')
  829. value = 127;
  830. else
  831. value = arg[1] & ~0140; /* Non-letters get weird results. */
  832. } else
  833. value = bb_xparse_number(arg, stty_suffixes);
  834. mode->c_cc[info->offset] = value;
  835. }
  836. static void
  837. set_speed(enum speed_setting type, const char *arg, struct termios *mode)
  838. {
  839. speed_t baud;
  840. baud = string_to_baud(arg);
  841. if (type != output_speed) { /* either input or both */
  842. cfsetispeed(mode, baud);
  843. }
  844. if (type != input_speed) { /* either output or both */
  845. cfsetospeed(mode, baud);
  846. }
  847. }
  848. #ifdef TIOCGWINSZ
  849. static int get_win_size(int fd, struct winsize *win)
  850. {
  851. int err = ioctl(fd, TIOCGWINSZ, (char *) win);
  852. return err;
  853. }
  854. static void
  855. set_window_size(int rows, int cols, int fd)
  856. {
  857. struct winsize win;
  858. if (get_win_size(fd, &win)) {
  859. if (errno != EINVAL)
  860. perror_on_device("%s");
  861. memset(&win, 0, sizeof(win));
  862. }
  863. if (rows >= 0)
  864. win.ws_row = rows;
  865. if (cols >= 0)
  866. win.ws_col = cols;
  867. # ifdef TIOCSSIZE
  868. /* Alexander Dupuy <dupuy@cs.columbia.edu> wrote:
  869. The following code deals with a bug in the SunOS 4.x (and 3.x?) kernel.
  870. This comment from sys/ttold.h describes Sun's twisted logic - a better
  871. test would have been (ts_lines > 64k || ts_cols > 64k || ts_cols == 0).
  872. At any rate, the problem is gone in Solaris 2.x. */
  873. if (win.ws_row == 0 || win.ws_col == 0) {
  874. struct ttysize ttysz;
  875. ttysz.ts_lines = win.ws_row;
  876. ttysz.ts_cols = win.ws_col;
  877. win.ws_row = win.ws_col = 1;
  878. if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
  879. || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
  880. perror_on_device("%s");
  881. }
  882. return;
  883. }
  884. # endif
  885. if (ioctl(fd, TIOCSWINSZ, (char *) &win))
  886. perror_on_device("%s");
  887. }
  888. static void display_window_size(int fancy, int fd)
  889. {
  890. const char *fmt_str = "%s" "\0" "%s: no size information for this device";
  891. struct winsize win;
  892. if (get_win_size(fd, &win)) {
  893. if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
  894. perror_on_device(fmt_str);
  895. }
  896. } else {
  897. wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
  898. win.ws_row, win.ws_col);
  899. if (!fancy)
  900. current_col = 0;
  901. }
  902. }
  903. #endif
  904. static int screen_columns(void)
  905. {
  906. int columns;
  907. const char *s;
  908. #ifdef TIOCGWINSZ
  909. struct winsize win;
  910. /* With Solaris 2.[123], this ioctl fails and errno is set to
  911. EINVAL for telnet (but not rlogin) sessions.
  912. On ISC 3.0, it fails for the console and the serial port
  913. (but it works for ptys).
  914. It can also fail on any system when stdout isn't a tty.
  915. In case of any failure, just use the default. */
  916. if (get_win_size(STDOUT_FILENO, &win) == 0 && win.ws_col > 0)
  917. return win.ws_col;
  918. #endif
  919. columns = 80;
  920. if ((s = getenv("COLUMNS"))) {
  921. columns = atoi(s);
  922. }
  923. return columns;
  924. }
  925. static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode)
  926. {
  927. static const unsigned char tcflag_offsets[] = {
  928. offsetof(struct termios, c_cflag), /* control */
  929. offsetof(struct termios, c_iflag), /* input */
  930. offsetof(struct termios, c_oflag), /* output */
  931. offsetof(struct termios, c_lflag) /* local */
  932. };
  933. if (((unsigned int) type) <= local) {
  934. return (tcflag_t *)(((char *) mode) + tcflag_offsets[(int)type]);
  935. }
  936. return NULL;
  937. }
  938. static void display_changed(struct termios *mode, int fd)
  939. {
  940. int i;
  941. int empty_line;
  942. tcflag_t *bitsp;
  943. unsigned long mask;
  944. enum mode_type prev_type = control;
  945. display_speed(mode, 1);
  946. #ifdef HAVE_C_LINE
  947. wrapf("line = %d;", mode->c_line);
  948. #endif
  949. putchar('\n');
  950. current_col = 0;
  951. empty_line = 1;
  952. for (i = 0; control_info[i].name != stty_min; ++i) {
  953. if (mode->c_cc[control_info[i].offset] == control_info[i].saneval)
  954. continue;
  955. /* If swtch is the same as susp, don't print both. */
  956. #if VSWTCH == VSUSP
  957. if (control_info[i].name == stty_swtch)
  958. continue;
  959. #endif
  960. /* If eof uses the same slot as min, only print whichever applies. */
  961. #if VEOF == VMIN
  962. if ((mode->c_lflag & ICANON) == 0
  963. && (control_info[i].name == stty_eof
  964. || control_info[i].name == stty_eol)) continue;
  965. #endif
  966. empty_line = 0;
  967. wrapf("%s = %s;", control_info[i].name,
  968. visible(mode->c_cc[control_info[i].offset]));
  969. }
  970. if ((mode->c_lflag & ICANON) == 0) {
  971. wrapf("min = %d; time = %d;\n", (int) mode->c_cc[VMIN],
  972. (int) mode->c_cc[VTIME]);
  973. } else if (empty_line == 0)
  974. putchar('\n');
  975. current_col = 0;
  976. empty_line = 1;
  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. if (empty_line == 0) {
  982. putchar('\n');
  983. current_col = 0;
  984. empty_line = 1;
  985. }
  986. prev_type = mode_info[i].type;
  987. }
  988. bitsp = mode_type_flag(mode_info[i].type, mode);
  989. mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
  990. if ((*bitsp & mask) == mode_info[i].bits) {
  991. if (mode_info[i].flags & SANE_UNSET) {
  992. wrapf("%s", mode_info[i].name);
  993. empty_line = 0;
  994. }
  995. }
  996. else if ((mode_info[i].flags & (SANE_SET | REV)) ==
  997. (SANE_SET | REV)) {
  998. wrapf("-%s", mode_info[i].name);
  999. empty_line = 0;
  1000. }
  1001. }
  1002. if (empty_line == 0)
  1003. putchar('\n');
  1004. current_col = 0;
  1005. }
  1006. static void
  1007. display_all(struct termios *mode, int fd)
  1008. {
  1009. int i;
  1010. tcflag_t *bitsp;
  1011. unsigned long mask;
  1012. enum mode_type prev_type = control;
  1013. display_speed(mode, 1);
  1014. #ifdef TIOCGWINSZ
  1015. display_window_size(1, fd);
  1016. #endif
  1017. #ifdef HAVE_C_LINE
  1018. wrapf("line = %d;", mode->c_line);
  1019. #endif
  1020. putchar('\n');
  1021. current_col = 0;
  1022. for (i = 0; control_info[i].name != stty_min; ++i) {
  1023. /* If swtch is the same as susp, don't print both. */
  1024. #if VSWTCH == VSUSP
  1025. if (control_info[i].name == stty_swtch)
  1026. continue;
  1027. #endif
  1028. /* If eof uses the same slot as min, only print whichever applies. */
  1029. #if VEOF == VMIN
  1030. if ((mode->c_lflag & ICANON) == 0
  1031. && (control_info[i].name == stty_eof
  1032. || control_info[i].name == stty_eol)) continue;
  1033. #endif
  1034. wrapf("%s = %s;", control_info[i].name,
  1035. visible(mode->c_cc[control_info[i].offset]));
  1036. }
  1037. #if VEOF == VMIN
  1038. if ((mode->c_lflag & ICANON) == 0)
  1039. #endif
  1040. wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
  1041. if (current_col != 0)
  1042. putchar('\n');
  1043. current_col = 0;
  1044. for (i = 0; i < NUM_mode_info; ++i) {
  1045. if (mode_info[i].flags & OMIT)
  1046. continue;
  1047. if (mode_info[i].type != prev_type) {
  1048. putchar('\n');
  1049. current_col = 0;
  1050. prev_type = mode_info[i].type;
  1051. }
  1052. bitsp = mode_type_flag(mode_info[i].type, mode);
  1053. mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
  1054. if ((*bitsp & mask) == mode_info[i].bits)
  1055. wrapf("%s", mode_info[i].name);
  1056. else if (mode_info[i].flags & REV)
  1057. wrapf("-%s", mode_info[i].name);
  1058. }
  1059. putchar('\n');
  1060. current_col = 0;
  1061. }
  1062. static void display_speed(struct termios *mode, int fancy)
  1063. {
  1064. unsigned long ispeed, ospeed;
  1065. const char *fmt_str =
  1066. "%lu %lu\n\0" "ispeed %lu baud; ospeed %lu baud;\0"
  1067. "%lu\n\0" "\0\0\0\0" "speed %lu baud;";
  1068. ospeed = ispeed = cfgetispeed(mode);
  1069. if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
  1070. ispeed = ospeed; /* in case ispeed was 0 */
  1071. fmt_str += 43;
  1072. }
  1073. if (fancy) {
  1074. fmt_str += 9;
  1075. }
  1076. wrapf(fmt_str, bb_baud_to_value(ispeed), bb_baud_to_value(ospeed));
  1077. if (!fancy)
  1078. current_col = 0;
  1079. }
  1080. static void display_recoverable(struct termios *mode, int fd)
  1081. {
  1082. int i;
  1083. printf("%lx:%lx:%lx:%lx",
  1084. (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
  1085. (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
  1086. for (i = 0; i < NCCS; ++i)
  1087. printf(":%x", (unsigned int) mode->c_cc[i]);
  1088. putchar('\n');
  1089. }
  1090. static int recover_mode(char *arg, struct termios *mode)
  1091. {
  1092. int i, n;
  1093. unsigned int chr;
  1094. unsigned long iflag, oflag, cflag, lflag;
  1095. /* Scan into temporaries since it is too much trouble to figure out
  1096. the right format for `tcflag_t'. */
  1097. if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
  1098. &iflag, &oflag, &cflag, &lflag, &n) != 4)
  1099. return 0;
  1100. mode->c_iflag = iflag;
  1101. mode->c_oflag = oflag;
  1102. mode->c_cflag = cflag;
  1103. mode->c_lflag = lflag;
  1104. arg += n;
  1105. for (i = 0; i < NCCS; ++i) {
  1106. if (sscanf(arg, ":%x%n", &chr, &n) != 1)
  1107. return 0;
  1108. mode->c_cc[i] = chr;
  1109. arg += n;
  1110. }
  1111. /* Fail if there are too many fields. */
  1112. if (*arg != '\0')
  1113. return 0;
  1114. return 1;
  1115. }
  1116. static speed_t string_to_baud(const char *arg)
  1117. {
  1118. return bb_value_to_baud(bb_xparse_number(arg, 0));
  1119. }
  1120. static void sane_mode(struct termios *mode)
  1121. {
  1122. int i;
  1123. tcflag_t *bitsp;
  1124. for (i = 0; i < NUM_control_info; ++i) {
  1125. #if VMIN == VEOF
  1126. if (control_info[i].name == stty_min)
  1127. break;
  1128. #endif
  1129. mode->c_cc[control_info[i].offset] = control_info[i].saneval;
  1130. }
  1131. for (i = 0; i < NUM_mode_info; ++i) {
  1132. if (mode_info[i].flags & SANE_SET) {
  1133. bitsp = mode_type_flag(mode_info[i].type, mode);
  1134. *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
  1135. | mode_info[i].bits;
  1136. } else if (mode_info[i].flags & SANE_UNSET) {
  1137. bitsp = mode_type_flag(mode_info[i].type, mode);
  1138. *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
  1139. & ~mode_info[i].bits;
  1140. }
  1141. }
  1142. }
  1143. /* Return a string that is the printable representation of character CH. */
  1144. /* Adapted from `cat' by Torbjorn Granlund. */
  1145. static const char *visible(unsigned int ch)
  1146. {
  1147. static char buf[10];
  1148. char *bpout = buf;
  1149. if (ch == _POSIX_VDISABLE) {
  1150. return "<undef>";
  1151. }
  1152. if (ch >= 128) {
  1153. ch -= 128;
  1154. *bpout++ = 'M';
  1155. *bpout++ = '-';
  1156. }
  1157. if (ch < 32) {
  1158. *bpout++ = '^';
  1159. *bpout++ = ch + 64;
  1160. } else if (ch < 127) {
  1161. *bpout++ = ch;
  1162. } else {
  1163. *bpout++ = '^';
  1164. *bpout++ = '?';
  1165. }
  1166. *bpout = '\0';
  1167. return (const char *) buf;
  1168. }
  1169. #ifdef TEST
  1170. const char *bb_applet_name = "stty";
  1171. #endif