home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / CKPM5X_S.ZIP / CKCMAI.C < prev    next >
C/C++ Source or Header  |  1990-03-03  |  18KB  |  425 lines

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