home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d109 / uupc.lha / UUpc / Source / dcp.c next >
C/C++ Source or Header  |  1987-10-28  |  8KB  |  463 lines

  1. /*            dcp.c
  2.  
  3.             Revised edition of dcp
  4.  
  5.             Stuart Lynne May/87
  6.  
  7.             Copyright (c) Richard H. Lamb 1985, 1986, 1987
  8.             Changes Copyright (c) Stuart Lynne 1987
  9. Maintenance Notes:
  10.   25Aug87 - Added a version number - Jal
  11.   25Aug87 - Return 0 if contact made with host, or 5 otherwise.
  12.   04Sep87 - Bug causing premature sysend() fixed. - Randall Jessup.
  13.  
  14. */
  15. /* "DCP" a uucp clone. Copyright Richard H. Lamb 1985,1986,1987 */
  16. /* This program implements a uucico type file transfer and remote 
  17. execution type protocol. 
  18. */
  19. #define  VERSION "1.0"
  20. #include "dcp.h"
  21. /* */
  22. int    pktsize;                    /* packet size for pro */
  23. FILE    *logfile;                /* system log file */
  24. FILE    *syslog;                /* system log file */
  25. FILE    *fw;                     /* cfile pointer */
  26. char    state;                    /* system state*/
  27. char    cfile[80];              /* work file pointer */
  28. int        remote;                 /* -1 means we're remote ..7*/
  29. int        msgtime;                /* timout setting */
  30. char    fromfile[132];
  31. char    hostfile[132];        /* host version of fromfile */
  32. char    tofile[132];
  33. int        fp;                     /* current disk file ptr */
  34. int        size;                   /* nbytes in buff */
  35. FILE    *fsys;
  36. char    Rmtname[20];
  37. char    rmtname[20];
  38. char    * cctime;
  39. char    proto[5];
  40. /* char    loginseq[256]; */
  41. char     sysline[BUFSIZ];
  42. char    s_systems[64];
  43. char    s_logfile[64];
  44. char    s_syslog[64];
  45. char     *flds[60];
  46. int     kflds;
  47. int        debuglevel;        /* debugging flag */
  48.  
  49. unsigned int    checksum();
  50.  
  51.  
  52. /* */
  53. /* new usage
  54.  
  55.     dcp    [-xn] -r0        slave mode
  56.     dcp    [-xn] -shost         call host
  57.     dcp    [-xn] -sall        call all hosts
  58.     
  59.     dcp    [-xn]         call any hosts as required by C. files
  60.         
  61. */
  62.  
  63. complain( s )
  64. char *s;
  65. {
  66.     fprintf( stderr, "Please set your %s environment variable.", s );
  67. }
  68.  
  69. static void cant( file )
  70. char *file;
  71. {
  72.     fprintf( stderr, "Can't open: \"%s\"\n", file );
  73.     exit( 8 );
  74. }
  75.  
  76. dcpmain( argc, argv )
  77. int    argc;
  78. char*argv[];
  79. {
  80.     FILE    *ftmp;
  81.     char    line[132];
  82.     int    Contact = FALSE;
  83.  
  84.     if ( name == NULL || *name == '\0' ) {
  85.         complain( NAME );
  86.         exit( -1 );
  87.     }
  88.     if ( nodename == NULL || *nodename == '\0' ) {
  89.         complain( NODENAME );
  90.         exit( -1 );
  91.     }
  92.         
  93.  
  94.     mkfilename( s_logfile, spooldir, LOGFILE );
  95.     mkfilename( s_syslog,  spooldir, SYSLOG  );
  96.     mkfilename( s_systems, confdir,  SYSTEMS );
  97.  
  98.     if ( (logfile = FOPEN( s_logfile, "a", 't' )) == NULL )
  99.        cant( s_logfile );
  100.     if ( (syslog  = FOPEN( s_syslog,  "a", 't' )) == NULL )
  101.        cant( s_syslog );
  102.     
  103.     fp = -1;
  104.     fw = (FILE *)NULL;
  105.     remote = MASTER;
  106.     strcpy( Rmtname, "any" );
  107.     debuglevel = 1;
  108.     remote = MASTER;
  109.     printmsg(0, "\f\t\t\tUUPC Version %s\n", VERSION );
  110.  
  111.  
  112.     while ( --argc ) {
  113.         if ( **++argv == '-') {
  114.             switch(argv[0][1]) {
  115.             case 'x':
  116.                 debuglevel = atoi( &argv[0][2] );
  117.                 break;
  118.             case 's':
  119.                 sprintf( Rmtname, "%.7s", &argv[0][2] );
  120.                 break;
  121.             case 'r':
  122.                 remote = atoi( &argv[0][2] );
  123.                 break;
  124.                 
  125.             default:
  126.                 break;
  127.             }
  128.         }
  129.     }
  130.         
  131.  
  132.     if ( remote == MASTER ) {
  133.         printmsg( 0, "Calling %s    %d", Rmtname, debuglevel );
  134.         if (( fsys = FOPEN( s_systems, "r", 't' )) == (char *)NULL ) 
  135.             exit( FAILED );
  136.         state = 'I';
  137.         
  138.  
  139.         while (TRUE) {
  140.             printmsg( 4, "Mstate = %c", state );
  141.             switch (state) {
  142.             case 'I': 
  143.                 state = getsystem();    
  144.                 break;
  145.             case 'S': 
  146.                 state = callup();       
  147.                 break;
  148.             case 'P': 
  149.                 state = startup();      
  150.                 break;
  151.             case 'D': 
  152.                 state = master();       
  153.                 Contact = TRUE;
  154.                 break;
  155.             case 'Y': 
  156.                 state = sysend();       
  157.                 break;
  158.             case 'G': 
  159.                 if ( strcmp( Rmtname, "any" ) == SAME )
  160.                     state = 'Y';
  161.                 else
  162.                     state = 'I';            
  163.                 break;
  164.             }
  165.             if (state == 'A') 
  166.                 break;
  167.         }
  168.         fclose( fsys );
  169.     } else
  170.      {
  171.         if (openline( device, speed ) == -1) 
  172.             return(FALSE);
  173.         state = 'L';
  174.         while (TRUE) {
  175.             printmsg( 4, "Sstate = %c", state );
  176.             switch (state) {
  177.             case 'L':
  178.                 state = login();     
  179.                 break;
  180.             case 'I':
  181.                 state = startup();   
  182.                 break;
  183.             case 'R':
  184.                 state = slave();     
  185.                 break;
  186.             case 'Y':
  187.                 state = sysend();    
  188.                 break;
  189.             }
  190.             if (state == 'A') 
  191.                 break;
  192.         }
  193.         closeline();
  194.     }
  195.  
  196.  
  197.     /* fprintf( stderr, "calling dcxqt\n" ); */
  198.     if (dcxqt()) 
  199.         printmsg( 0, "ERROR in DCXQT" );
  200.             
  201.     /* scan and process any recieved files */
  202.  
  203.     fclose( syslog );
  204.     fclose( logfile );
  205.     if (Contact )
  206.        return 0;
  207.     else
  208.        return 5;
  209. }
  210.  
  211.  
  212. /* */
  213. /*
  214. **
  215. **
  216. **master
  217. **
  218. **
  219. */
  220. master()
  221. {
  222.     state = 'I';
  223.     while (TRUE) {
  224.         printmsg( 4, "Top level state (master mode) %c", state );
  225.         switch (state) {
  226.         case 'I':
  227.             state = sinit();      
  228.             break;
  229.         case 'B':
  230.             state = scandir();    
  231.             break;
  232.         case 'S':
  233.             state = send();       
  234.             break;
  235.         case 'Q':
  236.             state = sbreak();     
  237.             break;
  238.         case 'G':
  239.             state = receive();    
  240.             break;
  241.         case 'C':
  242.             state = 'Y';          
  243.             break;
  244.         case 'Y':
  245.             state = endp();       
  246.             break;
  247.         case 'P':
  248.             return('Y');
  249.         case 'A':
  250.             return('A');
  251.         default:
  252.             return('A');
  253.         }
  254.     }
  255. }
  256.  
  257.  
  258. /* */
  259. /*
  260. **
  261. **
  262. **slave
  263. **
  264. **
  265. */
  266. slave()
  267. {
  268.     state = 'I';
  269.     while (TRUE) {
  270.         printmsg( 4, "Top level state (slave mode) %c", state );
  271.         switch (state) {
  272.         case 'I':
  273.             state = rinit();      
  274.             break;
  275.         case 'F':
  276.             state = receive();    
  277.             break;
  278.         case 'C':
  279.             state = schkdir();    
  280.             break;
  281.         case 'T':
  282.             state = 'B';          
  283.             break;
  284.         case 'B':
  285.             state = scandir();    
  286.             break;
  287.         case 'S':
  288.             state = send();       
  289.             break;
  290.         case 'Q':
  291.             state = sbreak();     
  292.             break;
  293.         case 'G':
  294.             return('Y');
  295.         case 'Y':
  296.             state = endp();       
  297.             break;
  298.         case 'P':
  299.             return('Y');
  300.         case 'A':
  301.             return('A');
  302.         default:
  303.             return('A');
  304.         }
  305.     }
  306. }
  307.  
  308.  
  309. /* */
  310. /*
  311.  *  r e c e i v e
  312.  *
  313.  *  This is the state table switcher for receiving files.
  314.  */
  315.  
  316. receive()
  317. {
  318.  
  319.     state = 'F';/* Receive-Init is the start state */
  320.  
  321.     while (TRUE) {
  322.         printmsg( 4, " receive state: %c", state );
  323.         switch (state)/* Do until done */ {
  324.         case 'F':
  325.             state = rfile(); 
  326.             break; /* Receive-File */
  327.         case 'D':
  328.             state = rdata(); 
  329.             break; /* Receive-Data */
  330.         case 'C':
  331.             return('C');/* Complete state */
  332.         case 'A':
  333.             return('Y');/* "Abort" state */
  334.         default:
  335.             return('Y');
  336.         }
  337.     }
  338. }
  339.  
  340.  
  341. /* */
  342. /*
  343.  *  s e n d 
  344.  *
  345.  *  Sendsw is the state table switcher for sending files.  It loops until
  346.  *  either it finishes, or an error is encountered.  The routines called
  347.  *  by sendsw are responsible for changing the state.
  348.  *
  349.  */
  350. send()
  351. {
  352.     fp = -1;                /* reset file getter/opener */
  353.     state = 'F';/* Send initiate is the start state */
  354.     while (TRUE)/* Do this as long as necessary */ {
  355.         printmsg( 4, "send state: %c", state );
  356.         switch (state) {
  357.         case 'F':
  358.             state = sfile();  
  359.             break; /* Send-File */
  360.         case 'D':
  361.             state = sdata();  
  362.             break; /* Send-Data */
  363.         case 'Z':
  364.             state = seof();  
  365.             break; /* Send-End-of-File */
  366.         case 'B':
  367.             return ('B'); /* Complete */
  368.         case 'A':
  369.             return ('Y'); /* "Abort" */
  370.         default:
  371.             return ('Y'); /* Unknown, fail */
  372.         }
  373.     }
  374. }
  375.  
  376.  
  377. /* */
  378. /* A command formatter for DCP. RH Lamb */
  379. /* sets up stdin and stdout on various machines */
  380. /* There is NO command checking so watch what you send and who you */
  381. /* let accsess your machine. "C rm /usr/*.*" could be executed. */
  382. dcxqt()
  383. {
  384.     int    i;
  385.     char    command[60], input[60], output[60], line[BUFSIZ];
  386.     char    *cp;
  387.     
  388.     while (dscandir()) {
  389.         strcpy( line, cfile );
  390.         fw = FOPEN( line, "r", 'b' );/* imported X file */
  391.         strcpy(cfile, line);
  392.         printmsg( 2, "dcxqt:%s %ld", cfile, fw );
  393.         input[0]   = '\0';
  394.         output[0]  = '\0';
  395.         command[0] = '\0';
  396.         while ( fgets( line, BUFSIZ, fw ) != (char *)NULL ) {
  397.  
  398.             cp = index( line, '\n' );
  399.             if ( cp != (char *)NULL )
  400.                 *cp = '\0';
  401.  
  402.             printmsg( 8, "dcxqt: %s", line );
  403.             switch (line[0]) {
  404.             case 'U':       
  405.                 break;
  406.             case 'I':
  407.                 strcpy( input,   &line[2] );
  408.                 break;
  409.             case 'O':       
  410.                 strcpy( output,  &line[2] );
  411.                 break;
  412.             case 'C':
  413.                 strcpy( command, &line[2] );
  414.                 break;
  415.             case 'R':       
  416.                 break;
  417.             default :       
  418.                 break;
  419.             }
  420.         }
  421.         fclose( fw );
  422.  
  423.         printmsg( 0, "xqt: %s\n", command );
  424.             
  425.         shell( command, input, output, (char *)NULL );
  426.         
  427.         
  428.         unlink(cfile);
  429.          importpath( hostfile, input );
  430.          unlink(hostfile);
  431.         importpath( hostfile, output );
  432.          unlink(hostfile);
  433.     }
  434.     return(0);
  435. }
  436.  
  437. /**/
  438. /*
  439.  *  p r i n t m s g
  440.  *
  441.  *  Print error message on standard output if not remote.
  442.  */
  443. /*VARARGS1*/
  444. printmsg(level, fmt, a1, a2, a3, a4, a5)
  445. int     level;
  446. char    *fmt;
  447. char    *a1, *a2, *a3, *a4, *a5;
  448. {
  449.     char    msg[256];
  450.  
  451.     if ( debuglevel > level ) {
  452.         sprintf( msg, fmt, a1, a2, a3, a4, a5 );
  453.         strcat( msg, "\n" );
  454.         if ( remote == MASTER ) 
  455.             fputs( msg, stdout );
  456.         fputs( msg, logfile );
  457.     }
  458. }
  459.  
  460.  
  461.  
  462.  
  463.