1#!/usr/local/bin/bash
2
3SCRIPT_NAME=[[`basename "$0"`]]
4SCRIPT_DIR=`dirname "$0"`
5
6# Expects args: include paths and libs for READLINE
7# expects env variables CXX and CXXFLAGS inherited from parent shell.
8
9# Check READLINE lib is compatible with CXXFLAGS (from GMP)
10# Exit code is 0 if compatible, and output is one of -ltermcap, -lncurses, -lcurses.
11# Exit code is 1 is not compatible (no mesg is output)
12# Exit code is 2 is input was bad (a diagnostic is output on /dev/stderr).
13
14# taken from StackExchange 256434
15is_absolute()
16{
17    case "$1" in
18	///* | //) true;;
19	//*) false;;
20	/*) true;;
21	*) false;;
22    esac
23}
24
25
26if [ $# -ne 2 ]
27then
28    echo "ERROR: expected 2 args (abs paths of readline header and libreadline)   $SCRIPT_NAME" > /dev/stderr
29    exit 2
30fi
31
32READLINE_HDR="$1"
33READLINE_LIB="$2"
34# The following is a cryptic if...then block
35is_absolute "$READLINE_HDR" || is_absolute "$READLINE_LIB" ||
36(
37  echo "ERROR: args must be absolute paths (readline header and libreadline)   $SCRIPT_NAME"   > /dev/stderr
38  exit 1
39)
40
41
42READLINE_HDR_DIR=`dirname "$1"`
43READLINE_HDR_DIR_DIR=`dirname "$READLINE_HDR_DIR"`
44
45if [ -z "$CXX" ]
46then
47  echo "ERROR: environment variable CXX must be set.   $SCRIPT_NAME"   > /dev/stderr
48  exit 1
49fi
50
51# Create tmp directory, put test prog in it, compile and run.
52umask 22
53source "$SCRIPT_DIR/shell-fns.sh"
54TMP_DIR=`mktempdir readline-check-cxxflags`
55
56pushd "$TMP_DIR"  > /dev/null
57/bin/cat > test-readline.c <<EOF
58#include "stdlib.h"
59#include "stdio.h"
60#include "unistd.h"
61#include "readline/readline.h"
62#include "readline/history.h"
63
64#include <string>
65
66int main()
67{
68  char *line;
69  line = readline("");
70  std::string str(line);
71  free(line);
72  // We shall test on an input of 3 chars...
73  if (str.size() != 3) return 1;
74  return 0;
75}
76EOF
77
78for TERMCAP in termcap ncurses curses
79do
80  echo "Trying TERMCAP=$TERMCAP"  >> LogFile
81  $CXX $CXXFLAGS -I"$READLINE_HDR_DIR_DIR" test-readline.c -o test-readline "$READLINE_LIB" -l$TERMCAP  >> LogFile 2>&1
82  if [ $? = 0 ]
83  then
84    LIBTERMCAP=-l$TERMCAP
85    break
86  fi
87done
88
89if [ -z "$LIBTERMCAP" ]
90then
91  echo "ERROR: did not find suitable termcap/curses library   $SCRIPT_NAME"  > /dev/stderr
92  exit 1
93fi
94
95/bin/cat > test-readline.in <<EOF
96abc
97EOF
98
99./test-readline < test-readline.in  >> LogFile  2>&1
100if [ $? -ne 0 ]
101then
102  echo "ERROR: test program gave run-time error   $SCRIPT_NAME"  > /dev/stderr
103  exit 1
104fi
105
106# Clean up TMP_DIR
107popd  > /dev/null
108/bin/rm -rf "$TMP_DIR"
109echo "$LIBTERMCAP"
110exit 0
111