blob: f3cfcdf27ea5118479430db389feae5cc7d429d4 [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
Frank Morgner8a759ad2013-05-16 13:32:49 +02009#ifndef HAVE_SYMLINK
10#define symlink(a,b) (errno=ENOSYS, -1)
11#endif
12
Lev Walkin34944f22010-10-07 08:25:37 +000013#define HINCLUDE(s) \
14 ((arg->flags & A1C_INCLUDES_QUOTED) \
15 ? fprintf(fp_h, "#include \"%s\"\n", s) \
16 : fprintf(fp_h, "#include <%s>\n", s)) \
17
Lev Walkinc46b7cb2006-08-18 02:27:55 +000018static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000019static int asn1c_print_streams(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000020static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000021static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000022static int identical_files(const char *fname1, const char *fname2);
Lev Walkin66adab42006-09-23 02:52:12 +000023static int need_to_generate_pdu_collection(arg_t *arg);
Lev Walkin59b176e2005-11-26 11:25:14 +000024static int generate_pdu_collection_file(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000025static int generate_preamble(arg_t *, FILE *, int optc, char **argv);
Lev Walkin66adab42006-09-23 02:52:12 +000026static int include_type_to_pdu_collection(arg_t *arg);
27static void pdu_collection_print_unused_types(arg_t *arg);
28static const char *generate_pdu_C_definition(void);
Lev Walkinf15320b2004-06-03 03:38:44 +000029
30int
Lev Walkin866cff12005-03-05 00:50:53 +000031asn1c_save_compiled_output(arg_t *arg, const char *datadir,
Lev Walkin8253ea92006-03-17 01:47:57 +000032 int argc, int optc, char **argv) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000033 asn1c_fdeps_t *deps = 0;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000034 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000035 asn1p_module_t *mod;
Lev Walkin59b176e2005-11-26 11:25:14 +000036 FILE *mkf; /* Makefile.am.sample */
Lev Walkin866cff12005-03-05 00:50:53 +000037 int i;
Lev Walkinf15320b2004-06-03 03:38:44 +000038
Lev Walkinacd9f8b2004-08-19 13:29:03 +000039 deps = asn1c_read_file_dependencies(arg, datadir);
40 if(!deps && datadir) {
41 WARNING("Cannot read file-dependencies information "
42 "from %s\n", datadir);
43 }
Lev Walkinf15320b2004-06-03 03:38:44 +000044
Lev Walkinb85a8132005-08-18 13:38:19 +000045 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
46 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +000047 if(asn1_lang_map[arg->expr->meta_type]
48 [arg->expr->expr_type].type_cb) {
Lev Walkinc46b7cb2006-08-18 02:27:55 +000049 if(asn1c_dump_streams(arg, deps, optc, argv))
Lev Walkinf15320b2004-06-03 03:38:44 +000050 return -1;
51 }
52 }
53 }
54
55 /*
56 * Dump out the Makefile template and the rest of the support code.
57 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000058 if((arg->flags & A1C_PRINT_COMPILED)
59 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
60 return 0; /* Finished */
61 }
Lev Walkinf15320b2004-06-03 03:38:44 +000062
Lev Walkin4604d032005-03-04 08:52:50 +000063 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000064 if(mkf == NULL) {
65 perror("Makefile.am.sample");
66 return -1;
67 }
Lev Walkinf15320b2004-06-03 03:38:44 +000068
Lev Walkinacd9f8b2004-08-19 13:29:03 +000069 fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000070 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
71 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000072 if(asn1_lang_map[arg->expr->meta_type]
73 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000074 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000075 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000076 }
77 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000078 }
Lev Walkind29bbce2004-09-23 22:19:14 +000079 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000080 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
81 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000082 if(asn1_lang_map[arg->expr->meta_type]
83 [arg->expr->expr_type].type_cb) {
84 fprintf(mkf, "\t\\\n\t%s.h",
85 arg->expr->Identifier);
86 }
87 }
88 }
89 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000090
Lev Walkinacd9f8b2004-08-19 13:29:03 +000091 /*
92 * Move necessary skeleton files and add them to Makefile.am.sample.
93 */
94 dlist = asn1c_deps_makelist(deps);
95 if(dlist) {
96 char buf[8129];
97 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000098 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000099
Lev Walkin4efbfb72005-02-25 14:20:30 +0000100 assert(dlen < (sizeof(buf) / 2 - 2));
101 memcpy(buf, datadir, dlen);
102 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000103 *dir_end++ = '/';
104
105 for(i = 0; i < dlist->el_count; i++) {
Lev Walkinf218e782006-09-12 06:21:18 +0000106 char *what_class; /* MODULE or CONVERTER */
107 char *what_kind; /* HEADERS or SOURCES */
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000108 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +0000109 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000110
111 assert(strlen(fname) < (sizeof(buf) / 2));
112 strcpy(dir_end, fname);
113
114 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000115 fprintf(mkf, ">>>ABORTED<<<");
116 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000117 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000118 }
Lev Walkinf218e782006-09-12 06:21:18 +0000119
120 /* MODULE data versus CONVERTER data */
121 switch(dlist->elements[i]->usage) {
122 case FDEP_CONVERTER: what_class = "CONVERTER"; break;
123 default: what_class= "MODULE"; break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 }
Lev Walkinf218e782006-09-12 06:21:18 +0000125
126 /* HEADERS versus SOURCES */
127 dotH = strrchr(fname, 'h');
128 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1])
129 what_kind = "HEADERS";
130 else
131 what_kind = "SOURCES";
132 fprintf(mkf, "ASN_%s_%s+=%s\n",
133 what_class, what_kind, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 }
136
Lev Walkin66adab42006-09-23 02:52:12 +0000137 if(need_to_generate_pdu_collection(arg)) {
Lev Walkinf218e782006-09-12 06:21:18 +0000138 fprintf(mkf, "ASN_CONVERTER_SOURCES+=pdu_collection.c\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000139 if(generate_pdu_collection_file(arg))
140 return -1;
141 }
142
Lev Walkind29bbce2004-09-23 22:19:14 +0000143 fprintf(mkf, "\n\n"
144 "lib_LTLIBRARIES=libsomething.la\n"
145 "libsomething_la_SOURCES="
146 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
147 "\n"
148 "# This file may be used as an input for make(3)\n"
149 "# Remove the lines below to convert it into a pure .am file\n"
150 "TARGET = progname\n"
Lev Walkin66adab42006-09-23 02:52:12 +0000151 "CFLAGS +=%s%s -I.\n"
Lev Walkinf218e782006-09-12 06:21:18 +0000152 "OBJS=${ASN_MODULE_SOURCES:.c=.o}"
153 " ${ASN_CONVERTER_SOURCES:.c=.o}\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000154 "\nall: $(TARGET)\n"
155 "\n$(TARGET): ${OBJS}"
156 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
157 "\n.SUFFIXES:"
158 "\n.SUFFIXES: .c .o\n"
159 "\n.c.o:"
160 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
161 "\nclean:"
162 "\n\trm -f $(TARGET)"
163 "\n\trm -f $(OBJS)\n"
Lev Walkin866cff12005-03-05 00:50:53 +0000164 "\nregen: regenerate-from-asn1-source\n"
165 "\nregenerate-from-asn1-source:\n\t"
Lev Walkin66adab42006-09-23 02:52:12 +0000166 , (arg->flags & A1C_PDU_TYPE)
167 ? generate_pdu_C_definition() : ""
168 , need_to_generate_pdu_collection(arg)
169 ? " -DASN_PDU_COLLECTION" : ""
Lev Walkind29bbce2004-09-23 22:19:14 +0000170 );
Lev Walkin866cff12005-03-05 00:50:53 +0000171
172 for(i = 0; i < argc; i++)
173 fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
174 fprintf(mkf, "\n\n");
175
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000176 fclose(mkf);
177 fprintf(stderr, "Generated Makefile.am.sample\n");
178
Lev Walkinf15320b2004-06-03 03:38:44 +0000179 return 0;
180}
181
182/*
183 * Dump the streams.
184 */
185static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000186asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000187 if(arg->flags & A1C_PRINT_COMPILED) {
188 return asn1c_print_streams(arg);
189 } else {
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000190 return asn1c_save_streams(arg, deps, optc, argv);
Lev Walkinf15320b2004-06-03 03:38:44 +0000191 }
192}
193
194static int
195asn1c_print_streams(arg_t *arg) {
196 compiler_streams_t *cs = arg->expr->data;
197 asn1p_expr_t *expr = arg->expr;
198 int i;
199
Lev Walkin64399722004-08-11 07:17:22 +0000200 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000201 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000202 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000203 continue;
204
205 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
206 _compiler_stream2str[i],
207 expr->Identifier);
208
Lev Walkin59004fa2004-08-20 13:37:01 +0000209 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000210 fwrite(ot->buf, ot->len, 1, stdout);
211 }
212 }
213
214 return 0;
215}
216
217static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000218asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000219 asn1p_expr_t *expr = arg->expr;
220 compiler_streams_t *cs = expr->data;
221 out_chunk_t *ot;
222 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000223 char *tmpname_c, *tmpname_h;
224 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000225 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000226 const char *c_retained = "";
227 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000228
229 if(cs == NULL) {
230 fprintf(stderr, "Cannot compile %s at line %d\n",
231 expr->Identifier, expr->_lineno);
232 return -1;
233 }
234
Lev Walkin4604d032005-03-04 08:52:50 +0000235 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
236 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000237 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000238 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
239 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000240 return -1;
241 }
242
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000243 generate_preamble(arg, fp_c, optc, argv);
244 generate_preamble(arg, fp_h, optc, argv);
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000245
Lev Walkina00d6b32006-03-21 03:40:38 +0000246 header_id = asn1c_make_identifier(0, expr, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000247 fprintf(fp_h,
248 "#ifndef\t_%s_H_\n"
249 "#define\t_%s_H_\n"
250 "\n", header_id, header_id);
251
Lev Walkin34944f22010-10-07 08:25:37 +0000252 fprintf(fp_h, "\n");
253 HINCLUDE("asn_application.h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000254
Lev Walkinb9b8b952005-03-05 00:33:27 +0000255#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000256 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000257 fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000258 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
259 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinb9b8b952005-03-05 00:33:27 +0000260 fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000261 } \
262} while(0)
263
Lev Walkinb9b8b952005-03-05 00:33:27 +0000264 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000265
266 fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000267 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
268 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
269 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
270 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
Lev Walkin47370392006-02-19 04:30:15 +0000271 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000272
Lev Walkinae5540f2006-02-19 04:26:37 +0000273 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
274 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
275
Lev Walkin47370392006-02-19 04:30:15 +0000276 fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
277
Lev Walkin34944f22010-10-07 08:25:37 +0000278 HINCLUDE("asn_internal.h");
Lev Walkine8318b82005-03-06 09:29:03 +0000279 fprintf(fp_c, "#include \"%s.h\"\n\n", expr->Identifier);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000280 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000281 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin59004fa2004-08-20 13:37:01 +0000282 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000283 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000284 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
285 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin725883b2006-10-09 12:07:58 +0000286 TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
287 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000288 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000289 fwrite(ot->buf, ot->len, 1, fp_c);
290
Lev Walkin725883b2006-10-09 12:07:58 +0000291 assert(OT_MAX == 11); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000292
293 fclose(fp_c);
294 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000295
296 name_buf = alloca(strlen(expr->Identifier) + 3);
297
298 sprintf(name_buf, "%s.c", expr->Identifier);
299 if(identical_files(name_buf, tmpname_c)) {
300 c_retained = " (contents unchanged)";
301 unlink(tmpname_c);
302 } else {
303 if(rename(tmpname_c, name_buf)) {
304 unlink(tmpname_c);
305 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000306 free(tmpname_c);
307 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000308 return -1;
309 }
310 }
311
312 sprintf(name_buf, "%s.h", expr->Identifier);
313 if(identical_files(name_buf, tmpname_h)) {
314 h_retained = " (contents unchanged)";
315 unlink(tmpname_h);
316 } else {
317 if(rename(tmpname_h, name_buf)) {
318 unlink(tmpname_h);
319 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000320 free(tmpname_c);
321 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000322 return -1;
323 }
324 }
325
326 free(tmpname_c);
327 free(tmpname_h);
328
329 fprintf(stderr, "Compiled %s.c%s\n",
330 expr->Identifier, c_retained);
331 fprintf(stderr, "Compiled %s.h%s\n",
332 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000333 return 0;
334}
335
Lev Walkin4604d032005-03-04 08:52:50 +0000336static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000337generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
Lev Walkin8253ea92006-03-17 01:47:57 +0000338 fprintf(fp,
339 "/*\n"
340 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
341 " * From ASN.1 module \"%s\"\n"
342 " * \tfound in \"%s\"\n",
343 arg->expr->module->ModuleName,
344 arg->expr->module->source_file_name);
345 if(optc > 1) {
346 int i;
347 fprintf(fp, " * \t`asn1c ");
348 for(i = 1; i < optc; i++)
349 fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
350 fprintf(fp, "`\n");
351 }
352 fprintf(fp, " */\n\n");
353 return 0;
354}
355
356static int
Lev Walkin4604d032005-03-04 08:52:50 +0000357identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000358 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000359 FILE *fp1, *fp2;
360 size_t olen, nlen;
361 int retval = 1; /* Files are identical */
362
Lev Walkin93659562010-11-20 09:47:13 -0800363#ifndef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000364 struct stat sb;
365
366 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
367 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
368 return 0; /* Files are not identical */
369 }
370#endif
371
Lev Walkin4604d032005-03-04 08:52:50 +0000372 fp1 = fopen(fname1, "r");
373 if(!fp1) { return 0; }
374 fp2 = fopen(fname2, "r");
375 if(!fp2) { fclose(fp1); return 0; }
376
377 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
378 nlen = fread(buf[1], 1, olen, fp2);
379 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
380 retval = 0;
381 break;
382 }
383 }
384 nlen = fread(buf[1], 1, 1, fp2);
385 if(nlen) retval = 0;
386
387 fclose(fp1);
388 fclose(fp2);
389 return retval;
390}
391
Lev Walkin4efbfb72005-02-25 14:20:30 +0000392/*
393 * Copy file for real.
394 */
395static int
396real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000397 unsigned char buf[4096];
398 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000399 FILE *fpsrc, *fpdst;
400 size_t len;
401 int retval = 0;
402
Lev Walkin4604d032005-03-04 08:52:50 +0000403 if(identical_files(src, dst))
404 return retval; /* Success, no need to copy for real. */
405
406 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000407 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000408 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000409 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
410
411 while(!feof(fpsrc)) {
412 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinc0e70712005-10-05 09:01:51 +0000413 if(fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000414 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000415 errno = EIO;
416 retval = -1;
417 break;
418 }
419 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000420 fclose(fpsrc);
421 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000422
423 /* Check if copied correctly, and rename into a permanent name */
424 if(retval) {
425 unlink(tmpname);
426 } else if(rename(tmpname, dst)) {
427 unlink(tmpname);
428 perror(tmpname);
429 retval = -1;
430 }
431 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000432 return retval;
433}
434
Lev Walkinf15320b2004-06-03 03:38:44 +0000435static int
436asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000437 char *fname;
Lev Walkin93659562010-11-20 09:47:13 -0800438#ifdef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000439 int use_real_copy = 1;
440#else
Lev Walkin2655eb32013-03-25 19:09:04 -0700441 int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS);
Lev Walkina895afb2005-10-06 10:09:34 +0000442#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000443
Lev Walkin79f54952004-08-13 16:58:19 +0000444 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000445 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000446 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000447 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000448 if(errno == EEXIST) {
449 struct stat sb1, sb2;
450 if(stat(path, &sb1) == 0
451 && stat(fname, &sb2) == 0
452 && sb1.st_dev == sb2.st_dev
453 && sb1.st_ino == sb2.st_ino) {
454 /*
455 * Nothing to do.
456 */
457 fprintf(stderr,
458 "File %s is already here as %s\n",
459 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000460 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000461 } else {
462 fprintf(stderr,
463 "Retaining local %s (%s suggested)\n",
464 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000465 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000466 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000467 } else if(errno == ENOENT) {
468 /* Ignore this */
469 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000470 } else {
Lev Walkina895afb2005-10-06 10:09:34 +0000471 fprintf(stderr, "%s %s -> %s failed: %s\n",
472 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000473 path, fname, strerror(errno));
474 return -1;
475 }
476 }
477
Lev Walkina895afb2005-10-06 10:09:34 +0000478 fprintf(stderr, "%s %s\t-> %s\n",
479 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000480
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000481 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000482}
483
Lev Walkin59b176e2005-11-26 11:25:14 +0000484
485static int
486generate_pdu_collection_file(arg_t *arg) {
487 asn1p_module_t *mod;
488 FILE *fp;
489
490 fp = asn1c_open_file("pdu_collection", ".c", 0);
491 if(fp == NULL) {
492 perror("pdu_collection.c");
493 return -1;
494 }
495
496 fprintf(fp,
497 "/*\n"
498 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
499 " */\n\n");
500 fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
501 "/* Forward declaration */\n\n");
502
503 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
504 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkin66adab42006-09-23 02:52:12 +0000505 if(!include_type_to_pdu_collection(arg))
Lev Walkin59b176e2005-11-26 11:25:14 +0000506 continue;
507 fprintf(fp, "extern struct asn_TYPE_descriptor_s "
508 "asn_DEF_%s;\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000509 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000510 }
511 }
512
513 fprintf(fp, "\n\n");
514 fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
515 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
516 int mod_printed = 0;
517 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkin66adab42006-09-23 02:52:12 +0000518 if(!include_type_to_pdu_collection(arg))
Lev Walkin59b176e2005-11-26 11:25:14 +0000519 continue;
520 if(!mod_printed++)
521 fprintf(fp, "\t/* From module %s in %s */\n",
522 arg->expr->module->ModuleName,
523 arg->expr->module->source_file_name);
524 fprintf(fp, "\t&asn_DEF_%s,\t\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000525 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000526 }
527 }
528
529 fprintf(fp, "\t0\n};\n\n");
530
Lev Walkin66adab42006-09-23 02:52:12 +0000531 pdu_collection_print_unused_types(arg);
532
Lev Walkin59b176e2005-11-26 11:25:14 +0000533 fclose(fp);
534 fprintf(stderr, "Generated pdu_collection.c\n");
535
536 return 0;
537}
538
Lev Walkin66adab42006-09-23 02:52:12 +0000539static struct PDUType {
540 char *typename;
541 int used;
542} *pduType;
543static int pduTypes;
544
545static const char *
546generate_pdu_C_definition(void) {
547 const char *src;
548 char *def;
549 char *dst;
550 if(pduTypes == 0) return "";
551 def = malloc(strlen(pduType[0].typename) + 20);
552 strcpy(def, " -DPDU=");
553 for(src = pduType[0].typename, dst = def + 7; *src; src++, dst++)
554 if((*dst = *src) == '-')
555 *dst = '_';
556 *dst = 0;
557 return def;
558}
559
560void
561asn1c__add_pdu_type(const char *ctypename) {
562 char *typename = strdup(ctypename);
563 assert(typename && *typename);
564
565 pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1));
566 assert(pduType);
567 pduType[pduTypes].used = 0;
568 pduType[pduTypes].typename = typename;
569 pduTypes++;
570}
571
572static int
573asn1c__pdu_type_lookup(const char *typename) {
574 int i;
575 for(i = 0; i < pduTypes; i++) {
576 struct PDUType *pt = &pduType[i];
577 if(strcmp(pt->typename, typename) == 0) {
578 pt->used++;
579 return 1;
580 }
581 }
582 return 0;
583}
584
585static int
586need_to_generate_pdu_collection(arg_t *arg) {
587 if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO))
588 return 1;
589 if(arg->flags & A1C_PDU_TYPE)
590 return (pduTypes > 1) ? 1 : 0;
591 return 0;
592}
593
594static void
595pdu_collection_print_unused_types(arg_t *arg) {
596 int i;
597 for(i = 0; i < pduTypes; i++) {
598 struct PDUType *pt = &pduType[i];
599 if(!pt->used) {
600 WARNING("Missing type specified in -pdu=%s",
601 pt->typename);
602 }
603 }
604}
605
606static int
607include_type_to_pdu_collection(arg_t *arg) {
608 if(!asn1_lang_map[arg->expr->meta_type]
609 [arg->expr->expr_type].type_cb)
610 return 0;
611
612 if((arg->flags & A1C_PDU_ALL)
613 || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced)
614 || asn1c__pdu_type_lookup(arg->expr->Identifier)) {
615 return 1;
616 }
617
618 return 0;
619}