Commit b8a6be0a authored by Bi-Ruei, Chiu's avatar Bi-Ruei, Chiu

Prefix 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.

This is a re-implementation of this feature due to osmocom's
implementation version can not work on newest asn1c repository.
parent 1979a097
...@@ -94,7 +94,7 @@ static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode ...@@ -94,7 +94,7 @@ static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode
} while(0) } while(0)
/* MKID_safe() without checking for reserved keywords */ /* MKID_safe() without checking for reserved keywords */
#define MKID(expr) (asn1c_make_identifier(0, expr, 0)) #define MKID(expr) (asn1c_make_identifier(AMI_USE_PREFIX, expr, 0))
#define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0)) #define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0))
int int
...@@ -389,7 +389,7 @@ asn1c_lang_C_type_SEQUENCE(arg_t *arg) { ...@@ -389,7 +389,7 @@ asn1c_lang_C_type_SEQUENCE(arg_t *arg) {
c_name(arg).base_name); c_name(arg).base_name);
} else { } else {
OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
c_name(arg).short_name); arg->embed ? c_name(arg).as_member : c_name(arg).short_name);
if(!expr->_anonymous_type) OUT(";\n"); if(!expr->_anonymous_type) OUT(";\n");
} }
...@@ -632,7 +632,7 @@ asn1c_lang_C_type_SET(arg_t *arg) { ...@@ -632,7 +632,7 @@ asn1c_lang_C_type_SET(arg_t *arg) {
c_name(arg).base_name); c_name(arg).base_name);
} else { } else {
OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
c_name(arg).short_name); arg->embed ? c_name(arg).as_member : c_name(arg).short_name);
if(!expr->_anonymous_type) OUT(";\n"); if(!expr->_anonymous_type) OUT(";\n");
} }
...@@ -861,7 +861,7 @@ asn1c_lang_C_type_SEx_OF(arg_t *arg) { ...@@ -861,7 +861,7 @@ asn1c_lang_C_type_SEx_OF(arg_t *arg) {
c_name(arg).base_name); c_name(arg).base_name);
} else { } else {
OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
c_name(arg).short_name); arg->embed ? c_name(arg).as_member : c_name(arg).short_name);
if(!expr->_anonymous_type) OUT(";\n"); if(!expr->_anonymous_type) OUT(";\n");
} }
...@@ -1016,7 +1016,7 @@ asn1c_lang_C_type_CHOICE(arg_t *arg) { ...@@ -1016,7 +1016,7 @@ asn1c_lang_C_type_CHOICE(arg_t *arg) {
c_name(arg).base_name); c_name(arg).base_name);
} else { } else {
OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", OUT("} %s%s", (expr->marker.flags & EM_INDIRECT)?"*":"",
c_name(arg).short_name); arg->embed ? c_name(arg).as_member : c_name(arg).short_name);
} }
if(!expr->_anonymous_type) OUT(";\n"); if(!expr->_anonymous_type) OUT(";\n");
...@@ -1237,7 +1237,7 @@ asn1c_lang_C_type_REFERENCE_Value(arg_t *arg) { ...@@ -1237,7 +1237,7 @@ asn1c_lang_C_type_REFERENCE_Value(arg_t *arg) {
if((ref_type->expr_type == ASN_BASIC_INTEGER) || if((ref_type->expr_type == ASN_BASIC_INTEGER) ||
(ref_type->expr_type == ASN_BASIC_ENUMERATED)) { (ref_type->expr_type == ASN_BASIC_ENUMERATED)) {
OUT("#define %s_", MKID(ref_type)); OUT("#define %s_", MKID(ref_type));
OUT("%s\t", MKID(expr)); OUT("%s\t", c_name(arg).base_name);
OUT("((%s)", asn1c_type_name(arg, expr, TNF_CTYPE)); OUT("((%s)", asn1c_type_name(arg, expr, TNF_CTYPE));
OUT("%s)\n", asn1p_itoa(expr->value->value.v_integer)); OUT("%s)\n", asn1p_itoa(expr->value->value.v_integer));
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <asn1fix_export.h> #include <asn1fix_export.h>
#include <asn1print.h> #include <asn1print.h>
#define MKID(expr) asn1c_make_identifier(0, (expr), 0) #define MKID(expr) asn1c_make_identifier(AMI_USE_PREFIX, (expr), 0)
/* /*
* Given the table constraint or component relation constraint * Given the table constraint or component relation constraint
......
...@@ -38,6 +38,16 @@ reserved_keyword(const char *str) { ...@@ -38,6 +38,16 @@ reserved_keyword(const char *str) {
return 0; return 0;
} }
const char *
asn1c_prefix()
{
const char *prefix = getenv("ASN1C_PREFIX");
if(!prefix) prefix = "";
return prefix;
}
/* /*
* Construct identifier from multiple parts. * Construct identifier from multiple parts.
* Convert unsafe characters to underscores. * Convert unsafe characters to underscores.
...@@ -53,10 +63,13 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) { ...@@ -53,10 +63,13 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
char *first = 0; char *first = 0;
ssize_t size = 0; ssize_t size = 0;
char *p; char *p;
char *prefix = NULL; const char *prefix = NULL;
char *sptr[4], **psptr = &sptr[0]; char *sptr[4], **psptr = &sptr[0];
int sptr_cnt = 0; int sptr_cnt = 0;
if(flags & AMI_USE_PREFIX)
prefix = asn1c_prefix();
if(expr) { if(expr) {
/* /*
* Estimate the necessary storage size * Estimate the necessary storage size
...@@ -114,6 +127,7 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) { ...@@ -114,6 +127,7 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
if(prefix) { if(prefix) {
strcpy(storage, prefix); strcpy(storage, prefix);
p += strlen(prefix); p += strlen(prefix);
nodelimiter = 1;
} }
nextstr = ""; nextstr = "";
for(str = 0; str || nextstr; str = nextstr) { for(str = 0; str || nextstr; str = nextstr) {
...@@ -126,6 +140,11 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) { ...@@ -126,6 +140,11 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
if (!first) continue; if (!first) continue;
} }
if(str[0] == '\0') {
nodelimiter = 1; /* No delimiter */
continue;
}
if(str[0] == ' ' && str[1] == '\0') { if(str[0] == ' ' && str[1] == '\0') {
*p++ = ' '; *p++ = ' ';
nodelimiter = 1; /* No delimiter */ nodelimiter = 1; /* No delimiter */
...@@ -175,6 +194,7 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) { ...@@ -175,6 +194,7 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
asn1p_expr_t *terminal = 0; asn1p_expr_t *terminal = 0;
int stdname = 0; int stdname = 0;
const char *typename; const char *typename;
const char *prefix;
/* Rewind to the topmost parent expression */ /* Rewind to the topmost parent expression */
if((top_parent = expr->parent_expr)) if((top_parent = expr->parent_expr))
...@@ -317,28 +337,30 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) { ...@@ -317,28 +337,30 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
} }
} }
prefix = stdname ? "" : asn1c_prefix();
switch(_format) { switch(_format) {
case TNF_UNMODIFIED: case TNF_UNMODIFIED:
return asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, return asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER | (stdname ? 0 : AMI_USE_PREFIX),
0, MODULE_NAME_OF(exprid), exprid ? exprid->Identifier : typename, (char*)0); 0, prefix, MODULE_NAME_OF(exprid), exprid ? exprid->Identifier : typename, (char*)0);
case TNF_INCLUDE: case TNF_INCLUDE:
return asn1c_make_identifier( return asn1c_make_identifier(
AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, AMI_MASK_ONLY_SPACES | AMI_NODELIMITER,
0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) 0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
? "\"" : "<"), ? "\"" : "<"),
MODULE_NAME_OF(exprid), prefix, MODULE_NAME_OF(exprid),
exprid ? exprid->Identifier : typename, exprid ? exprid->Identifier : typename,
((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
? ".h\"" : ".h>"), (char*)0); ? ".h\"" : ".h>"), (char*)0);
case TNF_SAFE: case TNF_SAFE:
return asn1c_make_identifier(0, exprid, typename, (char*)0); return asn1c_make_identifier(stdname ? 0 : AMI_USE_PREFIX, exprid, typename, (char*)0);
case TNF_CTYPE: /* C type */ case TNF_CTYPE: /* C type */
case TNF_CONSTYPE: /* C type */ case TNF_CONSTYPE: /* C type */
return asn1c_make_identifier(0, exprid, return asn1c_make_identifier(stdname ? 0 : AMI_USE_PREFIX, exprid,
exprid?"t":typename, exprid?0:"t", (char*)0); exprid?"t":typename, exprid?0:"t", (char*)0);
case TNF_RSAFE: /* Recursion-safe type */ case TNF_RSAFE: /* Recursion-safe type */
return asn1c_make_identifier(AMI_CHECK_RESERVED | AMI_NODELIMITER, 0, return asn1c_make_identifier(AMI_CHECK_RESERVED | AMI_NODELIMITER, 0,
"struct", " ", MODULE_NAME_OF(exprid), typename, (char*)0); "struct", " ", prefix, MODULE_NAME_OF(exprid), typename, (char*)0);
} }
assert(!"unreachable"); assert(!"unreachable");
...@@ -505,4 +527,3 @@ asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) { ...@@ -505,4 +527,3 @@ asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) {
return FL_FITS_SIGNED; return FL_FITS_SIGNED;
} }
...@@ -52,5 +52,6 @@ enum asn1c_fitsfloat_e { ...@@ -52,5 +52,6 @@ enum asn1c_fitsfloat_e {
RL_FITS_DOUBLE64 RL_FITS_DOUBLE64
}; };
enum asn1c_fitsfloat_e asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr); enum asn1c_fitsfloat_e asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr);
const char *asn1c_prefix(void);
#endif /* ASN1_COMPILER_MISC_H */ #endif /* ASN1_COMPILER_MISC_H */
#include "asn1c_internal.h" #include "asn1c_internal.h"
#include "asn1c_naming.h" #include "asn1c_naming.h"
#include "asn1c_misc.h" #include "asn1c_misc.h"
#include "asn1c_misc.h"
#include <asn1_buffer.h> #include <asn1_buffer.h>
#include <genhash.h> #include <genhash.h>
...@@ -107,20 +106,20 @@ c_name_clash(arg_t *arg) { ...@@ -107,20 +106,20 @@ c_name_clash(arg_t *arg) {
static abuf * static abuf *
construct_base_name(abuf *buf, asn1p_expr_t *expr, int compound_names, construct_base_name(abuf *buf, asn1p_expr_t *expr, int compound_names,
int avoid_keywords) { int avoid_keywords, enum ami_flags_e flag) {
const char *id; const char *id;
assert(buf); assert(buf);
if(compound_names && expr->parent_expr) { if(compound_names && expr->parent_expr) {
construct_base_name(buf, expr->parent_expr, compound_names, 0); construct_base_name(buf, expr->parent_expr, compound_names, 0, flag);
if(buf->length) { if(buf->length) {
abuf_str(buf, "__"); /* component separator */ abuf_str(buf, "__"); /* component separator */
} }
} }
id = asn1c_make_identifier( id = asn1c_make_identifier(
((avoid_keywords && !buf->length) ? AMI_CHECK_RESERVED : 0), expr, 0); ((avoid_keywords && !buf->length) ? AMI_CHECK_RESERVED : 0) | flag, expr, 0);
abuf_str(buf, id); abuf_str(buf, id);
...@@ -165,7 +164,6 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) { ...@@ -165,7 +164,6 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
abuf_clear(&b_members_enum); abuf_clear(&b_members_enum);
abuf_clear(&b_members_name); abuf_clear(&b_members_name);
abuf_str(&b_type_asn_name, asn1c_type_name(arg, expr, TNF_UNMODIFIED)); abuf_str(&b_type_asn_name, asn1c_type_name(arg, expr, TNF_UNMODIFIED));
abuf_str(&b_type_part_name, asn1c_type_name(arg, expr, TNF_SAFE)); abuf_str(&b_type_part_name, asn1c_type_name(arg, expr, TNF_SAFE));
abuf_str(&b_type_base_name, asn1c_type_name(arg, expr, TNF_SAFE)); abuf_str(&b_type_base_name, asn1c_type_name(arg, expr, TNF_SAFE));
...@@ -183,30 +181,45 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) { ...@@ -183,30 +181,45 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
} }
} }
construct_base_name(&b_asn_name, expr, 0, 0); construct_base_name(&b_asn_name, expr, 0, 0, 0);
construct_base_name(&b_part_name, expr, 0, 0); construct_base_name(&b_part_name, expr, 0, 0, AMI_USE_PREFIX);
construct_base_name(&b_base_name, expr, compound_names, avoid_keywords); construct_base_name(&b_base_name, expr, compound_names, avoid_keywords, 0);
construct_base_name(&b_as_member, expr, 0, 1); construct_base_name(&b_as_member, expr, 0, 1, 0);
static abuf tmp_compoundable_part_name; static abuf tmp_compoundable_part_name;
static abuf compound_part_name; static abuf compound_part_name;
abuf_clear(&tmp_compoundable_part_name); abuf_clear(&tmp_compoundable_part_name);
abuf_clear(&compound_part_name); abuf_clear(&compound_part_name);
construct_base_name(&tmp_compoundable_part_name, expr, compound_names, 0); construct_base_name(&tmp_compoundable_part_name, expr, compound_names, 0, 0);
construct_base_name(&compound_part_name, expr, 1, 0); construct_base_name(&compound_part_name, expr, 1, 0, 0);
if(!expr->_anonymous_type) { if(strlen(asn1c_prefix()) == 0) {
if(arg->embed) { if(!expr->_anonymous_type) {
abuf_printf(&b_short_name, "%s", b_as_member.buffer); if(arg->embed) {
} else { abuf_printf(&b_short_name, "%s", b_as_member.buffer);
abuf_printf(&b_short_name, "%s_t", b_as_member.buffer); } else {
abuf_printf(&b_short_name, "%s_t", b_as_member.buffer);
}
}
abuf_printf(&b_full_name, "struct %s", b_base_name.buffer);
abuf_printf(&b_presence_enum, "enum %s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_presence_name, "%s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_members_enum, "enum %s", b_base_name.buffer);
abuf_printf(&b_members_name, "e_%s", tmp_compoundable_part_name.buffer);
} else {
if(!expr->_anonymous_type) {
if(arg->embed) {
abuf_printf(&b_short_name, "%s%s", asn1c_prefix(), b_as_member.buffer);
} else {
abuf_printf(&b_short_name, "%s%s_t", asn1c_prefix(), b_as_member.buffer);
}
} }
abuf_printf(&b_full_name, "struct %s%s", asn1c_prefix(), b_base_name.buffer);
abuf_printf(&b_presence_enum, "enum %s%s_PR", asn1c_prefix(), tmp_compoundable_part_name.buffer);
abuf_printf(&b_presence_name, "%s%s_PR", asn1c_prefix(), tmp_compoundable_part_name.buffer);
abuf_printf(&b_members_enum, "enum %s%s", asn1c_prefix(), b_base_name.buffer);
abuf_printf(&b_members_name, "e_%s%s", asn1c_prefix(), tmp_compoundable_part_name.buffer);
} }
abuf_printf(&b_full_name, "struct %s", b_base_name.buffer);
abuf_printf(&b_presence_enum, "enum %s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_presence_name, "%s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_members_enum, "enum %s", b_base_name.buffer);
abuf_printf(&b_members_name, "e_%s", tmp_compoundable_part_name.buffer);
names.type.asn_name = b_type_asn_name.buffer; names.type.asn_name = b_type_asn_name.buffer;
names.type.base_name = b_type_base_name.buffer; names.type.base_name = b_type_base_name.buffer;
...@@ -255,6 +268,7 @@ c_member_name(arg_t *arg, asn1p_expr_t *expr) { ...@@ -255,6 +268,7 @@ c_member_name(arg_t *arg, asn1p_expr_t *expr) {
abuf_clear(&ab); abuf_clear(&ab);
/* NB: do not use part_name, doesn't work for -fcompound-names */ /* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, asn1c_prefix());
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name); abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
abuf_str(&ab, "_"); abuf_str(&ab, "_");
abuf_str(&ab, asn1c_make_identifier(0, expr, 0)); abuf_str(&ab, asn1c_make_identifier(0, expr, 0));
...@@ -269,6 +283,7 @@ c_presence_name(arg_t *arg, asn1p_expr_t *expr) { ...@@ -269,6 +283,7 @@ c_presence_name(arg_t *arg, asn1p_expr_t *expr) {
abuf_clear(&ab); abuf_clear(&ab);
abuf_str(&ab, asn1c_prefix());
if(expr) { if(expr) {
/* NB: do not use part_name, doesn't work for -fcompound-names */ /* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name); abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
......
...@@ -76,7 +76,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps, ...@@ -76,7 +76,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
[arg->expr->expr_type].type_cb && [arg->expr->expr_type].type_cb &&
(arg->expr->meta_type != AMT_VALUE)) { (arg->expr->meta_type != AMT_VALUE)) {
safe_fprintf(mkf, "\t\\\n\t%s%s.c", destdir, safe_fprintf(mkf, "\t\\\n\t%s%s.c", destdir,
asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0)); asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_USE_PREFIX, arg->expr, 0));
} }
} }
} }
...@@ -88,7 +88,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps, ...@@ -88,7 +88,7 @@ asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
(arg->expr->meta_type != AMT_VALUE)) { (arg->expr->meta_type != AMT_VALUE)) {
safe_fprintf( safe_fprintf(
mkf, "\t\\\n\t%s%s.h", destdir, mkf, "\t\\\n\t%s%s.h", destdir,
asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0)); asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_USE_PREFIX, arg->expr, 0));
} }
} }
} }
...@@ -501,7 +501,8 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir, ...@@ -501,7 +501,8 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir,
return -1; return -1;
} }
filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0)); filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_USE_PREFIX,
expr, (char*)0));
fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c); fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c);
if(fp_c == NULL) { if(fp_c == NULL) {
return -1; return -1;
...@@ -517,7 +518,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir, ...@@ -517,7 +518,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir,
generate_preamble(arg, fp_c, optc, argv); generate_preamble(arg, fp_c, optc, argv);
generate_preamble(arg, fp_h, optc, argv); generate_preamble(arg, fp_h, optc, argv);
header_id = asn1c_make_identifier(0, expr, NULL); header_id = asn1c_make_identifier(AMI_USE_PREFIX, expr, NULL);
safe_fprintf(fp_h, safe_fprintf(fp_h,
"#ifndef\t_%s_H_\n" "#ifndef\t_%s_H_\n"
"#define\t_%s_H_\n" "#define\t_%s_H_\n"
...@@ -541,7 +542,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir, ...@@ -541,7 +542,7 @@ asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char *destdir,
SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0); SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0); SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0); SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0);
SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0); SAVE_STREAM(fp_h, OT_TYPE_DECLS, filename, 0);
SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0); SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n"); safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
...@@ -797,7 +798,7 @@ generate_pdu_collection(arg_t *arg) { ...@@ -797,7 +798,7 @@ generate_pdu_collection(arg_t *arg) {
abuf_printf(buf, abuf_printf(buf,
"extern struct asn_TYPE_descriptor_s " "extern struct asn_TYPE_descriptor_s "
"asn_DEF_%s;\n", "asn_DEF_%s;\n",
asn1c_make_identifier(0, arg->expr, NULL)); asn1c_make_identifier(AMI_USE_PREFIX, arg->expr, NULL));
} }
} }
...@@ -822,7 +823,7 @@ generate_pdu_collection(arg_t *arg) { ...@@ -822,7 +823,7 @@ generate_pdu_collection(arg_t *arg) {
arg->expr->module->source_file_name); arg->expr->module->source_file_name);
} }
abuf_printf(buf, "\t&asn_DEF_%s,\t\n", abuf_printf(buf, "\t&asn_DEF_%s,\t\n",
asn1c_make_identifier(0, arg->expr, NULL)); asn1c_make_identifier(AMI_USE_PREFIX, arg->expr, NULL));
} }
} }
...@@ -960,7 +961,7 @@ generate_constant_collection(arg_t *arg) { ...@@ -960,7 +961,7 @@ generate_constant_collection(arg_t *arg) {
abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION
" (http://lionet.info/asn1c)\n */\n\n"); " (http://lionet.info/asn1c)\n */\n\n");
abuf_printf(buf, "#ifndef _ASN_CONSTANT_H\n#define _ASN_CONSTANT_H\n\n"); abuf_printf(buf, "#ifndef _%sASN_CONSTANT_H\n#define _%sASN_CONSTANT_H\n\n", asn1c_prefix(), asn1c_prefix());
abuf_printf(buf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"); abuf_printf(buf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
...@@ -971,14 +972,14 @@ generate_constant_collection(arg_t *arg) { ...@@ -971,14 +972,14 @@ generate_constant_collection(arg_t *arg) {
if(arg->expr->expr_type == ASN_BASIC_INTEGER) { if(arg->expr->expr_type == ASN_BASIC_INTEGER) {
abuf_printf(buf, "#define %s (%s)\n", abuf_printf(buf, "#define %s (%s)\n",
asn1c_make_identifier(0, arg->expr, 0), asn1c_make_identifier(AMI_USE_PREFIX, arg->expr, 0),
asn1p_itoa(arg->expr->value->value.v_integer)); asn1p_itoa(arg->expr->value->value.v_integer));
empty_file = 0; empty_file = 0;
} }
} }
} }
abuf_printf(buf, "\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _ASN_CONSTANT_H */\n"); abuf_printf(buf, "\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _%sASN_CONSTANT_H */\n", asn1c_prefix());
if(empty_file) { if(empty_file) {
abuf_free(buf); abuf_free(buf);
...@@ -990,15 +991,21 @@ generate_constant_collection(arg_t *arg) { ...@@ -990,15 +991,21 @@ generate_constant_collection(arg_t *arg) {
static int static int
generate_constant_file(arg_t *arg, const char *destdir) { generate_constant_file(arg_t *arg, const char *destdir) {
abuf *buf = generate_constant_collection(arg); abuf *buf = generate_constant_collection(arg);
char *filename;
int filename_len;
if(!buf) return 0; if(!buf) return 0;
filename_len = strlen(asn1c_prefix()) + strlen("asn_constant");
filename = calloc(filename_len + 1, 1);
snprintf(filename, filename_len + 1, "%sasn_constant", asn1c_prefix());
if(arg->flags & A1C_PRINT_COMPILED) { if(arg->flags & A1C_PRINT_COMPILED) {
printf("\n/*** <<< asn_constant.h >>> ***/\n\n"); printf("\n/*** <<< asn_constant.h >>> ***/\n\n");
safe_fwrite(buf->buffer, buf->length, 1, stdout); safe_fwrite(buf->buffer, buf->length, 1, stdout);
} else { } else {
FILE *fp = asn1c_open_file(destdir, "asn_constant", ".h", 0); FILE *fp = asn1c_open_file(destdir, filename, ".h", 0);
if(fp == NULL) { if(fp == NULL) {
perror("asn_constant.h"); perror("asn_constant.h");
return -1; return -1;
...@@ -1006,8 +1013,8 @@ generate_constant_file(arg_t *arg, const char *destdir) { ...@@ -1006,8 +1013,8 @@ generate_constant_file(arg_t *arg, const char *destdir) {
safe_fwrite(buf->buffer, buf->length, 1, fp); safe_fwrite(buf->buffer, buf->length, 1, fp);
fclose(fp); fclose(fp);
} }
safe_fprintf(stderr, "Generated asn_constant.h\n"); safe_fprintf(stderr, "Generated %s.h\n", filename);
free(filename);
abuf_free(buf); abuf_free(buf);
return 0; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment