home *** CD-ROM | disk | FTP | other *** search
/ Brotikasten / BROTCD01.iso / amiga / c64trans.zoo / c64transfer.c < prev    next >
C/C++ Source or Header  |  1989-07-17  |  9KB  |  483 lines

  1. /*
  2.  * C64-Amiga file transfer program
  3.  *
  4.  * Timo Rossi  1987/88/89
  5.  *
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <resources/misc.h>
  10. #include <resources/cia.h>
  11. #include <libraries/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18.  
  19. /* server commands */
  20.  
  21. #define P_GETDIR  'D'
  22. #define P_ERROR      'E'
  23. #define P_GETFILE 'G'
  24. #define P_MORE      'M'
  25. #define P_OK      'O'
  26. #define P_PUTFILE 'P'
  27. #define P_QUIT      'Q'
  28. #define P_RETRY      'R'
  29. #define P_SCRATCH 'S'
  30. #define P_FILE      '1'
  31. #define P_DIR      '2'
  32. #define P_END      '9'
  33.  
  34. void InitParallelPort(),SetInput(),SetOutput(),OutputChar();
  35. void OutWord(),SendPacket(),GetDir(),cleanexit();
  36. void ShowError(),GetFile(),PutFile(),ScratchFile(),makelocalname();
  37. void GiveHelp();
  38. int InputChar(),GetPacket();
  39. short InWord();
  40.  
  41. #define CTRL_SIGS ((SIGBREAKF_CTRL_C)|(SIGBREAKF_CTRL_D)|(SIGBREAKF_CTRL_E)|(SIGBREAKF_CTRL_F))
  42.  
  43. APTR miscresource,ciaaresource;
  44.  
  45. #define peek(x) (*((UBYTE *)(x)))
  46.  
  47. #define poke(x,y) *((UBYTE *)(x))=y;
  48.  
  49. unsigned int res_flags=0;
  50. #define PARALLELPORT 1
  51.  
  52. #define PKID    0x75
  53. #define MAXSIZE 1024
  54.  
  55. char inbuf[256],localname[50];
  56. unsigned char buff[MAXSIZE];
  57. int pksize;
  58.  
  59. char ep[]={ P_END };
  60. char mp[]={ P_MORE };
  61.  
  62. BPTR file=NULL;
  63.  
  64. void main(argc,argv)
  65. int argc;
  66. char *argv[];
  67. {
  68. #ifdef DEBUG
  69.  int i;
  70. #endif
  71.  
  72.  static char qp[]={ P_QUIT };
  73.  
  74.  SetSignal(0,CTRL_SIGS);
  75.  InitParallelPort();
  76.  
  77.  printf("C64-Amiga File Transfer Program\nTimo Rossi 1987-89\n");
  78.  
  79.  for(;;)
  80.   {
  81.    printf("\n-> ");
  82.    gets(inbuf);
  83.    if(!strnicmp(inbuf,"quit",4))
  84.     {
  85.      SendPacket(qp,1);
  86.      break;
  87.     }
  88.    else if(!strnicmp(inbuf,"exit",4)) break;
  89.    else if(!strnicmp(inbuf,"dir",3)) GetDir();
  90.    else if(!strnicmp(inbuf,"get",3)) GetFile();
  91.    else if(!strnicmp(inbuf,"put",3)) PutFile();
  92.    else if(!strnicmp(inbuf,"del",3)) ScratchFile();
  93.    else if(inbuf[0]=='?' || (!strnicmp(inbuf,"help",4))) GiveHelp();
  94. #ifdef DEBUG
  95.    else
  96.     {
  97.      pksize=strlen(inbuf);
  98.      SendPacket(inbuf,pksize);
  99.      GetPacket();
  100.      if(buff[0]==P_ERROR) ShowError();
  101.      else if(buff[0]==P_END)
  102.       printf("End of data\n");
  103.      else
  104.       {
  105.        printf("got packet, size is %d bytes\n",pksize);
  106.        for(i=0;i<pksize;i++) putchar(buff[i]);
  107.        putchar('\n');
  108.        fflush(stdout);
  109.       }
  110.     }
  111. #else
  112.    else printf("\nUnknown command, use 'Help' or '?' to list commands\n");
  113. #endif
  114.   }
  115.  cleanexit();
  116. }
  117.  
  118. void ScratchFile()
  119. {
  120.  char *p;
  121.  short i;
  122.  
  123.  for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
  124.  p=stpblk(&inbuf[3]);
  125.  if(!*p)
  126.   {
  127.    printf("usage: del <filename>\n");
  128.    return;
  129.   }
  130.  buff[0]=P_SCRATCH;
  131.  strcpy(&buff[1],p);
  132.  SendPacket(buff,strlen(buff));
  133.  GetPacket();
  134.  if(buff[0]==P_ERROR) ShowError();
  135.  else if(buff[0]==P_OK)
  136.   {
  137.    putchar('\n');
  138.    for(i=0;i<pksize;i++) putchar(buff[i]);
  139.    putchar('\n');
  140.   }
  141.  else
  142.   {
  143.    printf("ScratchFile: illegal packet type %d\n",buff[0]);
  144.    cleanexit();
  145.   }
  146. }
  147.  
  148. void PutFile()
  149. {
  150.  char *p;
  151.  short s=0;
  152.  long rl,totalbytes=0;
  153.  
  154.  for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
  155.  p=stpblk(&inbuf[3]);
  156.  if(!*p)
  157.   {
  158.    printf("usage: put <filename>\n");
  159.    return;
  160.   }
  161.  if((file=Open(p,MODE_OLDFILE))==NULL)
  162.   {
  163.    printf("Can't open local file '%s'\n",p);
  164.    return;
  165.   }
  166.  buff[0]=P_PUTFILE;
  167.  strcpy(&buff[1],p);
  168.  SendPacket(buff,strlen(buff));
  169.  GetPacket();
  170.  if(buff[0]==P_ERROR) ShowError();
  171.  else if(buff[0]!=P_OK)
  172.   {
  173.    printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
  174.    cleanexit();
  175.   }
  176.  else
  177.   do
  178.    {
  179.     buff[0]=P_FILE;
  180.     buff[1]=s++;
  181.     rl=Read(file,&buff[2],MAXSIZE-2);
  182.     if(rl<0) printf("\nLocal file read error\n");
  183.     if(rl>0)
  184.      {
  185.       SendPacket(buff,rl+2);
  186.       GetPacket();
  187.       if(buff[0]==P_ERROR) ShowError();
  188.       else if(buff[0]!=P_OK)
  189.        {
  190.         printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
  191.         cleanexit();
  192.        }
  193.       else
  194.        {
  195.     totalbytes+=rl;
  196.     printf(".");
  197.        }
  198.      }
  199.    } while(rl>0);
  200.  SendPacket(ep,1);
  201.  GetPacket();
  202.  if(buff[0]==P_ERROR) ShowError();
  203.  else if(buff[0]!=P_OK)
  204.   {
  205.    printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
  206.    cleanexit();
  207.   }
  208.  if(file) Close(file);
  209.  file=NULL;
  210.  if(totalbytes) printf("\n%ld bytes sent\n",totalbytes);
  211. }
  212.  
  213. void GetFile()
  214. {
  215.  char *p;
  216.  short s=0;
  217.  long totalbytes=0;
  218.  
  219.  for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
  220.  p=stpblk(&inbuf[3]);
  221.  if(!*p)
  222.   {
  223.    printf("usage: get <filename>\n");
  224.    return;
  225.   }
  226.  buff[0]=P_GETFILE;
  227.  strcpy(&buff[1],p);
  228.  makelocalname(p,localname);
  229.  SendPacket(buff,strlen(buff));
  230.  for(;;)
  231.   {
  232.    GetPacket();
  233.    if(buff[0]==P_ERROR)
  234.     {
  235.      ShowError();
  236.      break;
  237.     }
  238.    if(buff[0]==P_END || buff[0]==P_OK) break;
  239.    if(buff[0]!=P_FILE)
  240.     {
  241.      printf("\nGetFile: illegal packet type %d\n",(unsigned)buff[0]);
  242.      cleanexit();
  243.     }
  244.    if(file==NULL)
  245.     {
  246.      if((file=Open(localname,MODE_NEWFILE))==NULL)
  247.       {
  248.        printf("\nCan't open local file '%s'\n",localname);
  249.        SendPacket(ep,1);
  250.        GetPacket();
  251.        if(buff[0]==P_ERROR) ShowError();
  252.        else if(buff[0]!=P_OK)
  253.         {
  254.          printf("\nGetFile: illegal packet type %d\n",(unsigned)buff[0]);
  255.          cleanexit();
  256.         }
  257.        break;
  258.       }
  259.      }
  260.     if(buff[1]!=s++)
  261.       {
  262.        printf("\nSequence error\n");
  263.        SendPacket(ep,1);
  264.        continue;
  265.       }
  266.     Write(file,&buff[2],pksize-2);
  267.     totalbytes+=pksize-2;
  268.     printf(".");
  269.     if(SetSignal(0,SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C)
  270.       {
  271.        printf("\nFile transfer aborted by user\n");
  272.        SendPacket(ep,1);
  273.       }
  274.     else SendPacket(mp,1);
  275.   }
  276.  if(file) Close(file);
  277.  file=NULL;
  278.  if(totalbytes) printf("\nReceived %ld bytes\n",totalbytes);
  279. }
  280.  
  281. void makelocalname(s,p)
  282. unsigned char *s,*p;
  283. {
  284.  register unsigned c;
  285.  while(c=*s++)
  286.   {
  287.    if(c>=0xC1 && c<=0xDA) c&=0x7F;
  288.    if(isupper(c)) c=tolower(c);
  289.    if(c<' ' || c>=0x7F || c=='*' || c=='?') c='_';
  290.    *p++=c;
  291.   }
  292.  *p++='\0';
  293. }
  294.  
  295. void GetDir()
  296. {
  297.  static char gd[]={ 'D' };
  298.  short i,s;
  299.  register int c;
  300.  
  301.  SendPacket(gd,1);
  302.  s=0;
  303.  for(;;)
  304.   {
  305.    GetPacket();
  306.    if(buff[0]==P_ERROR)
  307.     {
  308.      ShowError();
  309.      break;
  310.     }
  311.    if(buff[0]==P_END || buff[0]==P_OK) break;
  312.    if(buff[0]!=P_DIR)
  313.     {
  314.      printf("GetDir: illegal packet type %d\n",(unsigned)buff[0]);
  315.      cleanexit();
  316.     }
  317.    if(buff[1]!=s++)
  318.     {
  319.      printf("Sequence error\n");
  320.      SendPacket(ep,1);
  321.      continue;
  322.     }
  323.    for(i=2;i<pksize;i++)
  324.     {
  325.      c=buff[i]&0x7f;
  326.      if(c>=' ' && c<='_') putchar(c); else putchar('?');
  327.     }
  328.    putchar('\n');
  329.    fflush(stdout);
  330.    if(SetSignal(0,SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C) SendPacket(ep,1);
  331.    else SendPacket(mp,1);
  332.   }
  333. }
  334.  
  335. void ShowError()
  336. {
  337.  register int i;
  338.  
  339.  printf("\nError: ");
  340.  for(i=1;i<pksize;i++) putchar(buff[i]);
  341.  putchar('\n');
  342.  fflush(stdout);
  343. }
  344.  
  345. void SendPacket(s,n)
  346. register unsigned char *s;
  347. int n;
  348. {
  349.  short i,ck=0;
  350.  
  351.  SetOutput();
  352.  for(i=0;i<10;i++) OutputChar(0);
  353.  OutputChar(PKID);
  354.  OutWord(n);
  355.  for(i=0;i<n;i++,s++)
  356.   {
  357.    OutputChar(*s);
  358.    ck+=*s;
  359.   }
  360.  OutWord(ck);
  361.  SetInput();
  362. }
  363.  
  364. int GetPacket()
  365. {
  366.  register char *s=buff;
  367.  short i,ck=0;
  368.  short c;
  369.  /* input mode on by default */
  370.  while(InputChar()!=PKID);
  371.  pksize=InWord();
  372.  if(pksize>MAXSIZE)
  373.   {
  374.    printf("Packet too long (%d bytes)\n",pksize);
  375.    cleanexit();
  376.   }
  377.  for(i=0;i<pksize;i++)
  378.    {
  379.     c=InputChar();
  380.     *s++=c;
  381.     ck+=c;
  382.    }
  383.  if(ck!=InWord())
  384.   {
  385.    printf("Checksum error\n");
  386.    cleanexit();
  387.   }
  388.  return (int)pksize;
  389. }
  390.  
  391. short InWord()
  392. {
  393.  short w;
  394.  
  395.  w=InputChar()<<8;
  396.  w=w|InputChar();
  397.  return w;
  398. }
  399.  
  400. void OutWord(c)
  401. short c;
  402. {
  403.  OutputChar(c>>8);
  404.  OutputChar(c&0xff);
  405. }
  406.  
  407. void cleanexit()
  408. {
  409.  if(file) Close(file);
  410.  if(res_flags&PARALLELPORT)
  411.   {
  412.    SetInput();
  413.    FreeMiscResource(MR_PARALLELPORT,miscresource);
  414.   }
  415.  exit(0);
  416. }
  417.  
  418. void InitParallelPort()
  419. {
  420.  if((miscresource=(APTR)OpenResource(MISCNAME,0))==0)
  421.   {
  422.    printf("Can't open misc.resource\n");
  423.    cleanexit();
  424.   }
  425.  if((ciaaresource=(APTR)OpenResource(CIAANAME,0))==0)
  426.   {
  427.    printf("Can't open ciaa.resource\n");
  428.    cleanexit();
  429.   }
  430.  if(GetMiscResource(MR_PARALLELPORT,"belymt",miscresource))
  431.   {
  432.    printf("Can't allocate parallel port resource\n");
  433.    cleanexit();
  434.   }
  435.  res_flags|=PARALLELPORT;
  436.  SetInput();
  437. }
  438.  
  439. void SetInput()
  440. {
  441.  poke(0xbfe301,0x00);
  442. }
  443.  
  444. void SetOutput()
  445. {
  446.  poke