xref: /openbsd/usr.sbin/wsconscfg/wsconscfg.c (revision d7259957)
1 /* $OpenBSD: wsconscfg.c,v 1.18 2022/12/04 23:50:51 cheloha 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 [-dFkm] [-e emul] [-f ctldev] [-t type] index\n",
56 	    __progname);
57 	exit(1);
58 }
59 
60 int
main(int argc,char * argv[])61 main(int argc, char *argv[])
62 {
63 	char *wsdev;
64 	int c, delete, kbd, idx, wsfd, res, mux;
65 	struct wsdisplay_addscreendata asd;
66 	struct wsdisplay_delscreendata dsd;
67 	struct wsmux_device wmd;
68 
69 	wsdev = DEFDEV;
70 	delete = 0;
71 	kbd = 0;
72 	mux = 0;
73 	asd.screentype[0] = 0;
74 	asd.emul[0] = 0;
75 	dsd.flags = 0;
76 
77 	while ((c = getopt(argc, argv, "f:dkmt:e:F")) != -1) {
78 		switch (c) {
79 		case 'f':
80 			wsdev = optarg;
81 			break;
82 		case 'd':
83 			delete = 1;
84 			break;
85 		case 'k':
86 			kbd = 1;
87 			break;
88 		case 'm':
89 			mux = 1;
90 			kbd = 1;
91 			break;
92 		case 't':
93 			strlcpy(asd.screentype, optarg, WSSCREEN_NAME_SIZE);
94 			break;
95 		case 'e':
96 			strlcpy(asd.emul, optarg, WSEMUL_NAME_SIZE);
97 			break;
98 		case 'F':
99 			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
100 			break;
101 		default:
102 			usage();
103 			break;
104 		}
105 	}
106 	argc -= optind;
107 	argv += optind;
108 
109 	if (kbd ? (argc > 1) : (argc != 1))
110 		usage();
111 
112 	idx = -1;
113 	if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
114 		errx(1, "invalid index");
115 
116 	wsfd = open(wsdev, O_RDWR);
117 	if (wsfd < 0)
118 		err(2, "%s", wsdev);
119 
120 	if (kbd) {
121 		if (mux)
122 			wmd.type = WSMUX_MUX;
123 		else
124 			wmd.type = WSMUX_KBD;
125 		wmd.idx = idx;
126 		if (delete) {
127 			res = ioctl(wsfd, WSMUXIO_REMOVE_DEVICE, &wmd);
128 			if (res < 0)
129 				err(3, "WSMUXIO_REMOVE_DEVICE");
130 		} else {
131 			res = ioctl(wsfd, WSMUXIO_ADD_DEVICE, &wmd);
132 			if (res < 0)
133 				err(3, "WSMUXIO_ADD_DEVICE");
134 		}
135 	} else if (delete) {
136 		dsd.idx = idx;
137 		res = ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd);
138 		if (res < 0)
139 			err(3, "WSDISPLAYIO_DELSCREEN");
140 	} else {
141 		asd.idx = idx;
142 		res = ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd);
143 		if (res < 0) {
144 			if (errno == EBUSY)
145 				errx(3, "screen %d is already configured", idx);
146 			else
147 				err(3, "WSDISPLAYIO_ADDSCREEN");
148 		}
149 	}
150 
151 	return (0);
152 }
153