xref: /netbsd/sbin/wsconsctl/wsconsctl.c (revision bf9ec67e)
1 /*	$NetBSD: wsconsctl.c,v 1.5 2002/02/18 17:00:16 yamt Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <fcntl.h>
40 #include <err.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include "wsconsctl.h"
46 
47 #define PATH_KEYBOARD		"/dev/wskbd0"
48 #define PATH_MOUSE		"/dev/wsmouse0"
49 #define PATH_DISPLAY		"/dev/ttyE0"
50 
51 extern struct field keyboard_field_tab[];
52 extern struct field mouse_field_tab[];
53 extern struct field display_field_tab[];
54 extern int keyboard_field_tab_len;
55 extern int mouse_field_tab_len;
56 extern int display_field_tab_len;
57 
58 static void usage __P((char *));
59 int main __P((int, char **));
60 
61 static void
62 usage(msg)
63 	char *msg;
64 {
65 	const char *progname = getprogname();
66 
67 	if (msg != NULL)
68 		fprintf(stderr, "%s: %s\n\n", progname, msg);
69 
70 	fprintf(stderr, "usage: %s [-kmd] [-f file] [-n] name ...\n",
71 		progname);
72 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -w name=value ...\n",
73 		progname);
74 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -a\n", progname);
75 
76 	exit(1);
77 }
78 
79 int
80 main(argc, argv)
81 	int argc;
82 	char **argv;
83 {
84 	int i, ch, fd;
85 	int aflag, dflag, kflag, mflag, wflag;
86 	char *file, *sep, *p;
87 	struct field *f, *field_tab;
88 	int do_merge, field_tab_len;
89 	void (*getval) __P((int));
90 	void (*putval) __P((int));
91 
92 	aflag = 0;
93 	dflag = 0;
94 	kflag = 0;
95 	mflag = 0;
96 	wflag = 0;
97 	file = NULL;
98 	sep = "=";
99 
100 	while ((ch = getopt(argc, argv, "adf:kmnw")) != -1) {
101 		switch(ch) {
102 		case 'a':
103 			aflag = 1;
104 			break;
105 		case 'd':
106 			dflag = 1;
107 			break;
108 		case 'f':
109 			file = optarg;
110 			break;
111 		case 'k':
112 			kflag = 1;
113 			break;
114 		case 'm':
115 			mflag = 1;
116 			break;
117 		case 'n':
118 			sep = NULL;
119 			break;
120 		case 'w':
121 			wflag = 1;
122 			break;
123 		case '?':
124 		default:
125 			usage(NULL);
126 		}
127 	}
128 
129 	argc -= optind;
130 	argv += optind;
131 
132 	if (dflag + kflag + mflag == 0)
133 		kflag = 1;
134 	if (dflag + kflag + mflag > 1)
135 		usage("only one of -k, -d or -m may be given");
136 	if (argc > 0 && aflag != 0)
137 		usage("excess arguments after -a");
138 	if (aflag != 0 && wflag != 0)
139 		usage("only one of -a or -w may be given");
140 
141 	if (kflag) {
142 		if (file == NULL)
143 			file = PATH_KEYBOARD;
144 		field_tab = keyboard_field_tab;
145 		field_tab_len = keyboard_field_tab_len;
146 		getval = keyboard_get_values;
147 		putval = keyboard_put_values;
148 	} else if (mflag) {
149 		if (file == NULL)
150 			file = PATH_MOUSE;
151 		field_tab = mouse_field_tab;
152 		field_tab_len = mouse_field_tab_len;
153 		getval = mouse_get_values;
154 		putval = mouse_put_values;
155 	} else if (dflag) {
156 		if (file == NULL)
157 			file = PATH_DISPLAY;
158 		field_tab = display_field_tab;
159 		field_tab_len = display_field_tab_len;
160 		getval = display_get_values;
161 		putval = display_put_values;
162 	}
163 
164 	field_setup(field_tab, field_tab_len);
165 
166 	fd = open(file, O_WRONLY);
167 	if (fd < 0)
168 		fd = open(file, O_RDONLY);
169 	if (fd < 0)
170 		err(1, "%s", file);
171 
172 	if (aflag != 0) {
173 		for (i = 0; i < field_tab_len; i++)
174 			if ((field_tab[i].flags & (FLG_NOAUTO|FLG_WRONLY)) == 0)
175 				field_tab[i].flags |= FLG_GET;
176 		(*getval)(fd);
177 		for (i = 0; i < field_tab_len; i++)
178 			if (field_tab[i].flags & FLG_NOAUTO)
179 				warnx("Use explicit arg to view %s.",
180 				      field_tab[i].name);
181 			else if (field_tab[i].flags & FLG_GET)
182 				pr_field(field_tab + i, sep);
183 	} else if (argc > 0) {
184 		if (wflag != 0) {
185 			for (i = 0; i < argc; i++) {
186 				p = strchr(argv[i], '=');
187 				if (p == NULL)
188 					errx(1, "'=' not found");
189 				if (p > argv[i] && *(p - 1) == '+') {
190 					*(p - 1) = '\0';
191 					do_merge = 1;
192 				} else
193 					do_merge = 0;
194 				*p++ = '\0';
195 				f = field_by_name(argv[i]);
196 				if ((f->flags & FLG_RDONLY) != 0)
197 					errx(1, "%s: read only", argv[i]);
198 				if (do_merge) {
199 					if ((f->flags & FLG_MODIFY) == 0)
200 						errx(1, "%s: can only be set",
201 						     argv[i]);
202 					f->flags |= FLG_GET;
203 					(*getval)(fd);
204 					f->flags &= ~FLG_GET;
205 				}
206 				rd_field(f, p, do_merge);
207 				f->flags |= FLG_SET;
208 				(*putval)(fd);
209 				f->flags &= ~FLG_SET;
210 			}
211 		} else {
212 			for (i = 0; i < argc; i++) {
213 				f = field_by_name(argv[i]);
214 				if ((f->flags & FLG_WRONLY) != 0)
215 					errx(1, "%s: write only", argv[i]);
216 				f->flags |= FLG_GET;
217 			}
218 			(*getval)(fd);
219 			for (i = 0; i < field_tab_len; i++)
220 				if (field_tab[i].flags & FLG_GET)
221 					pr_field(field_tab + i, sep);
222 		}
223 	} else
224 		usage(NULL);
225 
226 	exit(0);
227 }
228