xref: /original-bsd/usr.bin/uucp/uucico/imsg.c (revision 2301fdfb)
1 #ifndef lint
2 static char sccsid[] = "@(#)imsg.c	5.4 (Berkeley) 02/24/88";
3 #endif
4 
5 #include "uucp.h"
6 #include <ctype.h>
7 
8 char Msync[2] = "\020";
9 
10 /* to talk to both eunice and x.25 without also screwing up tcp/ip
11  * we must adaptively  choose what character to end the msg with
12  *
13  * The idea is that initially we send ....\000\n
14  * Then, after they have sent us a message, we use the first character
15  * they send.
16  */
17 
18 int seenend = 0;
19 char Mend = '\0';
20 
21 /*
22  *	this is the initial read message routine -
23  *	used before a protocol is agreed upon.
24  *
25  *	return codes:
26  *		FAIL - no more messages
27  *		SUCCESS - message returned
28  */
29 
30 imsg(amsg, fn)
31 char *amsg;
32 register int fn;
33 {
34 	register char *msg = amsg;
35 	register int nchars = 0;
36 	int foundsync = FAIL;
37 	char c;
38 
39 	DEBUG(5, "imsg looking for SYNC<", CNULL);
40 	for (;;) {
41 		if (read(fn, &c, 1) != 1)
42 			return FAIL;
43 		DEBUG(9,"\t%o", c&0377);
44 		c &= 0177;
45 		if (c == '\n' || c == '\r')
46 			DEBUG(5, "%c", c);
47 		else
48 			DEBUG(5, (isprint(c) || isspace(c)) ? "%c" : "\\%o",
49 				c & 0377);
50 		c &= 0177;
51 		if (c == Msync[0]) {
52 			DEBUG(5, ">\nimsg input<", CNULL);
53 			msg = amsg;
54 			nchars = 0;
55 			foundsync = SUCCESS;
56 			continue;
57 		} else if (foundsync != SUCCESS)
58 				continue;
59 		if (c == '\n' || c == '\0') {
60 			if (!seenend) {
61 				Mend = c;
62 				seenend++;
63 				DEBUG(9, "\nUsing \\%o as End of message char\n", Mend);
64 			}
65 			break;
66 		}
67 		*msg++ = c;
68 		/* MAXFULLNAME should really be passed in as a parameter */
69 		if (nchars++ > MAXFULLNAME) {
70 			DEBUG(1, "buffer overrun in imsg", CNULL);
71 			return FAIL;
72 		}
73 		fflush(stderr);
74 	}
75 	*msg = '\0';
76 	DEBUG(5, ">got %d characters\n", strlen(amsg));
77 	return foundsync;
78 }
79 
80 
81 /*
82  *	this is the initial write message routine -
83  *	used before a protocol is agreed upon.
84  *
85  *	return code:  always 0
86  */
87 
88 omsg(type, msg, fn)
89 register char *msg;
90 char type;
91 int fn;
92 {
93 	char buf[MAXFULLNAME];
94 	register char *c;
95 
96 	c = buf;
97 	*c = '\0';	/* avoid pdp 11/23,40 auto-incr stack trap bug */
98 	*c++ = Msync[0];
99 	*c++ = type;
100 	while (*msg)
101 		*c++ = *msg++;
102 	*c++ = '\0';
103 	DEBUG(5, "omsg <%s>\n", buf);
104 	if (seenend)
105 		c[-1] = Mend;
106 	else
107 		*c++ = '\n';
108 	write(fn, buf, (int)(c - buf));
109 	return 0;
110 }
111