1 /* vmhsbr.c - routines to help vmh along */
3 static char ident[] = "@(#)$Id: vmhsbr.c,v 1.11 1993/08/25 17:29:53 jromine Exp $";
7 INI: include width of windows
11 #include "../h/vmhsbr.h"
16 static char *types[] = {
18 "INI", "ACK", "ERR", "CMD", "QRY", "TTY", "WIN", "DATA", "EOF", "FIN",
22 static FILE *fp = NULL;
24 static int PEERrfd = NOTOK;
25 static int PEERwfd = NOTOK;
31 extern char *sys_errlist[];
47 if ((cp = getenv ("MHVDEBUG")) && *cp) {
48 (void) sprintf (buffer, "%s.out", invo_name);
49 if (fp = fopen (buffer, "w")) {
50 (void) fseek (fp, 0L, 2);
51 fprintf (fp, "%d: rcinit (%d, %d)\n", getpid (), rfd, wfd);
62 (void) close (PEERrfd);
64 (void) close (PEERwfd);
75 int rc2rc (code, len, data, rc)
81 if (rc2peer (code, len, data) == NOTOK)
88 int str2rc (code, str, rc)
93 return rc2rc (code, str ? strlen (str) : 0, str, rc);
99 register struct record *rc;
102 free (rc -> rc_data);
104 if (read (PEERrfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
105 return rclose (rc, "read from peer lost(1)");
107 if ((rc -> rc_data = malloc ((unsigned) rc -> rc_len + 1)) == NULL)
108 return rclose (rc, "malloc of %d lost", rc -> rc_len + 1);
109 if (read (PEERrfd, rc -> rc_data, rc -> rc_len) != rc -> rc_len)
110 return rclose (rc, "read from peer lost(2)");
111 rc -> rc_data[rc -> rc_len] = 0;
114 rc -> rc_data = NULL;
117 (void) fseek (fp, 0L, 2);
118 fprintf (fp, "%d: <--- %s %d: \"%*.*s\"\n", getpid (),
119 types[rc -> rc_type], rc -> rc_len,
120 rc -> rc_len, rc -> rc_len, rc -> rc_data);
124 return rc -> rc_type;
129 int rc2peer (code, len, data)
135 register struct record *rc = &rcs;
137 rc -> rc_type = code;
141 (void) fseek (fp, 0L, 2);
142 fprintf (fp, "%d: ---> %s %d: \"%*.*s\"\n", getpid (),
143 types[rc -> rc_type], rc -> rc_len,
144 rc -> rc_len, rc -> rc_len, data);
148 if (write (PEERwfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
149 return rclose (rc, "write to peer lost(1)");
152 if (write (PEERwfd, data, rc -> rc_len) != rc -> rc_len)
153 return rclose (rc, "write to peer lost(2)");
160 int str2peer (code, str)
164 return rc2peer (code, str ? strlen (str) : 0, str);
170 int fmt2peer (code, fmt, a, b, c, d, e, f)
180 return err2peer (code, NULLCP, fmt, a, b, c, d, e, f);
187 int err2peer (code, what, fmt, a, b, c, d, e, f)
200 char buffer[BUFSIZ * 2];
202 (void) sprintf (buffer, fmt, a, b, c, d, e, f);
203 bp = buffer + strlen (buffer);
206 (void) sprintf (bp, " %s: ", what);
209 if (eindex > 0 && eindex < sys_nerr)
210 (void) strcpy (bp, sys_errlist[eindex]);
212 (void) sprintf (bp, "Error %d", eindex);
216 return rc2peer (code, bp - buffer, buffer);
223 static int rclose (rc, fmt, a, b, c, d, e, f)
224 register struct record *rc;
233 static char buffer[BUFSIZ * 2];
235 (void) sprintf (buffer, fmt, a, b, c, d, e, f);
237 rc -> rc_len = strlen (rc -> rc_data = getcpy (buffer));
238 return (rc -> rc_type = RC_XXX);