blob: 1cc11a272fc623f4681403557b0e61b3d8f8ec74 [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 Walkin5f4dbb72017-11-11 14:52:11 -080035enum include_type_result {
36 TI_NOT_INCLUDED,
37 TI_INCLUDED_FROM_BULK,
38 TI_INCLUDED_FROM_CMDLINE
39};
40
Jon Ringle6431b1c2017-11-15 01:22:02 -050041static int asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
Lev Walkinf15320b2004-06-03 03:38:44 +000042static int asn1c_print_streams(arg_t *arg);
Jon Ringle6431b1c2017-11-15 01:22:02 -050043static int asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *, const char *, int, char **);
44static int asn1c_copy_over(arg_t *arg, const char *destdir, const char *path, const char *msg);
Lev Walkin4604d032005-03-04 08:52:50 +000045static int identical_files(const char *fname1, const char *fname2);
Lev Walkin66adab42006-09-23 02:52:12 +000046static int need_to_generate_pdu_collection(arg_t *arg);
Lev Walkin5f4dbb72017-11-11 14:52:11 -080047static abuf *generate_pdu_collection(arg_t *arg);
Jon Ringle6431b1c2017-11-15 01:22:02 -050048static int generate_pdu_collection_file(arg_t *arg, const char *destdir);
Lev Walkinc46b7cb2006-08-18 02:27:55 +000049static int generate_preamble(arg_t *, FILE *, int optc, char **argv);
Lev Walkin5f4dbb72017-11-11 14:52:11 -080050static enum include_type_result include_type_to_pdu_collection(arg_t *arg);
51static int pdu_collection_has_unused_types(arg_t *arg);
Lev Walkin66adab42006-09-23 02:52:12 +000052static const char *generate_pdu_C_definition(void);
Lev Walkin5230c642017-09-26 18:10:06 -070053static void asn1c__cleanup_pdu_type(void);
Lev Walkin5f4dbb72017-11-11 14:52:11 -080054static int asn1c__pdu_type_lookup(const char *typename);
Lev Walkinf15320b2004-06-03 03:38:44 +000055
Lev Walkin8a85b362017-09-26 22:54:44 -070056static int
Lev Walkin7d5d9302017-11-13 22:17:20 -080057asn1c__save_library_makefile(arg_t *arg, const asn1c_dep_chainset *deps,
Jon Ringle6431b1c2017-11-15 01:22:02 -050058 const char *datadir, const char *destdir, const char *makefile_name) {
Lev Walkinb85a8132005-08-18 13:38:19 +000059 asn1p_module_t *mod;
Lev Walkin8a85b362017-09-26 22:54:44 -070060 FILE *mkf;
Lev Walkinf15320b2004-06-03 03:38:44 +000061
Jon Ringle6431b1c2017-11-15 01:22:02 -050062 mkf = asn1c_open_file(destdir, makefile_name, "", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000063 if(mkf == NULL) {
Lev Walkin8a85b362017-09-26 22:54:44 -070064 perror(makefile_name);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000065 return -1;
66 }
Lev Walkinf15320b2004-06-03 03:38:44 +000067
Jon Ringle7871abf2017-11-13 21:29:40 -050068 safe_fprintf(mkf, "ASN_MODULE_SRCS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000069 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
70 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000071 if(asn1_lang_map[arg->expr->meta_type]
72 [arg->expr->expr_type].type_cb) {
Jon Ringle6431b1c2017-11-15 01:22:02 -050073 safe_fprintf(mkf, "\t\\\n\t%s%s.c", destdir,
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080074 asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
Lev Walkinf15320b2004-06-03 03:38:44 +000075 }
76 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000077 }
Jon Ringle7871abf2017-11-13 21:29:40 -050078 safe_fprintf(mkf, "\n\nASN_MODULE_HDRS=");
Lev Walkinb85a8132005-08-18 13:38:19 +000079 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
80 TQ_FOR(arg->expr, &(mod->members), next) {
Lev Walkind29bbce2004-09-23 22:19:14 +000081 if(asn1_lang_map[arg->expr->meta_type]
82 [arg->expr->expr_type].type_cb) {
Jon Ringle6431b1c2017-11-15 01:22:02 -050083 safe_fprintf(mkf, "\t\\\n\t%s%s.h", destdir,
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080084 asn1c_make_identifier(AMI_MASK_ONLY_SPACES, arg->expr, 0));
Lev Walkind29bbce2004-09-23 22:19:14 +000085 }
86 }
87 }
Lev Walkinf2b2f372016-03-14 02:23:48 -070088 safe_fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000089
Lev Walkinacd9f8b2004-08-19 13:29:03 +000090 /*
Lev Walkin8a85b362017-09-26 22:54:44 -070091 * Move necessary skeleton files and add them to Makefile.am.targets.
Lev Walkinacd9f8b2004-08-19 13:29:03 +000092 */
Lev Walkin7d5d9302017-11-13 22:17:20 -080093 asn1c_dep_chain *dlist = asn1c_deps_flatten(deps, ~FDEP_CONVERTER);
94 if(dlist) {
95 char dstpath[PATH_MAX];
Lev Walkinacd9f8b2004-08-19 13:29:03 +000096 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000097 size_t dlen = strlen(datadir);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000098
Lev Walkin7d5d9302017-11-13 22:17:20 -080099 assert(dlen < (sizeof(dstpath) / 2 - 2));
100 memcpy(dstpath, datadir, dlen);
101 dir_end = dstpath + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000102 *dir_end++ = '/';
103
Jon Ringle7871abf2017-11-13 21:29:40 -0500104 for(size_t i = 0; i < dlist->deps_count; i++) {
105 char where[32]; /* Location of the */
106 char *what_kind; /* HDRS or SRCS */
107 const asn1c_dep_filename *dep_file = dlist->deps[i];
Lev Walkin7d5d9302017-11-13 22:17:20 -0800108 char *fname = dep_file->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +0000109 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000110
Lev Walkin7d5d9302017-11-13 22:17:20 -0800111 assert(strlen(fname) < (sizeof(dstpath) / 2));
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000112 strcpy(dir_end, fname);
113
Jon Ringle7871abf2017-11-13 21:29:40 -0500114 if(arg->flags & A1C_DEBUG) {
115 snprintf(where, sizeof(where), "(line %d col %d)",
116 dep_file->lineno, dep_file->column);
117 } else {
118 where[0] = '\0';
119 }
Lev Walkind1c28aa2017-11-11 18:04:26 -0800120
Jon Ringle6431b1c2017-11-15 01:22:02 -0500121 if(asn1c_copy_over(arg, destdir, dstpath, where) == -1) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700122 safe_fprintf(mkf, ">>>ABORTED<<<");
Lev Walkinf15320b2004-06-03 03:38:44 +0000123 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +0000124 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +0000125 }
Lev Walkinf218e782006-09-12 06:21:18 +0000126
Jon Ringle7871abf2017-11-13 21:29:40 -0500127 /* HDRS versus SRCS */
128 dotH = strrchr(fname, 'h');
129 if(dotH && fname < dotH && dotH[-1] == '.' && !dotH[1]) {
130 what_kind = "HDRS";
131 } else {
132 what_kind = "SRCS";
133 }
Jon Ringle6431b1c2017-11-15 01:22:02 -0500134 safe_fprintf(mkf, "ASN_MODULE_%s+=%s%s\n", what_kind, destdir, fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000135 }
Bi-Ruei, Chiu6f348942016-11-08 15:41:23 +0800136
Lev Walkin7d5d9302017-11-13 22:17:20 -0800137 asn1c_dep_chain_free(dlist);
Lev Walkin8a85b362017-09-26 22:54:44 -0700138 }
139
140 safe_fprintf(
141 mkf,
142 "\n"
143 "ASN_MODULE_CFLAGS=%s%s",
144 (arg->flags & A1C_GEN_OER) ? "" : "-DASN_DISABLE_OER_SUPPORT ",
145 (arg->flags & A1C_GEN_PER) ? "" : "-DASN_DISABLE_PER_SUPPORT ");
146
147 safe_fprintf(
148 mkf,
149 "\n\n"
Jon Ringlef71745a2017-11-13 23:54:13 -0500150 "lib_LTLIBRARIES+=libasncodec.la\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700151 "libasncodec_la_SOURCES="
Jon Ringle7871abf2017-11-13 21:29:40 -0500152 "$(ASN_MODULE_SRCS) $(ASN_MODULE_HDRS)\n"
Jon Ringlef71745a2017-11-13 23:54:13 -0500153 "libasncodec_la_CPPFLAGS=-I$(top_srcdir)/%s\n"
154 "libasncodec_la_CFLAGS=$(ASN_MODULE_CFLAGS)\n"
155 "libasncodec_la_LDFLAGS=-lm\n", destdir);
Lev Walkin8a85b362017-09-26 22:54:44 -0700156 fclose(mkf);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500157 safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
Lev Walkin8a85b362017-09-26 22:54:44 -0700158
159 return 0;
160}
161
162static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500163asn1c__save_example_makefile(arg_t *arg, const asn1c_dep_chainset *deps, const char *datadir,
164 const char *destdir, const char *makefile_name,
Lev Walkin8a85b362017-09-26 22:54:44 -0700165 const char *library_makefile_name, int argc,
166 char **argv) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500167 FILE *mkf;
168 asn1c_dep_chain *dlist = asn1c_deps_flatten(deps, FDEP_CONVERTER);
Lev Walkin8a85b362017-09-26 22:54:44 -0700169
Jon Ringle6431b1c2017-11-15 01:22:02 -0500170 mkf = asn1c_open_file(destdir, makefile_name, "", 0);
Lev Walkin8a85b362017-09-26 22:54:44 -0700171 if(mkf == NULL) {
172 perror(makefile_name);
173 return -1;
174 }
Jon Ringle6431b1c2017-11-15 01:22:02 -0500175 safe_fprintf(mkf,
176 "include %s%s\n\n"
177 "LIBS += -lm\n"
178 "CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n"
179 "ASN_LIBRARY ?= libasncodec.a\n"
180 "ASN_PROGRAM ?= converter-example\n"
181 "ASN_PROGRAM_SRCS ?= ",
182 destdir, library_makefile_name,
183 (arg->flags & A1C_PDU_TYPE) ? generate_pdu_C_definition() : "",
184 need_to_generate_pdu_collection(arg) ? "-DASN_PDU_COLLECTION " : "");
Lev Walkin8a85b362017-09-26 22:54:44 -0700185
Jon Ringle6431b1c2017-11-15 01:22:02 -0500186 if(dlist) {
Lev Walkin7d5d9302017-11-13 22:17:20 -0800187 for(size_t i = 0; i < dlist->deps_count; i++) {
188 char dstpath[PATH_MAX];
189 int ret = snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir,
190 dlist->deps[i]->filename);
191 assert(ret > 0 && (size_t)ret < sizeof(dstpath));
Jon Ringle6431b1c2017-11-15 01:22:02 -0500192 if(asn1c_copy_over(arg, destdir, dstpath, "implicit") == -1) {
Lev Walkin7d5d9302017-11-13 22:17:20 -0800193 safe_fprintf(mkf, ">>>ABORTED<<<");
194 fclose(mkf);
195 return -1;
Jon Ringle6431b1c2017-11-15 01:22:02 -0500196 }
197 safe_fprintf(mkf, "\\\n\t%s%s", destdir, dlist->deps[i]->filename);
198 }
Lev Walkin7d5d9302017-11-13 22:17:20 -0800199 asn1c_dep_chain_free(dlist);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500200 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000201
Lev Walkin66adab42006-09-23 02:52:12 +0000202 if(need_to_generate_pdu_collection(arg)) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500203 safe_fprintf(mkf, "\\\n\t%spdu_collection.c", destdir);
204 if(generate_pdu_collection_file(arg, destdir))
Lev Walkin59b176e2005-11-26 11:25:14 +0000205 return -1;
206 }
207
Lev Walkin8a85b362017-09-26 22:54:44 -0700208 safe_fprintf(
209 mkf,
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700210 "\n\nall: $(ASN_PROGRAM)\n"
Jon Ringle7871abf2017-11-13 21:29:40 -0500211 "\n$(ASN_PROGRAM): $(ASN_LIBRARY) $(ASN_PROGRAM_SRCS:.c=.o)"
212 "\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) $(ASN_PROGRAM_SRCS:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n"
213 "\n$(ASN_LIBRARY): $(ASN_MODULE_SRCS:.c=.o)"
214 "\n\t$(AR) rcs $@ $(ASN_MODULE_SRCS:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700215 "\n.SUFFIXES:"
216 "\n.SUFFIXES: .c .o\n"
217 "\n.c.o:"
218 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
219 "\nclean:"
Lev Walkine24bad12017-10-09 23:58:15 -0700220 "\n\trm -f $(ASN_PROGRAM) $(ASN_LIBRARY)"
Jon Ringle7871abf2017-11-13 21:29:40 -0500221 "\n\trm -f $(ASN_MODULE_SRCS:.c=.o) $(ASN_PROGRAM_SRCS:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700222 "\nregen: regenerate-from-asn1-source\n"
223 "\nregenerate-from-asn1-source:\n\t");
Lev Walkin866cff12005-03-05 00:50:53 +0000224
Lev Walkin8a85b362017-09-26 22:54:44 -0700225 for(int i = 0; i < argc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700226 safe_fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
227 safe_fprintf(mkf, "\n\n");
Lev Walkin866cff12005-03-05 00:50:53 +0000228
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000229 fclose(mkf);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500230 safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
231 return 0;
Lev Walkin8a85b362017-09-26 22:54:44 -0700232}
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000233
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800234static int
235can_generate_pdu_collection(arg_t *arg) {
236 abuf *buf = generate_pdu_collection(arg);
237 if(!buf) {
238 return -1;
239 }
240 abuf_free(buf);
241 return 0;
242}
243
Lev Walkin8a85b362017-09-26 22:54:44 -0700244int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500245asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
246 int argc, int optc, char **argv) {
Lev Walkin8a85b362017-09-26 22:54:44 -0700247 int ret = -1;
Lev Walkin5230c642017-09-26 18:10:06 -0700248
Jon Ringle6431b1c2017-11-15 01:22:02 -0500249 const char* program_makefile = "Makefile.am.example";
250 const char* library_makefile = "Makefile.am.libasncodec";
251
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800252 /*
253 * Early check that we can properly generate PDU collection.
254 */
255 if(can_generate_pdu_collection(arg) == -1) {
256 return -1;
257 }
258
Lev Walkin7d5d9302017-11-13 22:17:20 -0800259 asn1c_dep_chainset *deps;
Lev Walkin8a85b362017-09-26 22:54:44 -0700260 do {
261 asn1p_module_t *mod;
262
263 deps = asn1c_read_file_dependencies(arg, datadir);
264 if(!deps && datadir) {
265 WARNING(
266 "Cannot read file-dependencies information "
267 "from %s\n",
268 datadir);
269 }
270
271 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
272 TQ_FOR(arg->expr, &(mod->members), next) {
273 if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type]
274 .type_cb) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500275 if(asn1c_dump_streams(arg, deps, destdir, optc, argv)) break;
Lev Walkin8a85b362017-09-26 22:54:44 -0700276 }
277 }
278 }
279
280 /*
281 * Dump out the Makefile template and the rest of the support code.
282 */
283 if((arg->flags & A1C_PRINT_COMPILED)
284 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
285 ret = 0; /* Success */
286 break;
287 }
288
Jon Ringle6431b1c2017-11-15 01:22:02 -0500289 ret = asn1c__save_library_makefile(arg, deps, datadir, destdir,
290 library_makefile);
Lev Walkin8a85b362017-09-26 22:54:44 -0700291 if(ret) break;
Lev Walkind1c28aa2017-11-11 18:04:26 -0800292
293 if(arg->flags & A1C_GEN_EXAMPLE) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500294 ret = asn1c__save_example_makefile(arg, deps, datadir, destdir,
295 program_makefile,
296 library_makefile, argc, argv);
Lev Walkind1c28aa2017-11-11 18:04:26 -0800297 if(ret) break;
298 }
Lev Walkin8a85b362017-09-26 22:54:44 -0700299 } while(0);
300
Lev Walkin7d5d9302017-11-13 22:17:20 -0800301 asn1c_dep_chainset_free(deps);
Lev Walkin8a85b362017-09-26 22:54:44 -0700302 asn1c__cleanup_pdu_type();
303
304 return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000305}
306
307/*
308 * Dump the streams.
309 */
310static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500311asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
Lev Walkin7d5d9302017-11-13 22:17:20 -0800312 char **argv) {
313 if(arg->flags & A1C_PRINT_COMPILED) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000314 return asn1c_print_streams(arg);
315 } else {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500316 return asn1c_save_streams(arg, deps, destdir, optc, argv);
Lev Walkinf15320b2004-06-03 03:38:44 +0000317 }
318}
319
320static int
321asn1c_print_streams(arg_t *arg) {
322 compiler_streams_t *cs = arg->expr->data;
323 asn1p_expr_t *expr = arg->expr;
324 int i;
325
Lev Walkin64399722004-08-11 07:17:22 +0000326 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000328 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000329 continue;
330
331 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
332 _compiler_stream2str[i],
333 expr->Identifier);
334
Lev Walkin59004fa2004-08-20 13:37:01 +0000335 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700336 safe_fwrite(ot->buf, ot->len, 1, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000337 }
338 }
339
340 return 0;
341}
342
343static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500344asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
Lev Walkin7d5d9302017-11-13 22:17:20 -0800345 char **argv) {
346 asn1p_expr_t *expr = arg->expr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000347 compiler_streams_t *cs = expr->data;
348 out_chunk_t *ot;
349 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000350 char *tmpname_c, *tmpname_h;
Lev Walkina4f8e942017-10-08 19:28:20 -0700351 char name_buf[FILENAME_MAX];
Lev Walkinb46156d2017-09-05 02:53:05 -0700352 const char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000353 const char *c_retained = "";
354 const char *h_retained = "";
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800355 char *filename;
Lev Walkinf15320b2004-06-03 03:38:44 +0000356
357 if(cs == NULL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700358 safe_fprintf(stderr, "Cannot compile %s at line %d\n",
Lev Walkinf15320b2004-06-03 03:38:44 +0000359 expr->Identifier, expr->_lineno);
360 return -1;
361 }
362
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800363 filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0));
Jon Ringle6431b1c2017-11-15 01:22:02 -0500364 fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c);
365 fp_h = asn1c_open_file(destdir, filename, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000366 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000367 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
368 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 return -1;
370 }
371
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000372 generate_preamble(arg, fp_c, optc, argv);
373 generate_preamble(arg, fp_h, optc, argv);
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000374
Lev Walkina00d6b32006-03-21 03:40:38 +0000375 header_id = asn1c_make_identifier(0, expr, NULL);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700376 safe_fprintf(fp_h,
Lev Walkinf15320b2004-06-03 03:38:44 +0000377 "#ifndef\t_%s_H_\n"
378 "#define\t_%s_H_\n"
379 "\n", header_id, header_id);
380
Lev Walkinf2b2f372016-03-14 02:23:48 -0700381 safe_fprintf(fp_h, "\n");
Lev Walkin34944f22010-10-07 08:25:37 +0000382 HINCLUDE("asn_application.h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000383
Lev Walkinb9b8b952005-03-05 00:33:27 +0000384#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000385 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700386 safe_fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000387 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
Lev Walkin7d5d9302017-11-13 22:17:20 -0800388 if(actdep) asn1c_activate_dependency(deps, ot->buf, header_id); \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700389 safe_fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000390 } \
391} while(0)
392
Lev Walkinb9b8b952005-03-05 00:33:27 +0000393 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000394
Lev Walkinf2b2f372016-03-14 02:23:48 -0700395 safe_fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000396 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
397 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800398 SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000399 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
400 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700401 safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000402
Lev Walkinae5540f2006-02-19 04:26:37 +0000403 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
404 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
405
Lev Walkinf2b2f372016-03-14 02:23:48 -0700406 safe_fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
Lev Walkin47370392006-02-19 04:30:15 +0000407
Lev Walkin34944f22010-10-07 08:25:37 +0000408 HINCLUDE("asn_internal.h");
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800409 safe_fprintf(fp_c, "#include \"%s.h\"\n\n", filename);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000410 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000411 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin9de6cd82017-08-10 05:47:46 -0700412 TQ_FOR(ot, &(cs->destination[OT_IOC_TABLES].chunks), next)
413 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000414 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700415 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000416 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700417 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin725883b2006-10-09 12:07:58 +0000418 TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700419 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000420 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700421 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkinf15320b2004-06-03 03:38:44 +0000422
Lev Walkin9de6cd82017-08-10 05:47:46 -0700423 assert(OT_MAX == 13); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000424
425 fclose(fp_c);
426 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000427
Jon Ringle6431b1c2017-11-15 01:22:02 -0500428 int ret = snprintf(name_buf, sizeof(name_buf), "%s%s.c", destdir, filename);
Lev Walkina4f8e942017-10-08 19:28:20 -0700429 assert(ret > 0 && ret < (ssize_t)sizeof(name_buf));
Lev Walkin4604d032005-03-04 08:52:50 +0000430
Lev Walkin4604d032005-03-04 08:52:50 +0000431 if(identical_files(name_buf, tmpname_c)) {
432 c_retained = " (contents unchanged)";
433 unlink(tmpname_c);
434 } else {
435 if(rename(tmpname_c, name_buf)) {
436 unlink(tmpname_c);
437 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000438 free(tmpname_c);
439 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000440 return -1;
441 }
442 }
443
Jon Ringle6431b1c2017-11-15 01:22:02 -0500444 sprintf(name_buf, "%s%s.h", destdir, filename);
Lev Walkin4604d032005-03-04 08:52:50 +0000445 if(identical_files(name_buf, tmpname_h)) {
446 h_retained = " (contents unchanged)";
447 unlink(tmpname_h);
448 } else {
449 if(rename(tmpname_h, name_buf)) {
450 unlink(tmpname_h);
451 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000452 free(tmpname_c);
453 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000454 return -1;
455 }
456 }
457
458 free(tmpname_c);
459 free(tmpname_h);
460
Jon Ringle6431b1c2017-11-15 01:22:02 -0500461 safe_fprintf(stderr, "Compiled %s%s.c%s\n",
462 destdir, filename, c_retained);
463 safe_fprintf(stderr, "Compiled %s%s.h%s\n",
464 destdir, filename, h_retained);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800465 free(filename);
Lev Walkinf15320b2004-06-03 03:38:44 +0000466 return 0;
467}
468
Lev Walkin4604d032005-03-04 08:52:50 +0000469static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000470generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700471 safe_fprintf(fp,
Lev Walkin8253ea92006-03-17 01:47:57 +0000472 "/*\n"
473 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
474 " * From ASN.1 module \"%s\"\n"
475 " * \tfound in \"%s\"\n",
476 arg->expr->module->ModuleName,
477 arg->expr->module->source_file_name);
478 if(optc > 1) {
479 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700480 safe_fprintf(fp, " * \t`asn1c ");
Lev Walkin8253ea92006-03-17 01:47:57 +0000481 for(i = 1; i < optc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700482 safe_fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
483 safe_fprintf(fp, "`\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000484 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700485 safe_fprintf(fp, " */\n\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000486 return 0;
487}
488
489static int
Lev Walkin4604d032005-03-04 08:52:50 +0000490identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000491 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000492 FILE *fp1, *fp2;
493 size_t olen, nlen;
494 int retval = 1; /* Files are identical */
495
Lev Walkin93659562010-11-20 09:47:13 -0800496#ifndef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000497 struct stat sb;
498
499 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
500 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
501 return 0; /* Files are not identical */
502 }
503#endif
504
Lev Walkin4604d032005-03-04 08:52:50 +0000505 fp1 = fopen(fname1, "r");
506 if(!fp1) { return 0; }
507 fp2 = fopen(fname2, "r");
508 if(!fp2) { fclose(fp1); return 0; }
509
510 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
511 nlen = fread(buf[1], 1, olen, fp2);
512 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
513 retval = 0;
514 break;
515 }
516 }
517 nlen = fread(buf[1], 1, 1, fp2);
518 if(nlen) retval = 0;
519
520 fclose(fp1);
521 fclose(fp2);
522 return retval;
523}
524
Lev Walkin4efbfb72005-02-25 14:20:30 +0000525/*
526 * Copy file for real.
527 */
528static int
529real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000530 unsigned char buf[4096];
531 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000532 FILE *fpsrc, *fpdst;
533 size_t len;
534 int retval = 0;
535
Lev Walkin4604d032005-03-04 08:52:50 +0000536 if(identical_files(src, dst))
537 return retval; /* Success, no need to copy for real. */
538
539 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000540 if(!fpsrc) { errno = EIO; return -1; }
Jon Ringle6431b1c2017-11-15 01:22:02 -0500541 fpdst = asn1c_open_file(NULL, dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000542 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
543
544 while(!feof(fpsrc)) {
545 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700546 if(safe_fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000547 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000548 errno = EIO;
549 retval = -1;
550 break;
551 }
552 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000553 fclose(fpsrc);
554 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000555
556 /* Check if copied correctly, and rename into a permanent name */
557 if(retval) {
558 unlink(tmpname);
559 } else if(rename(tmpname, dst)) {
560 unlink(tmpname);
561 perror(tmpname);
562 retval = -1;
563 }
564 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000565 return retval;
566}
567
Lev Walkinf15320b2004-06-03 03:38:44 +0000568static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500569asn1c_copy_over(arg_t *arg, const char* destdir, const char *path, const char *msg) {
Lev Walkin93659562010-11-20 09:47:13 -0800570#ifdef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000571 int use_real_copy = 1;
572#else
Lev Walkin2655eb32013-03-25 19:09:04 -0700573 int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS);
Lev Walkina895afb2005-10-06 10:09:34 +0000574#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000575
Jon Ringle6431b1c2017-11-15 01:22:02 -0500576 const char *fname = a1c_basename(path, destdir);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000577 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000578 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000579 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000580 if(errno == EEXIST) {
581 struct stat sb1, sb2;
582 if(stat(path, &sb1) == 0
583 && stat(fname, &sb2) == 0
584 && sb1.st_dev == sb2.st_dev
585 && sb1.st_ino == sb2.st_ino) {
586 /*
587 * Nothing to do.
588 */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700589 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000590 "File %s is already here as %s\n",
591 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000592 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700594 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000595 "Retaining local %s (%s suggested)\n",
596 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000597 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000598 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000599 } else if(errno == ENOENT) {
600 /* Ignore this */
601 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000602 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700603 safe_fprintf(stderr, "%s %s -> %s failed: %s\n",
Lev Walkina895afb2005-10-06 10:09:34 +0000604 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000605 path, fname, strerror(errno));
606 return -1;
607 }
608 }
609
Lev Walkin7d5d9302017-11-13 22:17:20 -0800610 const int has_msg = msg && *msg;
611 safe_fprintf(stderr, "%s %s\t-> %s%s%s\n",
612 use_real_copy ? "Copied" : "Symlinked", path, fname,
613 has_msg ? " " : "", has_msg ? msg : "");
Lev Walkinf15320b2004-06-03 03:38:44 +0000614
Lev Walkin7d5d9302017-11-13 22:17:20 -0800615 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000616}
617
Lev Walkin59b176e2005-11-26 11:25:14 +0000618
619static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500620generate_pdu_collection_file(arg_t *arg, const char* destdir) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800621 abuf *buf = generate_pdu_collection(arg);
622 assert(buf);
Lev Walkin59b176e2005-11-26 11:25:14 +0000623
Jon Ringle6431b1c2017-11-15 01:22:02 -0500624 FILE *fp = asn1c_open_file(destdir, "pdu_collection", ".c", 0);
Lev Walkin59b176e2005-11-26 11:25:14 +0000625 if(fp == NULL) {
626 perror("pdu_collection.c");
627 return -1;
628 }
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800629 safe_fwrite(buf->buffer, buf->length, 1, fp);
630 fclose(fp);
Lev Walkin59b176e2005-11-26 11:25:14 +0000631
Lev Walkinf2b2f372016-03-14 02:23:48 -0700632 safe_fprintf(stderr, "Generated pdu_collection.c\n");
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800633 return 0;
634}
Lev Walkin59b176e2005-11-26 11:25:14 +0000635
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800636static abuf *
637generate_pdu_collection(arg_t *arg) {
638 asn1p_module_t *mod;
639 abuf *buf = abuf_new();
640
641 abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION
642 " (http://lionet.info/asn1c)\n */\n\n");
Lev Walkin3be49192017-11-11 16:13:32 -0800643 abuf_printf(buf,
644 "struct asn_TYPE_descriptor_s;\t"
645 "/* Forward declaration */\n\n");
646
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800647
648 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
649 TQ_FOR(arg->expr, &(mod->members), next) {
650 if(include_type_to_pdu_collection(arg) == TI_NOT_INCLUDED) continue;
651 abuf_printf(buf,
Lev Walkin3be49192017-11-11 16:13:32 -0800652 "extern struct asn_TYPE_descriptor_s "
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800653 "asn_DEF_%s;\n",
654 asn1c_make_identifier(0, arg->expr, NULL));
655 }
656 }
657
658 abuf_printf(buf, "\n\n");
659 abuf_printf(buf, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
660 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
661 int mod_printed = 0;
662 TQ_FOR(arg->expr, &(mod->members), next) {
663 switch(include_type_to_pdu_collection(arg)) {
664 case TI_NOT_INCLUDED:
665 continue;
666 case TI_INCLUDED_FROM_BULK:
667 /* Increment */
668 asn1c__pdu_type_lookup(arg->expr->Identifier);
669 break;
670 case TI_INCLUDED_FROM_CMDLINE:
671 break;
672 }
673 if(!mod_printed++) {
674 abuf_printf(buf, "\t/* From module %s in %s */\n",
675 arg->expr->module->ModuleName,
676 arg->expr->module->source_file_name);
677 }
678 abuf_printf(buf, "\t&asn_DEF_%s,\t\n",
679 asn1c_make_identifier(0, arg->expr, NULL));
680 }
681 }
682
683 abuf_printf(buf, "\t0\n};\n\n");
684
685 if(pdu_collection_has_unused_types(arg)) {
686 abuf_free(buf);
687 return NULL;
688 }
689
690 return buf;
Lev Walkin59b176e2005-11-26 11:25:14 +0000691}
692
Lev Walkin66adab42006-09-23 02:52:12 +0000693static struct PDUType {
694 char *typename;
695 int used;
696} *pduType;
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800697static size_t pduTypes;
Lev Walkin66adab42006-09-23 02:52:12 +0000698
699static const char *
700generate_pdu_C_definition(void) {
Lev Walkina4f3d462017-08-05 22:40:14 -0700701 const char *src;
702 char *def;
Lev Walkin66adab42006-09-23 02:52:12 +0000703 char *dst;
Lev Walkina4f3d462017-08-05 22:40:14 -0700704 if(pduTypes == 0) return "";
705 def = malloc(strlen(pduType[0].typename) + 20);
706 assert(def);
707 strcpy(def, "-DPDU=");
708 for(src = pduType[0].typename, dst = def + 6; *src; src++, dst++) {
709 if((*dst = *src) == '-') {
710 *dst = '_';
711 }
712 }
713 *dst++ = ' ';
714 *dst = 0;
715 return def;
Lev Walkin66adab42006-09-23 02:52:12 +0000716}
717
718void
719asn1c__add_pdu_type(const char *ctypename) {
720 char *typename = strdup(ctypename);
721 assert(typename && *typename);
722
723 pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1));
724 assert(pduType);
725 pduType[pduTypes].used = 0;
726 pduType[pduTypes].typename = typename;
727 pduTypes++;
728}
729
Lev Walkin5230c642017-09-26 18:10:06 -0700730static void
731asn1c__cleanup_pdu_type() {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800732 for(size_t i = 0; i < pduTypes; i++) {
Lev Walkin5230c642017-09-26 18:10:06 -0700733 free(pduType[i].typename);
734 }
735 free(pduType);
736 pduType = NULL;
737 pduTypes = 0;
738}
739
Lev Walkin66adab42006-09-23 02:52:12 +0000740static int
741asn1c__pdu_type_lookup(const char *typename) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800742 for(size_t i = 0; i < pduTypes; i++) {
743 struct PDUType *pt = &pduType[i];
744 if(strcmp(pt->typename, typename) == 0) {
745 pt->used++;
746 return 1;
747 }
748 }
749 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000750}
751
752static int
753need_to_generate_pdu_collection(arg_t *arg) {
Lev Walkin3be49192017-11-11 16:13:32 -0800754 /* If -pdu=all or -pdu=auto are given, we need to generate one. */
Lev Walkin66adab42006-09-23 02:52:12 +0000755 if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO))
756 return 1;
Lev Walkin3be49192017-11-11 16:13:32 -0800757
758 /*
759 * If no -pdu=... flags were given, need to do it, too,
760 * effectively producing -pdu=auto.
761 */
762 if(!(arg->flags & (A1C_PDU_ALL | A1C_PDU_AUTO | A1C_PDU_TYPE))) return 1;
763
764 if(arg->flags & A1C_PDU_TYPE) {
765 return (pduTypes > 1) ? 1 : 0;
766 }
Lev Walkin66adab42006-09-23 02:52:12 +0000767 return 0;
768}
769
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800770static int
771pdu_collection_has_unused_types(arg_t *arg) {
772 int ret = 0;
773
774 for(size_t i = 0; i < pduTypes; i++) {
775 struct PDUType *pt = &pduType[i];
776 if(!pt->used) {
777 FATAL("Unknown ASN.1 type specified in -pdu=%s", pt->typename);
778 ret = -1;
779 }
780 }
781
782 return ret;
Lev Walkin66adab42006-09-23 02:52:12 +0000783}
784
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800785static enum include_type_result
Lev Walkin66adab42006-09-23 02:52:12 +0000786include_type_to_pdu_collection(arg_t *arg) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800787 if(!asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type].type_cb)
788 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000789
Lev Walkin8c44dff2017-08-26 17:17:27 -0700790 /* Parameterized types can't serve as PDU's without instantiation. */
Lev Walkin48e82d12017-10-19 03:06:35 -0700791 if(arg->expr->lhs_params) {
Lev Walkin8c44dff2017-08-26 17:17:27 -0700792 return 0;
Lev Walkin48e82d12017-10-19 03:06:35 -0700793 }
Lev Walkin8c44dff2017-08-26 17:17:27 -0700794
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800795 if((arg->flags & A1C_PDU_ALL)
Lev Walkin3be49192017-11-11 16:13:32 -0800796 /* -pdu=auto */
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800797 || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced)
Lev Walkin3be49192017-11-11 16:13:32 -0800798 /* No -pdu=... whatsoever, act as if -pdu=auto */
799 || (!(arg->flags & (A1C_PDU_ALL | A1C_PDU_AUTO | A1C_PDU_TYPE))
800 && !arg->expr->_type_referenced)
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800801 || asn1c__pdu_type_lookup(arg->expr->Identifier)) {
802 return 1;
803 }
Lev Walkin66adab42006-09-23 02:52:12 +0000804
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800805 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000806}