view pads2gpcb/parttype.c @ 63:455a0051f9d2

pads2gpcb: PARTTYPE parsing implemented, compiles
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sun, 31 Jan 2016 07:32:08 +0000
parents
children 2b71943a311b
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <strings.h>
#include "globals.h"
#include "struct.h"

extern struct part_decal *find_decal_by_name();
extern char *savestr();

static struct part_type *our_parttype;
static int ngates, nsignals;

static void
enter_parttype()
{
	struct part_type *p, **pp;
	char *name = input_line_fields[0];

	for (pp = &part_type_list; p = *pp; pp = &p->next)
		if (!strcmp(p->name, name)) {
			fprintf(stderr,
			"%s line %d: part type name \"%s\" already defined\n",
				input_filename, input_lineno, name);
			exit(1);
		}
	p = malloc(sizeof(struct part_type) + strlen(name) + 1);
	if (!p) {
		perror("malloc of struct part_type");
		exit(1);
	}
	bzero(p, sizeof(struct part_type));
	p->name = (char *)(p + 1);
	strcpy(p->name, name);
	*pp = p;
	our_parttype = p;
}

static void
find_decals()
{
	char *cp, *np;
	struct part_decal *decal;

	for (cp = input_line_fields[1]; cp; cp = np) {
		np = index(cp, ':');
		if (np)
			*np++ = '\0';
		if (!*cp)
			continue;
		decal = find_decal_by_name(cp);
		if (!decal) {
			fprintf(stderr,
"%s line %d: part type definition refers to unknown decal name \"%s\"\n",
				input_filename, input_lineno, cp);
			exit(1);
		}
		if (our_parttype->ndecals >= MAX_DECALS_PER_PART_TYPE) {
			fprintf(stderr,
			"%s line %d: MAX_DECALS_PER_PART_TYPE exceeded\n",
				input_filename, input_lineno);
			exit(1);
		}
		our_parttype->decals[our_parttype->ndecals++] = decal;
	}
	if (!our_parttype->ndecals) {
		fprintf(stderr,
			"%s line %d: empty list of decals in part type def\n",
			input_filename, input_lineno);
		exit(1);
	}
}

static void
process_header_line()
{
	if (input_line_nfields < 6) {
		fprintf(stderr,
"%s line %d: expected beginning of part type definition, wrong # of fields\n",
			input_filename, input_lineno);
		exit(1);
	}
	enter_parttype();
	printf("Processing part type %s\n", our_parttype->name);
	find_decals();
	ngates = atoi(input_line_fields[3]);
	nsignals = atoi(input_line_fields[4]);
	our_parttype->num_alpha_pins = atoi(input_line_fields[5]);
}

static void
get_line_internal()
{
	if (!get_input_line()) {
		fprintf(stderr,
		      "error: EOF in the middle of a part type definition\n");
		exit(1);
	}
}

static void
process_one_gate_def()
{
	int gatepins;

	get_line_internal();
	parse_input_line_fields();
	if (input_line_nfields != 3 || strcmp(input_line_fields[0], "G")) {
		fprintf(stderr, "%s line %d: expected gate def header line\n",
			input_filename, input_lineno);
		exit(1);
	}
	gatepins = atoi(input_line_fields[2]);
	while (gatepins > 0) {
		get_line_internal();
		parse_input_line_fields();
		gatepins -= input_line_nfields;
	}
}

static void
alloc_alpha_pins_array()
{
	our_parttype->alpha_pins =
			malloc(sizeof(char *) * our_parttype->num_alpha_pins);
	if (!our_parttype->alpha_pins) {
		perror("malloc of alpha_pins array");
		exit(1);
	}
}

static void
read_alpha_pins()
{
	int idx, i;

	for (idx = 0; idx < our_parttype->num_alpha_pins; ) {
		get_line_internal();
		parse_input_line_fields();
		for (i = 0; i < input_line_nfields; i++) {
			if (idx >= our_parttype->num_alpha_pins) {
				fprintf(stderr,
				"%s line %d: alpha_pins array overflow\n",
					input_filename, input_lineno);
				exit(1);
			}
			our_parttype->alpha_pins[idx++] =
						savestr(input_line_fields[i]);
		}
	}
}

static void
process_one_parttype()
{
	int i;

	process_header_line();
	for (i = 0; i < ngates; i++)
		process_one_gate_def();
	for (i = 0; i < nsignals; i++)
		get_line_internal();
	if (our_parttype->num_alpha_pins) {
		alloc_alpha_pins_array();
		read_alpha_pins();
	}
}

process_parttype_section()
{
	for (;;) {
		if (!get_input_line()) {
			fprintf(stderr, "error: EOF in PARTTYPE section\n");
			exit(1);
		}
		if (input_line_buf[0] == '*') {
			parse_starline();
			if (strcmp(input_line_starkw, "REMARK"))
				break;
			else
				continue;
		}
		parse_input_line_fields();
		if (input_line_nfields)
			process_one_parttype();
	}
}