Download c source code

 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
#include <stdio.h>

#include "args.h"

const char* const help =
	"usage: corr [-ein] regexp\n"
	"\n"
	"options:\n"
	"  -e extended\n"
	"  -i ignore case\n"
	"  -n new line chars separate strings\n"
	;

void args_init(args_t* args)
{
	args->extended = false;
	args->ignore_case = false;
	args->new_line = false;
	args->pattern = NULL;
}

int args_analyze(args_t* args, int argc, char* argv[])
{
	for ( int index = 1; index < argc; ++index )
	{
		if ( *argv[index] == '-' )
		{
			for ( const char* option = argv[index] + 1; *option; ++option )
				switch ( *option )
				{
					case 'e': args->extended = true; break;
					case 'i': args->ignore_case = true; break;
					case 'n': args->new_line = true; break;
					default: return fprintf(stderr, "error: invalid option: %c\n", *option), 1;
				}
		}
		else
		{
			if ( args->pattern == NULL )
				args->pattern = argv[index];
			else
				return fprintf(stderr, "error: multiple regular expressions: %s\n", argv[index]), 2;
		}
	}
	
	return 0;
}

int args_print_help(void)
{
	printf("%s", help);
	return 0;
}

void args_destroy(args_t* args)
{
	(void)args;
}