cu.c revision 1.18 1 1.18 yamt /* $NetBSD: cu.c,v 1.18 2006/04/05 23:30:57 yamt 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.10 tls
35 1.1 cgd #ifndef lint
36 1.3 jtc #if 0
37 1.3 jtc static char sccsid[] = "@(#)cu.c 8.1 (Berkeley) 6/6/93";
38 1.3 jtc #endif
39 1.18 yamt __RCSID("$NetBSD: cu.c,v 1.18 2006/04/05 23:30:57 yamt Exp $");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #include "tip.h"
43 1.1 cgd
44 1.10 tls static void cuhelp(void);
45 1.10 tls static void cuusage(void);
46 1.10 tls
47 1.1 cgd /*
48 1.1 cgd * Botch the interface to look like cu's
49 1.1 cgd */
50 1.10 tls void
51 1.13 perry cumain(int argc, char *argv[])
52 1.1 cgd {
53 1.17 christos int c, i, phonearg = 0;
54 1.12 tls int parity = 0; /* 0 is no parity */
55 1.12 tls int flow = -1; /* -1 is "tandem" ^S/^Q */
56 1.12 tls static int helpme = 0, nostop = 0;
57 1.10 tls char useresc = '~';
58 1.10 tls static char sbuf[12], brbuf[16];
59 1.10 tls extern char *optarg;
60 1.10 tls extern int optind;
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.1 cgd
84 1.17 christos while((c = getopt_long(argc, argv,
85 1.12 tls "E:F:P:a:p:c:l:s:heot0123456789", longopts, NULL)) != -1) {
86 1.10 tls
87 1.10 tls if (helpme == 1) cuhelp();
88 1.10 tls
89 1.17 christos switch(c) {
90 1.1 cgd
91 1.10 tls case 'E':
92 1.10 tls if(strlen(optarg) > 1)
93 1.10 tls errx(3, "only one escape character allowed");
94 1.10 tls useresc = optarg[0];
95 1.10 tls break;
96 1.12 tls case 'F':
97 1.12 tls if (strncmp(optarg, "hard", sizeof("hard") - 1 ) == 0)
98 1.12 tls flow = 1;
99 1.12 tls else
100 1.12 tls if (strncmp(optarg, "soft",
101 1.12 tls sizeof("soft") - 1 ) == 0)
102 1.12 tls flow = -1;
103 1.12 tls else
104 1.12 tls if(strcmp(optarg, "none") != 0)
105 1.12 tls errx(3, "bad flow setting");
106 1.12 tls else
107 1.12 tls flow = 0;
108 1.12 tls break;
109 1.10 tls case 'P':
110 1.10 tls if(strcmp(optarg, "even") == 0)
111 1.10 tls parity = -1;
112 1.10 tls else
113 1.10 tls if(strcmp(optarg, "odd") == 0)
114 1.10 tls parity = 1;
115 1.10 tls else
116 1.10 tls if(strcmp(optarg, "none") != 0)
117 1.10 tls errx(3, "bad parity setting");
118 1.12 tls else
119 1.12 tls parity = 0;
120 1.10 tls break;
121 1.1 cgd case 'a':
122 1.10 tls case 'p':
123 1.10 tls CU = optarg;
124 1.10 tls break;
125 1.10 tls case 'c':
126 1.10 tls phonearg = 1;
127 1.10 tls PN = optarg;
128 1.10 tls break;
129 1.10 tls case 'l':
130 1.10 tls if (DV != NULL)
131 1.10 tls errx(3,"more than one line specified");
132 1.10 tls if(strchr(optarg, '/'))
133 1.10 tls DV=optarg;
134 1.10 tls else
135 1.10 tls asprintf(&DV, "/dev/%s", optarg);
136 1.1 cgd break;
137 1.1 cgd case 's':
138 1.10 tls BR = atoi(optarg);
139 1.10 tls break;
140 1.10 tls case 'h':
141 1.10 tls HD = TRUE;
142 1.1 cgd break;
143 1.10 tls case 'e':
144 1.10 tls if (parity != 0)
145 1.10 tls errx(3, "more than one parity specified");
146 1.10 tls parity = -1; /* even */
147 1.10 tls break;
148 1.10 tls case 'o':
149 1.10 tls if (parity != 0)
150 1.10 tls errx(3, "more than one parity specified");
151 1.10 tls parity = 1; /* odd */
152 1.10 tls break;
153 1.10 tls case 't':
154 1.14 tls HW = 1, DU = -1, DC = 1;
155 1.1 cgd break;
156 1.1 cgd case '0': case '1': case '2': case '3': case '4':
157 1.1 cgd case '5': case '6': case '7': case '8': case '9':
158 1.10 tls snprintf(brbuf, sizeof(brbuf) -1, "%s%c",
159 1.17 christos brbuf, c);
160 1.10 tls BR = atoi(brbuf);
161 1.1 cgd break;
162 1.1 cgd default:
163 1.12 tls if (nostop == 0)
164 1.12 tls cuusage();
165 1.1 cgd break;
166 1.1 cgd }
167 1.1 cgd }
168 1.10 tls
169 1.10 tls argc -= optind;
170 1.10 tls argv += optind;
171 1.10 tls
172 1.10 tls switch (argc) {
173 1.10 tls case 1:
174 1.10 tls if (phonearg)
175 1.10 tls errx(3, "more than one phone number specified");
176 1.10 tls else
177 1.10 tls PN = argv[0];
178 1.10 tls break;
179 1.10 tls case 0:
180 1.14 tls /*
181 1.14 tls * No system or number to call. We're "direct", so use
182 1.14 tls * the tty as local.
183 1.14 tls */
184 1.14 tls HW = 1; DU = -1; DC = 1;
185 1.10 tls break;
186 1.10 tls default:
187 1.10 tls cuusage();
188 1.10 tls break;
189 1.10 tls }
190 1.13 perry
191 1.1 cgd signal(SIGINT, cleanup);
192 1.1 cgd signal(SIGQUIT, cleanup);
193 1.1 cgd signal(SIGHUP, cleanup);
194 1.1 cgd signal(SIGTERM, cleanup);
195 1.10 tls /* signal(SIGCHLD, SIG_DFL) */ /* XXX seems wrong */
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * The "cu" host name is used to define the
199 1.1 cgd * attributes of the generic dialer.
200 1.1 cgd */
201 1.6 lukem (void)snprintf(sbuf, sizeof sbuf, "cu%d", (int)BR);
202 1.1 cgd if ((i = hunt(sbuf)) == 0) {
203 1.10 tls errx(3,"all ports busy");
204 1.1 cgd }
205 1.1 cgd if (i == -1) {
206 1.15 perry errx(3, "link down");
207 1.1 cgd }
208 1.1 cgd setbuf(stdout, NULL);
209 1.1 cgd vinit();
210 1.10 tls switch (parity) {
211 1.10 tls case -1:
212 1.10 tls setparity("even");
213 1.10 tls break;
214 1.10 tls case 1:
215 1.10 tls setparity("odd");
216 1.10 tls break;
217 1.10 tls case 0:
218 1.10 tls setparity("none");
219 1.10 tls break;
220 1.10 tls default:
221 1.10 tls setparity("none");
222 1.10 tls break;
223 1.10 tls }
224 1.12 tls
225 1.12 tls switch (flow) {
226 1.12 tls case -1:
227 1.12 tls if(nostop) {
228 1.12 tls setboolean(value(TAND), FALSE);
229 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
230 1.12 tls }
231 1.12 tls else {
232 1.12 tls setboolean(value(TAND), TRUE);
233 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
234 1.12 tls }
235 1.12 tls break;
236 1.12 tls case 1:
237 1.12 tls setboolean(value(TAND), FALSE);
238 1.12 tls setboolean(value(HARDWAREFLOW), TRUE);
239 1.12 tls break;
240 1.12 tls case 0:
241 1.12 tls default:
242 1.12 tls setboolean(value(TAND), FALSE);
243 1.12 tls setboolean(value(HARDWAREFLOW), FALSE);
244 1.12 tls break;
245 1.12 tls }
246 1.10 tls setcharacter(value(ESCAPE), useresc);
247 1.10 tls setboolean(value(VERBOSE), FALSE);
248 1.12 tls if (HD)
249 1.12 tls setboolean(value(LECHO), TRUE);
250 1.10 tls if (HW) {
251 1.17 christos if (ttysetup((speed_t)BR) != 0) {
252 1.15 perry errx(3, "unsupported speed %ld", BR);
253 1.10 tls }
254 1.10 tls }
255 1.1 cgd if (connect()) {
256 1.15 perry errx(1, "Connect failed");
257 1.1 cgd }
258 1.10 tls if (!HW) {
259 1.17 christos if (ttysetup((speed_t)BR) != 0) {
260 1.15 perry errx(3, "unsupported speed %ld", BR);
261 1.10 tls }
262 1.10 tls }
263 1.10 tls }
264 1.10 tls
265 1.10 tls static void
266 1.10 tls cuusage(void)
267 1.10 tls {
268 1.10 tls fprintf(stderr, "Usage: cu [options] [phone-number]\n"
269 1.10 tls "Use cu --help for help\n");
270 1.10 tls exit(8);
271 1.10 tls }
272 1.10 tls
273 1.10 tls static void
274 1.10 tls cuhelp(void)
275 1.10 tls {
276 1.10 tls fprintf(stderr,
277 1.10 tls "BSD tip/cu\n"
278 1.10 tls "Usage: cu [options] [phone-number]\n"
279 1.10 tls " -E,--escape char: Use this escape character\n"
280 1.12 tls " -F,--flow {hard,soft,none}: Use RTS/CTS, ^S/^Q, no flow control\n"
281 1.12 tls " --nostop: Do not use software flow control\n"
282 1.10 tls " -a, -p,--port port: Use this port as ACU/Dialer\n"
283 1.10 tls " -c,--phone number: Call this number\n"
284 1.12 tls " -h,--halfduplex: Echo characters locally (use \"half duplex\")\n"
285 1.10 tls " -e: Use even parity\n"
286 1.10 tls " -o: Use odd parity\n"
287 1.10 tls " -P,--parity {even,odd,none}: use even, odd, no parity\n"
288 1.18 yamt " -l,--line line: Use this device (ttyXX)\n"
289 1.10 tls " -s,--speed,--baud speed,-#: Use this speed\n"
290 1.10 tls " -t: Connect via hard-wired connection\n");
291 1.6 lukem exit(0);
292 1.1 cgd }
293