home *** CD-ROM | disk | FTP | other *** search
- /*
- * C64-Amiga file transfer program
- *
- * Timo Rossi 1987/88/89
- *
- */
-
- #include <exec/types.h>
- #include <resources/misc.h>
- #include <resources/cia.h>
- #include <libraries/dos.h>
- #include <proto/exec.h>
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdlib.h>
-
- /* server commands */
-
- #define P_GETDIR 'D'
- #define P_ERROR 'E'
- #define P_GETFILE 'G'
- #define P_MORE 'M'
- #define P_OK 'O'
- #define P_PUTFILE 'P'
- #define P_QUIT 'Q'
- #define P_RETRY 'R'
- #define P_SCRATCH 'S'
- #define P_FILE '1'
- #define P_DIR '2'
- #define P_END '9'
-
- void InitParallelPort(),SetInput(),SetOutput(),OutputChar();
- void OutWord(),SendPacket(),GetDir(),cleanexit();
- void ShowError(),GetFile(),PutFile(),ScratchFile(),makelocalname();
- void GiveHelp();
- int InputChar(),GetPacket();
- short InWord();
-
- #define CTRL_SIGS ((SIGBREAKF_CTRL_C)|(SIGBREAKF_CTRL_D)|(SIGBREAKF_CTRL_E)|(SIGBREAKF_CTRL_F))
-
- APTR miscresource,ciaaresource;
-
- #define peek(x) (*((UBYTE *)(x)))
-
- #define poke(x,y) *((UBYTE *)(x))=y;
-
- unsigned int res_flags=0;
- #define PARALLELPORT 1
-
- #define PKID 0x75
- #define MAXSIZE 1024
-
- char inbuf[256],localname[50];
- unsigned char buff[MAXSIZE];
- int pksize;
-
- char ep[]={ P_END };
- char mp[]={ P_MORE };
-
- BPTR file=NULL;
-
- void main(argc,argv)
- int argc;
- char *argv[];
- {
- #ifdef DEBUG
- int i;
- #endif
-
- static char qp[]={ P_QUIT };
-
- SetSignal(0,CTRL_SIGS);
- InitParallelPort();
-
- printf("C64-Amiga File Transfer Program\nTimo Rossi 1987-89\n");
-
- for(;;)
- {
- printf("\n-> ");
- gets(inbuf);
- if(!strnicmp(inbuf,"quit",4))
- {
- SendPacket(qp,1);
- break;
- }
- else if(!strnicmp(inbuf,"exit",4)) break;
- else if(!strnicmp(inbuf,"dir",3)) GetDir();
- else if(!strnicmp(inbuf,"get",3)) GetFile();
- else if(!strnicmp(inbuf,"put",3)) PutFile();
- else if(!strnicmp(inbuf,"del",3)) ScratchFile();
- else if(inbuf[0]=='?' || (!strnicmp(inbuf,"help",4))) GiveHelp();
- #ifdef DEBUG
- else
- {
- pksize=strlen(inbuf);
- SendPacket(inbuf,pksize);
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]==P_END)
- printf("End of data\n");
- else
- {
- printf("got packet, size is %d bytes\n",pksize);
- for(i=0;i<pksize;i++) putchar(buff[i]);
- putchar('\n');
- fflush(stdout);
- }
- }
- #else
- else printf("\nUnknown command, use 'Help' or '?' to list commands\n");
- #endif
- }
- cleanexit();
- }
-
- void ScratchFile()
- {
- char *p;
- short i;
-
- for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
- p=stpblk(&inbuf[3]);
- if(!*p)
- {
- printf("usage: del <filename>\n");
- return;
- }
- buff[0]=P_SCRATCH;
- strcpy(&buff[1],p);
- SendPacket(buff,strlen(buff));
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]==P_OK)
- {
- putchar('\n');
- for(i=0;i<pksize;i++) putchar(buff[i]);
- putchar('\n');
- }
- else
- {
- printf("ScratchFile: illegal packet type %d\n",buff[0]);
- cleanexit();
- }
- }
-
- void PutFile()
- {
- char *p;
- short s=0;
- long rl,totalbytes=0;
-
- for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
- p=stpblk(&inbuf[3]);
- if(!*p)
- {
- printf("usage: put <filename>\n");
- return;
- }
- if((file=Open(p,MODE_OLDFILE))==NULL)
- {
- printf("Can't open local file '%s'\n",p);
- return;
- }
- buff[0]=P_PUTFILE;
- strcpy(&buff[1],p);
- SendPacket(buff,strlen(buff));
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]!=P_OK)
- {
- printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- else
- do
- {
- buff[0]=P_FILE;
- buff[1]=s++;
- rl=Read(file,&buff[2],MAXSIZE-2);
- if(rl<0) printf("\nLocal file read error\n");
- if(rl>0)
- {
- SendPacket(buff,rl+2);
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]!=P_OK)
- {
- printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- else
- {
- totalbytes+=rl;
- printf(".");
- }
- }
- } while(rl>0);
- SendPacket(ep,1);
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]!=P_OK)
- {
- printf("\nPutFile: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- if(file) Close(file);
- file=NULL;
- if(totalbytes) printf("\n%ld bytes sent\n",totalbytes);
- }
-
- void GetFile()
- {
- char *p;
- short s=0;
- long totalbytes=0;
-
- for(p=inbuf;*p;p++) if(islower(*p)) *p=toupper(*p);
- p=stpblk(&inbuf[3]);
- if(!*p)
- {
- printf("usage: get <filename>\n");
- return;
- }
- buff[0]=P_GETFILE;
- strcpy(&buff[1],p);
- makelocalname(p,localname);
- SendPacket(buff,strlen(buff));
- for(;;)
- {
- GetPacket();
- if(buff[0]==P_ERROR)
- {
- ShowError();
- break;
- }
- if(buff[0]==P_END || buff[0]==P_OK) break;
- if(buff[0]!=P_FILE)
- {
- printf("\nGetFile: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- if(file==NULL)
- {
- if((file=Open(localname,MODE_NEWFILE))==NULL)
- {
- printf("\nCan't open local file '%s'\n",localname);
- SendPacket(ep,1);
- GetPacket();
- if(buff[0]==P_ERROR) ShowError();
- else if(buff[0]!=P_OK)
- {
- printf("\nGetFile: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- break;
- }
- }
- if(buff[1]!=s++)
- {
- printf("\nSequence error\n");
- SendPacket(ep,1);
- continue;
- }
- Write(file,&buff[2],pksize-2);
- totalbytes+=pksize-2;
- printf(".");
- if(SetSignal(0,SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C)
- {
- printf("\nFile transfer aborted by user\n");
- SendPacket(ep,1);
- }
- else SendPacket(mp,1);
- }
- if(file) Close(file);
- file=NULL;
- if(totalbytes) printf("\nReceived %ld bytes\n",totalbytes);
- }
-
- void makelocalname(s,p)
- unsigned char *s,*p;
- {
- register unsigned c;
- while(c=*s++)
- {
- if(c>=0xC1 && c<=0xDA) c&=0x7F;
- if(isupper(c)) c=tolower(c);
- if(c<' ' || c>=0x7F || c=='*' || c=='?') c='_';
- *p++=c;
- }
- *p++='\0';
- }
-
- void GetDir()
- {
- static char gd[]={ 'D' };
- short i,s;
- register int c;
-
- SendPacket(gd,1);
- s=0;
- for(;;)
- {
- GetPacket();
- if(buff[0]==P_ERROR)
- {
- ShowError();
- break;
- }
- if(buff[0]==P_END || buff[0]==P_OK) break;
- if(buff[0]!=P_DIR)
- {
- printf("GetDir: illegal packet type %d\n",(unsigned)buff[0]);
- cleanexit();
- }
- if(buff[1]!=s++)
- {
- printf("Sequence error\n");
- SendPacket(ep,1);
- continue;
- }
- for(i=2;i<pksize;i++)
- {
- c=buff[i]&0x7f;
- if(c>=' ' && c<='_') putchar(c); else putchar('?');
- }
- putchar('\n');
- fflush(stdout);
- if(SetSignal(0,SIGBREAKF_CTRL_C)&SIGBREAKF_CTRL_C) SendPacket(ep,1);
- else SendPacket(mp,1);
- }
- }
-
- void ShowError()
- {
- register int i;
-
- printf("\nError: ");
- for(i=1;i<pksize;i++) putchar(buff[i]);
- putchar('\n');
- fflush(stdout);
- }
-
- void SendPacket(s,n)
- register unsigned char *s;
- int n;
- {
- short i,ck=0;
-
- SetOutput();
- for(i=0;i<10;i++) OutputChar(0);
- OutputChar(PKID);
- OutWord(n);
- for(i=0;i<n;i++,s++)
- {
- OutputChar(*s);
- ck+=*s;
- }
- OutWord(ck);
- SetInput();
- }
-
- int GetPacket()
- {
- register char *s=buff;
- short i,ck=0;
- short c;
- /* input mode on by default */
- while(InputChar()!=PKID);
- pksize=InWord();
- if(pksize>MAXSIZE)
- {
- printf("Packet too long (%d bytes)\n",pksize);
- cleanexit();
- }
- for(i=0;i<pksize;i++)
- {
- c=InputChar();
- *s++=c;
- ck+=c;
- }
- if(ck!=InWord())
- {
- printf("Checksum error\n");
- cleanexit();
- }
- return (int)pksize;
- }
-
- short InWord()
- {
- short w;
-
- w=InputChar()<<8;
- w=w|InputChar();
- return w;
- }
-
- void OutWord(c)
- short c;
- {
- OutputChar(c>>8);
- OutputChar(c&0xff);
- }
-
- void cleanexit()
- {
- if(file) Close(file);
- if(res_flags&PARALLELPORT)
- {
- SetInput();
- FreeMiscResource(MR_PARALLELPORT,miscresource);
- }
- exit(0);
- }
-
- void InitParallelPort()
- {
- if((miscresource=(APTR)OpenResource(MISCNAME,0))==0)
- {
- printf("Can't open misc.resource\n");
- cleanexit();
- }
- if((ciaaresource=(APTR)OpenResource(CIAANAME,0))==0)
- {
- printf("Can't open ciaa.resource\n");
- cleanexit();
- }
- if(GetMiscResource(MR_PARALLELPORT,"belymt",miscresource))
- {
- printf("Can't allocate parallel port resource\n");
- cleanexit();
- }
- res_flags|=PARALLELPORT;
- SetInput();
- }
-
- void SetInput()
- {
- poke(0xbfe301,0x00);
- }
-
- void SetOutput()
- {
- poke(0xbfe301,0xff);
- }
-
- InputChar()
- {
- while((SetICR(ciaaresource,0x10)&0x10)==0)
- if(SetSignal(0,0)&SIGBREAKF_CTRL_F)
- {
- printf("GetChar: *** BREAK ***\n");
- cleanexit();
- }
- return(peek(0xbfe101));
- }
-
- void OutputChar(c)
- char c;
- {
- poke(0xbfe101,c);
-
- while((SetICR(ciaaresource,0x10)&0x10)==0)
- if(SetSignal(0,0)&SIGBREAKF_CTRL_F)
- {
- printf("PutChar: *** BREAK ***\n");
- cleanexit();
- }
- }
-
- void GiveHelp()
- {
- printf("Commands are:\n\n\
- dir\t\tget C64 directory\n\
- get <filename>\ttransfer file from C64 to Amiga\n\
- put <filename>\ttransfer file from Amiga to C64\n\
- del <filename>\tdelete C64 file\n\
- exit\t\texit and leave C64 server running\n\
- quit\t\texit and quit the C64 server\n\n");
- }
-