xref: /freebsd/usr.bin/tip/tip/cu.c (revision 780fb4a2)
1 /*	$OpenBSD: cu.c,v 1.19 2006/05/25 08:41:52 jmc Exp $	*/
2 /*	$NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 1983, 1993
8  *	The Regents of the University of California.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)cu.c	8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] = "$OpenBSD: cu.c,v 1.19 2006/05/25 08:41:52 jmc Exp $";
42 #endif
43 #endif /* not lint */
44 
45 #include "tip.h"
46 
47 static void	cuusage(void);
48 
49 /*
50  * Botch the interface to look like cu's
51  */
52 void
53 cumain(int argc, char *argv[])
54 {
55 	int ch, i, parity;
56 	long l;
57 	char *cp;
58 	static char sbuf[12];
59 
60 	if (argc < 2)
61 		cuusage();
62 	CU = DV = NOSTR;
63 	BR = DEFBR;
64 	parity = 0;	/* none */
65 
66 	/*
67 	 * We want to accept -# as a speed.  It's easiest to look through
68 	 * the arguments, replace -# with -s#, and let getopt() handle it.
69 	 */
70 	for (i = 1; i < argc; i++) {
71 		if (argv[i][0] == '-' &&
72 		    argv[i][1] >= '0' && argv[i][1] <= '9') {
73 			asprintf(&cp, "-s%s", argv[i] + 1);
74 			if (cp == NULL) {
75 				fprintf(stderr,
76 				    "%s: cannot convert -# to -s#\n",
77 				    __progname);
78 				exit(3);
79 			}
80 			argv[i] = cp;
81 		}
82 	}
83 
84 	while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
85 		switch (ch) {
86 		case 'a':
87 			CU = optarg;
88 			break;
89 		case 'l':
90 			if (DV != NULL) {
91 				fprintf(stderr,
92 				    "%s: cannot specificy multiple -l options\n",
93 				    __progname);
94 				exit(3);
95 			}
96 			if (strchr(optarg, '/'))
97 				DV = optarg;
98 			else
99 				asprintf(&DV, "/dev/%s", optarg);
100 			break;
101 		case 's':
102 			l = strtol(optarg, &cp, 10);
103 			if (*cp != '\0' || l < 0 || l >= INT_MAX) {
104 				fprintf(stderr, "%s: unsupported speed %s\n",
105 				    __progname, optarg);
106 				exit(3);
107 			}
108 			BR = (int)l;
109 			break;
110 		case 'h':
111 			setboolean(value(LECHO), TRUE);
112 			HD = TRUE;
113 			break;
114 		case 't':
115 			HW = 1, DU = -1;
116 			break;
117 		case 'o':
118 			if (parity != 0)
119 				parity = 0;	/* -e -o */
120 			else
121 				parity = 1;	/* odd */
122 			break;
123 		case 'e':
124 			if (parity != 0)
125 				parity = 0;	/* -o -e */
126 			else
127 				parity = -1;	/* even */
128 			break;
129 		default:
130 			cuusage();
131 			break;
132 		}
133 	}
134 	argc -= optind;
135 	argv += optind;
136 
137 	switch (argc) {
138 	case 1:
139 		PN = argv[0];
140 		break;
141 	case 0:
142 		break;
143 	default:
144 		cuusage();
145 		break;
146 	}
147 
148 	signal(SIGINT, cleanup);
149 	signal(SIGQUIT, cleanup);
150 	signal(SIGHUP, cleanup);
151 	signal(SIGTERM, cleanup);
152 	signal(SIGCHLD, SIG_DFL);
153 
154 	/*
155 	 * The "cu" host name is used to define the
156 	 * attributes of the generic dialer.
157 	 */
158 	(void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
159 	if ((i = hunt(sbuf)) == 0) {
160 		printf("all ports busy\n");
161 		exit(3);
162 	}
163 	if (i == -1) {
164 		printf("link down\n");
165 		(void)uu_unlock(uucplock);
166 		exit(3);
167 	}
168 	setbuf(stdout, NULL);
169 	loginit();
170 	user_uid();
171 	vinit();
172 	switch (parity) {
173 	case -1:
174 		setparity("even");
175 		break;
176 	case 1:
177 		setparity("odd");
178 		break;
179 	default:
180 		setparity("none");
181 		break;
182 	}
183 	setboolean(value(VERBOSE), FALSE);
184 	if (HW && ttysetup(BR)) {
185 		fprintf(stderr, "%s: unsupported speed %ld\n",
186 		    __progname, BR);
187 		daemon_uid();
188 		(void)uu_unlock(uucplock);
189 		exit(3);
190 	}
191 	if (con()) {
192 		printf("Connect failed\n");
193 		daemon_uid();
194 		(void)uu_unlock(uucplock);
195 		exit(1);
196 	}
197 	if (!HW && ttysetup(BR)) {
198 		fprintf(stderr, "%s: unsupported speed %ld\n",
199 		    __progname, BR);
200 		daemon_uid();
201 		(void)uu_unlock(uucplock);
202 		exit(3);
203 	}
204 }
205 
206 static void
207 cuusage(void)
208 {
209 	fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
210 	    "[-s speed | -speed] [phone-number]\n");
211 	exit(8);
212 }
213