xref: /netbsd-src/sbin/wsconsctl/wsconsctl.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: wsconsctl.c,v 1.2 1998/12/29 22:40:20 hannken 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 <unistd.h>
44 #include "wsconsctl.h"
45 
46 #define PATH_KEYBOARD		"/dev/wskbd0"
47 #define PATH_MOUSE		"/dev/wsmouse0"
48 #define PATH_DISPLAY		"/dev/ttyE0"
49 
50 extern const char *__progname;		/* from crt0.o */
51 
52 extern struct field keyboard_field_tab[];
53 extern struct field mouse_field_tab[];
54 extern struct field display_field_tab[];
55 extern int keyboard_field_tab_len;
56 extern int mouse_field_tab_len;
57 extern int display_field_tab_len;
58 
59 static void usage __P((char *));
60 int main __P((int, char **));
61 
62 static void
63 usage(msg)
64 	char *msg;
65 {
66 	if (msg != NULL)
67 		fprintf(stderr, "%s: %s\n\n", __progname, msg);
68 
69 	fprintf(stderr, "usage: %s [-kmd] [-f file] [-n] name ...\n",
70 		__progname);
71 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -w name=value ...\n",
72 		__progname);
73 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -a\n", __progname);
74 
75 	exit(1);
76 }
77 
78 int
79 main(argc, argv)
80 	int argc;
81 	char **argv;
82 {
83 	int i, ch, fd;
84 	int aflag, dflag, kflag, mflag, wflag;
85 	char *file, *sep, *p;
86 	struct field *f, *field_tab;
87 	int do_merge, field_tab_len;
88 	void (*getval) __P((int));
89 	void (*putval) __P((int));
90 
91 	aflag = 0;
92 	dflag = 0;
93 	kflag = 0;
94 	mflag = 0;
95 	wflag = 0;
96 	file = NULL;
97 	sep = "=";
98 
99 	while ((ch = getopt(argc, argv, "adf:kmnw")) != -1) {
100 		switch(ch) {
101 		case 'a':
102 			aflag = 1;
103 			break;
104 		case 'd':
105 			dflag = 1;
106 			break;
107 		case 'f':
108 			file = optarg;
109 			break;
110 		case 'k':
111 			kflag = 1;
112 			break;
113 		case 'm':
114 			mflag = 1;
115 			break;
116 		case 'n':
117 			sep = NULL;
118 			break;
119 		case 'w':
120 			wflag = 1;
121 			break;
122 		case '?':
123 		default:
124 			usage(NULL);
125 		}
126 	}
127 
128 	argc -= optind;
129 	argv += optind;
130 
131 	if (dflag + kflag + mflag == 0)
132 		kflag = 1;
133 	if (dflag + kflag + mflag > 1)
134 		usage("only one of -k, -d or -m may be given");
135 	if (argc > 0 && aflag != 0)
136 		usage("excess arguments after -a");
137 	if (aflag != 0 && wflag != 0)
138 		usage("only one of -a or -w may be given");
139 
140 	if (kflag) {
141 		if (file == NULL)
142 			file = PATH_KEYBOARD;
143 		field_tab = keyboard_field_tab;
144 		field_tab_len = keyboard_field_tab_len;
145 		getval = keyboard_get_values;
146 		putval = keyboard_put_values;
147 	} else if (mflag) {
148 		if (file == NULL)
149 			file = PATH_MOUSE;
150 		field_tab = mouse_field_tab;
151 		field_tab_len = mouse_field_tab_len;
152 		getval = mouse_get_values;
153 		putval = mouse_put_values;
154 	} else if (dflag) {
155 		if (file == NULL)
156 			file = PATH_DISPLAY;
157 		field_tab = display_field_tab;
158 		field_tab_len = display_field_tab_len;
159 		getval = display_get_values;
160 		putval = display_put_values;
161 	}
162 
163 	field_setup(field_tab, field_tab_len);
164 
165 	fd = open(file, O_WRONLY);
166 	if (fd < 0)
167 		fd = open(file, O_RDONLY);
168 	if (fd < 0)
169 		err(1, "%s", file);
170 
171 	if (aflag != 0) {
172 		for (i = 0; i < field_tab_len; i++)
173 			if ((field_tab[i].flags & (FLG_NOAUTO|FLG_WRONLY)) == 0)
174 				field_tab[i].flags |= FLG_GET;
175 		(*getval)(fd);
176 		for (i = 0; i < field_tab_len; i++)
177 			if (field_tab[i].flags & FLG_NOAUTO)
178 				warnx("Use explicit arg to view %s.",
179 				      field_tab[i].name);
180 			else if (field_tab[i].flags & FLG_GET)
181 				pr_field(field_tab + i, sep);
182 	} else if (argc > 0) {
183 		if (wflag != 0) {
184 			for (i = 0; i < argc; i++) {
185 				p = strchr(argv[i], '=');
186 				if (p == NULL)
187 					errx(1, "'=' not found");
188 				if (p > argv[i] && *(p - 1) == '+') {
189 					*(p - 1) = '\0';
190 					do_merge = 1;
191 				} else
192 					do_merge = 0;
193 				*p++ = '\0';
194 				f = field_by_name(argv[i]);
195 				if ((f->flags & FLG_RDONLY) != 0)
196 					errx(1, "%s: read only", argv[i]);
197 				if (do_merge) {
198 					if ((f->flags & FLG_MODIFY) == 0)
199 						errx(1, "%s: can only be set",
200 						     argv[i]);
201 					f->flags |= FLG_GET;
202 					(*getval)(fd);
203 					f->flags &= ~FLG_GET;
204 				}
205 				rd_field(f, p, do_merge);
206 				f->flags |= FLG_SET;
207 				(*putval)(fd);
208 				f->flags &= ~FLG_SET;
209 			}
210 		} else {
211 			for (i = 0; i < argc; i++) {
212 				f = field_by_name(argv[i]);
213 				if ((f->flags & FLG_WRONLY) != 0)
214 					errx(1, "%s: read only", argv[i]);
215 				f->flags |= FLG_GET;
216 			}
217 			(*getval)(fd);
218 			for (i = 0; i < field_tab_len; i++)
219 				if (field_tab[i].flags & FLG_GET)
220 					pr_field(field_tab + i, sep);
221 		}
222 	} else
223 		usage(NULL);
224 
225 	exit(0);
226 }
227