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