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-21 05:16:51 +03:00
|
|
|
#include "relop.h"
|
2012-05-20 20:16:16 +03:00
|
|
|
#include "subst.h"
|
|
|
|
#include "subex.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define FIELDS 10
|
|
|
|
|
|
|
|
|
|
|
|
static const char *fn = NULL, *f[FIELDS];
|
|
|
|
|
|
|
|
/* Jump target that can never be reached. */
|
|
|
|
static struct subst jump_end;
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (match[n-1].rm_so == -1)
|
2012-05-21 06:20:07 +03:00
|
|
|
break;
|
2012-05-21 03:55:54 +03:00
|
|
|
len = match[n-1].rm_eo-match[n-1].rm_so;
|
2012-05-20 20:16:16 +03:00
|
|
|
tmp = alloc_size(len);
|
|
|
|
memcpy(tmp, s+match[n-1].rm_so, len);
|
|
|
|
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;
|
|
|
|
return regexec(re, *val, 10, match, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (var != fn)
|
|
|
|
return do_match_1(var, re, in, out, val, match);
|
|
|
|
for (i = 0; i != FIELDS; i++)
|
|
|
|
if (!do_match_1(f[i], re, in, out, val, match))
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct subst *recurse_sub(const struct subst *sub,
|
2012-05-22 01:19:04 +03:00
|
|
|
const struct param *in, const char *s, const regmatch_t *match,
|
|
|
|
const char *units, struct param **out)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
|
|
|
const struct subst *jump;
|
|
|
|
regmatch_t m_tmp[10];
|
|
|
|
const char *val;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
while (sub) {
|
|
|
|
switch (sub->type) {
|
|
|
|
case st_match:
|
|
|
|
if (do_match(sub->u.match.src, &sub->u.match.re,
|
|
|
|
in, *out, &val, m_tmp))
|
|
|
|
break;
|
|
|
|
jump = recurse_sub(sub->u.match.block, in, val, m_tmp,
|
2012-05-21 03:55:54 +03:00
|
|
|
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-21 03:55:54 +03:00
|
|
|
tmp = compose(sub->u.assign.pat, in, *out, s, match,
|
|
|
|
units);
|
2012-05-21 05:16:51 +03:00
|
|
|
do_assign(sub->u.assign.dst, out, sub->u.assign.op,
|
|
|
|
tmp);
|
2012-05-20 20:16:16 +03:00
|
|
|
break;
|
|
|
|
case st_end:
|
|
|
|
return &jump_end;
|
|
|
|
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 01:19:04 +03:00
|
|
|
struct param *substitute(const struct subst *sub, const struct param *in)
|
2012-05-20 20:16:16 +03:00
|
|
|
{
|
2012-05-22 01:19:04 +03:00
|
|
|
struct param *out = NULL;
|
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-21 03:55:54 +03:00
|
|
|
recurse_sub(sub, in, NULL, NULL, NULL, &out);
|
2012-05-20 20:16:16 +03:00
|
|
|
return out;
|
|
|
|
}
|