2012-05-20 17:18:18 +03:00
|
|
|
/*
|
|
|
|
* subst.c - Substitution rules
|
|
|
|
*
|
|
|
|
* 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 <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
2012-05-21 02:57:18 +03:00
|
|
|
#include "vstring.h"
|
2012-05-20 17:18:18 +03:00
|
|
|
#include "lang.h"
|
2012-05-21 05:16:51 +03:00
|
|
|
#include "relop.h"
|
2012-05-20 17:18:18 +03:00
|
|
|
#include "subst.h"
|
|
|
|
|
|
|
|
|
2012-06-03 08:04:27 +03:00
|
|
|
const char *fn;
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
/* ----- Rule set construction --------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static struct subst *alloc_subst(enum subst_type type)
|
|
|
|
{
|
|
|
|
struct subst *sub;
|
|
|
|
|
|
|
|
sub = alloc_type(struct subst);
|
|
|
|
sub->type = type;
|
|
|
|
sub->next = NULL;
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 03:55:54 +03:00
|
|
|
/*
|
|
|
|
* With M the SI multiplier prefixes and U the unit character, our regexp
|
|
|
|
* is
|
|
|
|
*
|
|
|
|
* (-?[0-9]+\.?[[0-9]*M?U?|-?[0-9]+[UM][0-9]*)
|
|
|
|
*
|
|
|
|
* The first part is for things like 10, 1.2k, 3.3V, -2mA, etc.
|
|
|
|
* The second part is for things like 1k20, 1R2, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void unit_expr(char **res, int *res_len, char unit)
|
|
|
|
{
|
|
|
|
append(res, res_len, "(-?[0-9]+\\.?[0-9]*[" MULT_CHARS "]?");
|
2012-06-03 15:50:21 +03:00
|
|
|
if (unit != '#')
|
|
|
|
append_char(res, res_len, unit);
|
2012-05-21 03:55:54 +03:00
|
|
|
append(res, res_len, "?|-?[0-9]+[");
|
2012-06-03 15:50:21 +03:00
|
|
|
if (unit != '#')
|
|
|
|
append_char(res, res_len, unit);
|
2012-05-21 03:55:54 +03:00
|
|
|
append(res, res_len, MULT_CHARS "][0-9]*)");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 19:51:57 +03:00
|
|
|
static char *prepare_re(const char *re, char *units)
|
2012-05-21 02:57:18 +03:00
|
|
|
{
|
|
|
|
char *res = NULL;
|
|
|
|
int res_len = 0;
|
2012-05-22 19:51:57 +03:00
|
|
|
int parens = 0;
|
2012-05-21 02:57:18 +03:00
|
|
|
|
2012-05-21 03:55:54 +03:00
|
|
|
memset(units, 0, 10);
|
2012-05-21 02:57:18 +03:00
|
|
|
append_char(&res, &res_len, '^');
|
2012-05-21 03:05:17 +03:00
|
|
|
while (*re) {
|
|
|
|
switch (*re) {
|
|
|
|
case '.':
|
|
|
|
append_n(&res, &res_len, "\\.", 2);
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
append_n(&res, &res_len, ".*", 2);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
append_char(&res, &res_len, '.');
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (!re[1])
|
|
|
|
yyerrorf("regexp ends with backslash");
|
|
|
|
append_n(&res, &res_len, re, 2);
|
|
|
|
re++;
|
|
|
|
break;
|
2012-05-21 03:55:54 +03:00
|
|
|
case '(':
|
2012-05-22 19:51:57 +03:00
|
|
|
parens++;
|
2012-05-22 18:07:41 +03:00
|
|
|
if (re[1] == '#' && re[2]) {
|
2012-06-03 15:50:21 +03:00
|
|
|
if ((!isalpha(re[2]) &&
|
|
|
|
re[2] != '%' && re[2] != '#') ||
|
2012-06-03 15:42:09 +03:00
|
|
|
re[3] != ')')
|
2012-05-22 18:07:41 +03:00
|
|
|
yyerrorf("invalid (#unit) syntax");
|
2012-05-22 19:51:57 +03:00
|
|
|
units[parens-1] = re[2];
|
2012-05-21 03:55:54 +03:00
|
|
|
unit_expr(&res, &res_len, re[2]);
|
|
|
|
re += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fall through */
|
2012-05-21 03:05:17 +03:00
|
|
|
default:
|
|
|
|
append_char(&res, &res_len, *re);
|
|
|
|
}
|
|
|
|
re++;
|
|
|
|
}
|
2012-05-21 02:57:18 +03:00
|
|
|
append(&res, &res_len, re);
|
|
|
|
append_char(&res, &res_len, '$');
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-03 16:10:47 +03:00
|
|
|
struct subst *subst_match(const char *src, const char *re, char **res)
|
2012-05-20 17:18:18 +03:00
|
|
|
{
|
|
|
|
char error[1000];
|
|
|
|
struct subst *sub;
|
|
|
|
char *tmp;
|
2012-05-21 06:20:07 +03:00
|
|
|
int err;
|
2012-05-20 17:18:18 +03:00
|
|
|
|
|
|
|
sub = alloc_subst(st_match);
|
|
|
|
sub->u.match.src = src;
|
2012-05-22 19:51:57 +03:00
|
|
|
tmp = prepare_re(re, sub->u.match.units);
|
2012-05-20 17:18:18 +03:00
|
|
|
err = regcomp(&sub->u.match.re, tmp, REG_EXTENDED);
|
2012-06-03 16:10:47 +03:00
|
|
|
if (res)
|
|
|
|
*res = tmp;
|
|
|
|
else
|
|
|
|
free(tmp);
|
2012-05-20 17:18:18 +03:00
|
|
|
if (err) {
|
|
|
|
regerror(err, &sub->u.match.re, error, sizeof(error));
|
|
|
|
yyerrorf("%s", error);
|
|
|
|
}
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void end_chunk(struct chunk ***last, const char *start, const char *s)
|
|
|
|
{
|
|
|
|
struct chunk *c;
|
|
|
|
|
|
|
|
if (s == start)
|
|
|
|
return;
|
|
|
|
|
|
|
|
c = alloc_type(struct chunk);
|
|
|
|
c->type = ct_string;
|
|
|
|
c->u.s = stralloc_n(start, s-start);;
|
|
|
|
c->next = NULL;
|
|
|
|
**last = c;
|
|
|
|
*last = &c->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char *parse_var(struct chunk *c, const char *s)
|
|
|
|
{
|
|
|
|
const char *t;
|
|
|
|
int braced;
|
|
|
|
|
|
|
|
if (!*s)
|
|
|
|
yyerror("trailing dollar sign");
|
|
|
|
|
|
|
|
braced = *s == '{';
|
|
|
|
if (braced)
|
|
|
|
s++;
|
|
|
|
|
|
|
|
t = s;
|
|
|
|
while (*t) {
|
|
|
|
if (braced && *t == '}')
|
|
|
|
break;
|
|
|
|
if (s == t && *t == '$') {
|
|
|
|
t++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!isalnum(*t))
|
|
|
|
break;
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
if (s == t)
|
|
|
|
yyerror("invalid variable name");
|
|
|
|
if (braced && !*t)
|
|
|
|
yyerror("unterminated variable name");
|
|
|
|
if (isdigit(*s)) {
|
|
|
|
if (t != s+1 || *s == '0')
|
|
|
|
yyerror("invalid variable name");
|
|
|
|
c->type = ct_sub;
|
|
|
|
c->u.sub = *s-'0';
|
|
|
|
} else if (isalnum(*s)) {
|
|
|
|
c->type = ct_var;
|
2012-05-23 22:47:34 +03:00
|
|
|
c->u.var = unique_n(s, t-s);
|
2012-05-20 17:18:18 +03:00
|
|
|
} else {
|
|
|
|
c->type = ct_sub;
|
|
|
|
c->u.sub = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (braced) {
|
2012-05-21 03:55:54 +03:00
|
|
|
if (*t != '}')
|
|
|
|
yyerror("invalid variable name");
|
2012-05-20 17:18:18 +03:00
|
|
|
t++;
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct chunk *parse_pattern(const char *s)
|
|
|
|
{
|
|
|
|
struct chunk *res = NULL, **last = &res;
|
|
|
|
struct chunk *c;
|
|
|
|
const char *start = s;
|
|
|
|
|
|
|
|
while (*s) {
|
|
|
|
if (*s == '\\') {
|
|
|
|
if (!s[1])
|
|
|
|
yyerror("trailing backslash");
|
|
|
|
end_chunk(&last, start, s);
|
|
|
|
start = s+1;
|
|
|
|
s += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s != '$') {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_chunk(&last, start, s);
|
|
|
|
c = alloc_type(struct chunk);
|
|
|
|
c->next = NULL;
|
|
|
|
*last = c;
|
|
|
|
last = &c->next;
|
|
|
|
start = s = parse_var(c, s+1);
|
|
|
|
}
|
|
|
|
end_chunk(&last, start, s);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 05:16:51 +03:00
|
|
|
struct subst *subst_assign(const char *dst, enum relop op, const char *pat)
|
2012-05-20 17:18:18 +03:00
|
|
|
{
|
|
|
|
struct subst *sub;
|
|
|
|
|
2012-06-03 08:08:32 +03:00
|
|
|
if (dst == fn)
|
|
|
|
yyerror("can't assign to pseudo-variable FN");
|
2012-05-20 17:18:18 +03:00
|
|
|
sub = alloc_subst(st_assign);
|
|
|
|
sub->u.assign.dst = dst;
|
2012-05-21 05:16:51 +03:00
|
|
|
sub->u.assign.op = op;
|
2012-05-20 17:18:18 +03:00
|
|
|
sub->u.assign.pat = parse_pattern(pat);
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-03 19:15:31 +03:00
|
|
|
struct subst *subst_print(const char *var)
|
|
|
|
{
|
|
|
|
struct subst *sub;
|
|
|
|
|
|
|
|
sub = alloc_subst(st_print);
|
|
|
|
sub->u.print = var;
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
struct subst *subst_end(void)
|
|
|
|
{
|
|
|
|
return alloc_subst(st_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 05:17:33 +03:00
|
|
|
struct subst *subst_ignore(void)
|
|
|
|
{
|
|
|
|
return alloc_subst(st_ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
struct subst *subst_break(const char *block)
|
|
|
|
{
|
|
|
|
struct subst *sub;
|
|
|
|
|
|
|
|
sub = alloc_subst(st_break);
|
|
|
|
sub->u.tmp = block;
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-22 21:33:22 +03:00
|
|
|
struct subst *subst_continue(const char *block)
|
2012-05-20 17:18:18 +03:00
|
|
|
{
|
|
|
|
struct subst *sub;
|
|
|
|
|
2012-05-22 21:33:22 +03:00
|
|
|
sub = alloc_subst(st_continue);
|
2012-05-20 17:18:18 +03:00
|
|
|
sub->u.tmp = block;
|
|
|
|
return sub;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Jump resolution --------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
struct parent {
|
|
|
|
const struct subst *sub;
|
|
|
|
const struct parent *parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const struct subst *resolve_jump(const char *name,
|
|
|
|
const struct parent *parent)
|
|
|
|
{
|
2012-05-22 21:47:02 +03:00
|
|
|
if (!name)
|
|
|
|
return parent->sub;
|
2012-05-20 17:18:18 +03:00
|
|
|
while (parent) {
|
|
|
|
assert(parent->sub->type == st_match);
|
2012-05-22 20:27:39 +03:00
|
|
|
if (name == parent->sub->u.match.src)
|
2012-05-20 17:18:18 +03:00
|
|
|
return parent->sub;
|
|
|
|
parent = parent->parent;
|
|
|
|
}
|
|
|
|
yyerrorf("cannot find \"%s\"", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-21 06:20:07 +03:00
|
|
|
static int find_var_use(const char *var, const struct subst *sub)
|
|
|
|
{
|
|
|
|
while (sub) {
|
|
|
|
switch (sub->type) {
|
|
|
|
case st_match:
|
2012-06-03 08:04:27 +03:00
|
|
|
if (sub->u.match.src == var && var != fn)
|
2012-05-21 06:20:07 +03:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case st_assign:
|
|
|
|
if (sub->u.assign.dst == var)
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sub = sub->prev;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void check_chunks(const struct chunk *c, const struct parent *parent,
|
|
|
|
const struct subst *prev)
|
|
|
|
{
|
|
|
|
int parens;
|
|
|
|
|
|
|
|
while (c) {
|
|
|
|
switch (c->type) {
|
|
|
|
case ct_sub:
|
|
|
|
if (!parent)
|
|
|
|
yyerrorf("$%c without match",
|
|
|
|
c->u.sub ? c->u.sub+'0' : '$');
|
2012-05-22 19:51:57 +03:00
|
|
|
parens = parent->sub->u.match.re.re_nsub;
|
2012-05-21 06:20:07 +03:00
|
|
|
if (c->u.sub > parens)
|
|
|
|
yyerrorf("$%d but only %d parenthes%s",
|
|
|
|
c->u.sub, parens,
|
|
|
|
parens == 1 ? "is" : "es");
|
|
|
|
break;
|
|
|
|
case ct_var:
|
|
|
|
if (!find_var_use(c->u.var, prev))
|
|
|
|
yyerrorf("$%s may be undefined", c->u.var);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
static void recurse_fin(struct subst *sub, const struct parent *parent)
|
|
|
|
{
|
|
|
|
struct parent next = {
|
|
|
|
.parent = parent,
|
|
|
|
};
|
2012-05-21 06:20:07 +03:00
|
|
|
const struct subst *prev;
|
2012-05-20 17:18:18 +03:00
|
|
|
|
2012-05-21 06:20:07 +03:00
|
|
|
prev = parent ? parent->sub : NULL;
|
2012-05-20 17:18:18 +03:00
|
|
|
while (sub) {
|
2012-05-21 06:20:07 +03:00
|
|
|
sub->prev = prev;
|
2012-05-20 17:18:18 +03:00
|
|
|
switch (sub->type) {
|
|
|
|
case st_match:
|
2012-05-22 19:01:39 +03:00
|
|
|
if (!parent && sub->u.match.src == dollar)
|
|
|
|
yyerror("$ without match");
|
2012-05-21 06:20:07 +03:00
|
|
|
next.sub = sub;
|
2012-05-20 17:18:18 +03:00
|
|
|
recurse_fin(sub->u.match.block, &next);
|
2012-05-21 06:20:07 +03:00
|
|
|
break;
|
2012-05-20 17:18:18 +03:00
|
|
|
case st_assign:
|
2012-05-22 19:01:39 +03:00
|
|
|
if (!parent && sub->u.assign.dst == dollar)
|
|
|
|
yyerror("$ without match");
|
2012-05-21 06:20:07 +03:00
|
|
|
check_chunks(sub->u.assign.pat, parent, prev);
|
|
|
|
break;
|
2012-06-03 19:15:31 +03:00
|
|
|
case st_print:
|
|
|
|
break;
|
2012-05-20 17:18:18 +03:00
|
|
|
case st_end:
|
|
|
|
break;
|
2012-05-22 05:17:33 +03:00
|
|
|
case st_ignore:
|
|
|
|
break;
|
2012-05-20 17:18:18 +03:00
|
|
|
case st_break:
|
2012-05-22 20:27:39 +03:00
|
|
|
/* fall through */
|
2012-05-22 21:33:22 +03:00
|
|
|
case st_continue:
|
2012-06-04 02:54:08 +03:00
|
|
|
if (!parent)
|
|
|
|
yyerror("jump without block");
|
2012-05-20 17:18:18 +03:00
|
|
|
sub->u.jump = resolve_jump(sub->u.tmp, parent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2012-05-21 06:20:07 +03:00
|
|
|
prev = sub;
|
2012-05-20 17:18:18 +03:00
|
|
|
sub = sub->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void subst_finalize(struct subst *sub)
|
|
|
|
{
|
|
|
|
recurse_fin(sub, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Dumping ----------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
#define INDENT 4
|
|
|
|
|
|
|
|
|
|
|
|
static void dump_chunks(FILE *file, const struct chunk *c)
|
|
|
|
{
|
|
|
|
while (c) {
|
|
|
|
switch (c->type) {
|
|
|
|
case ct_string:
|
|
|
|
fprintf(file, "%s", c->u.s);
|
|
|
|
break;
|
|
|
|
case ct_var:
|
2012-06-03 05:09:47 +03:00
|
|
|
fprintf(file, "${%s}", c->u.var);
|
2012-05-20 17:18:18 +03:00
|
|
|
break;
|
|
|
|
case ct_sub:
|
|
|
|
if (c->u.sub)
|
2012-05-21 03:55:54 +03:00
|
|
|
fprintf(file, "$%d", c->u.sub);
|
2012-05-20 17:18:18 +03:00
|
|
|
else
|
2012-05-21 03:55:54 +03:00
|
|
|
fprintf(file, "$$");
|
2012-05-20 17:18:18 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
c = c->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void recurse_dump(FILE *file, const struct subst *sub, int level)
|
|
|
|
{
|
|
|
|
while (sub) {
|
|
|
|
fprintf(file, "%*s", INDENT*level, "");
|
|
|
|
switch (sub->type) {
|
|
|
|
case st_match:
|
|
|
|
fprintf(file, "%s=RE {\n", sub->u.match.src);
|
|
|
|
recurse_dump(file, sub->u.match.block, level+1);
|
|
|
|
fprintf(file, "%*s}\n", INDENT*level, "");
|
|
|
|
break;
|
|
|
|
case st_assign:
|
2012-05-21 05:16:51 +03:00
|
|
|
fprintf(file, "%s", sub->u.assign.dst);
|
|
|
|
dump_relop(file, sub->u.assign.op);
|
2012-05-20 17:18:18 +03:00
|
|
|
dump_chunks(file, sub->u.assign.pat);
|
|
|
|
fprintf(file, "\n");
|
|
|
|
break;
|
2012-06-03 19:15:31 +03:00
|
|
|
case st_print:
|
|
|
|
fprintf(file, "print %s\n", sub->u.print);
|
|
|
|
break;
|
2012-05-20 17:18:18 +03:00
|
|
|
case st_end:
|
|
|
|
fprintf(file, "end\n");
|
|
|
|
break;
|
2012-05-22 05:17:33 +03:00
|
|
|
case st_ignore:
|
|
|
|
fprintf(file, "ignore\n");
|
|
|
|
break;
|
2012-05-20 17:18:18 +03:00
|
|
|
case st_break:
|
|
|
|
fprintf(file, "break %s\n", sub->u.jump->u.match.src);
|
|
|
|
break;
|
2012-05-22 21:33:22 +03:00
|
|
|
case st_continue:
|
|
|
|
fprintf(file, "continue %s\n",
|
|
|
|
sub->u.jump->u.match.src);
|
2012-05-20 17:18:18 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
sub = sub->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void subst_dump(FILE *file, const struct subst *sub)
|
|
|
|
{
|
|
|
|
recurse_dump(file, sub, 0);
|
|
|
|
}
|
2012-06-03 08:04:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
void subst_init(void)
|
|
|
|
{
|
|
|
|
fn = unique("FN");
|
|
|
|
}
|