This file is indexed.

/usr/bin/ldbash is in libbash 0.9.11-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/bin/bash

###########################################################################
# Copyright (c) 2004-2009 Hai Zaar and Gil Ran                            #
#                                                                         #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of version 3 of the GNU General Public License as       #
# published by the Free Software Foundation.                              #
#                                                                         #
###########################################################################


#
# Dynamic loader for bash libraries
# NOTE: The script can't load libraries if their file name starts with '-'.
#       (If someone gives a file a name that starts with a '-' he deserves it!)
#

# The prefix of internal functions
INTERNAL_PREFIX='__${Lib}_'

prefix=/usr
exec_prefix=${prefix}

# The directory that contains the bash libraries
LD_BASH_PATH=${exec_prefix}/lib/bash


# LD_BASH_CACHE - The ldbash cache file
LD_BASH_CACHE=/etc/ldbash.cache

# EXPORTS and REQUIREMENTS definitions
source $LD_BASH_CACHE || exit 1

# We want to use getopt_long...
source $LD_BASH_PATH/getopts.sh
source $LD_BASH_PATH/hashstash.sh

#############################################################
###################       FUNCTIONS      ####################
#############################################################

# Prints the script's usage.
usage()
{
	echo "usage: ldbash"
	echo "               [-h|--help]"
	echo "               [-l|--list]"
	echo "               [-L|--load <lib,[lib[...]]>]"
	echo "               [-U|--unload <lib,[lib[...]]>]"
	echo "               [-e|--externlist <lib,[lib[...]]>]"
	echo "               [--externlist-all]"
	echo "               [-i|--internlist <lib,[lib[...]]>]"
	echo "               [--internlist-all]"
	echo "				 [-v|--version]"
	exit
}

# Lists all the available libraries.
list()
{
	retval=`(cd $LD_BASH_PATH ; ls *.sh | sed "s/\.sh$//g")`
}

