pnpinfo(8): Oops, add missing semicolons.
[dragonfly.git] / usr.bin / sasc / sasc.c
blob2bf3e42c213b004e6b7372d7dcabd400b7b7e1f1
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.
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.
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.
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 $
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
41 #include <machine/asc_ioctl.h>
42 #include <machine/limits.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
50 #define DEFAULT_FILE "/dev/asc0"
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);
57 static void
58 usage(void)
60 fprintf(stderr,
61 "usage: sasc [-sq] [-f file] [-r dpi] [-w width] [-h height]"
62 "[-b len] [-t time]\n");
63 exit(1);
66 /* Check given numerical arguments */
67 static int
68 getnum(const char *str)
70 long val;
71 char *ep;
73 errno = 0;
74 val = strtol(str, &ep, 10);
75 if (errno)
76 err(1, "strtol failed: %s", str);
78 if (str == ep || *ep != '\0')
79 errx(1, "invalid value: %s", str);
81 if (val > INT_MAX || val < INT_MIN) {
82 errno = ERANGE;
83 errc(1, errno, "getnum failed:");
86 return((int)val);
89 static void
90 asc_set(int fd, u_long asc_setting, int asc_value, const char *asc_type)
92 if (ioctl(fd, asc_setting, &asc_value) < 0)
93 err(1, "ioctl failed setting %s(%d)", asc_type, asc_value);
95 printf("Successfully set\n");
98 static int
99 asc_get(int fd, u_long asc_setting)
101 int asc_value;
103 if (ioctl(fd, asc_setting, &asc_value) < 0)
104 err(1, "ioctl failed");
106 return(asc_value);
110 main(int argc, char **argv)
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;
119 show_dpi = show_width = show_height = 0;
120 show_blen = show_btime = show_all = 0;
122 set_blen = set_dpi = set_width = 0;
123 set_height = set_btime = set_switch = 0;
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();
156 if (argc == 1)
157 show_all = 1;
159 if ((fd = open(file, O_RDWR)) == -1)
160 err(1, "Unable to open: %s", file);
162 if (set_switch) {
163 if (ioctl(fd, ASC_SRESSW) < 0)
164 err(1, "ioctl: ASC_SRESSW failed");
167 if (set_dpi)
168 asc_set(fd, ASC_SRES, set_dpi, "ASC_SRES");
170 if (set_width)
171 asc_set(fd, ASC_SWIDTH, set_width, "ASC_SWIDTH");
173 if (set_height)
174 asc_set(fd, ASC_SHEIGHT, set_height, "ASC_SHEIGHT");
176 if (set_blen)
177 asc_set(fd, ASC_SBLEN, set_blen, "ASC_SBLEN");
179 if (set_btime)
180 asc_set(fd, ASC_SBTIME, set_btime, "ASC_SBTIME");
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);
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);
196 return 0;