blob: a6432418eab9dedf0f341e2bf49a121706ea179b [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include "asn1c_internal.h"
2
3/*
4 * Add an elementary chunk of target language text
5 * into appropriate output stream.
6 */
7int
8asn1c_compiled_output(arg_t *arg, const char *fmt, ...) {
9 const char *p;
10 int lf_found;
11 va_list ap;
12 out_chunk_t *m;
13 char *buf;
14 int ret;
15
16 /*
17 * Make sure the output has a single LF and only at the end.
18 */
19 for(lf_found = 0, p = fmt; *p; p++) {
20 if(*p == '\n') {
21 lf_found++;
22 assert(p[1] == '\0');
23 }
24 }
25 assert(lf_found <= 1);
26
27 /*
28 * Print out the indentation.
29 */
30 if(arg->indented == 0) {
31 int i = arg->indent_level;
32 arg->indented = 1;
33 while(i--) {
34 ret = asn1c_compiled_output(arg, "\t");
35 if(ret == -1) return -1;
36 }
37 }
38
39 /*
40 * Estimate necessary size.
41 */
42 buf = "";
43 va_start(ap, fmt);
44 ret = vsnprintf(buf, 0, fmt, ap);
45 va_end(ap);
46 assert(ret >= 0);
47
48 /*
49 * Allocate buffer.
50 */
51 m = calloc(1, sizeof(out_chunk_t));
52 if(m == NULL) return -1;
53 m->len = ret + 1;
54 m->buf = malloc(ret + 1);
55 if(m->buf == NULL) {
56 free(m);
57 return -1;
58 }
59
60 /*
61 * Fill the buffer.
62 */
63 va_start(ap, fmt);
64 ret = vsnprintf(m->buf, m->len, fmt, ap);
65 assert(ret < m->len);
66 m->len = ret;
67 va_end(ap);
68
69 TQ_ADD(&(arg->target->targets[arg->target->target]), m, next);
70
71 if(lf_found)
72 arg->indented = 0;
73
74 return 0;
75}