|
|
1.1 root 1: char *protv = "C-Kermit Protocol Module 4C(029), 11 Jul 85"; /* -*-C-*- */
2:
3: /* C K C P R O -- C-Kermit Protocol Module, in Wart preprocessor notation. */
4: /*
5: Authors: Frank da Cruz (SY.FDC@CU20B), Bill Catchings, Jeff Damens;
6: Columbia University Center for Computing Activities, January 1985.
7: Copyright (C) 1985, Trustees of Columbia University in the City of New York.
8: Permission is granted to any individual or institution to use, copy, or
9: redistribute this software so long as it is not sold for profit, provided this
10: copyright notice is retained.
11: */
12: #include "ckcdeb.h"
13: #include "ckcker.h"
14: /*
15: Note -- This file may also be preprocessed by the Unix Lex program, but
16: you must indent the above #include statements before using Lex, and then
17: restore them to the left margin in the resulting C program before compilation.
18: Also, the invocation of the "wart()" function below must be replaced by an
19: invocation of the "yylex()" function. It might also be necessary to remove
20: comments in the %%...%% section.
21: */
22:
23: /* State definitions for Wart (or Lex) */
24: %states ipkt rfile rdata ssinit ssfile ssdata sseof sseot
25: %states serve generic get rgen
26:
27: /* External C-Kermit variable declarations */
28: extern char sstate, *versio, *srvtxt, *cmarg, *cmarg2;
29: extern char data[], filnam[], srvcmd[], ttname[], *srvptr;
30: extern int pktnum, timint, nfils, image, hcflg, xflg, speed, flow, mdmtyp;
31: extern int prvpkt, cxseen, czseen, server, local, displa, bctu, bctr, quiet;
32: extern int tsecs;
33: extern int putsrv(), puttrm(), putfil(), errpkt();
34: extern char *DIRCMD, *DELCMD, *TYPCMD, *SPACMD, *SPACM2, *WHOCMD;
35:
36: /* Local variables */
37: static char vstate = 0; /* Saved State */
38: static char vcmd = 0; /* Saved Command */
39: int x; /* General-purpose integer */
40: char *s; /* General-purpose string pointer */
41:
42: /* Macros - Note, BEGIN is predefined by Wart (and Lex) */
43: #define SERVE tinit(); BEGIN serve
44: #define RESUME if (server) { SERVE; } else return
45:
46: %%
47: /* Protocol entry points, one for each start state (sstate) */
48:
49: s { tinit(); /* Do Send command */
50: if (sinit()) BEGIN ssinit;
51: else RESUME; }
52:
53: v { tinit(); sleep(1); nack(); BEGIN get; } /* Receive */
54: r { tinit(); vstate = get; vcmd = 0; sipkt('I'); BEGIN ipkt; } /* Get */
55: c { tinit(); vstate = rgen; vcmd = 'C'; sipkt('I'); BEGIN ipkt; } /* Host */
56: g { tinit(); vstate = rgen; vcmd = 'G'; sipkt('I'); BEGIN ipkt; } /* Generic */
57:
58: x { sleep(1); SERVE; } /* Be a Server */
59:
60: a { errpkt("User cancelled transaction"); /* "Abort" -- Tell other side. */
61: x = quiet; quiet = 1; /* Close files silently. */
62: clsif(); clsof(1);
63: quiet = x; return(0); } /* Return from protocol. */
64:
65: /* Dynamic states: <current-states>input-character { action } */
66:
67: <rgen,get,serve>S { rinit(data); bctu = bctr; /* Get Send-Init */
68: rtimer(); BEGIN rfile; }
69:
70: <ipkt>Y { spar(data); /* Get ack for I-packet */
71: if (vcmd) { scmd(vcmd,cmarg); vcmd = 0; }
72: if (vstate == get) srinit();
73: BEGIN vstate; }
74:
75: <ipkt>E { if (vcmd) scmd(vcmd,cmarg); /* Get E for I-packet (ignore) */
76: vcmd = 0; if (vstate == get) srinit();
77: BEGIN vstate; }
78:
79: <serve>R { srvptr = srvcmd; decode(data,putsrv); /* Get Receive-Init */
80: cmarg = srvcmd; nfils = -1;
81: if (sinit()) BEGIN ssinit; else { SERVE; } }
82:
83: <serve>I { spar(data); rpar(data); ack1(data); /* Get Init Parameters */
84: pktnum = 0; prvpkt = -1; }
85:
86: <serve>G { srvptr = srvcmd; decode(data,putsrv); /* Get & decode command. */
87: putsrv('\0'); putsrv('\0');
88: sstate = srvcmd[0]; BEGIN generic; }
89:
90: <serve>C { srvptr = srvcmd; /* Get command for shell */
91: decode(data,putsrv); putsrv('\0');
92: if (syscmd(srvcmd,"")) BEGIN ssinit;
93: else { errpkt("Can't do system command"); SERVE; } }
94:
95: <serve>. { errpkt("Unimplemented server function"); SERVE; } /* Other */
96:
97: <generic>C { if (!cwd(srvcmd+1)) errpkt("Can't change directory"); /* CWD */
98: SERVE; }
99:
100: <generic>D { if (syscmd(DIRCMD,srvcmd+2)) BEGIN ssinit; /* Directory */
101: else { errpkt("Can't list directory"); SERVE; } }
102:
103: <generic>E { if (syscmd(DELCMD,srvcmd+2)) BEGIN ssinit; /* Erase */
104: else { errpkt("Can't remove file"); SERVE; } }
105:
106: <generic>F { ack(); screen(SCR_TC,0,0l,""); return(0); } /* Finish and Bye */
107: <generic>L { ack(); ttres(); screen(SCR_TC,0,0l,""); return(zkself()); }
108:
109: <generic>H { if (sndhlp()) BEGIN ssinit;
110: else { errpkt("Can't send help"); SERVE; } }
111:
112: <generic>T { if (syscmd(TYPCMD,srvcmd+2)) BEGIN ssinit;
113: else { errpkt("Can't type file"); SERVE; } }
114:
115: <generic>U { x = *(srvcmd+1); /* Disk Usage query */
116: x = ((x == '\0') || (x == unchar(0)));
117: x = (x ? syscmd(SPACMD,"") : syscmd(SPACM2,srvcmd+2));
118: if (x) BEGIN ssinit; else { errpkt("Can't check space"); SERVE; }}
119:
120: <generic>W { if (syscmd(WHOCMD,srvcmd+2)) BEGIN ssinit;
121: else { errpkt("Can't do who command"); SERVE; } }
122:
123: <generic>. { errpkt("Unimplemented generic server function"); SERVE; }
124:
125: /* Dynamic states, cont'd */
126:
127:
128: <rgen>Y { decode(data,puttrm); RESUME; } /* Got reply in ACK data */
129:
130: <rgen,rfile>F { if (rcvfil()) { ack1(filnam); BEGIN rdata; } /* File header */
131: else { errpkt("Can't open file"); RESUME; } }
132:
133: <rgen,rfile>X { opent(); ack(); BEGIN rdata; } /* Screen data is coming */
134:
135: <rfile>B { ack(); tsecs = gtimer(); reot(); RESUME; } /* Got EOT */
136:
137: <rdata>D { if (cxseen) ack1("X"); /* Got data. */
138: else if (czseen) ack1("Z");
139: else ack();
140: decode(data,putfil); }
141:
142: <rdata>Z { if (reof() < 0) { /* Got End Of File */
143: errpkt("Can't close file"); RESUME;
144: } else { ack(); BEGIN rfile; } }
145:
146: <ssinit>Y { spar(data); bctu = bctr; /* Got ACK to Send-Init */
147: x = sfile(xflg); /* Send X or F header packet */
148: if (x) { rtimer(); BEGIN ssfile; }
149: else { s = xflg ? "Can't execute command" : "Can't open file";
150: errpkt(s); RESUME; }
151: }
152:
153: <ssfile>Y { srvptr = srvcmd; /* Got ACK to F */
154: decode(data,putsrv); putsrv('\0');
155: if (*srvcmd) tlog(F110," stored as",srvcmd,0);
156: if (sdata() < 0) { clsif(); seof(""); BEGIN sseof; }
157: else BEGIN ssdata; }
158:
159: <ssdata>Y { if (canned(data)) { clsif(); seof("D"); BEGIN sseof; }
160: else if (sdata() < 0) { clsif(); seof(""); BEGIN sseof; } }
161:
162: <sseof>Y { if (gnfile() > 0) { /* Got ACK to EOF, get next file */
163: if (sfile(xflg)) BEGIN ssdata;
164: else { errpkt("Can't open file") ; RESUME; }
165: } else { /* If no next file, EOT */
166: tsecs = gtimer();
167: seot();
168: BEGIN sseot; }
169: }
170:
171: <sseot>Y { RESUME; } /* Got ACK to EOT */
172:
173: E { ermsg(data); /* Error packet, issue message. */
174: x = quiet; quiet = 1; /* Close files silently, */
175: clsif(); clsof(1); /* discarding any output file. */
176: tsecs = gtimer();
177: quiet = x; RESUME; }
178:
179: . { errpkt("Unknown packet type"); RESUME; } /* Anything else, send error */
180: %%
181:
182: /* P R O T O -- Protocol entry function */
183:
184: proto() {
185:
186: extern int sigint();
187: int x;
188:
189: conint(sigint); /* Enable console interrupts */
190:
191: /* Set up the communication line for file transfer. */
192:
193: if (local && (speed < 0)) {
194: screen(SCR_EM,0,0l,"Sorry, you must 'set speed' first");
195: return;
196: }
197:
198: x = -1;
199: if (ttopen(ttname,&x,mdmtyp) < 0) {
200: debug(F111,"proto ttopen local",ttname,local);
201: screen(SCR_EM,0,0l,"Can't open line");
202: return;
203: }
204: if (x > -1) local = x;
205: debug(F111,"proto ttopen local",ttname,local);
206:
207: x = (local) ? speed : -1;
208: if (ttpkt(x,flow) < 0) { /* Put line in packet mode, */
209: screen(SCR_EM,0,0l,"Can't condition line");
210: return;
211: }
212: if (sstate == 'x') { /* If entering server mode, */
213: server = 1; /* set flag, */
214: if (!quiet) {
215: if (!local) /* and issue appropriate message. */
216: conol(srvtxt);
217: else {
218: conol("Entering server mode on ");
219: conoll(ttname);
220: }
221: }
222: } else server = 0;
223: sleep(1);
224:
225: /*
226: The 'wart()' function is generated by the wart program. It gets a
227: character from the input() routine and then based on that character and
228: the current state, selects the appropriate action, according to the state
229: table above, which is transformed by the wart program into a big case
230: statement. The function is active for one transaction.
231: */
232:
233: wart(); /* Enter the state table switcher. */
234:
235: if (server) { /* Back from packet protocol. */
236: server = 0;
237: if (!quiet) /* Give appropriate message */
238: conoll("C-Kermit server done");
239: }
240: screen(SCR_TC,0,0l,""); /* Transaction complete */
241: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.