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