Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.sbin / stallion / stlstty / stlstty.c
1 /*****************************************************************************/
2
3 /*
4  * stlstty.c  -- stallion intelligent multiport special options.
5  *
6  * Copyright (c) 1996-1998 Greg Ungerer (gerg@stallion.oz.au).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Greg Ungerer.
20  * 4. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/usr.sbin/stallion/stlstty/stlstty.c,v 1.1.2.1 2001/08/30 12:29:56 murray Exp $
37  * $DragonFly: src/usr.sbin/stallion/stlstty/stlstty.c,v 1.4 2007/11/25 01:28:24 swildner Exp $
38  */
39
40 /*****************************************************************************/
41
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <sys/ioctl.h>
48
49 #include <machine/cdk.h>
50
51 /*****************************************************************************/
52
53 char    *version = "2.0.0";
54
55 /*
56  *      Define some marker flags (to append to pflag values).
57  */
58 #define PFLAG_ON        0x40000000
59 #define PFLAG_OFF       0x80000000
60
61 /*
62  *      List of all options used. Use the option structure even though we
63  *      don't use getopt!
64  */
65 struct stloption {
66         char    *name;
67         int     val;
68 };
69
70
71 /*
72  *      List of all options used. Use the option structure even though we
73  *      don't use getopt!
74  */
75 struct stloption longops[] = {
76         { "-V", 'V' },
77         { "--version", 'V' },
78         { "-?", 'h' },
79         { "-h", 'h' },
80         { "--help", 'h' },
81         { "maprts", (PFLAG_ON | P_MAPRTS) },
82         { "-maprts", (PFLAG_OFF | P_MAPRTS) },
83         { "mapcts", (PFLAG_ON | P_MAPCTS) },
84         { "-mapcts", (PFLAG_OFF | P_MAPCTS) },
85         { "rtslock", (PFLAG_ON | P_RTSLOCK) },
86         { "-rtslock", (PFLAG_OFF | P_RTSLOCK) },
87         { "ctslock", (PFLAG_ON | P_CTSLOCK) },
88         { "-ctslock", (PFLAG_OFF | P_CTSLOCK) },
89         { "loopback", (PFLAG_ON | P_LOOPBACK) },
90         { "-loopback", (PFLAG_OFF | P_LOOPBACK) },
91         { "fakedcd", (PFLAG_ON | P_FAKEDCD) },
92         { "-fakedcd", (PFLAG_OFF | P_FAKEDCD) },
93         { "dtrfollow", (PFLAG_ON | P_DTRFOLLOW) },
94         { "-dtrfollow", (PFLAG_OFF | P_DTRFOLLOW) },
95         { "rximin", (PFLAG_ON | P_RXIMIN) },
96         { "-rximin", (PFLAG_OFF | P_RXIMIN) },
97         { "rxitime", (PFLAG_ON | P_RXITIME) },
98         { "-rxitime", (PFLAG_OFF | P_RXITIME) },
99         { "rxthold", (PFLAG_ON | P_RXTHOLD) },
100         { "-rxthold", (PFLAG_OFF | P_RXTHOLD) },
101         { 0, 0 }
102 };
103
104 /*****************************************************************************/
105
106 /*
107  *      Declare internal function prototypes here.
108  */
109 static void     usage(void);
110
111 /*****************************************************************************/
112
113 static void
114 usage(void)
115 {
116         fprintf(stderr, "Usage: stlstty [OPTION] [ARGS]\n\n");
117         fprintf(stderr, "  -h, --help            print this information\n");
118         fprintf(stderr, "  -V, --version         show version information "
119                 "and exit\n");
120         fprintf(stderr, "  maprts, -maprts       set RTS mapping to DTR "
121                 "on or off\n");
122         fprintf(stderr, "  mapcts, -mapcts       set CTS mapping to DCD "
123                 "on or off\n");
124         fprintf(stderr, "  rtslock, -rtslock     set RTS hardware flow "
125                 "on or off\n");
126         fprintf(stderr, "  ctslock, -ctslock     set CTS hardware flow "
127                 "on or off\n");
128         fprintf(stderr, "  fakedcd, -fakedcd     set fake DCD on or off\n");
129         fprintf(stderr, "  dtrfollow, -dtrfollow set DTR data follow "
130                 "on or off\n");
131         fprintf(stderr, "  loopback, -loopback   set port internal loop back "
132                 "on or off\n");
133         fprintf(stderr, "  rximin, -rximin       set RX buffer minimum "
134                 "count on or off\n");
135         fprintf(stderr, "  rxitime, -rxitime     set RX buffer minimum "
136                 "time on or off\n");
137         fprintf(stderr, "  rxthold, -rxthold     set RX FIFO minimum "
138                 "count on or off\n");
139         exit(0);
140 }
141
142 /*****************************************************************************/
143
144 void
145 getpflags(void)
146 {
147         unsigned long   pflags;
148
149         if (ioctl(0, STL_GETPFLAG, &pflags) < 0)
150                 errx(1, "stdin not a Stallion serial port\n");
151
152         if (pflags & P_MAPRTS)
153                 printf("maprts ");
154         else
155                 printf("-maprts ");
156         if (pflags & P_MAPCTS)
157                 printf("mapcts ");
158         else
159                 printf("-mapcts ");
160
161         if (pflags & P_RTSLOCK)
162                 printf("rtslock ");
163         else
164                 printf("-rtslock ");
165         if (pflags & P_CTSLOCK)
166                 printf("ctslock ");
167         else
168                 printf("-ctslock ");
169
170         if (pflags & P_FAKEDCD)
171                 printf("fakedcd ");
172         else
173                 printf("-fakedcd ");
174         if (pflags & P_DTRFOLLOW)
175                 printf("dtrfollow ");
176         else
177                 printf("-dtrfollow ");
178         if (pflags & P_LOOPBACK)
179                 printf("loopback ");
180         else
181                 printf("-loopback ");
182         printf("\n");
183
184         if (pflags & P_RXIMIN)
185                 printf("rximin ");
186         else
187                 printf("-rximin ");
188         if (pflags & P_RXITIME)
189                 printf("rxitime ");
190         else
191                 printf("-rxitime ");
192         if (pflags & P_RXTHOLD)
193                 printf("rxthold ");
194         else
195                 printf("-rxthold ");
196         printf("\n");
197 }
198
199 /*****************************************************************************/
200
201 void
202 setpflags(unsigned long pflagin, unsigned long pflagout)
203 {
204         unsigned long   pflags;
205
206         if (ioctl(0, STL_GETPFLAG, &pflags) < 0)
207                 errx(1, "stdin not a Stallion serial port\n");
208         
209
210         pflags &= ~(pflagout & ~PFLAG_OFF);
211         pflags |= (pflagin & ~PFLAG_ON);
212
213         if (ioctl(0, STL_SETPFLAG, &pflags) < 0)
214                 err(1, "ioctl(SET_SETPFLAGS) failed");
215 }
216
217 /*****************************************************************************/
218
219 int
220 main(int argc, char *argv[])
221 {
222         unsigned long   pflagin, pflagout;
223         int             optind, optfound;
224         int             i, val;
225
226         pflagin = 0;
227         pflagout = 0;
228
229         for (optind = 1; (optind < argc); optind++) {
230                 optfound = 0;
231                 for (i = 0; (longops[i].name[0] != 0) ; i++) {
232                         if (strcmp(argv[optind], &(longops[i].name[0])) == 0) {
233                                 val = longops[i].val;
234                                 optfound++;
235                                 break;
236                         }
237                 }
238                 if (optfound == 0)
239                         errx(1, "invalid option '%s'\n", argv[optind]);
240
241                 switch (val) {
242                 case 'V':
243                         printf("stlstats version %s\n", version);
244                         exit(0);
245                         break;
246                 case 'h':
247                         usage();
248                         break;
249                 default:
250                         if (val & PFLAG_ON) {
251                                 pflagin |= val;
252                         } else if (val & PFLAG_OFF) {
253                                 pflagout |= val;
254                         } else {
255                                 errx(1, "unknown option found, val=%x!\n", val);
256                         }
257                 }
258         }
259
260         if (pflagin | pflagout)
261                 setpflags(pflagin, pflagout);
262         else
263                 getpflags();
264
265         exit(0);
266 }
267
268 /*****************************************************************************/