|
|
1.1 root 1: #include <pads.pri>
2: #include <CC/sys/types.h>
3: #include <sgtty.h>
4:
5: void Remote::checkproto(int p) { if( get()!=p ) err(); }
6: void Remote::put(char c) { writebuffer[pktbase + pktsize++] = c; }
7: void Remote::proto(int p) { put( p ); }
8:
9: long Remote::rcvlong() { return (long) shiftin( P_LONG ); }
10: short Remote::rcvshort() { return (short) shiftin( P_SHORT ); }
11: unsigned char Remote::rcvuchar() { return (unsigned char) shiftin(P_UCHAR); }
12:
13: void Remote::sendlong(long x) { shiftout( P_LONG, x ); }
14: void Remote::sendshort(short x) { shiftout( P_SHORT, (long) x ); }
15: void Remote::senduchar(unsigned char x) { shiftout( P_UCHAR, (long) x ); }
16:
17: void Remote::pktstart(char c) { if( pktsize ) err(); put(c); }
18: void Remote::pktflush() { writesize = 0; pktend(); }
19:
20: #define ADDRBITS 24
21: #define ALIGNBITS 2
22: #define OIDBITS (32-(ADDRBITS-ALIGNBITS))
23: #define MASK(n) ((1<<n)-1)
24: void Remote::sendobj(PadRcv *o)
25: {
26: if( !o ){
27: sendlong(0);
28: return;
29: }
30: long l = (long) o;
31: if( l & (~MASK(ADDRBITS)^MASK(ALIGNBITS)) ) abort();
32: sendlong( (l<<(OIDBITS-ALIGNBITS)) | (o->oid&MASK(OIDBITS)) );
33: }
34:
35: PadRcv *Remote::rcvobj()
36: {
37: register long o = rcvlong(), a;
38: a = o >> (OIDBITS-ALIGNBITS);
39: a &= MASK(ADDRBITS)&~MASK(ALIGNBITS);
40: PadRcv *obj = (PadRcv*) a;
41: if( obj && (obj->oid&MASK(OIDBITS)) != (o&MASK(OIDBITS)) )
42: obj = 0;
43: return obj;
44: }
45:
46: void Remote::err(char *e)
47: {
48: if( !e ) e = "host/term protocol error";
49: write(fd, e, strlen(e));
50: }
51:
52: Remote::Remote(int opened)
53: {
54: fd = opened;
55: pktsize = writesize = pktbase = 0;
56: }
57:
58: Remote::Remote(char *dev)
59: {
60: struct sgttyb tty;
61:
62: fd = open(dev, 2);
63: pktsize = writesize = pktbase = 0;
64:
65: trace( "%d.Remote(%d,%d)", this, in, out );
66: if( ioctl(fd, TIOCGETP, &tty) ) err("tty ioctl: GETP failed");
67: tty.sg_flags = (tty.sg_flags|CBREAK|RAW) & ~ECHO;
68: if( ioctl(fd, TIOCSETP, &tty) ) err("tty ioctl: SETP failed");
69: trace( "sg_flags=0x%X", tty.sg_flags );
70: if( ioctl(fd, TIOCEXCL, 0) ) err("tty ioctl: EXCL failed");
71: }
72:
73: void Remote::share()
74: {
75: trace( "%d.share()", this, );
76: if( ioctl(fd, TIOCNXCL, 0) ) err("tty ioctl: NXCL failed");
77: }
78:
79: long Remote::shiftin(register int bytes)
80: {
81: register long shifter = 0;
82:
83: checkproto( bytes );
84: while( bytes-- ) shifter = (shifter<<8) + (get()&0xFF);
85: return shifter;
86: }
87:
88: void Remote::shiftout( register bytes, register long shifter )
89: {
90: proto( bytes );
91: do { put( (char)(shifter>>( (--bytes)*8 )) ); } while( bytes );
92: }
93:
94: long BytesToTerm;
95: void Remote::pktend()
96: {
97: if( pktsize<=0 ) err();
98: pktbase += pktsize;
99: if( pktbase > writesize ){
100: if( write(fd, (char*)writebuffer, pktbase) != pktbase ) abort();
101: BytesToTerm += pktbase;
102: pktbase = 0;
103: writesize = 768;
104: }
105: pktsize = 0;
106: }
107:
108: char *Remote::rcvstring( char *s0 )
109: {
110: register char *s = s0;
111: register unsigned char len;
112:
113: checkproto( P_STRING );
114: len = rcvuchar();
115: if( !s0 ) s = s0 = new char [len+1];
116: while( len-->0 ) *s++ = get();
117: *s = '\0';
118: return s0;
119: }
120:
121: void Remote::sendstring(register char *s)
122: {
123: int len;
124:
125: proto( P_STRING );
126: len = strlen(s);
127: if( len > 255 ) len = 255;
128: senduchar( len );
129: while( len-- ) put(*s++);
130: }
131:
132: long BytesFromTerm;
133: int Remote::get()
134: {
135: char c;
136: if( pktsize!=0 || read(fd, &c, 1)!=1 ) err();
137: ++BytesFromTerm;
138: return c;
139: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.