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