summaryrefslogtreecommitdiff
path: root/build-0.3/dialog.bash
blob: b6f1ed4ba6ba6517055e71ed1ea9a8a5155678f6 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# file      : build/dialog.bash
# copyright : Copyright (c) 2004-2012 Code Synthesis Tools CC
# license   : GNU GPL v2; see accompanying LICENSE file

# bld_root  - build root
# MAKE      - GNU make executable
#

#@@ can't I do exec 1>&2 or something?
#
function echo_error ()
{
  echo "$*" 1>&2
}

echo=echo_error

function abspath_old ()
{
  local r=`eval echo $1` # for tilde-expansion

  if [ "`echo $r | sed -e 's%^/.*$%%'`" ]; then

    r=`pwd`/$r
  fi

  # remove trailing `/'
  #
  r=`echo $r | sed -e 's%^\(.\+\)/$%\1%'`

  echo $r
}

# Get rid of jobserver info. See GNU make bug #12229.
#
MAKEFLAGS=`echo $MAKEFLAGS | sed -e 's/--jobserver-fds=[^ ]* *//g'`
MAKEFLAGS=`echo $MAKEFLAGS | sed -e 's/-j *//g'`


function abspath ()
{
  local r=`eval echo $1` # for tilde-expansion

  $MAKE --no-print-directory -f $bld_root/abspath.make $r
}

# $1  Default answer ('y' or 'n'). [optional]
#
function read_y_n ()
{
  local r

  while [ -z "$r" ]; do

    read -p "[$1]: " r

    if [ -z "$r" ]; then
      r=$1
    fi

    case "$r" in
      y) ;;
      n) ;;
      *) r= ;;
    esac
  done

  echo $r
}

# $1  Default answer. [optional]
#
function read_value ()
{
  local r

  read -e -p "[$1]: " r

  if [ -z "$r" ]; then
    r=$1
  fi

  echo $r
}

# $1  Space-separated list of options.
# $2  Default answer from the list of options. [optional]
#
function read_option ()
{
  local d o r i

  if [ "$2" ]; then

    d=1

    for o in $1; do

      if [ "$o" = "$2" ]; then break; fi

      d=$(($d + 1))
    done
  fi

  while [ -z "$r" ]; do

    read -p "[$d]: " r

    if [ -z "$r" ]; then

      r=$d
    fi

    i=1

    for o in $1; do

      if [ "$i" = "$r" ]; then

	echo $o
	return 0
      fi

      i=$(($i + 1))
    done

    # User must have entered some junk so let's ask her again.
    #
    r=
  done
}


# $1  Default answer. [optional]
#
# --command   - path to be read is a command (implies --exist)
#
# --directory - path to be read is a directory
#
# --exist     - path to be read should exist
#
#
function read_path ()
{
  local command=0
  local directory=0
  local exist=0

  # Parse options
  #
  while [ "$1" ]; do
    case $1 in

      --command)

	command=1
	exist=1
	shift
	;;

      --directory)

	directory=1
	shift
	;;

      --exist)

	exist=1
	shift
	;;

      *)

	break
	;;

    esac
  done

  local r tmp

  while [ -z "$r" ]; do

    read -e -p "[$1]: " r

    if [ -z "$r" ]; then

      r=$1
    fi

    if [ -z "$r" ]; then

      continue
    fi


    if [ $command == 1 ]; then

      # Do tilde expansion.
      #
      r=`eval echo $r`

      # Extract first word.
      #
      local cmd=`echo $r | cut -d " " -f 1`

      type -p $cmd 1>/dev/null 2>&1

      if [ $? != 0 ]; then

	$echo "$r: command not found"
	r=

      fi

      continue
    fi

    # abspath & checks
    #
    r=`abspath $r`

    if [ $exist == 1 ]; then

      if [ $directory == 1 ]; then

	if [ ! -d "$r" ]; then

	  $echo "$r: no such directory"
	  r=
	  continue
	fi

      else # something else

	if [ ! -e "$r" ]; then

	  $echo "$r: no such file or directory"
	  r=
	  continue
	fi
      fi
    fi
  done

  echo $r
}