2012-05-20 20:16:16 +03:00
|
|
|
/*
|
|
|
|
* subex.c - Substitution (execution)
|
|
|
|
*
|
|
|
|
* 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>
|
2012-05-21 03:55:54 +03:00
|
|
|
#include <ctype.h>
|
2012-05-20 20:16:16 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
2012-05-21 03:55:54 +03:00
|
|
|
#include <assert.h>
|
2012-05-20 20:16:16 +03:00
|
|
|
|
|
|
|
#include "util.h"
|
2012-05-21 02:53:34 +03:00
|
|
|
#include "vstring.h"
|
2012-05-22 19:01:39 +03:00
|
|
|
#include "lang.h"
|
2012-05-21 05:16:51 +03:00
|
|
|
#include "relop.h"
|
2012-05-20 20:16:16 +03:00
|
|
|
#include "subst.h"
|
|
|
|
#include "subex.h"
|
|
|
|
|
|
|
|
|
2012-05-22 19:46:44 +03:00
|
|
|
#define FIELDS 10 /* fields in KiCad schematics */
|
2012-05-20 20:16:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
static const char *fn = NULL, *f[FIELDS];
|
|
|
|
|
2012-05-22 05:17:33 +03:00
|
|
|
/* Jump targets that can never be reached. */
|
2012-05-20 20:16:16 +03:00
|
|
|
static struct subst jump_end;
|
2012-05-22 05:17:33 +03:00
|
|
|
static struct subst jump_ignore;
|
2012-05-20 20:16:16 +03:00
|
|
|
|
2012-05-21 03:55:54 +03:00
|
|
|
|
|
|
|
static char *canonicalize(const char *s, char unit)
|
|
|
|
{
|
|
|
|
char *res;
|
|
|
|
int res_len = 0;
|
|
|
|
int seen_dot = 0;
|
|
|
|
int seen_unit = 0;
|
|
|
|
char mult = 0;
|
|
|
|
|
|
|
|
if (!unit)
|
|
|
|
return stralloc(s);
|
|
|
|
res = stralloc("");
|
|
|
|
while (*s) {
|
|
|
|
if (*s == unit) {
|
|
|
|
assert(!seen_unit);
|
|
|
|
if (!s[1])
|
|
|
|
break;
|
|
|
|
if (!seen_dot)
|
|
|
|
append_char(&res, &res_len, '.');
|
|
|
|
seen_unit = seen_dot = 1;
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s == '.') {
|
|
|
|
assert(!seen_dot);
|
|
|
|
append_char(&res, &res_len, '.');
|
|
|
|
seen_dot = 1;
|
|
|
|
} else if (*s == '-' || isdigit(*s)) {
|
|
|
|
append_char(&res, &res_len, *s);
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
} else if (strchr(MULT_CHARS, *s)) {
|
|
|
|
assert(!seen_unit);
|
|
|
|
assert(!mult);
|
|
|
|
mult = *s;
|
|
|
|
if (!seen_dot)
|
|
|
|
append_char(&res, &res_len, '.');
|
|
|
|
seen_dot = 1;
|
|
|
|
} else {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (res_len && res[res_len-1] == '.')
|
|
|
|
res_len--;
|
|
|
|
if (mult)
|
|
|
|
append_char(&res, &res_len, mult);
|
|
|
|
append_char(&res, &res_len, unit);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2012-05-20 20:16:16 +03:00
|
|
|
|
|
|
|
static char *compose(const struct chunk *c,
|
2012-05-22 01:19:04 +03:00
|
|
|
const struct param *in, const struct param *out,
|
2012-05-21 03:55:54 +03:00
|
|
|
const char *s, const regmatch_t *match, const char *units)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
|
|
|
char *res = stralloc("");
|
2012-05-21 03:55:54 +03:00
|
|
|
int res_len = 0, len;
|
2012-05-20 20:16:16 +03:00
|
|
|
const char *val;
|
2012-05-21 03:55:54 +03:00
|
|
|
char *tmp, *tmp2;
|
2012-05-20 20:16:16 +03:00
|
|
|
int n;
|
|
|
|
|
|
|
|
while (c) {
|
|
|
|
switch (c->type) {
|
|
|
|
case ct_string:
|
|
|
|
append(&res, &res_len, c->u.s);
|
|
|
|
break;
|
|
|
|
case ct_var:
|
|
|
|
val = var_lookup(in, c->u.var);
|
|
|
|
if (!val)
|
|
|
|
val = var_lookup(out, c->u.var);
|
2012-05-21 06:20:07 +03:00
|
|
|
assert(val);
|
2012-05-20 20:16:16 +03:00
|
|
|
append(&res, &res_len, val);
|
|
|
|
break;
|
|
|
|
case ct_sub:
|
|
|
|
n = c->u.sub;
|
2012-05-21 06:20:07 +03:00
|
|
|
assert(s);
|
2012-05-20 20:16:16 +03:00
|
|
|
if (!n) {
|
|
|
|
append(&res, &res_len, s);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-22 19:46:44 +03:00
|
|
|
if (match[n].rm_so == -1)
|
2012-05-21 06:20:07 +03:00
|
|
|
break;
|
2012-05-22 19:46:44 +03:00
|
|
|
len = match[n].rm_eo-match[n].rm_so;
|
2012-05-20 20:16:16 +03:00
|
|
|
tmp = alloc_size(len);
|
2012-05-22 19:46:44 +03:00
|
|
|
memcpy(tmp, s+match[n].rm_so, len);
|
2012-05-20 20:16:16 +03:00
|
|
|
tmp[len] = 0;
|
2012-05-21 03:55:54 +03:00
|
|
|
tmp2 = canonicalize(tmp, units ? units[n-1] : 0);
|
2012-05-20 20:16:16 +03:00
|
|
|
append(&res, &res_len, tmp2);
|
2012-05-21 03:55:54 +03:00
|
|
|
free(tmp);
|
|
|
|
free(tmp2);
|
2012-05-20 20:16:16 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 01:19:04 +03:00
|
|
|
static void do_assign(const char *name, struct param **out, enum relop op,
|
2012-05-21 05:16:51 +03:00
|
|
|
const char *val)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
|
|
|
while (*out) {
|
2012-05-22 01:19:04 +03:00
|
|
|
if ((*out)->u.name == name)
|
2012-05-20 20:16:16 +03:00
|
|
|
break;
|
|
|
|
out = &(*out)->next;
|
|
|
|
}
|
2012-05-22 01:19:04 +03:00
|
|
|
if (*out)
|
|
|
|
(*out)->value.u.s = val;
|
|
|
|
else
|
2012-05-21 05:16:51 +03:00
|
|
|
*out = make_var(name, op, val);
|
2012-05-20 20:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int do_match_1(const char *var, const regex_t *re,
|
2012-05-22 01:19:04 +03:00
|
|
|
const struct param *in, const struct param *out,
|
2012-05-20 20:16:16 +03:00
|
|
|
const char **val, regmatch_t *match)
|
|
|
|
{
|
|
|
|
*val = var_lookup(out, var);
|
|
|
|
if (!*val)
|
|
|
|
*val = var_lookup(in, var);
|
|
|
|
if (!*val)
|
|
|
|
return -1;
|
2012-05-22 19:46:44 +03:00
|
|
|
return regexec(re, *val, NMATCH, match, 0);
|
2012-05-20 20:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 19:01:39 +03:00
|
|
|
static int do_match(const char **var, const regex_t *re,
|
2012-05-22 01:19:04 +03:00
|
|
|
const struct param *in, const struct param *out,
|
2012-05-20 20:16:16 +03:00
|
|
|
const char **val, regmatch_t *match)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-05-22 19:01:39 +03:00
|
|
|
if (*var != fn)
|
|
|
|
return do_match_1(*var, re, in, out, val, match);
|
2012-05-20 20:16:16 +03:00
|
|
|
for (i = 0; i != FIELDS; i++)
|
2012-05-22 19:01:39 +03:00
|
|
|
if (!do_match_1(f[i], re, in, out, val, match)) {
|
|
|
|
*var = f[i];
|
2012-05-20 20:16:16 +03:00
|
|
|
return 0;
|
2012-05-22 19:01:39 +03:00
|
|
|
}
|
2012-05-20 20:16:16 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct subst *recurse_sub(const struct subst *sub,
|
2012-05-22 19:01:39 +03:00
|
|
|
const struct param *in, const char *matched_var, const char *matched_val,
|
|
|
|
const regmatch_t *match, const char *units, struct param **out)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
|
|
|
const struct subst *jump;
|
2012-05-22 19:46:44 +03:00
|
|
|
regmatch_t m_tmp[NMATCH];
|
2012-05-22 19:01:39 +03:00
|
|
|
const char *var, *val;
|
2012-05-20 20:16:16 +03:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
while (sub) {
|
|
|
|
switch (sub->type) {
|
|
|
|
case st_match:
|
2012-05-22 19:01:39 +03:00
|
|
|
var = sub->u.match.src;
|
|
|
|
if (var == dollar)
|
|
|
|
var = matched_var;
|
|
|
|
if (do_match(&var, &sub->u.match.re,
|
2012-05-20 20:16:16 +03:00
|
|
|
in, *out, &val, m_tmp))
|
|
|
|
break;
|
2012-05-22 19:01:39 +03:00
|
|
|
jump = recurse_sub(sub->u.match.block, in,
|
|
|
|
var, val, m_tmp, sub->u.match.units, out);
|
2012-05-20 20:16:16 +03:00
|
|
|
if (jump && jump != sub)
|
|
|
|
return jump;
|
|
|
|
break;
|
|
|
|
case st_assign:
|
2012-05-22 19:01:39 +03:00
|
|
|
tmp = compose(sub->u.assign.pat, in, *out, matched_val,
|
|
|
|
match, units);
|
|
|
|
var = sub->u.assign.dst;
|
|
|
|
if (var == dollar)
|
|
|
|
var = matched_var;
|
|
|
|
do_assign(var, out, sub->u.assign.op, tmp);
|
2012-05-20 20:16:16 +03:00
|
|
|
break;
|
|
|
|
case st_end:
|
|
|
|
return &jump_end;
|
2012-05-22 05:17:33 +03:00
|
|
|
case st_ignore:
|
|
|
|
return &jump_ignore;
|
2012-05-20 20:16:16 +03:00
|
|
|
case st_break:
|
|
|
|
sub = sub->u.jump->next;
|
|
|
|
continue;
|
|
|
|
case st_again:
|
|
|
|
sub = sub->u.jump->u.match.block;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
sub = sub->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 05:17:33 +03:00
|
|
|
int substitute(const struct subst *sub, const struct param *in,
|
|
|
|
struct param **out)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char tmp[4];
|
|
|
|
|
|
|
|
if (!fn) {
|
|
|
|
fn = unique("FN");
|
|
|
|
for (i = 0; i != FIELDS; i++) {
|
|
|
|
sprintf(tmp, "F%d", i);
|
|
|
|
f[i] = unique(tmp);
|
|
|
|
}
|
|
|
|
}
|
2012-05-22 05:17:33 +03:00
|
|
|
*out = NULL;
|
2012-05-22 19:01:39 +03:00
|
|
|
return recurse_sub(sub, in, NULL, NULL, NULL, NULL, out)
|
|
|
|
!= &jump_ignore;
|
2012-05-20 20:16:16 +03:00
|
|
|
}
|