Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2 | #include "asn1c_out.h" |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 3 | |
| 4 | /* |
| 5 | * Add an elementary chunk of target language text |
| 6 | * into appropriate output stream. |
| 7 | */ |
| 8 | int |
| 9 | asn1c_compiled_output(arg_t *arg, const char *fmt, ...) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 10 | struct compiler_stream_destination_s *dst; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | const char *p; |
| 12 | int lf_found; |
| 13 | va_list ap; |
| 14 | out_chunk_t *m; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | int ret; |
| 16 | |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 17 | switch(arg->target->target) { |
| 18 | case OT_IGNORE: |
| 19 | return 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 20 | default: |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 21 | dst = &arg->target->destination[arg->target->target]; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 22 | break; |
| 23 | } |
| 24 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | /* |
| 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 Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 39 | if(dst->indented == 0) { |
| 40 | int i = dst->indent_level; |
| 41 | dst->indented = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | while(i--) { |
| 43 | ret = asn1c_compiled_output(arg, "\t"); |
| 44 | if(ret == -1) return -1; |
| 45 | } |
| 46 | } |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 47 | if(lf_found) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 48 | dst->indented = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | |
| 50 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | * Allocate buffer. |
| 52 | */ |
| 53 | m = calloc(1, sizeof(out_chunk_t)); |
| 54 | if(m == NULL) return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | |
Lev Walkin | a460ba3 | 2004-10-20 15:40:04 +0000 | [diff] [blame] | 56 | 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 Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | m->len = ret; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 75 | if(arg->target->target == OT_INCLUDES |
| 76 | || arg->target->target == OT_FWD_DECLS |
| 77 | || arg->target->target == OT_POST_INCLUDE) { |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 78 | out_chunk_t *v; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 79 | TQ_FOR(v, &dst->chunks, next) { |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 80 | 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 Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 92 | TQ_ADD(&dst->chunks, m, next); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 93 | |
| 94 | return 0; |
| 95 | } |