Commit | Line | Data |
---|---|---|
feec80c3 C |
1 | #!/bin/sh -e |
2 | ||
3 | # This is a program that gives scripted replies | |
4 | # to queries that are normally handled by programs | |
5 | # such as pkg-config and ocamlfind. This program | |
6 | # can serve as a poor substitute. | |
7 | ||
8 | # the replies file contains sets of two lines. The | |
9 | # first line is an extended regex to match against | |
10 | # the command line. The second line is the reply to | |
11 | # print to stdout. Variables ${BASH_REMATCH[i]} may be | |
12 | # used to match against capture groups. | |
13 | ||
14 | # the replies file assumes that the | |
15 | # libpcre and python libraries are installed, and | |
16 | # that none of the optional ocaml libraries are | |
17 | # installed. | |
18 | ||
19 | cmdline="$@" | |
20 | scriptdir=$(dirname "$BASH_SOURCE") | |
21 | responsefile="$scriptdir/replies.txt" | |
22 | ||
23 | # learning mode | |
24 | # echo "$cmdline" >> /tmp/queries.txt | |
25 | ||
26 | # some helper functions callable from the replacement macros | |
27 | function ocamllibdir { | |
28 | ocamlc -where &2>/dev/null | |
29 | } | |
30 | ||
31 | # outputs with what prefix 'python' was configured | |
32 | function pythonprefix { | |
33 | python -c "import sys; print(sys.prefix)" &2>/dev/null | |
34 | } | |
35 | ||
36 | # outputs the "include" cflags for python | |
37 | function pythoncflags { | |
38 | local version=$1 | |
39 | local prefix="$(pythonprefix)" | |
40 | test $? == 0 | |
41 | echo "-I${prefix}/include/python${version}" | |
42 | } | |
43 | ||
44 | # outputs the "linker" flags for python | |
45 | function pythonlibs { | |
46 | local version=$1 | |
47 | local prefix="$(pythonprefix)" | |
48 | ||
49 | echo "-L${prefix}/lib -lpython${version}" | |
50 | } | |
51 | ||
52 | # succeeds only if "/usr/include/pcre.h" exists | |
53 | function checkpcre { | |
54 | test -f /usr/include/pcre.h | |
55 | } | |
56 | ||
57 | # interate through pattern-response pairs | |
58 | while read pattern | |
59 | do | |
60 | # empty lines preceeding pattern | |
61 | if test -z "$pattern"; then | |
62 | continue | |
63 | fi | |
64 | ||
65 | # response may be empty | |
66 | read response | |
67 | ||
68 | if [[ $cmdline =~ $pattern ]]; then | |
69 | if test -n "$response"; then | |
70 | (eval "R=\"$response\""; test $? == 0; echo $R) | |
71 | test $? == 0 | |
72 | fi | |
73 | ||
74 | exit 0 | |
75 | fi | |
76 | done < $responsefile | |
77 | ||
78 | # fallback case | |
79 | echo "fake-subst.sh: no substitution for: $cmdline" 1>&2 | |
80 | exit 1 |