summaryrefslogtreecommitdiff
path: root/bin/config.awk
blob: 9d0a117f27720e6f0f40c010b43d74a868df36d9 (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
#!/usr/bin/awk -f

BEGIN {
	FS = "[ ]"; # use a single space as field separator and don't trim input
	ind = 0; # indentation level
	last = ARGC - 3; # last argument index
	exitcode = 1; # whether anything has been matched
	if(last < 0) { # there should be at least one argument after the filename
		exit 1;
	}
	ARGC = 2; # don't read ARGV[2] and onward as files
}

END {
	exit exitcode;
}

$0 != "" { # exit when the indentation block is exited
	for(i = 0; i < ind; i++) {
		if(! sub(/^\t/, "")) {
			exit exitcode;
		}
	}
}

# if on the last argument, interpret it as a key and print the value
ind == last && $1 == ARGV[ind + 2] {
	exitcode = 0;
	print substr($0, length($1) + 2);
}
# if not on the last argument, find the string exactly and increment indentation
ind != last && $0 == ARGV[ind + 2] {
	ind++;
}