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