Home | History | Annotate | Line # | Download | only in wsconsctl
wsconsctl.c revision 1.7
      1 /*	$NetBSD: wsconsctl.c,v 1.7 2004/06/25 14:33:48 wiz 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] -w name+=value ...\n",
     75 		progname);
     76 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -a\n", progname);
     77 
     78 	exit(1);
     79 }
     80 
     81 int
     82 main(argc, argv)
     83 	int argc;
     84 	char **argv;
     85 {
     86 	int i, ch, fd;
     87 	int aflag, dflag, kflag, mflag, wflag;
     88 	char *file, *sep, *p;
     89 	struct field *f, *field_tab;
     90 	int do_merge, field_tab_len;
     91 	void (*getval) __P((int));
     92 	void (*putval) __P((int));
     93 
     94 	aflag = 0;
     95 	dflag = 0;
     96 	kflag = 0;
     97 	mflag = 0;
     98 	wflag = 0;
     99 	file = NULL;
    100 	sep = "=";
    101 
    102 	while ((ch = getopt(argc, argv, "adf:kmnw")) != -1) {
    103 		switch(ch) {
    104 		case 'a':
    105 			aflag = 1;
    106 			break;
    107 		case 'd':
    108 			dflag = 1;
    109 			break;
    110 		case 'f':
    111 			file = optarg;
    112 			break;
    113 		case 'k':
    114 			kflag = 1;
    115 			break;
    116 		case 'm':
    117 			mflag = 1;
    118 			break;
    119 		case 'n':
    120 			sep = NULL;
    121 			break;
    122 		case 'w':
    123 			wflag = 1;
    124 			break;
    125 		case '?':
    126 		default:
    127 			usage(NULL);
    128 		}
    129 	}
    130 
    131 	argc -= optind;
    132 	argv += optind;
    133 
    134 	if (dflag + kflag + mflag == 0)
    135 		kflag = 1;
    136 	if (dflag + kflag + mflag > 1)
    137 		usage("only one of -k, -d or -m may be given");
    138 	if (argc > 0 && aflag != 0)
    139 		usage("excess arguments after -a");
    140 	if (aflag != 0 && wflag != 0)
    141 		usage("only one of -a or -w may be given");
    142 
    143 	if (kflag) {
    144 		if (file == NULL)
    145 			file = PATH_KEYBOARD;
    146 		field_tab = keyboard_field_tab;
    147 		field_tab_len = keyboard_field_tab_len;
    148 		getval = keyboard_get_values;
    149 		putval = keyboard_put_values;
    150 	} else if (mflag) {
    151 		if (file == NULL)
    152 			file = PATH_MOUSE;
    153 		field_tab = mouse_field_tab;
    154 		field_tab_len = mouse_field_tab_len;
    155 		getval = mouse_get_values;
    156 		putval = mouse_put_values;
    157 	} else if (dflag) {
    158 		if (file == NULL)
    159 			file = PATH_DISPLAY;
    160 		field_tab = display_field_tab;
    161 		field_tab_len = display_field_tab_len;
    162 		getval = display_get_values;
    163 		putval = display_put_values;
    164 	}
    165 
    166 	field_setup(field_tab, field_tab_len);
    167 
    168 	fd = open(file, O_WRONLY);
    169 	if (fd < 0)
    170 		fd = open(file, O_RDONLY);
    171 	if (fd < 0)
    172 		err(1, "%s", file);
    173 
    174 	if (aflag != 0) {
    175 		for (i = 0; i < field_tab_len; i++)
    176 			if ((field_tab[i].flags & (FLG_NOAUTO|FLG_WRONLY)) == 0)
    177 				field_tab[i].flags |= FLG_GET;
    178 		(*getval)(fd);
    179 		for (i = 0; i < field_tab_len; i++)
    180 			if (field_tab[i].flags & FLG_NOAUTO)
    181 				warnx("Use explicit arg to view %s.",
    182 				      field_tab[i].name);
    183 			else if (field_tab[i].flags & FLG_GET)
    184 				pr_field(field_tab + i, sep);
    185 	} else if (argc > 0) {
    186 		if (wflag != 0) {
    187 			for (i = 0; i < argc; i++) {
    188 				p = strchr(argv[i], '=');
    189 				if (p == NULL)
    190 					errx(1, "'=' not found");
    191 				if (p > argv[i] && *(p - 1) == '+') {
    192 					*(p - 1) = '\0';
    193 					do_merge = 1;
    194 				} else
    195 					do_merge = 0;
    196 				*p++ = '\0';
    197 				f = field_by_name(argv[i]);
    198 				if ((f->flags & FLG_RDONLY) != 0)
    199 					errx(1, "%s: read only", argv[i]);
    200 				if (do_merge) {
    201 					if ((f->flags & FLG_MODIFY) == 0)
    202 						errx(1, "%s: can only be set",
    203 						     argv[i]);
    204 					f->flags |= FLG_GET;
    205 					(*getval)(fd);
    206 					f->flags &= ~FLG_GET;
    207 				}
    208 				rd_field(f, p, do_merge);
    209 				f->flags |= FLG_SET;
    210 				(*putval)(fd);
    211 				f->flags &= ~FLG_SET;
    212 			}
    213 		} else {
    214 			for (i = 0; i < argc; i++) {
    215 				f = field_by_name(argv[i]);
    216 				if ((f->flags & FLG_WRONLY) != 0)
    217 					errx(1, "%s: write only", argv[i]);
    218 				f->flags |= FLG_GET;
    219 			}
    220 			(*getval)(fd);
    221 			for (i = 0; i < field_tab_len; i++)
    222 				if (field_tab[i].flags & FLG_GET)
    223 					pr_field(field_tab + i, sep);
    224 		}
    225 	} else {
    226 		close(fd);
    227 		usage(NULL);
    228 	}
    229 
    230 	close(fd);
    231 	exit(0);
    232 }
    233