summaryrefslogtreecommitdiffabout
path: root/src/cmdline.opt
blob: c4afef4ca492d195cec5f4da6cf700a444a1245b (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* This file is part of cfpeek  -*- c -*-
   Copyright (C) 2011, 2012 Sergey Poznyakoff

   Cfpeek 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.

   Cfpeek 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 cfpeek.  If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include <unistd.h>
#include "grecs/gitid.h"

static struct grecs_txtacc *pp_cmd_acc;
char *program_name;

#define PREPROC_NOT_SET 0
#define PREPROC_SET     1
#define PREPROC_DEFAULT 2
static int preproc_settings = PREPROC_NOT_SET;

#define CFPEEK_NODE_FLAG_DEFAULT \
	(GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE|GRECS_NODE_FLAG_DESCEND)

static int flags_on, flags_off;

struct format_flag {
	char *name;
	char *arg;
	char *descr;
	int flag;
	void (*handler)(const char *);
};

static void
set_parent_option(const char *str)
{
	struct locate_instr *instr = new_locate_instr();
	instr->cmd = locate_parent;
	instr->v.s = grecs_strdup(str);
}

static void
set_child_option(const char *str)
{
	struct locate_instr *instr = new_locate_instr();
	instr->cmd = locate_child;
	instr->v.s = grecs_strdup(str);
}

static void
set_sibling_option(const char *str)
{
	struct locate_instr *instr = new_locate_instr();
	instr->cmd = locate_sibling;
	instr->v.s = grecs_strdup(str);
}

static void
set_up_option(const char *str)
{
	struct locate_instr *instr = new_locate_instr();
	instr->cmd = locate_up;
	instr->v.n = atoi(str);
}

static void
set_down_option(const char *str)
{
	struct locate_instr *instr = new_locate_instr();
	instr->cmd = locate_down;
	instr->v.n = atoi(str);
}

static void
set_delim_option(const char *str)
{
	flags_on |= *str & 0xff;
}

struct format_flag format_flag_table[] = {
	{ "parent", "NAME", "print the parent node with the given NAME",
	  0, set_parent_option },
	{ "child", "NAME", "print the child node with the given NAME",
	  0, set_child_option },
	{ "up", "NUMBER", "ascend NUMBER of nodes",
	  0, set_up_option },
	{ "down", "NUMBER", "descend NUMBER of nodes",
	  0, set_down_option },
	{ "sibling",
	  "NAME", "locate a sibling with the given NAME and print it",
	  0, set_sibling_option },
	{ "delim", "CHAR", "set path component delimiter",
	  0, set_delim_option },
	{ "locus", NULL, "print locations",
	  GRECS_NODE_FLAG_LOCUS },
	{ "path", NULL, "print paths",
	  GRECS_NODE_FLAG_PATH },
	{ "value", NULL, "print values",
	  GRECS_NODE_FLAG_VALUE },
	{ "quote", NULL, "always quote values",
	  GRECS_NODE_FLAG_QUOTE },
	{ "never-quote", NULL, "never quote values",
	  GRECS_NODE_FLAG_NOQUOTE },
	{ "quote-hex", NULL, "show unprintable characters as hex",
	  GRECS_NODE_FLAG_QUOTE_HEX },
	{ "descend", NULL, "descend into subnodes",
	  GRECS_NODE_FLAG_DESCEND },
	{ "default", NULL, "set default options",
	  CFPEEK_NODE_FLAG_DEFAULT },
	{ NULL }
};

static void
flags_help(FILE *stream)
{
	struct format_flag *fp;

	fprintf(stream, "[*] FLAGS is a comma-separated list of the following:\n");

	for (fp = format_flag_table; fp->name; fp++) {
		int n;
		fprintf(stream, "  ");
		if (fp->arg)
			n = fprintf(stream, "%s=%s", fp->name, fp->arg);
		else
			n = fprintf(stream, "[no]%s", fp->name);
		if (n < 24)
			n = 24 - n;
		else
			n = 2;
		fprintf(stream, "%*s%s\n", n, "", fp->descr);
	}
	fputc('\n', stream);
}

static const char *
cmpasgn(const char *a, const char *b)
{
	for (; *a && *b; a++, b++)  {
		if (*a != *b)
			return NULL;
	}
	if (*a == 0 && *b == '=')
		return b+1;

	return NULL;
}

static void
set_flag(const char *str)
{
	struct format_flag *fp;

	for (fp = format_flag_table; fp->name; fp++) {
		if (fp->arg) {
			const char *arg = cmpasgn(fp->name, str);
			if (arg) {
				fp->handler(arg);
				return;
			}
		} else if (strcmp(str, fp->name) == 0) {
			if (fp->handler)
				fp->handler(str);
			else
				flags_on |= fp->flag;
			return;
		} else if (strncmp(str, "no", 2) == 0 &&
			   strcmp(str + 2, fp->name) == 0) {
			if (fp->handler)
				fp->handler(str);
			else
				flags_off |= fp->flag;
			return;
		}
	}
	grecs_error(NULL, 0, "unknown option: %s", str);
}

OPTIONS_BEGIN("cfpeek",
              [<Get variables from a configuration file>],
	      [<FILE [PATH...]>],
	      [<gnu>],
              [<copyright_year=2011,2012>],
              [<copyright_holder=Sergey Poznyakoff>])

GROUP(Output control)

OPTION(format,H,[<FLAGS>],
       [<set output format flags [*]>])
BEGIN
        int i;
        struct wordsplit ws;
	ws.ws_delim = ",";
	if (wordsplit(optarg, &ws,
		      WRDSF_DELIM|WRDSF_SQUEEZE_DELIMS|
		      WRDSF_NOVAR|WRDSF_NOCMD|WRDSF_SHOWERR))
		exit(EX_SOFTWARE);
	for (i = 0; i < ws.ws_wordc; i++) {
		set_flag(ws.ws_wordv[i]);
	}
	wordsplit_free(&ws);
END

OPTION(quiet,q,,
       [<suppress error diagnostics>])
BEGIN
	quiet_option++;
END

GROUP(Modifiers)

OPTION(parser,p,[<GRECS|META1|BIND|GIT|PATH>],
       [<set parser type>])
BEGIN
	if (strcasecmp(optarg, "GRECS") == 0) {
		grecs_parser_fun = grecs_grecs_parser;
		if (preproc_settings == PREPROC_NOT_SET)
			preproc_settings = PREPROC_DEFAULT;
	} else if (strcasecmp(optarg, "META1") == 0)
		grecs_parser_fun = grecs_meta1_parser;
	else if (strcasecmp(optarg, "BIND") == 0)
		grecs_parser_fun = grecs_bind_parser;
	else if (strcasecmp(optarg, "DHCPD") == 0)
		grecs_parser_fun = grecs_dhcpd_parser;
	else if (strcasecmp(optarg, "GIT") == 0)
		grecs_parser_fun = grecs_git_parser;
	else if (strcasecmp(optarg, "PATH") == 0)
		grecs_parser_fun = grecs_path_parser;
	else {
		grecs_error(NULL, 0, "unsupported parser type");
		exit(EX_USAGE);
	}
END			 

OPTION(literal,L,,
       [<use literal matching>])
BEGIN
	mode = MODE_LITERAL;
END
	
OPTION(reduce,r,,
       [<reduce the tree>])
BEGIN
	reduce_option = 1;
END

OPTION(sort,S,,
       [<sort parse tree lexicografically>])
BEGIN
       sort_option = 1;
END
       
OPTION(matches,m,NUMBER,
       [<output at most NUMBER matches>])
BEGIN
	max_matches = atoi(optarg);
END

OPTION(set,s,PATH=VAL,
       [<set a keyword>])
BEGIN
        struct wordsplit ws;

	ws.ws_delim = "=";
	if (wordsplit(optarg, &ws, WRDSF_NOVAR | WRDSF_NOCMD |
                                   WRDSF_QUOTE | WRDSF_CESCAPES	|
				   WRDSF_DELIM)) {
	     grecs_error(NULL, 0, "cannot split string: %s",
	                 wordsplit_strerror(&ws));
	     exit(EX_SOFTWARE);
	}
        if (ws.ws_wordc != 2) {
                grecs_error(NULL, 0, "set: invalid syntax");
		exit(EX_USAGE);
	}
	OPTNODE(ws.ws_wordv[0], ws.ws_wordv[1]);
        wordsplit_free(&ws);
END

GROUP(Scripting)

OPTION(lang,l,NAME,
       [<select scripting language to use>])
BEGIN
	script_select(optarg);
END
	
OPTION(file,f,FILE,
       [<load the script FILE>])
BEGIN
	script_file = optarg;
END

OPTION(expression,e,EXPR,
       [<apply this expression to each node found>])
BEGIN
	script_expr = optarg;
END

OPTION(init,i,EXPR,
       [<initial setup expession>])
BEGIN
	script_init_expr = optarg;
END

OPTION(done,d,EXPR,
       [<final cleanup expession>])
BEGIN
	script_done_expr = optarg;
END

GROUP(Preprocessor control)

OPTION(include-directory,I,DIR,
       [<add include directory>])
BEGIN
        if (preproc_settings == PREPROC_NOT_SET)
		preproc_settings = PREPROC_DEFAULT;
	grecs_preproc_add_include_dir(optarg);
END

OPTION(define,D,SYMBOL[=VALUE],
       [<define a preprocessor symbol>])
BEGIN
        char *p;

        if (preproc_settings == PREPROC_NOT_SET)
		preproc_settings = PREPROC_DEFAULT;
        if (!pp_cmd_acc)
		pp_cmd_acc = grecs_txtacc_create();
        grecs_txtacc_grow(pp_cmd_acc, " \"-D", 4);
        for (p = optarg; *p; p++) {
		if (*p == '\\' || *p == '"')
			grecs_txtacc_grow_char(pp_cmd_acc, '\\');
		grecs_txtacc_grow_char(pp_cmd_acc, *p);
	}
	grecs_txtacc_grow_char(pp_cmd_acc, '"');			
END

OPTION(preprocessor,P,COMMAND,
       [<use COMMAND instead of the default preprocessor>])
BEGIN
	grecs_preprocessor = optarg;
        preproc_settings = PREPROC_SET;
END

OPTION(no-preprocessor,N,,
       [<disable preprocessing>])
BEGIN
	grecs_preprocessor = NULL;
        preproc_settings = PREPROC_SET;
END

GROUP(Debugging)

OPTION(debug-parser,x,,
       [<debug configuration file parser>])
BEGIN
	grecs_gram_trace(1);
END

OPTION(debug-lexer,X,,
       [<debug configuration file lexer>])
BEGIN
	grecs_lex_trace(1);
END

OPTIONS_END

void
cfpeek_version_hook(FILE *stream)
{
	fprintf(stream, "compiled with:\n");
	fprintf(stream, "  Grecs %s\n", GRECS_GIT_ID);
#ifdef GUILE_VERSION
	fprintf(stream, "  Guile %s\n", GUILE_VERSION);
#endif
}


void
parse_options(int argc, char *argv[])
{
	program_name = strrchr(argv[0], '/');
	if (program_name)
		program_name++;
	else
		program_name = argv[0];
	print_help_hook = flags_help;
	print_version_hook = cfpeek_version_hook;
        GETOPT(argc, argv, file_index, exit(EX_USAGE))
	if (!(flags_on & _GRECS_NODE_MASK_OUTPUT)) 
		flags_on |= GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE;
	flags = flags_on & ~flags_off;
	if (!(flags & (GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE))) {
		grecs_warning(NULL, 0, "assuming --format=path,value");
		flags |= GRECS_NODE_FLAG_PATH|GRECS_NODE_FLAG_VALUE;
	}
	if (preproc_settings == PREPROC_DEFAULT)
		grecs_preprocessor = DEFAULT_PREPROCESSOR;
	if (pp_cmd_acc && grecs_preprocessor) {
		char *args, *cmd;

		grecs_txtacc_grow_char(pp_cmd_acc, 0);
		args = grecs_txtacc_finish(pp_cmd_acc, 0);
		cmd = grecs_malloc(strlen(grecs_preprocessor) +
				   strlen(args) + 1);
		strcpy(cmd, grecs_preprocessor);
		strcat(cmd, args);
		grecs_preprocessor = cmd;
	}
	grecs_txtacc_free(pp_cmd_acc);

	script_init();
}

Return to:

Send suggestions and report system problems to the System administrator.