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