blob: 47888d06f37147b27a70c718b144318aff8f6edc [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"
150 "lib_LTLIBRARIES=libasncodec.la\n"
151 "libasncodec_la_SOURCES="
Jon Ringle7871abf2017-11-13 21:29:40 -0500152 "$(ASN_MODULE_SRCS) $(ASN_MODULE_HDRS)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700153 "libasncodec_la_CFLAGS=$(ASN_MODULE_CFLAGS)\n");
154 fclose(mkf);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500155 safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
Lev Walkin8a85b362017-09-26 22:54:44 -0700156
157 return 0;
158}
159
160static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500161asn1c__save_example_makefile(arg_t *arg, const asn1c_dep_chainset *deps, const char *datadir,
162 const char *destdir, const char *makefile_name,
Lev Walkin8a85b362017-09-26 22:54:44 -0700163 const char *library_makefile_name, int argc,
164 char **argv) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500165 FILE *mkf;
166 asn1c_dep_chain *dlist = asn1c_deps_flatten(deps, FDEP_CONVERTER);
Lev Walkin8a85b362017-09-26 22:54:44 -0700167
Jon Ringle6431b1c2017-11-15 01:22:02 -0500168 mkf = asn1c_open_file(destdir, makefile_name, "", 0);
Lev Walkin8a85b362017-09-26 22:54:44 -0700169 if(mkf == NULL) {
170 perror(makefile_name);
171 return -1;
172 }
Jon Ringle6431b1c2017-11-15 01:22:02 -0500173 safe_fprintf(mkf,
174 "include %s%s\n\n"
175 "LIBS += -lm\n"
176 "CFLAGS += $(ASN_MODULE_CFLAGS) %s%s-I.\n"
177 "ASN_LIBRARY ?= libasncodec.a\n"
178 "ASN_PROGRAM ?= converter-example\n"
179 "ASN_PROGRAM_SRCS ?= ",
180 destdir, library_makefile_name,
181 (arg->flags & A1C_PDU_TYPE) ? generate_pdu_C_definition() : "",
182 need_to_generate_pdu_collection(arg) ? "-DASN_PDU_COLLECTION " : "");
Lev Walkin8a85b362017-09-26 22:54:44 -0700183
Jon Ringle6431b1c2017-11-15 01:22:02 -0500184 if(dlist) {
Lev Walkin7d5d9302017-11-13 22:17:20 -0800185 for(size_t i = 0; i < dlist->deps_count; i++) {
186 char dstpath[PATH_MAX];
187 int ret = snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir,
188 dlist->deps[i]->filename);
189 assert(ret > 0 && (size_t)ret < sizeof(dstpath));
Jon Ringle6431b1c2017-11-15 01:22:02 -0500190 if(asn1c_copy_over(arg, destdir, dstpath, "implicit") == -1) {
Lev Walkin7d5d9302017-11-13 22:17:20 -0800191 safe_fprintf(mkf, ">>>ABORTED<<<");
192 fclose(mkf);
193 return -1;
Jon Ringle6431b1c2017-11-15 01:22:02 -0500194 }
195 safe_fprintf(mkf, "\\\n\t%s%s", destdir, dlist->deps[i]->filename);
196 }
Lev Walkin7d5d9302017-11-13 22:17:20 -0800197 asn1c_dep_chain_free(dlist);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500198 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000199
Lev Walkin66adab42006-09-23 02:52:12 +0000200 if(need_to_generate_pdu_collection(arg)) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500201 safe_fprintf(mkf, "\\\n\t%spdu_collection.c", destdir);
202 if(generate_pdu_collection_file(arg, destdir))
Lev Walkin59b176e2005-11-26 11:25:14 +0000203 return -1;
204 }
205
Lev Walkin8a85b362017-09-26 22:54:44 -0700206 safe_fprintf(
207 mkf,
Lev Walkin40b8a7a2017-10-08 22:36:29 -0700208 "\n\nall: $(ASN_PROGRAM)\n"
Jon Ringle7871abf2017-11-13 21:29:40 -0500209 "\n$(ASN_PROGRAM): $(ASN_LIBRARY) $(ASN_PROGRAM_SRCS:.c=.o)"
210 "\n\t$(CC) $(CFLAGS) $(CPPFLAGS) -o $(ASN_PROGRAM) $(ASN_PROGRAM_SRCS:.c=.o) $(LDFLAGS) $(ASN_LIBRARY) $(LIBS)\n"
211 "\n$(ASN_LIBRARY): $(ASN_MODULE_SRCS:.c=.o)"
212 "\n\t$(AR) rcs $@ $(ASN_MODULE_SRCS:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700213 "\n.SUFFIXES:"
214 "\n.SUFFIXES: .c .o\n"
215 "\n.c.o:"
216 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
217 "\nclean:"
Lev Walkine24bad12017-10-09 23:58:15 -0700218 "\n\trm -f $(ASN_PROGRAM) $(ASN_LIBRARY)"
Jon Ringle7871abf2017-11-13 21:29:40 -0500219 "\n\trm -f $(ASN_MODULE_SRCS:.c=.o) $(ASN_PROGRAM_SRCS:.c=.o)\n"
Lev Walkin8a85b362017-09-26 22:54:44 -0700220 "\nregen: regenerate-from-asn1-source\n"
221 "\nregenerate-from-asn1-source:\n\t");
Lev Walkin866cff12005-03-05 00:50:53 +0000222
Lev Walkin8a85b362017-09-26 22:54:44 -0700223 for(int i = 0; i < argc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700224 safe_fprintf(mkf, "%s%s", i ? " " : "", argv[i]);
225 safe_fprintf(mkf, "\n\n");
Lev Walkin866cff12005-03-05 00:50:53 +0000226
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000227 fclose(mkf);
Jon Ringle6431b1c2017-11-15 01:22:02 -0500228 safe_fprintf(stderr, "Generated %s%s\n", destdir, makefile_name);
229 return 0;
Lev Walkin8a85b362017-09-26 22:54:44 -0700230}
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000231
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800232static int
233can_generate_pdu_collection(arg_t *arg) {
234 abuf *buf = generate_pdu_collection(arg);
235 if(!buf) {
236 return -1;
237 }
238 abuf_free(buf);
239 return 0;
240}
241
Lev Walkin8a85b362017-09-26 22:54:44 -0700242int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500243asn1c_save_compiled_output(arg_t *arg, const char *datadir, const char *destdir,
244 int argc, int optc, char **argv) {
Lev Walkin8a85b362017-09-26 22:54:44 -0700245 int ret = -1;
Lev Walkin5230c642017-09-26 18:10:06 -0700246
Jon Ringle6431b1c2017-11-15 01:22:02 -0500247 const char* program_makefile = "Makefile.am.example";
248 const char* library_makefile = "Makefile.am.libasncodec";
249
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800250 /*
251 * Early check that we can properly generate PDU collection.
252 */
253 if(can_generate_pdu_collection(arg) == -1) {
254 return -1;
255 }
256
Lev Walkin7d5d9302017-11-13 22:17:20 -0800257 asn1c_dep_chainset *deps;
Lev Walkin8a85b362017-09-26 22:54:44 -0700258 do {
259 asn1p_module_t *mod;
260
261 deps = asn1c_read_file_dependencies(arg, datadir);
262 if(!deps && datadir) {
263 WARNING(
264 "Cannot read file-dependencies information "
265 "from %s\n",
266 datadir);
267 }
268
269 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
270 TQ_FOR(arg->expr, &(mod->members), next) {
271 if(asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type]
272 .type_cb) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500273 if(asn1c_dump_streams(arg, deps, destdir, optc, argv)) break;
Lev Walkin8a85b362017-09-26 22:54:44 -0700274 }
275 }
276 }
277
278 /*
279 * Dump out the Makefile template and the rest of the support code.
280 */
281 if((arg->flags & A1C_PRINT_COMPILED)
282 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
283 ret = 0; /* Success */
284 break;
285 }
286
Jon Ringle6431b1c2017-11-15 01:22:02 -0500287 ret = asn1c__save_library_makefile(arg, deps, datadir, destdir,
288 library_makefile);
Lev Walkin8a85b362017-09-26 22:54:44 -0700289 if(ret) break;
Lev Walkind1c28aa2017-11-11 18:04:26 -0800290
291 if(arg->flags & A1C_GEN_EXAMPLE) {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500292 ret = asn1c__save_example_makefile(arg, deps, datadir, destdir,
293 program_makefile,
294 library_makefile, argc, argv);
Lev Walkind1c28aa2017-11-11 18:04:26 -0800295 if(ret) break;
296 }
Lev Walkin8a85b362017-09-26 22:54:44 -0700297 } while(0);
298
Lev Walkin7d5d9302017-11-13 22:17:20 -0800299 asn1c_dep_chainset_free(deps);
Lev Walkin8a85b362017-09-26 22:54:44 -0700300 asn1c__cleanup_pdu_type();
301
302 return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000303}
304
305/*
306 * Dump the streams.
307 */
308static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500309asn1c_dump_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
Lev Walkin7d5d9302017-11-13 22:17:20 -0800310 char **argv) {
311 if(arg->flags & A1C_PRINT_COMPILED) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000312 return asn1c_print_streams(arg);
313 } else {
Jon Ringle6431b1c2017-11-15 01:22:02 -0500314 return asn1c_save_streams(arg, deps, destdir, optc, argv);
Lev Walkinf15320b2004-06-03 03:38:44 +0000315 }
316}
317
318static int
319asn1c_print_streams(arg_t *arg) {
320 compiler_streams_t *cs = arg->expr->data;
321 asn1p_expr_t *expr = arg->expr;
322 int i;
323
Lev Walkin64399722004-08-11 07:17:22 +0000324 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000325 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000326 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000327 continue;
328
329 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
330 _compiler_stream2str[i],
331 expr->Identifier);
332
Lev Walkin59004fa2004-08-20 13:37:01 +0000333 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700334 safe_fwrite(ot->buf, ot->len, 1, stdout);
Lev Walkinf15320b2004-06-03 03:38:44 +0000335 }
336 }
337
338 return 0;
339}
340
341static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500342asn1c_save_streams(arg_t *arg, asn1c_dep_chainset *deps, const char* destdir, int optc,
Lev Walkin7d5d9302017-11-13 22:17:20 -0800343 char **argv) {
344 asn1p_expr_t *expr = arg->expr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000345 compiler_streams_t *cs = expr->data;
346 out_chunk_t *ot;
347 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000348 char *tmpname_c, *tmpname_h;
Lev Walkina4f8e942017-10-08 19:28:20 -0700349 char name_buf[FILENAME_MAX];
Lev Walkinb46156d2017-09-05 02:53:05 -0700350 const char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000351 const char *c_retained = "";
352 const char *h_retained = "";
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800353 char *filename;
Lev Walkinf15320b2004-06-03 03:38:44 +0000354
355 if(cs == NULL) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700356 safe_fprintf(stderr, "Cannot compile %s at line %d\n",
Lev Walkinf15320b2004-06-03 03:38:44 +0000357 expr->Identifier, expr->_lineno);
358 return -1;
359 }
360
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800361 filename = strdup(asn1c_make_identifier(AMI_MASK_ONLY_SPACES, expr, (char*)0));
Jon Ringle6431b1c2017-11-15 01:22:02 -0500362 fp_c = asn1c_open_file(destdir, filename, ".c", &tmpname_c);
363 fp_h = asn1c_open_file(destdir, filename, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000364 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000365 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
366 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000367 return -1;
368 }
369
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000370 generate_preamble(arg, fp_c, optc, argv);
371 generate_preamble(arg, fp_h, optc, argv);
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000372
Lev Walkina00d6b32006-03-21 03:40:38 +0000373 header_id = asn1c_make_identifier(0, expr, NULL);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700374 safe_fprintf(fp_h,
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 "#ifndef\t_%s_H_\n"
376 "#define\t_%s_H_\n"
377 "\n", header_id, header_id);
378
Lev Walkinf2b2f372016-03-14 02:23:48 -0700379 safe_fprintf(fp_h, "\n");
Lev Walkin34944f22010-10-07 08:25:37 +0000380 HINCLUDE("asn_application.h");
Lev Walkinf15320b2004-06-03 03:38:44 +0000381
Lev Walkinb9b8b952005-03-05 00:33:27 +0000382#define SAVE_STREAM(fp, idx, msg, actdep) do { \
Lev Walkin866cff12005-03-05 00:50:53 +0000383 if(TQ_FIRST(&(cs->destination[idx].chunks)) && *msg) \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700384 safe_fprintf(fp, "\n/* %s */\n", msg); \
Lev Walkinc8285712005-03-04 22:18:20 +0000385 TQ_FOR(ot, &(cs->destination[idx].chunks), next) { \
Lev Walkin7d5d9302017-11-13 22:17:20 -0800386 if(actdep) asn1c_activate_dependency(deps, ot->buf, header_id); \
Lev Walkinf2b2f372016-03-14 02:23:48 -0700387 safe_fwrite(ot->buf, ot->len, 1, fp); \
Lev Walkinc8285712005-03-04 22:18:20 +0000388 } \
389} while(0)
390
Lev Walkinb9b8b952005-03-05 00:33:27 +0000391 SAVE_STREAM(fp_h, OT_INCLUDES, "Including external dependencies", 1);
Lev Walkin3d551c02005-07-15 18:49:41 +0000392
Lev Walkinf2b2f372016-03-14 02:23:48 -0700393 safe_fprintf(fp_h, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
Lev Walkinb9b8b952005-03-05 00:33:27 +0000394 SAVE_STREAM(fp_h, OT_DEPS, "Dependencies", 0);
395 SAVE_STREAM(fp_h, OT_FWD_DECLS, "Forward declarations", 0);
Bi-Ruei, Chiu9b87e5b2016-06-06 00:23:16 +0800396 SAVE_STREAM(fp_h, OT_FWD_DEFS, "Forward definitions", 0);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000397 SAVE_STREAM(fp_h, OT_TYPE_DECLS, expr->Identifier, 0);
398 SAVE_STREAM(fp_h, OT_FUNC_DECLS,"Implementation", 0);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700399 safe_fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000400
Lev Walkinae5540f2006-02-19 04:26:37 +0000401 if(!(arg->flags & A1C_NO_INCLUDE_DEPS))
402 SAVE_STREAM(fp_h, OT_POST_INCLUDE, "Referred external types", 1);
403
Lev Walkinf2b2f372016-03-14 02:23:48 -0700404 safe_fprintf(fp_h, "\n#endif\t/* _%s_H_ */\n", header_id);
Lev Walkin47370392006-02-19 04:30:15 +0000405
Lev Walkin34944f22010-10-07 08:25:37 +0000406 HINCLUDE("asn_internal.h");
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800407 safe_fprintf(fp_c, "#include \"%s.h\"\n\n", filename);
Lev Walkinb9b8b952005-03-05 00:33:27 +0000408 if(arg->flags & A1C_NO_INCLUDE_DEPS)
Lev Walkin866cff12005-03-05 00:50:53 +0000409 SAVE_STREAM(fp_c, OT_POST_INCLUDE, "", 1);
Lev Walkin9de6cd82017-08-10 05:47:46 -0700410 TQ_FOR(ot, &(cs->destination[OT_IOC_TABLES].chunks), next)
411 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000412 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700413 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000414 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700415 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin725883b2006-10-09 12:07:58 +0000416 TQ_FOR(ot, &(cs->destination[OT_CTDEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700417 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000418 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700419 safe_fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkinf15320b2004-06-03 03:38:44 +0000420
Lev Walkin9de6cd82017-08-10 05:47:46 -0700421 assert(OT_MAX == 13); /* Protection from reckless changes */
Lev Walkinf15320b2004-06-03 03:38:44 +0000422
423 fclose(fp_c);
424 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000425
Jon Ringle6431b1c2017-11-15 01:22:02 -0500426 int ret = snprintf(name_buf, sizeof(name_buf), "%s%s.c", destdir, filename);
Lev Walkina4f8e942017-10-08 19:28:20 -0700427 assert(ret > 0 && ret < (ssize_t)sizeof(name_buf));
Lev Walkin4604d032005-03-04 08:52:50 +0000428
Lev Walkin4604d032005-03-04 08:52:50 +0000429 if(identical_files(name_buf, tmpname_c)) {
430 c_retained = " (contents unchanged)";
431 unlink(tmpname_c);
432 } else {
433 if(rename(tmpname_c, name_buf)) {
434 unlink(tmpname_c);
435 perror(tmpname_c);
Lev Walkina895afb2005-10-06 10:09:34 +0000436 free(tmpname_c);
437 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000438 return -1;
439 }
440 }
441
Jon Ringle6431b1c2017-11-15 01:22:02 -0500442 sprintf(name_buf, "%s%s.h", destdir, filename);
Lev Walkin4604d032005-03-04 08:52:50 +0000443 if(identical_files(name_buf, tmpname_h)) {
444 h_retained = " (contents unchanged)";
445 unlink(tmpname_h);
446 } else {
447 if(rename(tmpname_h, name_buf)) {
448 unlink(tmpname_h);
449 perror(tmpname_h);
Lev Walkina895afb2005-10-06 10:09:34 +0000450 free(tmpname_c);
451 free(tmpname_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000452 return -1;
453 }
454 }
455
456 free(tmpname_c);
457 free(tmpname_h);
458
Jon Ringle6431b1c2017-11-15 01:22:02 -0500459 safe_fprintf(stderr, "Compiled %s%s.c%s\n",
460 destdir, filename, c_retained);
461 safe_fprintf(stderr, "Compiled %s%s.h%s\n",
462 destdir, filename, h_retained);
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800463 free(filename);
Lev Walkinf15320b2004-06-03 03:38:44 +0000464 return 0;
465}
466
Lev Walkin4604d032005-03-04 08:52:50 +0000467static int
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000468generate_preamble(arg_t *arg, FILE *fp, int optc, char **argv) {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700469 safe_fprintf(fp,
Lev Walkin8253ea92006-03-17 01:47:57 +0000470 "/*\n"
471 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
472 " * From ASN.1 module \"%s\"\n"
473 " * \tfound in \"%s\"\n",
474 arg->expr->module->ModuleName,
475 arg->expr->module->source_file_name);
476 if(optc > 1) {
477 int i;
Lev Walkinf2b2f372016-03-14 02:23:48 -0700478 safe_fprintf(fp, " * \t`asn1c ");
Lev Walkin8253ea92006-03-17 01:47:57 +0000479 for(i = 1; i < optc; i++)
Lev Walkinf2b2f372016-03-14 02:23:48 -0700480 safe_fprintf(fp, "%s%s", i>1?" ":"", argv[i]);
481 safe_fprintf(fp, "`\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000482 }
Lev Walkinf2b2f372016-03-14 02:23:48 -0700483 safe_fprintf(fp, " */\n\n");
Lev Walkin8253ea92006-03-17 01:47:57 +0000484 return 0;
485}
486
487static int
Lev Walkin4604d032005-03-04 08:52:50 +0000488identical_files(const char *fname1, const char *fname2) {
Lev Walkina895afb2005-10-06 10:09:34 +0000489 char buf[2][4096];
Lev Walkin4604d032005-03-04 08:52:50 +0000490 FILE *fp1, *fp2;
491 size_t olen, nlen;
492 int retval = 1; /* Files are identical */
493
Lev Walkin93659562010-11-20 09:47:13 -0800494#ifndef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000495 struct stat sb;
496
497 if(lstat(fname1, &sb) || !S_ISREG(sb.st_mode)
498 || lstat(fname2, &sb) || !S_ISREG(sb.st_mode)) {
499 return 0; /* Files are not identical */
500 }
501#endif
502
Lev Walkin4604d032005-03-04 08:52:50 +0000503 fp1 = fopen(fname1, "r");
504 if(!fp1) { return 0; }
505 fp2 = fopen(fname2, "r");
506 if(!fp2) { fclose(fp1); return 0; }
507
508 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
509 nlen = fread(buf[1], 1, olen, fp2);
510 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
511 retval = 0;
512 break;
513 }
514 }
515 nlen = fread(buf[1], 1, 1, fp2);
516 if(nlen) retval = 0;
517
518 fclose(fp1);
519 fclose(fp2);
520 return retval;
521}
522
Lev Walkin4efbfb72005-02-25 14:20:30 +0000523/*
524 * Copy file for real.
525 */
526static int
527real_copy(const char *src, const char *dst) {
Lev Walkina895afb2005-10-06 10:09:34 +0000528 unsigned char buf[4096];
529 char *tmpname;
Lev Walkin4efbfb72005-02-25 14:20:30 +0000530 FILE *fpsrc, *fpdst;
531 size_t len;
532 int retval = 0;
533
Lev Walkin4604d032005-03-04 08:52:50 +0000534 if(identical_files(src, dst))
535 return retval; /* Success, no need to copy for real. */
536
537 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000538 if(!fpsrc) { errno = EIO; return -1; }
Jon Ringle6431b1c2017-11-15 01:22:02 -0500539 fpdst = asn1c_open_file(NULL, dst, "", &tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000540 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
541
542 while(!feof(fpsrc)) {
543 len = fread(buf, 1, sizeof(buf), fpsrc);
Lev Walkinf2b2f372016-03-14 02:23:48 -0700544 if(safe_fwrite(buf, 1, len, fpdst) != len) {
Lev Walkina895afb2005-10-06 10:09:34 +0000545 perror(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000546 errno = EIO;
547 retval = -1;
548 break;
549 }
550 }
Lev Walkin4efbfb72005-02-25 14:20:30 +0000551 fclose(fpsrc);
552 fclose(fpdst);
Lev Walkina895afb2005-10-06 10:09:34 +0000553
554 /* Check if copied correctly, and rename into a permanent name */
555 if(retval) {
556 unlink(tmpname);
557 } else if(rename(tmpname, dst)) {
558 unlink(tmpname);
559 perror(tmpname);
560 retval = -1;
561 }
562 free(tmpname);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000563 return retval;
564}
565
Lev Walkinf15320b2004-06-03 03:38:44 +0000566static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500567asn1c_copy_over(arg_t *arg, const char* destdir, const char *path, const char *msg) {
Lev Walkin93659562010-11-20 09:47:13 -0800568#ifdef _WIN32
Lev Walkina895afb2005-10-06 10:09:34 +0000569 int use_real_copy = 1;
570#else
Lev Walkin2655eb32013-03-25 19:09:04 -0700571 int use_real_copy = !(arg->flags & A1C_LINK_SKELETONS);
Lev Walkina895afb2005-10-06 10:09:34 +0000572#endif
Lev Walkind9bd7752004-06-05 08:17:50 +0000573
Jon Ringle6431b1c2017-11-15 01:22:02 -0500574 const char *fname = a1c_basename(path, destdir);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000575 if(!fname
Lev Walkina895afb2005-10-06 10:09:34 +0000576 || (use_real_copy ? real_copy(path, fname) : symlink(path, fname))
Lev Walkin4efbfb72005-02-25 14:20:30 +0000577 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000578 if(errno == EEXIST) {
579 struct stat sb1, sb2;
580 if(stat(path, &sb1) == 0
581 && stat(fname, &sb2) == 0
582 && sb1.st_dev == sb2.st_dev
583 && sb1.st_ino == sb2.st_ino) {
584 /*
585 * Nothing to do.
586 */
Lev Walkinf2b2f372016-03-14 02:23:48 -0700587 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000588 "File %s is already here as %s\n",
589 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000590 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000591 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700592 safe_fprintf(stderr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000593 "Retaining local %s (%s suggested)\n",
594 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000595 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000596 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000597 } else if(errno == ENOENT) {
598 /* Ignore this */
599 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000600 } else {
Lev Walkinf2b2f372016-03-14 02:23:48 -0700601 safe_fprintf(stderr, "%s %s -> %s failed: %s\n",
Lev Walkina895afb2005-10-06 10:09:34 +0000602 use_real_copy ? "Copy" : "Symlink",
Lev Walkinf15320b2004-06-03 03:38:44 +0000603 path, fname, strerror(errno));
604 return -1;
605 }
606 }
607
Lev Walkin7d5d9302017-11-13 22:17:20 -0800608 const int has_msg = msg && *msg;
609 safe_fprintf(stderr, "%s %s\t-> %s%s%s\n",
610 use_real_copy ? "Copied" : "Symlinked", path, fname,
611 has_msg ? " " : "", has_msg ? msg : "");
Lev Walkinf15320b2004-06-03 03:38:44 +0000612
Lev Walkin7d5d9302017-11-13 22:17:20 -0800613 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000614}
615
Lev Walkin59b176e2005-11-26 11:25:14 +0000616
617static int
Jon Ringle6431b1c2017-11-15 01:22:02 -0500618generate_pdu_collection_file(arg_t *arg, const char* destdir) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800619 abuf *buf = generate_pdu_collection(arg);
620 assert(buf);
Lev Walkin59b176e2005-11-26 11:25:14 +0000621
Jon Ringle6431b1c2017-11-15 01:22:02 -0500622 FILE *fp = asn1c_open_file(destdir, "pdu_collection", ".c", 0);
Lev Walkin59b176e2005-11-26 11:25:14 +0000623 if(fp == NULL) {
624 perror("pdu_collection.c");
625 return -1;
626 }
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800627 safe_fwrite(buf->buffer, buf->length, 1, fp);
628 fclose(fp);
Lev Walkin59b176e2005-11-26 11:25:14 +0000629
Lev Walkinf2b2f372016-03-14 02:23:48 -0700630 safe_fprintf(stderr, "Generated pdu_collection.c\n");
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800631 return 0;
632}
Lev Walkin59b176e2005-11-26 11:25:14 +0000633
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800634static abuf *
635generate_pdu_collection(arg_t *arg) {
636 asn1p_module_t *mod;
637 abuf *buf = abuf_new();
638
639 abuf_printf(buf, "/*\n * Generated by asn1c-" VERSION
640 " (http://lionet.info/asn1c)\n */\n\n");
Lev Walkin3be49192017-11-11 16:13:32 -0800641 abuf_printf(buf,
642 "struct asn_TYPE_descriptor_s;\t"
643 "/* Forward declaration */\n\n");
644
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800645
646 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
647 TQ_FOR(arg->expr, &(mod->members), next) {
648 if(include_type_to_pdu_collection(arg) == TI_NOT_INCLUDED) continue;
649 abuf_printf(buf,
Lev Walkin3be49192017-11-11 16:13:32 -0800650 "extern struct asn_TYPE_descriptor_s "
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800651 "asn_DEF_%s;\n",
652 asn1c_make_identifier(0, arg->expr, NULL));
653 }
654 }
655
656 abuf_printf(buf, "\n\n");
657 abuf_printf(buf, "struct asn_TYPE_descriptor_s *asn_pdu_collection[] = {\n");
658 TQ_FOR(mod, &(arg->asn->modules), mod_next) {
659 int mod_printed = 0;
660 TQ_FOR(arg->expr, &(mod->members), next) {
661 switch(include_type_to_pdu_collection(arg)) {
662 case TI_NOT_INCLUDED:
663 continue;
664 case TI_INCLUDED_FROM_BULK:
665 /* Increment */
666 asn1c__pdu_type_lookup(arg->expr->Identifier);
667 break;
668 case TI_INCLUDED_FROM_CMDLINE:
669 break;
670 }
671 if(!mod_printed++) {
672 abuf_printf(buf, "\t/* From module %s in %s */\n",
673 arg->expr->module->ModuleName,
674 arg->expr->module->source_file_name);
675 }
676 abuf_printf(buf, "\t&asn_DEF_%s,\t\n",
677 asn1c_make_identifier(0, arg->expr, NULL));
678 }
679 }
680
681 abuf_printf(buf, "\t0\n};\n\n");
682
683 if(pdu_collection_has_unused_types(arg)) {
684 abuf_free(buf);
685 return NULL;
686 }
687
688 return buf;
Lev Walkin59b176e2005-11-26 11:25:14 +0000689}
690
Lev Walkin66adab42006-09-23 02:52:12 +0000691static struct PDUType {
692 char *typename;
693 int used;
694} *pduType;
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800695static size_t pduTypes;
Lev Walkin66adab42006-09-23 02:52:12 +0000696
697static const char *
698generate_pdu_C_definition(void) {
Lev Walkina4f3d462017-08-05 22:40:14 -0700699 const char *src;
700 char *def;
Lev Walkin66adab42006-09-23 02:52:12 +0000701 char *dst;
Lev Walkina4f3d462017-08-05 22:40:14 -0700702 if(pduTypes == 0) return "";
703 def = malloc(strlen(pduType[0].typename) + 20);
704 assert(def);
705 strcpy(def, "-DPDU=");
706 for(src = pduType[0].typename, dst = def + 6; *src; src++, dst++) {
707 if((*dst = *src) == '-') {
708 *dst = '_';
709 }
710 }
711 *dst++ = ' ';
712 *dst = 0;
713 return def;
Lev Walkin66adab42006-09-23 02:52:12 +0000714}
715
716void
717asn1c__add_pdu_type(const char *ctypename) {
718 char *typename = strdup(ctypename);
719 assert(typename && *typename);
720
721 pduType = realloc(pduType, sizeof(pduType[0]) * (pduTypes + 1));
722 assert(pduType);
723 pduType[pduTypes].used = 0;
724 pduType[pduTypes].typename = typename;
725 pduTypes++;
726}
727
Lev Walkin5230c642017-09-26 18:10:06 -0700728static void
729asn1c__cleanup_pdu_type() {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800730 for(size_t i = 0; i < pduTypes; i++) {
Lev Walkin5230c642017-09-26 18:10:06 -0700731 free(pduType[i].typename);
732 }
733 free(pduType);
734 pduType = NULL;
735 pduTypes = 0;
736}
737
Lev Walkin66adab42006-09-23 02:52:12 +0000738static int
739asn1c__pdu_type_lookup(const char *typename) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800740 for(size_t i = 0; i < pduTypes; i++) {
741 struct PDUType *pt = &pduType[i];
742 if(strcmp(pt->typename, typename) == 0) {
743 pt->used++;
744 return 1;
745 }
746 }
747 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000748}
749
750static int
751need_to_generate_pdu_collection(arg_t *arg) {
Lev Walkin3be49192017-11-11 16:13:32 -0800752 /* If -pdu=all or -pdu=auto are given, we need to generate one. */
Lev Walkin66adab42006-09-23 02:52:12 +0000753 if(arg->flags & (A1C_PDU_ALL|A1C_PDU_AUTO))
754 return 1;
Lev Walkin3be49192017-11-11 16:13:32 -0800755
756 /*
757 * If no -pdu=... flags were given, need to do it, too,
758 * effectively producing -pdu=auto.
759 */
760 if(!(arg->flags & (A1C_PDU_ALL | A1C_PDU_AUTO | A1C_PDU_TYPE))) return 1;
761
762 if(arg->flags & A1C_PDU_TYPE) {
763 return (pduTypes > 1) ? 1 : 0;
764 }
Lev Walkin66adab42006-09-23 02:52:12 +0000765 return 0;
766}
767
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800768static int
769pdu_collection_has_unused_types(arg_t *arg) {
770 int ret = 0;
771
772 for(size_t i = 0; i < pduTypes; i++) {
773 struct PDUType *pt = &pduType[i];
774 if(!pt->used) {
775 FATAL("Unknown ASN.1 type specified in -pdu=%s", pt->typename);
776 ret = -1;
777 }
778 }
779
780 return ret;
Lev Walkin66adab42006-09-23 02:52:12 +0000781}
782
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800783static enum include_type_result
Lev Walkin66adab42006-09-23 02:52:12 +0000784include_type_to_pdu_collection(arg_t *arg) {
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800785 if(!asn1_lang_map[arg->expr->meta_type][arg->expr->expr_type].type_cb)
786 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000787
Lev Walkin8c44dff2017-08-26 17:17:27 -0700788 /* Parameterized types can't serve as PDU's without instantiation. */
Lev Walkin48e82d12017-10-19 03:06:35 -0700789 if(arg->expr->lhs_params) {
Lev Walkin8c44dff2017-08-26 17:17:27 -0700790 return 0;
Lev Walkin48e82d12017-10-19 03:06:35 -0700791 }
Lev Walkin8c44dff2017-08-26 17:17:27 -0700792
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800793 if((arg->flags & A1C_PDU_ALL)
Lev Walkin3be49192017-11-11 16:13:32 -0800794 /* -pdu=auto */
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800795 || ((arg->flags & A1C_PDU_AUTO) && !arg->expr->_type_referenced)
Lev Walkin3be49192017-11-11 16:13:32 -0800796 /* No -pdu=... whatsoever, act as if -pdu=auto */
797 || (!(arg->flags & (A1C_PDU_ALL | A1C_PDU_AUTO | A1C_PDU_TYPE))
798 && !arg->expr->_type_referenced)
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800799 || asn1c__pdu_type_lookup(arg->expr->Identifier)) {
800 return 1;
801 }
Lev Walkin66adab42006-09-23 02:52:12 +0000802
Lev Walkin5f4dbb72017-11-11 14:52:11 -0800803 return 0;
Lev Walkin66adab42006-09-23 02:52:12 +0000804}