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