/*------------ SREC_UX: receiving file via terminal line compatible to bruce carbreys sreceive/ssend System: Unix (MINIX / Coherent / AIX / Solaris) R. Henze 01.04.91 last edit: 25.12.95 -------------*/ #define rst 0 #define mnx 1 #define aix 2 #define systm aix #include #include #include /*tty*/ #if (systm == aix) #include /*tty*/ #endif #include /*zeitumrechnungen*/ #if (systm == mnx) #include #endif #define bufsz 0x0400 #define max_retry 5 #define nak 0x15 #define ack 0x06 #define cntrz 0x1a #define esc 0x1b #define true 1 #define false 0 #define cr 0x0d #define lf 0x0a #define f2 0xbc unsigned char bcheck; unsigned char checksum; unsigned char retry; unsigned int blksz; unsigned int bnum; unsigned int blknum; unsigned char dummy; unsigned int deflt; unsigned char chr; unsigned char buf[bufsz]; unsigned char ib; int file; unsigned int i; int input_fd = 0; unsigned char chr; unsigned char ib; struct sgttyb old_tty; struct sgttyb new_tty; #if (systm == aix) struct termio old_tmio; struct termio new_tmio; #endif time_t fdtime; /*long*/ int flag; /*---------------------------------*/ void quit() { close(file); exit(1); } /*---------------------------------*/ unsigned char get_com() { unsigned char chr; read(input_fd,&chr,1); return(chr); } /*---------------------------------*/ unsigned char get_com_8b() /* wait for byte from aux port. return data byte. tests for key on keyboard and aborts if pressed.*/ register int i; register unsigned char chr; i = (get_com()&0x03)<<7; i = i + get_com() - 32; return((unsigned char)(i&0x00ff)); /*---------------------------------*/ int send_com(chr) unsigned char chr; { if (write(input_fd, &chr, 1)) return(true); else return(false); } /*---------------------------------*/ void put_com(chr) unsigned char chr; { while(!send_com(chr)) ; } /*---------------------------------------------------------------------*/ void input_blk() { unsigned int k; retry = 0; if (blknum>1) put_com(ack); do checksum = 0; ib = get_com_8b(); checksum=checksum ^ ib; bnum = ib; ib = get_com_8b(); checksum=checksum ^ ib; bnum = bnum | (ib<<8); if (blknum != bnum) quit(); ib = get_com_8b(); checksum=checksum ^ ib; blksz = ib; ib = get_com_8b(); checksum=checksum ^ ib; blksz = blksz | (ib<<8); k = 0; i = 0; while (i 0) ; if (blksz==0) put_com(ack); return; retry++; put_com(nak); while (retry < max_retry); quit(); } /* ------------------------------------------------------------------------ */ /* --- file date and time ------------------------------------------------- */ /* ------------------------------------------------------------------------ */ void getftimdat() { unsigned char ib; ib = get_com(); ib = get_com(); ib = get_com(); ib = get_com(); ib = get_com(); ib = get_com(); } /* ------------------------------------------------------------------------ */ void main(argc,argv) int argc; char *argv[]; { ioctl(input_fd,TIOCGETP,&old_tty); ioctl(input_fd,TIOCGETP,&new_tty); new_tty.sg_flags = new_tty.sg_flags | RAW; /* RAW */ #if (systm == mnx) new_tty.sg_flags = new_tty.sg_flags | BITS8; /* 8 bit */ #endif new_tty.sg_flags = new_tty.sg_flags & (ECHO ^ 0xffff); /* no ECHO */ new_tty.sg_flags = new_tty.sg_flags & (CRMOD ^ 0xffff); /*no lf to crlf*/ new_tty.sg_flags = new_tty.sg_flags & (XTABS ^ 0xffff); /*no tab expansion*/ ioctl(input_fd,TIOCSETP,&new_tty); #if (systm == aix) ioctl(input_fd,TCGETA,&old_tmio); ioctl(input_fd,TCGETA,&new_tmio); new_tmio.c_oflag = new_tmio.c_oflag & (ONLCR ^ 0xffff); /* no Outp-NLCR-mapping*/ new_tmio.c_iflag = new_tmio.c_iflag & (ICRNL ^ 0xffff); /* no Input-CRNL-mapping*/ ioctl(input_fd,TCSETA,&new_tmio); #endif printf("\nserial port file receiver"); flag = false; if (argc < 2) printf("\nusage: srec_ux filename [flag]"); exit(1); if (argc > 2) if (*(argv[2]) == 'a') flag = true; if (send_com(f2)) do chr = get_com(); while(chr != ack); i = 0; do put_com(*(argv[1]+i)); i++; while (*(argv[1]+i-1) != 0); file = open(argv[1],0); /*test if file exists*/ if (file == -1) umask(0); file = creat(argv[1],0x0180); if (file == -1) quit(); put_com(ack); getftimdat(); do chr = get_com(); while(chr != ack); put_com(ack); blknum=0; /* block sequence #*/ do blknum++; input_blk() ; if (blksz > 0) write(file,buf,blksz); while (blksz > 0); close(file); printf ("\nfile successfully received!"); else put_com(nak); #if (systm == aix) ioctl(input_fd,TCSETA,&old_tmio); #endif ioctl(input_fd,TIOCSETP,&old_tty); } /*---------------------- end of file srec_mx.i -----------------------------*/