Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / vmhsbr.c
1 /* vmhsbr.c - routines to help vmh along */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: vmhsbr.c,v 1.11 1993/08/25 17:29:53 jromine Exp $";
4 #endif  /* lint */
5
6 /* TODO (for vrsn 2):
7         INI: include width of windows
8  */
9
10 #include "../h/mh.h"
11 #include "../h/vmhsbr.h"
12 #include <stdio.h>
13
14 /* \f */
15
16 static char *types[] = {
17     "OK",
18     "INI", "ACK", "ERR", "CMD", "QRY", "TTY", "WIN", "DATA", "EOF", "FIN",
19     "XXX", NULL
20 };
21
22 static  FILE *fp = NULL;
23
24 static  int PEERrfd = NOTOK;
25 static  int PEERwfd = NOTOK;
26
27
28 extern int  errno;
29 #ifndef BSD44
30 extern int  sys_nerr;
31 extern char *sys_errlist[];
32 #endif
33
34 static int      rclose();
35 /* \f */
36
37 int     rcinit (rfd, wfd)
38 int     rfd,
39         wfd;
40 {
41     char   *cp,
42             buffer[BUFSIZ];
43
44     PEERrfd = rfd;
45     PEERwfd = wfd;
46
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);
52           (void) fflush (fp);
53         }
54     }
55
56     return OK;
57 }
58
59
60 int     rcdone () {
61     if (PEERrfd != NOTOK)
62         (void) close (PEERrfd);
63     if (PEERwfd != NOTOK)
64         (void) close (PEERwfd);
65
66     if (fp) {
67         (void) fclose (fp);
68         fp = NULL;
69     }
70     return OK;
71 }
72
73 /* \f */
74
75 int     rc2rc (code, len, data, rc)
76 char    code;
77 int     len;
78 char   *data;
79 struct record *rc;
80 {
81     if (rc2peer (code, len, data) == NOTOK)
82         return NOTOK;
83
84     return peer2rc (rc);
85 }
86
87
88 int     str2rc (code, str, rc)
89 char    code;
90 char   *str;
91 struct record *rc;
92 {
93     return rc2rc (code, str ? strlen (str) : 0, str, rc);
94 }
95
96 /* \f */
97
98 int     peer2rc (rc)
99 register struct record *rc;
100 {
101     if (rc -> rc_data)
102         free (rc -> rc_data);
103
104     if (read (PEERrfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
105         return rclose (rc, "read from peer lost(1)");
106     if (rc -> rc_len) {
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;
112     }
113     else
114         rc -> rc_data = NULL;
115
116     if (fp) {
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);
121         (void) fflush (fp);
122     }
123
124     return rc -> rc_type;
125 }
126
127 /* \f */
128
129 int     rc2peer (code, len, data)
130 char    code;
131 int     len;
132 char   *data;
133 {
134     struct record   rcs;
135     register struct record *rc = &rcs;
136
137     rc -> rc_type = code;
138     rc -> rc_len = len;
139
140     if (fp) {
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);
145         (void) fflush (fp);
146     }
147
148     if (write (PEERwfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
149         return rclose (rc, "write to peer lost(1)");
150
151     if (rc -> rc_len)
152         if (write (PEERwfd, data, rc -> rc_len) != rc -> rc_len)
153             return rclose (rc, "write to peer lost(2)");
154
155     return OK;
156 }
157
158 /* \f */
159
160 int     str2peer (code, str)
161 char    code;
162 char   *str;
163 {
164     return rc2peer (code, str ? strlen (str) : 0, str);
165 }
166
167
168 /* VARARGS2 */
169
170 int     fmt2peer (code, fmt, a, b, c, d, e, f)
171 char    code;
172 char   *fmt,
173        *a,
174        *b,
175        *c,
176        *d,
177        *e,
178        *f;
179 {
180     return err2peer (code, NULLCP, fmt, a, b, c, d, e, f);
181 }
182
183 /* \f */
184
185 /* VARARGS3 */
186
187 int     err2peer (code, what, fmt, a, b, c, d, e, f)
188 char    code;
189 char   *what,
190        *fmt,
191        *a,
192        *b,
193        *c,
194        *d,
195        *e,
196        *f;
197 {
198     int     eindex = errno;
199     register char  *bp;
200     char    buffer[BUFSIZ * 2];
201
202     (void) sprintf (buffer, fmt, a, b, c, d, e, f);
203     bp = buffer + strlen (buffer);
204     if (what) {
205         if (*what) {
206             (void) sprintf (bp, " %s: ", what);
207             bp += strlen (bp);
208         }
209         if (eindex > 0 && eindex < sys_nerr)
210             (void) strcpy (bp, sys_errlist[eindex]);
211         else
212             (void) sprintf (bp, "Error %d", eindex);
213         bp += strlen (bp);
214     }
215
216     return rc2peer (code, bp - buffer, buffer);
217 }
218
219 /* \f */
220
221 /* VARARGS2 */
222
223 static  int     rclose (rc, fmt, a, b, c, d, e, f)
224 register struct record *rc;
225 char   *fmt,
226        *a,
227        *b,
228        *c,
229        *d,
230        *e,
231        *f;
232 {
233     static char buffer[BUFSIZ * 2];
234
235     (void) sprintf (buffer, fmt, a, b, c, d, e, f);
236
237     rc -> rc_len = strlen (rc -> rc_data = getcpy (buffer));
238     return (rc -> rc_type = RC_XXX);
239 }