3 * vmhsbr.c -- routines to help vmh along
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 * INI: include width of windows
21 static char *types[] = {
23 "INI", "ACK", "ERR", "CMD", "QRY", "TTY", "WIN", "DATA", "EOF", "FIN",
27 static FILE *fp = NULL;
29 static int PEERrfd = NOTOK;
30 static int PEERwfd = NOTOK;
35 static int rclose (struct record *, char *, ...);
39 rcinit (int rfd, int wfd)
41 char *cp, buffer[BUFSIZ];
46 if ((cp = getenv ("MHVDEBUG")) && *cp) {
47 snprintf (buffer, sizeof(buffer), "%s.out", invo_name);
48 if ((fp = fopen (buffer, "w"))) {
49 fseek (fp, 0L, SEEK_END);
50 fprintf (fp, "%d: rcinit (%d, %d)\n", (int) getpid(), rfd, wfd);
76 rc2rc (char code, int len, char *data, struct record *rc)
78 if (rc2peer (code, len, data) == NOTOK)
86 str2rc (char code, char *str, struct record *rc)
88 return rc2rc (code, str ? strlen (str) : 0, str, rc);
93 peer2rc (struct record *rc)
98 if (read (PEERrfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
99 return rclose (rc, "read from peer lost(1)");
101 if ((rc->rc_data = malloc ((unsigned) rc->rc_len + 1)) == NULL)
102 return rclose (rc, "malloc of %d lost", rc->rc_len + 1);
103 if (read (PEERrfd, rc->rc_data, rc->rc_len) != rc->rc_len)
104 return rclose (rc, "read from peer lost(2)");
105 rc->rc_data[rc->rc_len] = 0;
111 fseek (fp, 0L, SEEK_END);
112 fprintf (fp, "%d: <--- %s %d: \"%*.*s\"\n", (int) getpid(),
113 types[(unsigned char)rc->rc_type], rc->rc_len,
114 rc->rc_len, rc->rc_len, rc->rc_data);
123 rc2peer (char code, int len, char *data)
126 register struct record *rc = &rcs;
132 fseek (fp, 0L, SEEK_END);
133 fprintf (fp, "%d: ---> %s %d: \"%*.*s\"\n", (int) getpid(),
134 types[(unsigned char)rc->rc_type], rc->rc_len,
135 rc->rc_len, rc->rc_len, data);
139 if (write (PEERwfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
140 return rclose (rc, "write to peer lost(1)");
143 if (write (PEERwfd, data, rc->rc_len) != rc->rc_len)
144 return rclose (rc, "write to peer lost(2)");
151 str2peer (char code, char *str)
153 return rc2peer (code, str ? strlen (str) : 0, str);
158 fmt2peer (char code, char *fmt, ...)
163 return verr2peer (code, NULL, fmt, ap);
169 err2peer (char code, char *what, char *fmt, ...)
175 return_value = verr2peer(code, what, fmt, ap);
177 return return_value; /* This routine returned garbage before 1999-07-15. */
182 verr2peer (char code, char *what, char *fmt, va_list ap)
186 char *bp, *s, buffer[BUFSIZ * 2];
188 /* Get buffer ready to go */
190 buflen = sizeof(buffer);
192 vsnprintf (bp, buflen, fmt, ap);
199 snprintf (bp, buflen, " %s: ", what);
204 if ((s = strerror (eindex)))
205 strncpy (bp, s, buflen);
207 snprintf (bp, buflen, "unknown error %d", eindex);
213 return rc2peer (code, bp - buffer, buffer);
218 rclose (struct record *rc, char *fmt, ...)
221 static char buffer[BUFSIZ * 2];
224 vsnprintf (buffer, sizeof(buffer), fmt, ap);
227 rc->rc_len = strlen (rc->rc_data = getcpy (buffer));
228 return (rc->rc_type = RC_XXX);