cu.c revision 1.10 1 /* $NetBSD: cu.c,v 1.10 2006/04/02 19:04:24 tls 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.10 2006/04/02 19:04:24 tls Exp $");
40 #endif /* not lint */
41
42 #include "tip.h"
43
44 static void cuhelp(void);
45 static void cuusage(void);
46
47 /*
48 * Botch the interface to look like cu's
49 */
50 void
51 cumain(argc, argv)
52 int argc;
53 char *argv[];
54 {
55 int i, phonearg = 0, parity = 0; /* 0 is no parity */
56 static int helpme = 0;
57 char useresc = '~';
58 static char sbuf[12], brbuf[16];
59 extern char *optarg;
60 extern int optind;
61
62 static struct option longopts[] = {
63 { "help", no_argument, &helpme, 1 },
64 { "escape", required_argument, NULL, 'E' },
65 { "parity", required_argument, NULL, 'P' },
66 { "phone", required_argument, NULL, 'c' },
67 { "port", required_argument, NULL, 'a' },
68 { "line", required_argument, NULL, 'l' },
69 { "speed", required_argument, NULL, 's' },
70 { "halfduplex", required_argument, NULL, 'h' },
71 { NULL, 0, NULL, 0 }
72 };
73
74
75 if (argc < 2)
76 cuusage();
77
78 CU = NULL;
79 DV = NULL;
80 BR = DEFBR;
81
82 while((ch = getopt_long(argc, argv,
83 "E:P:a:p:c:l:s:heot0123456789", longopts, NULL)) != -1) {
84
85 if (helpme == 1) cuhelp();
86
87 switch(ch) {
88
89 case 'E':
90 if(strlen(optarg) > 1)
91 errx(3, "only one escape character allowed");
92 useresc = optarg[0];
93 break;
94 case 'P':
95 if(strcmp(optarg, "even") == 0)
96 parity = -1;
97 else
98 if(strcmp(optarg, "odd") == 0)
99 parity = 1;
100 else
101 if(strcmp(optarg, "none") != 0)
102 errx(3, "bad parity setting");
103 break;
104 case 'a':
105 case 'p':
106 CU = optarg;
107 break;
108 case 'c':
109 phonearg = 1;
110 PN = optarg;
111 break;
112 case 'l':
113 if (DV != NULL)
114 errx(3,"more than one line specified");
115 if(strchr(optarg, '/'))
116 DV=optarg;
117 else
118 asprintf(&DV, "/dev/%s", optarg);
119 break;
120 case 's':
121 BR = atoi(optarg);
122 break;
123 case 'h':
124 setboolean(value(LECHO), TRUE);
125 HD = TRUE;
126 break;
127 case 'e':
128 if (parity != 0)
129 errx(3, "more than one parity specified");
130 parity = -1; /* even */
131 break;
132 case 'o':
133 if (parity != 0)
134 errx(3, "more than one parity specified");
135 parity = 1; /* odd */
136 break;
137 case 't':
138 HW = 1, DU = -1;
139 break;
140 case '0': case '1': case '2': case '3': case '4':
141 case '5': case '6': case '7': case '8': case '9':
142 snprintf(brbuf, sizeof(brbuf) -1, "%s%c",
143 brbuf, ch);
144 BR = atoi(brbuf);
145 break;
146 default:
147 cuusage();
148 break;
149 }
150 }
151
152 argc -= optind;
153 argv += optind;
154
155 switch (argc) {
156 case 1:
157 if (phonearg)
158 errx(3, "more than one phone number specified");
159 else
160 PN = argv[0];
161 break;
162 case 0:
163 break;
164 default:
165 cuusage();
166 break;
167 }
168
169 signal(SIGINT, cleanup);
170 signal(SIGQUIT, cleanup);
171 signal(SIGHUP, cleanup);
172 signal(SIGTERM, cleanup);
173 /* signal(SIGCHLD, SIG_DFL) */ /* XXX seems wrong */
174
175 /*
176 * The "cu" host name is used to define the
177 * attributes of the generic dialer.
178 */
179 (void)snprintf(sbuf, sizeof sbuf, "cu%d", (int)BR);
180 if ((i = hunt(sbuf)) == 0) {
181 errx(3,"all ports busy");
182 }
183 if (i == -1) {
184 warnx("link down");
185 (void)uu_unlock(uucplock);
186 exit(3);
187 }
188 setbuf(stdout, NULL);
189 loginit();
190 user_uid();
191 vinit();
192 switch (parity) {
193 case -1:
194 setparity("even");
195 break;
196 case 1:
197 setparity("odd");
198 break;
199 case 0:
200 setparity("none");
201 break;
202 default:
203 setparity("none");
204 break;
205 }
206 setcharacter(value(ESCAPE), useresc);
207 setboolean(value(VERBOSE), FALSE);
208 if (HW) {
209 if (ttysetup(BR) != 0) {
210 warnx("unsupported speed %ld", BR);
211 daemon_uid();
212 (void)uu_unlock(uucplock);
213 exit(3);
214 }
215 }
216 if (connect()) {
217 warnx("Connect failed");
218 daemon_uid();
219 (void)uu_unlock(uucplock);
220 exit(1);
221 }
222 if (!HW) {
223 if (ttysetup(BR) != 0) {
224 warnx("unsupported speed %ld", BR);
225 daemon_uid();
226 (void)uu_unlock(uucplock);
227 exit(3);
228 }
229 }
230 }
231
232 static void
233 cuusage(void)
234 {
235 fprintf(stderr, "Usage: cu [options] [phone-number]\n"
236 "Use cu --help for help\n");
237 exit(8);
238 }
239
240 static void
241 cuhelp(void)
242 {
243 fprintf(stderr,
244 "BSD tip/cu\n"
245 "Usage: cu [options] [phone-number]\n"
246 " -E,--escape char: Use this escape character\n"
247 " -a, -p,--port port: Use this port as ACU/Dialer\n"
248 " -c,--phone number: Call this number\n"
249 " -h: Echo characters locally (use \"half duplex\")\n"
250 " -e: Use even parity\n"
251 " -o: Use odd parity\n"
252 " -P,--parity {even,odd,none}: use even, odd, no parity\n"
253 " -l,-line line: Use this device (ttyXX)\n"
254 " -s,--speed,--baud speed,-#: Use this speed\n"
255 " -t: Connect via hard-wired connection\n");
256 exit(0);
257 }
258