view bootmatch/comp_ranges.c @ 21:9d7d2fdc733b

new shell scripts c155-gen-fc-{ffs-img,script}
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Jun 2023 00:28:10 +0000
parents bfcc8180cf3c
children
line wrap: on
line source

/*
 * Bootmatch compiler: here we read and parse the *.ranges file.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include "comp_defs.h"

extern struct range range_list[MAX_RANGES];
extern unsigned range_count;

static void
process_line(line, filename, lineno)
	char *line, *filename;
{
	char *cp;
	struct range *rp;
	u_long hex_start, hex_end;

	for (cp = line; isspace(*cp); cp++)
		;
	if (*cp == '\0' || *cp == '#')
		return;
	if (!isxdigit(*cp)) {
inv_syntax:	fprintf(stderr, "%s line %d: invalid syntax\n",
			filename, lineno);
		exit(1);
	}
	hex_start = strtoul(cp, &cp, 16);
	if (!isspace(*cp))
		goto inv_syntax;
	while (isspace(*cp))
		cp++;
	if (!isxdigit(*cp))
		goto inv_syntax;
	hex_end = strtoul(cp, &cp, 16);
	if (*cp && !isspace(*cp))
		goto inv_syntax;
	while (isspace(*cp))
		cp++;
	if (*cp && *cp != '#')
		goto inv_syntax;
	if (hex_start >= BOOT_BLOCK_SIZE) {
		fprintf(stderr, "%s line %d: start offset is out of range\n",
			filename, lineno);
		exit(1);
	}
	if (hex_end >= BOOT_BLOCK_SIZE) {
		fprintf(stderr, "%s line %d: end offset is out of range\n",
			filename, lineno);
		exit(1);
	}
	if (hex_start >= hex_end) {
		fprintf(stderr,
		"%s line %d: end offset must be greater than start offset\n",
			filename, lineno);
		exit(1);
	}
	if (range_count >= MAX_RANGES) {
		fprintf(stderr, "%s line %d: too many ranges defined\n",
			filename, lineno);
		exit(1);
	}
	rp = range_list + range_count;
	rp->offset = hex_start;
	rp->nbytes = hex_end - hex_start;
	range_count++;
}

void
read_ranges_file(filename)
	char *filename;
{
	FILE *inf;
	char linebuf[256];
	int lineno;

	inf = fopen(filename, "r");
	if (!inf) {
		perror(filename);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
		process_line(linebuf, filename, lineno);
	fclose(inf);
	if (!range_count) {
		fprintf(stderr, "error: no ranges defined in %s\n", filename);
		exit(1);
	}
}