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