Newsgroups: comp.sys.palmtops


> Is there any free/share/software to backup a Casio BOSS to a UNIX workstation?
>
>   Thanks,
>
>   Jacob

I have some software that I used to use with my BOSS (I now use a (95LX).
It worked well on my HP-UX operating system. Offered here as is.

Gene Dick
HP Fort Collins

#include <std.disclaimer>


#--------------------------cut here------------------------------
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# This archive contains:
#	casio.h		casioformat.c	casioprint.c	casioread.c	
#	casioreadraw.c	casiowrite.c	readme		
#

LANG=""; export LANG
PATH=/bin:/usr/bin:$PATH; export PATH

echo x - casio.h
cat >casio.h <<'@EOF'

/* Block Types */

#define BT_UNKNOWN   0
#define BT_START     1
#define BT_END       2
#define BT_ENDRECORD 3
#define BT_TEXT      4
#define BT_TIME      5
#define BT_ALARM     6
#define BT_DATE      7
#define BT_CALENDAR  8

/* Entry Types */

#define ET_UNKNOWN   0
#define ET_CALENDAR  1
#define ET_TELEPHONE 2
#define ET_MEMO      3
#define ET_SCHEDULE  4
#define ET_BUSCARD   5
@EOF

chmod 666 casio.h

echo x - casioformat.c
cat >casioformat.c <<'@EOF'
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "casio.h"

#define FALSE 0
#define TRUE  1

extern unsigned char *malloc();

char *bltype_text[9] = { "%%%UNKNOWN","%%%TYPE","","%%%EOR",
			 "","%%%TIME","%%%ALARM","%%%DATE",
			 "%%%CALENDAR" };

char *entype_text[6] = { "UNKNOWN","CALENDAR",
			 "PHONE", "MEMO",
			 "SCHEDULE", "BUSCARD" };

/* Forward Declarations */

void print_text(), print_calendar();


main()
{
    register unsigned char *s;
    register unsigned char *ends;
    register int i;
    int filesize;
    int packsize;
    int markedflag;
    struct stat sbuf;
    unsigned char *inbuf;
    int etype;

    if (fstat(0,&sbuf) != 0) {
	fprintf(stderr,"Could not stat standard input.\n");
	exit(1);
    }

    if ((sbuf.st_mode & S_IFMT) != S_IFREG) {
	fprintf(stderr,"standard input is not a regular file.\n");
	exit(1);
    }

    filesize = sbuf.st_size;

    if ((inbuf = malloc(filesize)) == (unsigned char *)0) {
	fprintf(stderr,"Malloc Failure.\n");
	exit(1);
    }

    if (read(0,inbuf,filesize) != filesize) {
	fprintf(stderr,"Read Failure.\n");
	exit(1);
    }

    s = inbuf;
    ends = s + filesize;
    etype = ET_UNKNOWN;
    while (s < ends) {

	if (*s != ':') {
	    fprintf(stderr,"Illegal sequence in input.\n");
	    exit(1);
	}

	s++;

	packsize = hex(s);

	s += 2;

	i = block_type(s);

	if (i != BT_END && i != BT_TEXT)
	    printf("%s ",bltype_text[i]);

	if (i == BT_ENDRECORD) {
	    if ((hex(s) & 0x80) == 0)
		markedflag = FALSE;
	    else
		markedflag = TRUE;
	}

	s += 6;

	switch(i) {

	case BT_START:
	    etype = entry_type(s);
	    printf("%s",entype_text[etype]);
	    break;

	case BT_ENDRECORD:
	    if (markedflag != FALSE)
		printf("MARK");
	    break;

	case BT_UNKNOWN:
	case BT_END:
	    break;

	case BT_TIME:
	case BT_TEXT:
	case BT_ALARM:
	case BT_DATE:
	    if (etype == ET_TELEPHONE || etype == ET_BUSCARD)
		print_text(s,packsize,'\n');
	    else
		print_text(s,packsize,'\r');
	    break;

	case BT_CALENDAR:
	    print_calendar(s);
	    break;
	}
	putchar('\n');

	s += (packsize * 2);

	/* Skip over checksum for now */

	s += 2;
    }

    exit(0);
}

int
block_type(headptr)
    register unsigned char *headptr;
{
    register int i;

    /* Figure out Block Type */

    i = hex(headptr + 4);
    switch (i) {

    case 0x00:
	i = hex(headptr);
	switch (i & 0xf0) {

	case 0x80:
	    i = BT_TEXT;
	    break;

	case 0xC0:
	    i = BT_ALARM;
	    break;

	case 0xD0:
	    i = BT_CALENDAR;
	    break;

	case 0xE0:
	    i = BT_TIME;
	    break;

	case 0xF0:
	    i = BT_DATE;
	    break;

	default:
	    i = BT_UNKNOWN;
	    break;
	}
	break;

    case 0x01:
	i = BT_ENDRECORD;
	break;

    case 0x02:
	i = BT_START;
	break;

    case 0xFF:
	i = BT_END;
	break;

    default:
	i = BT_UNKNOWN;
	break;
    }

    return(i);
}

int
entry_type(dataptr)
    register unsigned char *dataptr;
{
    register int i;

    i = hex(dataptr);

    switch (i) {

    case 0x80:
	i = ET_CALENDAR;
	break;

    case 0x90:
	i = ET_TELEPHONE;
	break;

    case 0xA0:
	i = ET_MEMO;
	break;

    case 0xB0:
	i = ET_SCHEDULE;
	break;

    case 0xC0:
	i = ET_BUSCARD;
	break;

    default:
	i = ET_UNKNOWN;
	break;
    }

    return(i);
}

int
hex(hexbuf)
    register unsigned char *hexbuf;
{
    register int h1;
    register int h2;

    h1 = *hexbuf++;
    h2 = *hexbuf;

    if (h1 >= '0' && h1 <= '9')
	h1 -= '0';
    else
	h1 += (10 - 'A');

    if (h2 >= '0' && h2 <= '9')
	h2 -= '0';
    else
	h2 += (10 - 'A');

    return(h1 * 16 + h2);
}

void
print_text(buf,nbytes,endbyte)
    register unsigned char *buf;
    register int nbytes;
    int endbyte;
{
    register int i;
    register int j;

    while (nbytes-- > 0) {

	i = hex(buf);
	if (i == '\r' || i == '\n') {
	    if (i != endbyte) {
		putchar('\\');
		if (i == '\r')
		    putchar('r');
		else
		    putchar('n');

	    }
	}
	else
	    putchar(i);

	buf += 2;
    }

    return;
}

void
print_calendar(calptr)
    register unsigned char *calptr;
{
    register unsigned long datebits;
    register int i;
    register unsigned long mask;

    datebits =   (hex(calptr) << 24) | (hex(calptr + 2) << 16)
	       | (hex(calptr + 4) << 8) | hex(calptr + 6);

    mask = 0x1;
    for (i = 1; i < 32; i++) {
	if ((datebits & mask) != 0) {
	    printf("%d ",i);
	}

	mask <<= 1;
    }

    return;
}
@EOF

chmod 666 casioformat.c

echo x - casioprint.c
cat >casioprint.c <<'@EOF'
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/termio.h>
#include <sys/modem.h>
#include <sys/signal.h>
#include "casio.h"

int dlfd;
struct termio t_old,t_new;
int modeschanged = 0;

#define IOBUFSIZE 2048
unsigned char iobuf[IOBUFSIZE];
int  iosize;

/* Forward Declarations */

void read_hexbytes();

catch_intr() {
    if (modeschanged != 0) {
	ioctl(dlfd,TCSETA,&t_old);
    }
    exit(1);
}

main(argc,argv)
    int argc;
    char *argv[];
{
    register int i;
    char inbuf[2048];
    unsigned long modemflags;

    signal(SIGINT,catch_intr);

    if ((dlfd = open(argv[1],O_RDWR)) < 0) {
	fprintf(stderr,"Could not open casio device %s.\n",argv[1]);
	exit(1);
    }

    ioctl(dlfd,TCGETA,&t_old);

    t_new.c_cc[VMIN]  = 1;
    t_new.c_cc[VTIME] = 0;
    t_new.c_iflag = IGNBRK;
    t_new.c_oflag = 0;
    t_new.c_cflag = (B9600 | CS8 | CREAD);
    t_new.c_lflag = 0;
    t_new.c_line  = 0;

    ioctl(dlfd,TCSETAW,&t_new);
    modeschanged = 1;

    /* Make sure DTR is on and RTS is off */

    ioctl(dlfd,MCGETA,&modemflags);
    modemflags |= MDTR;
    modemflags &= ~MRTS;
    ioctl(dlfd,MCSETA,&modemflags);

    for (;;) {

	if ((i = read(dlfd,inbuf,2048)) < 0) {
	    fprintf(stderr,"Read Failure.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}
	if (i > 0) {
	    if (write(1,inbuf,i) != i) {
		fprintf(stderr,"Write Failure.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(1);
	    }
	}
    }
}

@EOF

chmod 666 casioprint.c

echo x - casioread.c
cat >casioread.c <<'@EOF'
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/termio.h>
#include <sys/modem.h>
#include <sys/signal.h>
#include "casio.h"

int dlfd;
struct termio t_old,t_new;
int modeschanged = 0;

#define IOBUFSIZE 2048
unsigned char iobuf[IOBUFSIZE];
int  iosize;

/* Forward Declarations */

void read_hexbytes();

catch_intr() {
    if (modeschanged != 0) {
	ioctl(dlfd,TCSETA,&t_old);
    }
    exit(1);
}

main(argc,argv)
    int argc;
    char *argv[];
{
    char ch;
    register int block_type;
    unsigned long modemflags;

    signal(SIGINT,catch_intr);

    if ((dlfd = open(argv[1],O_RDWR)) < 0) {
	fprintf(stderr,"Could not open casio device %s.\n",argv[1]);
	exit(1);
    }

    ioctl(dlfd,TCGETA,&t_old);

    t_new.c_cc[VMIN]  = 1;
    t_new.c_cc[VTIME] = 0;
    t_new.c_iflag = IGNBRK;
    t_new.c_oflag = 0;
    t_new.c_cflag = (B9600 | CS8 | CREAD);
    t_new.c_lflag = 0;
    t_new.c_line  = 0;

    ioctl(dlfd,TCSETAW,&t_new);
    modeschanged = 1;

    /* Make sure DTR is on and RTS is off */

    ioctl(dlfd,MCGETA,&modemflags);
    modemflags |= MDTR;
    modemflags &= ~MRTS;
    ioctl(dlfd,MCSETA,&modemflags);

    for (;;) {

	if (read(dlfd,&ch,1) != 1) {
	    fprintf(stderr,"Read Failure.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}

	switch (ch) {

	case '\r':

#ifdef DEBUG
	    fprintf(stderr,"Got Return\n");
#endif

	    /* Ignore It */

	    break;


	case '\n':

#ifdef DEBUG
	    fprintf(stderr,"Got Newline\n");
#endif

	    /* Respond with ctr q */

	    ch = '\021';
	    if (write(dlfd,&ch,1) != 1) {
		fprintf(stderr,"Write Failure.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(1);
	    }
	    break;

	case '"':
	    fprintf(stderr,"Received Error Indicator.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	    break;

	case '!':
	    fprintf(stderr,"Transfer Stopped at transmitter.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(0);
	    break;

	case ':':
#ifdef DEBUG
	    fprintf(stderr,"Got :\n");
#endif
	    block_type = read_block();
	    switch (block_type) {

	    case BT_END:
		fprintf(stderr,"Normal Termination.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(0);
		break;

	    case BT_ENDRECORD:

		/* Respond with '#' */

		ch = '#';
		if (write(dlfd,&ch,1) != 1) {
		    fprintf(stderr,"Write Failure.\n");
		    ioctl(dlfd,TCSETAW,&t_old);
		    exit(1);
		}
		break;

	    case BT_START:
	    case BT_TEXT:
	    case BT_TIME:
	    case BT_DATE:
	    case BT_CALENDAR:
	    case BT_ALARM:
		break;

	    default:
		fprintf(stderr,"Unknown block type from read_block\n");
		fprintf(stderr,"Write Failure.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(1);
		break;
	    }

	    /* Write the record to standard out with a ':' */

	    ch = ':';
	    if (write(1,&ch,1) != 1) {
		fprintf(stderr,"Write Failure.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(1);
	    }

	    if (write(1,iobuf,iosize) != iosize) {
		fprintf(stderr,"Write Failure.\n");
		ioctl(dlfd,TCSETAW,&t_old);
		exit(1);
	    }
	    break;

	default:
#ifdef DEBUG
	    fprintf(stderr,"Got %c(%x)\n",ch,ch);
#endif
	    fprintf(stderr,"Bad Character received from casio.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	    break;
	}
    }
}

read_block()
{
    register unsigned char *s;
    register int packsize;
    register int nread;
    register int i;

    s = iobuf;

    /* Read Packet Length */

    read_hexbytes(s,2);

    packsize = 2 * (hex(s) + 4);

    s += 2;
    read_hexbytes(s,packsize);

    /* Figure out Block Type */

    i = hex(&iobuf[6]);
    switch (i) {

    case 0x00:
	i = hex(&iobuf[2]);
	switch (i & 0xf0) {

	case 0x80:
	    i = BT_TEXT;
	    break;

	case 0xC0:
	    i = BT_ALARM;
	    break;

	case 0xD0:
	    i = BT_CALENDAR;
	    break;

	case 0xE0:
	    i = BT_TIME;
	    break;

	case 0xF0:
	    i = BT_DATE;
	    break;

	default:
#ifdef DEBUG
	    fprintf(stderr,"Unknown block type 00 in position 3, %02x in position 1\n",i);
#endif
	    i = BT_UNKNOWN;
	    break;
	}
	break;

    case 0x01:
	i = BT_ENDRECORD;
	break;

    case 0x02:
	i = BT_START;
	break;

    case 0xFF:
	i = BT_END;
	break;

    default:
#ifdef DEBUG
	fprintf(stderr,"Unknown block type %02x in position 3\n",i);
#endif
	i = BT_UNKNOWN;
	break;
    }

    iosize = packsize + 2;
    return(i);
}

int
hex(hexbuf)
    register unsigned char *hexbuf;
{
    register int h1;
    register int h2;

    h1 = *hexbuf++;
    h2 = *hexbuf;

    if (h1 >= '0' && h1 <= '9')
	h1 -= '0';
    else
	h1 += (10 - 'A');

    if (h2 >= '0' && h2 <= '9')
	h2 -= '0';
    else
	h2 += (10 - 'A');

    return(h1 * 16 + h2);
}

void
read_hexbytes(buf,nbytes)
    unsigned char *buf;
    register int nbytes;
{
    register int i;
    register int j;
    register unsigned char *s;

    s = buf;
    i = nbytes;

    while (i > 0) {
	if ((j = read(dlfd,s,i)) <= 0) {
	    if (j == 0)
		fprintf(stderr,"EOF on casio device.\n");
	    else
		fprintf(stderr,"Read Failure.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}

	i -= j;
	s += j;
    }

    /* Make sure bytes are HEX */


    s = buf;
    i = nbytes;
    while (i > 0) {

	j = *s++;

	if ((j < '0' || j > '9') && (j < 'A' || j > 'F')) {
	    fprintf(stderr,"Illegal Hex Digit read %x(%c).\n",j,j);
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}
	i--;
    }

    return;
}
@EOF

chmod 666 casioread.c

echo x - casioreadraw.c
cat >casioreadraw.c <<'@EOF'
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/termio.h>
#include <sys/modem.h>
#include <sys/signal.h>
#include "casio.h"

int dlfd;
struct termio t_old,t_new;
int modeschanged = 0;

#define IOBUFSIZE 2048
unsigned char iobuf[IOBUFSIZE];
int  iosize;

/* Forward Declarations */

void read_hexbytes();

catch_intr() {
    if (modeschanged != 0) {
	ioctl(dlfd,TCSETA,&t_old);
    }
    exit(1);
}

main(argc,argv)
    int argc;
    char *argv[];
{
    char ch;
    register int block_type;
    unsigned long modemflags;

    signal(SIGINT,catch_intr);

    if ((dlfd = open(argv[1],O_RDWR)) < 0) {
	fprintf(stderr,"Could not open casio device %s.\n",argv[1]);
	exit(1);
    }

    ioctl(dlfd,TCGETA,&t_old);

    t_new.c_cc[VMIN]  = 1;
    t_new.c_cc[VTIME] = 0;
    t_new.c_iflag = IGNBRK;
    t_new.c_oflag = 0;
    t_new.c_cflag = (B9600 | CS8 | CREAD);
    t_new.c_lflag = 0;
    t_new.c_line  = 0;

    ioctl(dlfd,TCSETAW,&t_new);
    modeschanged = 1;

    /* Make sure DTR is on and RTS is off */

    ioctl(dlfd,MCGETA,&modemflags);
    modemflags |= MDTR;
    modemflags &= ~MRTS;
    ioctl(dlfd,MCSETA,&modemflags);

    for (;;) {
	iosize = read(dlfd, iobuf, IOBUFSIZE);
	if (iosize <= 0)
	    exit(1);
	write(1, iobuf, iosize);
    }
}

read_block()
{
    register unsigned char *s;
    register int packsize;
    register int nread;
    register int i;

    s = iobuf;

    /* Read Packet Length */

    read_hexbytes(s,2);

    packsize = 2 * (hex(s) + 4);

    s += 2;
    read_hexbytes(s,packsize);

    /* Figure out Block Type */

    i = hex(&iobuf[6]);
    switch (i) {

    case 0x00:
	i = hex(&iobuf[2]);
	switch (i & 0xf0) {

	case 0x80:
	    i = BT_TEXT;
	    break;

	case 0xC0:
	    i = BT_ALARM;
	    break;

	case 0xD0:
	    i = BT_CALENDAR;
	    break;

	case 0xE0:
	    i = BT_TIME;
	    break;

	case 0xF0:
	    i = BT_DATE;
	    break;

	default:
#ifdef DEBUG
	    fprintf(stderr,"Unknown block type 00 in position 3, %02x in position 1\n",i);
#endif
	    i = BT_UNKNOWN;
	    break;
	}
	break;

    case 0x01:
	i = BT_ENDRECORD;
	break;

    case 0x02:
	i = BT_START;
	break;

    case 0xFF:
	i = BT_END;
	break;

    default:
#ifdef DEBUG
	fprintf(stderr,"Unknown block type %02x in position 3\n",i);
#endif
	i = BT_UNKNOWN;
	break;
    }

    iosize = packsize + 2;
    return(i);
}

int
hex(hexbuf)
    register unsigned char *hexbuf;
{
    register int h1;
    register int h2;

    h1 = *hexbuf++;
    h2 = *hexbuf;

    if (h1 >= '0' && h1 <= '9')
	h1 -= '0';
    else
	h1 += (10 - 'A');

    if (h2 >= '0' && h2 <= '9')
	h2 -= '0';
    else
	h2 += (10 - 'A');

    return(h1 * 16 + h2);
}

void
read_hexbytes(buf,nbytes)
    unsigned char *buf;
    register int nbytes;
{
    register int i;
    register int j;
    register unsigned char *s;

    s = buf;
    i = nbytes;

    while (i > 0) {
	if ((j = read(dlfd,s,i)) <= 0) {
	    if (j == 0)
		fprintf(stderr,"EOF on casio device.\n");
	    else
		fprintf(stderr,"Read Failure.\n");
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}

	i -= j;
	s += j;
    }

    /* Make sure bytes are HEX */


    s = buf;
    i = nbytes;
    while (i > 0) {

	j = *s++;

	if ((j < '0' || j > '9') && (j < 'A' || j > 'F')) {
	    fprintf(stderr,"Illegal Hex Digit read %x(%c).\n",j,j);
	    ioctl(dlfd,TCSETAW,&t_old);
	    exit(1);
	}
	i--;
    }

    return;
}
@EOF

chmod 666 casioreadraw.c

echo x - casiowrite.c
cat >casiowrite.c <<'@EOF'
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <sys/termio.h>
#include <sys/modem.h>
#include <sys/signal.h>
#include "casio.h"

#define FALSE 0
#define TRUE  1

#define MAXARGS 40
char *arg[MAXARGS];

int dlfd;
struct termio t_old,t_new;
int modeschanged = 0;

/* External Declarations */

extern char *fgets();

/* Forward Declarations */

void send(), packsend(), convert_to_hex();

catch_intr() {
    if (modeschanged != 0) {
	ioctl(dlfd,TCSETA,&t_old);
    }
    exit(1);
}


char *commands[] = { "","%%%TYPE","","%%%EOR","","%%%TIME",
		     "%%%ALARM","%%%DATE","%%%CALENDAR",
		     (char *)0
		   };


main(argc,argv)
    int argc;
    char **argv;
{
    register unsigned char *s;
    register int nbytes;
    register int i;
    register int pass;
    int nargs;
    int lineno;
    unsigned long modemflags;
    static unsigned char inline[1024];
    unsigned char term_default;
    struct timeval tv;
    int readfds;

    signal(SIGINT,catch_intr);

    if ((dlfd = open(argv[1],O_RDWR)) < 0) {
	fprintf(stderr,"Could not open casio device %s.\n",argv[1]);
	exit(1);
    }

    ioctl(dlfd,TCGETA,&t_old);

    t_new.c_cc[VMIN]  = 1;
    t_new.c_cc[VTIME] = 0;
    t_new.c_iflag = (IGNBRK | IXON);
    t_new.c_oflag = 0;
    t_new.c_cflag = (B9600 | CS8 | CREAD | CSTOPB);
    t_new.c_lflag = 0;
    t_new.c_line  = 0;

    ioctl(dlfd,TCSETAW,&t_new);
    modeschanged = 1;

    /* Make sure DTR is on and RTS is off */

    ioctl(dlfd,MCGETA,&modemflags);
    modemflags |= MDTR;
    modemflags &= ~MRTS;
    ioctl(dlfd,MCSETA,&modemflags);

    /* Read any initial junk */


    tv.tv_sec  = 0;
    tv.tv_usec = 0;
    readfds = 1 << dlfd;
    select(dlfd + 1,&readfds,(int *)0,(int *)0,&tv);
    if (readfds != 0)
	(void) read(dlfd,inline,1024);

    /* Initial Handshake */

    send("\r\n",2,'\0',FALSE);
    send("\r\n",2,'\0',FALSE);

    term_default = '\r';

    for (pass = 0; pass < 2; pass++) {
	if (pass == 0)
	    fprintf(stderr,"Checking Syntax\n");
	else
	    fprintf(stderr,"Beginning Download\n");

	nbytes = 0;
	lineno = 0;
	while (fgets(inline,1024,stdin) != (char *)0) {

	    lineno++;

	    if (memcmp(inline,"%%%",3) == 0) {
		nargs = parse(inline);
		i = 0;
		for (i= 0; commands[i] != (char *)0; i++) {
		    if (strcmp(commands[i],arg[0]) == 0)
			break;
		}


		switch (i) {

		case BT_START:
		    i = type_fill(inline,nargs,&term_default);
		    if (pass != 0)
			packsend(0x00,0x00,0x02,inline,i,FALSE);
		    break;

		case BT_ENDRECORD:
		    i = 0;
		    if (nargs > 1) {
			if (nargs != 2 || strcmp(arg[1],"MARK") != 0) {
			    fprintf(stderr,"Line %d: Illegal parameter to EOR command.\n",lineno);
			    exit(1);
			}
			i = 1;
		    }

		    if (pass != 0) {
			if (i == 0)
			    packsend(0x00,0x00,0x01,inline,0,TRUE);
			else
			    packsend(0x80,0x00,0x01,inline,0,TRUE);
		    }
		    break;

		case BT_ALARM:
		    i = alarm_fill(inline,nargs);
		    if (pass != 0)
			packsend(0xC0,0x00,0x00,inline,i,FALSE);
		    break;

		case BT_TIME:
		    i = time_fill(inline,nargs);
		    if (pass != 0)
			packsend(0xE0,0x00,0x00,inline,i,FALSE);
		    break;

		case BT_DATE:
		    i = date_fill(inline,nargs);
		    if (pass != 0)
			packsend(0xF0,0x00,0x00,inline,i,FALSE);
		    break;

		default:
		    fprintf(stderr,"Illegal command in input file.\n");
		    exit(1);
		}
		nbytes = 0;
	    }
	    else {
		i = strlen(inline);
		if (inline[i-1] != '\n') {
		    fprintf(stderr,"Line %d: Too long.\n",lineno);
		    exit(1);
		}

		if (pass != 0) {
		    if (i >= 3 && inline[i - 3] == '\\'
			&& (i == 3 || inline[i-4] != '\\')
			&& (inline[i-2] == 'r' || inline[i-2] == 'n') ) {

			if (inline[i-2] == 'r')
			    inline[i-3] = '\r';
			else
			    inline[i-3] = '\n';

			inline[i-2] = '\0';

			i -= 2;
		    }
		    else
			inline[i-1] = term_default;

		    packsend((0x80 | (nbytes >> 8)),(nbytes & 0xff),0x00,inline,i,FALSE);
		    nbytes += i;
		}
	    }
	}

	/* Rewind Standard Input */

	rewind(stdin);
    }

    /* End Transfer */

    packsend(0x00,0x00,0xFF,inline,0,FALSE);

    putc('\n',stderr);
    exit(0);
}

int
parse(pbuf)
    register char *pbuf;
{
    register int argcount;

    /* Get rid of trailing newline */
    /* (temporarily use argcount)  */

    argcount = strlen(pbuf);
    if (pbuf[argcount - 1] == '\n')
	pbuf[argcount - 1] = '\0';

    argcount = 0;

    while (*pbuf != '\0' && argcount != MAXARGS) {

	while (*pbuf == ' ' || *pbuf == '\t')
	    pbuf++;

	if (*pbuf != '\0') {
	    arg[argcount++] = pbuf;

	    /* Skip until seperator or null */

	    while (*pbuf != ' ' && *pbuf != '\t' && *pbuf != '\0')
		pbuf++;

	    if (*pbuf != '\0')
		*pbuf++ = '\0';
	}
    }

    if (argcount == MAXARGS)
	return(-1);

    return(argcount);
}

char *types[] = { "","CALENDAR","PHONE","MEMO","SCHEDULE","BUSCARD",
		  (char *)0
		};

int
type_fill(buf,nargs,term_defptr)
    register char *buf;
    int nargs;
    char *term_defptr;
{
    register int i;
    register char *s;

    if (nargs != 2) {
	fprintf(stderr,"Bad number of arguments for TYPE command.\n");
	exit(1);
    }

    s = arg[1];
    for (i = 0; types[i] != (char *)0; i++) {
	if (strcmp(types[i],s) == 0)
	    break;
    }


    s = buf;
    *term_defptr = '\r';
    switch (i) {

    case ET_CALENDAR:
	*s++ = 0x80;
	break;

    case ET_TELEPHONE:
	*s++ = 0x90;
	*term_defptr = '\n';
	break;

    case ET_MEMO:
	*s++ = 0xA0;
	break;

    case ET_SCHEDULE:
	*s++ = 0xB0;
	break;

    case ET_BUSCARD:
	*s++ = 0xC0;
	*term_defptr = '\n';
	break;

    default:
	fprintf(stderr,"Illegal type parameter for TYPE command.\n");
	exit(1);
    }

    *s++ = 0x00;
    return(2);
}

int
alarm_fill(buf,nargs)
    register char *buf;
    int nargs;
{
    register int i;
    register char *s;
    int alarm_time;

    if (nargs != 2) {
	fprintf(stderr,"Bad number of arguments for ALARM command.\n");
	exit(1);
    }

    s = arg[1];
    alarm_time = get_time(s,"ALARM");
    sprintf(buf,"%02d:%02d",alarm_time / 100, alarm_time % 100);
    return(5);
}

int
time_fill(buf,nargs)
    register char *buf;
    int nargs;
{
    register char *s;
    int start_time;
    int end_time;
    int time_len;

    if (nargs != 2) {
	fprintf(stderr,"Bad number of arguments for TIME command.\n");
	exit(1);
    }

    s = arg[1];
    if (strlen(s) == 11 && s[5] == '~') {
	s[5] = '\0';
	end_time = get_time(&s[6],"TIME");
    }
    else
	end_time = -1;

    start_time = get_time(s,"TIME");

    if (end_time != -1) {
	sprintf(buf,"%02d:%02d~%02d%02d",start_time / 100, start_time % 100,
					 end_time / 100, end_time % 100);
	time_len = 11;
    }
    else {
	sprintf(buf,"%02d:%02d",start_time / 100, start_time % 100);
	time_len = 5;
    }

    return(time_len);
}

int
get_time(timestring,timetype)
    char *timestring;
    char *timetype;
{
    register char *s;
    int hour;
    int minute;

    s = timestring;
    if (strlen(s) != 5 || s[2] != ':') {
	fprintf(stderr,"Illegal format in time for %s command.\n",timetype);
	exit(1);
    }

    s[2] = '\0';
    hour = atoi(&s[0]);
    if (hour < 0 || hour > 23) {
	fprintf(stderr,"Illegal hour in time for %s command.\n",timetype);
	exit(1);
    }

    minute = atoi(&s[3]);
    if (minute < 0 || minute > 59) {
	fprintf(stderr,"Illegal minute in time for %s command.\n",timetype);
	exit(1);
    }

    return((hour * 100) + minute);
}

int
date_fill(buf,nargs)
    register char *buf;
    int nargs;
{
    register char *s;
    int year;
    int month;
    int day;

    if (nargs != 2) {
	fprintf(stderr,"Bad number of arguments for DATE command.\n");
	exit(1);
    }

    s = arg[1];

    if (strlen(s) != 10 || s[4] != '-' || s[7] != '-') {
	fprintf(stderr,"Illegal format in date for DATE command.\n");
	exit(1);
    }

    s[4] = '\0';
    s[7] = '\0';

    year = atoi(&s[0]);
    if (year < 1901 || year > 2099) {
	fprintf(stderr,"Illegal year in DATE command.\n");
	exit(1);
    }

    month = atoi(&s[5]);
    if (month < 1 || month > 12) {
	fprintf(stderr,"Illegal month in DATE command.\n");
	exit(1);
    }

    day = atoi(&s[8]);
    if (day < 1 || day > 31) {
	fprintf(stderr,"Illegal day in DATE command.\n");
	exit(1);
    }

    sprintf(buf,"%4d-%02d-%02d",year,month,day);
    return(10);
}

void
packsend(ctrlbyte1,ctrlbyte2,ctrlbyte3,inbuf,nbytes,handshakeflag)
    unsigned char ctrlbyte1;
    unsigned char ctrlbyte2;
    unsigned char ctrlbyte3;
    unsigned char *inbuf;
    register int nbytes;
    int handshakeflag;
{
    register int i;
    register int checksum;
    register unsigned char *s;
    unsigned char ch;
    static unsigned char outbuf[4096];

    /* Compute Checksum */

    checksum = nbytes + ctrlbyte1 + ctrlbyte2 + ctrlbyte3;

    s = inbuf;

    for (i = 0; i < nbytes; i++)
	checksum += *s++;

    checksum = 0x100 - (checksum & 0xff);

    s = outbuf;
    *s++ = ':';

    ch = nbytes;
    convert_to_hex(s,&ch,1);
    s += 2;
    convert_to_hex(s,&ctrlbyte1,1);
    s += 2;
    convert_to_hex(s,&ctrlbyte2,1);
    s += 2;
    convert_to_hex(s,&ctrlbyte3,1);
    s += 2;
    convert_to_hex(s,inbuf,nbytes);
    s += (nbytes * 2);
    ch = checksum;
    convert_to_hex(s,&ch,1);

    send(outbuf,1 + 2 * (nbytes + 5),'#',handshakeflag);

    return;
}

void
send(outbuf,nbytes,handshakebyte,handshakeflag)
    unsigned char *outbuf;
    int nbytes;
    unsigned char handshakebyte;
    int handshakeflag;
{
    unsigned char ch;

    if (write(dlfd,outbuf,nbytes) != nbytes) {
	fprintf(stderr,"Write Failure.\n");
	exit(1);
    }

    if (handshakeflag != FALSE) {
	putc('.',stderr);

	if (read(dlfd,&ch,1) != 1) {
	    fprintf(stderr,"Read Failure.\n");
	    exit(1);
	}

	if (ch != handshakebyte) {
	    fprintf(stderr,"Hand shake failed, expected %x(%c) got %x(%c).\n",
			    handshakebyte,handshakebyte,ch,ch);
	    exit(1);
	}
    }

    return;
}

void
convert_to_hex(to,from,nbytes)
    unsigned char *to;
    unsigned char *from;
    register int nbytes;
{
    register unsigned char *s1;
    register unsigned char *s2;
    register int i;

    s1 = to;
    s2 = from;
    while (nbytes-- > 0) {

	i = ((*s2) >> 4) & 0xf;
	if (i > 9)
	    *s1++ = 'A' + (i - 10);
	else
	    *s1++ = '0' + i;

	i = (*s2++) & 0xf;
	if (i > 9)
	    *s1++ = 'A' + (i - 10);
	else
	    *s1++ = '0' + i;
    }

    return;
}
@EOF

chmod 666 casiowrite.c

echo x - readme
cat >readme <<'@EOF'
to backup data: casioread /dev/cul09 >file
to look at it: casioformat <file 
@EOF

chmod 666 readme

exit 0
Newsgroups: comp.sys.palmtops