137ec494ru/*	$OpenBSD: cu.c,v 1.19 2006/05/25 08:41:52 jmc Exp $	*/
2ea00e48markm/*	$NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $	*/
3ea00e48markm
4872b698pfg/*-
5872b698pfg * SPDX-License-Identifier: BSD-3-Clause
6872b698pfg *
73242e11jkh * Copyright (c) 1983, 1993
83242e11jkh *	The Regents of the University of California.  All rights reserved.
93242e11jkh *
103242e11jkh * Redistribution and use in source and binary forms, with or without
113242e11jkh * modification, are permitted provided that the following conditions
123242e11jkh * are met:
133242e11jkh * 1. Redistributions of source code must retain the above copyright
143242e11jkh *    notice, this list of conditions and the following disclaimer.
153242e11jkh * 2. Redistributions in binary form must reproduce the above copyright
163242e11jkh *    notice, this list of conditions and the following disclaimer in the
173242e11jkh *    documentation and/or other materials provided with the distribution.
1837ec494ru * 3. Neither the name of the University nor the names of its contributors
193242e11jkh *    may be used to endorse or promote products derived from this software
203242e11jkh *    without specific prior written permission.
213242e11jkh *
223242e11jkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233242e11jkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243242e11jkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253242e11jkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263242e11jkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273242e11jkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283242e11jkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293242e11jkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303242e11jkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313242e11jkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323242e11jkh * SUCH DAMAGE.
333242e11jkh */
343242e11jkh
35ea00e48markm#include <sys/cdefs.h>
36ea00e48markm__FBSDID("$FreeBSD$");
37ea00e48markm
383242e11jkh#ifndef lint
396fbe0aacharnier#if 0
403242e11jkhstatic char sccsid[] = "@(#)cu.c	8.1 (Berkeley) 6/6/93";
4137ec494rustatic const char rcsid[] = "$OpenBSD: cu.c,v 1.19 2006/05/25 08:41:52 jmc Exp $";
426fbe0aacharnier#endif
433242e11jkh#endif /* not lint */
443242e11jkh
453242e11jkh#include "tip.h"
463242e11jkh
4737ec494rustatic void	cuusage(void);
483242e11jkh
493242e11jkh/*
503242e11jkh * Botch the interface to look like cu's
513242e11jkh */
52ea00e48markmvoid
5337ec494rucumain(int argc, char *argv[])
543242e11jkh{
5537ec494ru	int ch, i, parity;
56ea00e48markm	long l;
57ea00e48markm	char *cp;
583242e11jkh	static char sbuf[12];
593242e11jkh
60ea00e48markm	if (argc < 2)
61ea00e48markm		cuusage();
623242e11jkh	CU = DV = NOSTR;
633242e11jkh	BR = DEFBR;
6437ec494ru	parity = 0;	/* none */
655da4fcdru
665da4fcdru	/*
675da4fcdru	 * We want to accept -# as a speed.  It's easiest to look through
685da4fcdru	 * the arguments, replace -# with -s#, and let getopt() handle it.
695da4fcdru	 */
705da4fcdru	for (i = 1; i < argc; i++) {
715da4fcdru		if (argv[i][0] == '-' &&
725da4fcdru		    argv[i][1] >= '0' && argv[i][1] <= '9') {
735da4fcdru			asprintf(&cp, "-s%s", argv[i] + 1);
745da4fcdru			if (cp == NULL) {
755da4fcdru				fprintf(stderr,
765da4fcdru				    "%s: cannot convert -# to -s#\n",
775da4fcdru				    __progname);
785da4fcdru				exit(3);
795da4fcdru			}
805da4fcdru			argv[i] = cp;
815da4fcdru		}
825da4fcdru	}
835da4fcdru
845da4fcdru	while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
8537ec494ru		switch (ch) {
863242e11jkh		case 'a':
87ea00e48markm			CU = optarg;
88ea00e48markm			break;
89ea00e48markm		case 'l':
90ea00e48markm			if (DV != NULL) {
91ea00e48markm				fprintf(stderr,
92ea00e48markm				    "%s: cannot specificy multiple -l options\n",
93ea00e48markm				    __progname);
94ea00e48markm				exit(3);
95ea00e48markm			}
96ea00e48markm			if (strchr(optarg, '/'))
97ea00e48markm				DV = optarg;
98ea00e48markm			else
99ea00e48markm				asprintf(&DV, "/dev/%s", optarg);
1003242e11jkh			break;
1013242e11jkh		case 's':
102ea00e48markm			l = strtol(optarg, &cp, 10);
10337ec494ru			if (*cp != '\0' || l < 0 || l >= INT_MAX) {
104ea00e48markm				fprintf(stderr, "%s: unsupported speed %s\n",
105ea00e48markm				    __progname, optarg);
1063242e11jkh				exit(3);
1073242e11jkh			}
108ea00e48markm			BR = (int)l;
1093242e11jkh			break;
110ea00e48markm		case 'h':
111ea00e48markm			setboolean(value(LECHO), TRUE);
112ea00e48markm			HD = TRUE;
113ea00e48markm			break;
114ea00e48markm		case 't':
115ea00e48markm			HW = 1, DU = -1;
116ea00e48markm			break;
117ea00e48markm		case 'o':
11837ec494ru			if (parity != 0)
11937ec494ru				parity = 0;	/* -e -o */
12037ec494ru			else
12137ec494ru				parity = 1;	/* odd */
122ea00e48markm			break;
123ea00e48markm		case 'e':
12437ec494ru			if (parity != 0)
12537ec494ru				parity = 0;	/* -o -e */
12637ec494ru			else
12737ec494ru				parity = -1;	/* even */
1283242e11jkh			break;
1293242e11jkh		default:
130ea00e48markm			cuusage();
1313242e11jkh			break;
1323242e11jkh		}
1333242e11jkh	}
134ea00e48markm	argc -= optind;
135ea00e48markm	argv += optind;
136ea00e48markm
137ea00e48markm	switch (argc) {
138ea00e48markm	case 1:
139ea00e48markm		PN = argv[0];
140ea00e48markm		break;
141ea00e48markm	case 0:
142ea00e48markm		break;
143ea00e48markm	default:
144ea00e48markm		cuusage();
145ea00e48markm		break;
146ea00e48markm	}
147ea00e48markm
1483242e11jkh	signal(SIGINT, cleanup);
1493242e11jkh	signal(SIGQUIT, cleanup);
1503242e11jkh	signal(SIGHUP, cleanup);
1513242e11jkh	signal(SIGTERM, cleanup);
15237ec494ru	signal(SIGCHLD, SIG_DFL);
1533242e11jkh
1543242e11jkh	/*
1553242e11jkh	 * The "cu" host name is used to define the
1563242e11jkh	 * attributes of the generic dialer.
1573242e11jkh	 */
158ea00e48markm	(void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
1593242e11jkh	if ((i = hunt(sbuf)) == 0) {
1603242e11jkh		printf("all ports busy\n");
1613242e11jkh		exit(3);
1623242e11jkh	}
1633242e11jkh	if (i == -1) {
1643242e11jkh		printf("link down\n");
1653242e11jkh		(void)uu_unlock(uucplock);
1663242e11jkh		exit(3);
1673242e11jkh	}
1683242e11jkh	setbuf(stdout, NULL);
1693242e11jkh	loginit();
1703242e11jkh	user_uid();
1713242e11jkh	vinit();
17237ec494ru	switch (parity) {
17337ec494ru	case -1:
17437ec494ru		setparity("even");
17537ec494ru		break;
17637ec494ru	case 1:
17737ec494ru		setparity("odd");
17837ec494ru		break;
17937ec494ru	default:
18037ec494ru		setparity("none");
18137ec494ru		break;
18237ec494ru	}
183ea00e48markm	setboolean(value(VERBOSE), FALSE);
18437ec494ru	if (HW && ttysetup(BR)) {
18537ec494ru		fprintf(stderr, "%s: unsupported speed %ld\n",
18637ec494ru		    __progname, BR);
18737ec494ru		daemon_uid();
18837ec494ru		(void)uu_unlock(uucplock);
18937ec494ru		exit(3);
19037ec494ru	}
19137ec494ru	if (con()) {
1923242e11jkh		printf("Connect failed\n");
1933242e11jkh		daemon_uid();
1943242e11jkh		(void)uu_unlock(uucplock);
1953242e11jkh		exit(1);
1963242e11jkh	}
19737ec494ru	if (!HW && ttysetup(BR)) {
19837ec494ru		fprintf(stderr, "%s: unsupported speed %ld\n",
19937ec494ru		    __progname, BR);
20037ec494ru		daemon_uid();
20137ec494ru		(void)uu_unlock(uucplock);
20237ec494ru		exit(3);
20337ec494ru	}
2043242e11jkh}
205ea00e48markm
20637ec494rustatic void
20737ec494rucuusage(void)
208ea00e48markm{
20937ec494ru	fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
21037ec494ru	    "[-s speed | -speed] [phone-number]\n");
211ea00e48markm	exit(8);
212ea00e48markm}
213