blob: dca0cf1b4b655110eb44ca071e8510d2aa8ff3db [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");
252
Lev Walkinb9b8b952005-03-05 00:33:27 +0000253 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
254 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
255 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
256 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
257 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
258 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000259
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000260 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
261 "#endif\t/* _%s_H_ */\n",
262 header_id);
263
Lev Walkin0ffabe22004-09-22 16:02:03 +0000264 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkine8318b82005-03-06 09:29:03 +0000265 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000266 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000267 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000268 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000269 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000270 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
271 fwrite(ot->buf, ot->len, 1, fp_c);
272 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000273 fwrite(ot->buf, ot->len, 1, fp_c);
274
Lev Walkinc8285712005-03-04 22:18:20 +0000275 assert(OT_MAX == 10); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000276
277 fclose(fp_c);
278 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000279
280 name_buf = alloca(strlen(expr->Identifier) + 3);
281
282 sprintf(name_buf, "%s.c", expr->Identifier);
283 if(identical_files(name_buf, tmpname_c)) {
284 c_retained = " (contents unchanged)";
285 unlink(tmpname_c);
286 } else {
287 if(rename(tmpname_c, name_buf)) {
288 unlink(tmpname_c);
289 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000290 free(tmpname_c);
291 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000292 return -1;
293 }
294 }
295
296 sprintf(name_buf, "%s.h", expr->Identifier);
297 if(identical_files(name_buf, tmpname_h)) {
298 h_retained = " (contents unchanged)";
299 unlink(tmpname_h);
300 } else {
301 if(rename(tmpname_h, name_buf)) {
302 unlink(tmpname_h);
303 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000304 free(tmpname_c);
305 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000306 return -1;
307 }
308 }
309
310 free(tmpname_c);
311 free(tmpname_h);
312
313 fprintf(stderr, "Compiled %s.c%s\n",
314 expr->Identifier, c_retained);
315 fprintf(stderr, "Compiled %s.h%s\n",
316 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 return 0;
318}
319
Lev Walkin4604d032005-03-04 08:52:50 +0000320static int
321identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000322 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000323 FILE *fp1, *fp2;
324 size_t olen, nlen;
325 int retval = 1; /* Files are identical */
326
Lev Walkina895afb2005-10-06 10:09:34 +0000327#ifndef WIN32
328 struct stat sb;
329
330 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
331 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
332 return 0; /* Files are not identical */
333 }
334#endif
335
Lev Walkin4604d032005-03-04 08:52:50 +0000336 fp1 = fopen(fname1, "r");
337 if(!fp1) { return 0; }
338 fp2 = fopen(fname2, "r");
339 if(!fp2) { fclose(fp1); return 0; }
340
341 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
342 nlen = fread(buf[1], 1, olen, fp2);
343 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
344 retval = 0;
345 break;
346 }
347 }
348 nlen = fread(buf[1], 1, 1, fp2);
349 if(nlen) retval = 0;
350
351 fclose(fp1);
352 fclose(fp2);
353 return retval;
354}
355
Lev Walkin4efbfb72005-02-25 14:20:30 +0000356/*
357 * Copy file for real.
358 */
359static int
360real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000361 unsigned char buf[4096];
362 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000363 FILE *fpsrc, *fpdst;
364 size_t len;
365 int retval = 0;
366
Lev Walkin4604d032005-03-04 08:52:50 +0000367 if(identical_files(src, dst))
368 return retval; /* Success, no need to copy for real. */
369
370 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000371 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000372 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000373 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
374
375 while(!feof(fpsrc)) {
376 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinc0e70712005-10-05 09:01:51 +0000377 if(fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000378 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000379 errno = EIO;
380 retval = -1;
381 break;
382 }
383 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000384 fclose(fpsrc);
385 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000386
387 /* Check if copied correctly, and rename into a permanent name */
388 if(retval) {
389 unlink(tmpname);
390 } else if(rename(tmpname, dst)) {
391 unlink(tmpname);
392 perror(tmpname);
393 retval = -1;
394 }
395 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000396 return retval;
397}
398
Lev Walkinf15320b2004-06-03 03:38:44 +0000399static int
400asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000401 char *fname;
Lev Walkina895afb2005-10-06 10:09:34 +0000402#ifdef WIN32
403 int use_real_copy = 1;
404#else
405 int use_real_copy = (arg->flags & A1C_SKELETONS_COPY);
406#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000407
Lev Walkin79f54952004-08-13 16:58:19 +0000408 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000409 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000410 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000411 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000412 if(errno == EEXIST) {
413 struct stat sb1, sb2;
414 if(stat(path, &sb1) == 0
415 && stat(fname, &sb2) == 0
416 && sb1.st_dev == sb2.st_dev
417 && sb1.st_ino == sb2.st_ino) {
418 /*
419 * Nothing to do.
420 */
421 fprintf(stderr,
422 "File %s is already here as %s\n",
423 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000424 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000425 } else {
426 fprintf(stderr,
427 "Retaining local %s (%s suggested)\n",
428 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000429 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000430 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000431 } else if(errno == ENOENT) {
432 /* Ignore this */
433 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000434 } else {
Lev Walkina895afb2005-10-06 10:09:34 +0000435 fprintf(stderr, "%s %s -> %s failed: %s\n",
436 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000437 path, fname, strerror(errno));
438 return -1;
439 }
440 }
441
Lev Walkina895afb2005-10-06 10:09:34 +0000442 fprintf(stderr, "%s %s\t-> %s\n",
443 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000444
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000445 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000446}
447
Lev Walkin59b176e2005-11-26 11:25:14 +0000448
449static int
450generate_pdu_collection_file(arg_t *arg) {
451 asn1p_module_t *mod;
452 FILE *fp;
453
454 fp = asn1c_open_file("pdu_collection", ".c", 0);
455 if(fp == NULL) {
456 perror("pdu_collection.c");
457 return -1;
458 }
459
460 fprintf(fp,
461 "/*\n"
462 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
463 " */\n\n");
464 fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
465 "/* Forward declaration */\n\n");
466
467 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
468 TQ_FOR(arg->expr, &(mod->members), next) {
469 if(arg->expr->_type_referenced
470 || !asn1_lang_map[arg->expr->meta_type]
471 [arg->expr->expr_type].type_cb)
472 continue;
473 fprintf(fp, "extern struct asn_TYPE_descriptor_s "
474 "asn_DEF_%s;\n",
475 asn1c_make_identifier(0, arg->expr->Identifier,
476 NULL));
477 }
478 }
479
480 fprintf(fp, "\n\n");
481 fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
482 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
483 int mod_printed = 0;
484 TQ_FOR(arg->expr, &(mod->members), next) {
485 if(arg->expr->_type_referenced
486 || !asn1_lang_map[arg->expr->meta_type]
487 [arg->expr->expr_type].type_cb)
488 continue;
489 if(!mod_printed++)
490 fprintf(fp, "\t/* From module %s in %s */\n",
491 arg->expr->module->ModuleName,
492 arg->expr->module->source_file_name);
493 fprintf(fp, "\t&asn_DEF_%s,\t\n",
494 asn1c_make_identifier(0, arg->expr->Identifier,
495 NULL));
496 }
497 }
498
499 fprintf(fp, "\t0\n};\n\n");
500
501 fclose(fp);
502 fprintf(stderr, "Generated pdu_collection.c\n");
503
504 return 0;
505}
506