blob: 755196f364b206e6d65345f56fff222f89274c87 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#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 */
12asn1p_expr_t *
13asn1p_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
25asn1p_expr_t *
Lev Walkin070a52d2004-08-22 03:19:54 +000026asn1p_expr_clone(asn1p_expr_t *expr, int skip_extensions) {
Lev Walkinf15320b2004-06-03 03:38:44 +000027 asn1p_expr_t *clone;
28 asn1p_expr_t *tcmemb; /* Child of tc */
Lev Walkin070a52d2004-08-22 03:19:54 +000029 int hit_ext = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000030
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 Walkin070a52d2004-08-22 03:19:54 +000049 CLCOPY(marker); /* OPTIONAL/DEFAULT */
50 CLCOPY(module);
Lev Walkinf15320b2004-06-03 03:38:44 +000051 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 Walkinf59d0752004-08-18 04:59:12 +000062 CLCLONE(combined_constraints, asn1p_constraint_clone);
Lev Walkinf15320b2004-06-03 03:38:44 +000063 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 Walkin070a52d2004-08-22 03:19:54 +000071 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 Walkinf15320b2004-06-03 03:38:44 +000081 if(cmemb == NULL) {
82 asn1p_expr_free(clone);
83 return NULL;
84 }
Lev Walkin1004aa92004-09-08 00:28:11 +000085 asn1p_expr_add(clone, cmemb);
Lev Walkinf15320b2004-06-03 03:38:44 +000086 }
87
88 return clone;
89}
90
91/*
Lev Walkin1004aa92004-09-08 00:28:11 +000092 * Add expression as a member of another.
93 */
94void
95asn1p_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 Walkinf15320b2004-06-03 03:38:44 +0000102 * Destruct the types collection structure.
103 */
104void
105asn1p_expr_free(asn1p_expr_t *expr) {
106 if(expr) {
107 asn1p_expr_t *tm;
108
Lev Walkin1004aa92004-09-08 00:28:11 +0000109 /* 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 Walkinf15320b2004-06-03 03:38:44 +0000119 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 Walkinf59d0752004-08-18 04:59:12 +0000125 if(expr->combined_constraints)
126 asn1p_constraint_free(expr->combined_constraints);
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 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 Walkinf15320b2004-06-03 03:38:44 +0000134 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