1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-06-28 23:14:33 +03:00
eda-tools/b2/boom.c

203 lines
4.2 KiB
C

/*
* boom.c - BOOM, main function
*
* Copyright 2012 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
#include "lang.h"
#include "param.h"
#include "chr.h"
#include "subst.h"
#include "subex.h"
#include "db.h"
static struct param *vars = NULL, **last_var = &vars;
static int select_parts = 0;
static int verbose = 0;
static void add_var(const char *arg)
{
char *tmp = stralloc(arg);
char *eq;
eq = strchr(tmp, '=');
if (!eq) {
fprintf(stderr, "no = in variable setting\n");
exit(1);
}
*eq = 0;
*last_var = make_var(tmp, rel_eq, eq+1);
last_var = &(*last_var)->next;
free(tmp);
}
static void do_substitutions(void)
{
struct param *out;
const struct param *var;
const struct part **parts, **p;
if (!substitute(substitutions, vars, &out)) {
fprintf(stderr, "ignore\n");
return;
}
if (select_parts) {
parts = select_parametric(out, &hierarchy);
if (!parts) {
fprintf(stderr, "no matches\n");
} else {
for (p = parts; *p; p++)
printf("%s %s\n", (*p)->domain, (*p)->name);
free(parts);
}
} else {
for (var = out; var; var = var->next) {
printf("%s", var->u.name);
dump_relop(stdout, var->op);
printf("%s\n", var->value.u.s);
}
free_vars(out);
}
}
static void parse_re(const char *re)
{
char *res;
subst_match(NULL, re, &res);
printf("%s\n", res);
}
static void dump_hierarchy(struct action act)
{
fields_dump(stderr, act.fields);
if (act.fields)
fprintf(stderr, "\n");
rules_dump(stderr, act.rules);
}
static void dump(const char *s)
{
while (*s) {
switch (*s) {
case 'c':
parts_dump(stderr);
break;
case 'h':
dump_hierarchy(hierarchy);
break;
case 's':
subst_dump(stderr, substitutions);
break;
default:
fprintf(stderr, "no database '%c'\n", *s);
exit(1);
}
s++;
}
}
static void usage(const char *name)
{
fprintf(stderr,
"usage: %s file [-v] [[-N name] [-type] file ...] [(-q|-Q) var=value ...] ...\n\n"
" file types:\n"
" -c characteristics\n"
" -i inventory\n"
" -x currency exchange\n"
" -p providers\n"
" -s substitutions\n"
" -b KiCad eeschema BOM\n"
" -X symbols (BOM supplement)\n"
" other options:\n"
" -v ... increase verbosity level\n"
" -dCHARS dump the specified database (h, c, s, ...)\n"
" -N name for the next file, override the name in diagnostics\n"
" -q var=value ... run substitutions with the specified inputs\n"
" -Q var=value ... run substitutions and then do parametric search\n"
" -R regex parse and print regular expression\n"
, name);
exit(1);
}
int main(int argc, char **argv)
{
void (*process)(const char *name) = parse_hierarchy;
int query = 0;
int i;
dollar = unique("$");
subst_init();
subex_init();
for (i = 1; i != argc; i++) {
if (*argv[i] != '-') {
process(argv[i]);
continue;
}
if (!strcmp(argv[i], "-N")) {
i++;
file_name_override = argv[i];
} else if (!strcmp(argv[i], "-v")) {
verbose++;
} else if (!strncmp(argv[i], "-d", 2)) {
if (argv[i][2]) {
dump(argv[i]+2);
} else {
i++;
if (!argv[i])
usage(*argv);
dump(argv[i]);
}
} else if (!strcmp(argv[i], "-c")) {
process = parse_characteristics;
} else if (!strcmp(argv[i], "-i")) {
process = parse_inventory;
} else if (!strcmp(argv[i], "-x")) {
process = parse_currencies;
} else if (!strcmp(argv[i], "-p")) {
process = parse_providers;
} else if (!strcmp(argv[i], "-s")) {
process = parse_substitutions;
} else if (!strcmp(argv[i], "-b")) {
process = parse_kicad_bom;
} else if (!strcmp(argv[i], "-X")) {
process = parse_symbols;
} else if (!strcmp(argv[i], "-q")) {
process = add_var;
query = 1;
} else if (!strcmp(argv[i], "-Q")) {
process = add_var;
query = 1;
select_parts = 1;
} else if (!strcmp(argv[i], "-R")) {
i++;
if (!argv[i])
usage(*argv);
parse_re(argv[i]);
} else
usage(*argv);
}
if (query)
do_substitutions();
return 0;
}