xref: /openbsd/usr.sbin/wsconscfg/wsconscfg.c (revision c18ef1e4)
1 /* $OpenBSD: wsconscfg.c,v 1.19 2024/11/06 17:14:03 miod 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/ioctl.h>
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <err.h>
40 #include <errno.h>
41 
42 #include <dev/wscons/wsconsio.h>
43 
44 #define DEFDEV "/dev/ttyCcfg"
45 
46 static void usage(void);
47 int main(int, char**);
48 
49 static void
usage(void)50 usage(void)
51 {
52 	extern char *__progname;
53 
54 	(void)fprintf(stderr,
55 	    "usage: %s [-dFgkm] [-e emul] [-f ctldev] [-t type] index\n",
56 	    __progname);
57 	exit(1);
58 }
59 
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	char *wsdev;
65 	int c, delete, get, kbd, idx, wsfd, res, mux;
66 	struct wsdisplay_addscreendata asd;
67 	struct wsdisplay_delscreendata dsd;
68 	struct wsmux_device wmd;
69 
70 	wsdev = DEFDEV;
71 	delete = 0;
72 	get = 0;
73 	kbd = 0;
74 	mux = 0;
75 	asd.screentype[0] = 0;
76 	asd.emul[0] = 0;
77 	dsd.flags = 0;
78 
79 	while ((c = getopt(argc, argv, "f:dgkmt:e:F")) != -1) {
80 		switch (c) {
81 		case 'f':
82 			wsdev = optarg;
83 			break;
84 		case 'g':
85 			get = 1;
86 			break;
87 		case 'd':
88 			delete = 1;
89 			break;
90 		case 'k':
91 			kbd = 1;
92 			break;
93 		case 'm':
94 			mux = 1;
95 			kbd = 1;
96 			break;
97 		case 't':
98 			strlcpy(asd.screentype, optarg, WSSCREEN_NAME_SIZE);
99 			break;
100 		case 'e':
101 			strlcpy(asd.emul, optarg, WSEMUL_NAME_SIZE);
102 			break;
103 		case 'F':
104 			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
105 			break;
106 		default:
107 			usage();
108 			break;
109 		}
110 	}
111 	argc -= optind;
112 	argv += optind;
113 
114 	if ((kbd && get) ||
115 	    ((kbd || get) ? (argc > 1) : (argc != 1)))
116 		usage();
117 
118 	idx = -1;
119 	if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
120 		errx(1, "invalid index");
121 
122 	wsfd = open(wsdev, get ? O_RDONLY : O_RDWR);
123 	if (wsfd < 0)
124 		err(2, "%s", wsdev);
125 
126 	if (kbd) {
127 		if (mux)
128 			wmd.type = WSMUX_MUX;
129 		else
130 			wmd.type = WSMUX_KBD;
131 		wmd.idx = idx;
132 		if (delete) {
133 			res = ioctl(wsfd, WSMUXIO_REMOVE_DEVICE, &wmd);
134 			if (res < 0)
135 				err(3, "WSMUXIO_REMOVE_DEVICE");
136 		} else {
137 			res = ioctl(wsfd, WSMUXIO_ADD_DEVICE, &wmd);
138 			if (res < 0)
139 				err(3, "WSMUXIO_ADD_DEVICE");
140 		}
141 	} else if (delete) {
142 		dsd.idx = idx;
143 		res = ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd);
144 		if (res < 0)
145 			err(3, "WSDISPLAYIO_DELSCREEN");
146 	} else if (get) {
147 		asd.idx = idx;
148 		res = ioctl(wsfd, WSDISPLAYIO_GETSCREEN, &asd);
149 		if (res < 0)
150 			err(3, "WSDISPLAYIO_GETSCREEN");
151 		else
152 			printf("%d\n", asd.idx);
153 	} else {
154 		asd.idx = idx;
155 		res = ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd);
156 		if (res < 0) {
157 			if (errno == EBUSY)
158 				errx(3, "screen %d is already configured", idx);
159 			else
160 				err(3, "WSDISPLAYIO_ADDSCREEN");
161 		}
162 	}
163 
164 	return (0);
165 }
166