cu.c revision 1.25 1 1.25 rillig /* $NetBSD: cu.c,v 1.25 2024/11/03 10:43:27 rillig Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1983, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.7 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 lukem #include <sys/cdefs.h>
33 1.10 tls #include <getopt.h>
34 1.24 christos #include <inttypes.h>
35 1.10 tls
36 1.1 cgd #ifndef lint
37 1.3 jtc #if 0
38 1.3 jtc static char sccsid[] = "@(#)cu.c 8.1 (Berkeley) 6/6/93";
39 1.3 jtc #endif
40 1.25 rillig __RCSID("$NetBSD: cu.c,v 1.25 2024/11/03 10:43:27 rillig Exp $");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #include "tip.h"
44 1.1 cgd
45 1.21 joerg __dead static void cuhelp(void);
46 1.21 joerg __dead static void cuusage(void);
47 1.10 tls
48 1.1 cgd /*
49 1.1 cgd * Botch the interface to look like cu's
50 1.1 cgd */
51 1.10 tls void
52 1.13 perry cumain(int argc, char *argv[])
53 1.1 cgd {
54 1.24 christos int c, i, phonearg = 0, e;
55 1.12 tls int parity = 0; /* 0 is no parity */
56 1.12 tls int flow = -1; /* -1 is "tandem" ^S/^Q */
57 1.12 tls static int helpme = 0, nostop = 0;
58 1.23 christos int useresc = '~';
59 1.22 dholland static char sbuf[12];
60 1.22 dholland int cmdlineBR;
61 1.10 tls
62 1.10 tls static struct option longopts[] = {
63 1.10 tls { "help", no_argument, &helpme, 1 },
64 1.10 tls { "escape", required_argument, NULL, 'E' },
65 1.12 tls { "flow", required_argument, NULL, 'F' },
66 1.10 tls { "parity", required_argument, NULL, 'P' },
67 1.10 tls { "phone", required_argument, NULL, 'c' },
68 1.10 tls { "port", required_argument, NULL, 'a' },
69 1.10 tls { "line", required_argument, NULL, 'l' },
70 1.10 tls { "speed", required_argument, NULL, 's' },
71 1.12 tls { "halfduplex", no_argument, NULL, 'h' },
72 1.12 tls { "nostop", no_argument, &nostop, 1 },
73 1.10 tls { NULL, 0, NULL, 0 }
74 1.10 tls };
75 1.13 perry
76 1.10 tls
77 1.10 tls if (argc < 2)
78 1.10 tls cuusage();
79 1.1 cgd
80 1.10 tls CU = NULL;
81 1.10 tls DV = NULL;
82 1.1 cgd BR = DEFBR;
83 1.22 dholland cmdlineBR = 0;
84 1.1 cgd
85 1.17 christos while((c = getopt_long(argc, argv,
86 1.23 christos "E:F:P:a:p:c:l:ns:hefot0123456789", longopts, NULL)) != -1) {
87 1.10 tls
88 1.10 tls if (helpme == 1) cuhelp();
89 1.10 tls
90 1.17 christos switch(c) {
91 1.1 cgd
92 1.10 tls case 'E':
93 1.10 tls if(strlen(optarg) > 1)
94 1.10 tls errx(3, "only one escape character allowed");
95 1.10 tls useresc = optarg[0];
96 1.10 tls break;
97 1.12 tls case 'F':
98 1.12 tls if (strncmp(optarg, "hard", sizeof("hard") - 1 ) == 0)
99 1.12 tls flow = 1;
100 1.12 tls else
101 1.12 tls if (strncmp(optarg, "soft",
102 1.12 tls sizeof("soft") - 1 ) == 0)
103 1.12 tls flow = -1;
104 1.12 tls else
105 1.12 tls if(strcmp(optarg, "none") != 0)
106 1.12 tls errx(3, "bad flow setting");
107 1.12 tls else
108 1.12 tls flow = 0;
109 1.12 tls break;
110 1.10 tls case 'P':
111 1.10 tls if(strcmp(optarg, "even") == 0)
112 1.10 tls parity = -1;
113 1.10 tls else
114 1.10 tls if(strcmp(optarg, "odd") == 0)
115 1.10 tls parity = 1;
116 1.10 tls else
117 1.10 tls if(strcmp(optarg, "none") != 0)
118 1.10 tls errx(3, "bad parity setting");
119 1.12 tls else
120 1.12 tls parity = 0;
121 1.10 tls break;
122 1.1 cgd case 'a':
123 1.10 tls case 'p':
124 1.10 tls CU = optarg;
125 1.10 tls break;
126 1.10 tls case 'c':
127 1.10 tls phonearg = 1;
128 1.10 tls PN = optarg;
129 1.10 tls break;
130 1.10 tls case 'l':
131 1.10 tls if (DV != NULL)
132 1.10 tls errx(3,"more than one line specified");
133 1.10 tls if(strchr(optarg, '/'))
134 1.20 christos DV = optarg;
135 1.10 tls else
136 1.20 christos (void)asprintf(&DV, "/dev/%s", optarg);
137 1.1 cgd break;
138 1.23 christos case 'n':
139 1.23 christos useresc = -1;
140 1.23 christos break;
141 1.1 cgd case 's':
142 1.24 christos BR = (long)strtoi(optarg, NULL, 0, 1, LONG_MAX, &e);
143 1.24 christos if (e)
144 1.24 christos warnc(e, "Conversion of `%s' to a baud rate "
145 1.24 christos "failed, using %ld", optarg, BR);
146 1.10 tls break;
147 1.10 tls case 'h':
148 1.10 tls HD = TRUE;
149 1.1 cgd break;
150 1.10 tls case 'e':
151 1.10 tls if (parity != 0)
152 1.10 tls errx(3, "more than one parity specified");
153 1.10 tls parity = -1; /* even */
154 1.10 tls break;
155 1.19 jdc /* Compatibility with Taylor cu */
156 1.19 jdc case 'f':
157 1.19 jdc flow = 0;
158 1.19 jdc break;
159 1.10 tls case 'o':
160 1.10 tls if (parity != 0)
161 1.10 tls errx(3, "more than one parity specified");
162 1.10 tls parity = 1; /* odd */
163 1.10 tls break;
164 1.10 tls case 't':
165 1.14 tls HW = 1, DU = -1, DC = 1;
166 1.1 cgd break;
167 1.1 cgd case '0': case '1': case '2': case '3': case '4':
168 1.1 cgd case '5': case '6': case '7': case '8': case '9':
169 1.22 dholland cmdlineBR = cmdlineBR * 10 + (c - '0');
170 1.22 dholland BR = cmdlineBR;
171 1.1 cgd break;
172 1.1 cgd default:
173 1.12 tls if (nostop == 0)
174 1.12 tls cuusage();
175 1.1 cgd break;
176 1.1 cgd }
177 1.1 cgd }
178 1.10 tls
179 1.10 tls argc -= optind;
180 1.10 tls argv += optind;
181 1.10 tls
182 1.10 tls switch (argc) {
183 1.10 tls case 1:
184 1.10 tls if (phonearg)
185 1.10 tls errx(3, "more than one phone number specified");
186 1.10 tls else
187 1.19 jdc /* Compatibility with Taylor cu */
188 1.19 jdc if(!strcmp(argv[0], "dir")) {
189 1.19 jdc HW = 1; DU = -1; DC = 1;
190 1.19 jdc } else
191 1.19 jdc PN = argv[0];
192 1.10 tls break;
193 1.10 tls case 0:
194 1.14 tls /*
195 1.14 tls * No system or number to call. We're "direct", so use
196 1.14 tls * the tty as local.
197 1.14 tls */
198 1.14 tls HW = 1; DU = -1; DC = 1;
199 1.10 tls break;
200 1.10 tls default:
201 1.10 tls cuusage();
202 1.10 tls break;
203 1.10 tls }
204 1.13 perry
205 1.20 christos (void)signal(SIGINT, cleanup);
206 1.20 christos (void)signal(SIGQUIT, cleanup);
207 1.20 christos (void)signal(SIGHUP, cleanup);
208 1.20 christos (void)signal(SIGTERM, cleanup);
209 1.20 christos /* (void)signal(SIGCHLD, SIG_DFL) */ /* XXX seems wrong */
210 1.1 cgd
211 1.1 cgd /*
212 1.1 cgd * The "cu" host name is used to define the
213 1.1 cgd * attributes of the generic dialer.
214 1.1 cgd */
215 1.6 lukem (void)snprintf(sbuf, sizeof sbuf, "cu%d", (int)BR);
216 1.1 cgd if ((i = hunt(sbuf)) == 0) {
217 1.10 tls errx(3,"all ports busy");
218 1.1 cgd }
219 1.1 cgd if (i == -1) {
220 1.15 perry errx(3, "link down");
221 1.1 cgd }
222 1.1 cgd setbuf(stdout, NULL);
223 1.1 cgd vinit();
224 1.10 tls switch (parity) {
225 1.10 tls case -1:
226 1.10 tls setparity("even");
227 1.10 tls break;
228 1.10 tls case 1:
229 1.10 tls setparity("odd");
230 1.10 tls break;
231 1.10 tls case 0:
232 1.10 tls setparity("none");
233 1.10 tls break;
234 1.10 tls default:
235 1.10 tls setparity("none");
236 1.10 tls break;
237 1.10 tls }
238 1.12 tls
239 1.12 tls switch (flow) {
240 1.12 tls case -1:
241 1.12 tls if(nostop) {
242 1.12 tls setboolean(value(TAND), FALSE);
243 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
244 1.12 tls }
245 1.12 tls else {
246 1.12 tls setboolean(value(TAND), TRUE);
247 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
248 1.12 tls }
249 1.12 tls break;
250 1.12 tls case 1:
251 1.12 tls setboolean(value(TAND), FALSE);
252 1.12 tls setboolean(value(HARDWAREFLOW), TRUE);
253 1.12 tls break;
254 1.12 tls case 0:
255 1.12 tls default:
256 1.12 tls setboolean(value(TAND), FALSE);
257 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
258 1.12 tls break;
259 1.12 tls }
260 1.10 tls setcharacter(value(ESCAPE), useresc);
261 1.10 tls setboolean(value(VERBOSE), FALSE);
262 1.12 tls if (HD)
263 1.12 tls setboolean(value(LECHO), TRUE);
264 1.10 tls if (HW) {
265 1.17 christos if (ttysetup((speed_t)BR) != 0) {
266 1.15 perry errx(3, "unsupported speed %ld", BR);
267 1.10 tls }
268 1.10 tls }
269 1.20 christos if (tip_connect()) {
270 1.15 perry errx(1, "Connect failed");
271 1.1 cgd }
272 1.10 tls if (!HW) {
273 1.17 christos if (ttysetup((speed_t)BR) != 0) {
274 1.15 perry errx(3, "unsupported speed %ld", BR);
275 1.10 tls }
276 1.10 tls }
277 1.10 tls }
278 1.10 tls
279 1.10 tls static void
280 1.10 tls cuusage(void)
281 1.10 tls {
282 1.20 christos (void)fprintf(stderr, "Usage: cu [options] [phone-number|\"dir\"]\n"
283 1.10 tls "Use cu --help for help\n");
284 1.10 tls exit(8);
285 1.10 tls }
286 1.10 tls
287 1.10 tls static void
288 1.10 tls cuhelp(void)
289 1.10 tls {
290 1.20 christos (void)fprintf(stderr,
291 1.10 tls "BSD tip/cu\n"
292 1.19 jdc "Usage: cu [options] [phone-number|\"dir\"]\n"
293 1.10 tls " -E,--escape char: Use this escape character\n"
294 1.12 tls " -F,--flow {hard,soft,none}: Use RTS/CTS, ^S/^Q, no flow control\n"
295 1.19 jdc " -f: Use no flow control\n"
296 1.12 tls " --nostop: Do not use software flow control\n"
297 1.10 tls " -a, -p,--port port: Use this port as ACU/Dialer\n"
298 1.10 tls " -c,--phone number: Call this number\n"
299 1.12 tls " -h,--halfduplex: Echo characters locally (use \"half duplex\")\n"
300 1.10 tls " -e: Use even parity\n"
301 1.10 tls " -o: Use odd parity\n"
302 1.10 tls " -P,--parity {even,odd,none}: use even, odd, no parity\n"
303 1.18 yamt " -l,--line line: Use this device (ttyXX)\n"
304 1.23 christos " -n: Disable escape character processing\n"
305 1.10 tls " -s,--speed,--baud speed,-#: Use this speed\n"
306 1.10 tls " -t: Connect via hard-wired connection\n");
307 1.6 lukem exit(0);
308 1.1 cgd }
309