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