File: parser.awk

package info (click to toggle)
zplug 2.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,068 kB
  • sloc: sh: 1,504; awk: 235; makefile: 26
file content (63 lines) | stat: -rw-r--r-- 1,320 bytes parent folder | download | duplicates (4)
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
BEGIN {
    KEY_SEP  = ":";
    TAGS_SEP = ", ";
}

{
    arr[NR] = $0;
}

END {
    if (get_tag(arr, package) != 0)
        exit 1;
}

function get_name(arr, tag,     n, tags, i, name) {
    n = split(tag, tags, "#");
    for (i in tags) {
        if (tags[i] ~ /\//) {
            name = tags[i];
            break;
        }
    }
    return name;
}

function get_tag(line, tag,    name, i, j, n, tags, line_tags, ret, key_and_value, key, value) {
    name = get_name(line, tag);
    if (name == "") {
        return 1;
    }
    n = split(tag, tags, "#");

    for (i in line) {
        if (line[i] ~ "name" KEY_SEP name TAGS_SEP) {
            m = split(line[i], line_tags, TAGS_SEP);
            for (j in line_tags) {
                split(line_tags[j], key_and_value, KEY_SEP);
                key   = key_and_value[1];
                value = key_and_value[2];
                if (n == 1) {
                    ret[key] = value;
                }
                if (in_array(key, tags) == 0) {
                    ret[key] = value;
                }
            }
        }
    }

    if (length(ret) == 0)
        return 1;

    for (key in ret)
        print key KEY_SEP ret[key];
}

function in_array(e, arr,    i) {
    for (i in arr) {
        if (arr[i] == e)
            return 0;
    }
    return 1;
}