view netdiff/flip2pin/mainproc.c @ 146:7ddfb9a67b0c

netdiff: donl-flip2pin utility written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 15 Nov 2020 03:32:48 +0000
parents
children
line wrap: on
line source

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

extern struct flip_list *flip_list;

#define	MAX_FIELDS	2

static char *infname;
static FILE *inf;
static char linebuf[512];
static int lineno;
static char *fields[MAX_FIELDS];
static unsigned nfields;

static
get_line()
{
	if (!fgets(linebuf, sizeof linebuf, inf))
		return(0);
	lineno++;
	if (!index(linebuf, '\n')) {
		fprintf(stderr, "%s line %d: missing newline\n",
			infname, lineno);
		exit(1);
	}
	return(1);
}

static void
parse_into_fields()
{
	char *cp;

	nfields = 0;
	for (cp = linebuf; ; ) {
		while (isspace(*cp))
			cp++;
		if (*cp == '\0' || *cp == '#')
			break;
		if (nfields >= MAX_FIELDS) {
			fprintf(stderr, "%s line %d: too many fields\n",
				infname, lineno);
			exit(1);
		}
		fields[nfields++] = cp;
		while (*cp && !isspace(*cp))
			cp++;
		if (*cp)
			*cp++ = '\0';
	}
}

static void
process_netpoint()
{
	char *cp, *refdes, *pin;
	struct flip_list *rp;

	cp = index(fields[1], '.');
	if (!cp) {
		fprintf(stderr, "%s line %d: expected '.' not found\n",
			infname, lineno);
		exit(1);
	}
	*cp++ = '\0';
	refdes = fields[1];
	pin = cp;
	for (rp = flip_list; rp; rp = rp->next) {
		if (!strcmp(refdes, rp->refdes))
			break;
	}
	if (rp) {
		if (!strcmp(pin, "1"))
			pin = "2";
		else if (!strcmp(pin, "2"))
			pin = "1";
		else {
			fprintf(stderr,
				"%s line %d: flip pin number is not 1 or 2\n",
				infname, lineno);
			exit(1);
		}
	}
	printf("%s\t%s.%s\n", fields[0], refdes, pin);
}

main_process(input_filename)
	char *input_filename;
{
	infname = input_filename;
	inf = fopen(infname, "r");
	if (!inf) {
		perror(infname);
		exit(1);
	}
	for (;;) {
		if (!get_line())
			break;
		parse_into_fields();
		if (!nfields)
			continue;
		if (nfields != 2) {
			fprintf(stderr, "%s line %d: expected 2 fields\n",
				infname, lineno);
			exit(1);
		}
		process_netpoint();
	}
	fclose(inf);
	return(0);
}