home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff369.lzh / VaxTerm / src / transfer.c < prev   
C/C++ Source or Header  |  1990-08-15  |  15KB  |  517 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <exec/types.h>
  4. #include <devices/keymap.h>
  5. #include <libraries/dos.h>
  6. #include <graphics/sprite.h>
  7. #include <intuition/intuition.h>
  8. #include <proto/dos.h>
  9. #include <proto/exec.h>
  10.  
  11. #include "main.h"
  12.  
  13. #define SEOF        "\x1A"
  14.  
  15. #define TERMCHAR    '%'
  16. #define TERMLEN     4
  17. #define TERMSTRING  "%%%%"
  18. #define TERMDEL     "\x7f\x7f\x7f\x7f"
  19.  
  20. #define DEVLEN      32
  21. #define COMLEN      128
  22. #define PATHLEN     128
  23. #define PACKET      512
  24. #define MAXLINE     512
  25.  
  26. #define PAUSE       (50 * 2)
  27. #define ATTR        0
  28.  
  29. transfer(register struct console *con)
  30. {
  31. register char   *cp;
  32. USHORT          len;
  33. BOOL            stat = TRUE;
  34.  
  35. conprint(con,"\nS = send, R = receive, Q = Quit, F4 = host setup ? ");
  36. while (stat) {
  37.     len = readconsole(con);
  38.     *(con->buf + len) = '\0';
  39.     cp = con->buf;
  40.     while ((*cp)&&(stat)) {
  41.         switch (*cp) {
  42.             case 'r':
  43.             case 'R':
  44.                 conprint(con,"R");
  45.                 receive(con);
  46.                 stat = FALSE;
  47.                 break;
  48.             case 's':
  49.             case 'S':
  50.                 conprint(con,"S");
  51.                 send(con);
  52.                 stat = FALSE;
  53.                 break;
  54.             case 'q':
  55.             case 'Q':
  56.                 con->gstat &= (MASK - ON);
  57.                 stat = FALSE;
  58.                 break;
  59.             case PU1:
  60.                 conprint(con,"F4\n");
  61.                 host_setup(con);
  62.                 stat = FALSE;
  63.                 break;
  64.             default:
  65.                 writeserial(con,"\x0D",1);
  66.                 stat = FALSE;
  67.                 break;
  68.             }
  69.         cp++;
  70.         }
  71.     }
  72. return(0);
  73. }
  74.  
  75. host_setup(register struct console *con)
  76. {
  77. FILEHANDLE          fp;
  78. register USHORT     len;
  79. char                c;
  80.  
  81. if (fp = OPEN_FOR_READING(HOSTSETUP)) {
  82.     while (READ(fp,&c,1)) {
  83.         if (c == LF) c = CR;
  84.         writeserial(con,&c,1);
  85.         len = readserial(con,0);
  86.         if (len) interpret(con,len);
  87.         }
  88.     CLOSEFILE(fp);
  89.     }
  90. return(0);
  91. }
  92.  
  93. send(register struct console *con)
  94. {
  95. struct FileInfoBlock    fib;
  96. FILEHANDLE              fp;
  97. register USHORT         cnt,lenght,termcount,error,stat;
  98. register char           *ccp,*cp;
  99. char                    dest[PATHLEN],filename[PATHLEN];
  100. char                    command[COMLEN],files[COMLEN];
  101. char                    device[] = "SYS$INPUT";
  102. char                    readsync[] = "SET TERMINAL/READSYNC/NOPASTHRU\x0D";
  103. char                    vms[] = "\x0D",notopen[] = "Can't open file %s\n";
  104.  
  105. sendstring(con,readsync);
  106. stat = TRUE;
  107. ccp = readsync;
  108. while (stat) {
  109.     cnt = readserial(con,0);
  110.     cp = con->buf;
  111.     while (cnt) {
  112.         if (*cp++ != *ccp++) ccp = readsync;
  113.         if (!*ccp) {
  114.             stat = FALSE;
  115.             break;
  116.             }
  117.         cnt--;
  118.         }
  119.     }
  120. stat = TRUE;
  121. while (cnt) {
  122.     if (*cp++ == LF) {
  123.         stat = FALSE;
  124.         break;
  125.         }
  126.     cnt--;
  127.     }
  128. while (stat) {
  129.     cnt = readserial(con,0);
  130.     cp = con->buf;
  131.     while (cnt) {
  132.         if (*cp++ == LF) {
  133.             stat = FALSE;
  134.             break;
  135.             }
  136.         cnt--;
  137.         }
  138.     }
  139. conprint(con,"\nVMS destination directory: ");
  140. congets(con,dest);
  141. forever {
  142.     conprint(con,"AmigaDOS file expression: ");
  143.     congets(con,files);
  144.     if (files[0] == '\0') {
  145.         sendstring(con,vms);
  146.         return(0);
  147.         }
  148.     error = dfind(&fib,files,ATTR);
  149.     cnt = strlen(files);
  150.     cp = files + cnt - 1;
  151.     while ((cnt)&&(*cp != ':')&&(*cp != '/')) {
  152.         cp--;
  153.         cnt--;
  154.         }
  155.     cp++;
  156.     *cp = '\0';
  157.     while (!error) {
  158.         strcpy(filename,files);
  159.         strcat(filename,fib.fib_FileName);
  160.         fp = OPEN_FOR_READING(filename);
  161.         if (fp == NULL) {
  162.             sprintf(command,notopen,fib.fib_FileName);
  163.             conprint(con,command);
  164.             sendstring(con,vms);
  165.             return(0);
  166.             }
  167.         sprintf(command,"sending file: %s\n",fib.fib_FileName);
  168.         conprint(con,command);
  169.         sprintf(command,"COPY %s %s%s\x0D",device,dest,fib.fib_FileName);
  170.         sendstring(con,command);
  171.         ccp = command;
  172.         while (stat) {
  173.             cnt = readserial(con,0);
  174.             cp = con->buf;
  175.             while (cnt) {
  176.                 if (*cp++ != *ccp++) ccp = command;
  177.                 if (!*ccp) {
  178.                     stat = FALSE;
  179.                     break;
  180.                     }
  181.                 cnt--;
  182.                 }
  183.             }
  184.         stat = TRUE;
  185.         while (cnt) {
  186.             if (*cp++ == LF) {
  187.                 stat = FALSE;
  188.                 break;
  189.                 }
  190.             cnt--;
  191.             }
  192.         while (stat) {
  193.             cnt = readserial(con,0);
  194.             cp = con->buf;
  195.             while (cnt) {
  196.                 if (*cp++ == LF) {
  197.                     stat = FALSE;
  198.                     break;
  199.                     }
  200.                 cnt--;
  201.                 }
  202.             }
  203.         for (cnt = 0; cnt < PAUSE; cnt++) WaitTOF();
  204.         lenght = PACKET;
  205.         while (lenght == PACKET) {
  206.             lenght = READ(fp,con->conv,PACKET);
  207.             cp = con->conv;
  208.             cnt = lenght;
  209.             while (cnt--) {
  210.                 if (*cp == LF) *cp = CR;
  211.                 cp++;
  212.                 }
  213.             if (lenght)
  214.                 if (writeasync(con,lenght)) break;
  215.             while (readserial(con,0));
  216.             }
  217.         CLOSEFILE(fp);
  218.         sendstring(con,SEOF);
  219.         readserial(con,0);
  220.         sendstring(con,TERMSTRING);
  221.         termcount = 0;
  222.         while (termcount < TERMLEN) {  
  223.             lenght = readserial(con,0);
  224.             cp = con->buf;
  225.             while (lenght) {
  226.                 if (*cp++ == TERMCHAR) termcount++; else termcount = 0;
  227.                 lenght--;
  228.                 }
  229.             }
  230.         sendstring(con,TERMDEL);
  231.         lenght = 0;
  232.         while (lenght < TERMLEN) lenght += readserial(con,0);
  233.         error = dnext(&fib);
  234.         }
  235.     }
  236. }
  237.  
  238. receive(register struct console *con)
  239. {
  240. BOOL                wild,stat;
  241. FILEHANDLE          fp;
  242. register USHORT     lenght,bufcount,termcount,len,tlen;
  243. USHORT              saved,first;
  244. register char       *cp,*dcp,*icp,*name,*outbuf;
  245. char                filename[COMLEN],line[MAXLINE];
  246. char                files[COMLEN],command[COMLEN],dest[PATHLEN];
  247. char                vms[] = "\x0D",notopen[] = "Can't open file %s\n";
  248.  
  249. outbuf = con->conv;
  250. conprint(con,"\nAmigaDOS destination directory: ");
  251. congets(con,dest);
  252. if (dest[0]) {
  253.     cp = dest + strlen(dest) - 1;
  254.     if ((*cp != ':')&&(*cp != '/')) {
  255.         cp++;
  256.         *cp++ = '/';
  257.         *cp = '\0';              
  258.         }
  259.     }
  260. forever {
  261.     conprint(con,"VMS file expression: ");
  262.     congets(con,files);
  263.     if (files[0] == '\0') {
  264.         sendstring(con,vms);
  265.         return(0);
  266.         }
  267.     if (strchr(files,'*') != NULL) {
  268.         wild = TRUE;
  269.         fp = NULL;
  270.         }
  271.     else {
  272.         wild = FALSE;
  273.         strcpy(filename,dest);
  274.         lenght = strlen(files);
  275.         cp = files + lenght - 1;
  276.         while (lenght--) {
  277.             if ((*cp == ']')||(*cp == '>')||(*cp == ':'))
  278.                 break;
  279.             cp--;
  280.             }
  281.         cp++;
  282.         strcat(filename,cp);
  283.         fp = OPEN_FOR_WRITING(filename);
  284.         if (fp == NULL) {
  285.             sprintf(command,notopen,filename);
  286.             conprint(con,command);
  287.             sendstring(con,vms);
  288.             return(0);
  289.             }
  290.         }
  291.     sprintf(command,"type %s\x0D",files);
  292.     sendstring(con,command);
  293.     stat = TRUE;
  294.     while (stat) {
  295.         tlen = readserial(con,0);
  296.         cp = con->buf;
  297.         while (tlen) {
  298.             tlen--;
  299.             if (*cp++ == LF) {
  300.                 stat = FALSE;
  301.                 break;
  302.                 }
  303.             }
  304.         }
  305.     sendstring(con,TERMSTRING);
  306.     if (!wild) {
  307.         sprintf(command,"receiving file: %s\n",filename);
  308.         conprint(con,command);
  309.         }
  310.     termcount = 0;
  311.     saved = 0;
  312.     bufcount = 0;
  313.     dcp = line;
  314.     lenght = 0;
  315.     first = 0;
  316.     while (termcount < TERMLEN) {
  317.         if (tlen) {
  318.             icp = cp;
  319.             len = tlen;
  320.             tlen = 0;
  321.             }
  322.         else {
  323.             len = readserial(con,0);
  324.             icp = con->buf;
  325.             }
  326.         while (len) {
  327.             if ((*icp == CR)||(!*icp)) {
  328.                 icp++;
  329.                 len--;
  330.                 }
  331.             if (len) {
  332.                 if (*icp == TERMCHAR) termcount++; else termcount = 0;
  333.                 if ((*icp == LF)||(lenght == MAXLINE-2)) {
  334.                     if (first) {
  335.                         first = 0;
  336.                         lenght = 0;
  337.                         }
  338.                     else {
  339.                         *dcp++ = *icp;
  340.                         lenght++;
  341.                         }
  342.                     *dcp = '\0';
  343.                     cp = line;
  344.                     if ((wild)&&(*cp != '%')) {
  345.                         while ((cp = strchr(cp,':')) != NULL) {
  346.                             cp++;
  347.                             if ((*cp == '[')||(*cp == '<')) {
  348.                                 if ((name = strchr(cp,']')) == NULL)
  349.                                      name = strchr(cp,'>');
  350.                                 if (name != NULL) {
  351.                                     name++;
  352.                                     if ((cp = strchr(name,';')) != NULL) {
  353.                                         *cp = '\0';
  354.                                         if (fp != NULL) {
  355.                                             WRITE(fp,outbuf,bufcount);
  356.                                             bufcount = 0;
  357.                                             }
  358.                                         dcp = line;
  359.                                         lenght = 0;
  360.                                         saved = 0;
  361.                                         first = 1;
  362.                                         if (fp != NULL) CLOSEFILE(fp);
  363.                                         strcpy(filename,dest);
  364.                                         strcat(filename,name);
  365.                                         fp = OPEN_FOR_WRITING(filename);
  366.                                         if (fp == NULL) {
  367.                                             sprintf(command,notopen,filename);
  368.                                             conprint(con,command);
  369.                                             sendstring(con,vms);
  370.                                             return(0);
  371.                                             }
  372.                                         sprintf(command,
  373.                                             "receiving file: %s\n",name);
  374.                                         conprint(con,command);
  375.                                         }
  376.                                     }
  377.                                 }
  378.                             }
  379.                         }
  380.                     if (fp != NULL) {
  381.                         cp = line + lenght - 2;
  382.                         while ((lenght > 1)&&(*cp <= ' ')&&(*cp >= '\0')) {
  383.                             *cp = LF;
  384.                             cp--;
  385.                             lenght--;
  386.                             }
  387.                         if (lenght == 1) {
  388.                             lenght = 0;
  389.                             saved++;
  390.                             }
  391.                         dcp = outbuf + bufcount;
  392.                         if (saved == 2) {
  393.                             *dcp++ = LF;
  394.                             bufcount++;
  395.                             if (bufcount > CONVSIZE-2) {
  396.                                 WRITE(fp,outbuf,bufcount);
  397.                                 bufcount = 0;
  398.                                 dcp = outbuf;
  399.                                 }
  400.                             saved = 1;
  401.                             }
  402.                         if (lenght) {
  403.                             if (saved) {
  404.                                 *dcp++ = LF;
  405.                                 bufcount++;
  406.                                 saved = 0;
  407.                                 }
  408.                             cp = line;
  409.                             while (lenght) {
  410.                                 *dcp++ = *cp++;
  411.                                 bufcount++;
  412.                                 if (bufcount > CONVSIZE-2) {
  413.                                     WRITE(fp,outbuf,bufcount);
  414.                                     bufcount = 0;
  415.                                     dcp = outbuf;
  416.                                     }
  417.                                 lenght--;
  418.                                 }
  419.                             }
  420.                         }
  421.                     dcp = line;
  422.                     lenght = 0;
  423.                     }
  424.                 else {
  425.                     *dcp++ = *icp;
  426.                     lenght++;
  427.                     }
  428.                 len--;
  429.                 icp++;
  430.                 }
  431.             }
  432.         }
  433.     if (fp != NULL) {
  434.         if (bufcount) WRITE(fp,outbuf,bufcount);
  435.         CLOSEFILE(fp);
  436.         }
  437.     sendstring(con,TERMDEL);
  438.     lenght = 0;
  439.     while (lenght < TERMLEN) lenght += readserial(con,0);
  440.     }
  441. }
  442.  
  443. conprint(register struct console *con,register char *cp)
  444. {
  445. while(*cp) {
  446.     if ((*cp == BS)||(*cp == DEL)) {
  447.         *con->buf = BS;
  448.         interpret(con,1);
  449.         *con->buf = ' ';
  450.         interpret(con,1);
  451.         *cp = BS;
  452.         }
  453.     if (*cp == LF) commit(con);
  454.     else {
  455.         *con->buf = *cp;
  456.         interpret(con,1);
  457.         }
  458.     cp++;
  459.     }
  460. cursorout(con);
  461. return(0);
  462. }
  463.  
  464. congets(register struct console *con,register char *cp)
  465. {
  466. register char *icp,str[32];
  467. register USHORT len,cnt = 0;
  468.  
  469. forever {
  470.     len = readconsole(con);
  471.     if (len) {
  472.         *(con->buf + len) = '\0';
  473.         strcpy(str,con->buf);
  474.         conprint(con,str);
  475.         icp = str;
  476.         while (len) {
  477.             if (*icp == CR) {
  478.                 commit(con);
  479.                 *cp = '\0';
  480.                 return(0);
  481.                 }
  482.             else if (*icp == SS3) {
  483.                 if (*(icp + 1) == 'M') {
  484.                     commit(con);
  485.                     *cp = '\0';
  486.                     return(0);
  487.                     }
  488.                 }
  489.             else {
  490.                 if ((*icp == DEL)||(*icp == BS)) {
  491.                     if (cnt) {
  492.                         cnt--;
  493.                         cp--;
  494.                         }
  495.                     icp++;
  496.                     }
  497.                 else {
  498.                     *cp++ = *icp++;
  499.                     cnt++;
  500.                     }
  501.                 }
  502.             len--;
  503.             }
  504.         }
  505.     }
  506. return(0);
  507. }
  508.  
  509. commit(register struct console *con)
  510. {
  511. *con->buf = CR;
  512. interpret(con,1);
  513. *con->buf = LF;
  514. interpret(con,1);
  515. return(0);
  516. }
  517.