2012-05-20 17:18:18 +03:00
|
|
|
/*
|
|
|
|
* subst.h - 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef SUBST_H
|
|
|
|
#define SUBST_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <regex.h>
|
|
|
|
|
2012-05-21 05:16:51 +03:00
|
|
|
#include "relop.h"
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
|
2012-05-22 19:46:44 +03:00
|
|
|
#define NMATCH 10 /* $0 (not used), $1...$9 */
|
|
|
|
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
enum chunk_type {
|
|
|
|
ct_string,
|
|
|
|
ct_var,
|
|
|
|
ct_sub,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct chunk {
|
|
|
|
enum chunk_type type;
|
|
|
|
union {
|
|
|
|
const char *s;
|
|
|
|
const char *var;
|
|
|
|
int sub; /* 0 if $$ */
|
|
|
|
} u;
|
|
|
|
struct chunk *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum subst_type {
|
2012-05-22 05:17:33 +03:00
|
|
|
st_match, /* try to match a variable */
|
|
|
|
st_assign, /* assign to a variable */
|
|
|
|
st_end, /* end the substitutions, accepting the part */
|
|
|
|
st_ignore, /* ignore the part */
|
|
|
|
st_break, /* jump to an outer block */
|
2012-05-22 21:33:22 +03:00
|
|
|
st_continue, /* repeat an outer block */
|
2012-06-03 19:15:31 +03:00
|
|
|
st_print, /* print a variable, for debugging */
|
2012-05-20 17:18:18 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
struct subst {
|
|
|
|
enum subst_type type;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
const char *src;
|
|
|
|
regex_t re;
|
|
|
|
struct subst *block;
|
2012-05-22 19:46:44 +03:00
|
|
|
char units[NMATCH-1];
|
2012-05-20 17:18:18 +03:00
|
|
|
} match;
|
|
|
|
struct {
|
|
|
|
const char *dst;
|
2012-05-21 05:16:51 +03:00
|
|
|
enum relop op;
|
2012-05-20 17:18:18 +03:00
|
|
|
struct chunk *pat;
|
|
|
|
} assign;
|
2012-06-03 19:15:31 +03:00
|
|
|
const char *print;
|
2012-05-20 17:18:18 +03:00
|
|
|
const struct subst *jump;
|
|
|
|
const char *tmp; /* jump target name; for subst_finalize */
|
|
|
|
} u;
|
2012-05-21 06:20:07 +03:00
|
|
|
const struct subst *prev; /* for tracking availability of variables */
|
2012-05-20 17:18:18 +03:00
|
|
|
struct subst *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-05-21 03:55:54 +03:00
|
|
|
#define MULT_CHARS "GMkmunpf"
|
|
|
|
|
|
|
|
|
2012-06-03 08:04:27 +03:00
|
|
|
extern const char *fn;
|
|
|
|
|
|
|
|
|
2012-06-03 16:10:47 +03:00
|
|
|
struct subst *subst_match(const char *src, const char *re, char **res);
|
2012-05-21 05:16:51 +03:00
|
|
|
struct subst *subst_assign(const char *dst, enum relop op, const char *pat);
|
2012-06-03 19:15:31 +03:00
|
|
|
struct subst *subst_print(const char *var);
|
2012-05-20 17:18:18 +03:00
|
|
|
struct subst *subst_end(void);
|
2012-05-22 05:17:33 +03:00
|
|
|
struct subst *subst_ignore(void);
|
2012-05-20 17:18:18 +03:00
|
|
|
struct subst *subst_break(const char *block);
|
2012-05-22 21:33:22 +03:00
|
|
|
struct subst *subst_continue(const char *block);
|
2012-05-20 17:18:18 +03:00
|
|
|
|
|
|
|
void subst_finalize(struct subst *sub);
|
|
|
|
|
|
|
|
void subst_dump(FILE *file, const struct subst *sub);
|
|
|
|
|
2012-06-03 08:04:27 +03:00
|
|
|
void subst_init(void);
|
|
|
|
|
2012-05-20 17:18:18 +03:00
|
|
|
#endif /* !SUBST_H */
|