2019-08-18 17:16:02 +03:00
|
|
|
/* $NetBSD: cu.c,v 1.24 2019/08/18 14:16:02 christos Exp $ */
|
1994-12-08 12:30:36 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-12-08 12:30:36 +03:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-11-22 10:28:39 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-04-02 23:04:24 +04:00
|
|
|
#include <getopt.h>
|
2019-08-18 17:16:02 +03:00
|
|
|
#include <inttypes.h>
|
2006-04-02 23:04:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1994-12-08 12:30:36 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)cu.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#endif
|
2019-08-18 17:16:02 +03:00
|
|
|
__RCSID("$NetBSD: cu.c,v 1.24 2019/08/18 14:16:02 christos Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "tip.h"
|
|
|
|
|
2011-09-06 22:33:01 +04:00
|
|
|
__dead static void cuhelp(void);
|
|
|
|
__dead static void cuusage(void);
|
2006-04-02 23:04:24 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Botch the interface to look like cu's
|
|
|
|
*/
|
2006-04-02 23:04:24 +04:00
|
|
|
void
|
2006-04-03 04:51:13 +04:00
|
|
|
cumain(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2019-08-18 17:16:02 +03:00
|
|
|
int c, i, phonearg = 0, e;
|
2006-04-03 02:19:26 +04:00
|
|
|
int parity = 0; /* 0 is no parity */
|
|
|
|
int flow = -1; /* -1 is "tandem" ^S/^Q */
|
|
|
|
static int helpme = 0, nostop = 0;
|
2016-01-03 18:38:29 +03:00
|
|
|
int useresc = '~';
|
2014-07-27 08:32:23 +04:00
|
|
|
static char sbuf[12];
|
|
|
|
int cmdlineBR;
|
2006-04-02 23:04:24 +04:00
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-04-02 23:04:24 +04:00
|
|
|
static struct option longopts[] = {
|
|
|
|
{ "help", no_argument, &helpme, 1 },
|
|
|
|
{ "escape", required_argument, NULL, 'E' },
|
2006-04-03 02:19:26 +04:00
|
|
|
{ "flow", required_argument, NULL, 'F' },
|
2006-04-02 23:04:24 +04:00
|
|
|
{ "parity", required_argument, NULL, 'P' },
|
|
|
|
{ "phone", required_argument, NULL, 'c' },
|
|
|
|
{ "port", required_argument, NULL, 'a' },
|
|
|
|
{ "line", required_argument, NULL, 'l' },
|
|
|
|
{ "speed", required_argument, NULL, 's' },
|
2006-04-03 02:19:26 +04:00
|
|
|
{ "halfduplex", no_argument, NULL, 'h' },
|
|
|
|
{ "nostop", no_argument, &nostop, 1 },
|
2006-04-02 23:04:24 +04:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2006-04-03 04:51:13 +04:00
|
|
|
|
2006-04-02 23:04:24 +04:00
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
cuusage();
|
|
|
|
|
|
|
|
CU = NULL;
|
|
|
|
DV = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
BR = DEFBR;
|
2014-07-27 08:32:23 +04:00
|
|
|
cmdlineBR = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-04-03 08:53:58 +04:00
|
|
|
while((c = getopt_long(argc, argv,
|
2016-01-03 18:38:29 +03:00
|
|
|
"E:F:P:a:p:c:l:ns:hefot0123456789", longopts, NULL)) != -1) {
|
2006-04-02 23:04:24 +04:00
|
|
|
|
|
|
|
if (helpme == 1) cuhelp();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-04-03 08:53:58 +04:00
|
|
|
switch(c) {
|
2006-04-02 23:04:24 +04:00
|
|
|
|
|
|
|
case 'E':
|
|
|
|
if(strlen(optarg) > 1)
|
|
|
|
errx(3, "only one escape character allowed");
|
|
|
|
useresc = optarg[0];
|
|
|
|
break;
|
2006-04-03 02:19:26 +04:00
|
|
|
case 'F':
|
|
|
|
if (strncmp(optarg, "hard", sizeof("hard") - 1 ) == 0)
|
|
|
|
flow = 1;
|
|
|
|
else
|
|
|
|
if (strncmp(optarg, "soft",
|
|
|
|
sizeof("soft") - 1 ) == 0)
|
|
|
|
flow = -1;
|
|
|
|
else
|
|
|
|
if(strcmp(optarg, "none") != 0)
|
|
|
|
errx(3, "bad flow setting");
|
|
|
|
else
|
|
|
|
flow = 0;
|
|
|
|
break;
|
2006-04-02 23:04:24 +04:00
|
|
|
case 'P':
|
|
|
|
if(strcmp(optarg, "even") == 0)
|
|
|
|
parity = -1;
|
|
|
|
else
|
|
|
|
if(strcmp(optarg, "odd") == 0)
|
|
|
|
parity = 1;
|
|
|
|
else
|
|
|
|
if(strcmp(optarg, "none") != 0)
|
|
|
|
errx(3, "bad parity setting");
|
2006-04-03 02:19:26 +04:00
|
|
|
else
|
|
|
|
parity = 0;
|
2006-04-02 23:04:24 +04:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'a':
|
2006-04-02 23:04:24 +04:00
|
|
|
case 'p':
|
|
|
|
CU = optarg;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2006-04-02 23:04:24 +04:00
|
|
|
case 'c':
|
|
|
|
phonearg = 1;
|
|
|
|
PN = optarg;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 'l':
|
2006-04-02 23:04:24 +04:00
|
|
|
if (DV != NULL)
|
|
|
|
errx(3,"more than one line specified");
|
|
|
|
if(strchr(optarg, '/'))
|
2006-12-14 20:09:43 +03:00
|
|
|
DV = optarg;
|
2006-04-02 23:04:24 +04:00
|
|
|
else
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)asprintf(&DV, "/dev/%s", optarg);
|
2006-04-02 23:04:24 +04:00
|
|
|
break;
|
2016-01-03 18:38:29 +03:00
|
|
|
case 'n':
|
|
|
|
useresc = -1;
|
|
|
|
break;
|
2006-04-02 23:04:24 +04:00
|
|
|
case 's':
|
2019-08-18 17:16:02 +03:00
|
|
|
BR = (long)strtoi(optarg, NULL, 0, 1, LONG_MAX, &e);
|
|
|
|
if (e)
|
|
|
|
warnc(e, "Conversion of `%s' to a baud rate "
|
|
|
|
"failed, using %ld", optarg, BR);
|
2006-04-02 23:04:24 +04:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
HD = TRUE;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (parity != 0)
|
|
|
|
errx(3, "more than one parity specified");
|
|
|
|
parity = -1; /* even */
|
|
|
|
break;
|
2006-11-29 17:44:45 +03:00
|
|
|
/* Compatibility with Taylor cu */
|
|
|
|
case 'f':
|
|
|
|
flow = 0;
|
|
|
|
break;
|
2006-04-02 23:04:24 +04:00
|
|
|
case 'o':
|
|
|
|
if (parity != 0)
|
|
|
|
errx(3, "more than one parity specified");
|
|
|
|
parity = 1; /* odd */
|
|
|
|
break;
|
|
|
|
case 't':
|
2006-04-03 05:48:22 +04:00
|
|
|
HW = 1, DU = -1, DC = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2014-07-27 08:32:23 +04:00
|
|
|
cmdlineBR = cmdlineBR * 10 + (c - '0');
|
|
|
|
BR = cmdlineBR;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
2006-04-03 02:19:26 +04:00
|
|
|
if (nostop == 0)
|
|
|
|
cuusage();
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-04-02 23:04:24 +04:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 1:
|
|
|
|
if (phonearg)
|
|
|
|
errx(3, "more than one phone number specified");
|
|
|
|
else
|
2006-11-29 17:44:45 +03:00
|
|
|
/* Compatibility with Taylor cu */
|
|
|
|
if(!strcmp(argv[0], "dir")) {
|
|
|
|
HW = 1; DU = -1; DC = 1;
|
|
|
|
} else
|
|
|
|
PN = argv[0];
|
2006-04-02 23:04:24 +04:00
|
|
|
break;
|
|
|
|
case 0:
|
2006-04-03 05:48:22 +04:00
|
|
|
/*
|
|
|
|
* No system or number to call. We're "direct", so use
|
|
|
|
* the tty as local.
|
|
|
|
*/
|
|
|
|
HW = 1; DU = -1; DC = 1;
|
2006-04-02 23:04:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cuusage();
|
|
|
|
break;
|
|
|
|
}
|
2006-04-03 04:51:13 +04:00
|
|
|
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)signal(SIGINT, cleanup);
|
|
|
|
(void)signal(SIGQUIT, cleanup);
|
|
|
|
(void)signal(SIGHUP, cleanup);
|
|
|
|
(void)signal(SIGTERM, cleanup);
|
|
|
|
/* (void)signal(SIGCHLD, SIG_DFL) */ /* XXX seems wrong */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The "cu" host name is used to define the
|
|
|
|
* attributes of the generic dialer.
|
|
|
|
*/
|
1997-11-22 10:28:39 +03:00
|
|
|
(void)snprintf(sbuf, sizeof sbuf, "cu%d", (int)BR);
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((i = hunt(sbuf)) == 0) {
|
2006-04-02 23:04:24 +04:00
|
|
|
errx(3,"all ports busy");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (i == -1) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "link down");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
setbuf(stdout, NULL);
|
|
|
|
vinit();
|
2006-04-02 23:04:24 +04:00
|
|
|
switch (parity) {
|
|
|
|
case -1:
|
|
|
|
setparity("even");
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
setparity("odd");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
setparity("none");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
setparity("none");
|
|
|
|
break;
|
|
|
|
}
|
2006-04-03 02:19:26 +04:00
|
|
|
|
|
|
|
switch (flow) {
|
|
|
|
case -1:
|
|
|
|
if(nostop) {
|
|
|
|
setboolean(value(TAND), FALSE);
|
|
|
|
setboolean(value(HARDWAREFLOW), FALSE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setboolean(value(TAND), TRUE);
|
|
|
|
setboolean(value(HARDWAREFLOW), FALSE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
setboolean(value(TAND), FALSE);
|
|
|
|
setboolean(value(HARDWAREFLOW), TRUE);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
setboolean(value(TAND), FALSE);
|
|
|
|
setboolean(value(HARDWAREFLOW), FALSE);
|
|
|
|
break;
|
|
|
|
}
|
2006-04-02 23:04:24 +04:00
|
|
|
setcharacter(value(ESCAPE), useresc);
|
|
|
|
setboolean(value(VERBOSE), FALSE);
|
2006-04-03 02:19:26 +04:00
|
|
|
if (HD)
|
|
|
|
setboolean(value(LECHO), TRUE);
|
2006-04-02 23:04:24 +04:00
|
|
|
if (HW) {
|
2006-04-03 08:53:58 +04:00
|
|
|
if (ttysetup((speed_t)BR) != 0) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "unsupported speed %ld", BR);
|
2006-04-02 23:04:24 +04:00
|
|
|
}
|
|
|
|
}
|
2006-12-14 20:09:43 +03:00
|
|
|
if (tip_connect()) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(1, "Connect failed");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2006-04-02 23:04:24 +04:00
|
|
|
if (!HW) {
|
2006-04-03 08:53:58 +04:00
|
|
|
if (ttysetup((speed_t)BR) != 0) {
|
2006-04-03 06:01:28 +04:00
|
|
|
errx(3, "unsupported speed %ld", BR);
|
2006-04-02 23:04:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cuusage(void)
|
|
|
|
{
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr, "Usage: cu [options] [phone-number|\"dir\"]\n"
|
2006-04-02 23:04:24 +04:00
|
|
|
"Use cu --help for help\n");
|
|
|
|
exit(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cuhelp(void)
|
|
|
|
{
|
2006-12-14 20:09:43 +03:00
|
|
|
(void)fprintf(stderr,
|
2006-04-02 23:04:24 +04:00
|
|
|
"BSD tip/cu\n"
|
2006-11-29 17:44:45 +03:00
|
|
|
"Usage: cu [options] [phone-number|\"dir\"]\n"
|
2006-04-02 23:04:24 +04:00
|
|
|
" -E,--escape char: Use this escape character\n"
|
2006-04-03 02:19:26 +04:00
|
|
|
" -F,--flow {hard,soft,none}: Use RTS/CTS, ^S/^Q, no flow control\n"
|
2006-11-29 17:44:45 +03:00
|
|
|
" -f: Use no flow control\n"
|
2006-04-03 02:19:26 +04:00
|
|
|
" --nostop: Do not use software flow control\n"
|
2006-04-02 23:04:24 +04:00
|
|
|
" -a, -p,--port port: Use this port as ACU/Dialer\n"
|
|
|
|
" -c,--phone number: Call this number\n"
|
2006-04-03 02:19:26 +04:00
|
|
|
" -h,--halfduplex: Echo characters locally (use \"half duplex\")\n"
|
2006-04-02 23:04:24 +04:00
|
|
|
" -e: Use even parity\n"
|
|
|
|
" -o: Use odd parity\n"
|
|
|
|
" -P,--parity {even,odd,none}: use even, odd, no parity\n"
|
2006-04-06 03:30:57 +04:00
|
|
|
" -l,--line line: Use this device (ttyXX)\n"
|
2016-01-03 18:38:29 +03:00
|
|
|
" -n: Disable escape character processing\n"
|
2006-04-02 23:04:24 +04:00
|
|
|
" -s,--speed,--baud speed,-#: Use this speed\n"
|
|
|
|
" -t: Connect via hard-wired connection\n");
|
1997-11-22 10:28:39 +03:00
|
|
|
exit(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|