/* dehex.c  30-Apr-89  A.J.Travis *//* Uses __bbc__ and __riscos__ instead of DFS *//* * based on Alan Philips hex to binary converter for * binaries in the Lancaster BBC Micro kermit distribution */#include <stdio.h>#define TRUE	1#define FALSE	0#define SAME	0		/* strcmp == */#ifdef __bbc__#define W_CAT 1				/* write MOS catalogue info */#define RW 3				/* read/write attributes */#endif#ifdef __riscos__#define W_CAT 1				/* write MOS catalogue info */#define RW 3				/* read/write attributes */#endifFILE *in;FILE *out;int type;int c;char sum;int len;int rec;int base;int addr;int q;int i;int verbose;			/* print details of each record extracted */int main(argc, argv)int argc;char *argv[];{	in = NULL;	out = NULL;	if (argc < 3) {		fprintf(stderr, "usage: dehex [-v] infile outfile\n");		fatal(-1);	}	if (strcmp(argv[1], "-v") == SAME) {		verbose = TRUE;		--argc;		++argv;	}	else		verbose = FALSE;	if ((in = fopen(argv[1], "r")) == NULL) {		fprintf(stderr, "Input file does not exist\n");		fatal(-1);	}	if ((out = fopen(argv[2], "wb")) == NULL) {		fprintf(stderr, "Can't open output file\n");		fatal(-1);	}	rec = 1;	while (record())		++rec;	fclose(in);	fclose(out);#ifdef W_CAT	attributes(argv[2], base, base);#endif	exit(0);}/* * close files, and exit on fatal error */fatal(stat)int stat;{	if (in)		fclose(in);	if (out)		fclose(out);	exit(stat);}		/* * read one record */record(){	while ((c = getc(in)) != ':') {		if (c == EOF)			return 0;	}	sum = 0;	len = getb();	addr = getw();	if (rec == 1)		base = addr;	if ((type = getb()) == 0) {		unhex(len);		return 1;	}	else if (type == 1) {		if (verbose)			printf("+++ End-of-file record detected\n");		return 0;	}	else {		fprintf(stderr, "*** Unknown record type %d detected\n", type);		fatal(-1);	}	if (sum != 0) {		fprintf(stderr, "*** Checksum error (%04X)\n", sum);		fatal(-1);	}}/* * get hex byte from input */getb(){	int val;		val = toint(getc(in)) << 4;	val = val + toint(getc(in));	sum = sum + val;	return val;}/* * get hex word from input */getw(){	int val;		val = getb() << 8;	val = val + getb();	return val;}/* * convert hex char to int */toint(c)int c;{	if (c >= 'A')		return c - 'A' + 10;	else		return c - '0';}/* * extract hex record */unhex(len)int len;{	if (verbose)		printf("Record %4d: Size %2d, address $%04X\n", rec, len, addr);	addr = addr + len;	while (len--)		putc(getb(), out);}#ifdef W_CAT/* * set MOS load and execution attributes on object file */attributes(file, load, exec)char *file;				/* filename to set attributes on */unsigned load;				/* load address */unsigned exec;				/* execution address */{	int *fcb[9];			/* file control block */	fcb[0] = 0;	fcb[1] = load;	fcb[2] = 0;	fcb[3] = exec;	fcb[4] = 0;	fcb[5] = 0;	fcb[6] = 0;	fcb[7] = RW;	fcb[8] = 0;	return(osfile(file, fcb, W_CAT));}#endif