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