xref: /illumos-gate/usr/src/cmd/bnu/imsg.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 2005 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 #include "uucp.h"
31 
32 #define MSYNC	'\020'
33 /* maximum likely message - make sure you don't get run away input */
34 #define MAXIMSG	256
35 
36 /*
37  * read message routine used before a
38  * protocol is agreed upon.
39  *	msg	-> address of input buffer
40  *	fn	-> input file descriptor
41  * returns:
42  *	EOF	-> no more messages
43  *	0	-> message returned
44  */
45 int
imsg(msg,fn)46 imsg(msg, fn)
47 char *msg;
48 int fn;
49 {
50 	char c;
51 	int i;
52 	short fndsync;
53 	char *bmsg;
54 
55 	fndsync = 0;
56 	bmsg = msg;
57 	CDEBUG(7, "imsg %s>", "");
58 	while ((i = (*Read)(fn, msg, sizeof(char))) == sizeof(char)) {
59 		*msg &= 0177;
60 		c = *msg;
61 		CDEBUG(7, "%s", c < 040 ? "^" : "");
62 		CDEBUG(7, "%c", c < 040 ? c | 0100 : c);
63 		if (c == MSYNC) { /* look for sync character */
64 			msg = bmsg;
65 			fndsync = 1;
66 			continue;
67 		}
68 		if (!fndsync)
69 			continue;
70 
71 		if (c == '\0' || c == '\n') {
72 			*msg = '\0';
73 			return(0);
74 		}
75 		else
76 			msg++;
77 
78 		if (msg - bmsg > MAXIMSG)	/* unlikely */
79 			return(FAIL);
80 	}
81 	/* have not found sync or end of message */
82 	if (i < 0) {
83 		CDEBUG(7, "\nimsg read error: %s\n", strerror(errno));
84 	}
85 	*msg = '\0';
86 	return(EOF);
87 }
88 
89 /*
90  * initial write message routine -
91  * used before a protocol is agreed upon.
92  *	type	-> message type
93  *	msg	-> message body address
94  *	fn	-> file descriptor
95  * return:
96  *	Must always return 0 - wmesg (WMESG) looks for zero
97  */
98 int
omsg(type,msg,fn)99 omsg(type, msg, fn)
100 char *msg;
101 char type;
102 int fn;
103 {
104 	char buf[BUFSIZ];
105 
106 	(void) sprintf(buf, "%c%c%s", MSYNC, type, msg);
107 	DEBUG( 7, "omsg \"%s\"\n", &buf[1] );
108 	(*Write)(fn, buf, strlen(buf) + 1);
109 	return(0);
110 }
111 
112 /*
113  * null turnoff routine to be used for errors
114  * during protocol selection.
115  */
116 int
turnoff(void)117 turnoff(void)
118 {
119 	return(0);
120 }
121