Home | History | Annotate | Line # | Download | only in tip
cu.c revision 1.12
      1 /*	$NetBSD: cu.c,v 1.12 2006/04/02 22:19:26 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #include <getopt.h>
     34 
     35 #ifndef lint
     36 #if 0
     37 static char sccsid[] = "@(#)cu.c	8.1 (Berkeley) 6/6/93";
     38 #endif
     39 __RCSID("$NetBSD: cu.c,v 1.12 2006/04/02 22:19:26 tls Exp $");
     40 #endif /* not lint */
     41 
     42 #include "tip.h"
     43 
     44 static void cuhelp(void);
     45 static void cuusage(void);
     46 
     47 /*
     48  * Botch the interface to look like cu's
     49  */
     50 void
     51 cumain(argc, argv)
     52 	int argc;
     53 	char *argv[];
     54 {
     55 	int i, phonearg = 0;
     56 	int parity = 0;		/* 0 is no parity */
     57 	int flow = -1;		/* -1 is "tandem" ^S/^Q */
     58 	static int helpme = 0, nostop = 0;
     59 	char useresc = '~';
     60 	static char sbuf[12], brbuf[16];
     61 	extern char *optarg;
     62 	extern int optind;
     63 
     64 	static struct option longopts[] = {
     65 		{ "help",	no_argument,		&helpme,	1 },
     66 		{ "escape",	required_argument,	NULL,		'E' },
     67 		{ "flow",	required_argument,	NULL,		'F' },
     68 		{ "parity",	required_argument,	NULL,		'P' },
     69 		{ "phone", 	required_argument,	NULL,		'c' },
     70 		{ "port",	required_argument,	NULL,		'a' },
     71 		{ "line",	required_argument,	NULL,		'l' },
     72 		{ "speed",	required_argument,	NULL,		's' },
     73 		{ "halfduplex",	no_argument,		NULL,		'h' },
     74 		{ "nostop",	no_argument,		&nostop,	1  },
     75 		{ NULL,		0,			NULL,		0 }
     76 	};
     77 
     78 
     79 	if (argc < 2)
     80 		cuusage();
     81 
     82 	CU = NULL;
     83 	DV = NULL;
     84 	BR = DEFBR;
     85 
     86 	while((ch = getopt_long(argc, argv,
     87 	    "E:F:P:a:p:c:l:s:heot0123456789", longopts, NULL)) != -1) {
     88 
     89 		if (helpme == 1) cuhelp();
     90 
     91 		switch(ch) {
     92 
     93 		case 'E':
     94 			if(strlen(optarg) > 1)
     95 				errx(3, "only one escape character allowed");
     96 			useresc = optarg[0];
     97 			break;
     98 		case 'F':
     99 			if (strncmp(optarg, "hard", sizeof("hard") - 1 ) == 0)
    100 				flow = 1;
    101 			else
    102 				if (strncmp(optarg, "soft",
    103 				    sizeof("soft") - 1 ) == 0)
    104 					flow = -1;
    105 				else
    106 					if(strcmp(optarg, "none") != 0)
    107 						errx(3, "bad flow setting");
    108 					else
    109 						flow = 0;
    110 			break;
    111 		case 'P':
    112 			if(strcmp(optarg, "even") == 0)
    113 				parity = -1;
    114 			else
    115 				if(strcmp(optarg, "odd") == 0)
    116 					parity = 1;
    117 				else
    118 					if(strcmp(optarg, "none") != 0)
    119 						errx(3, "bad parity setting");
    120 					else
    121 						parity = 0;
    122 			break;
    123 		case 'a':
    124 		case 'p':
    125 			CU = optarg;
    126 			break;
    127 		case 'c':
    128 			phonearg = 1;
    129 			PN = optarg;
    130 			break;
    131 		case 'l':
    132 			if (DV != NULL)
    133 				errx(3,"more than one line specified");
    134 			if(strchr(optarg, '/'))
    135 				DV=optarg;
    136 			else
    137 				asprintf(&DV, "/dev/%s", optarg);
    138 			break;
    139 		case 's':
    140 			BR = atoi(optarg);
    141 			break;
    142 		case 'h':
    143 			HD = TRUE;
    144 			break;
    145 		case 'e':
    146 			if (parity != 0)
    147 				errx(3, "more than one parity specified");
    148 			parity = -1; /* even */
    149 			break;
    150 		case 'o':
    151 			if (parity != 0)
    152 				errx(3, "more than one parity specified");
    153 			parity = 1; /* odd */
    154 			break;
    155 		case 't':
    156 			HW = 1, DU = -1;
    157 			break;
    158 		case '0': case '1': case '2': case '3': case '4':
    159 		case '5': case '6': case '7': case '8': case '9':
    160 			snprintf(brbuf, sizeof(brbuf) -1, "%s%c",
    161 				 brbuf, ch);
    162 			BR = atoi(brbuf);
    163 			break;
    164 		default:
    165 			if (nostop == 0)
    166 				cuusage();
    167 			break;
    168 		}
    169 	}
    170 
    171 	argc -= optind;
    172 	argv += optind;
    173 
    174 	switch (argc) {
    175 	case 1:
    176 		if (phonearg)
    177 			errx(3, "more than one phone number specified");
    178 		else
    179 			PN = argv[0];
    180 		break;
    181 	case 0:
    182 		break;
    183 	default:
    184 		cuusage();
    185 		break;
    186 	}
    187 
    188 	signal(SIGINT, cleanup);
    189 	signal(SIGQUIT, cleanup);
    190 	signal(SIGHUP, cleanup);
    191 	signal(SIGTERM, cleanup);
    192 	/* signal(SIGCHLD, SIG_DFL) */	/* XXX seems wrong */
    193 
    194 	/*
    195 	 * The "cu" host name is used to define the
    196 	 * attributes of the generic dialer.
    197 	 */
    198 	(void)snprintf(sbuf, sizeof sbuf, "cu%d", (int)BR);
    199 	if ((i = hunt(sbuf)) == 0) {
    200 		errx(3,"all ports busy");
    201 	}
    202 	if (i == -1) {
    203 		warnx("link down");
    204 		(void)uu_unlock(uucplock);
    205 		exit(3);
    206 	}
    207 	setbuf(stdout, NULL);
    208 #ifdef ACULOG
    209 	loginit();
    210 #endif
    211 	user_uid();
    212 	vinit();
    213 	switch (parity) {
    214 	case -1:
    215 		setparity("even");
    216 		break;
    217 	case 1:
    218 		setparity("odd");
    219 		break;
    220 	case 0:
    221 		setparity("none");
    222 		break;
    223 	default:
    224 		setparity("none");
    225 		break;
    226 	}
    227 
    228 	switch (flow) {
    229 	case -1:
    230 		if(nostop) {
    231 			setboolean(value(TAND), FALSE);
    232 			setboolean(value(HARDWAREFLOW), FALSE);
    233 		}
    234 		else {
    235 			setboolean(value(TAND), TRUE);
    236 			setboolean(value(HARDWAREFLOW), FALSE);
    237 		}
    238 		break;
    239 	case 1:
    240 		setboolean(value(TAND), FALSE);
    241 		setboolean(value(HARDWAREFLOW), TRUE);
    242 		break;
    243 	case 0:
    244 	default:
    245 		setboolean(value(TAND), FALSE);
    246 		setboolean(value(HARDWAREFLOW), FALSE);
    247 		break;
    248 	}
    249 	setcharacter(value(ESCAPE), useresc);
    250 	setboolean(value(VERBOSE), FALSE);
    251 	if (HD)
    252 		setboolean(value(LECHO), TRUE);
    253 	if (HW) {
    254 		if (ttysetup(BR) != 0) {
    255 			warnx("unsupported speed %ld", BR);
    256 			daemon_uid();
    257 			(void)uu_unlock(uucplock);
    258 			exit(3);
    259 		}
    260 	}
    261 	if (connect()) {
    262 		warnx("Connect failed");
    263 		daemon_uid();
    264 		(void)uu_unlock(uucplock);
    265 		exit(1);
    266 	}
    267 	if (!HW) {
    268 		if (ttysetup(BR) != 0) {
    269 			warnx("unsupported speed %ld", BR);
    270 			daemon_uid();
    271 			(void)uu_unlock(uucplock);
    272 			exit(3);
    273 		}
    274 	}
    275 }
    276 
    277 static void
    278 cuusage(void)
    279 {
    280 	fprintf(stderr, "Usage: cu [options] [phone-number]\n"
    281 	    "Use cu --help for help\n");
    282 	exit(8);
    283 }
    284 
    285 static void
    286 cuhelp(void)
    287 {
    288 	fprintf(stderr,
    289 	    "BSD tip/cu\n"
    290 	    "Usage: cu [options] [phone-number]\n"
    291 	    " -E,--escape char: Use this escape character\n"
    292 	    " -F,--flow {hard,soft,none}: Use RTS/CTS, ^S/^Q, no flow control\n"
    293 	    " --nostop: Do not use software flow control\n"
    294 	    " -a, -p,--port port: Use this port as ACU/Dialer\n"
    295 	    " -c,--phone number: Call this number\n"
    296 	    " -h,--halfduplex: Echo characters locally (use \"half duplex\")\n"
    297 	    " -e: Use even parity\n"
    298 	    " -o: Use odd parity\n"
    299 	    " -P,--parity {even,odd,none}: use even, odd, no parity\n"
    300 	    " -l,-line line: Use this device (ttyXX)\n"
    301 	    " -s,--speed,--baud speed,-#: Use this speed\n"
    302 	    " -t: Connect via hard-wired connection\n");
    303 	exit(0);
    304 }
    305