summaryrefslogtreecommitdiffabout
path: root/src/config.c
blob: 78f012949c7745c99ec82c2f8947240c42730d8c (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* This file is part of Eclat.
   Copyright (C) 2012, 2013 Sergey Poznyakoff.
 
   Eclat is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
 
   Eclat is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with Eclat.  If not, see <http://www.gnu.org/licenses/>. */

#include "eclat.h"

struct config_finish_hook_entry
{
	struct config_finish_hook_entry *next;
	config_finish_hook_t fun;
	void *data;
};

static struct config_finish_hook_entry *cfh_head, *cfh_tail;
	
void
add_config_finish_hook(config_finish_hook_t fun, void *data)
{
	struct config_finish_hook_entry *ent = grecs_malloc(sizeof(*ent));
	ent->next = NULL;
	ent->fun = fun;
	ent->data = data;
	if (cfh_tail)
		cfh_tail->next = ent;
	else
		cfh_head = ent;
	cfh_tail = ent;
}

int
run_config_finish_hooks()
{
	struct config_finish_hook_entry *p;
	int rc = 0;
	
	for (p = cfh_head; p; p = p->next)
		rc |= p->fun(p->data);
	return rc;
}

/* Translation table: region -> endpoint.  Its elements are of
   struct ec2_param type. */
struct grecs_symtab *ec2_regtab; 

char *
region_to_endpoint(const char *region)
{
	struct ec2_param *p, key;

	key.name = (char*) region;
	p = grecs_symtab_lookup_or_install(ec2_regtab, &key, NULL);
	return p ? p->value : NULL;
}

static int
cb_region(enum grecs_callback_command cmd,
	  grecs_locus_t *locus,
	  void *varptr,
	  grecs_value_t *value,
	  void *cb_data)
{
	struct ec2_param *p, key;
	int install = 1;
	
	if (cmd != grecs_callback_set_value) {
		grecs_error(locus, 0, "Unexpected block statement");
		return 1;
	}
	if (!value || value->type != GRECS_TYPE_ARRAY || value->v.arg.c != 2) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}
	if (value->v.arg.v[0]->type != GRECS_TYPE_STRING ||
	    value->v.arg.v[1]->type != GRECS_TYPE_STRING) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}

	key.name = value->v.arg.v[0]->v.string;
	p = grecs_symtab_lookup_or_install(ec2_regtab, &key, &install);
	if (!install)
		free(p->value); /* FIXME: Redefinition warning */
	p->value = grecs_strdup(value->v.arg.v[1]->v.string);
	return 0;
}

static int
cb_format(enum grecs_callback_command cmd,
	  grecs_locus_t *locus,
	  void *varptr,
	  grecs_value_t *value,
	  void *cb_data)
{
	if (cmd != grecs_callback_set_value) {
		grecs_error(locus, 0, "Unexpected block statement");
		return 1;
	}
	if (!value || value->type != GRECS_TYPE_ARRAY || value->v.arg.c != 2) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}
	if (value->v.arg.v[0]->type != GRECS_TYPE_STRING ||
	    value->v.arg.v[1]->type != GRECS_TYPE_STRING) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}
	set_command_format(value->v.arg.v[0]->v.string,
			   value->v.arg.v[1]->v.string,
			   &value->v.arg.v[1]->locus);
	return 0;
}

static int
cb_define_format(enum grecs_callback_command cmd,
		 grecs_locus_t *locus,
		 void *varptr,
		 grecs_value_t *value,
		 void *cb_data)
{
	if (cmd != grecs_callback_set_value) {
		grecs_error(locus, 0, "Unexpected block statement");
		return 1;
	}
	if (!value || value->type != GRECS_TYPE_ARRAY || value->v.arg.c != 2) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}
	if (value->v.arg.v[0]->type != GRECS_TYPE_STRING ||
	    value->v.arg.v[1]->type != GRECS_TYPE_STRING) {
		grecs_error(locus, 0, "expected two strings");
		return 1;
	}

	define_format(value->v.arg.v[0]->v.string,
		      value->v.arg.v[1]->v.string,
		      &value->v.arg.v[1]->locus);
	return 0;
}

static int
cb_confirm(enum grecs_callback_command cmd,
	   grecs_locus_t *locus,
	   void *varptr,
	   grecs_value_t *value,
	   void *cb_data)
{
	struct grecs_list_entry *ep;
	grecs_value_t *argval;
	enum eclat_confirm_mode cfmode;
	char *s;
	
	if (cmd != grecs_callback_set_value) {
		grecs_error(locus, 0, "Unexpected block statement");
		return 1;
	}
	if (!value || value->type != GRECS_TYPE_ARRAY || value->v.arg.c != 2) {
		grecs_error(locus, 0, "expected two values");
		return 1;
	}

	if (value->v.arg.v[0]->type != GRECS_TYPE_STRING) {
		grecs_error(locus, 0, "first argument not a string");
		return 1;
	}

	s = value->v.arg.v[0]->v.string;
	if (strcmp(s, "positive") == 0)
		cfmode = eclat_confirm_positive;
	else if (strcmp(s, "negative") == 0)
		cfmode = eclat_confirm_negative;
	else if (strcmp(s, "tty") == 0)
		cfmode = eclat_confirm_tty;
	else if (strcmp(s, "always") == 0)
		cfmode = eclat_confirm_always;
	else {
		grecs_error(&value->v.arg.v[0]->locus, 0,
			    "unrecognized confirmation mode");
		return 1;
	}
	
	switch (value->v.arg.v[1]->type) {
	case GRECS_TYPE_STRING:
		set_command_confirmation(value->v.arg.v[1]->v.string, cfmode,
					 &value->v.arg.v[1]->locus);
		return 0;

	case GRECS_TYPE_LIST:
		break;

	default:
		grecs_error(locus, 0, "second argument not a list");
		return 1;
	}

	for (ep = value->v.arg.v[1]->v.list->head; ep; ep = ep->next) {
		argval = ep->data;
		if (argval->type != GRECS_TYPE_STRING) {
			grecs_error(&argval->locus, 0,
				    "list element not a string");
			continue;
		}

		set_command_confirmation(argval->v.string, cfmode,
					 &argval->locus);
	}
	
	return 0;
}

