xref: /csrg-svn/usr.bin/uucp/uucico/imsg.c (revision 25706)
113657Ssam #ifndef lint
2*25706Sbloom static char sccsid[] = "@(#)imsg.c	5.3 (Berkeley) 01/06/86";
313657Ssam #endif
413657Ssam 
513657Ssam #include "uucp.h"
6*25706Sbloom #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:
26*25706Sbloom  *		FAIL - no more messages
27*25706Sbloom  *		SUCCESS - message returned
2813657Ssam  */
2913657Ssam 
30*25706Sbloom imsg(amsg, fn)
31*25706Sbloom char *amsg;
3213657Ssam register int fn;
3313657Ssam {
34*25706Sbloom 	register char *msg = amsg;
35*25706Sbloom 	int foundsync = FAIL;
36*25706Sbloom 	char c;
3717835Sralph 
38*25706Sbloom 	DEBUG(5, "imsg looking for SYNC<", CNULL);
39*25706Sbloom 	for (;;) {
40*25706Sbloom 		if (read(fn, &c, 1) != 1)
41*25706Sbloom 			return FAIL;
42*25706Sbloom 		c &= 0177;
43*25706Sbloom 		if (c == '\n' || c == '\r')
44*25706Sbloom 			DEBUG(5, "%c", c);
45*25706Sbloom 		else
46*25706Sbloom 			DEBUG(5, (isprint(c) || isspace(c)) ? "%c" : "\\%o",
47*25706Sbloom 				c & 0377);
48*25706Sbloom 		if (c == Msync[0]) {
49*25706Sbloom 			DEBUG(5, ">\nimsg input<", CNULL);
5017835Sralph 			msg = amsg;
51*25706Sbloom 			foundsync = SUCCESS;
52*25706Sbloom 			continue;
53*25706Sbloom 		} else if (foundsync != SUCCESS)
54*25706Sbloom 				continue;
55*25706Sbloom 		if (c == '\n' || c == '\0') {
5617835Sralph 			if (!seenend) {
57*25706Sbloom 				Mend = c;
5817835Sralph 				seenend++;
59*25706Sbloom 				DEBUG(9, "\nUsing \\%o as End of message char\n", Mend);
6017835Sralph 			}
6113657Ssam 			break;
6217835Sralph 		}
63*25706Sbloom 		*msg++ = c;
6417835Sralph 		fflush(stderr);
6513657Ssam 	}
6613657Ssam 	*msg = '\0';
67*25706Sbloom 	DEBUG(5, ">got %d characters\n", strlen(amsg));
68*25706Sbloom 	return foundsync;
6913657Ssam }
7013657Ssam 
7113657Ssam 
7217835Sralph /*
7317835Sralph  *	this is the initial write message routine -
7413657Ssam  *	used before a protocol is agreed upon.
7513657Ssam  *
7613657Ssam  *	return code:  always 0
7713657Ssam  */
7813657Ssam 
7913657Ssam omsg(type, msg, fn)
8013657Ssam register char *msg;
8113657Ssam char type;
8213657Ssam int fn;
8313657Ssam {
8417835Sralph 	char buf[MAXFULLNAME];
8513657Ssam 	register char *c;
8613657Ssam 
8713657Ssam 	c = buf;
8817835Sralph 	*c = '\0';	/* avoid pdp 11/23,40 auto-incr stack trap bug */
8913657Ssam 	*c++ = Msync[0];
9013657Ssam 	*c++ = type;
9113657Ssam 	while (*msg)
9213657Ssam 		*c++ = *msg++;
9313657Ssam 	*c++ = '\0';
9417835Sralph 	DEBUG(5, "omsg <%s>\n", buf);
9517835Sralph 	if (seenend)
9617835Sralph 		c[-1] = Mend;
9717835Sralph 	else
9817835Sralph 		*c++ = '\n';
9917835Sralph 	write(fn, buf, (int)(c - buf));
10017835Sralph 	return 0;
10113657Ssam }
102