3 * vmhsbr.c -- routines to help vmh along
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 * INI: include width of windows
20 static char *types[] = {
22 "INI", "ACK", "ERR", "CMD", "QRY", "TTY", "WIN", "DATA", "EOF", "FIN",
26 static FILE *fp = NULL;
28 static int PEERrfd = NOTOK;
29 static int PEERwfd = NOTOK;
34 static int rclose (struct record *, char *, ...);
38 rcinit (int rfd, int wfd)
40 char *cp, buffer[BUFSIZ];
45 if ((cp = getenv ("MHVDEBUG")) && *cp) {
46 snprintf (buffer, sizeof(buffer), "%s.out", invo_name);
47 if ((fp = fopen (buffer, "w"))) {
48 fseek (fp, 0L, SEEK_END);
49 fprintf (fp, "%d: rcinit (%d, %d)\n", (int) getpid(), rfd, wfd);
75 rc2rc (char code, int len, char *data, struct record *rc)
77 if (rc2peer (code, len, data) == NOTOK)
85 str2rc (char code, char *str, struct record *rc)
87 return rc2rc (code, str ? strlen (str) : 0, str, rc);
92 peer2rc (struct record *rc)
97 if (read (PEERrfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
98 return rclose (rc, "read from peer lost(1)");
100 rc->rc_data = mh_xmalloc ((unsigned) rc->rc_len + 1);
101 if (read (PEERrfd, rc->rc_data, rc->rc_len) != rc->rc_len)
102 return rclose (rc, "read from peer lost(2)");
103 rc->rc_data[rc->rc_len] = 0;
109 fseek (fp, 0L, SEEK_END);
110 fprintf (fp, "%d: <--- %s %d: \"%*.*s\"\n", (int) getpid(),
111 types[(unsigned char)rc->rc_type], rc->rc_len,
112 rc->rc_len, rc->rc_len, rc->rc_data);
121 rc2peer (char code, int len, char *data)
124 register struct record *rc = &rcs;
130 fseek (fp, 0L, SEEK_END);
131 fprintf (fp, "%d: ---> %s %d: \"%*.*s\"\n", (int) getpid(),
132 types[(unsigned char)rc->rc_type], rc->rc_len,
133 rc->rc_len, rc->rc_len, data);
137 if (write (PEERwfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
138 return rclose (rc, "write to peer lost(1)");
141 if (write (PEERwfd, data, rc->rc_len) != rc->rc_len)
142 return rclose (rc, "write to peer lost(2)");
149 str2peer (char code, char *str)
151 return rc2peer (code, str ? strlen (str) : 0, str);
156 fmt2peer (char code, char *fmt, ...)
161 return verr2peer (code, NULL, fmt, ap);
167 err2peer (char code, char *what, char *fmt, ...)
173 return_value = verr2peer(code, what, fmt, ap);
175 return return_value; /* This routine returned garbage before 1999-07-15. */
180 verr2peer (char code, char *what, char *fmt, va_list ap)
184 char *bp, *s, buffer[BUFSIZ * 2];
186 /* Get buffer ready to go */
188 buflen = sizeof(buffer);
190 vsnprintf (bp, buflen, fmt, ap);
197 snprintf (bp, buflen, " %s: ", what);
202 if ((s = strerror (eindex)))
203 strncpy (bp, s, buflen);
205 snprintf (bp, buflen, "unknown error %d", eindex);
211 return rc2peer (code, bp - buffer, buffer);
216 rclose (struct record *rc, char *fmt, ...)
219 static char buffer[BUFSIZ * 2];
222 vsnprintf (buffer, sizeof(buffer), fmt, ap);
225 rc->rc_len = strlen (rc->rc_data = getcpy (buffer));
226 return (rc->rc_type = RC_XXX);