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