Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <errno.h> |
| 5 | #include <assert.h> |
| 6 | |
| 7 | #include "asn1parser.h" |
| 8 | |
| 9 | /* |
| 10 | * Construct a new empty types collection. |
| 11 | */ |
| 12 | asn1p_expr_t * |
| 13 | asn1p_expr_new(int _lineno) { |
| 14 | asn1p_expr_t *expr; |
| 15 | |
| 16 | expr = calloc(1, sizeof *expr); |
| 17 | if(expr) { |
| 18 | TQ_INIT(&(expr->members)); |
| 19 | expr->_lineno = _lineno; |
| 20 | } |
| 21 | |
| 22 | return expr; |
| 23 | } |
| 24 | |
| 25 | asn1p_expr_t * |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 26 | asn1p_expr_clone(asn1p_expr_t *expr, int skip_extensions) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | asn1p_expr_t *clone; |
| 28 | asn1p_expr_t *tcmemb; /* Child of tc */ |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 29 | int hit_ext = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | |
| 31 | clone = asn1p_expr_new(expr->_lineno); |
| 32 | if(clone == NULL) return NULL; |
| 33 | |
| 34 | #define CLCOPY(field) do { clone->field = expr->field; } while(0) |
| 35 | #define CLCLONE(field, func) do { if(expr->field) { \ |
| 36 | clone->field = func(expr->field); \ |
| 37 | if(clone->field == NULL) { \ |
| 38 | asn1p_expr_free(clone); \ |
| 39 | return NULL; \ |
| 40 | } \ |
| 41 | } } while(0) |
| 42 | |
| 43 | /* |
| 44 | * Copy simple fields. |
| 45 | */ |
| 46 | CLCOPY(meta_type); |
| 47 | CLCOPY(expr_type); |
| 48 | CLCOPY(tag); |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 49 | CLCOPY(marker); /* OPTIONAL/DEFAULT */ |
| 50 | CLCOPY(module); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | CLCOPY(_mark); |
| 52 | |
| 53 | clone->data = 0; /* Do not clone this */ |
| 54 | clone->data_free = 0; /* Do not clone this */ |
| 55 | |
| 56 | /* |
| 57 | * Clone complex fields. |
| 58 | */ |
| 59 | CLCLONE(Identifier, strdup); |
| 60 | CLCLONE(reference, asn1p_ref_clone); |
| 61 | CLCLONE(constraints, asn1p_constraint_clone); |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 62 | CLCLONE(combined_constraints, asn1p_constraint_clone); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 63 | CLCLONE(params, asn1p_paramlist_clone); |
| 64 | CLCLONE(value, asn1p_value_clone); |
| 65 | CLCLONE(with_syntax, asn1p_wsyntx_clone); |
| 66 | |
| 67 | /* |
| 68 | * Copy all the children of this expr. |
| 69 | */ |
| 70 | TQ_FOR(tcmemb, &(expr->members), next) { |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 71 | asn1p_expr_t *cmemb; |
| 72 | |
| 73 | if(skip_extensions |
| 74 | && tcmemb->expr_type == A1TC_EXTENSIBLE) { |
| 75 | hit_ext++; /* Even if hit_ext wraps around, we're OK. */ |
| 76 | continue; |
| 77 | } |
| 78 | if(hit_ext == 1) continue; /* Skip between ...'s */ |
| 79 | |
| 80 | cmemb = asn1p_expr_clone(tcmemb, skip_extensions); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | if(cmemb == NULL) { |
| 82 | asn1p_expr_free(clone); |
| 83 | return NULL; |
| 84 | } |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 85 | asn1p_expr_add(clone, cmemb); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return clone; |
| 89 | } |
| 90 | |
| 91 | /* |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 92 | * Add expression as a member of another. |
| 93 | */ |
| 94 | void |
| 95 | asn1p_expr_add(asn1p_expr_t *to, asn1p_expr_t *what) { |
| 96 | TQ_ADD(&(to->members), what, next); |
| 97 | what->parent_expr = to; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | * Destruct the types collection structure. |
| 103 | */ |
| 104 | void |
| 105 | asn1p_expr_free(asn1p_expr_t *expr) { |
| 106 | if(expr) { |
| 107 | asn1p_expr_t *tm; |
| 108 | |
Lev Walkin | 1004aa9 | 2004-09-08 00:28:11 +0000 | [diff] [blame] | 109 | /* Remove all children */ |
| 110 | while((tm = TQ_REMOVE(&(expr->members), next))) { |
| 111 | if(tm->parent_expr != expr) |
| 112 | printf("<%s:%p !-> %s:%p>\n", |
| 113 | tm->Identifier, tm->parent_expr, |
| 114 | expr->Identifier, expr); |
| 115 | assert(tm->parent_expr == expr); |
| 116 | asn1p_expr_free(tm); |
| 117 | } |
| 118 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | if(expr->Identifier) |
| 120 | free(expr->Identifier); |
| 121 | if(expr->reference) |
| 122 | asn1p_ref_free(expr->reference); |
| 123 | if(expr->constraints) |
| 124 | asn1p_constraint_free(expr->constraints); |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 125 | if(expr->combined_constraints) |
| 126 | asn1p_constraint_free(expr->combined_constraints); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | if(expr->params) |
| 128 | asn1p_paramlist_free(expr->params); |
| 129 | if(expr->value) |
| 130 | asn1p_value_free(expr->value); |
| 131 | if(expr->with_syntax) |
| 132 | asn1p_wsyntx_free(expr->with_syntax); |
| 133 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | if(expr->data && expr->data_free) |
| 135 | expr->data_free(expr->data); |
| 136 | |
| 137 | memset(expr, 0, sizeof(*expr)); |
| 138 | free(expr); |
| 139 | } |
| 140 | } |
| 141 | |