Root/b2/boom.c

1/*
2 * boom.c - BOOM, main function
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16
17#include "util.h"
18#include "lang.h"
19#include "param.h"
20#include "chr.h"
21#include "subst.h"
22#include "subex.h"
23#include "db.h"
24
25
26static struct param *vars = NULL, **last_var = &vars;
27static int select_parts = 0;
28static int verbose = 0;
29
30
31static void add_var(const char *arg)
32{
33    char *tmp = stralloc(arg);
34    char *eq;
35
36    eq = strchr(tmp, '=');
37    if (!eq) {
38        fprintf(stderr, "no = in variable setting\n");
39        exit(1);
40    }
41    *eq = 0;
42    *last_var = make_var(tmp, rel_eq, eq+1);
43    last_var = &(*last_var)->next;
44    free(tmp);
45}
46
47
48static void do_substitutions(void)
49{
50    struct param *out;
51    const struct param *var;
52    const struct part **parts, **p;
53
54    if (!substitute(substitutions, vars, &out)) {
55        fprintf(stderr, "ignore\n");
56        return;
57    }
58    if (select_parts) {
59        parts = select_parametric(out, &hierarchy);
60        if (!parts) {
61            fprintf(stderr, "no matches\n");
62        } else {
63            for (p = parts; *p; p++)
64                printf("%s %s\n", (*p)->domain, (*p)->name);
65            free(parts);
66        }
67    } else {
68        for (var = out; var; var = var->next) {
69            printf("%s", var->u.name);
70            dump_relop(stdout, var->op);
71            printf("%s\n", var->value.u.s);
72        }
73        free_vars(out);
74    }
75}
76
77
78static void parse_re(const char *re)
79{
80    char *res;
81
82    subst_match(NULL, re, &res);
83    printf("%s\n", res);
84}
85
86
87static void dump_hierarchy(struct action act)
88{
89    fields_dump(stderr, act.fields);
90    if (act.fields)
91        fprintf(stderr, "\n");
92    rules_dump(stderr, act.rules);
93}
94
95
96static void dump(const char *s)
97{
98    while (*s) {
99        switch (*s) {
100        case 'c':
101            parts_dump(stderr);
102            break;
103        case 'h':
104            dump_hierarchy(hierarchy);
105            break;
106        case 's':
107            subst_dump(stderr, substitutions);
108            break;
109        default:
110            fprintf(stderr, "no database '%c'\n", *s);
111            exit(1);
112        }
113        s++;
114    }
115}
116
117
118static void usage(const char *name)
119{
120    fprintf(stderr,
121"usage: %s file [-v] [[-N name] [-type] file ...] [(-q|-Q) var=value ...] ...\n\n"
122" file types:\n"
123" -c characteristics\n"
124" -i inventory\n"
125" -x currency exchange\n"
126" -p providers\n"
127" -s substitutions\n"
128" -b KiCad eeschema BOM\n"
129" -X symbols (BOM supplement)\n"
130" other options:\n"
131" -v ... increase verbosity level\n"
132" -dCHARS dump the specified database (h, c, s, ...)\n"
133" -N name for the next file, override the name in diagnostics\n"
134" -q var=value ... run substitutions with the specified inputs\n"
135" -Q var=value ... run substitutions and then do parametric search\n"
136" -R regex parse and print regular expression\n"
137    , name);
138    exit(1);
139}
140
141
142int main(int argc, char **argv)
143{
144    void (*process)(const char *name) = parse_hierarchy;
145    int query = 0;
146    int i;
147
148    dollar = unique("$");
149    subst_init();
150    subex_init();
151    for (i = 1; i != argc; i++) {
152        if (*argv[i] != '-') {
153            process(argv[i]);
154            continue;
155        }
156        if (!strcmp(argv[i], "-N")) {
157            i++;
158            file_name_override = argv[i];
159        } else if (!strcmp(argv[i], "-v")) {
160            verbose++;
161        } else if (!strncmp(argv[i], "-d", 2)) {
162            if (argv[i][2]) {
163                dump(argv[i]+2);
164            } else {
165                i++;
166                if (!argv[i])
167                    usage(*argv);
168                dump(argv[i]);
169            }
170        } else if (!strcmp(argv[i], "-c")) {
171            process = parse_characteristics;
172        } else if (!strcmp(argv[i], "-i")) {
173            process = parse_inventory;
174        } else if (!strcmp(argv[i], "-x")) {
175            process = parse_currencies;
176        } else if (!strcmp(argv[i], "-p")) {
177            process = parse_providers;
178        } else if (!strcmp(argv[i], "-s")) {
179            process = parse_substitutions;
180        } else if (!strcmp(argv[i], "-b")) {
181            process = parse_kicad_bom;
182        } else if (!strcmp(argv[i], "-X")) {
183            process = parse_symbols;
184        } else if (!strcmp(argv[i], "-q")) {
185            process = add_var;
186            query = 1;
187        } else if (!strcmp(argv[i], "-Q")) {
188            process = add_var;
189            query = 1;
190            select_parts = 1;
191        } else if (!strcmp(argv[i], "-R")) {
192            i++;
193            if (!argv[i])
194                usage(*argv);
195            parse_re(argv[i]);
196        } else
197            usage(*argv);
198    }
199    if (query)
200        do_substitutions();
201    return 0;
202}
203

Archive Download this file

Branches:
master



interactive