blob: 2d0410f5800c4822f3647456723c4133ac0be787 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin79f54952004-08-13 16:58:19 +00002#include "asn1c_compat.h"
Lev Walkinacd9f8b2004-08-19 13:29:03 +00003#include "asn1c_fdeps.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00004#include "asn1c_lang.h"
5#include "asn1c_misc.h"
6#include "asn1c_save.h"
7#include "asn1c_out.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00008
Lev Walkinacd9f8b2004-08-19 13:29:03 +00009static int asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000010static int asn1c_print_streams(arg_t *arg);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000011static int asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *);
Lev Walkinf15320b2004-06-03 03:38:44 +000012static int asn1c_copy_over(arg_t *arg, char *path);
Lev Walkin4604d032005-03-04 08:52:50 +000013static int identical_files(const char *fname1, const char *fname2);
Lev Walkinf15320b2004-06-03 03:38:44 +000014
15int
16asn1c_save_compiled_output(arg_t *arg, const char *datadir) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000017 asn1c_fdeps_t *deps = 0;
18 FILE *mkf;
19 asn1c_fdeps_t *dlist;
Lev Walkinf15320b2004-06-03 03:38:44 +000020
Lev Walkinacd9f8b2004-08-19 13:29:03 +000021 deps = asn1c_read_file_dependencies(arg, datadir);
22 if(!deps && datadir) {
23 WARNING("Cannot read file-dependencies information "
24 "from %s\n", datadir);
25 }
Lev Walkinf15320b2004-06-03 03:38:44 +000026
27 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
28 TQ_FOR(arg->expr, &(arg->mod->members), next) {
29 if(asn1_lang_map[arg->expr->meta_type]
30 [arg->expr->expr_type].type_cb) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +000031 if(asn1c_dump_streams(arg, deps))
Lev Walkinf15320b2004-06-03 03:38:44 +000032 return -1;
33 }
34 }
35 }
36
37 /*
38 * Dump out the Makefile template and the rest of the support code.
39 */
Lev Walkinacd9f8b2004-08-19 13:29:03 +000040 if((arg->flags & A1C_PRINT_COMPILED)
41 || (arg->flags & A1C_OMIT_SUPPORT_CODE)) {
42 return 0; /* Finished */
43 }
Lev Walkinf15320b2004-06-03 03:38:44 +000044
Lev Walkin4604d032005-03-04 08:52:50 +000045 mkf = asn1c_open_file("Makefile.am", ".sample", 0);
Lev Walkinacd9f8b2004-08-19 13:29:03 +000046 if(mkf == NULL) {
47 perror("Makefile.am.sample");
48 return -1;
49 }
Lev Walkinf15320b2004-06-03 03:38:44 +000050
Lev Walkinacd9f8b2004-08-19 13:29:03 +000051 fprintf(mkf, "ASN_MODULE_SOURCES=");
52 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
53 TQ_FOR(arg->expr, &(arg->mod->members), next) {
54 if(asn1_lang_map[arg->expr->meta_type]
55 [arg->expr->expr_type].type_cb) {
Lev Walkind29bbce2004-09-23 22:19:14 +000056 fprintf(mkf, "\t\\\n\t%s.c",
Lev Walkinacd9f8b2004-08-19 13:29:03 +000057 arg->expr->Identifier);
Lev Walkinf15320b2004-06-03 03:38:44 +000058 }
59 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +000060 }
Lev Walkind29bbce2004-09-23 22:19:14 +000061 fprintf(mkf, "\n\nASN_MODULE_HEADERS=");
62 TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
63 TQ_FOR(arg->expr, &(arg->mod->members), next) {
64 if(asn1_lang_map[arg->expr->meta_type]
65 [arg->expr->expr_type].type_cb) {
66 fprintf(mkf, "\t\\\n\t%s.h",
67 arg->expr->Identifier);
68 }
69 }
70 }
71 fprintf(mkf, "\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +000072
Lev Walkinacd9f8b2004-08-19 13:29:03 +000073 /*
74 * Move necessary skeleton files and add them to Makefile.am.sample.
75 */
76 dlist = asn1c_deps_makelist(deps);
77 if(dlist) {
78 char buf[8129];
79 char *dir_end;
Lev Walkin4efbfb72005-02-25 14:20:30 +000080 size_t dlen = strlen(datadir);
81 int i;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000082
Lev Walkin4efbfb72005-02-25 14:20:30 +000083 assert(dlen < (sizeof(buf) / 2 - 2));
84 memcpy(buf, datadir, dlen);
85 dir_end = buf + dlen;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000086 *dir_end++ = '/';
87
88 for(i = 0; i < dlist->el_count; i++) {
89 char *fname = dlist->elements[i]->filename;
Lev Walkind29bbce2004-09-23 22:19:14 +000090 char *dotH;
Lev Walkinacd9f8b2004-08-19 13:29:03 +000091
92 assert(strlen(fname) < (sizeof(buf) / 2));
93 strcpy(dir_end, fname);
94
95 if(asn1c_copy_over(arg, buf) == -1) {
Lev Walkinf15320b2004-06-03 03:38:44 +000096 fprintf(mkf, ">>>ABORTED<<<");
97 fclose(mkf);
Lev Walkinf15320b2004-06-03 03:38:44 +000098 return -1;
Lev Walkind29bbce2004-09-23 22:19:14 +000099 }
100 dotH = strrchr(fname, 'h');
101 if(dotH && fname<dotH && dotH[-1] == '.' && !dotH[1]) {
102 fprintf(mkf, "ASN_MODULE_HEADERS+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000103 } else {
Lev Walkind29bbce2004-09-23 22:19:14 +0000104 fprintf(mkf, "ASN_MODULE_SOURCES+=%s\n", fname);
Lev Walkinf15320b2004-06-03 03:38:44 +0000105 }
106 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 }
108
Lev Walkind29bbce2004-09-23 22:19:14 +0000109 fprintf(mkf, "\n\n"
110 "lib_LTLIBRARIES=libsomething.la\n"
111 "libsomething_la_SOURCES="
112 "$(ASN_MODULE_SOURCES) $(ASN_MODULE_HEADERS)\n"
113 "\n"
114 "# This file may be used as an input for make(3)\n"
115 "# Remove the lines below to convert it into a pure .am file\n"
116 "TARGET = progname\n"
117 "CFLAGS += -I.\n"
118 "OBJS=${ASN_MODULE_SOURCES:.c=.o} $(TARGET).o\n"
119 "\nall: $(TARGET)\n"
120 "\n$(TARGET): ${OBJS}"
121 "\n\t$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)\n"
122 "\n.SUFFIXES:"
123 "\n.SUFFIXES: .c .o\n"
124 "\n.c.o:"
125 "\n\t$(CC) $(CFLAGS) -o $@ -c $<\n"
126 "\nclean:"
127 "\n\trm -f $(TARGET)"
128 "\n\trm -f $(OBJS)\n"
129 "\n"
130 );
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000131 fclose(mkf);
132 fprintf(stderr, "Generated Makefile.am.sample\n");
133
Lev Walkinf15320b2004-06-03 03:38:44 +0000134 return 0;
135}
136
137/*
138 * Dump the streams.
139 */
140static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000141asn1c_dump_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000142 if(arg->flags & A1C_PRINT_COMPILED) {
143 return asn1c_print_streams(arg);
144 } else {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000145 return asn1c_save_streams(arg, deps);
Lev Walkinf15320b2004-06-03 03:38:44 +0000146 }
147}
148
149static int
150asn1c_print_streams(arg_t *arg) {
151 compiler_streams_t *cs = arg->expr->data;
152 asn1p_expr_t *expr = arg->expr;
153 int i;
154
Lev Walkin64399722004-08-11 07:17:22 +0000155 for(i = 1; i < OT_MAX; i++) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 out_chunk_t *ot;
Lev Walkin59004fa2004-08-20 13:37:01 +0000157 if(TQ_FIRST(&cs->destination[i].chunks) == NULL)
Lev Walkinf15320b2004-06-03 03:38:44 +0000158 continue;
159
160 printf("\n/*** <<< %s [%s] >>> ***/\n\n",
161 _compiler_stream2str[i],
162 expr->Identifier);
163
Lev Walkin59004fa2004-08-20 13:37:01 +0000164 TQ_FOR(ot, &(cs->destination[i].chunks), next) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000165 fwrite(ot->buf, ot->len, 1, stdout);
166 }
167 }
168
169 return 0;
170}
171
172static int
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000173asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000174 asn1p_expr_t *expr = arg->expr;
175 compiler_streams_t *cs = expr->data;
176 out_chunk_t *ot;
177 FILE *fp_c, *fp_h;
Lev Walkin4604d032005-03-04 08:52:50 +0000178 char *tmpname_c, *tmpname_h;
179 char *name_buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000180 char *header_id;
Lev Walkin4604d032005-03-04 08:52:50 +0000181 const char *c_retained = "";
182 const char *h_retained = "";
Lev Walkinf15320b2004-06-03 03:38:44 +0000183
184 if(cs == NULL) {
185 fprintf(stderr, "Cannot compile %s at line %d\n",
186 expr->Identifier, expr->_lineno);
187 return -1;
188 }
189
Lev Walkin4604d032005-03-04 08:52:50 +0000190 fp_c = asn1c_open_file(expr->Identifier, ".c", &tmpname_c);
191 fp_h = asn1c_open_file(expr->Identifier, ".h", &tmpname_h);
Lev Walkinf15320b2004-06-03 03:38:44 +0000192 if(fp_c == NULL || fp_h == NULL) {
Lev Walkin4604d032005-03-04 08:52:50 +0000193 if(fp_c) { unlink(tmpname_c); free(tmpname_c); fclose(fp_c); }
194 if(fp_h) { unlink(tmpname_h); free(tmpname_h); fclose(fp_h); }
Lev Walkinf15320b2004-06-03 03:38:44 +0000195 return -1;
196 }
197
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000198 fprintf(fp_c,
199 "/*\n"
200 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000201 " * From ASN.1 module \"%s\"\n"
202 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000203 " */\n\n",
204 arg->mod->Identifier,
205 arg->mod->source_file_name
206 );
207 fprintf(fp_h,
208 "/*\n"
209 " * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
Lev Walkind29bbce2004-09-23 22:19:14 +0000210 " * From ASN.1 module \"%s\"\n"
211 " * \tfound in \"%s\"\n"
Lev Walkinc3b8f6d2004-06-03 05:06:25 +0000212 " */\n\n",
213 arg->mod->Identifier,
214 arg->mod->source_file_name
215 );
216
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000217 header_id = asn1c_make_identifier(0, expr->Identifier, NULL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000218 fprintf(fp_h,
219 "#ifndef\t_%s_H_\n"
220 "#define\t_%s_H_\n"
221 "\n", header_id, header_id);
222
Lev Walkin43634792004-08-10 01:14:29 +0000223 fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");
224
Lev Walkin11c3e172004-09-24 21:00:50 +0000225 fprintf(fp_h, "#include <asn_application.h>\n\n");
Lev Walkinf15320b2004-06-03 03:38:44 +0000226
Lev Walkin59004fa2004-08-20 13:37:01 +0000227 TQ_FOR(ot, &(cs->destination[OT_INCLUDES].chunks), next) {
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000228 asn1c_activate_dependency(deps, 0, ot->buf);
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000229 fwrite(ot->buf, ot->len, 1, fp_h);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000230 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000231 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000232 TQ_FOR(ot, &(cs->destination[OT_DEPS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000233 fwrite(ot->buf, ot->len, 1, fp_h);
234 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000235 TQ_FOR(ot, &(cs->destination[OT_TYPE_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 fwrite(ot->buf, ot->len, 1, fp_h);
237 fprintf(fp_h, "\n");
Lev Walkin59004fa2004-08-20 13:37:01 +0000238 TQ_FOR(ot, &(cs->destination[OT_FUNC_DECLS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000239 fwrite(ot->buf, ot->len, 1, fp_h);
240
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000241 fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
242 "#endif\t/* _%s_H_ */\n",
243 header_id);
244
Lev Walkin0ffabe22004-09-22 16:02:03 +0000245 fprintf(fp_c, "#include <asn_internal.h>\n\n");
Lev Walkin3dcaafa2004-08-11 05:21:32 +0000246 fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier); /* Myself */
Lev Walkin59004fa2004-08-20 13:37:01 +0000247 TQ_FOR(ot, &(cs->destination[OT_CTABLES].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000248 fwrite(ot->buf, ot->len, 1, fp_c);
Lev Walkin59004fa2004-08-20 13:37:01 +0000249 TQ_FOR(ot, &(cs->destination[OT_CODE].chunks), next)
250 fwrite(ot->buf, ot->len, 1, fp_c);
251 TQ_FOR(ot, &(cs->destination[OT_STAT_DEFS].chunks), next)
Lev Walkinf15320b2004-06-03 03:38:44 +0000252 fwrite(ot->buf, ot->len, 1, fp_c);
253
Lev Walkin59004fa2004-08-20 13:37:01 +0000254 assert(OT_MAX == 8);
Lev Walkinf15320b2004-06-03 03:38:44 +0000255
256 fclose(fp_c);
257 fclose(fp_h);
Lev Walkin4604d032005-03-04 08:52:50 +0000258
259 name_buf = alloca(strlen(expr->Identifier) + 3);
260
261 sprintf(name_buf, "%s.c", expr->Identifier);
262 if(identical_files(name_buf, tmpname_c)) {
263 c_retained = " (contents unchanged)";
264 unlink(tmpname_c);
265 } else {
266 if(rename(tmpname_c, name_buf)) {
267 unlink(tmpname_c);
268 perror(tmpname_c);
269 return -1;
270 }
271 }
272
273 sprintf(name_buf, "%s.h", expr->Identifier);
274 if(identical_files(name_buf, tmpname_h)) {
275 h_retained = " (contents unchanged)";
276 unlink(tmpname_h);
277 } else {
278 if(rename(tmpname_h, name_buf)) {
279 unlink(tmpname_h);
280 perror(tmpname_h);
281 return -1;
282 }
283 }
284
285 free(tmpname_c);
286 free(tmpname_h);
287
288 fprintf(stderr, "Compiled %s.c%s\n",
289 expr->Identifier, c_retained);
290 fprintf(stderr, "Compiled %s.h%s\n",
291 expr->Identifier, h_retained);
Lev Walkinf15320b2004-06-03 03:38:44 +0000292 return 0;
293}
294
Lev Walkin4604d032005-03-04 08:52:50 +0000295static int
296identical_files(const char *fname1, const char *fname2) {
297 char buf[2][8192];
298 FILE *fp1, *fp2;
299 size_t olen, nlen;
300 int retval = 1; /* Files are identical */
301
302 fp1 = fopen(fname1, "r");
303 if(!fp1) { return 0; }
304 fp2 = fopen(fname2, "r");
305 if(!fp2) { fclose(fp1); return 0; }
306
307 while((olen = fread(buf[0], 1, sizeof(buf[0]), fp1))) {
308 nlen = fread(buf[1], 1, olen, fp2);
309 if(nlen != olen || memcmp(buf[0], buf[1], nlen)) {
310 retval = 0;
311 break;
312 }
313 }
314 nlen = fread(buf[1], 1, 1, fp2);
315 if(nlen) retval = 0;
316
317 fclose(fp1);
318 fclose(fp2);
319 return retval;
320}
321
Lev Walkin4efbfb72005-02-25 14:20:30 +0000322/*
323 * Copy file for real.
324 */
325static int
326real_copy(const char *src, const char *dst) {
327 unsigned char buf[8192];
328 FILE *fpsrc, *fpdst;
329 size_t len;
330 int retval = 0;
331
Lev Walkin4604d032005-03-04 08:52:50 +0000332 if(identical_files(src, dst))
333 return retval; /* Success, no need to copy for real. */
334
335 fpsrc = fopen(src, "r");
Lev Walkin4efbfb72005-02-25 14:20:30 +0000336 if(!fpsrc) { errno = EIO; return -1; }
Lev Walkin4604d032005-03-04 08:52:50 +0000337 fpdst = asn1c_open_file(dst, "", 0);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000338 if(!fpdst) { fclose(fpsrc); errno = EIO; return -1; }
339
340 while(!feof(fpsrc)) {
341 len = fread(buf, 1, sizeof(buf), fpsrc);
342 if(fwrite(buf, 1, len, fpsrc) != len) {
343 errno = EIO;
344 retval = -1;
345 break;
346 }
347 }
348
349 fclose(fpsrc);
350 fclose(fpdst);
351 return retval;
352}
353
Lev Walkinf15320b2004-06-03 03:38:44 +0000354static int
355asn1c_copy_over(arg_t *arg, char *path) {
Lev Walkin79f54952004-08-13 16:58:19 +0000356 char *fname;
Lev Walkinf15320b2004-06-03 03:38:44 +0000357
Lev Walkind9bd7752004-06-05 08:17:50 +0000358 (void)arg; /* Unused argument */
359
Lev Walkin79f54952004-08-13 16:58:19 +0000360 fname = a1c_basename(path);
Lev Walkin4efbfb72005-02-25 14:20:30 +0000361 if(!fname
362#ifdef WIN32
363 || real_copy(path, fname)
364#else
365 || (1 ? symlink(path, fname) : real_copy(path, fname))
366#endif
367 ) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000368 if(errno == EEXIST) {
369 struct stat sb1, sb2;
370 if(stat(path, &sb1) == 0
371 && stat(fname, &sb2) == 0
372 && sb1.st_dev == sb2.st_dev
373 && sb1.st_ino == sb2.st_ino) {
374 /*
375 * Nothing to do.
376 */
377 fprintf(stderr,
378 "File %s is already here as %s\n",
379 path, fname);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000380 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000381 } else {
382 fprintf(stderr,
383 "Retaining local %s (%s suggested)\n",
384 fname, path);
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000385 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000386 }
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000387 } else if(errno == ENOENT) {
388 /* Ignore this */
389 return 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000390 } else {
391 fprintf(stderr, "Symlink %s -> %s failed: %s\n",
392 path, fname, strerror(errno));
393 return -1;
394 }
395 }
396
397 fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);
398
Lev Walkinacd9f8b2004-08-19 13:29:03 +0000399 return 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000400}
401