xref: /csrg-svn/usr.bin/uucp/uucico/imsg.c (revision 33565)
113657Ssam #ifndef lint
2*33565Srick static char sccsid[] = "@(#)imsg.c	5.4 (Berkeley) 02/24/88";
313657Ssam #endif
413657Ssam 
513657Ssam #include "uucp.h"
625706Sbloom #include <ctype.h>
713657Ssam 
813657Ssam char Msync[2] = "\020";
917835Sralph 
1017835Sralph /* to talk to both eunice and x.25 without also screwing up tcp/ip
1117835Sralph  * we must adaptively  choose what character to end the msg with
1217835Sralph  *
1317835Sralph  * The idea is that initially we send ....\000\n
1417835Sralph  * Then, after they have sent us a message, we use the first character
1517835Sralph  * they send.
1617835Sralph  */
1717835Sralph 
1817835Sralph int seenend = 0;
1917835Sralph char Mend = '\0';
2017835Sralph 
2117835Sralph /*
2217835Sralph  *	this is the initial read message routine -
2313657Ssam  *	used before a protocol is agreed upon.
2413657Ssam  *
2513657Ssam  *	return codes:
2625706Sbloom  *		FAIL - no more messages
2725706Sbloom  *		SUCCESS - message returned
2813657Ssam  */
2913657Ssam 
3025706Sbloom imsg(amsg, fn)
3125706Sbloom char *amsg;
3213657Ssam register int fn;
3313657Ssam {
3425706Sbloom 	register char *msg = amsg;
35*33565Srick 	register int nchars = 0;
3625706Sbloom 	int foundsync = FAIL;
3725706Sbloom 	char c;
3817835Sralph 
3925706Sbloom 	DEBUG(5, "imsg looking for SYNC<", CNULL);
4025706Sbloom 	for (;;) {
4125706Sbloom 		if (read(fn, &c, 1) != 1)
4225706Sbloom 			return FAIL;
43*33565Srick 		DEBUG(9,"\t%o", c&0377);
4425706Sbloom 		c &= 0177;
4525706Sbloom 		if (c == '\n' || c == '\r')
4625706Sbloom 			DEBUG(5, "%c", c);
4725706Sbloom 		else
4825706Sbloom 			DEBUG(5, (isprint(c) || isspace(c)) ? "%c" : "\\%o",
4925706Sbloom 				c & 0377);
50*33565Srick 		c &= 0177;
5125706Sbloom 		if (c == Msync[0]) {
5225706Sbloom 			DEBUG(5, ">\nimsg input<", CNULL);
5317835Sralph 			msg = amsg;
54*33565Srick 			nchars = 0;
5525706Sbloom 			foundsync = SUCCESS;
5625706Sbloom 			continue;
5725706Sbloom 		} else if (foundsync != SUCCESS)
5825706Sbloom 				continue;
5925706Sbloom 		if (c == '\n' || c == '\0') {
6017835Sralph 			if (!seenend) {
6125706Sbloom 				Mend = c;
6217835Sralph 				seenend++;
6325706Sbloom 				DEBUG(9, "\nUsing \\%o as End of message char\n", Mend);
6417835Sralph 			}
6513657Ssam 			break;
6617835Sralph 		}
6725706Sbloom 		*msg++ = c;
68*33565Srick 		/* MAXFULLNAME should really be passed in as a parameter */
69*33565Srick 		if (nchars++ > MAXFULLNAME) {
70*33565Srick 			DEBUG(1, "buffer overrun in imsg", CNULL);
71*33565Srick 			return FAIL;
72*33565Srick 		}
7317835Sralph 		fflush(stderr);
7413657Ssam 	}
7513657Ssam 	*msg = '\0';
7625706Sbloom 	DEBUG(5, ">got %d characters\n", strlen(amsg));
7725706Sbloom 	return foundsync;
7813657Ssam }
7913657Ssam 
8013657Ssam 
8117835Sralph /*
8217835Sralph  *	this is the initial write message routine -
8313657Ssam  *	used before a protocol is agreed upon.
8413657Ssam  *
8513657Ssam  *	return code:  always 0
8613657Ssam  */
8713657Ssam 
8813657Ssam omsg(type, msg, fn)
8913657Ssam register char *msg;
9013657Ssam char type;
9113657Ssam int fn;
9213657Ssam {
9317835Sralph 	char buf[MAXFULLNAME];
9413657Ssam 	register char *c;
9513657Ssam 
9613657Ssam 	c = buf;
9717835Sralph 	*c = '\0';	/* avoid pdp 11/23,40 auto-incr stack trap bug */
9813657Ssam 	*c++ = Msync[0];
9913657Ssam 	*c++ = type;
10013657Ssam 	while (*msg)
10113657Ssam 		*c++ = *msg++;
10213657Ssam 	*c++ = '\0';
10317835Sralph 	DEBUG(5, "omsg <%s>\n", buf);
10417835Sralph 	if (seenend)
10517835Sralph 		c[-1] = Mend;
10617835Sralph 	else
10717835Sralph 		*c++ = '\n';
10817835Sralph 	write(fn, buf, (int)(c - buf));
10917835Sralph 	return 0;
11013657Ssam }
111