xref: /freebsd/usr.bin/tip/tip/cu.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
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 "tip.h"
36 
37 static void	cuusage(void);
38 
39 /*
40  * Botch the interface to look like cu's
41  */
42 void
cumain(int argc,char * argv[])43 cumain(int argc, char *argv[])
44 {
45 	int ch, i, parity;
46 	long l;
47 	char *cp;
48 	static char sbuf[12];
49 
50 	if (argc < 2)
51 		cuusage();
52 	CU = DV = NOSTR;
53 	BR = DEFBR;
54 	parity = 0;	/* none */
55 
56 	/*
57 	 * We want to accept -# as a speed.  It's easiest to look through
58 	 * the arguments, replace -# with -s#, and let getopt() handle it.
59 	 */
60 	for (i = 1; i < argc; i++) {
61 		if (argv[i][0] == '-' &&
62 		    argv[i][1] >= '0' && argv[i][1] <= '9') {
63 			asprintf(&cp, "-s%s", argv[i] + 1);
64 			if (cp == NULL) {
65 				fprintf(stderr,
66 				    "%s: cannot convert -# to -s#\n",
67 				    __progname);
68 				exit(3);
69 			}
70 			argv[i] = cp;
71 		}
72 	}
73 
74 	while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
75 		switch (ch) {
76 		case 'a':
77 			CU = optarg;
78 			break;
79 		case 'l':
80 			if (DV != NULL) {
81 				fprintf(stderr,
82 				    "%s: cannot specificy multiple -l options\n",
83 				    __progname);
84 				exit(3);
85 			}
86 			if (strchr(optarg, '/'))
87 				DV = optarg;
88 			else
89 				asprintf(&DV, "/dev/%s", optarg);
90 			break;
91 		case 's':
92 			l = strtol(optarg, &cp, 10);
93 			if (*cp != '\0' || l < 0 || l >= INT_MAX) {
94 				fprintf(stderr, "%s: unsupported speed %s\n",
95 				    __progname, optarg);
96 				exit(3);
97 			}
98 			BR = (int)l;
99 			break;
100 		case 'h':
101 			setboolean(value(LECHO), TRUE);
102 			HD = TRUE;
103 			break;
104 		case 't':
105 			HW = 1, DU = -1;
106 			break;
107 		case 'o':
108 			if (parity != 0)
109 				parity = 0;	/* -e -o */
110 			else
111 				parity = 1;	/* odd */
112 			break;
113 		case 'e':
114 			if (parity != 0)
115 				parity = 0;	/* -o -e */
116 			else
117 				parity = -1;	/* even */
118 			break;
119 		default:
120 			cuusage();
121 			break;
122 		}
123 	}
124 	argc -= optind;
125 	argv += optind;
126 
127 	switch (argc) {
128 	case 1:
129 		PN = argv[0];
130 		break;
131 	case 0:
132 		break;
133 	default:
134 		cuusage();
135 		break;
136 	}
137 
138 	signal(SIGINT, cleanup);
139 	signal(SIGQUIT, cleanup);
140 	signal(SIGHUP, cleanup);
141 	signal(SIGTERM, cleanup);
142 	signal(SIGCHLD, SIG_DFL);
143 
144 	/*
145 	 * The "cu" host name is used to define the
146 	 * attributes of the generic dialer.
147 	 */
148 	(void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
149 	if ((i = hunt(sbuf)) == 0) {
150 		printf("all ports busy\n");
151 		exit(3);
152 	}
153 	if (i == -1) {
154 		printf("link down\n");
155 		(void)uu_unlock(uucplock);
156 		exit(3);
157 	}
158 	setbuf(stdout, NULL);
159 	loginit();
160 	user_uid();
161 	vinit();
162 	switch (parity) {
163 	case -1:
164 		setparity("even");
165 		break;
166 	case 1:
167 		setparity("odd");
168 		break;
169 	default:
170 		setparity("none");
171 		break;
172 	}
173 	setboolean(value(VERBOSE), FALSE);
174 	if (HW && ttysetup(BR)) {
175 		fprintf(stderr, "%s: unsupported speed %ld\n",
176 		    __progname, BR);
177 		daemon_uid();
178 		(void)uu_unlock(uucplock);
179 		exit(3);
180 	}
181 	if (con()) {
182 		printf("Connect failed\n");
183 		daemon_uid();
184 		(void)uu_unlock(uucplock);
185 		exit(1);
186 	}
187 	if (!HW && ttysetup(BR)) {
188 		fprintf(stderr, "%s: unsupported speed %ld\n",
189 		    __progname, BR);
190 		daemon_uid();
191 		(void)uu_unlock(uucplock);
192 		exit(3);
193 	}
194 }
195 
196 static void
cuusage(void)197 cuusage(void)
198 {
199 	fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
200 	    "[-s speed | -speed] [phone-number]\n");
201 	exit(8);
202 }
203