xref: /netbsd-src/usr.sbin/wsconscfg/wsconscfg.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /* $NetBSD: wsconscfg.c,v 1.12 2005/07/12 08:48:20 wiz Exp $ */
2 
3 /*
4  * Copyright (c) 1999
5  *	Matthias Drochner.  All rights reserved.
6  *
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 for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <err.h>
42 #include <errno.h>
43 
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wsdisplay_usl_io.h>
46 
47 #define DEFDEV "/dev/ttyEcfg"
48 
49 static void usage(void) __attribute__((__unused__));
50 int main(int, char **);
51 
52 static void
53 usage(void)
54 {
55 
56 	(void)fprintf(stderr,
57 	     "Usage: %s [-e emul] [-f ctldev] [-t type] index\n"
58 	     "       %s -d [-F] [-f ctldev] index\n"
59 	     "       %s -k | -m [-d] [-f ctldev] [index]\n"
60 	     "       %s -s [-f ctldev] index\n", getprogname(),
61 	     getprogname(), getprogname(), getprogname());
62 	exit(1);
63 }
64 
65 int
66 main(int argc, char **argv)
67 {
68 	const char *wsdev;
69 	int c, delete, kbd, idx, wsfd, swtch, mux;
70 	struct wsdisplay_addscreendata asd;
71 	struct wsdisplay_delscreendata dsd;
72 	struct wsmux_device wmd;
73 
74 	setprogname(argv[0]);
75 	wsdev = DEFDEV;
76 	delete = 0;
77 	kbd = 0;
78 	mux = 0;
79 	swtch = 0;
80 	idx = -1;
81 	asd.screentype = 0;
82 	asd.emul = 0;
83 	dsd.flags = 0;
84 
85 	while ((c = getopt(argc, argv, "f:dkmst:e:F")) != -1) {
86 		switch (c) {
87 		case 'f':
88 			wsdev = optarg;
89 			break;
90 		case 'd':
91 			delete++;
92 			break;
93 		case 'k':
94 			kbd++;
95 			break;
96 		case 'm':
97 			mux++;
98 			kbd++;
99 			break;
100 		case 's':
101 			swtch++;
102 			break;
103 		case 't':
104 			asd.screentype = optarg;
105 			break;
106 		case 'e':
107 			asd.emul = optarg;
108 			break;
109 		case 'F':
110 			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
111 			break;
112 		case '?':
113 		default:
114 			usage();
115 			break;
116 		}
117 	}
118 	argc -= optind;
119 	argv += optind;
120 
121 	if ((kbd || swtch) ? (argc > 1) : (argc != 1))
122 		usage();
123 
124 	if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
125 		errx(1, "invalid index");
126 
127 	if ((wsfd = open(wsdev, O_RDWR)) == -1)
128 		err(EXIT_FAILURE, "Cannot open `%s'", wsdev);
129 
130 	if (swtch) {
131 		if (ioctl(wsfd, VT_ACTIVATE, idx) == -1)
132 		    err(EXIT_FAILURE, "Cannot switch to %d", idx);
133 	} else if (kbd) {
134 		wmd.type = mux ? WSMUX_MUX : WSMUX_KBD;
135 		wmd.idx = idx;
136 		if (delete) {
137 			if (ioctl(wsfd, WSMUX_REMOVE_DEVICE, &wmd) == -1)
138 				err(EXIT_FAILURE, "WSMUX_REMOVE_DEVICE");
139 		} else {
140 			if (ioctl(wsfd, WSMUX_ADD_DEVICE, &wmd) == -1)
141 				err(EXIT_FAILURE, "WSMUX_ADD_DEVICE");
142 		}
143 	} else if (delete) {
144 		dsd.idx = idx;
145 		if (ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd) == -1)
146 			err(EXIT_FAILURE, "WSDISPLAYIO_DELSCREEN");
147 	} else {
148 		asd.idx = idx;
149 		if (ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd) == -1) {
150 			if (errno == EBUSY)
151 				errx(EXIT_FAILURE,
152 				    "screen %d is already configured", idx);
153 			else
154 				err(EXIT_FAILURE, "WSDISPLAYIO_ADDSCREEN");
155 		}
156 	}
157 
158 	return 0;
159 }
160