Root/
| 1 | /* |
| 2 | * subst.h - Substitution rules |
| 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 | #ifndef SUBST_H |
| 14 | #define SUBST_H |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <regex.h> |
| 19 | |
| 20 | #include "relop.h" |
| 21 | |
| 22 | |
| 23 | #define NMATCH 10 /* $0 (not used), $1...$9 */ |
| 24 | |
| 25 | |
| 26 | enum chunk_type { |
| 27 | ct_string, |
| 28 | ct_var, |
| 29 | ct_sub, |
| 30 | }; |
| 31 | |
| 32 | struct chunk { |
| 33 | enum chunk_type type; |
| 34 | union { |
| 35 | const char *s; |
| 36 | const char *var; |
| 37 | int sub; /* 0 if $$ */ |
| 38 | } u; |
| 39 | struct chunk *next; |
| 40 | }; |
| 41 | |
| 42 | enum subst_type { |
| 43 | st_match, /* try to match a variable */ |
| 44 | st_assign, /* assign to a variable */ |
| 45 | st_end, /* end the substitutions, accepting the part */ |
| 46 | st_ignore, /* ignore the part */ |
| 47 | st_break, /* jump to an outer block */ |
| 48 | st_continue, /* repeat an outer block */ |
| 49 | st_print, /* print a variable, for debugging */ |
| 50 | }; |
| 51 | |
| 52 | struct subst { |
| 53 | enum subst_type type; |
| 54 | union { |
| 55 | struct { |
| 56 | const char *src; |
| 57 | regex_t re; |
| 58 | struct subst *block; |
| 59 | char units[NMATCH-1]; |
| 60 | } match; |
| 61 | struct { |
| 62 | const char *dst; |
| 63 | enum relop op; |
| 64 | struct chunk *pat; |
| 65 | } assign; |
| 66 | const char *print; |
| 67 | const struct subst *jump; |
| 68 | const char *tmp; /* jump target name; for subst_finalize */ |
| 69 | } u; |
| 70 | const struct subst *prev; /* for tracking availability of variables */ |
| 71 | struct subst *next; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | #define MULT_CHARS "GMkmunpf" |
| 76 | |
| 77 | |
| 78 | extern const char *fn; |
| 79 | |
| 80 | |
| 81 | struct subst *subst_match(const char *src, const char *re, char **res); |
| 82 | struct subst *subst_assign(const char *dst, enum relop op, const char *pat); |
| 83 | struct subst *subst_print(const char *var); |
| 84 | struct subst *subst_end(void); |
| 85 | struct subst *subst_ignore(void); |
| 86 | struct subst *subst_break(const char *block); |
| 87 | struct subst *subst_continue(const char *block); |
| 88 | |
| 89 | void subst_finalize(struct subst *sub); |
| 90 | |
| 91 | void subst_dump(FILE *file, const struct subst *sub); |
| 92 | |
| 93 | void subst_init(void); |
| 94 | |
| 95 | #endif /* !SUBST_H */ |
| 96 |
Branches:
master
