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