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 | |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 9 | 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] | 10 | static int asn1c_print_streams(arg_t *arg); |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 11 | 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] | 12 | static int asn1c_copy_over(arg_t *arg, char *path); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 13 | static int identical_files(const char *fname1, const char *fname2); |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 14 | static int need_to_generate_pdu_collection(arg_t *arg); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 15 | static int generate_pdu_collection_file(arg_t *arg); |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 16 | static int generate_preamble(arg_t *, FILE *, int optc, char **argv); |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 17 | static int include_type_to_pdu_collection(arg_t *arg); |
| 18 | static void pdu_collection_print_unused_types(arg_t *arg); |
| 19 | static const char *generate_pdu_C_definition(void); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | |
| 21 | int |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 22 | asn1c_save_compiled_output(arg_t *arg, const char *datadir, |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 23 | int argc, int optc, char **argv) { |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 24 | asn1c_fdeps_t *deps = 0; |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 25 | asn1c_fdeps_t *dlist; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 26 | asn1p_module_t *mod; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 27 | FILE *mkf; /* Makefile.am.sample */ |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 28 | int i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 29 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 30 | deps = asn1c_read_file_dependencies(arg, datadir); |
| 31 | if(!deps && datadir) { |
| 32 | WARNING("Cannot read file-dependencies information " |
| 33 | "from %s\n", datadir); |
| 34 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 35 | |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 36 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 37 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | if(asn1_lang_map[arg->expr->meta_type] |
| 39 | [arg->expr->expr_type].type_cb) { |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 40 | if(asn1c_dump_streams(arg, deps, optc, argv)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Dump out the Makefile template and the rest of the support code. |
| 48 | */ |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 49 | if((arg->flags & A1C_PRINT_COMPILED) |
| 50 | || (arg->flags & A1C_OMIT_SUPPORT_CODE)) { |
| 51 | return 0; /* Finished */ |
| 52 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 54 | mkf = asn1c_open_file("Makefile.am", ".sample", 0); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 55 | if(mkf == NULL) { |
| 56 | perror("Makefile.am.sample"); |
| 57 | return -1; |
| 58 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 60 | 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 | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 65 | fprintf(mkf, "\t\\\n\t%s.c", |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 66 | arg->expr->Identifier); |
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 | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 70 | 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) { |
| 75 | fprintf(mkf, "\t\\\n\t%s.h", |
| 76 | arg->expr->Identifier); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | 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 | /* |
| 83 | * Move necessary skeleton files and add them to Makefile.am.sample. |
| 84 | */ |
| 85 | dlist = asn1c_deps_makelist(deps); |
| 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 | |
| 96 | for(i = 0; i < dlist->el_count; i++) { |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 97 | char *what_class; /* MODULE or CONVERTER */ |
| 98 | char *what_kind; /* HEADERS or SOURCES */ |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 99 | char *fname = dlist->elements[i]->filename; |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 100 | char *dotH; |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 101 | |
| 102 | assert(strlen(fname) < (sizeof(buf) / 2)); |
| 103 | strcpy(dir_end, fname); |
| 104 | |
| 105 | if(asn1c_copy_over(arg, buf) == -1) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | fprintf(mkf, ">>>ABORTED<<<"); |
| 107 | fclose(mkf); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 108 | return -1; |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 109 | } |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 110 | |
| 111 | /* MODULE data versus CONVERTER data */ |
| 112 | switch(dlist->elements[i]->usage) { |
| 113 | case FDEP_CONVERTER: what_class = "CONVERTER"; break; |
| 114 | default: what_class= "MODULE"; break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 115 | } |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 116 | |
| 117 | /* HEADERS versus SOURCES */ |
| 118 | dotH = strrchr(fname, 'h'); |
| 119 | if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) |
| 120 | what_kind = "HEADERS"; |
| 121 | else |
| 122 | what_kind = "SOURCES"; |
| 123 | fprintf(mkf, "ASN_%s_%s+=%s\n", |
| 124 | what_class, what_kind, fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 128 | if(need_to_generate_pdu_collection(arg)) { |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 129 | fprintf(mkf, "ASN_CONVERTER_SOURCES+=pdu_collection.c\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 130 | if(generate_pdu_collection_file(arg)) |
| 131 | return -1; |
| 132 | } |
| 133 | |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 134 | fprintf(mkf, "\n\n" |
| 135 | "lib_LTLIBRARIES=libsomething.la\n" |
| 136 | "libsomething_la_SOURCES=" |
| 137 | "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n" |
| 138 | "\n" |
| 139 | "# This file may be used as an input for make(3)\n" |
| 140 | "# Remove the lines below to convert it into a pure .am file\n" |
| 141 | "TARGET = progname\n" |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 142 | "CFLAGS +=%s%s -I.\n" |
Lev Walkin | f218e78 | 2006-09-12 06:21:18 +0000 | [diff] [blame] | 143 | "OBJS=${ASN_MODULE_SOURCES:.c=.o}" |
| 144 | " ${ASN_CONVERTER_SOURCES:.c=.o}\n" |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 145 | "\nall: $(TARGET)\n" |
| 146 | "\n$(TARGET): ${OBJS}" |
| 147 | "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n" |
| 148 | "\n.SUFFIXES:" |
| 149 | "\n.SUFFIXES: .c .o\n" |
| 150 | "\n.c.o:" |
| 151 | "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n" |
| 152 | "\nclean:" |
| 153 | "\n\trm -f $(TARGET)" |
| 154 | "\n\trm -f $(OBJS)\n" |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 155 | "\nregen: regenerate-from-asn1-source\n" |
| 156 | "\nregenerate-from-asn1-source:\n\t" |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 157 | , (arg->flags & A1C_PDU_TYPE) |
| 158 | ? generate_pdu_C_definition() : "" |
| 159 | , need_to_generate_pdu_collection(arg) |
| 160 | ? " -DASN_PDU_COLLECTION" : "" |
Lev Walkin | d29bbce | 2004-09-23 22:19:14 +0000 | [diff] [blame] | 161 | ); |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 162 | |
| 163 | for(i = 0; i < argc; i++) |
| 164 | fprintf(mkf, "%s%s", i ? " " : "", argv[i]); |
| 165 | fprintf(mkf, "\n\n"); |
| 166 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 167 | fclose(mkf); |
| 168 | fprintf(stderr, "Generated Makefile.am.sample\n"); |
| 169 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
| 174 | * Dump the streams. |
| 175 | */ |
| 176 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 177 | 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] | 178 | if(arg->flags & A1C_PRINT_COMPILED) { |
| 179 | return asn1c_print_streams(arg); |
| 180 | } else { |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 181 | return asn1c_save_streams(arg, deps, optc, argv); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | static int |
| 186 | asn1c_print_streams(arg_t *arg) { |
| 187 | compiler_streams_t *cs = arg->expr->data; |
| 188 | asn1p_expr_t *expr = arg->expr; |
| 189 | int i; |
| 190 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 191 | for(i = 1; i < OT_MAX; i++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | out_chunk_t *ot; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 193 | if(TQ_FIRST(&cs->destination[i].chunks) == NULL) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 194 | continue; |
| 195 | |
| 196 | printf("\n/*** <<< %s [%s] >>> ***/\n\n", |
| 197 | _compiler_stream2str[i], |
| 198 | expr->Identifier); |
| 199 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 200 | TQ_FOR(ot, &(cs->destination[i].chunks), next) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 201 | fwrite(ot->buf, ot->len, 1, stdout); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 209 | 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] | 210 | asn1p_expr_t *expr = arg->expr; |
| 211 | compiler_streams_t *cs = expr->data; |
| 212 | out_chunk_t *ot; |
| 213 | FILE *fp_c, *fp_h; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 214 | char *tmpname_c, *tmpname_h; |
| 215 | char *name_buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 216 | char *header_id; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 217 | const char *c_retained = ""; |
| 218 | const char *h_retained = ""; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | |
| 220 | if(cs == NULL) { |
| 221 | fprintf(stderr, "Cannot compile %s at line %d\n", |
| 222 | expr->Identifier, expr->_lineno); |
| 223 | return -1; |
| 224 | } |
| 225 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 226 | fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c); |
| 227 | fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 228 | if(fp_c == NULL || fp_h == NULL) { |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 229 | if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); } |
| 230 | if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | return -1; |
| 232 | } |
| 233 | |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 234 | generate_preamble(arg, fp_c, optc, argv); |
| 235 | generate_preamble(arg, fp_h, optc, argv); |
Lev Walkin | c3b8f6d | 2004-06-03 05:06:25 +0000 | [diff] [blame] | 236 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 237 | header_id = asn1c_make_identifier(0, expr, NULL); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 238 | fprintf(fp_h, |
| 239 | "#ifndef\t_%s_H_\n" |
| 240 | "#define\t_%s_H_\n" |
| 241 | "\n", header_id, header_id); |
| 242 | |
Lev Walkin | 3d551c0 | 2005-07-15 18:49:41 +0000 | [diff] [blame] | 243 | fprintf(fp_h, "\n#include <asn_application.h>\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 244 | |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 245 | #define SAVE_STREAM(fp, idx, msg, actdep) do { \ |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 246 | if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \ |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 247 | fprintf(fp, "\n/* %s */\n", msg); \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 248 | TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \ |
| 249 | if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \ |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 250 | fwrite(ot->buf, ot->len, 1, fp); \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 251 | } \ |
| 252 | } while(0) |
| 253 | |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 254 | SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1); |
Lev Walkin | 3d551c0 | 2005-07-15 18:49:41 +0000 | [diff] [blame] | 255 | |
| 256 | fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 257 | SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0); |
| 258 | SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0); |
| 259 | SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0); |
| 260 | SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0); |
Lev Walkin | 4737039 | 2006-02-19 04:30:15 +0000 | [diff] [blame] | 261 | fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 262 | |
Lev Walkin | ae5540f | 2006-02-19 04:26:37 +0000 | [diff] [blame] | 263 | if(!(arg->flags & A1C_NO_INCLUDE_DEPS)) |
| 264 | SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1); |
| 265 | |
Lev Walkin | 4737039 | 2006-02-19 04:30:15 +0000 | [diff] [blame] | 266 | fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id); |
| 267 | |
Lev Walkin | 0ffabe2 | 2004-09-22 16:02:03 +0000 | [diff] [blame] | 268 | fprintf(fp_c, "#include <asn_internal.h>\n\n"); |
Lev Walkin | e8318b8 | 2005-03-06 09:29:03 +0000 | [diff] [blame] | 269 | fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier); |
Lev Walkin | b9b8b95 | 2005-03-05 00:33:27 +0000 | [diff] [blame] | 270 | if(arg->flags & A1C_NO_INCLUDE_DEPS) |
Lev Walkin | 866cff1 | 2005-03-05 00:50:53 +0000 | [diff] [blame] | 271 | SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 272 | TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 273 | fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 274 | TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next) |
| 275 | fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 276 | TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next) |
| 277 | fwrite(ot->buf, ot->len, 1, fp_c); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 278 | TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 279 | fwrite(ot->buf, ot->len, 1, fp_c); |
| 280 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 281 | assert(OT_MAX == 11); /* Protection from reckless changes */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 282 | |
| 283 | fclose(fp_c); |
| 284 | fclose(fp_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 285 | |
| 286 | name_buf = alloca(strlen(expr->Identifier) + 3); |
| 287 | |
| 288 | sprintf(name_buf, "%s.c", expr->Identifier); |
| 289 | if(identical_files(name_buf, tmpname_c)) { |
| 290 | c_retained = " (contents unchanged)"; |
| 291 | unlink(tmpname_c); |
| 292 | } else { |
| 293 | if(rename(tmpname_c, name_buf)) { |
| 294 | unlink(tmpname_c); |
| 295 | perror(tmpname_c); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 296 | free(tmpname_c); |
| 297 | free(tmpname_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | sprintf(name_buf, "%s.h", expr->Identifier); |
| 303 | if(identical_files(name_buf, tmpname_h)) { |
| 304 | h_retained = " (contents unchanged)"; |
| 305 | unlink(tmpname_h); |
| 306 | } else { |
| 307 | if(rename(tmpname_h, name_buf)) { |
| 308 | unlink(tmpname_h); |
| 309 | perror(tmpname_h); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 310 | free(tmpname_c); |
| 311 | free(tmpname_h); |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | free(tmpname_c); |
| 317 | free(tmpname_h); |
| 318 | |
| 319 | fprintf(stderr, "Compiled %s.c%s\n", |
| 320 | expr->Identifier, c_retained); |
| 321 | fprintf(stderr, "Compiled %s.h%s\n", |
| 322 | expr->Identifier, h_retained); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 326 | static int |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 327 | generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) { |
Lev Walkin | 8253ea9 | 2006-03-17 01:47:57 +0000 | [diff] [blame] | 328 | fprintf(fp, |
| 329 | "/*\n" |
| 330 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 331 | " * From ASN.1 module \"%s\"\n" |
| 332 | " * \tfound in \"%s\"\n", |
| 333 | arg->expr->module->ModuleName, |
| 334 | arg->expr->module->source_file_name); |
| 335 | if(optc > 1) { |
| 336 | int i; |
| 337 | fprintf(fp, " * \t`asn1c "); |
| 338 | for(i = 1; i < optc; i++) |
| 339 | fprintf(fp, "%s%s", i>1?" ":"", argv[i]); |
| 340 | fprintf(fp, "`\n"); |
| 341 | } |
| 342 | fprintf(fp, " */\n\n"); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static int |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 347 | identical_files(const char *fname1, const char *fname2) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 348 | char buf[2][4096]; |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 349 | FILE *fp1, *fp2; |
| 350 | size_t olen, nlen; |
| 351 | int retval = 1; /* Files are identical */ |
| 352 | |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 353 | #ifndef WIN32 |
| 354 | struct stat sb; |
| 355 | |
| 356 | if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode) |
| 357 | || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) { |
| 358 | return 0; /* Files are not identical */ |
| 359 | } |
| 360 | #endif |
| 361 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 362 | fp1 = fopen(fname1, "r"); |
| 363 | if(!fp1) { return 0; } |
| 364 | fp2 = fopen(fname2, "r"); |
| 365 | if(!fp2) { fclose(fp1); return 0; } |
| 366 | |
| 367 | while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) { |
| 368 | nlen = fread(buf[1], 1, olen, fp2); |
| 369 | if(nlen != olen || memcmp(buf[0], buf[1], nlen)) { |
| 370 | retval = 0; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | nlen = fread(buf[1], 1, 1, fp2); |
| 375 | if(nlen) retval = 0; |
| 376 | |
| 377 | fclose(fp1); |
| 378 | fclose(fp2); |
| 379 | return retval; |
| 380 | } |
| 381 | |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 382 | /* |
| 383 | * Copy file for real. |
| 384 | */ |
| 385 | static int |
| 386 | real_copy(const char *src, const char *dst) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 387 | unsigned char buf[4096]; |
| 388 | char *tmpname; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 389 | FILE *fpsrc, *fpdst; |
| 390 | size_t len; |
| 391 | int retval = 0; |
| 392 | |
Lev Walkin | 4604d03 | 2005-03-04 08:52:50 +0000 | [diff] [blame] | 393 | if(identical_files(src, dst)) |
| 394 | return retval; /* Success, no need to copy for real. */ |
| 395 | |
| 396 | fpsrc = fopen(src, "r"); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 397 | if(!fpsrc) { errno = EIO; return -1; } |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 398 | fpdst = asn1c_open_file(dst, "", &tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 399 | if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; } |
| 400 | |
| 401 | while(!feof(fpsrc)) { |
| 402 | len = fread(buf, 1, sizeof(buf), fpsrc); |
Lev Walkin | c0e7071 | 2005-10-05 09:01:51 +0000 | [diff] [blame] | 403 | if(fwrite(buf, 1, len, fpdst) != len) { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 404 | perror(tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 405 | errno = EIO; |
| 406 | retval = -1; |
| 407 | break; |
| 408 | } |
| 409 | } |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 410 | fclose(fpsrc); |
| 411 | fclose(fpdst); |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 412 | |
| 413 | /* Check if copied correctly, and rename into a permanent name */ |
| 414 | if(retval) { |
| 415 | unlink(tmpname); |
| 416 | } else if(rename(tmpname, dst)) { |
| 417 | unlink(tmpname); |
| 418 | perror(tmpname); |
| 419 | retval = -1; |
| 420 | } |
| 421 | free(tmpname); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 422 | return retval; |
| 423 | } |
| 424 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 425 | static int |
| 426 | asn1c_copy_over(arg_t *arg, char *path) { |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 427 | char *fname; |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 428 | #ifdef WIN32 |
| 429 | int use_real_copy = 1; |
| 430 | #else |
| 431 | int use_real_copy = (arg->flags & A1C_SKELETONS_COPY); |
| 432 | #endif |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 433 | |
Lev Walkin | 79f5495 | 2004-08-13 16:58:19 +0000 | [diff] [blame] | 434 | fname = a1c_basename(path); |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 435 | if(!fname |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 436 | || (use_real_copy ? real_copy(path, fname) : symlink(path, fname)) |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 437 | ) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 438 | if(errno == EEXIST) { |
| 439 | struct stat sb1, sb2; |
| 440 | if(stat(path, &sb1) == 0 |
| 441 | && stat(fname, &sb2) == 0 |
| 442 | && sb1.st_dev == sb2.st_dev |
| 443 | && sb1.st_ino == sb2.st_ino) { |
| 444 | /* |
| 445 | * Nothing to do. |
| 446 | */ |
| 447 | fprintf(stderr, |
| 448 | "File %s is already here as %s\n", |
| 449 | path, fname); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 450 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 451 | } else { |
| 452 | fprintf(stderr, |
| 453 | "Retaining local %s (%s suggested)\n", |
| 454 | fname, path); |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 455 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 456 | } |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 457 | } else if(errno == ENOENT) { |
| 458 | /* Ignore this */ |
| 459 | return 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 460 | } else { |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 461 | fprintf(stderr, "%s %s -> %s failed: %s\n", |
| 462 | use_real_copy ? "Copy" : "Symlink", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 463 | path, fname, strerror(errno)); |
| 464 | return -1; |
| 465 | } |
| 466 | } |
| 467 | |
Lev Walkin | a895afb | 2005-10-06 10:09:34 +0000 | [diff] [blame] | 468 | fprintf(stderr, "%s %s\t-> %s\n", |
| 469 | use_real_copy ? "Copied" : "Symlinked", path, fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 470 | |
Lev Walkin | acd9f8b | 2004-08-19 13:29:03 +0000 | [diff] [blame] | 471 | return 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 474 | |
| 475 | static int |
| 476 | generate_pdu_collection_file(arg_t *arg) { |
| 477 | asn1p_module_t *mod; |
| 478 | FILE *fp; |
| 479 | |
| 480 | fp = asn1c_open_file("pdu_collection", ".c", 0); |
| 481 | if(fp == NULL) { |
| 482 | perror("pdu_collection.c"); |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | fprintf(fp, |
| 487 | "/*\n" |
| 488 | " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n" |
| 489 | " */\n\n"); |
| 490 | fprintf(fp, "struct asn_TYPE_descriptor_s;\t" |
| 491 | "/* Forward declaration */\n\n"); |
| 492 | |
| 493 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 494 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 495 | if(!include_type_to_pdu_collection(arg)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 496 | continue; |
| 497 | fprintf(fp, "extern struct asn_TYPE_descriptor_s " |
| 498 | "asn_DEF_%s;\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 499 | asn1c_make_identifier(0, arg->expr, NULL)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
| 503 | fprintf(fp, "\n\n"); |
| 504 | fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n"); |
| 505 | TQ_FOR(mod, &(arg->asn->modules), mod_next) { |
| 506 | int mod_printed = 0; |
| 507 | TQ_FOR(arg->expr, &(mod->members), next) { |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 508 | if(!include_type_to_pdu_collection(arg)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 509 | continue; |
| 510 | if(!mod_printed++) |
| 511 | fprintf(fp, "\t/* From module %s in %s */\n", |
| 512 | arg->expr->module->ModuleName, |
| 513 | arg->expr->module->source_file_name); |
| 514 | fprintf(fp, "\t&asn_DEF_%s,\t\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 515 | asn1c_make_identifier(0, arg->expr, NULL)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | fprintf(fp, "\t0\n};\n\n"); |
| 520 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 521 | pdu_collection_print_unused_types(arg); |
| 522 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 523 | fclose(fp); |
| 524 | fprintf(stderr, "Generated pdu_collection.c\n"); |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Lev Walkin | 66adab4 | 2006-09-23 02:52:12 +0000 | [diff] [blame] | 529 | static struct PDUType { |
| 530 | char *typename; |
| 531 | int used; |
| 532 | } *pduType; |
| 533 | static int pduTypes; |
| 534 | |
| 535 | static const char * |
| 536 | generate_pdu_C_definition(void) { |
| 537 | const char *src; |
| 538 | char *def; |
| 539 | char *dst; |
| 540 | if(pduTypes == 0) return ""; |
| 541 | def = malloc(strlen(pduType[0].typename) + 20); |
| 542 | strcpy(def, " -DPDU="); |
| 543 | for(src = pduType[0].typename, dst = def + 7; *src; src++, dst++) |
| 544 | if((*dst = *src) == '-') |
| 545 | *dst = '_'; |
| 546 | *dst = 0; |
| 547 | return def; |
| 548 | } |
| 549 | |
| 550 | void |
| 551 | asn1c__add_pdu_type(const char *ctypename) { |
| 552 | char *typename = strdup(ctypename); |
| 553 | assert(typename && *typename); |
| 554 | |
| 555 | pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1)); |
| 556 | assert(pduType); |
| 557 | pduType[pduTypes].used = 0; |
| 558 | pduType[pduTypes].typename = typename; |
| 559 | pduTypes++; |
| 560 | } |
| 561 | |
| 562 | static int |
| 563 | asn1c__pdu_type_lookup(const char *typename) { |
| 564 | int i; |
| 565 | for(i = 0; i < pduTypes; i++) { |
| 566 | struct PDUType *pt = &pduType[i]; |
| 567 | if(strcmp(pt->typename, typename) == 0) { |
| 568 | pt->used++; |
| 569 | return 1; |
| 570 | } |
| 571 | } |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static int |
| 576 | need_to_generate_pdu_collection(arg_t *arg) { |
| 577 | if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO)) |
| 578 | return 1; |
| 579 | if(arg->flags & A1C_PDU_TYPE) |
| 580 | return (pduTypes > 1) ? 1 : 0; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static void |
| 585 | pdu_collection_print_unused_types(arg_t *arg) { |
| 586 | int i; |
| 587 | for(i = 0; i < pduTypes; i++) { |
| 588 | struct PDUType *pt = &pduType[i]; |
| 589 | if(!pt->used) { |
| 590 | WARNING("Missing type specified in -pdu=%s", |
| 591 | pt->typename); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static int |
| 597 | include_type_to_pdu_collection(arg_t *arg) { |
| 598 | if(!asn1_lang_map[arg->expr->meta_type] |
| 599 | [arg->expr->expr_type].type_cb) |
| 600 | return 0; |
| 601 | |
| 602 | if((arg->flags & A1C_PDU_ALL) |
| 603 | || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced) |
| 604 | || asn1c__pdu_type_lookup(arg->expr->Identifier)) { |
| 605 | return 1; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | } |