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