1 | /*- |
---|
2 | * Copyright (c) 1991 The Regents of the University of California. |
---|
3 | * 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 | * 3. All advertising materials mentioning features or use of this software |
---|
14 | * must display the following acknowledgement: |
---|
15 | * This product includes software developed by the University of |
---|
16 | * California, Berkeley and its contributors. |
---|
17 | * 4. Neither the name of the University nor the names of its contributors |
---|
18 | * may be used to endorse or promote products derived from this software |
---|
19 | * without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
31 | * SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /* This is slightly hacked version of bin/stty/modes.c. |
---|
35 | * James Clark (jjc@jclark.com) as part of lprps package. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef lint |
---|
39 | static char sccsid[] = "@(#)modes.c 5.4 (Berkeley) 6/10/91"; |
---|
40 | #endif /* not lint */ |
---|
41 | |
---|
42 | /* The Berkeley spooler uses some flags which are not supported by POSIX |
---|
43 | * but which may be present in SYS5 or BSD |
---|
44 | */ |
---|
45 | #if defined(_POSIX_SOURCE) |
---|
46 | #undef _POSIX_SOURCE |
---|
47 | #endif |
---|
48 | |
---|
49 | #include <termios.h> |
---|
50 | #include <string.h> |
---|
51 | |
---|
52 | struct modes { |
---|
53 | char *name; |
---|
54 | long set; |
---|
55 | long unset; |
---|
56 | }; |
---|
57 | |
---|
58 | static struct modes cmodes[] = { |
---|
59 | "cs5", CS5, CSIZE, |
---|
60 | "cs6", CS6, CSIZE, |
---|
61 | "cs7", CS7, CSIZE, |
---|
62 | "cs8", CS8, CSIZE, |
---|
63 | "cstopb", CSTOPB, 0, |
---|
64 | "-cstopb", 0, CSTOPB, |
---|
65 | "cread", CREAD, 0, |
---|
66 | "-cread", 0, CREAD, |
---|
67 | "parenb", PARENB, 0, |
---|
68 | "-parenb", 0, PARENB, |
---|
69 | "parodd", PARODD, 0, |
---|
70 | "-parodd", 0, PARODD, |
---|
71 | "parity", PARENB | CS7, PARODD | CSIZE, |
---|
72 | "-parity", CS8, PARODD | PARENB | CSIZE, |
---|
73 | "evenp", PARENB | CS7, PARODD | CSIZE, |
---|
74 | "-evenp", CS8, PARODD | PARENB | CSIZE, |
---|
75 | "oddp", PARENB | CS7 | PARODD, CSIZE, |
---|
76 | "-oddp", CS8, PARODD | PARENB | CSIZE, |
---|
77 | "pass8", CS8, PARODD | PARENB | CSIZE, |
---|
78 | "hupcl", HUPCL, 0, |
---|
79 | "-hupcl", 0, HUPCL, |
---|
80 | "hup", HUPCL, 0, |
---|
81 | "-hup", 0, HUPCL, |
---|
82 | "clocal", CLOCAL, 0, |
---|
83 | "-clocal", 0, CLOCAL, |
---|
84 | NULL |
---|
85 | }; |
---|
86 | |
---|
87 | static struct modes imodes[] = { |
---|
88 | "ignbrk", IGNBRK, 0, |
---|
89 | "-ignbrk", 0, IGNBRK, |
---|
90 | "brkint", BRKINT, 0, |
---|
91 | "-brkint", 0, BRKINT, |
---|
92 | "ignpar", IGNPAR, 0, |
---|
93 | "-ignpar", 0, IGNPAR, |
---|
94 | "parmrk", PARMRK, 0, |
---|
95 | "-parmrk", 0, PARMRK, |
---|
96 | "inpck", INPCK, 0, |
---|
97 | "-inpck", 0, INPCK, |
---|
98 | "istrip", ISTRIP, 0, |
---|
99 | "-istrip", 0, ISTRIP, |
---|
100 | "inlcr", INLCR, 0, |
---|
101 | "-inlcr", 0, INLCR, |
---|
102 | "igncr", IGNCR, 0, |
---|
103 | "-igncr", 0, IGNCR, |
---|
104 | "icrnl", ICRNL, 0, |
---|
105 | "-icrnl", 0, ICRNL, |
---|
106 | "ixon", IXON, 0, |
---|
107 | "-ixon", 0, IXON, |
---|
108 | "flow", IXON, 0, |
---|
109 | "-flow", 0, IXON, |
---|
110 | "ixoff", IXOFF, 0, |
---|
111 | "-ixoff", 0, IXOFF, |
---|
112 | "tandem", IXOFF, 0, |
---|
113 | "-tandem", 0, IXOFF, |
---|
114 | "ixany", IXANY, 0, |
---|
115 | "-ixany", 0, IXANY, |
---|
116 | "decctlq", 0, IXANY, |
---|
117 | "-decctlq", IXANY, 0, |
---|
118 | #ifdef IMAXBEL |
---|
119 | "imaxbel", IMAXBEL, 0, |
---|
120 | "-imaxbel", 0, IMAXBEL, |
---|
121 | #endif |
---|
122 | NULL |
---|
123 | }; |
---|
124 | |
---|
125 | static struct modes lmodes[] = { |
---|
126 | "echo", ECHO, 0, |
---|
127 | "-echo", 0, ECHO, |
---|
128 | "echoe", ECHOE, 0, |
---|
129 | "-echoe", 0, ECHOE, |
---|
130 | "crterase", ECHOE, 0, |
---|
131 | "-crterase", 0, ECHOE, |
---|
132 | "crtbs", ECHOE, 0, /* crtbs not supported, close enough */ |
---|
133 | "-crtbs", 0, ECHOE, |
---|
134 | "echok", ECHOK, 0, |
---|
135 | "-echok", 0, ECHOK, |
---|
136 | #ifdef ECHOKE |
---|
137 | "echoke", ECHOKE, 0, |
---|
138 | "-echoke", 0, ECHOKE, |
---|
139 | "crtkill", ECHOKE, 0, |
---|
140 | "-crtkill", 0, ECHOKE, |
---|
141 | #endif |
---|
142 | "iexten", IEXTEN, 0, |
---|
143 | "-iexten", 0, IEXTEN, |
---|
144 | "echonl", ECHONL, 0, |
---|
145 | "-echonl", 0, ECHONL, |
---|
146 | #ifdef ECHOCTL |
---|
147 | "echoctl", ECHOCTL, 0, |
---|
148 | "-echoctl", 0, ECHOCTL, |
---|
149 | "ctlecho", ECHOCTL, 0, |
---|
150 | "-ctlecho", 0, ECHOCTL, |
---|
151 | #endif |
---|
152 | #ifdef ECHOPRT |
---|
153 | "echoprt", ECHOPRT, 0, |
---|
154 | "-echoprt", 0, ECHOPRT, |
---|
155 | "prterase", ECHOPRT, 0, |
---|
156 | "-prterase", 0, ECHOPRT, |
---|
157 | #endif |
---|
158 | "isig", ISIG, 0, |
---|
159 | "-isig", 0, ISIG, |
---|
160 | "icanon", ICANON, 0, |
---|
161 | "-icanon", 0, ICANON, |
---|
162 | "noflsh", NOFLSH, 0, |
---|
163 | "-noflsh", 0, NOFLSH, |
---|
164 | "tostop", TOSTOP, 0, |
---|
165 | "-tostop", 0, TOSTOP, |
---|
166 | #ifndef POSIX |
---|
167 | "mdmbuf", MDMBUF, 0, |
---|
168 | "-mdmbuf", 0, MDMBUF, |
---|
169 | #endif |
---|
170 | "flusho", FLUSHO, 0, |
---|
171 | "-flusho", 0, FLUSHO, |
---|
172 | "pendin", PENDIN, 0, |
---|
173 | "-pendin", 0, PENDIN, |
---|
174 | #if defined(ECHOKE) && defined(ECHOCTL) && defined(ECHOPRT) |
---|
175 | "crt", ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT, |
---|
176 | "-crt", ECHOK, ECHOE|ECHOKE|ECHOCTL, |
---|
177 | "newcrt", ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT, |
---|
178 | "-newcrt", ECHOK, ECHOE|ECHOKE|ECHOCTL, |
---|
179 | #endif |
---|
180 | NULL |
---|
181 | }; |
---|
182 | |
---|
183 | static struct modes omodes[] = { |
---|
184 | "opost", OPOST, 0, |
---|
185 | "-opost", 0, OPOST, |
---|
186 | "litout", 0, OPOST, |
---|
187 | "-litout", OPOST, 0, |
---|
188 | "onlcr", ONLCR, 0, |
---|
189 | "-onlcr", 0, ONLCR, |
---|
190 | NULL |
---|
191 | }; |
---|
192 | |
---|
193 | #define CHK(s) (*name == s[0] && !strcmp(name, s)) |
---|
194 | |
---|
195 | /* Return 0 for OK, -1 on error (analogous to cfset[oi]speed.) */ |
---|
196 | |
---|
197 | setmode(tp, name) |
---|
198 | struct termios *tp; |
---|
199 | char *name; |
---|
200 | { |
---|
201 | register struct modes *mp; |
---|
202 | |
---|
203 | for (mp = cmodes; mp->name; ++mp) |
---|
204 | if (CHK(mp->name)) { |
---|
205 | tp->c_cflag &= ~mp->unset; |
---|
206 | tp->c_cflag |= mp->set; |
---|
207 | return(0); |
---|
208 | } |
---|
209 | for (mp = imodes; mp->name; ++mp) |
---|
210 | if (CHK(mp->name)) { |
---|
211 | tp->c_iflag &= ~mp->unset; |
---|
212 | tp->c_iflag |= mp->set; |
---|
213 | return(0); |
---|
214 | } |
---|
215 | for (mp = lmodes; mp->name; ++mp) |
---|
216 | if (CHK(mp->name)) { |
---|
217 | tp->c_lflag &= ~mp->unset; |
---|
218 | tp->c_lflag |= mp->set; |
---|
219 | return(0); |
---|
220 | } |
---|
221 | for (mp = omodes; mp->name; ++mp) |
---|
222 | if (CHK(mp->name)) { |
---|
223 | tp->c_oflag &= ~mp->unset; |
---|
224 | tp->c_oflag |= mp->set; |
---|
225 | return(0); |
---|
226 | } |
---|
227 | return(-1); |
---|
228 | } |
---|