#
# $retval findLibExternalFuncs <lib[,lib[...]]>
#
#	Finds all exported functions of given libraries.
#
#	Parameters: 
#		lib		- A name of a bash library
#	Return value:
#		A list of external (exported) functions of the given library
findLibExternalFuncs()
{
	retval=""

	# for each lib, find it's external functions
	local Lib=
	for Lib in ${*//,/ } ; do
		eval LibInternalPrefix=$INTERNAL_PREFIX
		
		# Add to the retval the name of the lib
		retval="$retval $Lib: "

		# Step by step:
		# Grep functions declarations from the code of given library. 
		# Take just the declaration (looks like: "funcName<white_space>()")
		# Remove the '()'
		# Now throw away the lines that define internal functions
		retval="$retval"`grep ^"[ |\t]*[a-zA-Z_][0-9a-zA-Z_]*[ |\t]*()" ${LD_BASH_PATH}/${Lib}.sh 2> /dev/null | \
						 cut -d : -f 2 | \
						 sed -e "s/()//g" -e "s/[ |\t]//g" 2> /dev/null | \
						 grep -v ^"[ |\t]*${LibInternalPrefix}" 2> /dev/null`
	done
}

#
# $retval findLibInternalFuncs <lib[,lib[...]]>
#
#	Finds all internal (not-to-be-exported) functions of given libraries.
#
#	Parameters: 
#		lib		- A name of a bash library
# 	Return value:
#		A list of internal functions of the given library
findLibInternalFuncs()
{
	retval=""
	
	# for each lib, find it's internal functions
	local Lib=
	for Lib in ${*//,/ } ; do
		eval LibInternalPrefix=$INTERNAL_PREFIX
		
		# Add to the retval the name of the lib
		retval="$retval $Lib: "

		# Step by step:
		# Grep internal functions declarations from the code of given library. Step by step:
		# Take just the declaration (looks like: "funcName<whitespace>()")
		# Remove the '()'
		retval="$retval"`grep ^"[ |\t]*$LibInternalPrefix[0-9a-zA-Z_]*[ |\t]*()" ${LD_BASH_PATH}/${Lib}.sh 2> /dev/null | \
						 cut -d : -f 2 2> /dev/null | 
						 sed -e "s/()//g" -e "s/[ |\t]//g" 2> /dev/null`
	done
}

#
# ldbash_load load <lib[,lib[...]]>
#
#	See description of return value.
#	
#	Parameters:
#		lib		- A list of libraries to load.
#	Return value:
#		A string that contains a command that will load the libraries.
#		This string should be evaled (eval $retval).
load()
{
	local LoadString=""
	
	# LibsRequests is a list of requests for libraries to be loaded. Once a 
	# request is dealt with, it is removed from the list, and the appropriate 
	# library is added to LibsAdded (if it's not already there).
	# LibsAdded is the list which aggregates the names of libraries to 
	# eventually load.
	local LibsRequests="${*//,/ } "
	local LibsAdded=
	
	# Create the list of libs to load (with dependencies)
	local Lib=
	while [[ $LibsRequests ]]; do
		
		# Handling the request first on the list (and removing it)
		Lib=${LibsRequests%% *}
		LibsRequests=${LibsRequests#* }
		
		# Requests for libraries which have already been encountered are ignored.
		[[ $LibsAdded == $Lib\ * ]] || [[ $LibsAdded == *\ $Lib\ * ]] && continue
		LibsAdded="$LibsAdded$Lib "
		
		# Get the names of the needed functions
		eval LibRequire='${'${Lib}_REQUIRE'}'
			
		# Find out, for each function, who exports it
		local Func=
		for Func in $LibRequire ; do
			# This finds the name of the library that defines the function
			# It does it by greping "EXPORT" string and the function name from the cache file
			# and then taking only the lib name from the line.
			# NeededLib is not local so it can be treeted as an array.
			NeededLib=(`grep EXPORT $LD_BASH_CACHE | \
						grep " $Func " | \
						awk -F= '{print $1}' | \
						sed -e 's/_EXPORT//g'`)
			
			LibsRequests="$LibsRequests$NeededLib "
			unset NeededLib
		done
	done

	# Build the command that loads the libs
	for Lib in $LibsAdded ; do
		LoadString="${LoadString} source ${LD_BASH_PATH}/${Lib}.sh;"
	done

	retval=$LoadString
}

#
# ldbash_unload unload <lib,[lib[...]]>
#
#	See description of return value.
#	
#	Parameters:
#		lib		- A list of libraries to unload.
#	Return value
#		A string that contains a command that will unloaded the libraries.
#		This string should be evaled (\e eval \e retval)
unload()
{
	local UnloadString=""
	
	local Lib=
	
	# Build the command that unloads the libs
	#
	# we've sources cache file that defines ${lib}_EXPORT
	# ${lib}_EXPORT contains list of all functions that we've defined in load()
	# by running eval 'unset ${lib}_EXPORT' we'll unset them all
	#
	for Lib in ${*//,/ } ; do
		eval LibExports='${'${Lib}_EXPORT'}'
		UnloadString="${UnloadString} unset $LibExports;"
	done

	retval=$UnloadString
}

##############################################################
##################          MAIN          ####################
##############################################################

if ! [[ $* ]] ; then
	usage
fi

# parsing parameters
# 
# -e will set Extern to its argument
# BUT
# --externlist-all will set Extern to '-all'
#
getopt_long '-l|--list->List
			 -L|--load->Load:
			 -U|--unload->Unload:
			 -e|--externlist->Extern:
			 -i|--internlist->Intern:
			 -v|--version->Version
			 -h|--help->Help' "$@"
eval "$retval"

# Do we need to show usage message?
[[ $Help ]] && usage

if [[ $Version ]] ; then
	echo "libbash 0.9.11"
	echo "Writen by Gil Ran and Hai Zaar"
	exit 0
fi

# Do we need to show a list of libraries?
if [[ $List ]] ; then
	echo "Libraries:"
	list
	for LibName in $retval ; do
		echo "	$LibName"
	done
fi

# Do we need to show a list of external functions?
if [[ $Extern ]] ; then
	# Check if the parameter is -all
	if [[ "$Extern" = '-all' ]] ; then
		# Run the function for all the available libraries
		list
		findLibExternalFuncs "${retval// /,}"
	else
		findLibExternalFuncs "$Extern"
	fi
	
	echo "External:"
	for FuncName in $retval ; do
		# Check if it is the library's name
		if [[ "$FuncName" = *: ]] ; then
			echo "	$FuncName"
		else # It must be a function name
			echo "		$FuncName"
		fi
	done
fi

# Do we need to show a list of internal functions?
if [[ $Intern ]] ; then
	# Check if the parameter is -all
	if [[ "$Intern" = '-all' ]] ; then
		# Run the function for all the available libraries
		list
		findLibInternalFuncs "${retval// /,}"
	else
		findLibInternalFuncs "$Intern"
	fi

	echo "Internal:"
	for FuncName in $retval ; do
		# Check if it is the library's name
		if [[ "$FuncName" = *: ]] ; then
			echo "	$FuncName"
		else # It must be a function name
			echo "		$FuncName"
		fi
	done
fi

# If any on the listing options were set, do not preform load/unload
[[ $List ]] || [[ $Extern ]] || [[ $Intern ]] && exit 0

# Do we need to load any libraries?
if [[ $Load ]] ; then
	load "$Load"
	echo $retval
fi

# Do we need to unload any libraries?
if [[ $Unload ]] ; then
	unload "$Unload"
	echo $retval
fi