view tool/readspec.c @ 18:94999935b791 default tip

main-rvtmodem.patch created
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sun, 01 Nov 2015 06:40:12 +0000
parents 12e230d431f0
children
line wrap: on
line source

/*
 * This module contains the code that reads and parses the patch
 * description or specification ASCII text file.
 */

#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "patchinfo.h"
#include "globals.h"

static int lineno;
static struct patch_module_desc **module_add_ptr = &patch_module_list;
static struct patch_desc **patch_add_ptr;

static void
set_module(name)
	char *name;
{
	int len;
	char *buf;
	struct patch_module_desc *newmod;

	len = sizeof(struct patch_module_desc) + strlen(name) + 1;
	buf = malloc(len);
	if (!buf) {
		perror("malloc");
		exit(1);
	}
	newmod = (struct patch_module_desc *) buf;
	newmod->member_name = buf + sizeof(struct patch_module_desc);
	strcpy(newmod->member_name, name);
	newmod->patches = 0;
	newmod->next = 0;
	*module_add_ptr = newmod;
	module_add_ptr = &newmod->next;
	patch_add_ptr = &newmod->patches;
}

static void
add_patch(argline)
	char *argline;
{
	char *cp, *args[3];
	int i, len;
	char *buf;
	struct patch_desc *rec;

	for (cp = argline, i = 0; i < 3; i++) {
		while (isspace(*cp))
			cp++;
		if (!*cp) {
			fprintf(stderr, "%s line %d: too few fields\n",
				patch_desc_filename, lineno);
			exit(1);
		}
		args[i] = cp;
		while (*cp && !isspace(*cp))
			cp++;
		if (*cp)
			*cp++ = '\0';
	}
	while (isspace(*cp))
		cp++;
	if (*cp) {
		fprintf(stderr, "%s line %d: too many fields\n",
			patch_desc_filename, lineno);
		exit(1);
	}
	if (!patch_add_ptr) {
		fprintf(stderr,
			"error: patch given before module (%s line %d)\n",
			patch_desc_filename, lineno);
		exit(1);
	}
	len = sizeof(struct patch_desc) + strlen(args[0]) + 1;
	buf = malloc(len);
	if (!buf) {
		perror("malloc");
		exit(1);
	}
	rec = (struct patch_desc *) buf;
	rec->section = buf + sizeof(struct patch_desc);
	strcpy(rec->section, args[0]);
	rec->offset = strtoul(args[1], 0, 16);
	rec->new_byte = strtoul(args[2], 0, 16);
	rec->next = 0;
	*patch_add_ptr = rec;
	patch_add_ptr = &rec->next;
}

read_spec_file()
{
	FILE *f;
	char linebuf[128], *cp, *np;

	f = fopen(patch_desc_filename, "r");
	if (!f) {
		perror(patch_desc_filename);
		exit(1);
	}
	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (!*cp || *cp == '#')
			continue;
		if (*cp == '[') {
			np = ++cp;
			cp = index(cp, ']');
			if (!cp) {
				fprintf(stderr,
					"%s line %d: unmatched bracket\n",
					patch_desc_filename, lineno);
				exit(1);
			}
			*cp = '\0';
			set_module(np);
		} else
			add_patch(cp);
	}
	fclose(f);
	return(0);
}