view netdiff/renpart/mainproc.c @ 142:7bdce91da1a5

unet-excise utility added
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 19 Sep 2020 22:50:24 +0000
parents bf188727e606
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 part_rename *part_rename_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;
	struct part_rename *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];
	for (rp = part_rename_list; rp; rp = rp->next) {
		if (!strcmp(refdes, rp->old)) {
			refdes = rp->new;
			break;
		}
	}
	printf("%s\t%s.%s\n", fields[0], refdes, cp);
}

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);
}