home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / os9 / os9ker.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  12KB  |  284 lines

  1. /*
  2.  * os9ker.c  Os9 kermit main program
  3.  */
  4.  
  5. #define EXTERN  /* global variables are actually declared here */
  6. #include "os9inc.h"
  7.  
  8. /*
  9.  * K e r m i t  File Transfer Utility
  10.  *
  11.  * UNIX Kermit, Columbia University, 1981, 1982, 1983
  12.  * Bill Catchings, Bob Cattani, Chris Maio, Frank da Cruz, Alan Crosswell
  13.  *
  14.  *  Also:       Jim Guyton, Rand Corporation
  15.  *              Walter Underwood, Ford Aerospace
  16.  *              Glenn Seaton, Kennedy Space Center
  17.  *              Bradley Bosch,
  18.  *              Robert Larson, U. of Southern California <Blarson@usc-ecl>      
  19.  *              James Jones
  20.  *
  21.  *
  22.  *  usage:  kermit c [lbe line baud escapechar]         to connect
  23.  *          kermit s [d..iflb line baud] file ...       to send files
  24.  *          kermit r [d..iflb line baud]                to receive files
  25.  *
  26.  *  where   c=connect, s=send, r=receive,
  27.  *          d=debug, i=image mode, f=no filename conversion, l=tty line,
  28.  *          b=baud rate, e=escape char.
  29.  *
  30.  *  For remote Kermit, format is either:
  31.  *       kermit r               to receive files
  32.  *  or   kermit s file ...            to send files
  33.  *
  34.  */
  35. /*
  36.  *   OS-9 Version and differences
  37.  *   kermit s[ifdl] path filename      is send format
  38.  *   OS-9 version has time-out on rx data of MAXTRY
  39.  *     if not server "W"ait mode.
  40.  *
  41.  *   Host Server command
  42.  *   Kermit h[idfl path]           Host Server mode
  43.  *   Kermit g[idf]l path filename  Get file from a server
  44.  *   Kermit q[idf]l path           Tell Host Server to terminate
  45.  *
  46.  */
  47.  
  48. /*
  49.  *  Modification History:
  50.  *
  51.  * 07/02/85 ral     Add connect command from Brad Bosch's os9 kermit
  52.  *                  Rewrite setstat/getstat for putting in raw mode (os9raw.c)
  53.  *                  Add flushinput call to send packet routine
  54.  *                  Rewrite timeout code to be system independent and more
  55.  *                    comprehensive.  (routine readtimed)
  56.  *                  Add compile switches for timeout
  57.  *                  Add nooverlap switch for good performance on CoCo and others
  58.  *                  (coco can't overlap disk and serial i/o, and will timeout
  59.  *                   often if the code assumes it can.  Others should overlap to
  60.  *                   maximize performance.)
  61.  *                  Fix problems with image vs. text.  Make CRLF processing the
  62.  *                    default, (eliminate 'u' mode)
  63.  *                  Make lowercasing of received filenames the default
  64.  *                  Add handling of nak and timeout in get and quit commands
  65.  *                  Allow / in filenames sent in get command
  66.  *                  Fix long-standing bug in receiving SOH in the middle of a
  67.  *                  packet
  68.  *                  Miscellaneous indentation and other coding fixes.
  69.  *                  (Don't blame the indentation on me, someone's tab to space
  70.  *                   converter must have done this mess.)
  71.  * 06/29/85 ral     Split into smaller separately compilable files,
  72.  *                  move #defines, #includes, and global variables to os9inc.h
  73.  *                  rename everything so it fits with Columbia distribution
  74.  *
  75.  * April 9, 1985    Move Option messages, debug show Rpack
  76.  *                  rx cksum if bad.
  77.  * Feburary 27, 1985 Defined rpack type char for OS-9/68k
  78.  * December 12, 1984 fix OS-9 time-out, Server to standard
  79.  * November 19, 1984 Added OS9 time-out for Rx data.  W.G. Seaton
  80.  * November 14, 1984 Added Host Server Mode  W.G. Seaton
  81.  *                 - Added include kermserv.c for server function
  82.  *                 - Added include kermget.c for get function
  83.  * November  9,1984 Modified for OS9  by W.G. Seaton,NASA KSC
  84.  *  Oct. 17 Included fixes from Alan Crosswell (CUCCA) for IBM_UTS:
  85.  *       - Changed MYEOL character from \n to \r.
  86.  *       - Change char to int in bufill so getc would return -1 on
  87.  *         EOF instead of 255 (-1 truncated to 8 bits)
  88.  *       - Added read() in rpack to eat the EOL character
  89.  *       - Added fflush() call in printmsg to force the output
  90.  *       NOTE: The last three changes are not conditionally compiled
  91.  *        since they should work equally well on any system.
  92.  *
  93.  *       Added support for error packets and cleaned up the printing
  94.  *       routines.
  95.  */
  96.  
  97. /*
  98.  *  m a i n
  99.  *
  100.  *  Main routine - parse command and options, set up the
  101.  *  tty lines, and dispatch to the appropriate routine.
  102.  */
  103.  
  104. main(argc, argv)
  105. int     argc;           /* Character pointers to and count of */
  106. char    **argv;         /* command line arguments */
  107. {
  108.         char    *ttyname,       /* tty name for LINE argument */
  109.                 *cp;            /* char pointer */
  110.         int     speed,          /* speed of assigned tty, */
  111.                 len, num,       /* length, number  */
  112.                 qflg, cflg,     /* GET, CONNECT flags   */
  113.                 rflg, sflg;     /* flags for RECEIVE, SEND */
  114.  
  115.         struct sgbuf 
  116.                 insave,         /* Input scf mode save */
  117.                 outsave,        /* Output scf mode save */
  118.                 linesave;       /* mode of tty line in LINE option */
  119.  
  120.         if (argc < 2)
  121.                 usage();        /* Make sure there's a command line */
  122.  
  123.         cp = *++argv; 
  124.         argv++; 
  125.         argc -= 2;      /* Set up pointers to args */
  126.  
  127.         /*  Initialize these values and hope the first packet gets across OK */
  128.  
  129.         eol = CR;               /* EOL for outgoing packets */
  130.         quote = '#';            /* Standard control-quote char "#" */
  131.         pad = 0;                /* No padding */
  132.         padchar = NULL;         /* Use null if any padding wanted */
  133.  
  134.         speed = cflg = sflg = rflg = 0; /* Turn off all parse flags */
  135.         uflg = server = qflg = gflg = hflg = 0;
  136.         ttyname = 0;            /* Default is remote mode */
  137.  
  138.         image = FALSE;
  139.         filnamcnv = TRUE; 
  140.  
  141.         escchr = ESCCHR;        /* Default escape character */
  142.  
  143.         while ((*cp) != NULL)   /* Parse characters in first arg. */
  144.                 switch (*cp++) {
  145.                 case 'c':
  146.                         cflg++; break;  /* C = Connect command */
  147.                 case 's':
  148.                         sflg++; break;  /* S = Send command */
  149.                 case 'r':
  150.                         rflg++; break;  /* R = Receive command */
  151.                 case 'g':
  152.                         gflg++; break;  /* G = Get file from server */
  153.                 case 'h':
  154.                         hflg++; break;  /* H = Host Server Command */
  155.                 case 'q':
  156.                         qflg++; break;  /* Q = tell remote Host to quit */
  157.                 case 'u': 
  158.                         uflg++; break;  /* U = Unix mode file transfer */
  159.                 case 'd':
  160.                         debug++; break; /* D = Increment debug mode count */
  161.                 case 'f':
  162.                         filnamcnv = FALSE; break;
  163.                                         /* F = don't do case conversion */
  164.                 case 'i':
  165.                         image = TRUE; break;
  166.                                         /* I = Image mode; 8-bit, no CRLF map */
  167.                 case 'l':               /* L = specify tty line to use */
  168.                         if (argc--)
  169.                                 ttyname = *argv++;
  170.                         else
  171.                                 usage(); 
  172.                         if (debug)
  173.                                 printf("Line to remote host is %s\n", ttyname); 
  174.                         break;
  175.                 case 'e':               /* E = specify escape char */
  176.                         if (argc--)
  177.                                 escchr = **argv++;
  178.                         else
  179.                                 usage();
  180.                         if (debug)
  181.                                 printf("Escape char is \"%c\"\n",escchr);
  182.                         break;
  183.                 case 'b':               /* B = specify baud rate */
  184.                         printmsg("Speed setting not yet implemented.");
  185.                         exit(1);
  186.                 }
  187.  
  188.         /* Done parsing */
  189.  
  190.         /* Only one command allowed */
  191.  
  192.         if ((cflg + qflg + gflg + hflg + sflg + rflg) != 1)
  193.                 usage();
  194.  
  195.         if (ttyname) {
  196.                 /* LINE was specified, we operate in local mode */ 
  197.                 ttyfd = open(ttyname, 3);   /* Open the line for update */
  198.                 if (ttyfd < 0) {
  199.                         printmsg("Cannot open %s", ttyname);
  200.                         exit(1);
  201.                 }
  202.                 remot = FALSE;         /* Indicate we're in local mode */
  203.         } else {
  204.                 /* No LINE specified so we operate */
  205.                 /* in remote mode (ie. controlling */
  206.                 /* tty is the communications line) */
  207.                 ttyfd = 0;
  208.                 remot = TRUE;
  209.         }
  210.  
  211.  
  212.         /*
  213.          * Tell User Options and Modes Invoked.
  214.          */
  215.         if (debug) {
  216.                 printf("Debugging level = %d\n\n", debug);
  217.                 if      (cflg) puts("Connect command");
  218.                 else if (sflg) puts("Send command");
  219.                 else if (rflg) puts("Receive command");
  220.                 else if (gflg) puts("Get command");
  221.                 else if (hflg) puts("Host Server command");
  222.                 else if (qflg) puts("Quit to Host Server command");
  223.                 else if (uflg) puts("Unix file transfer Mode");
  224.         }
  225.         /* Put the proper tty into the correct mode */
  226.  
  227.         if (remot) {          /* If remote, use controlling tty */
  228.                 raw(0, &insave);        /* put input in raw mode */
  229.                 raw(1, &outsave);       /* ditto for output */
  230.         } else {            /* Local, use assigned line */
  231.                 raw(ttyfd,&linesave);   /* put assigned line in raw mode */
  232.         }   
  233.  
  234.         /* All set up, now execute the command that was given. */
  235.  
  236.         if (cflg)
  237.                 connect();      /* Connect command */
  238.         else {
  239.                 if (gflg) {              /* Set up Get Function  */
  240.                         /* Get file to request  */
  241.                         if (argc--)
  242.                                 filnam = *argv++;
  243.                         else
  244.                                 usage();         /* Parameter error quit */
  245.                         fp = NULL;        /* No files open yet  */
  246.                         filelist = argv;  /* set up list of files to request */
  247.                         filecount = argc; /* Number of files to get */
  248.                         getsw();          /* Initate remote Get    */
  249.                 }
  250.                 if (hflg) servsw();       /* Initate Server Mode */
  251.                 if (qflg) quitsw();       /* Initate Quit mode */
  252.                 if (sflg) {               /* Send command */
  253.                         /* Get file to send */
  254.                         if (argc--)
  255.                                 filnam = *argv++;
  256.                         else {
  257.                                 if (remot){
  258.                                         /* restore controlling scf device */
  259.                                         /* modes in reverse order */
  260.                                         cook(1, &outsave);
  261.                                         cook(0, &insave);
  262.                                 } else
  263.                                         /* restore assigned line */
  264.                                         cook(ttyfd, &linesave);
  265.                                 usage();
  266.                         }
  267.                         fp = NULL;        /* Indicate no file open yet */
  268.                         filelist = argv;  /* Set up the rest of the file list */
  269.                         filecount = argc; /* Number of files left to send */
  270.                         /* Send the file(s) */
  271.                         printmsg(sendsw() == FALSE ? "Send failed." : "done.");
  272.                 }
  273.                 if (rflg) {            /* Receive command */
  274.                         printmsg(recsw() == FALSE ? "Receive failed.": "done.");
  275.                 }
  276.         }
  277.  
  278.         if (remot) {
  279.                 cook(1, &outsave);    /* restore controlling scf modes */
  280.                 cook(0, &insave);     /* in reverse order */
  281.         } else
  282.                 cook(ttyfd, &linesave); /* restore assigned line */
  283. }
  284.