14Srgrimes/*-
24Srgrimes * Copyright (c) 1992 Christopher G. Demetriou
34Srgrimes * All rights reserved.
44Srgrimes *
54Srgrimes * Redistribution and use in source and binary forms, with or without
64Srgrimes * modification, are permitted provided that the following conditions
74Srgrimes * are met:
84Srgrimes * 1. Redistributions of source code must retain the above copyright
94Srgrimes *    notice, this list of conditions and the following disclaimer.
104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
114Srgrimes *    notice, this list of conditions and the following disclaimer in the
124Srgrimes *    documentation and/or other materials provided with the distribution.
134Srgrimes * 3. The name of the author may not be used to endorse or promote
144Srgrimes *    products derived from this software without specific written permission.
154Srgrimes *
164Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
174Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
204Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264Srgrimes * SUCH DAMAGE.
274Srgrimes */
284Srgrimes
29114589Sobrien#include <sys/cdefs.h>
30114589Sobrien__FBSDID("$FreeBSD$");
314Srgrimes
3237273Scharnier#include <ctype.h>
3337273Scharnier#include <err.h>
3459838Sache#include <errno.h>
3537273Scharnier#include <fcntl.h>
3637273Scharnier#include <stdio.h>
3737273Scharnier#include <stdlib.h>
3878732Sdd#include <string.h>
3937273Scharnier#include <unistd.h>
404Srgrimes#include <sys/types.h>
414Srgrimes#include <sys/ioctl.h>
424Srgrimes
43118671Sjohanstatic void usage(void);
44118671Sjohan
4537273Scharnierstatic void
46201227Sedusage(void)
474Srgrimes{
4837273Scharnier	fprintf(stderr,
4962470Ssheldonh	"usage: comcontrol <filename> [dtrwait <n>] [drainwait <n>]\n");
504Srgrimes	exit(1);
514Srgrimes}
524Srgrimes
5337273Scharnierint
5437273Scharniermain(int argc, char *argv[])
554Srgrimes{
564Srgrimes	int	fd;
575397Sache	int     res = 0;
5859838Sache	int     print_dtrwait = 1, print_drainwait = 1;
595397Sache	int     dtrwait = -1, drainwait = -1;
604Srgrimes
615459Sbde	if (argc < 2)
6237273Scharnier		usage();
634Srgrimes
6459791Sache	if (strcmp(argv[1], "-") == 0)
6559791Sache		fd = STDIN_FILENO;
6659791Sache	else {
6759791Sache		fd = open(argv[1], O_RDONLY|O_NONBLOCK, 0);
6859791Sache		if (fd < 0) {
6959791Sache			warn("couldn't open file %s", argv[1]);
7059791Sache			return 1;
7159791Sache		}
724Srgrimes	}
734Srgrimes	if (argc == 2) {
74837Sache		if (ioctl(fd, TIOCMGDTRWAIT, &dtrwait) < 0) {
7559838Sache			print_dtrwait = 0;
7659838Sache			if (errno != ENOTTY) {
7759838Sache				res = 1;
7859838Sache				warn("TIOCMGDTRWAIT");
7959838Sache			}
80837Sache		}
815397Sache		if (ioctl(fd, TIOCGDRAINWAIT, &drainwait) < 0) {
8259838Sache			print_drainwait = 0;
8359838Sache			if (errno != ENOTTY) {
8459838Sache				res = 1;
8559838Sache				warn("TIOCGDRAINWAIT");
8659838Sache			}
875397Sache		}
8859838Sache		if (print_dtrwait)
8959838Sache			printf("dtrwait %d ", dtrwait);
9059838Sache		if (print_drainwait)
9159838Sache			printf("drainwait %d ", drainwait);
9259838Sache		printf("\n");
934Srgrimes	} else {
94837Sache		while (argv[2] != NULL) {
951629Sache			if (!strcmp(argv[2],"dtrwait")) {
96837Sache				if (dtrwait >= 0)
9737273Scharnier					usage();
9859838Sache				if (argv[3] == NULL || !isdigit(argv[3][0]))
9959838Sache					usage();
100837Sache				dtrwait = atoi(argv[3]);
101837Sache				argv += 2;
1025397Sache			} else if (!strcmp(argv[2],"drainwait")) {
1035397Sache				if (drainwait >= 0)
10437273Scharnier					usage();
10559838Sache				if (argv[3] == NULL || !isdigit(argv[3][0]))
10659838Sache					usage();
1075397Sache				drainwait = atoi(argv[3]);
1085397Sache				argv += 2;
1095397Sache			} else
11037273Scharnier				usage();
1114Srgrimes		}
112837Sache		if (dtrwait >= 0) {
113837Sache			if (ioctl(fd, TIOCMSDTRWAIT, &dtrwait) < 0) {
1145397Sache				res = 1;
11537273Scharnier				warn("TIOCMSDTRWAIT");
116837Sache			}
117837Sache		}
1185397Sache		if (drainwait >= 0) {
1195397Sache			if (ioctl(fd, TIOCSDRAINWAIT, &drainwait) < 0) {
1205397Sache				res = 1;
12137273Scharnier				warn("TIOCSDRAINWAIT");
1225397Sache			}
1235397Sache		}
1244Srgrimes	}
1254Srgrimes
1264Srgrimes	close(fd);
1275397Sache	return res;
1284Srgrimes}
129