File: fake-subst.sh

package info (click to toggle)
coccinelle 1.0.8.deb-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 26,148 kB
  • sloc: ml: 136,392; ansic: 23,594; sh: 2,189; makefile: 2,157; perl: 1,576; lisp: 840; python: 823; awk: 70; csh: 12
file content (147 lines) | stat: -rwxr-xr-x 3,260 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /bin/sh -e

set -e

# This is a program that gives scripted replies
# to queries that are normally handled by programs
# such as pkg-config and ocamlfind. This program
# can serve as a poor substitute.

# the replies file contains sets of three lines. The
# first line is a pattern for grep. Grep matches
# this pattern against our command line. If fails,
# we skip to the next set of three lines.
# The second line is a replacement pattern for sed (or empty),
# which rewrites our command line, and stores the result
# in $MATCH. The third line is evaluated and may use
# $MATCH. If the execution succeeds, the script exists
# with a zero exit code. Otherwise it repeats the process with
# the next lines in the file.

# the replies file assumes that the
# libpcre and python libraries are installed, and
# that none of the optional ocaml libraries are
# installed.

if test -n "${ZSH_VERSION}"; then
  setopt BASH_REMATCH
fi

if test -z "${BASH_SOURCE}"; then
  BASH_SOURCE=$0
fi


#
# Before we actually try to substitute a given
# command, we first try to execute the original command
#

# Check if the given program exists.
if command -v "$1" > /dev/null; then
  # exits the script if the command succeeds.
  $@ && exit $?
fi


#
# Trying a substitute
#

cmdline="$@"
scriptdir=$(dirname "${BASH_SOURCE}")
responsefile="${scriptdir}/replies.txt"

# learning mode
# echo "$cmdline" >> /tmp/queries.txt

# some helper functions callable from the replacement macros
ocamllibdir() {
  ocamlc -where
}

# outputs with what prefix 'python' was configured
pythonprefix() {
  python -c "import sys; print(sys.prefix)"
}

pythonversion() {
  python -c "import sys; print('%d.%d' % (sys.version_info[0], sys.version_info[1]))"
}

pythonexists() {
  local version=$1
  local prefix="$(pythonprefix)"
  test $? = 0

  if test -z "$version"; then
    version="$(pythonversion)"
  fi

  if test ! -f "${prefix}/include/python${version}/Python.h"; then
    echo "error: ${prefix}/include/python${version}/Python.h not found (a development version of python is not installed?)" 1>&2
    false
  fi
}

# outputs the "include" cflags for python
pythoncflags() {
  local version=$1
  local prefix="$(pythonprefix)"
  test $? = 0

  echo "-I${prefix}/include/python${version}"
}

# outputs the "linker" flags for python
pythonlibs() {
  local version=$1
  local prefix="$(pythonprefix)"
  test $? = 0

  echo "-L${prefix}/lib -lpython${version}"
}

# succeeds only if "/usr/include/pcre.h" exists
checkpcre() {
  test -f /usr/include/pcre.h
}

# iterate through pattern-response pairs
found=
response=
while read pattern
do
  # empty lines preceding pattern
  if test -z "${pattern}"; then
    continue
  fi

  # response may be empty
  read replace
  read response

  if echo "${cmdline}" | grep -qE "${pattern}"; then
    found=1
    break
  fi
done < "${responsefile}"

if test -n "${found}"; then
  MATCH=no
  if test -n "${replace}"; then
    MATCH="$(echo "$cmdline" | sed -E "${replace}")"
  fi

  if test -n "${response}"; then
    (eval "R=\"${response}\""; test $? = 0; if test -n "${R}"; then echo "${R}"; fi)
    test $? = 0
  fi

  exit 0
else
  # fallback case
  echo "fake-subst.sh: no substitution for: ${cmdline}. Running the
original." 1>&2
  exec $@
fi