blob: 5fe1b3733cb555300b3b4c7d7b5368328adb4b0f [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 Walkinf2b2f372016-03-14 02:23:48 -070013/* Pedantically check fprintf's return value. */
14static int safe_fprintf(FILE *fp, const char *fmt, ...) {
15 va_list ap;
16 va_start(ap, fmt);
17 int ret = vfprintf(fp, fmt, ap);
18 va_end(ap);
19 assert(ret >= 0);
20 return ret;
21}
22
23/* Pedantically check fwrite's return value. */
24static size_t safe_fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream) {
25 size_t ret = fwrite(ptr, 1, size * nitems, stream);
26 assert(ret == size * nitems);
27 return ret;
28}
29
Lev Walkin34944f22010-10-07 08:25:37 +000030#define HINCLUDE(s) \
31 ((arg->flags & A1C_INCLUDES_QUOTED) \
Lev Walkinf2b2f372016-03-14 02:23:48 -070032 ? safe_fprintf(fp_h, "#include \"%s\"\n", s) \
33 : safe_fprintf(fp_h, "#include <%s>\n", s)) \
Lev Walkin34944f22010-10-07 08:25:37 +000034
Lev Walkinc46b7cb2006-08-18 02:27:55 +000035static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000036static int asn1c_print_streams(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000037static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000038static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000039static int identical_files(const char *fname1, const char *fname2);
Lev Walkin66adab42006-09-23 02:52:12 +000040static int need_to_generate_pdu_collection(arg_t *arg);
Lev Walkin59b176e2005-11-26 11:25:14 +000041static int generate_pdu_collection_file(arg_t *arg);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000042static int generate_preamble(arg_t *, FILE *, int optc, char **argv);
Lev Walkin66adab42006-09-23 02:52:12 +000043static int include_type_to_pdu_collection(arg_t *arg);
44static void pdu_collection_print_unused_types(arg_t *arg);
45static const char *generate_pdu_C_definition(void);
Lev Walkin5230c642017-09-26 18:10:06 -070046static void asn1c__cleanup_pdu_type(void);
Lev Walkinf15320b2004-06-03 03:38:44 +000047
Lev Walkin8a85b362017-09-26 22:54:44 -070048static int
49asn1c__save_library_makefile(arg_t *arg, const asn1c_fdeps_t *deps, const char *datadir, const char *makefile_name) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000050 asn1c_fdeps_t *dlist;
Lev Walkinb85a8132005-08-18 13:38:19 +000051 asn1p_module_t *mod;
Lev Walkin8a85b362017-09-26 22:54:44 -070052 FILE *mkf;
Lev Walkinf15320b2004-06-03 03:38:44 +000053
Lev Walkin8a85b362017-09-26 22:54:44 -070054 mkf = asn1c_open_file(makefile_name, "", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000055 if(mkf == NULL) {
Lev Walkin8a85b362017-09-26 22:54:44 -070056 perror(makefile_name);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000057 return -1;
58 }
Lev Walkinf15320b2004-06-03 03:38:44 +000059
Lev Walkinf2b2f372016-03-14 02:23:48 -070060 safe_fprintf(mkf, "ASN_MODULE_SOURCES=");
Lev Walkinb85a8132005-08-18 13:38:19 +000061 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
62 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000063 if(asn1_lang_map[arg->expr->meta_type]
64 [arg->expr->expr_type].type_cb) {
Lev Walkinf2b2f372016-03-14 02:23:48 -070065 safe_fprintf(mkf, "\t\\\n\t%s.c",
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080066 asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +000067 }
68 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000069 }
Lev Walkinf2b2f372016-03-14 02:23:48 -070070 safe_fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000071 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
72 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000073 if(asn1_lang_map[arg->expr->meta_type]
74 [arg->expr->expr_type].type_cb) {
Lev Walkinf2b2f372016-03-14 02:23:48 -070075 safe_fprintf(mkf, "\t\\\n\t%s.h",
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080076 asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
Lev Walkind29bbce2004-09-23 22:19:14 +000077 }
78 }
79 }
Lev Walkinf2b2f372016-03-14 02:23:48 -070080 safe_fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000081
Lev Walkinacd9f8b2004-08-19 13:29:03 +000082 /*
Lev Walkin8a85b362017-09-26 22:54:44 -070083 * Move necessary skeleton files and add them to Makefile.am.targets.
Lev Walkinacd9f8b2004-08-19 13:29:03 +000084 */
Lev Walkin8a85b362017-09-26 22:54:44 -070085 dlist = asn1c_deps_flatten(deps);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000086 if(dlist) {
87 char buf[8129];
88 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000089 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000090
Lev Walkin4efbfb72005-02-25 14:20:30 +000091 assert(dlen < (sizeof(buf) / 2 - 2));
92 memcpy(buf, datadir, dlen);
93 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000094 *dir_end++ = '/';
95
Lev Walkin8a85b362017-09-26 22:54:44 -070096 for(int i = 0; i < dlist->el_count; i++) {
Lev Walkinf218e782006-09-12 06:21:18 +000097 char *what_kind; /* HEADERS or SOURCES */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000098 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000099 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000100
101 assert(strlen(fname) < (sizeof(buf) / 2));
102 strcpy(dir_end, fname);
103
104 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700105 safe_fprintf(mkf, ">>>ABORTED<<<");
Lev Walkinf15320b2004-06-03 03:38:44 +0000106 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000108 }
Lev Walkinf218e782006-09-12 06:21:18 +0000109
Lev Walkin8a85b362017-09-26 22:54:44 -0700110 /* no CONVERTER data in Makefile.am.targets */
111 if(dlist->elements[i]->usage != FDEP_CONVERTER) {
112 /* HEADERS versus SOURCES */
113 dotH = strrchr(fname, 'h');
114 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1])
115 what_kind = "HEADERS";
116 else
117 what_kind = "SOURCES";
118 safe_fprintf(mkf, "ASN_MODULE_%s+=%s\n",
119 what_kind, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000120 }
121 }
Bi-Ruei, Chiu6f348942016-11-08 15:41:23 +0800122
Lev Walkin8a85b362017-09-26 22:54:44 -0700123 asn1c_deps_freelist(dlist);
124 }
125
126 safe_fprintf(
127 mkf,
128 "\n"
129 "ASN_MODULE_CFLAGS=%s%s",
130 (arg->flags & A1C_GEN_OER) ? "" : "-DASN_DISABLE_OER_SUPPORT ",
131 (arg->flags & A1C_GEN_PER) ? "" : "-DASN_DISABLE_PER_SUPPORT ");
132
133 safe_fprintf(
134 mkf,
135 "\n\n"
136 "lib_LTLIBRARIES=libasncodec.la\n"
137 "libasncodec_la_SOURCES="
138 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
139 "libasncodec_la_CFLAGS=$(ASN_MODULE_CFLAGS)\n");
140 fclose(mkf);
141 safe_fprintf(stderr, "Generated Makefile.am.targets\n");
142
143 return 0;
144}
145
146static int
147asn1c__save_example_makefile(arg_t *arg, const asn1c_fdeps_t *deps,
148 const char *makefile_name,
149 const char *library_makefile_name, int argc,
150 char **argv) {
151 asn1c_fdeps_t *dlist;
152 FILE *mkf;
153
154 mkf = asn1c_open_file(makefile_name, "", 0);
155 if(mkf == NULL) {
156 perror(makefile_name);
157 return -1;
158 }
159 safe_fprintf(
160 mkf,
161 "include %s\n\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700162 "LIBS += -lm\n"
163 "CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n"
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700164 "ASN_LIBRARY ?= libasncodec.a\n"
165 "ASN_PROGRAM ?= converter-example\n"
166 "ASN_PROGRAM_SOURCES ?= ",
Lev Walkin8a85b362017-09-26 22:54:44 -0700167 library_makefile_name,
168 (arg->flags & A1C_PDU_TYPE) ? generate_pdu_C_definition() : "",
169 need_to_generate_pdu_collection(arg) ? "-DASN_PDU_COLLECTION " : "");
170
171 dlist = asn1c_deps_flatten(deps);
172 if(dlist) {
173 /* only CONVERTER data in the makefile */
174 for(int i = 0; i < dlist->el_count; i++) {
175 if(dlist->elements[i]->usage == FDEP_CONVERTER) {
176 safe_fprintf(mkf, "\\\n\t%s", dlist->elements[i]->filename);
177 }
178 }
Bi-Ruei, Chiu6f348942016-11-08 15:41:23 +0800179 asn1c_deps_freelist(dlist);
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 }
181
Lev Walkin66adab42006-09-23 02:52:12 +0000182 if(need_to_generate_pdu_collection(arg)) {
Lev Walkin8a85b362017-09-26 22:54:44 -0700183 safe_fprintf(mkf, "\\\n\tpdu_collection.c");
Lev Walkin59b176e2005-11-26 11:25:14 +0000184 if(generate_pdu_collection_file(arg))
185 return -1;
186 }
187
Lev Walkin8a85b362017-09-26 22:54:44 -0700188 safe_fprintf(
189 mkf,
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700190 "\n\nall: $(ASN_PROGRAM)\n"
191 "\n$(ASN_PROGRAM): $(ASN_LIBRARY) $(ASN_PROGRAM_SOURCES:.c=.o)"
192 "\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) $(ASN_PROGRAM_SOURCES:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700193 "\n$(ASN_LIBRARY): $(ASN_MODULE_SOURCES:.c=.o)"
Lev Walkinb458fc02017-10-02 08:18:36 +0000194 "\n\t$(AR) rcs $@ $(ASN_MODULE_SOURCES:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700195 "\n.SUFFIXES:"
196 "\n.SUFFIXES: .c .o\n"
197 "\n.c.o:"
198 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
199 "\nclean:"
Lev Walkine24bad12017-10-09 23:58:15 -0700200 "\n\trm -f $(ASN_PROGRAM) $(ASN_LIBRARY)"
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700201 "\n\trm -f $(ASN_MODULE_SOURCES:.c=.o) $(ASN_PROGRAM_SOURCES:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700202 "\nregen: regenerate-from-asn1-source\n"
203 "\nregenerate-from-asn1-source:\n\t");
Lev Walkin866cff12005-03-05 00:50:53 +0000204
Lev Walkin8a85b362017-09-26 22:54:44 -0700205 for(int i = 0; i < argc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700206 safe_fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
207 safe_fprintf(mkf, "\n\n");
Lev Walkin866cff12005-03-05 00:50:53 +0000208
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000209 fclose(mkf);
Lev Walkin8a85b362017-09-26 22:54:44 -0700210 safe_fprintf(stderr, "Generated %s\n", makefile_name);
211 return 0;
212}
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000213
Lev Walkin8a85b362017-09-26 22:54:44 -0700214int
215asn1c_save_compiled_output(arg_t *arg, const char *datadir,
216 int argc, int optc, char **argv) {
217 asn1c_fdeps_t *deps = 0;
218 int ret = -1;
Lev Walkin5230c642017-09-26 18:10:06 -0700219
Lev Walkin8a85b362017-09-26 22:54:44 -0700220 do {
221 asn1p_module_t *mod;
222
223 deps = asn1c_read_file_dependencies(arg, datadir);
224 if(!deps && datadir) {
225 WARNING(
226 "Cannot read file-dependencies information "
227 "from %s\n",
228 datadir);
229 }
230
231 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
232 TQ_FOR(arg->expr, &(mod->members), next) {
233 if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type]
234 .type_cb) {
235 if(asn1c_dump_streams(arg, deps, optc, argv)) break;
236 }
237 }
238 }
239
240 /*
241 * Dump out the Makefile template and the rest of the support code.
242 */
243 if((arg->flags & A1C_PRINT_COMPILED)
244 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
245 ret = 0; /* Success */
246 break;
247 }
248
249 ret = asn1c__save_library_makefile(arg, deps, datadir,
250 "Makefile.am.libasncodec");
251 if(ret) break;
252 ret = asn1c__save_example_makefile(arg, deps, "Makefile.am.example",
253 "Makefile.am.libasncodec", argc, argv);
254 if(ret) break;
255 } while(0);
256
257 asn1c_deps_freelist(deps);
258 asn1c__cleanup_pdu_type();
259
260 return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000261}
262
263/*
264 * Dump the streams.
265 */
266static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000267asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000268 if(arg->flags & A1C_PRINT_COMPILED) {
269 return asn1c_print_streams(arg);
270 } else {
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000271 return asn1c_save_streams(arg, deps, optc, argv);
Lev Walkinf15320b2004-06-03 03:38:44 +0000272 }
273}
274
275static int
276asn1c_print_streams(arg_t *arg) {
277 compiler_streams_t *cs = arg->expr->data;
278 asn1p_expr_t *expr = arg->expr;
279 int i;
280
Lev Walkin64399722004-08-11 07:17:22 +0000281 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000282 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000283 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000284 continue;
285
286 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
287 _compiler_stream2str[i],
288 expr->Identifier);
289
Lev Walkin59004fa2004-08-20 13:37:01 +0000290 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700291 safe_fwrite(ot->buf, ot->len, 1, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 }
293 }
294
295 return 0;
296}
297
298static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000299asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000300 asn1p_expr_t *expr = arg->expr;
301 compiler_streams_t *cs = expr->data;
302 out_chunk_t *ot;
303 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000304 char *tmpname_c, *tmpname_h;
Lev Walkina4f8e942017-10-08 19:28:20 -0700305 char name_buf[FILENAME_MAX];
Lev Walkinb46156d2017-09-05 02:53:05 -0700306 const char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000307 const char *c_retained = "";
308 const char *h_retained = "";
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800309 char *filename;
Lev Walkinf15320b2004-06-03 03:38:44 +0000310
311 if(cs == NULL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700312 safe_fprintf(stderr, "Cannot compile %s at line %d\n",
Lev Walkinf15320b2004-06-03 03:38:44 +0000313 expr->Identifier, expr->_lineno);
314 return -1;
315 }
316
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800317 filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0));
318 fp_c = asn1c_open_file(filename, ".c", &tmpname_c);
319 fp_h = asn1c_open_file(filename, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000320 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000321 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
322 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000323 return -1;
324 }
325
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000326 generate_preamble(arg, fp_c, optc, argv);
327 generate_preamble(arg, fp_h, optc, argv);
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000328
Lev Walkina00d6b32006-03-21 03:40:38 +0000329 header_id = asn1c_make_identifier(0, expr, NULL);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700330 safe_fprintf(fp_h,
Lev Walkinf15320b2004-06-03 03:38:44 +0000331 "#ifndef\t_%s_H_\n"
332 "#define\t_%s_H_\n"
333 "\n", header_id, header_id);
334
Lev Walkinf2b2f372016-03-14 02:23:48 -0700335 safe_fprintf(fp_h, "\n");
Lev Walkin34944f22010-10-07 08:25:37 +0000336 HINCLUDE("asn_application.h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000337
Lev Walkinb9b8b952005-03-05 00:33:27 +0000338#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000339 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700340 safe_fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000341 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
342 if(actdep) asn1c_activate_dependency(deps, 0, ot->buf); \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700343 safe_fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000344 } \
345} while(0)
346
Lev Walkinb9b8b952005-03-05 00:33:27 +0000347 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000348
Lev Walkinf2b2f372016-03-14 02:23:48 -0700349 safe_fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000350 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
351 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800352 SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000353 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
354 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700355 safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000356
Lev Walkinae5540f2006-02-19 04:26:37 +0000357 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
358 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
359
Lev Walkinf2b2f372016-03-14 02:23:48 -0700360 safe_fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
Lev Walkin47370392006-02-19 04:30:15 +0000361
Lev Walkin34944f22010-10-07 08:25:37 +0000362 HINCLUDE("asn_internal.h");
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800363 safe_fprintf(fp_c, "#include \"%s.h\"\n\n", filename);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000364 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000365 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin9de6cd82017-08-10 05:47:46 -0700366 TQ_FOR(ot, &(cs->destination[OT_IOC_TABLES].chunks), next)
367 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000368 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700369 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000370 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700371 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin725883b2006-10-09 12:07:58 +0000372 TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700373 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000374 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700375 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkinf15320b2004-06-03 03:38:44 +0000376
Lev Walkin9de6cd82017-08-10 05:47:46 -0700377 assert(OT_MAX == 13); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000378
379 fclose(fp_c);
380 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000381
Lev Walkina4f8e942017-10-08 19:28:20 -0700382 int ret = snprintf(name_buf, sizeof(name_buf), "%s.c", filename);
383 assert(ret > 0 && ret < (ssize_t)sizeof(name_buf));
Lev Walkin4604d032005-03-04 08:52:50 +0000384
Lev Walkin4604d032005-03-04 08:52:50 +0000385 if(identical_files(name_buf, tmpname_c)) {
386 c_retained = " (contents unchanged)";
387 unlink(tmpname_c);
388 } else {
389 if(rename(tmpname_c, name_buf)) {
390 unlink(tmpname_c);
391 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000392 free(tmpname_c);
393 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000394 return -1;
395 }
396 }
397
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800398 sprintf(name_buf, "%s.h", filename);
Lev Walkin4604d032005-03-04 08:52:50 +0000399 if(identical_files(name_buf, tmpname_h)) {
400 h_retained = " (contents unchanged)";
401 unlink(tmpname_h);
402 } else {
403 if(rename(tmpname_h, name_buf)) {
404 unlink(tmpname_h);
405 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000406 free(tmpname_c);
407 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000408 return -1;
409 }
410 }
411
412 free(tmpname_c);
413 free(tmpname_h);
414
Lev Walkinf2b2f372016-03-14 02:23:48 -0700415 safe_fprintf(stderr, "Compiled %s.c%s\n",
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800416 filename, c_retained);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700417 safe_fprintf(stderr, "Compiled %s.h%s\n",
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800418 filename, h_retained);
419 free(filename);
Lev Walkinf15320b2004-06-03 03:38:44 +0000420 return 0;
421}
422
Lev Walkin4604d032005-03-04 08:52:50 +0000423static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000424generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700425 safe_fprintf(fp,
Lev Walkin8253ea92006-03-17 01:47:57 +0000426 "/*\n"
427 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
428 " * From ASN.1 module \"%s\"\n"
429 " * \tfound in \"%s\"\n",
430 arg->expr->module->ModuleName,
431 arg->expr->module->source_file_name);
432 if(optc > 1) {
433 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700434 safe_fprintf(fp, " * \t`asn1c ");
Lev Walkin8253ea92006-03-17 01:47:57 +0000435 for(i = 1; i < optc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700436 safe_fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
437 safe_fprintf(fp, "`\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000438 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700439 safe_fprintf(fp, " */\n\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000440 return 0;
441}
442
443static int
Lev Walkin4604d032005-03-04 08:52:50 +0000444identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000445 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000446 FILE *fp1, *fp2;
447 size_t olen, nlen;
448 int retval = 1; /* Files are identical */
449
Lev Walkin93659562010-11-20 09:47:13 -0800450#ifndef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000451 struct stat sb;
452
453 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
454 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
455 return 0; /* Files are not identical */
456 }
457#endif
458
Lev Walkin4604d032005-03-04 08:52:50 +0000459 fp1 = fopen(fname1, "r");
460 if(!fp1) { return 0; }
461 fp2 = fopen(fname2, "r");
462 if(!fp2) { fclose(fp1); return 0; }
463
464 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
465 nlen = fread(buf[1], 1, olen, fp2);
466 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
467 retval = 0;
468 break;
469 }
470 }
471 nlen = fread(buf[1], 1, 1, fp2);
472 if(nlen) retval = 0;
473
474 fclose(fp1);
475 fclose(fp2);
476 return retval;
477}
478
Lev Walkin4efbfb72005-02-25 14:20:30 +0000479/*
480 * Copy file for real.
481 */
482static int
483real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000484 unsigned char buf[4096];
485 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000486 FILE *fpsrc, *fpdst;
487 size_t len;
488 int retval = 0;
489
Lev Walkin4604d032005-03-04 08:52:50 +0000490 if(identical_files(src, dst))
491 return retval; /* Success, no need to copy for real. */
492
493 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000494 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkina895afb2005-10-06 10:09:34 +0000495 fpdst = asn1c_open_file(dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000496 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
497
498 while(!feof(fpsrc)) {
499 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700500 if(safe_fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000501 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000502 errno = EIO;
503 retval = -1;
504 break;
505 }
506 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000507 fclose(fpsrc);
508 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000509
510 /* Check if copied correctly, and rename into a permanent name */
511 if(retval) {
512 unlink(tmpname);
513 } else if(rename(tmpname, dst)) {
514 unlink(tmpname);
515 perror(tmpname);
516 retval = -1;
517 }
518 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000519 return retval;
520}
521
Lev Walkinf15320b2004-06-03 03:38:44 +0000522static int
523asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin93659562010-11-20 09:47:13 -0800524#ifdef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000525 int use_real_copy = 1;
526#else
Lev Walkin2655eb32013-03-25 19:09:04 -0700527 int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS);
Lev Walkina895afb2005-10-06 10:09:34 +0000528#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000529
Lev Walkina4f8e942017-10-08 19:28:20 -0700530 const char *fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000531 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000532 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000533 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000534 if(errno == EEXIST) {
535 struct stat sb1, sb2;
536 if(stat(path, &sb1) == 0
537 && stat(fname, &sb2) == 0
538 && sb1.st_dev == sb2.st_dev
539 && sb1.st_ino == sb2.st_ino) {
540 /*
541 * Nothing to do.
542 */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700543 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000544 "File %s is already here as %s\n",
545 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000546 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000547 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700548 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000549 "Retaining local %s (%s suggested)\n",
550 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000551 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000552 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000553 } else if(errno == ENOENT) {
554 /* Ignore this */
555 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000556 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700557 safe_fprintf(stderr, "%s %s -> %s failed: %s\n",
Lev Walkina895afb2005-10-06 10:09:34 +0000558 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000559 path, fname, strerror(errno));
560 return -1;
561 }
562 }
563
Lev Walkinf2b2f372016-03-14 02:23:48 -0700564 safe_fprintf(stderr, "%s %s\t-> %s\n",
Lev Walkina895afb2005-10-06 10:09:34 +0000565 use_real_copy ? "Copied" : "Symlinked", path, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000566
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000567 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000568}
569
Lev Walkin59b176e2005-11-26 11:25:14 +0000570
571static int
572generate_pdu_collection_file(arg_t *arg) {
573 asn1p_module_t *mod;
574 FILE *fp;
575
576 fp = asn1c_open_file("pdu_collection", ".c", 0);
577 if(fp == NULL) {
578 perror("pdu_collection.c");
579 return -1;
580 }
581
Lev Walkinf2b2f372016-03-14 02:23:48 -0700582 safe_fprintf(fp,
Lev Walkin59b176e2005-11-26 11:25:14 +0000583 "/*\n"
584 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
585 " */\n\n");
Lev Walkinf2b2f372016-03-14 02:23:48 -0700586 safe_fprintf(fp, "struct asn_TYPE_descriptor_s;\t"
Lev Walkin59b176e2005-11-26 11:25:14 +0000587 "/* Forward declaration */\n\n");
588
589 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
590 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkin66adab42006-09-23 02:52:12 +0000591 if(!include_type_to_pdu_collection(arg))
Lev Walkin59b176e2005-11-26 11:25:14 +0000592 continue;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700593 safe_fprintf(fp, "extern struct asn_TYPE_descriptor_s "
Lev Walkin59b176e2005-11-26 11:25:14 +0000594 "asn_DEF_%s;\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000595 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000596 }
597 }
598
Lev Walkinf2b2f372016-03-14 02:23:48 -0700599 safe_fprintf(fp, "\n\n");
600 safe_fprintf(fp, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000601 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
602 int mod_printed = 0;
603 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkin66adab42006-09-23 02:52:12 +0000604 if(!include_type_to_pdu_collection(arg))
Lev Walkin59b176e2005-11-26 11:25:14 +0000605 continue;
606 if(!mod_printed++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700607 safe_fprintf(fp, "\t/* From module %s in %s */\n",
Lev Walkin59b176e2005-11-26 11:25:14 +0000608 arg->expr->module->ModuleName,
609 arg->expr->module->source_file_name);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700610 safe_fprintf(fp, "\t&asn_DEF_%s,\t\n",
Lev Walkina00d6b32006-03-21 03:40:38 +0000611 asn1c_make_identifier(0, arg->expr, NULL));
Lev Walkin59b176e2005-11-26 11:25:14 +0000612 }
613 }
614
Lev Walkinf2b2f372016-03-14 02:23:48 -0700615 safe_fprintf(fp, "\t0\n};\n\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000616
Lev Walkin66adab42006-09-23 02:52:12 +0000617 pdu_collection_print_unused_types(arg);
618
Lev Walkin59b176e2005-11-26 11:25:14 +0000619 fclose(fp);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700620 safe_fprintf(stderr, "Generated pdu_collection.c\n");
Lev Walkin59b176e2005-11-26 11:25:14 +0000621
622 return 0;
623}
624
Lev Walkin66adab42006-09-23 02:52:12 +0000625static struct PDUType {
626 char *typename;
627 int used;
628} *pduType;
629static int pduTypes;
630
631static const char *
632generate_pdu_C_definition(void) {
Lev Walkina4f3d462017-08-05 22:40:14 -0700633 const char *src;
634 char *def;
Lev Walkin66adab42006-09-23 02:52:12 +0000635 char *dst;
Lev Walkina4f3d462017-08-05 22:40:14 -0700636 if(pduTypes == 0) return "";
637 def = malloc(strlen(pduType[0].typename) + 20);
638 assert(def);
639 strcpy(def, "-DPDU=");
640 for(src = pduType[0].typename, dst = def + 6; *src; src++, dst++) {
641 if((*dst = *src) == '-') {
642 *dst = '_';
643 }
644 }
645 *dst++ = ' ';
646 *dst = 0;
647 return def;
Lev Walkin66adab42006-09-23 02:52:12 +0000648}
649
650void
651asn1c__add_pdu_type(const char *ctypename) {
652 char *typename = strdup(ctypename);
653 assert(typename && *typename);
654
655 pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1));
656 assert(pduType);
657 pduType[pduTypes].used = 0;
658 pduType[pduTypes].typename = typename;
659 pduTypes++;
660}
661
Lev Walkin5230c642017-09-26 18:10:06 -0700662static void
663asn1c__cleanup_pdu_type() {
664 int i;
665 for(i = 0; i < pduTypes; i++) {
666 free(pduType[i].typename);
667 }
668 free(pduType);
669 pduType = NULL;
670 pduTypes = 0;
671}
672
Lev Walkin66adab42006-09-23 02:52:12 +0000673static int
674asn1c__pdu_type_lookup(const char *typename) {
675 int i;
676 for(i = 0; i < pduTypes; i++) {
677 struct PDUType *pt = &pduType[i];
678 if(strcmp(pt->typename, typename) == 0) {
679 pt->used++;
680 return 1;
681 }
682 }
683 return 0;
684}
685
686static int
687need_to_generate_pdu_collection(arg_t *arg) {
688 if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO))
689 return 1;
690 if(arg->flags & A1C_PDU_TYPE)
691 return (pduTypes > 1) ? 1 : 0;
692 return 0;
693}
694
695static void
696pdu_collection_print_unused_types(arg_t *arg) {
697 int i;
698 for(i = 0; i < pduTypes; i++) {
699 struct PDUType *pt = &pduType[i];
700 if(!pt->used) {
701 WARNING("Missing type specified in -pdu=%s",
702 pt->typename);
703 }
704 }
705}
706
707static int
708include_type_to_pdu_collection(arg_t *arg) {
709 if(!asn1_lang_map[arg->expr->meta_type]
710 [arg->expr->expr_type].type_cb)
711 return 0;
712
Lev Walkin8c44dff2017-08-26 17:17:27 -0700713 /* Parameterized types can't serve as PDU's without instantiation. */
Lev Walkin48e82d12017-10-19 03:06:35 -0700714 if(arg->expr->lhs_params) {
Lev Walkin8c44dff2017-08-26 17:17:27 -0700715 return 0;
Lev Walkin48e82d12017-10-19 03:06:35 -0700716 }
Lev Walkin8c44dff2017-08-26 17:17:27 -0700717
Lev Walkin66adab42006-09-23 02:52:12 +0000718 if((arg->flags & A1C_PDU_ALL)
719 || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced)
720 || asn1c__pdu_type_lookup(arg->expr->Identifier)) {
721 return 1;
722 }
723
724 return 0;
725}