home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / xyz.lzh / main.c < prev    next >
C/C++ Source or Header  |  1995-08-18  |  10KB  |  308 lines

  1. /*
  2.    `main' function for XY, Z, and K programs.
  3.  
  4.    This file shamelessly uses ANSI token-pasting and other
  5.    hairy constructs in order to provide a single main file
  6.    for three different programs.  GCC 1.42 has no problems
  7.    with this, and Ultra-C should work.  The older Microware
  8.    C compiler will almost certainly not work.
  9. */
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <ctype.h>
  15. #include "ftserial.h"
  16. #include "ftdisk.h"
  17. #include "ftdebug.h"
  18. #include "ftprog.h"
  19.  
  20. #ifdef XYMAIN
  21. #include "ftxy.h"
  22. #define PROG "XY"
  23. #define PROTOCOL_FCN(func)  XYModem ## func
  24. #endif
  25.  
  26. #ifdef ZMAIN
  27. #include "ftz.h"
  28. #define PROG "Z"
  29. #define PROTOCOL_FCN(func)  ZModem ## func
  30. #endif
  31.  
  32. #ifdef KMAIN
  33. #include "ftk.h"
  34. #define PROG "K"
  35. #define PROTOCOL_FCN(func)  Kermit ## func
  36. #endif
  37.  
  38. #define DEBUG_FILE (0x80000000U)
  39.  
  40. #include <stddef.h>
  41. const char *getenv(const char *);
  42. void exit(int);
  43. void *malloc(size_t);
  44. int strlen(const char *);
  45. char *strcpy(char *,const char *);
  46. char *strcat(char *,const char *);
  47. #define FALSE (0)
  48. #define TRUE (1)
  49.  
  50. /****************************************************************************/
  51. static void Banner(void)
  52. {
  53.     const char *banner[] = {
  54.         PROG " version 1.1   (" __DATE__ ")",
  55.         "Copyright (C) 1995 Tim Kientzle.  All Rights Reserved.",
  56.         NULL
  57.     };
  58.  
  59.     const char **p = banner;
  60.  
  61.     while (*p) fprintf(stderr, "%s\n",*p++);
  62. }
  63.  
  64. /****************************************************************************/
  65. static void Usage(void)
  66. {
  67.     const char *help[] = {
  68.         "General Usage:",
  69.         "   To send files:      " PROG " <file1> <file2> <etc.>",
  70.         "   To receive files:   " PROG,
  71.         "",
  72.         "By default, " PROG " will automatically guess the type of each file.",
  73.         "   To treat all files as a certain type:",
  74.         "     -A   All files are ASCII      -B   All files are Binary",
  75. #ifdef XYMAIN
  76.         "By default, XY attempts to use the YModem protocol.",
  77.         "   To influence xy's protocol determination:",
  78.         "     -X XModem       -K XModem-K   -G YModem-G",
  79.         "     -C XModem-CRC   -Y YModem",
  80. #endif
  81. #ifdef ZMAIN
  82.         "   To send a command via ZModem: -c \"command\"",
  83. #endif
  84. #ifdef KMAIN
  85. #endif
  86.         "   To get debugging information: -d, -dd, etc.",
  87.         "",
  88.         PROG " uses the MODEM environment variable to determine the port",
  89.         "to use.  If none is available, stdio paths will be used.",
  90.         "The -p<port> option overrides the MODEM environment variable.",
  91.         "Host mode: -h forces " PROG " to use stdio paths and suppresses all",
  92.         "progress and debug messages.  Equivalent to `unsetenv MODEM'.",
  93.         "",
  94.         "Based on \"The Working Programmer's Guide to Serial Protocols\"",
  95.         "Copyright (C) 1995 Tim Kientzle and The Coriolis Group, Inc.",
  96.         NULL
  97.     };
  98.  
  99.     const char **p = help;
  100.  
  101.     while (*p) fprintf(stderr, "%s\n",*p++);
  102.     exit(0);
  103. }
  104.  
  105. /****************************************************************************/
  106. int main(int argc, char **argv)
  107. {
  108.     SERIAL_PORT serialPort;
  109.     const char *portName = getenv("MODEM");
  110.     DEBUG debug = NULL;
  111.     long debugFilter = 0;
  112.     PROGRESS progress = NULL;
  113.     const char **fileList;
  114.     int fileListLength = 0;
  115.     int hostMode = 0;
  116.     int fileType = diskFileUnknown;
  117.     int returnVal = 1;
  118. #ifdef XYMAIN
  119.     XYMODEM ftHandle;
  120.     int protocol = YMODEM;  /* Default protocol */
  121. #endif
  122. #ifdef ZMAIN
  123.     ZMODEM ftHandle;
  124.     int commandMode = FALSE;
  125. #endif
  126. #ifdef KMAIN
  127.     KERMIT ftHandle;
  128.     int speed = 0;
  129. #endif
  130.  
  131.     /* Allocate buffer for list of files to send */
  132.     fileList = (const char **)malloc(argc * sizeof(char *));
  133.  
  134.     /* Process options */
  135.     while( --argc > 0 ) {
  136.         char *p=*++argv;
  137.         if (*p == '-') { /* Process option */
  138.             while (*++p) { /* Loop through all the letters */
  139.                 switch(*p) {  /* Process each letter */
  140.                     /* Generic options */
  141.                     case '?': Usage(); break;
  142.                     case 'p':
  143.                     case 'P':
  144.                       p++;
  145.                       if (*p == '=') p++;
  146.                       if ( (*p == 0) && (--argc > 0) )
  147.                         p = *++argv;
  148.                       if (*p) {
  149.                         portName = p;
  150.                         p = p + strlen(p) - 1;
  151.                       }
  152.                       break;
  153.                     case 'h': case 'H': hostMode++; break;
  154.                     case 'D': debugFilter |= DEBUG_FILE;
  155.                     case 'd':
  156.                             {
  157.                                 char *q = p+1;
  158.                                 int d = 0;
  159.                                 while (isdigit(*q)) {
  160.                                     d = d*10 + *q++ - '0';
  161.                                 }
  162.                                 debugFilter |= d;
  163.                                 p = q-1;
  164.                             }
  165.                             break;
  166.                     case 'a': case 'A': fileType = diskFileAscii; break;
  167.                     case 'b': case 'B': fileType = diskFileBinary; break;
  168.                     default:    fprintf(stderr,"I don't understand option `-%c'.\n",*p);
  169.                                 Usage(); break;
  170. #ifdef XYMAIN
  171.                     /* XYModem-specific options */
  172.                     case 'g': case 'G': protocol = YMODEMG; break;
  173.                     case 'y': case 'Y': protocol = YMODEM; break;
  174.                     case 'k': case 'K': protocol = XMODEMK; break;
  175.                     case 'c': case 'C': protocol = XMODEMCRC; break;
  176.                     case 'x': case 'X': protocol = XMODEM; break;
  177. #endif
  178. #ifdef ZMAIN
  179.                     /* ZModem-specific options */
  180.                     case 'c': case 'C': commandMode = TRUE; break;
  181. #endif
  182. #ifdef KMAIN
  183.                     /* Kermit-specific options */
  184.                     case 'f': case 'F': speed++; break;
  185. #endif
  186.                 }
  187.             }
  188.         } else {
  189.             fileList[fileListLength++] = p;
  190.         }
  191.     }
  192.     fileList[fileListLength] = NULL;
  193.  
  194.     /* If no port name, suppress progress */
  195.     if ((portName != NULL) && (portName[0] == 0)) portName = NULL;
  196.     if (portName == NULL) hostMode++;
  197.  
  198.     /* Print banner */
  199.     if (!hostMode) Banner();
  200.  
  201. #ifdef ZMAIN
  202.     /* ZModem-specific: If command mode, build command string */
  203.     if (commandMode) {
  204.         char *command;
  205.         unsigned length = 0;
  206.         int i;
  207.         for (i=0; i<fileListLength; i++) length += strlen(fileList[i])+1;
  208.         command = malloc(length);
  209.         strcpy(command,fileList[0]);
  210.         for (i=1; i<fileListLength; i++) {
  211.             strcat(command," ");
  212.             strcat(command,fileList[i]);
  213.         }
  214.         if (strlen(command) == 0) {
  215.             fprintf(stderr,"Z: Empty command not allowed!\n");
  216.             exit(1);
  217.         }
  218.         fileList[0] = command;
  219.     }
  220. #endif
  221.  
  222.     /* Print `Sending' or `Receiving' message */
  223.     if (fileListLength > 0) { /* Do send */
  224.         int i;
  225.         fprintf(stderr,"Sending: %s",fileList[0]);
  226.         for(i=1;i<fileListLength;i++)
  227.             fprintf(stderr,", %s",fileList[i]);
  228.         fprintf(stderr,"\n");
  229.     } else
  230.         fprintf(stderr,"Receiving...\n");
  231.  
  232.     /* Initialize debug handle */
  233.     if (debugFilter & ~DEBUG_FILE) /* Leave NULL if no debugging requested */
  234.         DebugInit(&debug);
  235.     if ( (debugFilter & DEBUG_FILE) || (hostMode) ) {
  236.         DebugFile(debug,PROG ".log");
  237.     debugFilter &= ~DEBUG_FILE;
  238.     }
  239.     DebugSetFilter(debug, debugFilter);
  240. /*    if (!hostMode) diskDebug = debugFilter;*/
  241.  
  242.     /* Initialize progress handle */
  243.     if (!hostMode) { /* No progress display in host mode */
  244.         ProgressInit(&progress);
  245.         ProgressSetDebug(progress, debug);
  246.     }
  247.  
  248.     /* Open and setup serial port */
  249.     if (hostMode) portName = NULL;
  250.     if (SerialOpen(&serialPort,portName)) {
  251.         if (portName)
  252.             fprintf(stderr,PROG ": Couldn't open serial port ``%s.''\n",
  253.                     portName);
  254.         else
  255.             fprintf(stderr,PROG ": Can't access stdio??\n");
  256.         exit(1);
  257.     };
  258.     SerialSaveState(serialPort);
  259. #ifdef XYMAIN /* XYModem requires 8 bits, no parity, and no Xon/Xoff */
  260.     SerialSetWord(serialPort,8);
  261.     SerialSetParity(serialPort,parityNone);
  262.     SerialMakeTransparent(serialPort); /* XYModem-specific */
  263. #endif
  264. #ifdef ZMAIN /* ZModem requires 8 bits, no parity */
  265.     SerialSetWord(serialPort,8);
  266.     SerialSetParity(serialPort,parityNone);
  267. #endif
  268.  
  269.     if (PROTOCOL_FCN(Init)( &ftHandle, serialPort))
  270.         fprintf(stderr,"Error initializing file transfer handle\n");
  271.     else if (PROTOCOL_FCN(SetDebug)(ftHandle,debug))
  272.         fprintf(stderr,"Error setting debug handle\n");
  273.     else if (PROTOCOL_FCN(SetProgress)(ftHandle,progress))
  274.         fprintf(stderr,"Error setting progress handle\n");
  275.     else if (PROTOCOL_FCN(SetFileType)(ftHandle,fileType))
  276.         fprintf(stderr,"Error setting file type\n");
  277. #ifdef XYMAIN
  278.     else if (PROTOCOL_FCN(SetProtocol)(ftHandle, protocol)) /* XYModem-specific */
  279.         fprintf(stderr,"Error setting protocol dialect\n");
  280. #endif
  281. #ifdef KMAIN
  282.     else if ((speed > 0) && PROTOCOL_FCN(SetFast)(ftHandle,speed))
  283.         fprintf(stderr,"Error setting speed\n");
  284. #endif
  285. #ifdef ZMAIN
  286.     else if (commandMode) { /* ZModem-specific: Send Command */
  287.         if ((returnVal = PROTOCOL_FCN(SendCommand)(ftHandle,fileList[0])))
  288.             fprintf(stderr,"\nError sending command!!\n");
  289.     }
  290. #endif
  291.     else if (fileListLength > 0) { /* Send Files */
  292.         if ((returnVal = PROTOCOL_FCN(Send)(ftHandle,fileList,fileListLength)))
  293.             fprintf(stderr,"\nError sending!!\n");
  294.     } else { /* Receive Files */
  295.         if ((returnVal = PROTOCOL_FCN(Receive)(ftHandle)))
  296.             fprintf(stderr,"\nError receiving!!\n");
  297.     }
  298.  
  299.     /* Destroy the handles and close the port */
  300.     PROTOCOL_FCN(Destroy)(ftHandle);
  301.     ProgressDestroy(progress);
  302.     DebugDestroy(debug);
  303.     SerialRestoreState(serialPort);
  304.     SerialClose(serialPort);
  305.     return returnVal;
  306. }
  307.  
  308.