aboutsummaryrefslogtreecommitdiffstats
path: root/libasn1compiler/asn1c_save.c
blob: 5a8d3b6e9be7a0c565209ca617d22c3c7a6d6ac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include "asn1c_internal.h"

static int asn1c_dump_streams(arg_t *arg);
static int asn1c_print_streams(arg_t *arg);
static int asn1c_save_streams(arg_t *arg);
static int asn1c_copy_over(arg_t *arg, char *path);

int
asn1c_save_compiled_output(arg_t *arg, const char *datadir) {

	(void)datadir;

	TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
		TQ_FOR(arg->expr, &(arg->mod->members), next) {
			if(asn1_lang_map[arg->expr->meta_type]
				[arg->expr->expr_type].type_cb) {
				if(asn1c_dump_streams(arg))
					return -1;
			}
		}
	}

	/*
	 * Dump out the Makefile template and the rest of the support code.
	 */
	if((arg->flags & A1C_PRINT_COMPILED) == 0
	&& (arg->flags & A1C_OMIT_SUPPORT_CODE) == 0) {
		glob_t pg;
		FILE *mkf;
		char *p;
		int i;

		i = strlen(datadir) + sizeof("/*.[ch]");
		p = alloca(i);
		snprintf(p, i, "%s/*.[ch]", datadir);

		memset(&pg, 0, sizeof(pg));
		if(glob(p, GLOB_ERR
#ifdef	GLOB_TILDE
			| GLOB_TILDE
#endif	/* GLOB_TILDE */
		, NULL, &pg)) {
			fprintf(stderr,
				"Bad skeletons directory (-S) %s: %s\n",
				datadir, strerror(errno));
			return -1;
		}

		mkf = asn1c_open_file(arg, "Makefile.am", ".sample");
		if(mkf == NULL) {
			globfree(&pg);
			perror("Makefile.am.sample");
			return -1;
		}

		fprintf(mkf, "ASN_SRCS=");
		TQ_FOR(arg->mod, &(arg->asn->modules), mod_next) {
			TQ_FOR(arg->expr, &(arg->mod->members), next) {
				if(asn1_lang_map[arg->expr->meta_type]
					[arg->expr->expr_type].type_cb) {
					fprintf(mkf, "\t\\\n\t%s.c %s.h",
					arg->expr->Identifier,
					arg->expr->Identifier);
				}
			}
		}

		for(i = 0; i < pg.gl_pathc; i++) {
			if(asn1c_copy_over(arg, pg.gl_pathv[i])) {
				fprintf(mkf, ">>>ABORTED<<<");
				fclose(mkf);
				globfree(&pg);
				return -1;
			} else {
				fprintf(mkf, "\t\\\n\t%s",
					basename(pg.gl_pathv[i]));
			}
		}

		fprintf(mkf, "\n\n");
		fprintf(mkf, "lib_LTLIBRARIES=libsomething.la\n");
		fprintf(mkf, "libsomething_la_SOURCES=${ASN_SRCS}\n");
		fclose(mkf);
		fprintf(stderr, "Generated Makefile.am.sample\n");
		globfree(&pg);
	}

	return 0;
}

/*
 * Dump the streams.
 */
static int
asn1c_dump_streams(arg_t *arg)  {
	if(arg->flags & A1C_PRINT_COMPILED) {
		return asn1c_print_streams(arg);
	} else {
		return asn1c_save_streams(arg);
	}
}

static int
asn1c_print_streams(arg_t *arg)  {
	compiler_streams_t *cs = arg->expr->data;
	asn1p_expr_t *expr = arg->expr;
	int i;

	for(i = 0; i < OT_MAX; i++) {
		out_chunk_t *ot;
		if(TQ_FIRST(&cs->targets[i]) == NULL)
			continue;

		printf("\n/*** <<< %s [%s] >>> ***/\n\n",
			_compiler_stream2str[i],
			expr->Identifier);

		TQ_FOR(ot, &(cs->targets[i]), next) {
			fwrite(ot->buf, ot->len, 1, stdout);
		}
	}

	return 0;
}

