blob: 46029f7610a8e353b1a18d9646a48719aaf926e8 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
Lev Walkin59004fa2004-08-20 13:37:01 +00002#include "asn1c_out.h"
Lev Walkinf15320b2004-06-03 03:38:44 +00003
4/*
5 * Add an elementary chunk of target language text
6 * into appropriate output stream.
7 */
8int
9asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {
Lev Walkin59004fa2004-08-20 13:37:01 +000010 struct compiler_stream_destination_s *dst;
Lev Walkinf15320b2004-06-03 03:38:44 +000011 const char *p;
12 int lf_found;
13 va_list ap;
14 out_chunk_t *m;
Lev Walkinf15320b2004-06-03 03:38:44 +000015 int ret;
16
Lev Walkin64399722004-08-11 07:17:22 +000017 switch(arg->target->target) {
18 case OT_IGNORE:
19 return 0;
Lev Walkin64399722004-08-11 07:17:22 +000020 default:
Lev Walkin59004fa2004-08-20 13:37:01 +000021 dst = &arg->target->destination[arg->target->target];
Lev Walkin64399722004-08-11 07:17:22 +000022 break;
23 }
24
Lev Walkinf15320b2004-06-03 03:38:44 +000025 /*
26 * Make sure the output has a single LF and only at the end.
27 */
28 for(lf_found = 0, p = fmt; *p; p++) {
29 if(*p == '\n') {
30 lf_found++;
31 assert(p[1] == '\0');
32 }
33 }
34 assert(lf_found <= 1);
35
36 /*
37 * Print out the indentation.
38 */
Lev Walkin59004fa2004-08-20 13:37:01 +000039 if(dst->indented == 0) {
40 int i = dst->indent_level;
41 dst->indented = 1;
Lev Walkinf15320b2004-06-03 03:38:44 +000042 while(i--) {
43 ret = asn1c_compiled_output(arg, "\t");
44 if(ret == -1) return -1;
45 }
46 }
Lev Walkin3dcaafa2004-08-11 05:21:32 +000047 if(lf_found)
Lev Walkin59004fa2004-08-20 13:37:01 +000048 dst->indented = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +000049
50 /*
Lev Walkinf15320b2004-06-03 03:38:44 +000051 * Allocate buffer.
52 */
53 m = calloc(1, sizeof(out_chunk_t));
54 if(m == NULL) return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +000055
Lev Walkina460ba32004-10-20 15:40:04 +000056 m->len = 16;
57 do {
58 void *tmp;
59 m->len <<= 2;
60 tmp = realloc(m->buf, m->len);
61 if(tmp) {
62 m->buf = (char *)tmp;
63 } else {
64 free(m->buf);
65 free(m);
66 return -1;
67 }
68 va_start(ap, fmt);
69 ret = vsnprintf(m->buf, m->len, fmt, ap);
70 va_end(ap);
71 } while(ret >= (m->len - 1) || ret < 0);
72
Lev Walkinf15320b2004-06-03 03:38:44 +000073 m->len = ret;
Lev Walkinf15320b2004-06-03 03:38:44 +000074
Lev Walkinc8285712005-03-04 22:18:20 +000075 if(arg->target->target == OT_INCLUDES
76 || arg->target->target == OT_FWD_DECLS
77 || arg->target->target == OT_POST_INCLUDE) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +000078 out_chunk_t *v;
Lev Walkin59004fa2004-08-20 13:37:01 +000079 TQ_FOR(v, &dst->chunks, next) {
Lev Walkin3dcaafa2004-08-11 05:21:32 +000080 if(m->len == v->len
81 && !memcmp(m->buf, v->buf, m->len))
82 break;
83 }
84 if(v) {
85 /* Entry is already present. Skip it. */
86 free(m->buf);
87 free(m);
88 return 0;
89 }
90 }
Lev Walkinf15320b2004-06-03 03:38:44 +000091
Lev Walkin59004fa2004-08-20 13:37:01 +000092 TQ_ADD(&dst->chunks, m, next);
Lev Walkinf15320b2004-06-03 03:38:44 +000093
94 return 0;
95}