1295016Sjkim/*-
2280304Sjkim * Copyright (c) 1992 Christopher G. Demetriou
3280304Sjkim * All rights reserved.
4280304Sjkim *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon * 1. Redistributions of source code must retain the above copyright
9160814Ssimon *    notice, this list of conditions and the following disclaimer.
10160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
11160814Ssimon *    notice, this list of conditions and the following disclaimer in the
12160814Ssimon *    documentation and/or other materials provided with the distribution.
13160814Ssimon * 3. The name of the author may not be used to endorse or promote
14280304Sjkim *    products derived from this software without specific written permission.
15160814Ssimon *
16160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19160814Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26160814Ssimon * SUCH DAMAGE.
27160814Ssimon */
28160814Ssimon
29160814Ssimon#include <sys/cdefs.h>
30160814Ssimon__FBSDID("$FreeBSD$");
31160814Ssimon
32160814Ssimon#include <ctype.h>
33160814Ssimon#include <err.h>
34160814Ssimon#include <errno.h>
35160814Ssimon#include <fcntl.h>
36160814Ssimon#include <stdio.h>
37160814Ssimon#include <stdlib.h>
38160814Ssimon#include <string.h>
39160814Ssimon#include <unistd.h>
40160814Ssimon#include <sys/types.h>
41160814Ssimon#include <sys/ioctl.h>
42160814Ssimon
43160814Ssimonstatic void usage(void);
44160814Ssimon
45160814Ssimonstatic void
46160814Ssimonusage(void)
47160814Ssimon{
48160814Ssimon	fprintf(stderr,
49160814Ssimon	"usage: comcontrol <filename> [dtrwait <n>] [drainwait <n>]\n");
50160814Ssimon	exit(1);
51160814Ssimon}
52160814Ssimon
53160814Ssimonint
54160814Ssimonmain(int argc, char *argv[])
55160814Ssimon{
56160814Ssimon	int	fd;
57160814Ssimon	int     res = 0;
58160814Ssimon	int     print_dtrwait = 1, print_drainwait = 1;
59160814Ssimon	int     dtrwait = -1, drainwait = -1;
60160814Ssimon
61280304Sjkim	if (argc < 2)
62160814Ssimon		usage();
63280304Sjkim
64280304Sjkim	if (strcmp(argv[1], "-") == 0)
65160814Ssimon		fd = STDIN_FILENO;
66160814Ssimon	else {
67160814Ssimon		fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
68160814Ssimon		if (fd < 0) {
69160814Ssimon			warn("couldn't open file %s", argv[1]);
70280304Sjkim			return 1;
71280304Sjkim		}
72280304Sjkim	}
73280304Sjkim	if (argc == 2) {
74280304Sjkim		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
75280304Sjkim			print_dtrwait = 0;
76280304Sjkim			if (errno != ENOTTY) {
77280304Sjkim				res = 1;
78280304Sjkim				warn("TIOCMGDTRWAIT");
79280304Sjkim			}
80280304Sjkim		}
81280304Sjkim		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
82280304Sjkim			print_drainwait = 0;
83280304Sjkim			if (errno != ENOTTY) {
84280304Sjkim				res = 1;
85280304Sjkim				warn("TIOCGDRAINWAIT");
86280304Sjkim			}
87280304Sjkim		}
88280304Sjkim		if (print_dtrwait)
89280304Sjkim			printf("dtrwait %d ", dtrwait);
90280304Sjkim		if (print_drainwait)
91280304Sjkim			printf("drainwait %d ", drainwait);
92280304Sjkim		printf("\n");
93280304Sjkim	} else {
94280304Sjkim		while (argv[2] != NULL) {
95280304Sjkim			if (!strcmp(argv[2],"dtrwait")) {
96280304Sjkim				if (dtrwait >= 0)
97280304Sjkim					usage();
98280304Sjkim				if (argv[3] == NULL || !isdigit(argv[3][0]))
99280304Sjkim					usage();
100280304Sjkim				dtrwait = atoi(argv[3]);
101280304Sjkim				argv += 2;
102280304Sjkim			} else if (!strcmp(argv[2],"drainwait")) {
103280304Sjkim				if (drainwait >= 0)
104280304Sjkim					usage();
105280304Sjkim				if (argv[3] == NULL || !isdigit(argv[3][0]))
106280304Sjkim					usage();
107280304Sjkim				drainwait = atoi(argv[3]);
108280304Sjkim				argv += 2;
109280304Sjkim			} else
110280304Sjkim				usage();
111280304Sjkim		}
112280304Sjkim		if (dtrwait >= 0) {
113160814Ssimon			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
114280304Sjkim				res = 1;
115280304Sjkim				warn("TIOCMSDTRWAIT");
116280304Sjkim			}
117280304Sjkim		}
118280304Sjkim		if (drainwait >= 0) {
119280304Sjkim			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
120280304Sjkim				res = 1;
121160814Ssimon				warn("TIOCSDRAINWAIT");
122160814Ssimon			}
123160814Ssimon		}
124160814Ssimon	}
125160814Ssimon
126	close(fd);
127	return res;
128}
129