Annotation of src/usr.bin/tip/cu.c, Revision 1.25
1.25 ! nicm 1: /* $OpenBSD: cu.c,v 1.24 2009/10/27 23:59:44 deraadt Exp $ */
1.3 millert 2: /* $NetBSD: cu.c,v 1.5 1997/02/11 09:24:05 mrg Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.14 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
1.21 ray 33: #include <err.h>
34: #include <paths.h>
35:
1.1 deraadt 36: #include "tip.h"
37:
1.18 moritz 38: static void cuusage(void);
1.1 deraadt 39:
40: /*
41: * Botch the interface to look like cu's
42: */
1.4 deraadt 43: void
1.17 deraadt 44: cumain(int argc, char *argv[])
1.1 deraadt 45: {
1.11 miod 46: int ch, i, parity;
1.20 millert 47: const char *errstr;
1.1 deraadt 48: static char sbuf[12];
49:
1.6 millert 50: if (argc < 2)
51: cuusage();
1.22 moritz 52: CU = DV = NULL;
1.1 deraadt 53: BR = DEFBR;
1.11 miod 54: parity = 0; /* none */
1.20 millert 55:
56: /*
57: * Convert obsolecent -### speed to modern -s### syntax which
58: * getopt() can handle.
59: */
60: for (i = 1; i < argc; i++) {
61: if (argv[i][0] == '-') {
62: switch (argv[i][1]) {
63: case '0': case '1': case '2': case '3': case '4':
64: case '5': case '6': case '7': case '8': case '9':
65: ch = snprintf(sbuf, sizeof(sbuf), "-s%s",
66: &argv[i][1]);
67: if (ch <= 0 || ch >= sizeof(sbuf)) {
68: errx(3, "invalid speed: %s",
69: &argv[i][1]);
70: }
71: argv[i] = sbuf;
72: break;
73: case '-':
74: /* if we get "--" stop processing args */
75: if (argv[i][2] == '\0')
76: goto getopt;
77: break;
78: }
79: }
80: }
81:
82: getopt:
83: while ((ch = getopt(argc, argv, "a:l:s:htoe")) != -1) {
1.17 deraadt 84: switch (ch) {
1.1 deraadt 85: case 'a':
1.21 ray 86: if (optarg[0] == '\0')
87: errx(3, "invalid acu: \"\"");
1.6 millert 88: CU = optarg;
89: break;
90: case 'l':
1.8 deraadt 91: if (DV != NULL) {
92: fprintf(stderr,
1.23 martynas 93: "%s: cannot specify multiple -l options\n",
1.8 deraadt 94: __progname);
95: exit(3);
96: }
97: if (strchr(optarg, '/'))
98: DV = optarg;
99: else
1.21 ray 100: if (asprintf(&DV, "%s%s", _PATH_DEV, optarg) == -1)
101: err(3, "asprintf");
1.1 deraadt 102: break;
103: case 's':
1.20 millert 104: BR = (int)strtonum(optarg, 0, INT_MAX, &errstr);
105: if (errstr)
106: errx(3, "speed is %s: %s", errstr, optarg);
1.6 millert 107: break;
108: case 'h':
1.9 millert 109: setboolean(value(LECHO), TRUE);
1.6 millert 110: HD = TRUE;
111: break;
112: case 't':
113: HW = 1, DU = -1;
1.1 deraadt 114: break;
1.6 millert 115: case 'o':
1.11 miod 116: if (parity != 0)
117: parity = 0; /* -e -o */
118: else
119: parity = 1; /* odd */
1.6 millert 120: break;
121: case 'e':
1.11 miod 122: if (parity != 0)
123: parity = 0; /* -o -e */
124: else
125: parity = -1; /* even */
1.1 deraadt 126: break;
127: default:
1.10 pvalchev 128: cuusage();
1.1 deraadt 129: break;
130: }
131: }
1.6 millert 132: argc -= optind;
133: argv += optind;
134:
135: switch (argc) {
136: case 1:
137: PN = argv[0];
138: break;
139: case 0:
140: break;
141: default:
142: cuusage();
143: break;
144: }
145:
1.1 deraadt 146: signal(SIGINT, cleanup);
147: signal(SIGQUIT, cleanup);
148: signal(SIGHUP, cleanup);
149: signal(SIGTERM, cleanup);
1.16 otto 150: signal(SIGCHLD, SIG_DFL);
1.1 deraadt 151:
152: /*
153: * The "cu" host name is used to define the
154: * attributes of the generic dialer.
155: */
1.4 deraadt 156: (void)snprintf(sbuf, sizeof(sbuf), "cu%ld", BR);
1.1 deraadt 157: if ((i = hunt(sbuf)) == 0) {
158: printf("all ports busy\n");
159: exit(3);
160: }
161: if (i == -1) {
162: printf("link down\n");
163: (void)uu_unlock(uucplock);
164: exit(3);
165: }
166: setbuf(stdout, NULL);
167: loginit();
168: vinit();
1.11 miod 169: switch (parity) {
170: case -1:
171: setparity("even");
172: break;
173: case 1:
174: setparity("odd");
175: break;
176: default:
177: setparity("none");
178: break;
179: }
1.9 millert 180: setboolean(value(VERBOSE), FALSE);
1.13 millert 181: if (HW && ttysetup(BR)) {
182: fprintf(stderr, "%s: unsupported speed %ld\n",
183: __progname, BR);
184: (void)uu_unlock(uucplock);
185: exit(3);
186: }
1.17 deraadt 187: if (con()) {
1.1 deraadt 188: printf("Connect failed\n");
189: (void)uu_unlock(uucplock);
190: exit(1);
191: }
1.13 millert 192: if (!HW && ttysetup(BR)) {
193: fprintf(stderr, "%s: unsupported speed %ld\n",
194: __progname, BR);
195: (void)uu_unlock(uucplock);
196: exit(3);
197: }
1.6 millert 198: }
199:
1.18 moritz 200: static void
1.17 deraadt 201: cuusage(void)
1.6 millert 202: {
1.19 jmc 203: fprintf(stderr, "usage: cu [-ehot] [-a acu] [-l line] "
204: "[-s speed | -speed] [phone-number]\n");
1.6 millert 205: exit(8);
1.1 deraadt 206: }