xref: /titanic_50/usr/src/cmd/bnu/imsg.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1988 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include "uucp.h"
34 
35 #define MSYNC	'\020'
36 /* maximum likely message - make sure you don't get run away input */
37 #define MAXIMSG	256
38 
39 /*
40  * read message routine used before a
41  * protocol is agreed upon.
42  *	msg	-> address of input buffer
43  *	fn	-> input file descriptor
44  * returns:
45  *	EOF	-> no more messages
46  *	0	-> message returned
47  */
48 int
49 imsg(msg, fn)
50 register char *msg;
51 register int fn;
52 {
53 	register char c;
54 	register int i;
55 	short fndsync;
56 	char *bmsg;
57 	extern int	errno, sys_nerr;
58 	extern char *sys_errlist[];
59 
60 	fndsync = 0;
61 	bmsg = msg;
62 	CDEBUG(7, "imsg %s>", "");
63 	while ((i = (*Read)(fn, msg, sizeof(char))) == sizeof(char)) {
64 		*msg &= 0177;
65 		c = *msg;
66 		CDEBUG(7, "%s", c < 040 ? "^" : "");
67 		CDEBUG(7, "%c", c < 040 ? c | 0100 : c);
68 		if (c == MSYNC) { /* look for sync character */
69 			msg = bmsg;
70 			fndsync = 1;
71 			continue;
72 		}
73 		if (!fndsync)
74 			continue;
75 
76 		if (c == '\0' || c == '\n') {
77 			*msg = '\0';
78 			return(0);
79 		}
80 		else
81 			msg++;
82 
83 		if (msg - bmsg > MAXIMSG)	/* unlikely */
84 			return(FAIL);
85 	}
86 	/* have not found sync or end of message */
87 	if (i < 0) {
88 		if (errno < sys_nerr)
89 			CDEBUG(7, "\nimsg read error: %s\n",
90 			    sys_errlist[errno]);
91 		else
92 			CDEBUG(7, "\nimsg read error, errno %d\n", errno);
93 	}
94 	*msg = '\0';
95 	return(EOF);
96 }
97 
98 /*
99  * initial write message routine -
100  * used before a protocol is agreed upon.
101  *	type	-> message type
102  *	msg	-> message body address
103  *	fn	-> file descriptor
104  * return:
105  *	Must always return 0 - wmesg (WMESG) looks for zero
106  */
107 int
108 omsg(type, msg, fn)
109 register char *msg;
110 register char type;
111 int fn;
112 {
113 	char buf[BUFSIZ];
114 
115 	(void) sprintf(buf, "%c%c%s", MSYNC, type, msg);
116 	DEBUG( 7, "omsg \"%s\"\n", &buf[1] );
117 	(*Write)(fn, buf, strlen(buf) + 1);
118 	return(0);
119 }
120 
121 /*
122  * null turnoff routine to be used for errors
123  * during protocol selection.
124  */
125 turnoff()
126 {
127 	return(0);
128 }
129