xref: /csrg-svn/usr.bin/uucp/uucico/imsg.c (revision 17835)
113657Ssam #ifndef lint
2*17835Sralph static char sccsid[] = "@(#)imsg.c	5.2 (Berkeley) 01/22/85";
313657Ssam #endif
413657Ssam 
513657Ssam #include "uucp.h"
613657Ssam 
713657Ssam char Msync[2] = "\020";
8*17835Sralph 
9*17835Sralph /* to talk to both eunice and x.25 without also screwing up tcp/ip
10*17835Sralph  * we must adaptively  choose what character to end the msg with
11*17835Sralph  *
12*17835Sralph  * The idea is that initially we send ....\000\n
13*17835Sralph  * Then, after they have sent us a message, we use the first character
14*17835Sralph  * they send.
15*17835Sralph  */
16*17835Sralph 
17*17835Sralph int seenend = 0;
18*17835Sralph char Mend = '\0';
19*17835Sralph 
20*17835Sralph /*
21*17835Sralph  *	this is the initial read message routine -
2213657Ssam  *	used before a protocol is agreed upon.
2313657Ssam  *
2413657Ssam  *	return codes:
2513657Ssam  *		EOF - no more messages
2613657Ssam  *		0 - message returned
2713657Ssam  */
2813657Ssam 
2913657Ssam imsg(msg, fn)
3013657Ssam register char *msg;
3113657Ssam register int fn;
3213657Ssam {
3313657Ssam 	register int ret;
34*17835Sralph 	char *amsg;
35*17835Sralph 
36*17835Sralph 	DEBUG(5, "imsg %s<", "sync");
3713657Ssam 	while ((ret = read(fn, msg, 1)) == 1) {
38*17835Sralph 		*msg &= 0177;
39*17835Sralph 		DEBUG(5, (*msg>037 && *msg<0177) ? "%c" : "\\%03o", *msg & 0377);
4013657Ssam 		if (*msg == Msync[0])
4113657Ssam 			break;
42*17835Sralph 		fflush(stderr);
4313657Ssam 	}
44*17835Sralph 	DEBUG(5, ">got %s\n", ret == 1 ? "it" : "EOF");
4513657Ssam 	if (ret < 1)
46*17835Sralph 		return EOF;
47*17835Sralph 	amsg = msg;
48*17835Sralph resync:
49*17835Sralph 	DEBUG(5, "imsg %s<", "input");
5013657Ssam 	while (read(fn, msg, 1) == 1) {
5113657Ssam 		*msg &= 0177;
52*17835Sralph 		DEBUG(5, (*msg>037 && *msg<0177) ? "%c" : "\\%03o", *msg & 0377);
53*17835Sralph 		if (*msg == Msync[0]) {
54*17835Sralph 			DEBUG(5, "%s\n", ">found sync");
55*17835Sralph 			msg = amsg;
56*17835Sralph 			goto resync;
57*17835Sralph 		}
58*17835Sralph 		if (*msg == '\n' || *msg == '\0') {
59*17835Sralph 			if (!seenend) {
60*17835Sralph 				Mend = *msg;
61*17835Sralph 				seenend++;
62*17835Sralph 				DEBUG(6,"\nUsing \\%o as End of message char\n", Mend);
63*17835Sralph 			}
6413657Ssam 			break;
65*17835Sralph 		}
6613657Ssam 		msg++;
67*17835Sralph 		fflush(stderr);
6813657Ssam 	}
6913657Ssam 	*msg = '\0';
70*17835Sralph 	DEBUG(5, ">got %d\n", strlen(amsg));
71*17835Sralph 	return 0;
7213657Ssam }
7313657Ssam 
7413657Ssam 
75*17835Sralph /*
76*17835Sralph  *	this is the initial write message routine -
7713657Ssam  *	used before a protocol is agreed upon.
7813657Ssam  *
7913657Ssam  *	return code:  always 0
8013657Ssam  */
8113657Ssam 
8213657Ssam omsg(type, msg, fn)
8313657Ssam register char *msg;
8413657Ssam char type;
8513657Ssam int fn;
8613657Ssam {
87*17835Sralph 	char buf[MAXFULLNAME];
8813657Ssam 	register char *c;
8913657Ssam 
9013657Ssam 	c = buf;
91*17835Sralph 	*c = '\0';	/* avoid pdp 11/23,40 auto-incr stack trap bug */
9213657Ssam 	*c++ = Msync[0];
9313657Ssam 	*c++ = type;
9413657Ssam 	while (*msg)
9513657Ssam 		*c++ = *msg++;
9613657Ssam 	*c++ = '\0';
97*17835Sralph 	DEBUG(5, "omsg <%s>\n", buf);
98*17835Sralph 	if (seenend)
99*17835Sralph 		c[-1] = Mend;
100*17835Sralph 	else
101*17835Sralph 		*c++ = '\n';
102*17835Sralph 	write(fn, buf, (int)(c - buf));
103*17835Sralph 	return 0;
10413657Ssam }
105