Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.bin / sasc / sasc.c
1 /*
2  * sasc(1) - utility for the `asc' scanner device driver
3  * 
4  * Copyright (c) 1995 Gunther Schadow.  All rights reserved.
5  * Copyright (c) 2004 Liam J. Foy. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Gunther Schadow.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/usr.bin/sasc/sasc.c,v 1.7.2.1 2000/06/30 09:47:52 ps Exp $
33  * $DragonFly: src/usr.bin/sasc/sasc.c,v 1.6 2007/01/21 10:40:39 swildner Exp $
34  *
35  */
36
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
40
41 #include <machine/asc_ioctl.h>
42 #include <machine/limits.h>
43
44 #include <err.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49
50 #define DEFAULT_FILE "/dev/asc0"
51
52 static int      getnum(const char *);
53 static int      asc_get(int, u_long);
54 static void     asc_set(int, u_long, int, const char *);
55 static void     usage(void);
56
57 static void
58 usage(void)
59 {
60         fprintf(stderr,
61                 "usage: sasc [-sq] [-f file] [-r dpi] [-w width] [-h height]"
62                 "[-b len] [-t time]\n");
63         exit(1);
64 }
65
66 /* Check given numerical arguments */
67 static int
68 getnum(const char *str)
69 {
70         long val;
71         char *ep;
72
73         errno = 0;
74         val = strtol(str, &ep, 10);
75         if (errno)
76                 err(1, "strtol failed: %s", str);
77
78         if (str == ep || *ep != '\0')
79                 errx(1, "invalid value: %s", str);
80
81         if (val > INT_MAX || val < INT_MIN) {
82                 errno = ERANGE;
83                 errc(1, errno, "getnum failed:");
84         }
85
86         return((int)val);
87 }
88
89 static void
90 asc_set(int fd, u_long asc_setting, int asc_value, const char *asc_type)
91 {
92         if (ioctl(fd, asc_setting, &asc_value) < 0)
93                 err(1, "ioctl failed setting %s(%d)", asc_type, asc_value);
94
95         printf("Successfully set\n");
96 }
97
98 static int
99 asc_get(int fd, u_long asc_setting)
100 {
101         int asc_value;
102
103         if (ioctl(fd, asc_setting, &asc_value) < 0)
104                 err(1, "ioctl failed");
105
106         return(asc_value);
107 }
108
109 int
110 main(int argc, char **argv)
111 {
112         const char *file = DEFAULT_FILE;
113         int c, fd;
114         int show_dpi, show_width, show_height;
115         int show_blen, show_btime, show_all;
116         int set_blen, set_dpi, set_width;
117         int set_height, set_btime, set_switch;
118
119         show_dpi = show_width = show_height = 0;
120         show_blen = show_btime = show_all = 0;
121
122         set_blen = set_dpi = set_width = 0;
123         set_height = set_btime = set_switch = 0;
124
125         while ((c = getopt(argc, argv, "sqf:b:r:w:h:t:")) != -1) {
126                 switch (c) {
127                 case 'f':
128                         file = optarg;
129                         break;
130                 case 'r':
131                         set_dpi = getnum(optarg);
132                         break;
133                 case 'w':
134                         set_width = getnum(optarg);
135                         break;
136                 case 'h':
137                         set_height = getnum(optarg);
138                         break;
139                 case 'b':
140                         set_blen = getnum(optarg);
141                         break;
142                 case 't':
143                         set_btime = getnum(optarg);
144                         break;
145                 case 's':
146                         set_switch = 1;
147                         break;
148                 case 'q':
149                         show_all = 1;
150                         break;
151                 default:
152                         usage();
153                 }
154         }
155
156         if (argc == 1)
157                 show_all = 1;
158
159         if ((fd = open(file, O_RDWR)) == -1)
160                 err(1, "Unable to open: %s", file);
161
162         if (set_switch) {
163                 if (ioctl(fd, ASC_SRESSW) < 0)
164                         err(1, "ioctl: ASC_SRESSW failed");
165         }
166
167         if (set_dpi)
168                 asc_set(fd, ASC_SRES, set_dpi, "ASC_SRES");
169
170         if (set_width)
171                 asc_set(fd, ASC_SWIDTH, set_width, "ASC_SWIDTH");
172
173         if (set_height)
174                 asc_set(fd, ASC_SHEIGHT, set_height, "ASC_SHEIGHT");
175
176         if (set_blen)
177                 asc_set(fd, ASC_SBLEN, set_blen, "ASC_SBLEN");
178
179         if (set_btime)
180                 asc_set(fd, ASC_SBTIME, set_btime, "ASC_SBTIME");
181
182         if (show_all) {
183                 show_dpi = asc_get(fd, ASC_GRES);
184                 show_width = asc_get(fd, ASC_GWIDTH);
185                 show_height = asc_get(fd, ASC_GHEIGHT);
186                 show_blen = asc_get(fd, ASC_GBLEN);
187                 show_btime = asc_get(fd, ASC_GBTIME);
188
189                 printf("Device: %s\n", file);
190                 printf("Resolution: %d dpi\n", show_dpi);
191                 printf("Width: %d\n", show_width);
192                 printf("Height: %d\n", show_height);
193                 printf("Buffer length: %d\n", show_blen);
194                 printf("Buffer timeout: %d\n", show_btime);
195         }
196         return 0;
197 }