summaryrefslogtreecommitdiff
path: root/build-aux/extract-syscall-ranges.sh
blob: 3826fc25fe3c252caf011b65e2dc7d95943145d4 (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
#!/bin/sh

if test "$#" -lt 1 || test "$#" -gt 2
then
    echo "Usage: extract-syscall-ranges.sh FILENAME [abiname_regex]"
    exit 1
fi

numbers_to_ranges()
{
    if ! read number
    then
        printf '{}\n'
        return
    fi
    low="$number"
    high="$number"
    while true
    do
        if read number
        then
            if test "$number" -eq "$((high + 1))"
            then
                high="$number"
            else
                break
            fi
        else
            printf '{ {%d, %d} }\n' "$low" "$high"
            return
        fi
    done
    printf '{ {%d, %d}' "$low" "$high"
    low="$number"
    high="$number"
    while true
    do
        if read number
        then
            if test "$number" -eq "$((high + 1))"
            then
                high="$number"
            else
                printf ', {%d, %d}' "$low" "$high"
                low="$number"
                high="$number"
            fi
        else
            printf ', {%d, %d} }\n' "$low" "$high"
            return
        fi
    done
}

if test "$#" -eq 2
then
    abi_regex="$2"
    getnumbers()
    {
        # delete comment lines and space-only lines
        sed -e '/^[[:space:]]*#/d ; /^[[:space:]]*$/d' |
            # filter to only include lines with target abi or "common"
            grep -E "^[0-9]+[[:space:]]+(common|(${abi_regex}))[[:space:]]" |
            # limit to only syscall number
            sed -e 's/\([0-9]\+\).*/\1/g'
    }
else
    getnumbers()
    {
        # delete comment lines and space-only lines and limit to syscall number
        sed -e '/^[[:space:]]*#/d ; /^[[:space:]]*$/d ; s/\([0-9]\+\).*/\1/g'
    }
fi

getnumbers < "$1" |
    sort -n |
    uniq | # Yes, there are duplicate syscall entries...
    numbers_to_ranges