xref: /csrg-svn/usr.bin/uucp/uucico/imsg.c (revision 62391)
148664Sbostic /*-
2*62391Sbostic  * Copyright (c) 1985, 1993
3*62391Sbostic  *	The Regents of the University of California.  All rights reserved.
448664Sbostic  *
548664Sbostic  * %sccs.include.proprietary.c%
648664Sbostic  */
748664Sbostic 
813657Ssam #ifndef lint
9*62391Sbostic static char sccsid[] = "@(#)imsg.c	8.1 (Berkeley) 06/06/93";
1048664Sbostic #endif /* not lint */
1113657Ssam 
1213657Ssam #include "uucp.h"
1325706Sbloom #include <ctype.h>
1413657Ssam 
1513657Ssam char Msync[2] = "\020";
1617835Sralph 
1717835Sralph /* to talk to both eunice and x.25 without also screwing up tcp/ip
1817835Sralph  * we must adaptively  choose what character to end the msg with
1917835Sralph  *
2017835Sralph  * The idea is that initially we send ....\000\n
2117835Sralph  * Then, after they have sent us a message, we use the first character
2217835Sralph  * they send.
2317835Sralph  */
2417835Sralph 
2517835Sralph int seenend = 0;
2617835Sralph char Mend = '\0';
2717835Sralph 
2817835Sralph /*
2917835Sralph  *	this is the initial read message routine -
3013657Ssam  *	used before a protocol is agreed upon.
3113657Ssam  *
3213657Ssam  *	return codes:
3325706Sbloom  *		FAIL - no more messages
3425706Sbloom  *		SUCCESS - message returned
3513657Ssam  */
3613657Ssam 
imsg(amsg,fn)3725706Sbloom imsg(amsg, fn)
3825706Sbloom char *amsg;
3913657Ssam register int fn;
4013657Ssam {
4125706Sbloom 	register char *msg = amsg;
4233565Srick 	register int nchars = 0;
4325706Sbloom 	int foundsync = FAIL;
4425706Sbloom 	char c;
4517835Sralph 
4625706Sbloom 	DEBUG(5, "imsg looking for SYNC<", CNULL);
4725706Sbloom 	for (;;) {
4825706Sbloom 		if (read(fn, &c, 1) != 1)
4925706Sbloom 			return FAIL;
5033565Srick 		DEBUG(9,"\t%o", c&0377);
5125706Sbloom 		c &= 0177;
5225706Sbloom 		if (c == '\n' || c == '\r')
5325706Sbloom 			DEBUG(5, "%c", c);
5425706Sbloom 		else
5525706Sbloom 			DEBUG(5, (isprint(c) || isspace(c)) ? "%c" : "\\%o",
5625706Sbloom 				c & 0377);
5733565Srick 		c &= 0177;
5825706Sbloom 		if (c == Msync[0]) {
5925706Sbloom 			DEBUG(5, ">\nimsg input<", CNULL);
6017835Sralph 			msg = amsg;
6133565Srick 			nchars = 0;
6225706Sbloom 			foundsync = SUCCESS;
6325706Sbloom 			continue;
6425706Sbloom 		} else if (foundsync != SUCCESS)
6525706Sbloom 				continue;
6625706Sbloom 		if (c == '\n' || c == '\0') {
6717835Sralph 			if (!seenend) {
6825706Sbloom 				Mend = c;
6917835Sralph 				seenend++;
7025706Sbloom 				DEBUG(9, "\nUsing \\%o as End of message char\n", Mend);
7117835Sralph 			}
7213657Ssam 			break;
7317835Sralph 		}
7425706Sbloom 		*msg++ = c;
7533565Srick 		/* MAXFULLNAME should really be passed in as a parameter */
7633565Srick 		if (nchars++ > MAXFULLNAME) {
7733565Srick 			DEBUG(1, "buffer overrun in imsg", CNULL);
7833565Srick 			return FAIL;
7933565Srick 		}
8017835Sralph 		fflush(stderr);
8113657Ssam 	}
8213657Ssam 	*msg = '\0';
8325706Sbloom 	DEBUG(5, ">got %d characters\n", strlen(amsg));
8425706Sbloom 	return foundsync;
8513657Ssam }
8613657Ssam 
8713657Ssam 
8817835Sralph /*
8917835Sralph  *	this is the initial write message routine -
9013657Ssam  *	used before a protocol is agreed upon.
9113657Ssam  *
9213657Ssam  *	return code:  always 0
9313657Ssam  */
9413657Ssam 
omsg(type,msg,fn)9513657Ssam omsg(type, msg, fn)
9613657Ssam register char *msg;
9713657Ssam char type;
9813657Ssam int fn;
9913657Ssam {
10017835Sralph 	char buf[MAXFULLNAME];
10113657Ssam 	register char *c;
10213657Ssam 
10313657Ssam 	c = buf;
10417835Sralph 	*c = '\0';	/* avoid pdp 11/23,40 auto-incr stack trap bug */
10513657Ssam 	*c++ = Msync[0];
10613657Ssam 	*c++ = type;
10713657Ssam 	while (*msg)
10813657Ssam 		*c++ = *msg++;
10913657Ssam 	*c++ = '\0';
11017835Sralph 	DEBUG(5, "omsg <%s>\n", buf);
11117835Sralph 	if (seenend)
11217835Sralph 		c[-1] = Mend;
11317835Sralph 	else
11417835Sralph 		*c++ = '\n';
11517835Sralph 	write(fn, buf, (int)(c - buf));
11617835Sralph 	return 0;
11713657Ssam }
118