wsconscfg.c revision 1.11
1/* $OpenBSD: wsconscfg.c,v 1.11 2004/08/01 18:32:21 deraadt 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 <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <fcntl.h>
40#include <unistd.h>
41#include <sys/types.h>
42#include <sys/ioctl.h>
43#include <err.h>
44#include <errno.h>
45
46#include <dev/wscons/wsconsio.h>
47
48#define DEFDEV "/dev/ttyCcfg"
49
50static void usage(void);
51int main(int, char**);
52
53static void
54usage(void)
55{
56	extern char *__progname;
57
58	(void)fprintf(stderr,
59		      "Usage: %s [-dFkm] [-e emul] [-f ctldev] "
60		      "[-t type] index\n", __progname);
61	exit(1);
62}
63
64int
65main(int argc, char *argv[])
66{
67	char *wsdev;
68	int c, delete, kbd, idx, wsfd, res, mux;
69	struct wsdisplay_addscreendata asd;
70	struct wsdisplay_delscreendata dsd;
71	struct wsmux_device wmd;
72
73	wsdev = DEFDEV;
74	delete = 0;
75	kbd = 0;
76	mux = 0;
77	asd.screentype[0] = 0;
78	asd.emul[0] = 0;
79	dsd.flags = 0;
80
81	while ((c = getopt(argc, argv, "f:dkmt:e:F")) != -1) {
82		switch (c) {
83		case 'f':
84			wsdev = optarg;
85			break;
86		case 'd':
87			delete++;
88			break;
89		case 'k':
90			kbd++;
91			break;
92		case 'm':
93			mux++;
94			kbd++;
95			break;
96		case 't':
97			strlcpy(asd.screentype, optarg, WSSCREEN_NAME_SIZE);
98			break;
99		case 'e':
100			strlcpy(asd.emul, optarg, WSEMUL_NAME_SIZE);
101			break;
102		case 'F':
103			dsd.flags |= WSDISPLAY_DELSCR_FORCE;
104			break;
105		case '?':
106		default:
107			usage();
108			break;
109		}
110	}
111	argc -= optind;
112	argv += optind;
113
114	if (kbd ? (argc > 1) : (argc != 1))
115		usage();
116
117	idx = -1;
118	if (argc > 0 && sscanf(argv[0], "%d", &idx) != 1)
119		errx(1, "invalid index");
120
121	wsfd = open(wsdev, O_RDWR, 0);
122	if (wsfd < 0)
123		err(2, "%s", wsdev);
124
125	if (kbd) {
126		if (mux)
127			wmd.type = WSMUX_MUX;
128		else
129			wmd.type = WSMUX_KBD;
130		wmd.idx = idx;
131		if (delete) {
132			res = ioctl(wsfd, WSMUX_REMOVE_DEVICE, &wmd);
133			if (res < 0)
134				err(3, "WSMUX_REMOVE_DEVICE");
135		} else {
136			res = ioctl(wsfd, WSMUX_ADD_DEVICE, &wmd);
137			if (res < 0)
138				err(3, "WSMUX_ADD_DEVICE");
139		}
140	} else if (delete) {
141		dsd.idx = idx;
142		res = ioctl(wsfd, WSDISPLAYIO_DELSCREEN, &dsd);
143		if (res < 0)
144			err(3, "WSDISPLAYIO_DELSCREEN");
145	} else {
146		asd.idx = idx;
147		res = ioctl(wsfd, WSDISPLAYIO_ADDSCREEN, &asd);
148		if (res < 0) {
149			if (errno == EBUSY)
150				errx(3, "screen %d is already configured", idx);
151			else
152				err(3, "WSDISPLAYIO_ADDSCREEN");
153		}
154	}
155
156	return (0);
157}
158