Project

General

Profile

setup_ncurses6x.sh

Added building the the tread-safe version of NCURSES - Eugenie Lyzenko, 02/15/2025 03:20 PM

Download (5.66 KB)

 
1
#!/bin/bash
2

    
3
#set -x # Set to help debug
4

    
5
show_usage()
6
{
7
   i=0;        usage[$i]="Usage: $0 [-p<path>] [-fsad]"
8
   i=$((i+1)); usage[$i]="Where:"
9
   i=$((i+1)); usage[$i]="f \t= Use patched NCURSES version, thread-safe is default"
10
   i=$((i+1)); usage[$i]="p \t= Use specific path instead of current directory"
11
   i=$((i+1)); usage[$i]="s \t= Set system-wide environment instead of just current user"
12
   i=$((i+1)); usage[$i]="a \t= Force setup of /etc/apt/sources.list and restore when done (requires sudo)"
13
   i=$((i+1)); usage[$i]="d \t= Dry run. Just show commands."
14

    
15
   for i in "${usage[@]}"; do
16
      echo -e $i
17
   done
18
}
19

    
20
# process options
21
while getopts "h?p:fsad" opt; do
22
   case $opt in
23
      p  ) target_dir=$OPTARG ;;
24
      f  ) patch_ncurses=true ;;
25
      s  ) system=true ;;
26
      a  ) handle_apt=true ;;
27
      d  ) dry="echo" ;;
28
      h | \
29
      \? ) show_usage && exit 1
30
           exit 1 ;;
31
   esac
32
done
33
shift $(($OPTIND - 1))
34

    
35
# remove previous install, if any
36
$dry rm -rf ncurses*
37

    
38
# Learn the OS so that we can handle non-Ubuntu "kind of" gracefully
39
if [ "$(cat /etc/os-release | grep "^ID=" | cut -d'=' -f2 | sed "s/\"//g")" = "rhel" ]; then
40
   is_rhel=true
41
fi
42

    
43
# get up to date version
44
if [[ "$handle_apt" == "true" ]]; then
45
   $dry cp /etc/apt/sources.list /tmp
46
   $dry sed -i '/^# deb-src /s/^# //' /etc/apt/sources.list
47
   $dry apt-get update
48
fi
49

    
50
# install ncurses source code for this version
51
if [ -z "$is_rhel" ]; then
52
   $dry apt-get -q -y source ncurses
53
else
54
   $dry yum install ncurses-devel
55
fi
56

    
57
if [[ "$handle_apt" == "true" ]]; then
58
   $dry mv /tmp/sources.list /etc/apt/
59
   $dry apt-get update
60
fi
61

    
62
# check if sources obtained
63
if [ $? -ne 0 ] && [ -z "$dry" ]
64
then
65
   echo "+------------------------------------------------------------------------------+"
66
   echo "|                  Unable to get NCURSES source tree!                          |"
67
   echo "|       Check the available packages list in /etc/apt/sources.list.            |"
68
   echo "+------------------------------------------------------------------------------+"
69
   exit
70
fi
71

    
72
# patching the source files
73
$dry pushd ncurses-6.* > /dev/null
74
if [[ "$patch_ncurses" == "true" ]]; then
75
   $dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_curses_h_in_v6.1_20200708.patch
76
   $dry wget https://proj.goldencode.com/downloads/ncurses/ncurses_lib_getch_c_v6.1_20200708.patch
77
   $dry patch include/curses.h.in ncurses_curses_h_in_v6.1_20200708.patch
78
   $dry patch ncurses/base/lib_getch.c ncurses_lib_getch_c_v6.1_20200708.patch
79
fi
80

    
81
# configure and build object libraries
82
if [[ "$patch_ncurses" == "true" ]]; then
83
   $dry ./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6
84
else
85
   $dry ./configure --with-termlib CFLAGS='-fPIC -O2' --with-abi-version=6 --with-pthread --enable-pthreads-eintr
86
fi
87
$dry make
88

    
89
# make valid reference to updated ncurses.h
90
$dry cd include
91
$dry ln -s curses.h ncurses.h
92
$dry cd ..
93

    
94
if [[ "$patch_ncurses" == "true" ]]; then
95
   echo "+------------------------------------------------------------------------------+"
96
   echo "|                 Patched NCURSES libraries update completed.                  |"
97
else
98
   echo "+------------------------------------------------------------------------------+"
99
   echo "|               Thread-safe NCURSES libraries build completed.                 |"
100
fi
101

    
102
# Position minimal files if path is specified
103
if [[ ! -z "$target_dir" ]]; then
104
   $dry mkdir -p $target_dir
105
   $dry cp -r include $target_dir
106
   $dry cp -r lib $target_dir
107
   $dry cd $target_dir
108
fi
109

    
110
# setting up system variable if not yet defined to use custom libraries location
111
if [ -z "$NCURSES_FWD_STATIC" ]
112
then
113
   if [[ "$system" == "true" ]]; then
114
      rc_filename='/etc/profile.d/10-ncurses.sh'
115
      msg="|         The environment has been changed! Reboot to activate changes         |"
116
      update="export NCURSES_FWD_STATIC=\"$PWD\""
117
      rm $rc_filename
118
   else
119
      rc_filename=$HOME'/.bashrc'
120
      msg="|   The .bashrc has been changed! Please open new console to activate setup!   |"
121
      update="export NCURSES_FWD_STATIC=$PWD"
122
   fi
123
   if [[ -z "$dry" ]]; then
124
      echo '' >> $rc_filename
125

    
126
      if [[ "$patch_ncurses" == "true" ]]; then
127
         echo '# set up patched NCURSES root location' >> $rc_filename
128
      else
129
         echo '# set up thread-safe NCURSES root location' >> $rc_filename
130
      fi
131
      echo $update >> $rc_filename
132
      if [[ "$patch_ncurses" != "true" ]]; then
133
         echo 'export NCURSES_FWD_PTHREADS=true' >> $rc_filename
134
      fi
135
   fi
136
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
137
   echo "$msg"
138
   echo "|   ---------------------------   WARNING ! --------------------------------   |"
139
else
140
   if [ -z "$NCURSES_FWD_PTHREADS" ]
141
   then
142
      if [[ "$patch_ncurses" != "true" ]]; then
143
         if [[ "$system" == "true" ]]; then
144
            rc_filename='/etc/profile.d/10-ncurses.sh'
145
            msg="|         The environment has been changed! Reboot to activate changes         |"
146
            rm $rc_filename
147
         else
148
            rc_filename=$HOME'/.bashrc'
149
            msg="|   The .bashrc has been changed! Please open new console to activate setup!   |"
150
         fi
151
         if [[ -z "$dry" ]]; then
152
            echo 'export NCURSES_FWD_PTHREADS=true' >> $rc_filename
153
         fi
154
         echo "|   ---------------------------   WARNING ! --------------------------------   |"
155
         echo "$msg"
156
         echo "|   ---------------------------   WARNING ! --------------------------------   |"
157
      fi
158
   fi
159
fi
160

    
161
echo "|              Please do FULL FWD rebuild to activate the changes.             |"
162
echo "+------------------------------------------------------------------------------+"
163

    
164
# back to the starting directory
165
$dry popd > /dev/null