Skip to content

Commit b1163de

Browse files
author
Damian Rouson
committed
Adding bash stack utility only modified to add license.
Signed-off-by: Damian Rouson <[email protected]>
1 parent dcf456b commit b1163de

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

install_prerequisites/stack.sh

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# A stack, using bash arrays.
2+
# ---------------------------------------------------------------------------
3+
# This software is released under a BSD license, adapted from
4+
# <http://opensource.org/licenses/bsd-license.php>
5+
#
6+
# Copyright &copy; 1989-2012 Brian M. Clapper.
7+
# All rights reserved.
8+
#
9+
# Redistribution and use in source and binary forms, with or without
10+
# modification, are permitted provided that the following conditions are met:
11+
#
12+
# * Redistributions of source code must retain the above copyright notice,
13+
# this list of conditions and the following disclaimer.
14+
#
15+
# * Redistributions in binary form must reproduce the above copyright notice,
16+
# this list of conditions and the following disclaimer in the documentation
17+
# and/or other materials provided with the distribution.
18+
#
19+
# * Neither the name "clapper.org" nor the names of its contributors may be
20+
# used to endorse or promote products derived from this software without
21+
# specific prior written permission.
22+
#
23+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33+
# POSSIBILITY OF SUCH DAMAGE.
34+
#
35+
36+
# Create a new stack.
37+
#
38+
# Usage: stack_new name
39+
#
40+
# Example: stack_new x
41+
function stack_new
42+
{
43+
: ${1?'Missing stack name'}
44+
if stack_exists $1
45+
then
46+
echo "Stack already exists -- $1" >&2
47+
return 1
48+
fi
49+
50+
eval "declare -ag _stack_$1"
51+
eval "declare -ig _stack_$1_i"
52+
eval "let _stack_$1_i=0"
53+
return 0
54+
}
55+
56+
# Destroy a stack
57+
#
58+
# Usage: stack_destroy name
59+
function stack_destroy
60+
{
61+
: ${1?'Missing stack name'}
62+
eval "unset _stack_$1 _stack_$1_i"
63+
return 0
64+
}
65+
66+
# Push one or more items onto a stack.
67+
#
68+
# Usage: stack_push stack item ...
69+
function stack_push
70+
{
71+
: ${1?'Missing stack name'}
72+
: ${2?'Missing item(s) to push'}
73+
74+
if no_such_stack $1
75+
then
76+
echo "No such stack -- $1" >&2
77+
return 1
78+
fi
79+
80+
stack=$1
81+
shift 1
82+
83+
while (( $# > 0 ))
84+
do
85+
eval '_i=$'"_stack_${stack}_i"
86+
eval "_stack_${stack}[$_i]='$1'"
87+
eval "let _stack_${stack}_i+=1"
88+
shift 1
89+
done
90+
91+
unset _i
92+
return 0
93+
}
94+
95+
# Print a stack to stdout.
96+
#
97+
# Usage: stack_print name
98+
function stack_print
99+
{
100+
: ${1?'Missing stack name'}
101+
102+
if no_such_stack $1
103+
then
104+
echo "No such stack -- $1" >&2
105+
return 1
106+
fi
107+
108+
tmp=""
109+
eval 'let _i=$'_stack_$1_i
110+
while (( $_i > 0 ))
111+
do
112+
let _i=${_i}-1
113+
eval 'e=$'"{_stack_$1[$_i]}"
114+
tmp="$tmp $e"
115+
done
116+
echo "(" $tmp ")"
117+
}
118+
119+
# Get the size of a stack
120+
#
121+
# Usage: stack_size name var
122+
#
123+
# Example:
124+
# stack_size mystack n
125+
# echo "Size is $n"
126+
function stack_size
127+
{
128+
: ${1?'Missing stack name'}
129+
: ${2?'Missing name of variable for stack size result'}
130+
if no_such_stack $1
131+
then
132+
echo "No such stack -- $1" >&2
133+
return 1
134+
fi
135+
eval "$2"='$'"{#_stack_$1[*]}"
136+
}
137+
138+
# Pop the top element from the stack.
139+
#
140+
# Usage: stack_pop name var
141+
#
142+
# Example:
143+
# stack_pop mystack top
144+
# echo "Got $top"
145+
function stack_pop
146+
{
147+
: ${1?'Missing stack name'}
148+
: ${2?'Missing name of variable for popped result'}
149+
150+
eval 'let _i=$'"_stack_$1_i"
151+
if no_such_stack $1
152+
then
153+
echo "No such stack -- $1" >&2
154+
return 1
155+
fi
156+
157+
if [[ "$_i" -eq 0 ]]
158+
then
159+
echo "Empty stack -- $1" >&2
160+
return 1
161+
fi
162+
163+
let _i-=1
164+
eval "$2"='$'"{_stack_$1[$_i]}"
165+
eval "unset _stack_$1[$_i]"
166+
eval "_stack_$1_i=$_i"
167+
unset _i
168+
return 0
169+
}
170+
171+
function no_such_stack
172+
{
173+
: ${1?'Missing stack name'}
174+
stack_exists $1
175+
ret=$?
176+
declare -i x
177+
let x="1-$ret"
178+
return $x
179+
}
180+
181+
function stack_exists
182+
{
183+
: ${1?'Missing stack name'}
184+
185+
eval '_i=$'"_stack_$1_i"
186+
if [[ -z "$_i" ]]
187+
then
188+
return 1
189+
else
190+
return 0
191+
fi
192+
}

0 commit comments

Comments
 (0)