home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit4f / ckcmai.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  15KB  |  384 lines

  1. char *versio = "C-Kermit, 4F(095) 31 Aug 89";
  2.  
  3. /*  C K C M A I  --  C-Kermit Main program  */
  4.  
  5. /*
  6.  Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  7.  Columbia University Center for Computing Activities.
  8.  First released January 1985.
  9.  Copyright (C) 1985, 1989, Trustees of Columbia University in the City of New 
  10.  York.  Permission is granted to any individual or institution to use, copy, or
  11.  redistribute this software so long as it is not sold for profit, provided this
  12.  copyright notice is retained. 
  13. */
  14. /*
  15.  The Kermit file transfer protocol was developed at the Columbia University
  16.  Center for Computing Activities (CUCCA).  It is named after Kermit the Frog,
  17.  star of the television series THE MUPPET SHOW; the name is used by permission
  18.  of Henson Associates, Inc.  "Kermit" is also Celtic for "free".
  19. */
  20. /*
  21.  Thanks to Herm Fischer of Encino CA for extensive contributions to version
  22.  4.0, and to the following people for their contributions over the years:
  23.  
  24.    Chris Adie, Edinburgh U, Scotland (OS/2 support)
  25.    Larry Afrin, Clemson U
  26.    Barry Archer, U of Missouri
  27.    Robert Andersson, Oslo, Norway
  28.    Fuat Baran, CUCCA
  29.    Stan Barber, Rice U
  30.    Charles Brooks, EDN
  31.    Mike Brown, Purdue U
  32.    Mark Buda, DEC
  33.    Bill Catchings, formerly of CUCCA
  34.    Bob Cattani, Columbia U CS Dept
  35.    Howard Chu, U of Michigan
  36.    Bill Coalson, McDonnell Douglas
  37.    Alan Crosswell, CUCCA
  38.    Jeff Damens, formerly of CUCCA
  39.    Mark Davies, Bath U, UK
  40.    Joe R. Doupnik, Utah State U
  41.    John R. Evans, IRS, Kansas City
  42.    Glenn Everhart, RCA Labs
  43.    Carl Fongheiser, CWRU
  44.    Christine Gianone, CUCCA
  45.    John Gilmore, UC Berkeley
  46.    Yekta Gursel, MIT
  47.    Jim Guyton, Rand Corp
  48.    Marion Kananson, hakanson@cs.orst.edu
  49.    Stan Hanks, Rice U.
  50.    Michael Haertel
  51.    Ken Harrenstein, SRI
  52.    Chuck Hedrick, Rutgers U
  53.    Ron Heiby, Motorola Micromputer Division
  54.    Steve Hemminger, Tektronix
  55.    Randy Huntziger, National Library of Medicine
  56.    Phil Julian, SAS Institute
  57.    Howie Kaye, CUCCA
  58.    Jim Knutson, U of Texas at Austin
  59.    Bo Kullmar, Kista, Sweden
  60.    John Kunze, UC Berkeley
  61.    Bob Larson, USC (OS-9 support)
  62.    David Lawyer, UC Irvine
  63.    S.O. Lidie, Lehigh U
  64.    David MacKenzie, Environmental Defense Fund, Rockefeller U
  65.    Martin Maclaren, Bath U, UK
  66.    Chris Maio, Columbia U CS Dept
  67.    Peter Mauzey, AT&T
  68.    Leslie Mikesall, American Farm Bureau
  69.    Martin Minow, DEC
  70.    Ray Moody, Purdue U
  71.    Tony Movshon, NYU
  72.    Dan Murphy, ???
  73.    Jim Noble, Planning Research Corporation
  74.    Ian O'Brien, Bath U, UK
  75.    Paul Placeway, Ohio State U
  76.    Ken Poulton, HP Labs
  77.    Frank Prindle, NADC
  78.    Anton Rang, ???
  79.    Scott Ribe, ???
  80.    Jack Rouse, SAS Institute
  81.    Stew Rubenstein, Harvard U
  82.    Dan Schullman, DEC
  83.    Gordon Scott, Micro Focus, Newbury UK
  84.    David Sizeland, U of London Medical School
  85.    Bradley Smith, UCLA
  86.    Andy Tanenbaum, Vrije U, Amsterdam, Netherlands
  87.    Markku Toijala, Helsinki U of Technology
  88.    Dave Tweten, AMES-NAS
  89.    Walter Underwood, Ford Aerospace
  90.    Pieter Van Der Linden, Centre Mondial, Paris
  91.    Ge van Geldorp, Netherlands
  92.    Wayne Van Pelt, GE/CRD
  93.    Mark Vasoll & Gregg Wonderly, Oklahoma State U
  94.    Paul Vixie (DEC)
  95.    Stephen Walton, Calif State U, Northridge
  96.    Lauren Weinstein
  97.    Joachim Wiesel, U of Karlsruhe
  98.    Michael Williams, UCLA
  99.    Patrick Wolfe, Kuck & Associates, Inc.
  100.    Farrell Woods, Concurrent (formerly Masscomp)
  101.    Dave Woolley, CAP Communication Systems, London
  102.    John Zeeff, Ann Arbor, MI
  103. */
  104.  
  105. #include "ckcsym.h"            /* Macintosh once needed this */
  106. #include "ckcker.h"            /* Kermit symbols */
  107. #include "ckcdeb.h"            /* Debug & other symbols */
  108.  
  109. /* Text message definitions.. each should be 256 chars long, or less. */
  110. #ifdef MAC
  111. char *hlptxt = "\r\
  112. MacKermit Server Commands:\r\
  113. \r\
  114.     BYE\r\
  115.     FINISH\r\
  116.     GET filespec\r\
  117.     REMOTE CWD directory\r\
  118.     REMOTE HELP\r\
  119.     SEND filespec\r\
  120. \r\0";
  121. #else
  122. #ifdef AMIGA
  123. char *hlptxt = "C-Kermit Server Commands:\n\
  124. \n\
  125. GET filespec, SEND filespec, FINISH, BYE, REMOTE HELP\n\
  126. \n\0";
  127. #else
  128. #ifdef OS2
  129. char *hlptxt = "C-Kermit Server REMOTE Commands:\n\
  130. \n\
  131. GET files  REMOTE CWD [dir]    REMOTE DIRECTORY [files]\n\
  132. SEND files REMOTE SPACE [dir]  REMOTE HOST command\n\
  133. FINISH     REMOTE DELETE files REMOTE TYPE files\n\
  134. BYE        REMOTE HELP\n\
  135. \n\0";
  136. #else
  137. char *hlptxt = "C-Kermit Server REMOTE Commands:\n\
  138. \n\
  139. GET files  REMOTE CWD [dir]    REMOTE DIRECTORY [files]\n\
  140. SEND files REMOTE SPACE [dir]  REMOTE HOST command\n\
  141. MAIL files REMOTE DELETE files REMOTE WHO [user]\n\
  142. BYE        REMOTE PRINT files  REMOTE TYPE files\n\
  143. FINISH     REMOTE HELP\n\
  144. \n\0";
  145. #endif
  146. #endif
  147. #endif
  148.  
  149. #ifdef OSK
  150. char *srvtxt = "\r\l\
  151. C-Kermit server starting.  Return to your local machine by typing\r\l\
  152. its escape sequence for closing the connection, and issue further\r\l\
  153. commands from there.  To shut down the C-Kermit server, issue the\r\l\
  154. FINISH or BYE command and then reconnect.\r\l\
  155. \l\0";
  156. #else
  157. char *srvtxt = "\r\n\
  158. C-Kermit server starting.  Return to your local machine by typing\r\n\
  159. its escape sequence for closing the connection, and issue further\r\n\
  160. commands from there.  To shut down the C-Kermit server, issue the\r\n\
  161. FINISH or BYE command and then reconnect.\n\
  162. \r\n\0";
  163. #endif
  164.  
  165. /* Declarations for Send-Init Parameters */
  166.  
  167. int spsiz = DSPSIZ,                     /* curent packet size to send */
  168.     spmax = DSPSIZ,        /* (PWP) Biggest packet size we can send */
  169.                                 /* (see rcalcpsz()) */
  170.     spsizf = 0,                         /* Flag to override what you ask for */
  171.     rpsiz = DRPSIZ,                     /* Biggest we want to receive */
  172.     urpsiz = DRPSIZ,            /* User-requested rpsiz */
  173.     maxrps = MAXRP,            /* Maximum incoming long packet size */
  174.     maxsps = MAXSP,            /* Maximum outbound l.p. size */
  175.     maxtry = MAXTRY,            /* Maximum retries per packet */
  176.     wslots = 1,                /* Window size */
  177.     timint = DMYTIM,                    /* Timeout interval I use */
  178.     srvtim = DSRVTIM,            /* Server command wait timeout */
  179.     rtimo = URTIME,                     /* Timeout I want you to use */
  180.     timef = 0,                          /* Flag to override what you ask */
  181.     npad = MYPADN,                      /* How much padding to send */
  182.     mypadn = MYPADN,                    /* How much padding to ask for */
  183.     bctr = 1,                           /* Block check type requested */
  184.     bctu = 1,                           /* Block check type used */
  185.     ebq =  MYEBQ,                       /* 8th bit prefix */
  186.     ebqflg = 0,                         /* 8th-bit quoting flag */
  187.     rqf = -1,                /* Flag used in 8bq negotiation */
  188.     rq = 0,                /* Received 8bq bid */
  189.     sq = 'Y',                /* Sent 8bq bid */
  190.     rpt = 0,                            /* Repeat count */
  191.     rptq = MYRPTQ,                      /* Repeat prefix */
  192.     rptflg = 0;                         /* Repeat processing flag */
  193.  
  194. int capas = 10,                /* Position of Capabilities */
  195.     atcapb = 8,                /* Attribute capability */
  196.     atcapr = 1,                /*  requested */
  197.     atcapu = 0,                /*  used */
  198.     swcapb = 4,                /* Sliding Window capability */
  199.     swcapr = 0,                /*  requested */
  200.     swcapu = 0,                /*  used */
  201.     lpcapb = 2,                /* Long Packet capability */
  202.     lpcapr = 1,                /*  requested */
  203.     lpcapu = 0;                /*  used */
  204.  
  205. CHAR padch = MYPADC,                    /* Padding character to send */
  206.     mypadc = MYPADC,                    /* Padding character to ask for */
  207.     seol = MYEOL,                       /* End-Of-Line character to send */
  208.     eol = MYEOL,                        /* End-Of-Line character to look for */
  209.     ctlq = CTLQ,                        /* Control prefix in incoming data */
  210.     myctlq = CTLQ;                      /* Outbound control character prefix */
  211.  
  212. struct zattr iattr;            /* Incoming file attributes */
  213.  
  214. /* Packet-related variables */
  215.  
  216. int pktnum = 0,                         /* Current packet number */
  217.     prvpkt = -1,                        /* Previous packet number */
  218.     sndtyp,                             /* Type of packet just sent */
  219.     rsn,                /* Received packet sequence number */
  220.     rln,                /* Received packet length */
  221.     size,                               /* Current size of output pkt data */
  222.     osize,                              /* Previous output packet data size */
  223.     maxsize,                            /* Max size for building data field */
  224.     spktl = 0;                /* Length packet being sent */
  225.  
  226. CHAR sndpkt[MAXSP+100],                 /* Entire packet being sent */
  227.     recpkt[MAXRP+200],                  /* Packet most recently received */
  228.     *rdatap,                /* Pointer to received packet data */
  229.     data[MAXSP+4],            /* Packet data buffer */
  230.     srvcmd[MAXRP+4],                    /* Where to decode server command */
  231.     *srvptr,                            /* Pointer to above */
  232.     mystch = SOH,                       /* Outbound packet-start character */
  233.     stchr = SOH;                        /* Incoming packet-start character */
  234.  
  235. /* File-related variables */
  236.  
  237. #ifdef datageneral
  238. CHAR filnam[256];                       /* Name of current file. */
  239. #else
  240. CHAR filnam[50];                        /* Name of current file. */
  241. #endif
  242.  
  243. int nfils;                              /* Number of files in file group */
  244. long fsize;                             /* Size of current file */
  245.  
  246. /* Communication line variables */
  247.  
  248. CHAR ttname[50];                        /* Name of communication line. */
  249.  
  250. int parity,                             /* Parity specified, 0,'e','o',etc */
  251.     flow,                               /* Flow control, 1 = xon/xoff */
  252.     speed = -1,                         /* Line speed */
  253.     turn = 0,                           /* Line turnaround handshake flag */
  254.     turnch = XON,                       /* Line turnaround character */
  255.     duplex = 0,                         /* Duplex, full by default */
  256. #ifdef OS2
  257.     escape = 035,                       /* Escape character for connect */
  258. #else
  259.     escape = 034,                       /* Escape character for connect */
  260. #endif
  261.     delay = DDELAY,                     /* Initial delay before sending */
  262.     mdmtyp = 0;                         /* Modem type (initially none)  */
  263.  
  264.     int tlevel = -1;            /* Take-file command level */
  265.  
  266. /* Statistics variables */
  267.  
  268. long filcnt,                    /* Number of files in transaction */
  269.     flci,                       /* Characters from line, current file */
  270.     flco,                       /* Chars to line, current file  */
  271.     tlci,                       /* Chars from line in transaction */
  272.     tlco,                       /* Chars to line in transaction */
  273.     ffc,                        /* Chars to/from current file */
  274.     tfc;                        /* Chars to/from files in transaction */
  275.  
  276. int tsecs;                      /* Seconds for transaction */
  277.  
  278. /* Flags */
  279.  
  280. int deblog = 0,                         /* Flag for debug logging */
  281.     pktlog = 0,                         /* Flag for packet logging */
  282.     seslog = 0,                         /* Session logging */
  283.     tralog = 0,                         /* Transaction logging */
  284.     displa = 0,                         /* File transfer display on/off */
  285.     stdouf = 0,                         /* Flag for output to stdout */
  286.     xflg   = 0,                         /* Flag for X instead of F packet */
  287.     hcflg  = 0,                         /* Doing Host command */
  288.     fncnv  = 1,                         /* Flag for file name conversion */
  289.     binary = 0,                         /* Flag for binary file */
  290.     savmod = 0,                         /* Saved file mode (whole session) */
  291.     bsave  = 0,                /* Saved file mode (per file) */
  292.     bsavef = 0,                /* Flag if bsave was used. */
  293.     cmask  = 0177,            /* Connect byte mask */
  294.     fmask  = 0377,            /* File byte mask */
  295.     warn   = 0,                         /* Flag for file warning */
  296.     quiet  = 0,                         /* Be quiet during file transfer */
  297.     local  = 0,                         /* Flag for external tty vs stdout */
  298.     server = 0,                         /* Flag for being a server */
  299.     cnflg  = 0,                         /* Connect after transaction */
  300.     cxseen = 0,                         /* Flag for cancelling a file */
  301.     czseen = 0,                         /* Flag for cancelling file group */
  302.     keep = 0,                           /* Keep incomplete files */
  303.     nakstate = 0;            /* In a state where we can send NAKs */
  304.  
  305. /* Variables passed from command parser to protocol module */
  306.  
  307. char parser();                          /* The parser itself */
  308. char sstate  = 0;                       /* Starting state for automaton */
  309. char *cmarg  = "";                      /* Pointer to command data */
  310. char *cmarg2 = "";                      /* Pointer to 2nd command data */
  311. char **cmlist;                          /* Pointer to file list in argv */
  312.  
  313. /* Miscellaneous */
  314.  
  315. char **xargv;                           /* Global copies of argv */
  316. int  xargc;                             /* and argc  */
  317.  
  318. extern char *dftty;                     /* Default tty name from ckx???.c */
  319. extern int dfloc;                       /* Default location: remote/local */
  320. extern int dfprty;                      /* Default parity */
  321. extern int dfflow;                      /* Default flow control */
  322.  
  323. /* (PWP) buffered input and output buffers; see ckcfns.c getpkt(). */
  324. CHAR zinbuffer[INBUFSIZE], zoutbuffer[INBUFSIZE];
  325. CHAR *zinptr, *zoutptr;
  326. int zincnt, zoutcnt;
  327.  
  328.  
  329. /*  M A I N  --  C-Kermit main program  */
  330.  
  331. #ifdef aegis
  332. /* On the Apollo, intercept main to insert a cleanup handler */
  333. ckcmai(argc,argv) int argc; char **argv; {
  334. #else
  335. main(argc,argv) int argc; char **argv; {
  336. #endif
  337.  
  338.     char *strcpy();
  339.  
  340. /* Do some initialization */
  341.  
  342.     xargc = argc;                       /* Make global copies of argc */
  343.     xargv = argv;                       /* ...and argv. */
  344.     sstate = 0;                         /* No default start state. */
  345.     if (sysinit() < 0) doexit(BAD_EXIT); /* System-dependent initialization. */
  346.     strcpy(ttname,dftty);               /* Set up default tty name. */
  347.     local = dfloc;                      /* And whether it's local or remote. */
  348.     parity = dfprty;                    /* Set initial parity, */
  349.     flow = dfflow;                      /* and flow control. */
  350.  
  351. /*** attempt to take ini file before doing command line ***/
  352.  
  353.     cmdini();                /* Sets tlevel */
  354.     while (tlevel > -1) {        /* Execute init file. */
  355.     sstate = parser();        /* Loop getting commands. */
  356.         if (sstate) proto();            /* Enter protocol if requested. */
  357.     }
  358.  
  359. /* Look for a UNIX-style command line... */
  360.  
  361.     if (argc > 1) {                     /* Command line arguments? */
  362.         sstate = cmdlin();              /* Yes, parse. */
  363.         if (sstate) {
  364.             proto();                    /* Take any requested action, then */
  365.             if (!quiet) conoll("");     /* put cursor back at left margin, */
  366.             if (cnflg) conect();        /* connect if requested, */
  367.             doexit(GOOD_EXIT);          /* and then exit with status 0. */
  368.         }
  369.     }
  370.  
  371. #ifdef OS2
  372.     if (speed==-1) speed = ttspeed();    /* Unless explicitly changed,
  373.                            use the current line speed */
  374. #endif
  375.  
  376. /* If no action requested on command line, enter interactive parser */
  377.  
  378.     herald();                /* Display program herald. */
  379.     while(1) {                /* Loop getting commands. */
  380.     sstate = parser();
  381.         if (sstate) proto();            /* Enter protocol if requested. */
  382.     }
  383. }
  384.