blob: 3fed83d5e4a32544662b20a0b7ec8a519c416f58 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin79f54952004-08-13 16:58:19 +00002#include "asn1c_compat.h"
Lev Walkinacd9f8b2004-08-19 13:29:03 +00003#include "asn1c_fdeps.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00004#include "asn1c_lang.h"
5#include "asn1c_misc.h"
6#include "asn1c_save.h"
7#include "asn1c_out.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00008
Lev Walkinc46b7cb2006-08-18 02:27:55 +00009static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000010static int asn1c_print_streams(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000011static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000012static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000013static int identical_files(const char *fname1, const char *fname2);
Lev Walkin59b176e2005-11-26 11:25:14 +000014static int generate_pdu_collection_file(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000015static int generate_preamble(arg_t *, FILE *, int optc, char **argv);
Lev Walkinf15320b2004-06-03 03:38:44 +000016
17int
Lev Walkin866cff12005-03-05 00:50:53 +000018asn1c_save_compiled_output(arg_t *arg, const char *datadir,
Lev Walkin8253ea92006-03-17 01:47:57 +000019 int argc, int optc, char **argv) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000020 asn1c_fdeps_t *deps = 0;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000021 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000022 asn1p_module_t *mod;
Lev Walkin59b176e2005-11-26 11:25:14 +000023 FILE *mkf; /* Makefile.am.sample */
Lev Walkin866cff12005-03-05 00:50:53 +000024 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000025
Lev Walkinacd9f8b2004-08-19 13:29:03 +000026 deps = asn1c_read_file_dependencies(arg, datadir);
27 if(!deps && datadir) {
28 WARNING("Cannot read file-dependencies information "
29 "from %s\n", datadir);
30 }
Lev Walkinf15320b2004-06-03 03:38:44 +000031
Lev Walkinb85a8132005-08-18 13:38:19 +000032 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
33 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000034 if(asn1_lang_map[arg->expr->meta_type]
35 [arg->expr->expr_type].type_cb) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +000036 if(asn1c_dump_streams(arg, deps, optc, argv))
Lev Walkinf15320b2004-06-03 03:38:44 +000037 return -1;
38 }
39 }
40 }
41
42 /*
43 * Dump out the Makefile template and the rest of the support code.
44 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000045 if((arg->flags & A1C_PRINT_COMPILED)
46 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
47 return 0; /* Finished */
48 }
Lev Walkinf15320b2004-06-03 03:38:44 +000049
Lev Walkin4604d032005-03-04 08:52:50 +000050 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000051 if(mkf == NULL) {
52 perror("Makefile.am.sample");
53 return -1;
54 }
Lev Walkinf15320b2004-06-03 03:38:44 +000055
Lev Walkinacd9f8b2004-08-19 13:29:03 +000056 fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000057 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
58 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000059 if(asn1_lang_map[arg->expr->meta_type]
60 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000061 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000062 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000063 }
64 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000065 }
Lev Walkind29bbce2004-09-23 22:19:14 +000066 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000067 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
68 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000069 if(asn1_lang_map[arg->expr->meta_type]
70 [arg->expr->expr_type].type_cb) {
71 fprintf(mkf, "\t\\\n\t%s.h",
72 arg->expr->Identifier);
73 }
74 }
75 }
76 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000077
Lev Walkinacd9f8b2004-08-19 13:29:03 +000078 /*
79 * Move necessary skeleton files and add them to Makefile.am.sample.
80 */
81 dlist = asn1c_deps_makelist(deps);
82 if(dlist) {
83 char buf[8129];
84 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000085 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000086
Lev Walkin4efbfb72005-02-25 14:20:30 +000087 assert(dlen < (sizeof(buf) / 2 - 2));
88 memcpy(buf, datadir, dlen);
89 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000090 *dir_end++ = '/';
91
92 for(i = 0; i < dlist->el_count; i++) {
93 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000094 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000095
96 assert(strlen(fname) < (sizeof(buf) / 2));
97 strcpy(dir_end, fname);
98
99 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000100 fprintf(mkf, ">>>ABORTED<<<");
101 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000102 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000103 }
104 dotH = strrchr(fname, 'h');
105 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
106 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000108 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000109 }
110 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000111 }
112
Lev Walkin59b176e2005-11-26 11:25:14 +0000113 if(arg->flags & A1C_PDU_AUTO) {
114 fprintf(mkf, "ASN_MODULE_SOURCES+=pdu_collection.c\n");
115 if(generate_pdu_collection_file(arg))
116 return -1;
117 }
118
Lev Walkind29bbce2004-09-23 22:19:14 +0000119 fprintf(mkf, "\n\n"
120 "lib_LTLIBRARIES=libsomething.la\n"
121 "libsomething_la_SOURCES="
122 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
123 "\n"
124 "# This file may be used as an input for make(3)\n"
125 "# Remove the lines below to convert it into a pure .am file\n"
126 "TARGET = progname\n"
127 "CFLAGS += -I.\n"
128 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
129 "\nall: $(TARGET)\n"
130 "\n$(TARGET): ${OBJS}"
131 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
132 "\n.SUFFIXES:"
133 "\n.SUFFIXES: .c .o\n"
134 "\n.c.o:"
135 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
136 "\nclean:"
137 "\n\trm -f $(TARGET)"
138 "\n\trm -f $(OBJS)\n"
Lev Walkin866cff12005-03-05 00:50:53 +0000139 "\nregen: regenerate-from-asn1-source\n"
140 "\nregenerate-from-asn1-source:\n\t"
Lev Walkind29bbce2004-09-23 22:19:14 +0000141 );
Lev Walkin866cff12005-03-05 00:50:53 +0000142
143 for(i = 0; i < argc; i++)
144 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
145 fprintf(mkf, "\n\n");
146
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000147 fclose(mkf);
148 fprintf(stderr, "Generated Makefile.am.sample\n");
149
Lev Walkinf15320b2004-06-03 03:38:44 +0000150 return 0;
151}
152
153/*
154 * Dump the streams.
155 */
156static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000157asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 if(arg->flags & A1C_PRINT_COMPILED) {
159 return asn1c_print_streams(arg);
160 } else {
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000161 return asn1c_save_streams(arg, deps, optc, argv);
Lev Walkinf15320b2004-06-03 03:38:44 +0000162 }
163}
164
165static int
166asn1c_print_streams(arg_t *arg) {
167 compiler_streams_t *cs = arg->expr->data;
168 asn1p_expr_t *expr = arg->expr;
169 int i;
170
Lev Walkin64399722004-08-11 07:17:22 +0000171 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000172 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000173 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 continue;
175
176 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
177 _compiler_stream2str[i],
178 expr->Identifier);
179
Lev Walkin59004fa2004-08-20 13:37:01 +0000180 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000181 fwrite(ot->buf, ot->len, 1, stdout);
182 }
183 }
184
185 return 0;
186}
187
188static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000189asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000190 asn1p_expr_t *expr = arg->expr;
191 compiler_streams_t *cs = expr->data;
192 out_chunk_t *ot;
193 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000194 char *tmpname_c, *tmpname_h;
195 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000196 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000197 const char *c_retained = "";
198 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
200 if(cs == NULL) {
201 fprintf(stderr, "Cannot compile %s at line %d\n",
202 expr->Identifier, expr->_lineno);
203 return -1;
204 }
205
Lev Walkin4604d032005-03-04 08:52:50 +0000206 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
207 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000209 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
210 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000211 return -1;
212 }
213
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000214 generate_preamble(arg, fp_c, optc, argv);
215 generate_preamble(arg, fp_h, optc, argv);
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000216
Lev Walkina00d6b32006-03-21 03:40:38 +0000217 header_id = asn1c_make_identifier(0, expr, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 fprintf(fp_h,
219 "#ifndef\t_%s_H_\n"
220 "#define\t_%s_H_\n"
221 "\n", header_id, header_id);
222
Lev Walkin3d551c02005-07-15 18:49:41 +0000223 fprintf(fp_h, "\n#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000224
Lev Walkinb9b8b952005-03-05 00:33:27 +0000225#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000226 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000227 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000228 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
229 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000230 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000231 } \
232} while(0)
233
Lev Walkinb9b8b952005-03-05 00:33:27 +0000234 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000235
236 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000237 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
238 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
239 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
240 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
Lev Walkin47370392006-02-19 04:30:15 +0000241 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000242
Lev Walkinae5540f2006-02-19 04:26:37 +0000243 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
244 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
245
Lev Walkin47370392006-02-19 04:30:15 +0000246 fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
247
Lev Walkin0ffabe22004-09-22 16:02:03 +0000248 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000249 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000250 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000251 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000252 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000253 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000254 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
255 fwrite(ot->buf, ot->len, 1, fp_c);
256 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000257 fwrite(ot->buf, ot->len, 1, fp_c);
258
Lev Walkinc8285712005-03-04 22:18:20 +0000259 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000260
261 fclose(fp_c);
262 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000263
264 name_buf = alloca(strlen(expr->Identifier) + 3);
265
266 sprintf(name_buf, "%s.c", expr->Identifier);
267 if(identical_files(name_buf, tmpname_c)) {
268 c_retained = " (contents unchanged)";
269 unlink(tmpname_c);
270 } else {
271 if(rename(tmpname_c, name_buf)) {
272 unlink(tmpname_c);
273 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000274 free(tmpname_c);
275 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000276 return -1;
277 }
278 }
279
280 sprintf(name_buf, "%s.h", expr->Identifier);
281 if(identical_files(name_buf, tmpname_h)) {
282 h_retained = " (contents unchanged)";
283 unlink(tmpname_h);
284 } else {
285 if(rename(tmpname_h, name_buf)) {
286 unlink(tmpname_h);
287 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000288 free(tmpname_c);
289 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000290 return -1;
291 }
292 }
293
294 free(tmpname_c);
295 free(tmpname_h);
296
297 fprintf(stderr, "Compiled %s.c%s\n",
298 expr->Identifier, c_retained);
299 fprintf(stderr, "Compiled %s.h%s\n",
300 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000301 return 0;
302}
303
Lev Walkin4604d032005-03-04 08:52:50 +0000304static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000305generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
Lev Walkin8253ea92006-03-17 01:47:57 +0000306 fprintf(fp,
307 "/*\n"
308 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
309 " * From ASN.1 module \"%s\"\n"
310 " * \tfound in \"%s\"\n",
311 arg->expr->module->ModuleName,
312 arg->expr->module->source_file_name);
313 if(optc > 1) {
314 int i;
315 fprintf(fp, " * \t`asn1c ");
316 for(i = 1; i < optc; i++)
317 fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
318 fprintf(fp, "`\n");
319 }
320 fprintf(fp, " */\n\n");
321 return 0;
322}
323
324static int
Lev Walkin4604d032005-03-04 08:52:50 +0000325identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000326 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000327 FILE *fp1, *fp2;
328 size_t olen, nlen;
329 int retval = 1; /* Files are identical */
330
Lev Walkina895afb2005-10-06 10:09:34 +0000331#ifndef WIN32
332 struct stat sb;
333
334 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
335 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
336 return 0; /* Files are not identical */
337 }
338#endif
339
Lev Walkin4604d032005-03-04 08:52:50 +0000340 fp1 = fopen(fname1, "r");
341 if(!fp1) { return 0; }
342 fp2 = fopen(fname2, "r");
343 if(!fp2) { fclose(fp1); return 0; }
344
345 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
346 nlen = fread(buf[1], 1, olen, fp2);
347 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
348 retval = 0;
349 break;
350 }
351 }
352 nlen = fread(buf[1], 1, 1, fp2);
353 if(nlen) retval = 0;
354
355 fclose(fp1);
356 fclose(fp2);
357 return retval;
358}
359
Lev Walkin4efbfb72005-02-25 14:20:30 +0000360/*
361 * Copy file for real.
362 */
363static int
364real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000365 unsigned char buf[4096];
366 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000367 FILE *fpsrc, *fpdst;
368 size_t len;
369 int retval = 0;
370
Lev Walkin4604d032005-03-04 08:52:50 +0000371 if(identical_files(src, dst))
372 return retval; /* Success, no need to copy for real. */
373
374 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000375 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000376 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000377 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
378
379 while(!feof(fpsrc)) {
380 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinc0e70712005-10-05 09:01:51 +0000381 if(fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000382 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000383 errno = EIO;
384 retval = -1;
385 break;
386 }
387 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000388 fclose(fpsrc);
389 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000390
391 /* Check if copied correctly, and rename into a permanent name */
392 if(retval) {
393 unlink(tmpname);
394 } else if(rename(tmpname, dst)) {
395 unlink(tmpname);
396 perror(tmpname);
397 retval = -1;
398 }
399 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000400 return retval;
401}
402
Lev Walkinf15320b2004-06-03 03:38:44 +0000403static int
404asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000405 char *fname;
Lev Walkina895afb2005-10-06 10:09:34 +0000406#ifdef WIN32
407 int use_real_copy = 1;
408#else
409 int use_real_copy = (arg->flags & A1C_SKELETONS_COPY);
410#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000411
Lev Walkin79f54952004-08-13 16:58:19 +0000412 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000413 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000414 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000415 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000416 if(errno == EEXIST) {
417 struct stat sb1, sb2;
418 if(stat(path, &sb1) == 0
419 && stat(fname, &sb2) == 0
420 && sb1.st_dev == sb2.st_dev
421 && sb1.st_ino == sb2.st_ino) {
422 /*
423 * Nothing to do.
424 */
425 fprintf(stderr,
426 "File %s is already here as %s\n",
427 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000428 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 } else {
430 fprintf(stderr,
431 "Retaining local %s (%s suggested)\n",
432 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000433 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000435 } else if(errno == ENOENT) {
436 /* Ignore this */
437 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000438 } else {
Lev Walkina895afb2005-10-06 10:09:34 +0000439 fprintf(stderr, "%s %s -> %s failed: %s\n",
440 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 path, fname, strerror(errno));
442 return -1;
443 }
444 }
445
Lev Walkina895afb2005-10-06 10:09:34 +0000446 fprintf(stderr, "%s %s\t-> %s\n",
447 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000448
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000449 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000450}
451
Lev Walkin59b176e2005-11-26 11:25:14 +0000452
453static int
454generate_pdu_collection_file(arg_t *arg) {
455 asn1p_module_t *mod;
456 FILE *fp;
457
458 fp = asn1c_open_file("pdu_collection", ".c", 0);
459 if(fp == NULL) {
460 perror("pdu_collection.c");
461 return -1;
462 }
463
464 fprintf(fp,
465 "/*\n"
466 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
467 " */\n\n");
468 fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
469 "/* Forward declaration */\n\n");
470
471 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
472 TQ_FOR(arg->expr, &(mod->members), next) {
473 if(arg->expr->_type_referenced
474 || !asn1_lang_map[arg->expr->meta_type]
475 [arg->expr->expr_type].type_cb)
476 continue;
477 fprintf(fp, "extern struct asn_TYPE_descriptor_s "
478 "asn_DEF_%s;\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000479 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000480 }
481 }
482
483 fprintf(fp, "\n\n");
484 fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
485 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
486 int mod_printed = 0;
487 TQ_FOR(arg->expr, &(mod->members), next) {
488 if(arg->expr->_type_referenced
489 || !asn1_lang_map[arg->expr->meta_type]
490 [arg->expr->expr_type].type_cb)
491 continue;
492 if(!mod_printed++)
493 fprintf(fp, "\t/* From module %s in %s */\n",
494 arg->expr->module->ModuleName,
495 arg->expr->module->source_file_name);
496 fprintf(fp, "\t&asn_DEF_%s,\t\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000497 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000498 }
499 }
500
501 fprintf(fp, "\t0\n};\n\n");
502
503 fclose(fp);
504 fprintf(stderr, "Generated pdu_collection.c\n");
505
506 return 0;
507}
508