blob: d1876eedebdd4c787331d98807a5978ee9f517ba [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 Walkinacd9f8b2004-08-19 13:29:03 +00009static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000010static int asn1c_print_streams(arg_t *arg);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000011static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *);
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 Walkinf15320b2004-06-03 03:38:44 +000015
16int
Lev Walkin866cff12005-03-05 00:50:53 +000017asn1c_save_compiled_output(arg_t *arg, const char *datadir,
18 int argc, char **argv) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000019 asn1c_fdeps_t *deps = 0;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000020 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000021 asn1p_module_t *mod;
Lev Walkin59b176e2005-11-26 11:25:14 +000022 FILE *mkf; /* Makefile.am.sample */
Lev Walkin866cff12005-03-05 00:50:53 +000023 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000024
Lev Walkinacd9f8b2004-08-19 13:29:03 +000025 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 Walkinf15320b2004-06-03 03:38:44 +000030
Lev Walkinb85a8132005-08-18 13:38:19 +000031 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
32 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000033 if(asn1_lang_map[arg->expr->meta_type]
34 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000035 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000036 return -1;
37 }
38 }
39 }
40
41 /*
42 * Dump out the Makefile template and the rest of the support code.
43 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000044 if((arg->flags & A1C_PRINT_COMPILED)
45 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
46 return 0; /* Finished */
47 }
Lev Walkinf15320b2004-06-03 03:38:44 +000048
Lev Walkin4604d032005-03-04 08:52:50 +000049 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000050 if(mkf == NULL) {
51 perror("Makefile.am.sample");
52 return -1;
53 }
Lev Walkinf15320b2004-06-03 03:38:44 +000054
Lev Walkinacd9f8b2004-08-19 13:29:03 +000055 fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000056 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
57 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000058 if(asn1_lang_map[arg->expr->meta_type]
59 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000060 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000061 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000062 }
63 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000064 }
Lev Walkind29bbce2004-09-23 22:19:14 +000065 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000066 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
67 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000068 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 Walkinf15320b2004-06-03 03:38:44 +000076
Lev Walkinacd9f8b2004-08-19 13:29:03 +000077 /*
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 Walkin4efbfb72005-02-25 14:20:30 +000084 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000085
Lev Walkin4efbfb72005-02-25 14:20:30 +000086 assert(dlen < (sizeof(buf) / 2 - 2));
87 memcpy(buf, datadir, dlen);
88 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000089 *dir_end++ = '/';
90
91 for(i = 0; i < dlist->el_count; i++) {
92 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000093 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000094
95 assert(strlen(fname) < (sizeof(buf) / 2));
96 strcpy(dir_end, fname);
97
98 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000099 fprintf(mkf, ">>>ABORTED<<<");
100 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000101 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000102 }
103 dotH = strrchr(fname, 'h');
104 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
105 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000107 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000108 }
109 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 }
111
Lev Walkin59b176e2005-11-26 11:25:14 +0000112 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 Walkind29bbce2004-09-23 22:19:14 +0000118 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 Walkin866cff12005-03-05 00:50:53 +0000138 "\nregen: regenerate-from-asn1-source\n"
139 "\nregenerate-from-asn1-source:\n\t"
Lev Walkind29bbce2004-09-23 22:19:14 +0000140 );
Lev Walkin866cff12005-03-05 00:50:53 +0000141
142 for(i = 0; i < argc; i++)
143 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
144 fprintf(mkf, "\n\n");
145
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000146 fclose(mkf);
147 fprintf(stderr, "Generated Makefile.am.sample\n");
148
Lev Walkinf15320b2004-06-03 03:38:44 +0000149 return 0;
150}
151
152/*
153 * Dump the streams.
154 */
155static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000156asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000157 if(arg->flags & A1C_PRINT_COMPILED) {
158 return asn1c_print_streams(arg);
159 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000160 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000161 }
162}
163
164static int
165asn1c_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 Walkin64399722004-08-11 07:17:22 +0000170 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000171 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000172 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000173 continue;
174
175 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
176 _compiler_stream2str[i],
177 expr->Identifier);
178
Lev Walkin59004fa2004-08-20 13:37:01 +0000179 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 fwrite(ot->buf, ot->len, 1, stdout);
181 }
182 }
183
184 return 0;
185}
186
187static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000188asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000189 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 Walkin4604d032005-03-04 08:52:50 +0000193 char *tmpname_c, *tmpname_h;
194 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000196 const char *c_retained = "";
197 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000198
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 Walkin4604d032005-03-04 08:52:50 +0000205 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
206 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000207 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000208 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 Walkinf15320b2004-06-03 03:38:44 +0000210 return -1;
211 }
212
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000213 fprintf(fp_c,
214 "/*\n"
215 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000216 " * From ASN.1 module \"%s\"\n"
217 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000218 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000219 expr->module->ModuleName,
220 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000221 );
222 fprintf(fp_h,
223 "/*\n"
224 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000225 " * From ASN.1 module \"%s\"\n"
226 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000227 " */\n\n",
Lev Walkinb85a8132005-08-18 13:38:19 +0000228 expr->module->ModuleName,
229 expr->module->source_file_name
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000230 );
231
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000232 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 fprintf(fp_h,
234 "#ifndef\t_%s_H_\n"
235 "#define\t_%s_H_\n"
236 "\n", header_id, header_id);
237
Lev Walkin3d551c02005-07-15 18:49:41 +0000238 fprintf(fp_h, "\n#include <asn_application.h>\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000239
Lev Walkinb9b8b952005-03-05 00:33:27 +0000240#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000241 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000242 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000243 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
244 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000245 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000246 } \
247} while(0)
248
Lev Walkinb9b8b952005-03-05 00:33:27 +0000249 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000250
251 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000252 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 Walkin3dcaafa2004-08-11 05:21:32 +0000256 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
257 "#endif\t/* _%s_H_ */\n",
258 header_id);
259
Lev Walkinae5540f2006-02-19 04:26:37 +0000260 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
261 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
262
Lev Walkin0ffabe22004-09-22 16:02:03 +0000263 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000264 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000265 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000266 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000267 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000269 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 Walkinf15320b2004-06-03 03:38:44 +0000272 fwrite(ot->buf, ot->len, 1, fp_c);
273
Lev Walkinc8285712005-03-04 22:18:20 +0000274 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000275
276 fclose(fp_c);
277 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000278
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 Walkina895afb2005-10-06 10:09:34 +0000289 free(tmpname_c);
290 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000291 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 Walkina895afb2005-10-06 10:09:34 +0000303 free(tmpname_c);
304 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000305 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 Walkinf15320b2004-06-03 03:38:44 +0000316 return 0;
317}
318
Lev Walkin4604d032005-03-04 08:52:50 +0000319static int
320identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000321 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000322 FILE *fp1, *fp2;
323 size_t olen, nlen;
324 int retval = 1; /* Files are identical */
325
Lev Walkina895afb2005-10-06 10:09:34 +0000326#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 Walkin4604d032005-03-04 08:52:50 +0000335 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 Walkin4efbfb72005-02-25 14:20:30 +0000355/*
356 * Copy file for real.
357 */
358static int
359real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000360 unsigned char buf[4096];
361 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000362 FILE *fpsrc, *fpdst;
363 size_t len;
364 int retval = 0;
365
Lev Walkin4604d032005-03-04 08:52:50 +0000366 if(identical_files(src, dst))
367 return retval; /* Success, no need to copy for real. */
368
369 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000370 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000371 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000372 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
373
374 while(!feof(fpsrc)) {
375 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinc0e70712005-10-05 09:01:51 +0000376 if(fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000377 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000378 errno = EIO;
379 retval = -1;
380 break;
381 }
382 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000383 fclose(fpsrc);
384 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000385
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 Walkin4efbfb72005-02-25 14:20:30 +0000395 return retval;
396}
397
Lev Walkinf15320b2004-06-03 03:38:44 +0000398static int
399asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000400 char *fname;
Lev Walkina895afb2005-10-06 10:09:34 +0000401#ifdef WIN32
402 int use_real_copy = 1;
403#else
404 int use_real_copy = (arg->flags & A1C_SKELETONS_COPY);
405#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000406
Lev Walkin79f54952004-08-13 16:58:19 +0000407 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000408 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000409 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000410 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000411 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 Walkinacd9f8b2004-08-19 13:29:03 +0000423 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000424 } else {
425 fprintf(stderr,
426 "Retaining local %s (%s suggested)\n",
427 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000428 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000429 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000430 } else if(errno == ENOENT) {
431 /* Ignore this */
432 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000433 } else {
Lev Walkina895afb2005-10-06 10:09:34 +0000434 fprintf(stderr, "%s %s -> %s failed: %s\n",
435 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000436 path, fname, strerror(errno));
437 return -1;
438 }
439 }
440
Lev Walkina895afb2005-10-06 10:09:34 +0000441 fprintf(stderr, "%s %s\t-> %s\n",
442 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000443
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000444 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445}
446
Lev Walkin59b176e2005-11-26 11:25:14 +0000447
448static int
449generate_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