static struct grecs_keyword ssl_kw[] = {
	{ "enable", NULL,
	  "Use SSL",
	  grecs_type_bool, GRECS_DFLT, &use_ssl },
	{ "verify", NULL,
	  "Verify peer certificate",
	  grecs_type_bool, GRECS_DFLT, &ssl_verify_peer },
	{ "ca-file", NULL,
	  "File holding CA certificates",
	  grecs_type_string, GRECS_DFLT, &ssl_ca_file },
	{ "ca-path", NULL,
	  "Directory holding files with CA certificates",
	  grecs_type_string, GRECS_DFLT, &ssl_ca_path },
	{ NULL }
};

static int
cb_ssl(enum grecs_callback_command cmd,
       grecs_locus_t *locus,
       void *varptr,
       grecs_value_t *value,
       void *cb_data)
{
	if (cmd == grecs_callback_set_value) {
		if (!value || value->type != GRECS_TYPE_STRING ||
		    grecs_string_convert(&use_ssl, grecs_type_bool,
					 value->v.string, &value->locus)) {
			grecs_error (value ? &value->locus : locus, 0,
				     _("expected boolean value"));
			return 1;
		}
	}
	return 0;
}

static struct grecs_keyword eclat_kw[] = {
	{ "default-endpoint", "hostname",
	  "Set default EC2 endpoint",
	  grecs_type_string, GRECS_DFLT, &endpoint },
	{ "region", "<name: string> <endpoint: string>",
	  "Define a region",
	  grecs_type_string, GRECS_MULT, NULL, 0, cb_region },
	{ "signature-version", "version",
	  "Signature version",
	  grecs_type_string, GRECS_DFLT, &signature_version },
	{ "access-file", "file",
	  "Specify a file containing `accessID:accessKey' pairs",
	  grecs_type_string, GRECS_DFLT, &access_file_name },
	{ "default-region", "name",
	  "Define default AWS region",
	  grecs_type_string, GRECS_DFLT, &region_name },
	{ "ssl", NULL,
	  "Configure SSL.  The ssl keyword can also be used in scalar form, like this:\n\n"
	  "  ssl yes;\n"
	  "\n"
	  "Use this form if you don't need any fine tuning and only wish to enable\n"
	  "or disable SSL.\n\n"
	  "By default SSL is disabled."
	  ,
	  grecs_type_section, GRECS_DFLT,
	  NULL, 0,
	  cb_ssl, NULL, ssl_kw },
	{ "format", "<command: string> <format: string>",
	  "Set default format for <command>",
	  grecs_type_string, GRECS_MULT, NULL, 0, cb_format },
	{ "define-format", "<name: string> <format: string>",
	  "Define a named format",
	  grecs_type_string, GRECS_MULT, NULL, 0, cb_define_format },
	{ "format-file", "file", "Read format from <file>",
	  grecs_type_string, GRECS_DFLT, &format_file },
	{ "map", "name: string", "Configure a map",
	  grecs_type_section, GRECS_INAC },
	{ "confirm",
	  "<mode: { positive | negative | tty | always }> <commands: list>",
	  "Set confirmation mode",
	  grecs_type_string, GRECS_LIST, NULL, 0, cb_confirm },
	{ "translate", NULL,
	  "Use ID translation by default",
	  grecs_type_bool, GRECS_DFLT, &translation_enabled },
	{ NULL }
};

void
config_help()
{
	static char docstring[] =
		N_("Configuration file structure for eclat.\n"
		   "For more information, use `info eclat configuration'.");
	grecs_print_docstring(docstring, 0, stdout);
	grecs_print_statement_array(eclat_kw, 1, 0, stdout);
	eclat_map_confhelp();
}

static void
grecs_print_diag(grecs_locus_t const *locus, int err, int errcode,
		 const char *msg)
{
	diag(locus, err ? NULL : "warning", "%s", msg);
}

void
config_init()
{
	grecs_include_path_setup(DEFAULT_VERSION_INCLUDE_DIR,
				 DEFAULT_INCLUDE_DIR, NULL);
	grecs_preprocessor = DEFAULT_PREPROCESSOR;
	grecs_log_to_stderr = 1;
	grecs_adjust_string_locations = 1;
	grecs_print_diag_fun = grecs_print_diag;

	ec2_regtab = grecs_symtab_create_default(sizeof(struct ec2_param));
}

void
config_finish(struct grecs_node *tree)
{
	struct grecs_node *node;

	for (node = tree; node; node = node->next) {
		struct eclat_map *map;
		
		node = grecs_find_node(node, "map");
		if (!node)
			break;
		eclat_map_config(node, &map);
	}

	grecs_tree_reduce(tree, eclat_kw, GRECS_AGGR);
	if (debug_level(ECLAT_DEBCAT_CONF)) {
		grecs_print_node(tree, GRECS_NODE_FLAG_DEFAULT, stderr);
		fputc('\n', stdout);
	}
	if (grecs_tree_process(tree, eclat_kw))
		exit(EX_CONFIG);
	if (grecs_error_count || run_config_finish_hooks())
		exit(EX_CONFIG);
}	



Return to:

Send suggestions and report system problems to the System administrator.