Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:30:03 dillon Exp $
38  */
39
40 /*****************************************************************************/
41
42 #include <err.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <sys/ioctl.h>
47
48 #include <machine/cdk.h>
49
50 /*****************************************************************************/
51
52 char    *version = "2.0.0";
53
54 /*
55  *      Define some marker flags (to append to pflag values).
56  */
57 #define PFLAG_ON        0x40000000
58 #define PFLAG_OFF       0x80000000
59
60 /*
61  *      List of all options used. Use the option structure even though we
62  *      don't use getopt!
63  */
64 struct stloption {
65         char    *name;
66         int     val;
67 };
68
69
70 /*
71  *      List of all options used. Use the option structure even though we
72  *      don't use getopt!
73  */
74 struct stloption longops[] = {
75         { "-V", 'V' },
76         { "--version", 'V' },
77         { "-?", 'h' },
78         { "-h", 'h' },
79         { "--help", 'h' },
80         { "maprts", (PFLAG_ON | P_MAPRTS) },
81         { "-maprts", (PFLAG_OFF | P_MAPRTS) },
82         { "mapcts", (PFLAG_ON | P_MAPCTS) },
83         { "-mapcts", (PFLAG_OFF | P_MAPCTS) },
84         { "rtslock", (PFLAG_ON | P_RTSLOCK) },
85         { "-rtslock", (PFLAG_OFF | P_RTSLOCK) },
86         { "ctslock", (PFLAG_ON | P_CTSLOCK) },
87         { "-ctslock", (PFLAG_OFF | P_CTSLOCK) },
88         { "loopback", (PFLAG_ON | P_LOOPBACK) },
89         { "-loopback", (PFLAG_OFF | P_LOOPBACK) },
90         { "fakedcd", (PFLAG_ON | P_FAKEDCD) },
91         { "-fakedcd", (PFLAG_OFF | P_FAKEDCD) },
92         { "dtrfollow", (PFLAG_ON | P_DTRFOLLOW) },
93         { "-dtrfollow", (PFLAG_OFF | P_DTRFOLLOW) },
94         { "rximin", (PFLAG_ON | P_RXIMIN) },
95         { "-rximin", (PFLAG_OFF | P_RXIMIN) },
96         { "rxitime", (PFLAG_ON | P_RXITIME) },
97         { "-rxitime", (PFLAG_OFF | P_RXITIME) },
98         { "rxthold", (PFLAG_ON | P_RXTHOLD) },
99         { "-rxthold", (PFLAG_OFF | P_RXTHOLD) },
100         { 0, 0 }
101 };
102
103 /*****************************************************************************/
104
105 /*
106  *      Declare internal function prototypes here.
107  */
108 static void     usage(void);
109
110 /*****************************************************************************/
111
112 static void usage()
113 {
114         fprintf(stderr, "Usage: stlstty [OPTION] [ARGS]\n\n");
115         fprintf(stderr, "  -h, --help            print this information\n");
116         fprintf(stderr, "  -V, --version         show version information "
117                 "and exit\n");
118         fprintf(stderr, "  maprts, -maprts       set RTS mapping to DTR "
119                 "on or off\n");
120         fprintf(stderr, "  mapcts, -mapcts       set CTS mapping to DCD "
121                 "on or off\n");
122         fprintf(stderr, "  rtslock, -rtslock     set RTS hardware flow "
123                 "on or off\n");
124         fprintf(stderr, "  ctslock, -ctslock     set CTS hardware flow "
125                 "on or off\n");
126         fprintf(stderr, "  fakedcd, -fakedcd     set fake DCD on or off\n");
127         fprintf(stderr, "  dtrfollow, -dtrfollow set DTR data follow "
128                 "on or off\n");
129         fprintf(stderr, "  loopback, -loopback   set port internal loop back "
130                 "on or off\n");
131         fprintf(stderr, "  rximin, -rximin       set RX buffer minimum "
132                 "count on or off\n");
133         fprintf(stderr, "  rxitime, -rxitime     set RX buffer minimum "
134                 "time on or off\n");
135         fprintf(stderr, "  rxthold, -rxthold     set RX FIFO minimum "
136                 "count on or off\n");
137         exit(0);
138 }
139
140 /*****************************************************************************/
141
142 void getpflags()
143 {
144         unsigned long   pflags;
145
146         if (ioctl(0, STL_GETPFLAG, &pflags) < 0)
147                 errx(1, "stdin not a Stallion serial port\n");
148
149         if (pflags & P_MAPRTS)
150                 printf("maprts ");
151         else
152                 printf("-maprts ");
153         if (pflags & P_MAPCTS)
154                 printf("mapcts ");
155         else
156                 printf("-mapcts ");
157
158         if (pflags & P_RTSLOCK)
159                 printf("rtslock ");
160         else
161                 printf("-rtslock ");
162         if (pflags & P_CTSLOCK)
163                 printf("ctslock ");
164         else
165                 printf("-ctslock ");
166
167         if (pflags & P_FAKEDCD)
168                 printf("fakedcd ");
169         else
170                 printf("-fakedcd ");
171         if (pflags & P_DTRFOLLOW)
172                 printf("dtrfollow ");
173         else
174                 printf("-dtrfollow ");
175         if (pflags & P_LOOPBACK)
176                 printf("loopback ");
177         else
178                 printf("-loopback ");
179         printf("\n");
180
181         if (pflags & P_RXIMIN)
182                 printf("rximin ");
183         else
184                 printf("-rximin ");
185         if (pflags & P_RXITIME)
186                 printf("rxitime ");
187         else
188                 printf("-rxitime ");
189         if (pflags & P_RXTHOLD)
190                 printf("rxthold ");
191         else
192                 printf("-rxthold ");
193         printf("\n");
194 }
195
196 /*****************************************************************************/
197
198 void setpflags(unsigned long pflagin, unsigned long pflagout)
199 {
200         unsigned long   pflags;
201
202         if (ioctl(0, STL_GETPFLAG, &pflags) < 0)
203                 errx(1, "stdin not a Stallion serial port\n");
204         
205
206         pflags &= ~(pflagout & ~PFLAG_OFF);
207         pflags |= (pflagin & ~PFLAG_ON);
208
209         if (ioctl(0, STL_SETPFLAG, &pflags) < 0)
210                 err(1, "ioctl(SET_SETPFLAGS) failed");
211 }
212
213 /*****************************************************************************/
214
215 int main(int argc, char *argv[])
216 {
217         unsigned long   pflagin, pflagout;
218         int             optind, optfound;
219         int             i, val;
220
221         pflagin = 0;
222         pflagout = 0;
223
224         for (optind = 1; (optind < argc); optind++) {
225                 optfound = 0;
226                 for (i = 0; (longops[i].name[0] != 0) ; i++) {
227                         if (strcmp(argv[optind], &(longops[i].name[0])) == 0) {
228                                 val = longops[i].val;
229                                 optfound++;
230                                 break;
231                         }
232                 }
233                 if (optfound == 0)
234                         errx(1, "invalid option '%s'\n", argv[optind]);
235
236                 switch (val) {
237                 case 'V':
238                         printf("stlstats version %s\n", version);
239                         exit(0);
240                         break;
241                 case 'h':
242                         usage();
243                         break;
244                 default:
245                         if (val & PFLAG_ON) {
246                                 pflagin |= val;
247                         } else if (val & PFLAG_OFF) {
248                                 pflagout |= val;
249                         } else {
250                                 errx(1, "unknown option found, val=%x!\n", val);
251                         }
252                 }
253         }
254
255         if (pflagin | pflagout)
256                 setpflags(pflagin, pflagout);
257         else
258                 getpflags();
259
260         exit(0);
261 }
262
263 /*****************************************************************************/