stty.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <ctype.h>
  11. #include <termios.h>
  12. #include <sys/ioctl.h>
  13. static int hide_defaults = 1;
  14. static int printed = 0;
  15. static void print_cc(struct termios * t, const char * lbl, int val, int def) {
  16. int c = t->c_cc[val];
  17. if (hide_defaults && c == def) return;
  18. if (!c) {
  19. fprintf(stdout, "%s = <undef>; ", lbl);
  20. } else if (c < 32) {
  21. fprintf(stdout, "%s = ^%c; ", lbl, '@' + c);
  22. } else if (c == 0x7F) {
  23. fprintf(stdout, "%s = ^?; ", lbl);
  24. } else {
  25. fprintf(stdout, "%s = %c; ", lbl, c);
  26. }
  27. printed = 1;
  28. }
  29. static void print_(int flags, const char * lbl, int val, int def) {
  30. int c = !!(flags & val);
  31. if (!hide_defaults || c != def) {
  32. fprintf(stdout, "%s%s ", c ? "" : "-", lbl);
  33. printed = 1;
  34. }
  35. }
  36. #define print_cflag(lbl,val,def) print_(t.c_cflag, lbl, val, def)
  37. #define print_iflag(lbl,val,def) print_(t.c_iflag, lbl, val, def)
  38. #define print_oflag(lbl,val,def) print_(t.c_oflag, lbl, val, def)
  39. #define print_lflag(lbl,val,def) print_(t.c_lflag, lbl, val, def)
  40. static void set_char_(struct termios *t, const char * lbl, int val, const char * cmp, const char * arg, int * i) {
  41. if (!strcmp(cmp, lbl)) {
  42. if (!arg) {
  43. fprintf(stderr, "%s: expected argument\n", lbl);
  44. exit(1);
  45. }
  46. /* Parse arg */
  47. if (strlen(arg) == 1) {
  48. /* Assume raw character */
  49. t->c_cc[val] = *arg;
  50. } else if (*arg == '^') { /* ^c, etc. */
  51. int v = toupper(arg[1]);
  52. if (v == '?') { /* special case */
  53. t->c_cc[val] = 0x7F;
  54. } else {
  55. t->c_cc[val] = v - '@';
  56. }
  57. } else {
  58. /* Assume decimal for now */
  59. int v = atoi(arg);
  60. t->c_cc[val] = v;
  61. }
  62. (*i)++;
  63. }
  64. }
  65. #define set_char(lbl,val) set_char_(&t, lbl, val, argv[i], argv[i+1], &i)
  66. static void setunset_flag(tcflag_t * flag, const char * lbl, int val, const char * cmp) {
  67. if (*cmp == '-') {
  68. if (!strcmp(cmp+1, lbl)) {
  69. (*flag) = (*flag) & (~val);
  70. }
  71. } else {
  72. if (!strcmp(cmp, lbl)) {
  73. (*flag) = (*flag) | (val);
  74. }
  75. }
  76. }
  77. #define set_cflag(lbl,val) setunset_flag(&(t.c_cflag), lbl, val, argv[i])
  78. #define set_iflag(lbl,val) setunset_flag(&(t.c_iflag), lbl, val, argv[i])
  79. #define set_oflag(lbl,val) setunset_flag(&(t.c_oflag), lbl, val, argv[i])
  80. #define set_lflag(lbl,val) setunset_flag(&(t.c_lflag), lbl, val, argv[i])
  81. static void set_toggle_(tcflag_t * flag, const char * lbl, int base, int val, const char * cmp) {
  82. if (!strcmp(cmp, lbl)) {
  83. (*flag) = (*flag) & ~(base);
  84. (*flag) = (*flag) | (val);
  85. }
  86. }
  87. #define set_ctoggle(lbl,base,val) set_toggle_(&(t.c_cflag), lbl, base, val, argv[i])
  88. #define set_otoggle(lbl,base,val) set_toggle_(&(t.c_oflag), lbl, base, val, argv[i])
  89. static int show_settings(int all) {
  90. /* Size */
  91. struct winsize w;
  92. ioctl(STDERR_FILENO, TIOCGWINSZ, &w);
  93. fprintf(stdout, "rows %d; columns %d; ypixels %d; xpixels %d;\n", w.ws_row, w.ws_col, w.ws_ypixel, w.ws_xpixel);
  94. printed = 0;
  95. struct termios t;
  96. tcgetattr(STDERR_FILENO, &t);
  97. /* Keys */
  98. print_cc(&t, "intr", VINTR, 3);
  99. print_cc(&t, "quit", VQUIT, 28);
  100. print_cc(&t, "erase", VERASE, 0x7F);
  101. print_cc(&t, "kill", VKILL, 21);
  102. print_cc(&t, "eof", VEOF, 4);
  103. print_cc(&t, "eol", VEOL, 0);
  104. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  105. print_cc(&t, "start", VSTART, 17);
  106. print_cc(&t, "stop", VSTOP, 19);
  107. print_cc(&t, "susp", VSUSP, 26);
  108. print_cc(&t, "lnext", VLNEXT, 22);
  109. print_cc(&t, "werase",VWERASE, 23);
  110. /* MIN, TIME */
  111. if (!hide_defaults || t.c_cc[VMIN] != 1) { fprintf(stdout, "min = %d; ", t.c_cc[VMIN]); printed = 1; }
  112. if (!hide_defaults || t.c_cc[VTIME] != 0) { fprintf(stdout, "time = %d; ", t.c_cc[VTIME]); printed = 1; }
  113. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  114. switch ((t.c_cflag & CSIZE)) {
  115. case CS5: fprintf(stdout, "cs5 "); printed = 1; break;
  116. case CS6: fprintf(stdout, "cs6 "); printed = 1; break;
  117. case CS7: fprintf(stdout, "cs7 "); printed = 1; break;
  118. case CS8: if (!hide_defaults) { fprintf(stdout, "cs8 "); printed = 1; } break;
  119. }
  120. print_cflag("cstopb", CSTOPB, 0);
  121. print_cflag("cread", CREAD, 1);
  122. print_cflag("parenb", PARENB, 0);
  123. print_cflag("parodd", PARODD, 0);
  124. print_cflag("hupcl", HUPCL, 0);
  125. print_cflag("clocal", CLOCAL, 0);
  126. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  127. print_iflag("brkint", BRKINT, 1);
  128. print_iflag("icrnl", ICRNL, 1);
  129. print_iflag("ignbrk", IGNBRK, 0);
  130. print_iflag("igncr", IGNCR, 0);
  131. print_iflag("ignpar", IGNPAR, 0);
  132. print_iflag("inlcr", INLCR, 0);
  133. print_iflag("inpck", INPCK, 0);
  134. print_iflag("istrip", ISTRIP, 0);
  135. print_iflag("ixany", IXANY, 0);
  136. print_iflag("ixoff", IXOFF, 0);
  137. print_iflag("ixon", IXON, 0);
  138. print_iflag("parmrk", PARMRK, 0);
  139. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  140. print_oflag("opost", OPOST, 1);
  141. print_oflag("olcuc", OLCUC, 0);
  142. print_oflag("onlcr", ONLCR, 1);
  143. print_oflag("ocrnl", OCRNL, 0);
  144. print_oflag("onocr", ONOCR, 0);
  145. print_oflag("onlret", ONLRET, 0);
  146. print_oflag("ofill", OFILL, 0);
  147. print_oflag("ofdel", OFDEL, 0);
  148. switch ((t.c_oflag & CRDLY)) {
  149. case CR0: if (!hide_defaults) { fprintf(stdout, "cr0 "); printed = 1; } break;
  150. case CR1: fprintf(stdout, "cr1 "); printed = 1; break;
  151. case CR2: fprintf(stdout, "cr2 "); printed = 1; break;
  152. case CR3: fprintf(stdout, "cr3 "); printed = 1; break;
  153. }
  154. switch ((t.c_oflag & NLDLY)) {
  155. case NL0: if (!hide_defaults) { fprintf(stdout, "nl0 "); printed = 1; } break;
  156. case NL1: fprintf(stdout, "nl1 "); printed = 1; break;
  157. }
  158. switch ((t.c_oflag & TABDLY)) {
  159. case TAB0: if (!hide_defaults) { fprintf(stdout, "tab0 "); printed = 1; } break;
  160. case TAB1: fprintf(stdout, "tab1 "); printed = 1; break;
  161. case TAB2: fprintf(stdout, "tab2 "); printed = 1; break;
  162. case TAB3: fprintf(stdout, "tab3 "); printed = 1; break;
  163. }
  164. switch ((t.c_oflag & BSDLY)) {
  165. case BS0: if (!hide_defaults) { fprintf(stdout, "bs0 "); printed = 1; } break;
  166. case BS1: fprintf(stdout, "bs1 "); printed = 1; break;
  167. }
  168. switch ((t.c_oflag & FFDLY)) {
  169. case FF0: if (!hide_defaults) { fprintf(stdout, "ff0 "); printed = 1; } break;
  170. case FF1: fprintf(stdout, "ff1 "); printed = 1; break;
  171. }
  172. switch ((t.c_oflag & VTDLY)) {
  173. case VT0: if (!hide_defaults) { fprintf(stdout, "vt0"); printed = 1; } break;
  174. case VT1: fprintf(stdout, "vt1"); printed = 1; break;
  175. }
  176. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  177. print_lflag("isig", ISIG, 1);
  178. print_lflag("icanon", ICANON, 1);
  179. print_lflag("xcase", XCASE, 0);
  180. print_lflag("echo", ECHO, 1);
  181. print_lflag("echoe", ECHOE, 1);
  182. print_lflag("echok", ECHOK, 1);
  183. print_lflag("echonl", ECHONL, 0);
  184. print_lflag("noflsh", NOFLSH, 0);
  185. print_lflag("tostop", TOSTOP, 0);
  186. print_lflag("iexten", IEXTEN, 1);
  187. if (printed) { fprintf(stdout, "\n"); printed = 0; }
  188. return 0;
  189. }
  190. int main(int argc, char * argv[]) {
  191. int i = 1;
  192. if (i < argc && !strcmp(argv[i], "-a")) {
  193. hide_defaults = 0;
  194. i++;
  195. }
  196. if (i == argc) {
  197. return show_settings(0);
  198. }
  199. struct termios t;
  200. tcgetattr(STDERR_FILENO, &t);
  201. while (i < argc) {
  202. if (!strcmp(argv[i], "sane")) {
  203. t.c_iflag = ICRNL | BRKINT;
  204. t.c_oflag = ONLCR | OPOST;
  205. t.c_lflag = ECHO | ECHOE | ECHOK | ICANON | ISIG | IEXTEN;
  206. t.c_cflag = CREAD | CS8;
  207. t.c_cc[VEOF] = 4; /* ^D */
  208. t.c_cc[VEOL] = 0; /* Not set */
  209. t.c_cc[VERASE] = 0x7F; /* ^? */
  210. t.c_cc[VINTR] = 3; /* ^C */
  211. t.c_cc[VKILL] = 21; /* ^U */
  212. t.c_cc[VMIN] = 1;
  213. t.c_cc[VQUIT] = 28; /* ^\ */
  214. t.c_cc[VSTART] = 17; /* ^Q */
  215. t.c_cc[VSTOP] = 19; /* ^S */
  216. t.c_cc[VSUSP] = 26; /* ^Z */
  217. t.c_cc[VTIME] = 0;
  218. t.c_cc[VLNEXT] = 22; /* ^V */
  219. t.c_cc[VWERASE] = 23; /* ^W */
  220. }
  221. set_char("eof", VEOF);
  222. set_char("eol", VEOL);
  223. set_char("erase", VERASE);
  224. set_char("intr", VINTR);
  225. set_char("kill", VKILL);
  226. set_char("quit", VQUIT);
  227. set_char("start", VSTART);
  228. set_char("stop", VSTOP);
  229. set_char("susp", VSUSP);
  230. set_char("lnext", VLNEXT);
  231. set_char("vwerase",VWERASE);
  232. set_cflag("parenb", PARENB);
  233. set_cflag("parodd", PARODD);
  234. set_cflag("hupcl", HUPCL);
  235. set_cflag("hup", HUPCL); /* alias */
  236. set_cflag("cstopb", CSTOPB);
  237. set_cflag("cread", CREAD);
  238. set_cflag("clocal", CLOCAL);
  239. set_ctoggle("cs5", CSIZE, CS5);
  240. set_ctoggle("cs6", CSIZE, CS6);
  241. set_ctoggle("cs7", CSIZE, CS7);
  242. set_ctoggle("cs8", CSIZE, CS8);
  243. set_iflag("ignbrk", IGNBRK);
  244. set_iflag("brkint", BRKINT);
  245. set_iflag("ignpar", IGNPAR);
  246. set_iflag("parmrk", PARMRK);
  247. set_iflag("inpck", INPCK);
  248. set_iflag("istrip", ISTRIP);
  249. set_iflag("inlcr", INLCR);
  250. set_iflag("igncr", IGNCR);
  251. set_iflag("icrnl", ICRNL);
  252. set_iflag("ixon", IXON);
  253. set_iflag("ixany", IXANY);
  254. set_iflag("ixoff", IXOFF);
  255. set_oflag("olcuc", OLCUC);
  256. set_oflag("opost", OPOST);
  257. set_oflag("onlcr", ONLCR);
  258. set_oflag("ocrnl", OCRNL);
  259. set_oflag("onocr", ONOCR);
  260. set_oflag("onlret", ONLRET);
  261. set_oflag("ofill", OFILL);
  262. set_oflag("ofdel", OFDEL);
  263. set_otoggle("cr0", CRDLY, CR0);
  264. set_otoggle("cr1", CRDLY, CR1);
  265. set_otoggle("cr2", CRDLY, CR2);
  266. set_otoggle("cr3", CRDLY, CR3);
  267. set_otoggle("nl0", NLDLY, NL0);
  268. set_otoggle("nl1", NLDLY, NL1);
  269. set_otoggle("tab0", TABDLY, TAB0);
  270. set_otoggle("tab1", TABDLY, TAB1);
  271. set_otoggle("tab2", TABDLY, TAB2);
  272. set_otoggle("tab3", TABDLY, TAB3);
  273. set_otoggle("bs0", BSDLY, BS0);
  274. set_otoggle("bs1", BSDLY, BS1);
  275. set_otoggle("ff0", FFDLY, FF0);
  276. set_otoggle("ff1", FFDLY, FF1);
  277. set_otoggle("vt0", VTDLY, VT0);
  278. set_otoggle("vt1", VTDLY, VT1);
  279. set_lflag("isig", ISIG);
  280. set_lflag("icanon", ICANON);
  281. set_lflag("iexten", IEXTEN);
  282. set_lflag("echo", ECHO);
  283. set_lflag("echoe", ECHOE);
  284. set_lflag("echok", ECHOK);
  285. set_lflag("echonl", ECHONL);
  286. set_lflag("noflsh", NOFLSH);
  287. set_lflag("tostop", TOSTOP);
  288. i++;
  289. }
  290. tcsetattr(STDERR_FILENO, TCSAFLUSH, &t);
  291. return 0;
  292. }