home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uniflex.zip / ufkerm.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  15KB  |  425 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.5
  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.         *userfile,
  120.         *malloc();
  121.  
  122.    do_startup = TRUE;                     /* Use startup command file */
  123.    verbose = FALSE;                       /* Don't echo startup file */
  124.    cflg = sflg = rflg = FALSE;            /* Turn off all flags */
  125.    userfile = NULL;                       /* Nothing specified yet */
  126.  
  127.    if (argc > 1)
  128.    {
  129.       s = *++argv;                        /* Setup pointers to arguments */
  130.       argv++;
  131.       argc -= 2;
  132.       while (*s != '\0')                  /* loop until end of string */
  133.          switch(*s++)
  134.          {
  135.             case 'c':
  136.                      cflg++;              /* 'connect' mode */
  137.                      break;
  138.             case 'r':
  139.                      rflg = 1;            /* 'receive' mode */
  140.                      break;
  141.             case 'g':
  142.                      rflg = 2;            /* 'get' mode */
  143.                      break;
  144.             case 's':
  145.                      sflg = 1;            /* 'send' mode */
  146.                      break;
  147.             case 'x':
  148.                      sflg = 2;            /* 'server' mode */
  149.                      break;
  150.             case 'l':
  151.                      strcpy(tty_descr,"/dev/modem"); /* Use local line */
  152.                      break;
  153.             case 't':
  154.                      if (argc--)          /* Use specified terminal */
  155.                         strcpy(tty_descr,*argv++);
  156.                      else
  157.                         usage();
  158.                      break;
  159.             case 'o':
  160.                      maskflag = TRUE;     /* Turn on image mode */
  161.                      break;
  162.             case 'i':
  163.                      image = TRUE;        /* Turn on image mode */
  164.                      break;
  165.             case 'f':
  166.                      mapping = FALSE;     /* Turn off filename case mapping */
  167.                      break;
  168.             case 'e':
  169.                      if (argc--)
  170.                         escchr = **argv++;/* Get new escape character */
  171.                      else
  172.                         usage();
  173.                      break;
  174.             case 'n':
  175.                      do_startup = FALSE;  /* Don't get commands from file */
  176.                      break;
  177.             case 'v':
  178.                      verbose = TRUE;      /* Echo startup file */
  179.                      break;
  180.             case 'u':
  181.                      if (argc--)          /* Use specified command file */
  182.                         userfile = *argv++;
  183.                      else
  184.                         usage();
  185.                      break;
  186.             case 'b':
  187.                      if (argc--)          /* Use specified terminal */
  188.                      {
  189.                         if (set_baud(TRUE,argv++)) /* Set baud rate */
  190.                            usage();
  191.                      }
  192.                      else
  193.                         usage();
  194.                      break;
  195.             default:
  196.                      usage();             /* Tell him how to do it */
  197.          }
  198.  
  199.       if ((sflg && rflg) ||               /* check for illegal combinations */
  200.           (sflg && cflg) ||
  201.           (rflg && cflg))
  202.          usage();
  203.       prt_ident();
  204.       if ((do_startup) && !(sflg || rflg || cflg))
  205.          if (userfile == 0)
  206.          {
  207.             if (take(INITFILE,verbose) != NULL) /* Execute default init file */
  208.                take(INITFILE1,verbose);  /* Try alternate name */
  209.          }
  210.          else
  211.             if ((j = take(userfile,verbose)) != NULL)/* Exec user init file */
  212.                prterr(j);
  213.       if (cflg || rflg || sflg)
  214.       {
  215.          call_baud = FALSE;         /* Don't set baudrate anymore */
  216.          numprm = argc + 1;
  217.          if (argc != 0)
  218.          {
  219.             if ((params = malloc(numprm * sizeof(char *))) == NULL)
  220.             {                       /* get space for parameters */
  221.                prterr(ER_NOMEM);
  222.                kerm_exit();
  223.             }
  224.             for (j = 1; j < numprm; j++)
  225.                params[j] = *argv++; /* Setup correct parameter pointers */
  226.          }
  227.       }
  228.    }
  229.    else
  230.    {
  231.       prt_ident();
  232.       if (take(INITFILE,verbose) != NULL) /* Execute default init file */
  233.          take(INITFILE1,verbose);  /* Try alternate name */
  234.    }
  235. }
  236.  
  237. prt_ident()
  238. {
  239.    if (!cflg && !rflg && !sflg)
  240.    {
  241.       fputs(IDENT,stdout);                /* Tell him we're on the air! */
  242.       fputs(VERSION,stdout);              /* With this version */
  243.       fputs("\n\n",stdout);
  244.    }
  245. }
  246.  
  247. init_par()
  248. {
  249.    /*  Initialize these values and hope the first packet will get across OK */
  250.  
  251.    mypackstart = I_MYPACKSTART;           /* Start of packet character */
  252.    maxpacksiz = I_MAXPACKSIZ;             /* Maximum packet size */
  253.    maxtry = I_MAXTRY;                     /* Times to retry a packet */
  254.    myquote = I_MYQUOTE;                   /* Quote character I will use */
  255.    mypad = I_MYPAD;                       /* Number of padding chars I need */
  256.    mypchar = I_MYPCHAR;                   /* Padding character I need (NULL) */
  257.    myeol = I_MYEOL;                       /* End-Of-Line character I need */
  258.    mytime = I_MYTIME;                     /* My timeout in seconds */
  259.    myblock_check_type = I_BLOCKCHECKTYPE; /* Block check type */
  260.    myrptquote = I_MYRPTQUOTE;             /* Repeat count quote character */
  261.    myeightquote = I_MYEIGHTQUOTE;         /* Eight bit quote character */
  262.    send_delay = I_SEND_DELAY;             /* Delay before sending */
  263.    attribute = FALSE;                     /* No attribute packets yet */
  264.    allowattr = TRUE;                      /* Do allow them */
  265.  
  266.    strcpy(prompt,DEFPROMPT);              /* Setup prompt */
  267.    logfile[0] = '\0';                     /* No log filename yet */
  268.    logfileopen = FALSE;                   /* No logging done yet */
  269.    speed = 1200;                          /* Default line speed */
  270.    maskflag = FALSE;                      /* Use default mask for RTS & DCD */
  271.    config = 5;                            /* 8 bits, 1 stop, no parity */
  272.    image = FALSE;                         /* Translation */
  273.    save_file = FALSE;                     /* Don't save file on abort */
  274.    prvsetdone = FALSE;                    /* privileged task setup done */
  275.    call_baud = TRUE;                      /* call 'baud' on line open */
  276.    escchr = ESCCHR;                       /* Default escape character */
  277.    fulldup = TRUE;                        /* Default is full duplex */
  278.    warning = TRUE;                        /* File conflict warning */
  279.    mapping = TRUE;                        /* Filename case mapping */
  280.    auto_recover = TRUE;                   /* Recovery for extended packets */
  281.    t_chr_sent = t_dchr_sent = t_nak_sent = 0; /* reset counters */
  282.    t_chr_rec = t_dchr_rec = t_nak_rec = 0;
  283.    tabsleft = TABSIZE;                    /* Default tab setting */
  284.    dstop = XOFF;                          /* Init handshake characters */
  285.    dstart = XON;
  286.    dbgfil = ERROR;                        /* No debug file */
  287.    fp = ERROR;                            /* Indicate no file open yet */
  288.    strcpy(tty_descr,"Remote");
  289.    aborted = FALSE;                       /* Not yet aborted */
  290.    port_open = FALSE;                     /* Port closed now */
  291.    timint = I_MYTIME;                     /* Setup timeout value */
  292.    recpkt = 0;                            /* Invalidate memory pointers */
  293.    sndpkt = 0;
  294.    screen = terminit();                   /* Set TRUE if screen control */
  295.    init_crc_table();                      /* Setup table for CRC calculation */
  296.    bg = check_bg();                       /* Test if we're in the background */
  297. }
  298.  
  299. /*
  300.  * Process kermit command
  301.  */
  302.  
  303. kerm_command(instrng)
  304. char *instrng;
  305. {
  306.    int funcpoint;
  307.    char *free();
  308.  
  309.    if (split(instrng))                    /* Split command line */
  310.    {                                      /* into seperate strings */
  311.       cflg = sflg = rflg = 0;             /* Turn off all flags */
  312.       tabsleft = TABSIZE;                 /* Default tab setting */
  313.       aborted = FALSE;                    /* Not yet aborted */
  314.       timint = I_MYTIME;                  /* Setup timeout value */
  315.  
  316.       funcpoint = parse(params[0], command_table);/* Find routine address */
  317.       if (funcpoint == NULL)              /* Ambiguous */
  318.          prterr(ER_AMBIG);
  319.       else if (funcpoint == ERROR)        /* Unknown */
  320.          prterr(ER_UNKNOWN);
  321.       else
  322.          switch(funcpoint)                /* Process command */
  323.          {
  324.             case 1:
  325.                      sendfile();
  326.                      break;
  327.             case 2:
  328.                      recfile();
  329.                      break;
  330.             case 3:
  331.                      connect();
  332.                      break;
  333.             case 4:
  334.                      getfile();
  335.                      break;
  336.             case 5:
  337.                      set();
  338.                      break;
  339.             case 6:
  340.                      show();
  341.                      break;
  342.             case 7:
  343.                      server();
  344.                      break;
  345.             case 8:
  346.                      help();
  347.                      break;
  348.             case 9:
  349.             case 10:
  350.                      kerm_exit();
  351.                      break;
  352.             case 11:
  353.                      bye();
  354.                      break;
  355.             case 12:
  356.                      finish();
  357.                      break;
  358.             case 13:
  359.                      trnsmit();
  360.                      break;
  361.             case 14:
  362.                      do_uniflex();
  363.                      break;
  364.             case 15:
  365.                      statistics();
  366.                      break;
  367.             case 16:
  368.                      take(0,TRUE);
  369.                      break;
  370.             case 17:
  371.                      chd();
  372.                      break;
  373.             case 18:
  374.                      disk_free();
  375.                      break;
  376.          }
  377.       dealloc_pkt();                      /* Release allocated memory */
  378.       if (fp != ERROR)
  379.       {
  380.          fclose(fp);                      /* Close file if we're aborted */
  381.          fp = ERROR;                      /* Indicate no file open yet */
  382.       }
  383.    }
  384.    while (numprm)
  385.       free(params[--numprm]);             /* Free command string memory */
  386.    free(params);                          /* get rid of pointer table */
  387. }
  388.  
  389. usage()
  390. {
  391.    char **dp;
  392.    static char *doc[] = {
  393.    "Usage: kermit n  Disable startup command file",
  394.    "              v  Verbose startup, echo commands",
  395.    "              u  Use specified command file",
  396.    "              c  Connect to line",
  397.    "              r  Receive file(s)",
  398.    "              g  Get file(s) from server",
  399.    "              s  Send file(s)",
  400.    "              x  Start server mode",
  401.    "              l  Use local device ('/dev/modem')",
  402.    "              t  Use specified device",
  403.    "              i  Turn on image mode",
  404.    "              f  Turn off filename mapping",
  405.    "              e  Use new escape character",
  406.    "              b  Set new baud rate",
  407.    "              o  Ignore DCD and force RTS in port usage",
  408.    "",
  409.    "Kermit enters interactive mode if no parameters to",
  410.    "send or receive files are specified.",
  411.    0 };
  412.  
  413.    for (dp = doc; *dp; dp++)
  414.       fprintf(stderr,"%s\n",*dp);
  415.    exit(1);
  416. }
  417.  
  418. kerm_exit()
  419. {
  420.    if (port_open)
  421.       close_port(FALSE,TRUE);
  422.    sup_exit();                          /* stop support task */
  423.    exit(0);
  424. }
  425.