static int
asn1c_save_streams(arg_t *arg)  {
	asn1p_expr_t *expr = arg->expr;
	compiler_streams_t *cs = expr->data;
	out_chunk_t *ot;
	FILE *fp_c, *fp_h;
	char *header_id;

	if(cs == NULL) {
		fprintf(stderr, "Cannot compile %s at line %d\n",
			expr->Identifier, expr->_lineno);
		return -1;
	}

	fp_c = asn1c_open_file(arg, expr->Identifier, ".c");
	fp_h = asn1c_open_file(arg, expr->Identifier, ".h");
	if(fp_c == NULL || fp_h == NULL) {
		if(fp_c) fclose(fp_c);	/* lacks unlink() */
		if(fp_h) fclose(fp_h);	/* lacks unlink() */
		return -1;
	}

	fprintf(fp_c,
	"/*\n"
	" * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
	" * From ASN.1 module \"%s\" found in \"%s\"\n"
	" */\n\n",
		arg->mod->Identifier,
		arg->mod->source_file_name
	);
	fprintf(fp_h,
	"/*\n"
	" * Generated by asn1c-" VERSION " (http://lionet.info/asn1c)\n"
	" * From ASN.1 module \"%s\" found in \"%s\"\n"
	" */\n\n",
		arg->mod->Identifier,
		arg->mod->source_file_name
	);

	header_id = alloca(strlen(expr->Identifier) + 1);
	if(1) {
		char *src, *dst;
		for(src = expr->Identifier, dst = header_id;
				(*dst=*src); src++, dst++)
			if(!isalnum(*src)) *dst = '_';
		*dst = '\0';
	}

	fprintf(fp_h,
		"#ifndef\t_%s_H_\n"
		"#define\t_%s_H_\n"
		"\n", header_id, header_id);

	fprintf(fp_h, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n");

	fprintf(fp_h, "#include <constr_TYPE.h>\n\n");

	TQ_FOR(ot, &(cs->targets[OT_INCLUDES]), next)
		fwrite(ot->buf, ot->len, 1, fp_h);
	fprintf(fp_h, "\n");
	TQ_FOR(ot, &(cs->targets[OT_DEPS]), next)
		fwrite(ot->buf, ot->len, 1, fp_h);
	fprintf(fp_h, "\n");
	TQ_FOR(ot, &(cs->targets[OT_TYPE_DECLS]), next)
		fwrite(ot->buf, ot->len, 1, fp_h);
	fprintf(fp_h, "\n");
	TQ_FOR(ot, &(cs->targets[OT_FUNC_DECLS]), next)
		fwrite(ot->buf, ot->len, 1, fp_h);

	fprintf(fp_h, "\n#ifdef __cplusplus\n}\n#endif\n\n"
			"#endif\t/* _%s_H_ */\n",
		header_id);

	fprintf(fp_c, "#include <%s.h>\n\n", expr->Identifier);	/* Myself */
	TQ_FOR(ot, &(cs->targets[OT_STAT_DEFS]), next)
		fwrite(ot->buf, ot->len, 1, fp_c);
	TQ_FOR(ot, &(cs->targets[OT_CODE]), next)
		fwrite(ot->buf, ot->len, 1, fp_c);

	assert(OT_MAX == 6);

	fclose(fp_c);
	fclose(fp_h);
	fprintf(stderr, "Compiled %s.c\n", expr->Identifier);
	fprintf(stderr, "Compiled %s.h\n", expr->Identifier);
	return 0;
}

static int
asn1c_copy_over(arg_t *arg, char *path) {
	char *fname = basename(path);

	(void)arg;	/* Unused argument */

	if(symlink(path, fname)) {
		if(errno == EEXIST) {
			struct stat sb1, sb2;
			if(stat(path, &sb1) == 0
			&& stat(fname, &sb2) == 0
			&& sb1.st_dev == sb2.st_dev
			&& sb1.st_ino == sb2.st_ino) {
				/*
				 * Nothing to do.
				 */
				fprintf(stderr,
					"File %s is already here as %s\n",
					path, fname);
				return 0;
			} else {
				fprintf(stderr,
					"Retaining local %s (%s suggested)\n",
					fname, path);
				return 0;
			}
		} else {
			fprintf(stderr, "Symlink %s -> %s failed: %s\n",
				path, fname, strerror(errno));
			return -1;
		}
	}

	fprintf(stderr, "Symlinked %s\t-> %s\n", path, fname);

	return 0;
}