xref: /illumos-gate/usr/src/cmd/bnu/imsg.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 
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 char *msg;
51 int fn;
52 {
53 	char c;
54 	int i;
55 	short fndsync;
56 	char *bmsg;
57 
58 	fndsync = 0;
59 	bmsg = msg;
60 	CDEBUG(7, "imsg %s>", "");
61 	while ((i = (*Read)(fn, msg, sizeof(char))) == sizeof(char)) {
62 		*msg &= 0177;
63 		c = *msg;
64 		CDEBUG(7, "%s", c < 040 ? "^" : "");
65 		CDEBUG(7, "%c", c < 040 ? c | 0100 : c);
66 		if (c == MSYNC) { /* look for sync character */
67 			msg = bmsg;
68 			fndsync = 1;
69 			continue;
70 		}
71 		if (!fndsync)
72 			continue;
73 
74 		if (c == '\0' || c == '\n') {
75 			*msg = '\0';
76 			return(0);
77 		}
78 		else
79 			msg++;
80 
81 		if (msg - bmsg > MAXIMSG)	/* unlikely */
82 			return(FAIL);
83 	}
84 	/* have not found sync or end of message */
85 	if (i < 0) {
86 		CDEBUG(7, "\nimsg read error: %s\n", strerror(errno));
87 	}
88 	*msg = '\0';
89 	return(EOF);
90 }
91 
92 /*
93  * initial write message routine -
94  * used before a protocol is agreed upon.
95  *	type	-> message type
96  *	msg	-> message body address
97  *	fn	-> file descriptor
98  * return:
99  *	Must always return 0 - wmesg (WMESG) looks for zero
100  */
101 int
102 omsg(type, msg, fn)
103 char *msg;
104 char type;
105 int fn;
106 {
107 	char buf[BUFSIZ];
108 
109 	(void) sprintf(buf, "%c%c%s", MSYNC, type, msg);
110 	DEBUG( 7, "omsg \"%s\"\n", &buf[1] );
111 	(*Write)(fn, buf, strlen(buf) + 1);
112 	return(0);
113 }
114 
115 /*
116  * null turnoff routine to be used for errors
117  * during protocol selection.
118  */
119 int
120 turnoff(void)
121 {
122 	return(0);
123 }
124