1#!/bin/bash
2# Stop immediately if any executed command has exit status different from 0.
3set -eu
4
5script_name="$(basename "$0")"
6script_dir="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
7
8fatal() {
9  echo "${script_name}: $*" >&2
10  exit 1
11}
12
13usage() {
14  fatal "Usage: ${script_name} DATABASE OUT_DIR"
15}
16
17extrapolate_regex() {
18  lookbehind=$1
19  file=$2
20  grep -Po "(?<=${lookbehind}\"\\^).*(?=\\$\")" "${file}" | sed 's/\\\\/\\/'
21}
22
23if [ $# -lt 2 ]; then
24  usage
25fi
26
27DB=$1
28OUT_DIR=$2
29
30files_txt="${OUT_DIR}/files.txt"
31files_c_txt="${OUT_DIR}/files_c.txt"
32files_h_txt="${OUT_DIR}/files_h.txt"
33exclusions_txt="${OUT_DIR}/exclusions.txt"
34
35
36if [[ ! -d "${OUT_DIR}" ]]; then
37  mkdir -p "${OUT_DIR}"
38else
39  rm -f "${files_txt}"
40  rm -f "${files_c_txt}"
41  rm -f "${files_h_txt}"
42  rm -f "${exclusions_txt}"
43fi
44
45# Generating txt report with files
46"${ECLAIR_BIN_DIR}eclair_report" -db="${DB}" -files_txt="${files_txt}"
47
48{
49  # Extracting out of scope and adopted code
50  adopted_ecl="${script_dir}/adopted.ecl"
51  extrapolate_regex adopted,             "${adopted_ecl}"
52  out_of_scope_ecl="${script_dir}/out_of_scope.ecl"
53  extrapolate_regex adopted,             "${out_of_scope_ecl}"
54  extrapolate_regex out_of_scope_tools,  "${out_of_scope_ecl}"
55  extrapolate_regex out_of_scope,        "${out_of_scope_ecl}"
56  extrapolate_regex hypercall_ABI,       "${out_of_scope_ecl}"
57  extrapolate_regex "hide, "             "${out_of_scope_ecl}"
58} >"${exclusions_txt}"
59sort -o "${exclusions_txt}" -u "${exclusions_txt}"
60
61# Removing exclusions from files_txt
62grep -E -v "(object: |/dev/pipe)" "${files_txt}" > "${files_txt}.tmp"
63grep -vf "${exclusions_txt}" "${files_txt}.tmp" > "${files_txt}"
64rm "${files_txt}.tmp"
65# Creating files with only headers
66grep -Ev "(xen.*\.(h\w+|[^h]\w*) |.*ecl)" "${files_txt}" > "${files_h_txt}"
67# Creating files with only c files
68grep -Ev "(xen.*\.(c\w+|[^c]\w*) |.*ecl)" "${files_txt}" > "${files_c_txt}"
69