File: vmhsbr.c

package info (click to toggle)
mh 6.8.4-28
  • links: PTS
  • area: main
  • in suites: slink
  • size: 7,012 kB
  • ctags: 7,496
  • sloc: ansic: 75,211; sh: 3,112; lisp: 2,205; ml: 1,894; makefile: 724; perl: 482; csh: 150; tcl: 66; sed: 43; awk: 7
file content (239 lines) | stat: -rw-r--r-- 4,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* vmhsbr.c - routines to help vmh along */
#ifndef	lint
static char ident[] = "@(#)$Id: vmhsbr.c,v 1.11 1993/08/25 17:29:53 jromine Exp $";
#endif	/* lint */

/* TODO (for vrsn 2):
	INI: include width of windows
 */

#include "../h/mh.h"
#include "../h/vmhsbr.h"
#include <stdio.h>

/*  */

static char *types[] = {
    "OK",
    "INI", "ACK", "ERR", "CMD", "QRY", "TTY", "WIN", "DATA", "EOF", "FIN",
    "XXX", NULL
};

static	FILE *fp = NULL;

static	int PEERrfd = NOTOK;
static	int PEERwfd = NOTOK;


extern int  errno;
#if !defined( BSD44 ) && !defined( linux )
extern int  sys_nerr;
extern char *sys_errlist[];
#endif

static int	rclose();
/*  */

int	rcinit (rfd, wfd)
int	rfd,
	wfd;
{
    char   *cp,
            buffer[BUFSIZ];

    PEERrfd = rfd;
    PEERwfd = wfd;

    if ((cp = getenv ("MHVDEBUG")) && *cp) {
	(void) sprintf (buffer, "%s.out", invo_name);
	if (fp = fopen (buffer, "w")) {
	  (void) fseek (fp, 0L, 2);
	  fprintf (fp, "%d: rcinit (%d, %d)\n", getpid (), rfd, wfd);
	  (void) fflush (fp);
	}
    }

    return OK;
}


int     rcdone () {
    if (PEERrfd != NOTOK)
	(void) close (PEERrfd);
    if (PEERwfd != NOTOK)
	(void) close (PEERwfd);

    if (fp) {
	(void) fclose (fp);
	fp = NULL;
    }
    return OK;
}

/*  */

int	rc2rc (code, len, data, rc)
char	code;
int	len;
char   *data;
struct record *rc;
{
    if (rc2peer (code, len, data) == NOTOK)
	return NOTOK;

    return peer2rc (rc);
}


int	str2rc (code, str, rc)
char	code;
char   *str;
struct record *rc;
{
    return rc2rc (code, str ? strlen (str) : 0, str, rc);
}

/*  */

int	peer2rc (rc)
register struct	record *rc;
{
    if (rc -> rc_data)
	free (rc -> rc_data);

    if (read (PEERrfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
	return rclose (rc, "read from peer lost(1)");
    if (rc -> rc_len) {
	if ((rc -> rc_data = malloc ((unsigned) rc -> rc_len + 1)) == NULL)
	    return rclose (rc, "malloc of %d lost", rc -> rc_len + 1);
	if (read (PEERrfd, rc -> rc_data, rc -> rc_len) != rc -> rc_len)
	    return rclose (rc, "read from peer lost(2)");
	rc -> rc_data[rc -> rc_len] = 0;
    }
    else
	rc -> rc_data = NULL;

    if (fp) {
	(void) fseek (fp, 0L, 2);
	fprintf (fp, "%d: <--- %s %d: \"%*.*s\"\n", getpid (),
		types[rc -> rc_type], rc -> rc_len,
		rc -> rc_len, rc -> rc_len, rc -> rc_data);
	(void) fflush (fp);
    }

    return rc -> rc_type;
}

/*  */

int	rc2peer (code, len, data)
char	code;
int	len;
char   *data;
{
    struct record   rcs;
    register struct record *rc = &rcs;

    rc -> rc_type = code;
    rc -> rc_len = len;

    if (fp) {
	(void) fseek (fp, 0L, 2);
	fprintf (fp, "%d: ---> %s %d: \"%*.*s\"\n", getpid (),
		types[rc -> rc_type], rc -> rc_len,
		rc -> rc_len, rc -> rc_len, data);
	(void) fflush (fp);
    }

    if (write (PEERwfd, (char *) rc_head (rc), RHSIZE (rc)) != RHSIZE (rc))
	return rclose (rc, "write to peer lost(1)");

    if (rc -> rc_len)
	if (write (PEERwfd, data, rc -> rc_len) != rc -> rc_len)
	    return rclose (rc, "write to peer lost(2)");

    return OK;
}

/*  */

int	str2peer (code, str)
char	code;
char   *str;
{
    return rc2peer (code, str ? strlen (str) : 0, str);
}


/* VARARGS2 */

int	fmt2peer (code, fmt, a, b, c, d, e, f)
char	code;
char   *fmt,
       *a,
       *b,
       *c,
       *d,
       *e,
       *f;
{
    return err2peer (code, NULLCP, fmt, a, b, c, d, e, f);
}

/*  */

/* VARARGS3 */

int	err2peer (code, what, fmt, a, b, c, d, e, f)
char	code;
char   *what,
       *fmt,
       *a,
       *b,
       *c,
       *d,
       *e,
       *f;
{
    int     eindex = errno;
    register char  *bp;
    char    buffer[BUFSIZ * 2];

    (void) sprintf (buffer, fmt, a, b, c, d, e, f);
    bp = buffer + strlen (buffer);
    if (what) {
	if (*what) {
	    (void) sprintf (bp, " %s: ", what);
	    bp += strlen (bp);
	}
	if (eindex > 0 && eindex < sys_nerr)
	    (void) strcpy (bp, sys_errlist[eindex]);
	else
	    (void) sprintf (bp, "Error %d", eindex);
	bp += strlen (bp);
    }

    return rc2peer (code, bp - buffer, buffer);
}

/*  */

/* VARARGS2 */

static	int	rclose (rc, fmt, a, b, c, d, e, f)
register struct record *rc;
char   *fmt,
       *a,
       *b,
       *c,
       *d,
       *e,
       *f;
{
    static char buffer[BUFSIZ * 2];

    (void) sprintf (buffer, fmt, a, b, c, d, e, f);

    rc -> rc_len = strlen (rc -> rc_data = getcpy (buffer));
    return (rc -> rc_type = RC_XXX);
}