view ueda/mclutils/mkbom.c @ 139:bf188727e606

donl-rename-parts reader: no tEDAx-style escapes
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 07 Sep 2020 04:25:11 +0000
parents 88cdef7e6b1b
children
line wrap: on
line source

/*
 * This program generates a procurement-oriented BOM from the MCL.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "../libueda/mcl.h"
#include "bomstruct.h"

extern char *MCLfile;
extern char *get_comp_attr(), *get_comp_multiattr();

int check_completeness, refdes_lists;
struct bompart *bomhead;

do_cmdline_opts(argc, argv)
	char **argv;
{
	register int c;

	while ((c = getopt(argc, argv, "cM:p:r")) != EOF)
		switch (c) {
		case 'c':
			check_completeness++;
			break;
		case 'M':
			MCLfile = optarg;
			break;
		case 'p':
			set_popopt_list(optarg);
			break;
		case 'r':
			refdes_lists++;
			break;
		default:
			/* getopt prints the error message */
			exit(1);
		}
}

main(argc, argv)
	char **argv;
{
	do_cmdline_opts(argc, argv);
	read_MCL();
	tally_parts();
	output();
	exit(0);
}

output()
{
	register int i;
	register struct component *part;
	register struct bompart *bp;
	char *manuf, *partno, *desc, *spectitle;

	printf("Part\t\t\t\t\t\t\t\t\t    Qty\n");
	for (i = 0; i < 79; i++)
		putchar('-');
	putchar('\n');

	for (bp = bomhead; bp; bp = bp->next) {
		part = bp->part;
		manuf = get_comp_attr(part, "manufacturer");
		partno = get_comp_attr(part, "manufacturer_part_number");
		if (!partno)
			partno = get_comp_attr(part, "device");
		desc = get_comp_attr(part, "description");
		spectitle = get_comp_attr(part, "bom_part_title");
		if (spectitle) {
			fputs(spectitle, stdout);
			i = strlen(spectitle);
		} else if (manuf && partno)
			i = printf("%s %s", manuf, partno);
		else if (desc) {
			fputs(desc, stdout);
			i = strlen(desc);
			desc = NULL;		/* used it */
		} else {
			fprintf(stderr,
			"part %s: no identifying information for the BOM\n",
				part->name);
			continue;
		}
		for (i /= 8; i < 9; i++)
			putchar('\t');
		printf("%7d\n", bp->qty);
		if (desc)
			printf("  %s\n", desc);
		do_comments(part);
		do_sources(part);
		do_substitutes(part);
		if (refdes_lists)
			dump_refdes_list(bp->refdeslist);
	}
}

do_sources(part)
	register struct component *part;
{
	int scnt;
	register char *src;
	char *vendor, *vpn;

	for (scnt = 0; src = get_comp_multiattr(part, "source", &scnt); )
		printf("  Source: %s\n", src);
	if (scnt)
		return;
	/* no source= attributes, check for the old style vendor ones */
	vendor = get_comp_attr(part, "vendor");
	vpn = get_comp_attr(part, "vendor_part_number");
	if (vendor && vpn)
		printf("  Source: %s %s\n", vendor, vpn);
	else if (vendor)
		printf("  Source: %s\n", vendor);
}

do_substitutes(part)
	register struct component *part;
{
	int scnt;
	register char *s;

	for (scnt = 0; s = get_comp_multiattr(part, "substitute", &scnt); )
		printf("  Acceptable substitute: %s\n", s);
}

do_comments(part)
	register struct component *part;
{
	int scnt;
	register char *s;

	for (scnt = 0; s = get_comp_multiattr(part, "bom_comment", &scnt); )
		printf("  %s\n", s);
}

dump_refdes_list(le)
	register struct refdeslist *le;
{
	register int acc, i;

	for (acc = 0; le; le = le->next) {
		i = strlen(le->first) + 1;
		if (le->last != le->first)
			i += strlen(le->last) + 1;
		if (le->next)
			i++;
		if (acc && (acc + i >= 80)) {
			putchar('\n');
			acc = 0;
		}
		if (!acc) {
			putchar(' ');
			acc++;
		}
		printf(" %s", le->first);
		if (le->last != le->first)
			printf("-%s", le->last);
		if (le->next)
			putchar(',');
		acc += i;
	}
	if (acc)
		putchar('\n');
}