aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2015-09-10 09:31:18 +0200
committerNeels Hofmeyr <neels@hofmeyr.de>2016-07-06 17:08:57 +0200
commit5e2364f614b114a5373bf0a445e31b682c5ff3bb (patch)
treefddb9dc5d63611955eaa252b364c798dac922071
parent840ee6d2c7f204f3da90485591f6402611040a39 (diff)
prefixing of generated types using a ASN1C_PREFIX environment variable
When generating code for multiple ASN.1 syntaxes that have clashing names, we need to add a prefix in order to prevent clashes in the global C symbol namespace. Using the ASN1C_PREFIX environment variable and this patch serves as a work-around to that. All non-basic type names as well as references to that type and source code + header file names will be pre-fixed accordingly.
-rw-r--r--libasn1compiler/asn1c_misc.c37
-rw-r--r--libasn1compiler/asn1c_misc.h1
-rw-r--r--libasn1fix/asn1fix.c9
3 files changed, 39 insertions, 8 deletions
diff --git a/libasn1compiler/asn1c_misc.c b/libasn1compiler/asn1c_misc.c
index 9fb2eff1..498b120a 100644
--- a/libasn1compiler/asn1c_misc.c
+++ b/libasn1compiler/asn1c_misc.c
@@ -50,6 +50,10 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
char *second = 0;
ssize_t size;
char *p;
+ char *prefix = NULL;
+
+ if (flags & AMI_USE_PREFIX)
+ prefix = getenv("ASN1C_PREFIX");
if(expr) {
/*
@@ -74,6 +78,8 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
va_end(ap);
if(size == -1) return NULL;
+ if(prefix)
+ size += 1 + strlen(prefix);
/*
* Make sure we have this amount of storage.
*/
@@ -93,8 +99,12 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
*/
va_start(ap, expr);
p = storage;
+ if(prefix) {
+ strcpy(storage, prefix);
+ p += strlen(prefix);
+ }
nextstr = "";
- for(p = storage, str = 0; str || nextstr; str = nextstr) {
+ for(str = 0; str || nextstr; str = nextstr) {
int subst_made = 0;
nextstr = second ? second : va_arg(ap, char *);
@@ -262,23 +272,34 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
switch(_format) {
case TNF_UNMODIFIED:
- return asn1c_make_identifier(AMI_MASK_ONLY_SPACES,
+ return asn1c_make_identifier(stdname ? AMI_MASK_ONLY_SPACES :
+ AMI_MASK_ONLY_SPACES | AMI_USE_PREFIX,
0, exprid ? exprid->Identifier : typename, 0);
- case TNF_INCLUDE:
+ case TNF_INCLUDE: {
+ /* as we have the quote marks " or < preceding the type
+ * name, we cannot simply have asn1c_make_identifier
+ * generate the prefix. Then we would end up with
+ * strings like PREFIX_<foo.h>" */
+ char *prefix = getenv("ASN1C_PREFIX");
+ if (!prefix)
+ prefix = "";
return asn1c_make_identifier(
AMI_MASK_ONLY_SPACES | AMI_NODELIMITER,
0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
- ? "\"" : "<"),
+ ? "\"" : "<"), stdname ? "" : prefix,
exprid ? exprid->Identifier : typename,
((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
? ".h\"" : ".h>"), 0);
+ }
case TNF_SAFE:
- return asn1c_make_identifier(0, exprid, typename, 0);
+ return asn1c_make_identifier(stdname ? 0 : AMI_USE_PREFIX,
+ exprid, typename, 0);
case TNF_CTYPE: /* C type */
- return asn1c_make_identifier(0, exprid,
- exprid?"t":typename, exprid?0:"t", 0);
+ return asn1c_make_identifier(stdname ? 0 : AMI_USE_PREFIX,
+ exprid, exprid?"t":typename, exprid?0:"t", 0);
case TNF_RSAFE: /* Recursion-safe type */
- return asn1c_make_identifier(AMI_CHECK_RESERVED, 0,
+ return asn1c_make_identifier(stdname ? AMI_CHECK_RESERVED :
+ AMI_CHECK_RESERVED | AMI_USE_PREFIX, 0,
"struct", " ", typename, 0);
}
diff --git a/libasn1compiler/asn1c_misc.h b/libasn1compiler/asn1c_misc.h
index ee88cb15..4489ae13 100644
--- a/libasn1compiler/asn1c_misc.h
+++ b/libasn1compiler/asn1c_misc.h
@@ -10,6 +10,7 @@ enum ami_flags_e {
AMI_MASK_ONLY_SPACES = 1, /* Mask only spaces, everything else's safe */
AMI_CHECK_RESERVED = 2, /* Check against reserved keywords */
AMI_NODELIMITER = 4, /* Do not put delimiter, just concatenate */
+ AMI_USE_PREFIX = 8, /* Use Prefix when generating identifier */
};
char *asn1c_make_identifier(enum ami_flags_e, asn1p_expr_t *expr, ...);
diff --git a/libasn1fix/asn1fix.c b/libasn1fix/asn1fix.c
index 8d2317f4..6165873c 100644
--- a/libasn1fix/asn1fix.c
+++ b/libasn1fix/asn1fix.c
@@ -245,10 +245,19 @@ asn1f_fix_module__phase_2(arg_t *arg) {
asn1p_expr_t *expr;
int rvalue = 0;
int ret;
+ char *prefix = getenv("ASN1C_PREFIX");
TQ_FOR(expr, &(arg->mod->members), next) {
arg->expr = expr;
+ if (prefix) {
+ char *tmp = malloc(strlen(prefix)+strlen(expr->Identifier)+1);
+ sprintf(tmp, "%s%s", prefix, expr->Identifier);
+ expr->Identifier = tmp;
+ /* FIXME: what about old memory ? */
+#warning "Fix this memory leak"
+ }
+
/*
* Dereference DEFAULT values.
*/