Home | History | Annotate | Line # | Download | only in wsconsctl
wsconsctl.c revision 1.11
      1 /*	$NetBSD: wsconsctl.c,v 1.11 2005/04/29 16:51:33 augustss Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1998, 2004 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/wskbd"
     48 #define PATH_MOUSE		"/dev/wsmouse"
     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(char *);
     59 
     60 static void
     61 usage(char *msg)
     62 {
     63 	const char *progname = getprogname();
     64 
     65 	if (msg != NULL)
     66 		fprintf(stderr, "%s: %s\n\n", progname, msg);
     67 
     68 	fprintf(stderr, "usage: %s [-kmd] [-f file] [-n] name ...\n",
     69 		progname);
     70 	fprintf(stderr, " -or-  %s [-kmd] [-f file] [-n] -w name=value ...\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(int argc, char **argv)
     81 {
     82 	int i, ch, fd;
     83 	int aflag, dflag, kflag, mflag, wflag;
     84 	char *file, *sep, *p;
     85 	struct field *f, *field_tab;
     86 	int do_merge, field_tab_len;
     87 	void (*getval)(int);
     88 	void (*putval)(int);
     89 
     90 	aflag = 0;
     91 	dflag = 0;
     92 	kflag = 0;
     93 	mflag = 0;
     94 	wflag = 0;
     95 	file = NULL;
     96 	sep = "=";
     97 
     98 	while ((ch = getopt(argc, argv, "adf:kmnw")) != -1) {
     99 		switch(ch) {
    100 		case 'a':
    101 			aflag = 1;
    102 			break;
    103 		case 'd':
    104 			dflag = 1;
    105 			break;
    106 		case 'f':
    107 			file = optarg;
    108 			break;
    109 		case 'k':
    110 			kflag = 1;
    111 			break;
    112 		case 'm':
    113 			mflag = 1;
    114 			break;
    115 		case 'n':
    116 			sep = NULL;
    117 			break;
    118 		case 'w':
    119 			wflag = 1;
    120 			break;
    121 		case '?':
    122 		default:
    123 			usage(NULL);
    124 		}
    125 	}
    126 
    127 	argc -= optind;
    128 	argv += optind;
    129 
    130 	if (dflag + kflag + mflag == 0)
    131 		kflag = 1;
    132 	if (dflag + kflag + mflag > 1)
    133 		usage("only one of -k, -d or -m may be given");
    134 	if (argc > 0 && aflag != 0)
    135 		usage("excess arguments after -a");
    136 	if (aflag != 0 && wflag != 0)
    137 		usage("only one of -a or -w may be given");
    138 
    139 	if (kflag) {
    140 		if (file == NULL)
    141 			file = PATH_KEYBOARD;
    142 		field_tab = keyboard_field_tab;
    143 		field_tab_len = keyboard_field_tab_len;
    144 		getval = keyboard_get_values;
    145 		putval = keyboard_put_values;
    146 	} else if (mflag) {
    147 		if (file == NULL)
    148 			file = PATH_MOUSE;
    149 		field_tab = mouse_field_tab;
    150 		field_tab_len = mouse_field_tab_len;
    151 		getval = mouse_get_values;
    152 		putval = mouse_put_values;
    153 	} else if (dflag) {
    154 		if (file == NULL)
    155 			file = PATH_DISPLAY;
    156 		field_tab = display_field_tab;
    157 		field_tab_len = display_field_tab_len;
    158 		getval = display_get_values;
    159 		putval = display_put_values;
    160 	}
    161 
    162 	field_setup(field_tab, field_tab_len);
    163 
    164 	fd = open(file, O_WRONLY);
    165 	if (fd < 0)
    166 		fd = open(file, O_RDONLY);
    167 	if (fd < 0)
    168 		err(1, "%s", file);
    169 
    170 	if (aflag != 0) {
    171 		for (i = 0; i < field_tab_len; i++)
    172 			if ((field_tab[i].flags & (FLG_NOAUTO|FLG_WRONLY)) == 0)
    173 				field_tab[i].flags |= FLG_GET;
    174 		(*getval)(fd);
    175 		for (i = 0; i < field_tab_len; i++)
    176 			if (field_tab[i].flags & FLG_NOAUTO)
    177 				warnx("Use explicit arg to view %s.",
    178 				      field_tab[i].name);
    179 			else if (field_tab[i].flags & FLG_GET &&
    180 				 !(field_tab[i].flags & FLG_DISABLED))
    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: write 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_DISABLED)
    220 					errx(1, "%s: no kernel support",
    221 					     field_tab[i].name);
    222 				if (field_tab[i].flags & FLG_GET)
    223 					pr_field(field_tab + i, sep);
    224 			}
    225 		}
    226 	} else {
    227 		close(fd);
    228 		usage(NULL);
    229 	}
    230 
    231 	close(fd);
    232 	exit(0);
    233 }
    234