home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / UUPC11XT.ZIP / TEST / TESTULIB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  12.8 KB  |  495 lines

  1. /*--------------------------------------------------------------------*/
  2. /*          Program:    ulibtest.c           28 Nov 1992              */
  3. /*          Author:     Andrew H. Derbyshire                          */
  4. /*                      Kendra Electronic Wonderworks                 */
  5. /*                      P.O. Box 132                                  */
  6. /*                      Arlington, MA 02174-0002 USA                  */
  7. /*          Internet:   help@kew.com                                  */
  8. /*          Function:   Driver program for testing UUPC/extended      */
  9. /*                      ULIB communications packages                  */
  10. /*--------------------------------------------------------------------*/
  11.  
  12. /*--------------------------------------------------------------------*/
  13. /*       Copyright (c) 1992 by Kendra Electronic Wonderworks;         */
  14. /*       all rights reserved except those explicitly granted          */
  15. /*       through the UUPC/extended license.                           */
  16. /*--------------------------------------------------------------------*/
  17.  
  18. /*
  19.  *    $Id: TESTULIB.C 1.1 1992/11/29 22:09:10 ahd Exp $
  20.  *
  21.  *    $Log: TESTULIB.C $
  22.  * Revision 1.1  1992/11/29  22:09:10  ahd
  23.  * Initial revision
  24.  *
  25.  */
  26.  
  27. /*--------------------------------------------------------------------*/
  28. /*                        System include files                        */
  29. /*--------------------------------------------------------------------*/
  30.  
  31. #include <stdio.h>
  32. #include <io.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #include <signal.h>
  37.  
  38. /*--------------------------------------------------------------------*/
  39. /*                    UUPC/extended include files                     */
  40. /*--------------------------------------------------------------------*/
  41.  
  42. #include "lib.h"
  43. #include "hlib.h"
  44. #include "ulib.h"
  45. #include "catcher.h"
  46. #include "timestmp.h"
  47.  
  48. /*--------------------------------------------------------------------*/
  49. /*                          Global variables                          */
  50. /*--------------------------------------------------------------------*/
  51.  
  52. static char text[] =
  53. "Only the first letter of each command is examined; operands are\n"
  54. "are seperated by white space.  Defaults for commands are in\n"
  55. "parentheses.  Commands may be issued out of sequence, in which case\n"
  56. "the results are unpredictable.\n\n"
  57. "open [port [speed [direct]]]\t(COM1, 2400, and 0 (modem))\n"
  58. "close\n"
  59. "send [text]\t\t\t(this help text)\n"
  60. "Send [file]\t\t\t(console input)\n"
  61. "receive [timeout]\t\t(5 seconds for up to 512 characters)\n"
  62. "Receive [timeout] [file]\t(5 seconds, received.dat)\n"
  63. "hangup\n"
  64. "?\n"
  65. "debuglevel (4)\n"
  66. "quit\n";
  67.  
  68. static int opened = 0;
  69.  
  70. currentfile();
  71.  
  72. /*--------------------------------------------------------------------*/
  73. /*    h e l p                                                         */
  74. /*                                                                    */
  75. /*    Print help text                                                 */
  76. /*--------------------------------------------------------------------*/
  77.  
  78. static void help( void )
  79. {
  80.    puts(text);
  81. }
  82.  
  83. /*--------------------------------------------------------------------*/
  84. /*    o p e n i t                                                     */
  85. /*                                                                    */
  86. /*    Open communications port                                        */
  87. /*--------------------------------------------------------------------*/
  88.  
  89. static void openit( char *buf )
  90. {
  91.    char *token,
  92.         *port = "COM1";
  93.     BPS speed = 2400;
  94.     int direct = 0;
  95.  
  96.    token  = strtok( buf , WHITESPACE );
  97.    if ( token != NULL )
  98.    {
  99.       port = token;
  100.       token  = strtok( NULL , WHITESPACE );
  101.    }
  102.  
  103.    if ( token != NULL )
  104.    {
  105.       speed = atoi(token);
  106.       token  = strtok( NULL , WHITESPACE );
  107.    }
  108.  
  109.    if ( token != NULL )
  110.       direct = atoi(token);
  111.  
  112.    strupr( port );
  113.    printf("openline( %s, %d, %d ) -- ", port, speed, direct );
  114.    if ( openline( port, speed,  direct))
  115.       printf("failed\n");
  116.    else {
  117.       printf("succeeded\n");
  118.       opened = 1;
  119.    }
  120.  
  121. } /* openit */
  122.  
  123. /*--------------------------------------------------------------------*/
  124. /*    s e n d i t                                                     */
  125. /*                                                                    */
  126. /*    Send text to communications port                                */
  127. /*--------------------------------------------------------------------*/
  128.  
  129. static void sendit( char *buf )
  130. {
  131.    char *first = buf;
  132.    int len;
  133.  
  134.    if ( ! opened )
  135.    {
  136.       puts("Port isn't open ... issue o command first");
  137.       return;
  138.    }
  139.  
  140.    while( (first != NULL ) && *first && ! isprint(*first))
  141.       first++;
  142.  
  143.    if (( first == NULL ) || ! (*first))
  144.       first = text;
  145.    else
  146.       strcat( first, "\r\n" );
  147.  
  148.    len = strlen( first );
  149.    printf( "swrite( <text>, %d) -- ", len );
  150.    len = swrite( first, len );
  151.    printf("%d characters written\n", len);
  152.  
  153. } /* sendit */
  154.  
  155. /*--------------------------------------------------------------------*/
  156. /*    r e c v e i v e i t                                             */
  157. /*                                                                    */
  158. /*    Receive data to console from serial port                        */
  159. /*--------------------------------------------------------------------*/
  160.  
  161. static void receiveit( char *buf )
  162. {
  163.  
  164.    char *token;
  165.    int timeout = 5;
  166.    int len = 512;
  167.    int actual = 0;
  168.  
  169.    if ( ! opened )
  170.    {
  171.       puts("Port isn't open ... issue o command first");
  172.       return;
  173.    }
  174.  
  175.    token  = strtok( buf , WHITESPACE );
  176.    if ( token != NULL )
  177.    {
  178.       timeout = atoi( token );
  179.       token  = strtok( NULL , WHITESPACE );
  180.    }
  181.  
  182.    if ( token != NULL )
  183.       len = atoi( token );
  184.  
  185.    token = malloc( len + 1 );
  186.  
  187.    while( (actual < len))
  188.    {
  189.       if ( actual )
  190.          len = actual;
  191.       printf("sread( <buffer>,  %d, %d ) -- ", len, timeout );
  192.       actual = sread( token, len, timeout );
  193.       printf( "%d characters available\n", actual);
  194.       timeout = 0;
  195.  
  196.       if ( terminate_processing )
  197.          return;
  198.  
  199.       if ( !actual )
  200.          break;
  201.    }
  202.  
  203.    if ( actual )
  204.    {
  205.       token[actual] = '\0';
  206.       puts(token);
  207.    } /* if */
  208.  
  209.    free( token );
  210.  
  211. } /* receiveit */
  212.  
  213. /*--------------------------------------------------------------------*/
  214. /*    s e n d f i l e                                                 */
  215. /*                                                                    */
  216. /*    Send text to communications port from file                      */
  217. /*--------------------------------------------------------------------*/
  218.  
  219. static void sendfile( char *buf )
  220. {
  221.    char *fname = strtok( buf, WHITESPACE );
  222.  
  223.    FILE *stream;
  224.  
  225.    if ( ! opened )
  226.    {
  227.       puts("Port isn't open ... issue o command first");
  228.       return;
  229.    }
  230.  
  231.    if ( fname == NULL )
  232.       fname = "CON";
  233.  
  234.    stream = fopen( fname, "rb" );
  235.  
  236.    if ( stream == NULL )
  237.    {
  238.       perror( fname );
  239.       return;
  240.    }
  241.  
  242.    printf("Reading data from %s:\n",fname );
  243.  
  244.    for ( ;; )
  245.    {
  246.       char buf[BUFSIZ];
  247.       int len;
  248.       int actual;
  249.  
  250.       len = fread( buf, sizeof *buf, sizeof buf, stream );
  251.       if ( ferror( stream ))
  252.       {
  253.          perror( fname );
  254.          return;
  255.       }
  256.  
  257.       if ( feof( stream ))
  258.       {
  259.          puts("EOF\n");
  260.          return;
  261.       }
  262.  
  263.       printf( "swrite( <text>, %d) -- ", len );
  264.       actual = swrite( buf, len );
  265.       printf("%d characters written\n", actual);
  266.  
  267.       if ( terminate_processing )
  268.         return;
  269.  
  270.       if ( actual != len)
  271.          break;
  272.    }
  273.  
  274. } /* sendfile */
  275.  
  276. /*--------------------------------------------------------------------*/
  277. /*    r e c e i v e f i l e                                           */
  278. /*                                                                    */
  279. /*    Receive data to console from serial port                        */
  280. /*--------------------------------------------------------------------*/
  281.  
  282. static void receivefile( char *buf )
  283. {
  284.  
  285.    char *token;
  286.    char *fname = "received.dat";
  287.    int timeout = 5;
  288.    FILE *stream;
  289.    int len = BUFSIZ;
  290.  
  291.    if ( ! opened )
  292.    {
  293.       puts("Port isn't open ... issue o command first");
  294.       return;
  295.    }
  296.  
  297.    token  = strtok( buf , WHITESPACE );
  298.    if ( token != NULL )
  299.    {
  300.       timeout = atoi( token );
  301.       token  = strtok( NULL , WHITESPACE );
  302.    }
  303.  
  304.    if ( token != NULL )
  305.    {
  306.       len = atoi( token );
  307.       token  = strtok( NULL , WHITESPACE );
  308.    }
  309.  
  310.    if ( token != NULL )
  311.       fname = token;
  312.  
  313.    stream  = fopen( fname, "ab" );
  314.    if ( stream == NULL )
  315.    {
  316.       perror( fname );
  317.       return;
  318.    }
  319.  
  320.    for ( ;; )
  321.    {
  322.  
  323.       char buf[512];
  324.       int actual = 0;
  325.  
  326.       while( (len > actual) && (len) )
  327.       {
  328.          if ( actual )
  329.          {
  330.             len = actual;
  331.             actual = 0;
  332.             timeout = 0;
  333.          }
  334.  
  335.          printf("sread( <buffer>,  %d, %d ) -- ", len, timeout );
  336.          actual = sread( buf, len, timeout );
  337.          printf( "%d characters available\n", actual);
  338.  
  339.          if ( terminate_processing )
  340.              return;
  341.  
  342.       } /* while */
  343.  
  344.       if ( len > 0 )
  345.       {
  346.          actual = fwrite( buf, sizeof *buf, len, stream );
  347.  
  348.          if ( actual != len )
  349.          {
  350.             perror( fname );
  351.             return;
  352.          } /* if */
  353.  
  354.       } /* if */
  355.       else
  356.          break;
  357.  
  358.    } /* for */
  359.  
  360.    fclose( stream );
  361.  
  362. } /* receivefile */
  363.  
  364. static void shutdown( void )
  365. {
  366.    if ( opened )
  367.       closeline();
  368. }
  369.  
  370. /*--------------------------------------------------------------------*/
  371. /*    m a i n                                                         */
  372. /*                                                                    */
  373. /*    main program                                                    */
  374. /*--------------------------------------------------------------------*/
  375.  
  376. void main( int argc, char ** argv )
  377. {
  378.    char buf[BUFSIZ];
  379.    int done = 0;
  380.    char *next, *command;
  381.  
  382.    banner( argv );
  383.  
  384.    if (!configure( B_MUA ))
  385.       exit(1);    /* system configuration failed */
  386.  
  387.    debuglevel = 4;
  388.  
  389. /*--------------------------------------------------------------------*/
  390. /*                        Trap control C exits                        */
  391. /*--------------------------------------------------------------------*/
  392.  
  393.     if( signal( SIGINT, ctrlchandler ) == SIG_ERR )
  394.     {
  395.         printmsg( 0, "Couldn't set SIGINT\n" );
  396.         panic();
  397.     }
  398.  
  399.    if (!configure( B_MUA ))
  400.       exit(1);    /* system configuration failed */
  401.  
  402. /*--------------------------------------------------------------------*/
  403. /*                 Insure comm port is closed at exit                 */
  404. /*--------------------------------------------------------------------*/
  405.  
  406.    atexit( shutdown );
  407.    interactive_processing = FALSE;     /* Quit immediately           */
  408.  
  409.    while ( ! done )
  410.    {
  411.       if ( terminate_processing )
  412.          break;
  413.  
  414.       if ( opened )
  415.          printf("Opened, debuglevel %d, CD reports: %s\n",
  416.                   debuglevel,
  417.                   CD() ? "TRUE" : "FALSE" );
  418.       else
  419.          printf("Closed, debuglevel %d\n", debuglevel );
  420.  
  421.       printf("Enter command (? for help): ");
  422.  
  423.       if ( terminate_processing )
  424.          break;
  425.  
  426.       if ( fgets( buf, sizeof buf, stdin ) == NULL)
  427.          break;
  428.  
  429.       fputc('\n',stdout );
  430.       command = strtok( buf, WHITESPACE );
  431.       next    = strtok( NULL, "\r\n" );
  432.  
  433.       switch( *command )
  434.       {
  435.          case 'o':
  436.             openit( next );
  437.             break;
  438.  
  439.          case 'c':
  440.             printf("closeline() --");
  441.             closeline();
  442.             opened = 0;
  443.             fputc('\n', stdout);
  444.             break;
  445.  
  446.          case 's':
  447.             sendit( next );
  448.             break;
  449.  
  450.          case 'r':
  451.             receiveit( next );
  452.             break;
  453.  
  454.          case 'S':
  455.             sendfile( next );
  456.             break;
  457.  
  458.          case 'R':
  459.             receivefile( next );
  460.             break;
  461.  
  462.          case 'h':
  463.             printf("hangup() --");
  464.             hangup();
  465.             fputc('\n', stdout);
  466.             break;
  467.  
  468.          case '?':
  469.             help();
  470.             break;
  471.  
  472.          case 'q':
  473.             done = 1;
  474.             break;
  475.  
  476.          case 'd':
  477.             next = strtok( next, WHITESPACE );
  478.             if ( next != NULL )
  479.                debuglevel = atoi( next );
  480.             else
  481.                debuglevel = 4;
  482.             break;
  483.  
  484.          default:
  485.             printf("Invalid command '%c', try ? for help\n",
  486.                      *command );
  487.             break;
  488.  
  489.       } /* switch */
  490.    } /* for */
  491.  
  492.    exit(0);
  493.  
  494. } /* main */
  495.