home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / uniflex / ufkerm.uue / ufkerm.arc / UFKERM.C < prev    next >
C/C++ Source or Header  |  1989-01-09  |  14KB  |  407 lines

  1. /*
  2.  *  K e r m i t  File Transfer Utility
  3.  *
  4.  *  UniFLEX Kermit...  Adapted from UNIX version by
  5.  *
  6.  *    Jur van der Burg
  7.  *    Nettelhorst 56
  8.  *    2402 LS Alphen aan den Rijn
  9.  *    The Netherlands
  10.  *
  11.  *    Telephone: (0)1720 - 34057
  12.  *
  13.  */
  14.  
  15. #include "ufk.h"
  16. #include <signal.h>
  17.  
  18. #asm
  19.  info UniFLEX Kermit
  20.  info Author Jur van der Burg
  21.  info Nettelhorst 56
  22.  info 2402 LS  Alphen aan den Rijn
  23.  info The Netherlands
  24.  info Version: V 1.4
  25. #endasm
  26.  
  27. /*
  28.  * Main command table
  29.  */
  30.  
  31. TABLE  command_table[] =
  32. {
  33.    "send", 1,
  34.    "receive", 2,
  35.    "connect", 3,
  36.    "get", 4,
  37.    "set", 5,
  38.    "show", 6,
  39.    "server", 7,
  40.    "help", 8,
  41.    "exit", 9,
  42.    "quit", 10,
  43.    "bye", 11,
  44.    "finish", 12,
  45.    "transmit", 13,
  46.    "local", 14,
  47.    "statistics", 15,
  48.    "take", 16,
  49.    "chd", 17,
  50.    "free", 18,
  51.    "", 0
  52. };
  53.  
  54. /*
  55.  * Start of main program
  56.  */
  57.  
  58. main(argc,argv)
  59. int   argc;
  60. char  *argv[];
  61. {
  62.    char  input[257],                      /* Command input */
  63.          save_screen;
  64.    int   kerm_exit();
  65.  
  66.    setbuf(stdout,0);                      /* Don't buffer stdout */
  67.    pffinit();                             /* Link code for floating point */
  68.    pflinit();                             /* Link code for long printout */
  69.  
  70.    init_par();                            /* Setup parameters */
  71.    parse_flags(argc,argv);                /* Select correct options */
  72.    signal(SIGTERM,kerm_exit);             /* Catch TERMINATE interrupt */
  73.    signal(SIGHUP,kerm_exit);              /* Catch HANGUP interrupt */
  74.  
  75.    nooutput = TRUE;        /* Don't generate output if called with options */
  76.    save_screen = screen;                  /* Save for later */
  77.    screen = FALSE;
  78.    if (cflg)
  79.       connect();                          /* connect to line */
  80.    else if (rflg == 1)
  81.       recfile();                          /* receive file */
  82.    else if (rflg == 2)
  83.       getfile();                          /* Get file from server */
  84.    else if (sflg == 1)
  85.       sendfile();                         /* send file */
  86.    else if (sflg == 2)
  87.       server();                           /* server mode */
  88.    else
  89.    {
  90.       nooutput = FALSE;   /* Generate output if not called with optiAns */
  91.       screen = save_screen;               /* restore screen value */
  92.       for (;;)                            /* Do this forever */
  93.       {
  94.          do
  95.          {
  96.             fputs(prompt,stdout);         /* Prompt user */
  97.             if (fgets(input,256,stdin) == NULL) /* Get response */
  98.             {
  99.                fputs("\n",stdout);
  100.                kerm_exit();               /* E O F */
  101.             }
  102.             input[strlen(input) - 1] = '\0'; /* Zap newline */
  103.          }
  104.          while (!input[0] || (input[0] == COMMENT));/* Blank command line */
  105.          kerm_command(input);             /* Process command */
  106.       }
  107.    }
  108. }
  109.  
  110.  
  111. parse_flags(argc,argv)
  112. int argc;
  113. char *argv[];
  114. {
  115.    int do_startup,                        /* Start file flag */
  116.        verbose,                           /* Echo startup file */
  117.        j;
  118.    char *s;
  119.  
  120.    do_startup = TRUE;                     /* Use startup command file */
  121.    verbose = FALSE;                       /* Don't echo startup file */
  122.    cflg = sflg = rflg = FALSE;            /* Turn off all flags */
  123.    numprm = 0;                            /* Nothing specified yet */
  124.  
  125.    if (argc > 1)
  126.    {
  127.       s = *++argv;                        /* Setup pointers to arguments */
  128.       argv++;
  129.       argc -= 2;
  130.       while (*s != '\0')                  /* loop until end of string */
  131.          switch(*s++)
  132.          {
  133.             case 'c':
  134.                      cflg++;              /* 'connect' mode */
  135.                      break;
  136.             case 'r':
  137.                      rflg = 1;            /* 'receive' mode */
  138.                      break;
  139.             case 'g':
  140.                      rflg = 2;            /* 'get' mode */
  141.                      break;
  142.             case 's':
  143.                      sflg = 1;            /* 'send' mode */
  144.                      break;
  145.             case 'x':
  146.                      sflg = 2;            /* 'server' mode */
  147.                      break;
  148.             case 'l':
  149.                      strcpy(tty_descr,"/dev/modem"); /* Use local line */
  150.                      break;
  151.             case 't':
  152.                      if (argc--)          /* Use specified terminal */
  153.                         strcpy(tty_descr,*argv++);
  154.                      else
  155.                         usage();
  156.                      break;
  157.             case 'i':
  158.                      image = TRUE;        /* Turn on image mode */
  159.                      break;
  160.             case 'f':
  161.                      mapping = FALSE;     /* Turn off filename case mapping */
  162.                      break;
  163.             case 'e':
  164.                      if (argc--)
  165.                         escchr = **argv++;/* Get new escape character */
  166.                      else
  167.                         usage();
  168.                      break;
  169.             case 'n':
  170.                      do_startup = FALSE;  /* Don't get commands from file */
  171.                      break;
  172.             case 'v':
  173.                      verbose = TRUE;      /* Echo startup file */
  174.                      break;
  175.             case 'u':
  176.                      if (argc--)          /* Use specified command file */
  177.                      {
  178.                         numprm = 2;
  179.                         params[1] = *argv++;
  180.                      }
  181.                      else
  182.                         usage();
  183.                      break;
  184.             case 'b':
  185.                      if (argc--)          /* Use specified terminal */
  186.                      {
  187.                         if (set_baud(TRUE,argv++)) /* Set baud rate */
  188.                            usage();
  189.                      }
  190.                      else
  191.                         usage();
  192.                      break;
  193.             default:
  194.                      usage();             /* Tell him how to do it */
  195.          }
  196.  
  197.       if ((sflg && rflg) ||               /* check for illegal combinations */
  198.           (sflg && cflg) ||
  199.           (rflg && cflg))
  200.          usage();
  201.       prt_ident();
  202.       if ((do_startup) && !(sflg || rflg || cflg))
  203.          if (numprm == 0)
  204.             take(TRUE,verbose);     /* Execute eventual initialization file */
  205.          else
  206.             take(FALSE,verbose);    /* Execute user initialization file */
  207.       if (cflg || rflg || sflg)
  208.       {
  209.          call_baud = FALSE;         /* Don't set baudrate anymore */
  210.          numprm = argc + 1;
  211.          if (argc != 0)
  212.             for (j = 1; j < numprm; j++)
  213.                params[j] = *argv++; /* Setup correct parameter pointers */
  214.       }
  215.    }
  216.    else
  217.    {
  218.       prt_ident();
  219.       take(TRUE,verbose);           /* Execute eventual initialization file */
  220.    }
  221. }
  222.  
  223. prt_ident()
  224. {
  225.    if (!cflg && !rflg && !sflg)
  226.    {
  227.       fputs(IDENT,stdout);                /* Tell him we're on the air! */
  228.       fputs(VERSION,stdout);              /* With this version */
  229.       fputs("\n\n",stdout);
  230.    }
  231. }
  232.  
  233. init_par()
  234. {
  235.    /*  Initialize these values and hope the first packet will get across OK */
  236.  
  237.    mypackstart = I_MYPACKSTART;           /* Start of packet character */
  238.    maxpacksiz = I_MAXPACKSIZ;             /* Maximum packet size */
  239.    maxtry = I_MAXTRY;                     /* Times to retry a packet */
  240.    myquote = I_MYQUOTE;                   /* Quote character I will use */
  241.    mypad = I_MYPAD;                       /* Number of padding chars I need */
  242.    mypchar = I_MYPCHAR;                   /* Padding character I need (NULL) */
  243.    myeol = I_MYEOL;                       /* End-Of-Line character I need */
  244.    mytime = I_MYTIME;                     /* My timeout in seconds */
  245.    myblock_check_type = I_BLOCKCHECKTYPE; /* Block check type */
  246.    myrptquote = I_MYRPTQUOTE;             /* Repeat count quote character */
  247.    myeightquote = I_MYEIGHTQUOTE;         /* Eight bit quote character */
  248.    send_delay = I_SEND_DELAY;             /* Delay before sending */
  249.    attribute = FALSE;                     /* No attribute packets yet */
  250.    allowattr = TRUE;                      /* Do allow them */
  251.  
  252.    strcpy(prompt,DEFPROMPT);              /* Setup prompt */
  253.    logfile[0] = '\0';                     /* No log filename yet */
  254.    logfileopen = FALSE;                   /* No logging done yet */
  255.    speed = 1200;                          /* Default line speed */
  256.    config = 5;                            /* 8 bits, 1 stop, no parity */
  257.    image = FALSE;                         /* Translation */
  258.    save_file = FALSE;                     /* Don't save file on abort */
  259.    prvsetdone = FALSE;                    /* privileged task setup done */
  260.    call_baud = TRUE;                      /* call 'baud' on line open */
  261.    escchr = ESCCHR;                       /* Default escape character */
  262.    fulldup = TRUE;                        /* Default is full duplex */
  263.    warning = TRUE;                        /* File conflict warning */
  264.    mapping = TRUE;                        /* Filename case mapping */
  265.    auto_recover = TRUE;                   /* Recovery for extended packets */
  266.    t_chr_sent = t_dchr_sent = t_nak_sent = 0; /* reset counters */
  267.    t_chr_rec = t_dchr_rec = t_nak_rec = 0;
  268.    tabsleft = TABSIZE;                    /* Default tab setting */
  269.    dstop = XOFF;                          /* Init handshake characters */
  270.    dstart = XON;
  271.    dbgfil = ERROR;                        /* No debug file */
  272.    fp = ERROR;                            /* Indicate no file open yet */
  273.    strcpy(tty_descr,"Remote");
  274.    aborted = FALSE;                       /* Not yet aborted */
  275.    port_open = FALSE;                     /* Port closed now */
  276.    timint = I_MYTIME;                     /* Setup timeout value */
  277.    recpkt = 0;                            /* Invalidate memory pointers */
  278.    sndpkt = 0;
  279.    screen = terminit();                   /* Set TRUE if screen control */
  280.    init_crc_table();                      /* Setup table for CRC calculation */
  281.    check_bg();                            /* Test if we're in the background */
  282. }
  283.  
  284. /*
  285.  * Process kermit command
  286.  */
  287.  
  288. kerm_command(instrng)
  289. char *instrng;
  290. {
  291.    int funcpoint;
  292.    char *free();
  293.  
  294.    if (split(instrng, params))            /* Split command line */
  295.    {                                      /* into seperate strings */
  296.       cflg = sflg = rflg = 0;             /* Turn off all flags */
  297.       tabsleft = TABSIZE;                 /* Default tab setting */
  298.       aborted = FALSE;                    /* Not yet aborted */
  299.       timint = I_MYTIME;                  /* Setup timeout value */
  300.  
  301.       funcpoint = parse(params[0], command_table);/* Find routine address */
  302.       if (funcpoint == NULL)              /* Ambiguous */
  303.          prterr(ER_AMBIG);
  304.       else if (funcpoint == ERROR)        /* Unknown */
  305.          prterr(ER_UNKNOWN);
  306.       else
  307.          switch(funcpoint)                /* Process command */
  308.          {
  309.             case 1:
  310.                      sendfile();
  311.                      break;
  312.             case 2:
  313.                      recfile();
  314.                      break;
  315.             case 3:
  316.                      connect();
  317.                      break;
  318.             case 4:
  319.                      getfile();
  320.                      break;
  321.             case 5:
  322.                      set();
  323.                      break;
  324.             case 6:
  325.                      show();
  326.                      break;
  327.             case 7:
  328.                      server();
  329.                      break;
  330.             case 8:
  331.                      help();
  332.                      break;
  333.             case 9:
  334.             case 10:
  335.                      kerm_exit();
  336.                      break;
  337.             case 11:
  338.                      bye();
  339.                      break;
  340.             case 12:
  341.                      finish();
  342.                      break;
  343.             case 13:
  344.                      trnsmit();
  345.                      break;
  346.             case 14:
  347.                      do_uniflex();
  348.                      break;
  349.             case 15:
  350.                      statistics();
  351.                      break;
  352.             case 16:
  353.                      take(FALSE,TRUE);
  354.                      break;
  355.             case 17:
  356.                      chd();
  357.                      break;
  358.             case 18:
  359.                      disk_free();
  360.                      break;
  361.          }
  362.       dealloc_pkt();                      /* Release allocated memory */
  363.       if (fp != ERROR)
  364.       {
  365.          fclose(fp);                      /* Close file if we're aborted */
  366.          fp = ERROR;                      /* Indicate no file open yet */
  367.       }
  368.    }
  369.    while (numprm)
  370.       free(params[--numprm]);             /* Free command string memory */
  371. }
  372.  
  373. usage()
  374. {
  375.    char **dp;
  376.    static char *doc[] = {
  377.    "Usage: kermit n  Disable startup command file",
  378.    "              v  Verbose startup, echo commands",
  379.    "              u  Use specified command file",
  380.    "              c  Connect to line",
  381.    "              r  Receive file(s)",
  382.    "              g  Get file(s) from server",
  383.    "              s  Send file(s)",
  384.    "              x  Start server mode",
  385.    "              l  Use local device ('/dev/modem')",
  386.    "              t  Use specified device",
  387.    "              i  Turn on image mode",
  388.    "              f  Turn off filename mapping",
  389.    "              e  Use new escape character",
  390.    "              b  Set new baud rate",
  391.    "",
  392.    "Kermit enters interactive mode if no parameters are specified",
  393.    0 };
  394.  
  395.    for (dp = doc; *dp; dp++)
  396.       fprintf(stderr,"%s\n",*dp);
  397.    exit(1);
  398. }
  399.  
  400. kerm_exit()
  401. {
  402.    if (port_open)
  403.       close_port(FALSE,TRUE);
  404.    sup_exit();                          /* stop support task */
  405.    exit(0);
  406. }
  407.