view ueda/utils/instfileelem.c @ 25:ace4f60773ca

ueda/Makefile: unet-utils added
author Space Falcon <falcon@ivan.Harhan.ORG>
date Thu, 06 Aug 2015 20:22:24 +0000
parents e2130f1ef720
children
line wrap: on
line source

/*
 * This program instantiates a file element, i.e., locates it by name and emits
 * it on stdout with the refdes filled in.  The value can also be filled in
 * (optionally).
 */

#include <sys/param.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

char dirlist_pathname[] = "/usr/local/eda/file-element-dirs";
char *fpname, *refdes, *value;
char fileelem_pathname[MAXPATHLEN];

main(argc, argv)
	char **argv;
{
	register FILE *lf, *ef;
	char linebuf[1024];
	register char *cp;

	if (argc < 3 || argc > 4) {
		fprintf(stderr, "usage: %s footprint refdes [value]\n",
			argv[0]);
		exit(1);
	}
	fpname = argv[1];
	refdes = argv[2];
	value = argv[3];

	lf = fopen(dirlist_pathname, "r");
	if (!lf) {
		perror(dirlist_pathname);
		exit(1);
	}
	for (ef = NULL; fgets(linebuf, sizeof linebuf, lf); ) {
		if (linebuf[0] == '\0' || isspace(linebuf[0]))
			continue;
		cp = index(linebuf, '\0');
		while (isspace(cp[-1]))
			cp--;
		*cp = '\0';
		sprintf(fileelem_pathname, "%s/%s", linebuf, fpname);
		if (ef = fopen(fileelem_pathname, "r"))
			break;
		sprintf(fileelem_pathname, "%s/%s.fp", linebuf, fpname);
		if (ef = fopen(fileelem_pathname, "r"))
			break;
	}
	fclose(lf);
	if (!ef) {
		fprintf(stderr, "No footprint named %s\n", fpname);
		exit(1);
	}

	while (fgets(linebuf, sizeof linebuf, ef)) {
		for (cp = linebuf; isspace(*cp); cp++)
			;
		if (!strncmp(cp, "Element", 7) &&
		    (cp[7] == '(' || cp[7] == '[' || isspace(cp[7])))
			do_element_line(cp + 7);
		else
			fputs(linebuf, stdout);
	}
	fclose(ef);
	exit(0);
}

do_element_line(cp)
	register char *cp;
{
	char opench, closech;
	char *fields[11], refdesq[64], valueq[64];
	int nfields;
	register int c;
	char *err;

	while (isspace(*cp))
		cp++;
	opench = *cp++;
	switch (opench) {
	case '(':
		closech = ')';
		break;
	case '[':
		closech = ']';
		break;
	default:
		err = "no valid opening char";
inv:		fprintf(stderr, "%s: invalid Element line: %s\n",
			fileelem_pathname, err);
		exit(1);
	}

	for (nfields = 0; ; ) {
		while (isspace(*cp))
			cp++;
		if (!*cp) {
badeol:			err = "missing closing char";
			goto inv;
		}
		if (*cp == closech) {
			cp++;
			break;
		}
		if (nfields >= 11) {
			err = "too many fields";
			goto inv;
		}
		fields[nfields++] = cp;
		while ((c = *cp) && !isspace(c) && c != closech)
			cp++;
		if (!c)
			goto badeol;
		*cp++ = '\0';
		if (c == closech)
			break;
	}

	sprintf(refdesq, "\"%s\"", refdes);
	if (value)
		sprintf(valueq, "\"%s\"", value);
	if (nfields == 11 || nfields == 9) {
		fields[2] = refdesq;
		if (value)
			fields[3] = valueq;
	} else if (nfields == 8)
		fields[2] = refdesq;
	else if (nfields == 7)
		fields[1] = refdesq;
	else {
		err = "unrecognized format";
		goto inv;
	}

	printf("Element%c", opench);
	for (c = 0; c < nfields; c++) {
		if (c)
			putchar(' ');
		fputs(fields[c], stdout);
	}
	putchar(closech);
	fputs(cp, stdout);
}