/bin/stty/modes.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 246 lines · 202 code · 12 blank · 32 comment · 10 complexity · a2ea60959b74814bbbaaac990c8635d4 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1991, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. #if 0
  31. static char sccsid[] = "@(#)modes.c 8.3 (Berkeley) 4/2/94";
  32. #endif
  33. #endif /* not lint */
  34. #include <sys/cdefs.h>
  35. __FBSDID("$FreeBSD$");
  36. #include <sys/types.h>
  37. #include <stddef.h>
  38. #include <string.h>
  39. #include "stty.h"
  40. int msearch(char ***, struct info *);
  41. struct modes {
  42. const char *name;
  43. long set;
  44. long unset;
  45. };
  46. /*
  47. * The code in optlist() depends on minus options following regular
  48. * options, i.e. "foo" must immediately precede "-foo".
  49. */
  50. static const struct modes cmodes[] = {
  51. { "cs5", CS5, CSIZE },
  52. { "cs6", CS6, CSIZE },
  53. { "cs7", CS7, CSIZE },
  54. { "cs8", CS8, CSIZE },
  55. { "cstopb", CSTOPB, 0 },
  56. { "-cstopb", 0, CSTOPB },
  57. { "cread", CREAD, 0 },
  58. { "-cread", 0, CREAD },
  59. { "parenb", PARENB, 0 },
  60. { "-parenb", 0, PARENB },
  61. { "parodd", PARODD, 0 },
  62. { "-parodd", 0, PARODD },
  63. { "parity", PARENB | CS7, PARODD | CSIZE },
  64. { "-parity", CS8, PARODD | PARENB | CSIZE },
  65. { "evenp", PARENB | CS7, PARODD | CSIZE },
  66. { "-evenp", CS8, PARODD | PARENB | CSIZE },
  67. { "oddp", PARENB | CS7 | PARODD, CSIZE },
  68. { "-oddp", CS8, PARODD | PARENB | CSIZE },
  69. { "pass8", CS8, PARODD | PARENB | CSIZE },
  70. { "-pass8", PARENB | CS7, PARODD | CSIZE },
  71. { "hupcl", HUPCL, 0 },
  72. { "-hupcl", 0, HUPCL },
  73. { "hup", HUPCL, 0 },
  74. { "-hup", 0, HUPCL },
  75. { "clocal", CLOCAL, 0 },
  76. { "-clocal", 0, CLOCAL },
  77. { "crtscts", CRTSCTS, 0 },
  78. { "-crtscts", 0, CRTSCTS },
  79. { "ctsflow", CCTS_OFLOW, 0 },
  80. { "-ctsflow", 0, CCTS_OFLOW },
  81. { "dsrflow", CDSR_OFLOW, 0 },
  82. { "-dsrflow", 0, CDSR_OFLOW },
  83. { "dtrflow", CDTR_IFLOW, 0 },
  84. { "-dtrflow", 0, CDTR_IFLOW },
  85. { "rtsflow", CRTS_IFLOW, 0 },
  86. { "-rtsflow", 0, CRTS_IFLOW },
  87. { "mdmbuf", MDMBUF, 0 },
  88. { "-mdmbuf", 0, MDMBUF },
  89. { NULL, 0, 0 },
  90. };
  91. static const struct modes imodes[] = {
  92. { "ignbrk", IGNBRK, 0 },
  93. { "-ignbrk", 0, IGNBRK },
  94. { "brkint", BRKINT, 0 },
  95. { "-brkint", 0, BRKINT },
  96. { "ignpar", IGNPAR, 0 },
  97. { "-ignpar", 0, IGNPAR },
  98. { "parmrk", PARMRK, 0 },
  99. { "-parmrk", 0, PARMRK },
  100. { "inpck", INPCK, 0 },
  101. { "-inpck", 0, INPCK },
  102. { "istrip", ISTRIP, 0 },
  103. { "-istrip", 0, ISTRIP },
  104. { "inlcr", INLCR, 0 },
  105. { "-inlcr", 0, INLCR },
  106. { "igncr", IGNCR, 0 },
  107. { "-igncr", 0, IGNCR },
  108. { "icrnl", ICRNL, 0 },
  109. { "-icrnl", 0, ICRNL },
  110. { "ixon", IXON, 0 },
  111. { "-ixon", 0, IXON },
  112. { "flow", IXON, 0 },
  113. { "-flow", 0, IXON },
  114. { "ixoff", IXOFF, 0 },
  115. { "-ixoff", 0, IXOFF },
  116. { "tandem", IXOFF, 0 },
  117. { "-tandem", 0, IXOFF },
  118. { "ixany", IXANY, 0 },
  119. { "-ixany", 0, IXANY },
  120. { "decctlq", 0, IXANY },
  121. { "-decctlq", IXANY, 0 },
  122. { "imaxbel", IMAXBEL, 0 },
  123. { "-imaxbel", 0, IMAXBEL },
  124. { NULL, 0, 0 },
  125. };
  126. static const struct modes lmodes[] = {
  127. { "echo", ECHO, 0 },
  128. { "-echo", 0, ECHO },
  129. { "echoe", ECHOE, 0 },
  130. { "-echoe", 0, ECHOE },
  131. { "crterase", ECHOE, 0 },
  132. { "-crterase", 0, ECHOE },
  133. { "crtbs", ECHOE, 0 }, /* crtbs not supported, close enough */
  134. { "-crtbs", 0, ECHOE },
  135. { "echok", ECHOK, 0 },
  136. { "-echok", 0, ECHOK },
  137. { "echoke", ECHOKE, 0 },
  138. { "-echoke", 0, ECHOKE },
  139. { "crtkill", ECHOKE, 0 },
  140. { "-crtkill", 0, ECHOKE },
  141. { "altwerase", ALTWERASE, 0 },
  142. { "-altwerase", 0, ALTWERASE },
  143. { "iexten", IEXTEN, 0 },
  144. { "-iexten", 0, IEXTEN },
  145. { "echonl", ECHONL, 0 },
  146. { "-echonl", 0, ECHONL },
  147. { "echoctl", ECHOCTL, 0 },
  148. { "-echoctl", 0, ECHOCTL },
  149. { "ctlecho", ECHOCTL, 0 },
  150. { "-ctlecho", 0, ECHOCTL },
  151. { "echoprt", ECHOPRT, 0 },
  152. { "-echoprt", 0, ECHOPRT },
  153. { "prterase", ECHOPRT, 0 },
  154. { "-prterase", 0, ECHOPRT },
  155. { "isig", ISIG, 0 },
  156. { "-isig", 0, ISIG },
  157. { "icanon", ICANON, 0 },
  158. { "-icanon", 0, ICANON },
  159. { "noflsh", NOFLSH, 0 },
  160. { "-noflsh", 0, NOFLSH },
  161. { "tostop", TOSTOP, 0 },
  162. { "-tostop", 0, TOSTOP },
  163. { "flusho", FLUSHO, 0 },
  164. { "-flusho", 0, FLUSHO },
  165. { "pendin", PENDIN, 0 },
  166. { "-pendin", 0, PENDIN },
  167. { "crt", ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
  168. { "-crt", ECHOK, ECHOE|ECHOKE|ECHOCTL },
  169. { "newcrt", ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT },
  170. { "-newcrt", ECHOK, ECHOE|ECHOKE|ECHOCTL },
  171. { "nokerninfo", NOKERNINFO, 0 },
  172. { "-nokerninfo",0, NOKERNINFO },
  173. { "kerninfo", 0, NOKERNINFO },
  174. { "-kerninfo", NOKERNINFO, 0 },
  175. { NULL, 0, 0 },
  176. };
  177. static const struct modes omodes[] = {
  178. { "opost", OPOST, 0 },
  179. { "-opost", 0, OPOST },
  180. { "litout", 0, OPOST },
  181. { "-litout", OPOST, 0 },
  182. { "onlcr", ONLCR, 0 },
  183. { "-onlcr", 0, ONLCR },
  184. { "ocrnl", OCRNL, 0 },
  185. { "-ocrnl", 0, OCRNL },
  186. { "tabs", TAB0, TABDLY }, /* "preserve" tabs */
  187. { "-tabs", TAB3, TABDLY },
  188. { "oxtabs", TAB3, TABDLY },
  189. { "-oxtabs", TAB0, TABDLY },
  190. { "tab0", TAB0, TABDLY },
  191. { "tab3", TAB3, TABDLY },
  192. { "onocr", ONOCR, 0 },
  193. { "-onocr", 0, ONOCR },
  194. { "onlret", ONLRET, 0 },
  195. { "-onlret", 0, ONLRET },
  196. { NULL, 0, 0 },
  197. };
  198. #define CHK(s) (*name == s[0] && !strcmp(name, s))
  199. int
  200. msearch(char ***argvp, struct info *ip)
  201. {
  202. const struct modes *mp;
  203. char *name;
  204. name = **argvp;
  205. for (mp = cmodes; mp->name; ++mp)
  206. if (CHK(mp->name)) {
  207. ip->t.c_cflag &= ~mp->unset;
  208. ip->t.c_cflag |= mp->set;
  209. ip->set = 1;
  210. return (1);
  211. }
  212. for (mp = imodes; mp->name; ++mp)
  213. if (CHK(mp->name)) {
  214. ip->t.c_iflag &= ~mp->unset;
  215. ip->t.c_iflag |= mp->set;
  216. ip->set = 1;
  217. return (1);
  218. }
  219. for (mp = lmodes; mp->name; ++mp)
  220. if (CHK(mp->name)) {
  221. ip->t.c_lflag &= ~mp->unset;
  222. ip->t.c_lflag |= mp->set;
  223. ip->set = 1;
  224. return (1);
  225. }
  226. for (mp = omodes; mp->name; ++mp)
  227. if (CHK(mp->name)) {
  228. ip->t.c_oflag &= ~mp->unset;
  229. ip->t.c_oflag |= mp->set;
  230. ip->set = 1;
  231. return (1);
  232. }
  233. return (0);
  234. }