Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 2 | #include "asn1c_compat.h" |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 3 | #include "asn1c_fdeps.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 4 | #include "asn1c_lang.h" |
| 5 | #include "asn1c_misc.h" |
| 6 | #include "asn1c_save.h" |
| 7 | #include "asn1c_out.h" |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | |
Frank Morgner | 8a759ad | 2013-05-16 13:32:49 +0200 | [diff] [blame] | 9 | #ifndef HAVE_SYMLINK |
| 10 | #define symlink(a,b) (errno=ENOSYS, -1) |
| 11 | #endif |
| 12 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 13 | /* Pedantically check fprintf's return value. */ |
| 14 | static int safe_fprintf(FILE *fp, const char *fmt, ...) { |
| 15 | va_list ap; |
| 16 | va_start(ap, fmt); |
| 17 | int ret = vfprintf(fp, fmt, ap); |
| 18 | va_end(ap); |
| 19 | assert(ret >= 0); |
| 20 | return ret; |
| 21 | } |
| 22 | |
| 23 | /* Pedantically check fwrite's return value. */ |
| 24 | static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) { |
| 25 | size_t ret = fwrite(ptr, 1, size * nitems, stream); |
| 26 | assert(ret == size * nitems); |
| 27 | return ret; |
| 28 | } |
| 29 | |
Lev Walkin | 34944f2 | 2010-10-07 08:25:37 +0000 | [diff] [blame] | 30 | #define HINCLUDE(s) \ |
| 31 | ((arg->flags & A1C_INCLUDES_QUOTED) \ |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 32 | ? safe_fprintf(fp_h, "#include \"%s\"\n", s) \ |
| 33 | : safe_fprintf(fp_h, "#include <%s>\n", s)) \ |
Lev Walkin | 34944f2 | 2010-10-07 08:25:37 +0000 | [diff] [blame] | 34 | |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 35 | static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *, int, char **); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | static int asn1c_print_streams(arg_t *arg); |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 37 | static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *, int, char **); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | static int asn1c_copy_over(arg_t *arg, char *path); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 39 | static int identical_files(const char *fname1, const char *fname2); |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 40 | static int need_to_generate_pdu_collection(arg_t *arg); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 41 | static int generate_pdu_collection_file(arg_t *arg); |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 42 | static int generate_preamble(arg_t *, FILE *, int optc, char **argv); |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 43 | static int include_type_to_pdu_collection(arg_t *arg); |
| 44 | static void pdu_collection_print_unused_types(arg_t *arg); |
| 45 | static const char *generate_pdu_C_definition(void); |
Lev Walkin | 5230c64 | 2017-09-26 18:10:06 -0700 | [diff] [blame] | 46 | static void asn1c__cleanup_pdu_type(void); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 47 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 48 | static int |
| 49 | asn1c__save_library_makefile(arg_t *arg, const asn1c_fdeps_t *deps, const char *datadir, const char *makefile_name) { |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 50 | asn1c_fdeps_t *dlist; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 51 | asn1p_module_t *mod; |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 52 | FILE *mkf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 54 | mkf = asn1c_open_file(makefile_name, "", 0); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 55 | if(mkf == NULL) { |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 56 | perror(makefile_name); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 57 | return -1; |
| 58 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 60 | safe_fprintf(mkf, "ASN_MODULE_SOURCES="); |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 61 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 62 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 63 | if(asn1_lang_map[arg->expr->meta_type] |
| 64 | [arg->expr->expr_type].type_cb) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 65 | safe_fprintf(mkf, "\t\\\n\t%s.c", |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 66 | asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 69 | } |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 70 | safe_fprintf(mkf, "\n\nASN_MODULE_HEADERS="); |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 71 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 72 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 73 | if(asn1_lang_map[arg->expr->meta_type] |
| 74 | [arg->expr->expr_type].type_cb) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 75 | safe_fprintf(mkf, "\t\\\n\t%s.h", |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 76 | asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0)); |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 80 | safe_fprintf(mkf, "\n\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 82 | /* |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 83 | * Move necessary skeleton files and add them to Makefile.am.targets. |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 84 | */ |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 85 | dlist = asn1c_deps_flatten(deps); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 86 | if(dlist) { |
| 87 | char buf[8129]; |
| 88 | char *dir_end; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 89 | size_t dlen = strlen(datadir); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 90 | |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 91 | assert(dlen < (sizeof(buf) / 2 - 2)); |
| 92 | memcpy(buf, datadir, dlen); |
| 93 | dir_end = buf + dlen; |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 94 | *dir_end++ = '/'; |
| 95 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 96 | for(int i = 0; i < dlist->el_count; i++) { |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 97 | char *what_kind; /* HEADERS or SOURCES */ |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 98 | char *fname = dlist->elements[i]->filename; |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 99 | char *dotH; |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 100 | |
| 101 | assert(strlen(fname) < (sizeof(buf) / 2)); |
| 102 | strcpy(dir_end, fname); |
| 103 | |
| 104 | if(asn1c_copy_over(arg, buf) == -1) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 105 | safe_fprintf(mkf, ">>>ABORTED<<<"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | fclose(mkf); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | return -1; |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 108 | } |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 109 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 110 | /* no CONVERTER data in Makefile.am.targets */ |
| 111 | if(dlist->elements[i]->usage != FDEP_CONVERTER) { |
| 112 | /* HEADERS versus SOURCES */ |
| 113 | dotH = strrchr(fname, 'h'); |
| 114 | if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) |
| 115 | what_kind = "HEADERS"; |
| 116 | else |
| 117 | what_kind = "SOURCES"; |
| 118 | safe_fprintf(mkf, "ASN_MODULE_%s+=%s\n", |
| 119 | what_kind, fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
Bi-Ruei, Chiu | 6f34894 | 2016-11-08 15:41:23 +0800 | [diff] [blame] | 122 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 123 | asn1c_deps_freelist(dlist); |
| 124 | } |
| 125 | |
| 126 | safe_fprintf( |
| 127 | mkf, |
| 128 | "\n" |
| 129 | "ASN_MODULE_CFLAGS=%s%s", |
| 130 | (arg->flags & A1C_GEN_OER) ? "" : "-DASN_DISABLE_OER_SUPPORT ", |
| 131 | (arg->flags & A1C_GEN_PER) ? "" : "-DASN_DISABLE_PER_SUPPORT "); |
| 132 | |
| 133 | safe_fprintf( |
| 134 | mkf, |
| 135 | "\n\n" |
| 136 | "lib_LTLIBRARIES=libasncodec.la\n" |
| 137 | "libasncodec_la_SOURCES=" |
| 138 | "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n" |
| 139 | "libasncodec_la_CFLAGS=$(ASN_MODULE_CFLAGS)\n"); |
| 140 | fclose(mkf); |
| 141 | safe_fprintf(stderr, "Generated Makefile.am.targets\n"); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int |
| 147 | asn1c__save_example_makefile(arg_t *arg, const asn1c_fdeps_t *deps, |
| 148 | const char *makefile_name, |
| 149 | const char *library_makefile_name, int argc, |
| 150 | char **argv) { |
| 151 | asn1c_fdeps_t *dlist; |
| 152 | FILE *mkf; |
| 153 | |
| 154 | mkf = asn1c_open_file(makefile_name, "", 0); |
| 155 | if(mkf == NULL) { |
| 156 | perror(makefile_name); |
| 157 | return -1; |
| 158 | } |
| 159 | safe_fprintf( |
| 160 | mkf, |
| 161 | "include %s\n\n" |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 162 | "LIBS += -lm\n" |
| 163 | "CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n" |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 164 | "ASN_LIBRARY ?= libasncodec.a\n" |
| 165 | "ASN_PROGRAM ?= converter-example\n" |
| 166 | "ASN_PROGRAM_SOURCES ?= ", |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 167 | library_makefile_name, |
| 168 | (arg->flags & A1C_PDU_TYPE) ? generate_pdu_C_definition() : "", |
| 169 | need_to_generate_pdu_collection(arg) ? "-DASN_PDU_COLLECTION " : ""); |
| 170 | |
| 171 | dlist = asn1c_deps_flatten(deps); |
| 172 | if(dlist) { |
| 173 | /* only CONVERTER data in the makefile */ |
| 174 | for(int i = 0; i < dlist->el_count; i++) { |
| 175 | if(dlist->elements[i]->usage == FDEP_CONVERTER) { |
| 176 | safe_fprintf(mkf, "\\\n\t%s", dlist->elements[i]->filename); |
| 177 | } |
| 178 | } |
Bi-Ruei, Chiu | 6f34894 | 2016-11-08 15:41:23 +0800 | [diff] [blame] | 179 | asn1c_deps_freelist(dlist); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 182 | if(need_to_generate_pdu_collection(arg)) { |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 183 | safe_fprintf(mkf, "\\\n\tpdu_collection.c"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 184 | if(generate_pdu_collection_file(arg)) |
| 185 | return -1; |
| 186 | } |
| 187 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 188 | safe_fprintf( |
| 189 | mkf, |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 190 | "\n\nall: $(ASN_PROGRAM)\n" |
| 191 | "\n$(ASN_PROGRAM): $(ASN_LIBRARY) $(ASN_PROGRAM_SOURCES:.c=.o)" |
| 192 | "\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) $(ASN_PROGRAM_SOURCES:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n" |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 193 | "\n$(ASN_LIBRARY): $(ASN_MODULE_SOURCES:.c=.o)" |
Lev Walkin | b458fc0 | 2017-10-02 08:18:36 +0000 | [diff] [blame] | 194 | "\n\t$(AR) rcs $@ $(ASN_MODULE_SOURCES:.c=.o)\n" |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 195 | "\n.SUFFIXES:" |
| 196 | "\n.SUFFIXES: .c .o\n" |
| 197 | "\n.c.o:" |
| 198 | "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n" |
| 199 | "\nclean:" |
Lev Walkin | e24bad1 | 2017-10-09 23:58:15 -0700 | [diff] [blame] | 200 | "\n\trm -f $(ASN_PROGRAM) $(ASN_LIBRARY)" |
Lev Walkin | 40b8a7a | 2017-10-08 22:36:29 -0700 | [diff] [blame] | 201 | "\n\trm -f $(ASN_MODULE_SOURCES:.c=.o) $(ASN_PROGRAM_SOURCES:.c=.o)\n" |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 202 | "\nregen: regenerate-from-asn1-source\n" |
| 203 | "\nregenerate-from-asn1-source:\n\t"); |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 204 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 205 | for(int i = 0; i < argc; i++) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 206 | safe_fprintf(mkf, "%s%s", i ? " " : "", argv[i]); |
| 207 | safe_fprintf(mkf, "\n\n"); |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 208 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 209 | fclose(mkf); |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 210 | safe_fprintf(stderr, "Generated %s\n", makefile_name); |
| 211 | return 0; |
| 212 | } |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 213 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 214 | int |
| 215 | asn1c_save_compiled_output(arg_t *arg, const char *datadir, |
| 216 | int argc, int optc, char **argv) { |
| 217 | asn1c_fdeps_t *deps = 0; |
| 218 | int ret = -1; |
Lev Walkin | 5230c64 | 2017-09-26 18:10:06 -0700 | [diff] [blame] | 219 | |
Lev Walkin | 8a85b36 | 2017-09-26 22:54:44 -0700 | [diff] [blame] | 220 | do { |
| 221 | asn1p_module_t *mod; |
| 222 | |
| 223 | deps = asn1c_read_file_dependencies(arg, datadir); |
| 224 | if(!deps && datadir) { |
| 225 | WARNING( |
| 226 | "Cannot read file-dependencies information " |
| 227 | "from %s\n", |
| 228 | datadir); |
| 229 | } |
| 230 | |
| 231 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 232 | TQ_FOR(arg->expr, &(mod->members), next) { |
| 233 | if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type] |
| 234 | .type_cb) { |
| 235 | if(asn1c_dump_streams(arg, deps, optc, argv)) break; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Dump out the Makefile template and the rest of the support code. |
| 242 | */ |
| 243 | if((arg->flags & A1C_PRINT_COMPILED) |
| 244 | || (arg->flags & A1C_OMIT_SUPPORT_CODE)) { |
| 245 | ret = 0; /* Success */ |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | ret = asn1c__save_library_makefile(arg, deps, datadir, |
| 250 | "Makefile.am.libasncodec"); |
| 251 | if(ret) break; |
| 252 | ret = asn1c__save_example_makefile(arg, deps, "Makefile.am.example", |
| 253 | "Makefile.am.libasncodec", argc, argv); |
| 254 | if(ret) break; |
| 255 | } while(0); |
| 256 | |
| 257 | asn1c_deps_freelist(deps); |
| 258 | asn1c__cleanup_pdu_type(); |
| 259 | |
| 260 | return ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /* |
| 264 | * Dump the streams. |
| 265 | */ |
| 266 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 267 | asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 268 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 269 | return asn1c_print_streams(arg); |
| 270 | } else { |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 271 | return asn1c_save_streams(arg, deps, optc, argv); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | static int |
| 276 | asn1c_print_streams(arg_t *arg) { |
| 277 | compiler_streams_t *cs = arg->expr->data; |
| 278 | asn1p_expr_t *expr = arg->expr; |
| 279 | int i; |
| 280 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 281 | for(i = 1; i < OT_MAX; i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 282 | out_chunk_t *ot; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 283 | if(TQ_FIRST(&cs->destination[i].chunks) == NULL) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 284 | continue; |
| 285 | |
| 286 | printf("\n/*** <<< %s [%s] >>> ***/\n\n", |
| 287 | _compiler_stream2str[i], |
| 288 | expr->Identifier); |
| 289 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 290 | TQ_FOR(ot, &(cs->destination[i].chunks), next) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 291 | safe_fwrite(ot->buf, ot->len, 1, stdout); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 299 | asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 300 | asn1p_expr_t *expr = arg->expr; |
| 301 | compiler_streams_t *cs = expr->data; |
| 302 | out_chunk_t *ot; |
| 303 | FILE *fp_c, *fp_h; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 304 | char *tmpname_c, *tmpname_h; |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 305 | char name_buf[FILENAME_MAX]; |
Lev Walkin | b46156d | 2017-09-05 02:53:05 -0700 | [diff] [blame] | 306 | const char *header_id; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 307 | const char *c_retained = ""; |
| 308 | const char *h_retained = ""; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 309 | char *filename; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 310 | |
| 311 | if(cs == NULL) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 312 | safe_fprintf(stderr, "Cannot compile %s at line %d\n", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 313 | expr->Identifier, expr->_lineno); |
| 314 | return -1; |
| 315 | } |
| 316 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 317 | filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0)); |
| 318 | fp_c = asn1c_open_file(filename, ".c", &tmpname_c); |
| 319 | fp_h = asn1c_open_file(filename, ".h", &tmpname_h); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 320 | if(fp_c == NULL || fp_h == NULL) { |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 321 | if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); } |
| 322 | if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 326 | generate_preamble(arg, fp_c, optc, argv); |
| 327 | generate_preamble(arg, fp_h, optc, argv); |
Lev Walkin | c3b8f6d | 2004-06-03 05:06:25 +0000 | [diff] [blame] | 328 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 329 | header_id = asn1c_make_identifier(0, expr, NULL); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 330 | safe_fprintf(fp_h, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 331 | "#ifndef\t_%s_H_\n" |
| 332 | "#define\t_%s_H_\n" |
| 333 | "\n", header_id, header_id); |
| 334 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 335 | safe_fprintf(fp_h, "\n"); |
Lev Walkin | 34944f2 | 2010-10-07 08:25:37 +0000 | [diff] [blame] | 336 | HINCLUDE("asn_application.h"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 337 | |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 338 | #define SAVE_STREAM(fp, idx, msg, actdep) do { \ |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 339 | if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \ |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 340 | safe_fprintf(fp, "\n/* %s */\n", msg); \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 341 | TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \ |
| 342 | if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \ |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 343 | safe_fwrite(ot->buf, ot->len, 1, fp); \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 344 | } \ |
| 345 | } while(0) |
| 346 | |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 347 | SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1); |
Lev Walkin | 3d551c0 | 2005-07-15 18:49:41 +0000 | [diff] [blame] | 348 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 349 | safe_fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 350 | SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0); |
| 351 | SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 352 | SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0); |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 353 | SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0); |
| 354 | SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 355 | safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 356 | |
Lev Walkin | ae5540f | 2006-02-19 04:26:37 +0000 | [diff] [blame] | 357 | if(!(arg->flags & A1C_NO_INCLUDE_DEPS)) |
| 358 | SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1); |
| 359 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 360 | safe_fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id); |
Lev Walkin | 4737039 | 2006-02-19 04:30:15 +0000 | [diff] [blame] | 361 | |
Lev Walkin | 34944f2 | 2010-10-07 08:25:37 +0000 | [diff] [blame] | 362 | HINCLUDE("asn_internal.h"); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 363 | safe_fprintf(fp_c, "#include \"%s.h\"\n\n", filename); |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 364 | if(arg->flags & A1C_NO_INCLUDE_DEPS) |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 365 | SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1); |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 366 | TQ_FOR(ot, &(cs->destination[OT_IOC_TABLES].chunks), next) |
| 367 | safe_fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 368 | TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 369 | safe_fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 370 | TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 371 | safe_fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 372 | TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 373 | safe_fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 374 | TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 375 | safe_fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 376 | |
Lev Walkin | 9de6cd8 | 2017-08-10 05:47:46 -0700 | [diff] [blame] | 377 | assert(OT_MAX == 13); /* Protection from reckless changes */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 378 | |
| 379 | fclose(fp_c); |
| 380 | fclose(fp_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 381 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 382 | int ret = snprintf(name_buf, sizeof(name_buf), "%s.c", filename); |
| 383 | assert(ret > 0 && ret < (ssize_t)sizeof(name_buf)); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 384 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 385 | if(identical_files(name_buf, tmpname_c)) { |
| 386 | c_retained = " (contents unchanged)"; |
| 387 | unlink(tmpname_c); |
| 388 | } else { |
| 389 | if(rename(tmpname_c, name_buf)) { |
| 390 | unlink(tmpname_c); |
| 391 | perror(tmpname_c); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 392 | free(tmpname_c); |
| 393 | free(tmpname_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 394 | return -1; |
| 395 | } |
| 396 | } |
| 397 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 398 | sprintf(name_buf, "%s.h", filename); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 399 | if(identical_files(name_buf, tmpname_h)) { |
| 400 | h_retained = " (contents unchanged)"; |
| 401 | unlink(tmpname_h); |
| 402 | } else { |
| 403 | if(rename(tmpname_h, name_buf)) { |
| 404 | unlink(tmpname_h); |
| 405 | perror(tmpname_h); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 406 | free(tmpname_c); |
| 407 | free(tmpname_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 408 | return -1; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | free(tmpname_c); |
| 413 | free(tmpname_h); |
| 414 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 415 | safe_fprintf(stderr, "Compiled %s.c%s\n", |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 416 | filename, c_retained); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 417 | safe_fprintf(stderr, "Compiled %s.h%s\n", |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 418 | filename, h_retained); |
| 419 | free(filename); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 423 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 424 | generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 425 | safe_fprintf(fp, |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 426 | "/*\n" |
| 427 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 428 | " * From ASN.1 module \"%s\"\n" |
| 429 | " * \tfound in \"%s\"\n", |
| 430 | arg->expr->module->ModuleName, |
| 431 | arg->expr->module->source_file_name); |
| 432 | if(optc > 1) { |
| 433 | int i; |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 434 | safe_fprintf(fp, " * \t`asn1c "); |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 435 | for(i = 1; i < optc; i++) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 436 | safe_fprintf(fp, "%s%s", i>1?" ":"", argv[i]); |
| 437 | safe_fprintf(fp, "`\n"); |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 438 | } |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 439 | safe_fprintf(fp, " */\n\n"); |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 444 | identical_files(const char *fname1, const char *fname2) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 445 | char buf[2][4096]; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 446 | FILE *fp1, *fp2; |
| 447 | size_t olen, nlen; |
| 448 | int retval = 1; /* Files are identical */ |
| 449 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 450 | #ifndef _WIN32 |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 451 | struct stat sb; |
| 452 | |
| 453 | if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode) |
| 454 | || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) { |
| 455 | return 0; /* Files are not identical */ |
| 456 | } |
| 457 | #endif |
| 458 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 459 | fp1 = fopen(fname1, "r"); |
| 460 | if(!fp1) { return 0; } |
| 461 | fp2 = fopen(fname2, "r"); |
| 462 | if(!fp2) { fclose(fp1); return 0; } |
| 463 | |
| 464 | while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) { |
| 465 | nlen = fread(buf[1], 1, olen, fp2); |
| 466 | if(nlen != olen || memcmp(buf[0], buf[1], nlen)) { |
| 467 | retval = 0; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | nlen = fread(buf[1], 1, 1, fp2); |
| 472 | if(nlen) retval = 0; |
| 473 | |
| 474 | fclose(fp1); |
| 475 | fclose(fp2); |
| 476 | return retval; |
| 477 | } |
| 478 | |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 479 | /* |
| 480 | * Copy file for real. |
| 481 | */ |
| 482 | static int |
| 483 | real_copy(const char *src, const char *dst) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 484 | unsigned char buf[4096]; |
| 485 | char *tmpname; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 486 | FILE *fpsrc, *fpdst; |
| 487 | size_t len; |
| 488 | int retval = 0; |
| 489 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 490 | if(identical_files(src, dst)) |
| 491 | return retval; /* Success, no need to copy for real. */ |
| 492 | |
| 493 | fpsrc = fopen(src, "r"); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 494 | if(!fpsrc) { errno = EIO; return -1; } |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 495 | fpdst = asn1c_open_file(dst, "", &tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 496 | if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; } |
| 497 | |
| 498 | while(!feof(fpsrc)) { |
| 499 | len = fread(buf, 1, sizeof(buf), fpsrc); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 500 | if(safe_fwrite(buf, 1, len, fpdst) != len) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 501 | perror(tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 502 | errno = EIO; |
| 503 | retval = -1; |
| 504 | break; |
| 505 | } |
| 506 | } |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 507 | fclose(fpsrc); |
| 508 | fclose(fpdst); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 509 | |
| 510 | /* Check if copied correctly, and rename into a permanent name */ |
| 511 | if(retval) { |
| 512 | unlink(tmpname); |
| 513 | } else if(rename(tmpname, dst)) { |
| 514 | unlink(tmpname); |
| 515 | perror(tmpname); |
| 516 | retval = -1; |
| 517 | } |
| 518 | free(tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 519 | return retval; |
| 520 | } |
| 521 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 522 | static int |
| 523 | asn1c_copy_over(arg_t *arg, char *path) { |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 524 | #ifdef _WIN32 |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 525 | int use_real_copy = 1; |
| 526 | #else |
Lev Walkin | 2655eb3 | 2013-03-25 19:09:04 -0700 | [diff] [blame] | 527 | int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 528 | #endif |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 529 | |
Lev Walkin | a4f8e94 | 2017-10-08 19:28:20 -0700 | [diff] [blame] | 530 | const char *fname = a1c_basename(path); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 531 | if(!fname |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 532 | || (use_real_copy ? real_copy(path, fname) : symlink(path, fname)) |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 533 | ) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 534 | if(errno == EEXIST) { |
| 535 | struct stat sb1, sb2; |
| 536 | if(stat(path, &sb1) == 0 |
| 537 | && stat(fname, &sb2) == 0 |
| 538 | && sb1.st_dev == sb2.st_dev |
| 539 | && sb1.st_ino == sb2.st_ino) { |
| 540 | /* |
| 541 | * Nothing to do. |
| 542 | */ |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 543 | safe_fprintf(stderr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 544 | "File %s is already here as %s\n", |
| 545 | path, fname); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 546 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 547 | } else { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 548 | safe_fprintf(stderr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 549 | "Retaining local %s (%s suggested)\n", |
| 550 | fname, path); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 551 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 552 | } |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 553 | } else if(errno == ENOENT) { |
| 554 | /* Ignore this */ |
| 555 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 556 | } else { |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 557 | safe_fprintf(stderr, "%s %s -> %s failed: %s\n", |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 558 | use_real_copy ? "Copy" : "Symlink", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 559 | path, fname, strerror(errno)); |
| 560 | return -1; |
| 561 | } |
| 562 | } |
| 563 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 564 | safe_fprintf(stderr, "%s %s\t-> %s\n", |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 565 | use_real_copy ? "Copied" : "Symlinked", path, fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 566 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 567 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 570 | |
| 571 | static int |
| 572 | generate_pdu_collection_file(arg_t *arg) { |
| 573 | asn1p_module_t *mod; |
| 574 | FILE *fp; |
| 575 | |
| 576 | fp = asn1c_open_file("pdu_collection", ".c", 0); |
| 577 | if(fp == NULL) { |
| 578 | perror("pdu_collection.c"); |
| 579 | return -1; |
| 580 | } |
| 581 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 582 | safe_fprintf(fp, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 583 | "/*\n" |
| 584 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 585 | " */\n\n"); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 586 | safe_fprintf(fp, "struct asn_TYPE_descriptor_s;\t" |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 587 | "/* Forward declaration */\n\n"); |
| 588 | |
| 589 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 590 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 591 | if(!include_type_to_pdu_collection(arg)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 592 | continue; |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 593 | safe_fprintf(fp, "extern struct asn_TYPE_descriptor_s " |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 594 | "asn_DEF_%s;\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 595 | asn1c_make_identifier(0, arg->expr, NULL)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 599 | safe_fprintf(fp, "\n\n"); |
| 600 | safe_fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 601 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 602 | int mod_printed = 0; |
| 603 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 604 | if(!include_type_to_pdu_collection(arg)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 605 | continue; |
| 606 | if(!mod_printed++) |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 607 | safe_fprintf(fp, "\t/* From module %s in %s */\n", |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 608 | arg->expr->module->ModuleName, |
| 609 | arg->expr->module->source_file_name); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 610 | safe_fprintf(fp, "\t&asn_DEF_%s,\t\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 611 | asn1c_make_identifier(0, arg->expr, NULL)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 615 | safe_fprintf(fp, "\t0\n};\n\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 616 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 617 | pdu_collection_print_unused_types(arg); |
| 618 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 619 | fclose(fp); |
Lev Walkin | f2b2f37 | 2016-03-14 02:23:48 -0700 | [diff] [blame] | 620 | safe_fprintf(stderr, "Generated pdu_collection.c\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 621 | |
| 622 | return 0; |
| 623 | } |
| 624 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 625 | static struct PDUType { |
| 626 | char *typename; |
| 627 | int used; |
| 628 | } *pduType; |
| 629 | static int pduTypes; |
| 630 | |
| 631 | static const char * |
| 632 | generate_pdu_C_definition(void) { |
Lev Walkin | a4f3d46 | 2017-08-05 22:40:14 -0700 | [diff] [blame] | 633 | const char *src; |
| 634 | char *def; |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 635 | char *dst; |
Lev Walkin | a4f3d46 | 2017-08-05 22:40:14 -0700 | [diff] [blame] | 636 | if(pduTypes == 0) return ""; |
| 637 | def = malloc(strlen(pduType[0].typename) + 20); |
| 638 | assert(def); |
| 639 | strcpy(def, "-DPDU="); |
| 640 | for(src = pduType[0].typename, dst = def + 6; *src; src++, dst++) { |
| 641 | if((*dst = *src) == '-') { |
| 642 | *dst = '_'; |
| 643 | } |
| 644 | } |
| 645 | *dst++ = ' '; |
| 646 | *dst = 0; |
| 647 | return def; |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void |
| 651 | asn1c__add_pdu_type(const char *ctypename) { |
| 652 | char *typename = strdup(ctypename); |
| 653 | assert(typename && *typename); |
| 654 | |
| 655 | pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1)); |
| 656 | assert(pduType); |
| 657 | pduType[pduTypes].used = 0; |
| 658 | pduType[pduTypes].typename = typename; |
| 659 | pduTypes++; |
| 660 | } |
| 661 | |
Lev Walkin | 5230c64 | 2017-09-26 18:10:06 -0700 | [diff] [blame] | 662 | static void |
| 663 | asn1c__cleanup_pdu_type() { |
| 664 | int i; |
| 665 | for(i = 0; i < pduTypes; i++) { |
| 666 | free(pduType[i].typename); |
| 667 | } |
| 668 | free(pduType); |
| 669 | pduType = NULL; |
| 670 | pduTypes = 0; |
| 671 | } |
| 672 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 673 | static int |
| 674 | asn1c__pdu_type_lookup(const char *typename) { |
| 675 | int i; |
| 676 | for(i = 0; i < pduTypes; i++) { |
| 677 | struct PDUType *pt = &pduType[i]; |
| 678 | if(strcmp(pt->typename, typename) == 0) { |
| 679 | pt->used++; |
| 680 | return 1; |
| 681 | } |
| 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int |
| 687 | need_to_generate_pdu_collection(arg_t *arg) { |
| 688 | if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO)) |
| 689 | return 1; |
| 690 | if(arg->flags & A1C_PDU_TYPE) |
| 691 | return (pduTypes > 1) ? 1 : 0; |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static void |
| 696 | pdu_collection_print_unused_types(arg_t *arg) { |
| 697 | int i; |
| 698 | for(i = 0; i < pduTypes; i++) { |
| 699 | struct PDUType *pt = &pduType[i]; |
| 700 | if(!pt->used) { |
| 701 | WARNING("Missing type specified in -pdu=%s", |
| 702 | pt->typename); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | static int |
| 708 | include_type_to_pdu_collection(arg_t *arg) { |
| 709 | if(!asn1_lang_map[arg->expr->meta_type] |
| 710 | [arg->expr->expr_type].type_cb) |
| 711 | return 0; |
| 712 | |
Lev Walkin | 8c44dff | 2017-08-26 17:17:27 -0700 | [diff] [blame] | 713 | /* Parameterized types can't serve as PDU's without instantiation. */ |
| 714 | if(arg->expr->lhs_params) |
| 715 | return 0; |
| 716 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 717 | if((arg->flags & A1C_PDU_ALL) |
| 718 | || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced) |
| 719 | || asn1c__pdu_type_lookup(arg->expr->Identifier)) { |
| 720 | return 1; |
| 721 | } |
| 722 | |
| 723 | return 0; |
| 724 | } |