Skip to content

Commit

Permalink
Merge pull request #170 from v0-e/prefix-optional-argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mouse07410 authored Jan 17, 2024
2 parents 1b1e30a + 68723b6 commit 3270650
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 23 deletions.
4 changes: 4 additions & 0 deletions asn1c/asn1c.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ main(int ac, char **av) {
asn1_compiler_flags &= ~A1C_LINK_SKELETONS;
} else if(strcmp(optarg, "link-skeletons") == 0) {
asn1_compiler_flags |= A1C_LINK_SKELETONS;
} else if(strncmp(optarg, "prefix=", 7) == 0) {
char *prefix = optarg + 7;
asn1c_prefix_set(prefix);
} else {
fprintf(stderr, "-f%s: Invalid argument\n", optarg);
exit(EX_USAGE);
Expand Down Expand Up @@ -589,6 +592,7 @@ usage(const char *av0) {
" -fno-include-deps Do not generate the courtesy #includes for dependencies\n"
" -funnamed-unions Enable unnamed unions in structures\n"
" -fwide-types Use INTEGER_t instead of \"long\" by default, etc.\n"
" -fprefix=<prefix> Add the specified prefix to generated types\n"
"\n"

" -no-gen-BER Do not generate the Basic Encoding Rules (BER, X.690) support code\n"
Expand Down
30 changes: 22 additions & 8 deletions libasn1compiler/asn1c_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,28 @@ reserved_keyword(const char *str) {
return 0;
}

const char *
asn1c_prefix()
{
const char *prefix = getenv("ASN1C_PREFIX");
const char *asn1c_prefix = NULL;

void
asn1c_prefix_set(const char *prefix) {
asn1c_prefix = prefix;
}

const char *
asn1c_prefix_get() {
const char *prefix;

prefix = asn1c_prefix;
if (prefix)
return prefix;

prefix = getenv("ASN1C_PREFIX");
if (prefix)
return prefix;

if(!prefix) prefix = "";
prefix = "";

return prefix;
return prefix;
}

/*
Expand All @@ -68,7 +82,7 @@ asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
int sptr_cnt = 0;

if(flags & AMI_USE_PREFIX)
prefix = asn1c_prefix();
prefix = asn1c_prefix_get();

if(expr) {
/*
Expand Down Expand Up @@ -348,7 +362,7 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
}
}

prefix = stdname ? "" : asn1c_prefix();
prefix = stdname ? "" : asn1c_prefix_get();

switch(_format) {
case TNF_UNMODIFIED:
Expand Down
1 change: 0 additions & 1 deletion libasn1compiler/asn1c_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ enum asn1c_fitsfloat_e {
RL_FITS_DOUBLE64
};
enum asn1c_fitsfloat_e asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr);
const char *asn1c_prefix(void);

#endif /* ASN1_COMPILER_MISC_H */
20 changes: 10 additions & 10 deletions libasn1compiler/asn1c_naming.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
construct_base_name(&tmp_compoundable_part_name, expr, compound_names, 0, 0);
construct_base_name(&compound_part_name, expr, 1, 0, 0);

if(strlen(asn1c_prefix()) == 0) {
if(strlen(asn1c_prefix_get()) == 0) {
if(!expr->_anonymous_type) {
if(arg->embed) {
abuf_printf(&b_short_name, "%s", b_as_member.buffer);
Expand All @@ -209,16 +209,16 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
} else {
if(!expr->_anonymous_type) {
if(arg->embed) {
abuf_printf(&b_short_name, "%s%s", asn1c_prefix(), b_as_member.buffer);
abuf_printf(&b_short_name, "%s%s", asn1c_prefix_get(), b_as_member.buffer);
} else {
abuf_printf(&b_short_name, "%s%s_t", asn1c_prefix(), b_as_member.buffer);
abuf_printf(&b_short_name, "%s%s_t", asn1c_prefix_get(), 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%s", asn1c_prefix_get(), b_base_name.buffer);
abuf_printf(&b_presence_enum, "enum %s%s_PR", asn1c_prefix_get(), tmp_compoundable_part_name.buffer);
abuf_printf(&b_presence_name, "%s%s_PR", asn1c_prefix_get(), tmp_compoundable_part_name.buffer);
abuf_printf(&b_members_enum, "enum %s%s", asn1c_prefix_get(), b_base_name.buffer);
abuf_printf(&b_members_name, "e_%s%s", asn1c_prefix_get(), tmp_compoundable_part_name.buffer);
}

names.type.asn_name = b_type_asn_name.buffer;
Expand Down Expand Up @@ -268,7 +268,7 @@ c_member_name(arg_t *arg, asn1p_expr_t *expr) {
abuf_clear(&ab);

/* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, asn1c_prefix());
abuf_str(&ab, asn1c_prefix_get());
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
abuf_str(&ab, "_");
abuf_str(&ab, asn1c_make_identifier(0, expr, 0));
Expand All @@ -283,7 +283,7 @@ c_presence_name(arg_t *arg, asn1p_expr_t *expr) {

abuf_clear(&ab);

abuf_str(&ab, asn1c_prefix());
abuf_str(&ab, asn1c_prefix_get());
if(expr) {
/* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
Expand Down
8 changes: 4 additions & 4 deletions libasn1compiler/asn1c_save.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ generate_constant_collection(arg_t *arg) {

abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION
" (http://lionet.info/asn1c)\n */\n\n");
abuf_printf(buf, "#ifndef _%sASN_CONSTANT_H\n#define _%sASN_CONSTANT_H\n\n", asn1c_prefix(), asn1c_prefix());
abuf_printf(buf, "#ifndef _%sASN_CONSTANT_H\n#define _%sASN_CONSTANT_H\n\n", asn1c_prefix_get(), asn1c_prefix_get());

abuf_printf(buf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");

Expand Down Expand Up @@ -1054,7 +1054,7 @@ generate_constant_collection(arg_t *arg) {
}
}

abuf_printf(buf, "\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _%sASN_CONSTANT_H */\n", asn1c_prefix());
abuf_printf(buf, "\n\n#ifdef __cplusplus\n}\n#endif\n\n#endif /* _%sASN_CONSTANT_H */\n", asn1c_prefix_get());

if(empty_file) {
abuf_free(buf);
Expand All @@ -1071,9 +1071,9 @@ generate_constant_file(arg_t *arg, const char *destdir) {

if(!buf) return 0;

filename_len = strlen(asn1c_prefix()) + strlen("asn_constant");
filename_len = strlen(asn1c_prefix_get()) + strlen("asn_constant");
filename = calloc(filename_len + 1, 1);
snprintf(filename, filename_len + 1, "%sasn_constant", asn1c_prefix());
snprintf(filename, filename_len + 1, "%sasn_constant", asn1c_prefix_get());

if(arg->flags & A1C_PRINT_COMPILED) {
printf("\n/*** <<< asn_constant.h >>> ***/\n\n");
Expand Down
3 changes: 3 additions & 0 deletions libasn1compiler/asn1compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ void asn1c_debug_type_naming(asn1p_t *asn, enum asn1c_flags,

void asn1c__add_pdu_type(const char *typename);

void asn1c_prefix_set(const char *prefix);
const char* asn1c_prefix_get(void);

int asn1c_attach_streams(asn1p_expr_t *expr);

#endif /* ASN1_COMPILER_H */

0 comments on commit 3270650

Please sign in to comment.