summaryrefslogtreecommitdiffstats
path: root/examples/simple_menu/simple_menu.sh
blob: 2092478dce9696ed12c6076415c7c0fd6dd61a96 (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
#/bin/bash

# Variables
# """""""""
PROG=${0#*/}

typeset -a MENU_STACK # A stack of MENUS to store the previously visited menus

# Array of menu characteristics for caching purpose
# '''''''''''''''''''''''''''''''''''''''''''''''''
typeset -A MENU_ARRAY
typeset -A COL_ARRAY
typeset -A CENTERING_ARRAY
typeset -A TITLE_ARRAY
typeset -A ERASE_ARRAY

SEL= # The selection

# ============================ #
# Usage function, always fails #
# ============================ #
function usage
{
  echo "Usage: $PROG menu_file[.mnu] user_program," >&2
  echo "       read the README for an example"      >&2
  exit 1
}

# ==================== #
# Fatal error function #
# ==================== #
function error
{
  echo $* >&2
  exit 1
}

# The script expects exactly one argument (the filename of the root menu).
# """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
(( $# != 2 )) && usage

USER_PROGRAM=$2

# ======================================================================= #
# Parse a level in the menu hierarchy and call process with the selection #
# (possibly empty).                                                       #
# ======================================================================= #
function process_menu
{
  TITLE="[ENTER: select, q: abort]"$'\n' # Untitled by default
  CENTERING= # Is the menu centered in the screen ?
  ERASE=  # Destroy the selection window after use

  MENU_FILE=$1
  MENU= # Make sure the working area is empty

  # If the menu has already been seen, read its characteristics from the cache
  # else construct them.
  # """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  if [[ -z ${MENU_ARRAY[$MENU_FILE]} ]]; then
    COL=1

    # Parse the directives embedded in the menu file
    # """"""""""""""""""""""""""""""""""""""""""""""
    MENU_DIRECIVES=$(grep '^\.' $MENU_FILE)

    while read DIRECTIVE VALUE; do
      case $DIRECTIVE in
        .columns) # Number of columns of the menu
           COL=$VALUE
           (( COL < 1 || COL > 15 )) && error "$DIRECTIVE, bad value"
           ;;

        .centered) # Is the menu centered?
           [[ $VALUE == yes ]] && CENTERING="-M"
           ;;

        .eraseafter) # Will the space used by the menu be reclaimed?
           [[ $VALUE == yes ]] && ERASE="-d"
           ;;

        .title) # The menu title
           TITLE="$VALUE"$'\n'$TITLE
           ;;

        *)
           error "bad directive $DIRECTIVE"
           ;;
      esac
    done <<< "$MENU_DIRECIVES"

    # Build the menu entries in the working area
    # """"""""""""""""""""""""""""""""""""""""""
    MENU_LINES=$(grep -v -e '^\.' -e '^#' -e '^[ \t]*$' $MENU_FILE)

    # The special tag "---" creates an empty entry (a hole in a column)
    # The special tag "===" create an empty line
    # The special tag "EXIT" permit to exit the menu
    # '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    while read TAG VALUE; do
      ITEMS_TO_ADD=1 # By default, only one iteration of the tag is taken
                     # into account

      (( ${#TAG} > 10 )) && error "Menu tag too long (max 10 characters)."

      [[ $TAG == --- ]] && VALUE=@@@
      [[ $TAG == === ]] && VALUE=@@@ && ITEMS_TO_ADD=COL

      [[ -z $VALUE ]] && error "Empty menu entry for $TAG"
      [[ $TAG == EXIT ]] && TAG="@EXIT@xxxx00"
      [[ $TAG == "<"* ]] && TAG="<xxxxxxxxx00"

      # Protect quotes in VALUE
      # """""""""""""""""""""""
      FINAL_VALUE=$(echo "$VALUE" | sed -e 's/"/\\"/' -e "s/'/\\\\'/")

      while (( ITEMS_TO_ADD-- > 0 )); do
        MENU+="'$TAG $FINAL_VALUE'"$'\n'
      done
    done <<< "$MENU_LINES"

    # Feed the cache
    # """"""""""""""
    MENU_ARRAY[$MENU_FILE]="$MENU"
    COL_ARRAY[$MENU_FILE]=$COL
    CENTERING_ARRAY[$MENU_FILE]=$CENTERING
    TITLE_ARRAY[$MENU_FILE]=$TITLE
    ERASE_ARRAY[$MENU_FILE]=$ERASE
  else
    # Read from the cache
    # """""""""""""""""""
    MENU="${MENU_ARRAY[$MENU_FILE]}"
    COL=${COL_ARRAY[$MENU_FILE]}
    CENTERING=${CENTERING_ARRAY[$MENU_FILE]}
    TITLE=${TITLE_ARRAY[$MENU_FILE]}
    ERASE=${ERASE_ARRAY[$MENU_FILE]}
  fi

  # Display the menu and get the selection
  # """"""""""""""""""""""""""""""""""""""
  SEL=$(echo "$MENU" | ../../smenu          \
                         $CENTERING         \
                         $ERASE             \
                         -N                 \
                         -F -D o:10 i:0 n:2 \
                         -n                 \
                         -m "$TITLE"        \
                         -t $COL            \
                         -S'/@@@/ /'        \
                         -S'/^[^ ]+ //v'    \
                         -e @@@)
  SEL=${SEL%% *}
}

# Check for the presence of ../../smenu
# """""""""""""""""""""""""""""""""""""
[[ -x ../../smenu ]] || error "smenu is not there, please build it."

# Initialize the menu stack with the argument
# """""""""""""""""""""""""""""""""""""""""""
MENU_STACK+=(${1%.mnu}.mnu)

# And process the main menu
# """""""""""""""""""""""""
process_menu ${1%.mnu}.mnu

# According to the selection, navigate in the submenus or return
# the tag associated with the selected menu entry.
# """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
while true; do
  if [[ $SEL == "<"* ]]; then
    # Back to the previous menu
    # '''''''''''''''''''''''''
    if (( ${#MENU_STACK[*]} == 1 )); then
      process_menu ${MENU_STACK[-1]}
    else
      # Unstack the newly found submenu
      # '''''''''''''''''''''''''''''''
      unset MENU_STACK[-1]

      # And generate the previous menu
      # ''''''''''''''''''''''''''''''
      process_menu ${MENU_STACK[-1]}
    fi

  elif [[ $SEL == ">"* ]]; then
    # Enter the selected submenu
    # ''''''''''''''''''''''''''
    SMENU_FILE=${SEL#>}
    SMENU_FILE=${SMENU_FILE%.mnu}.mnu
    [[ -f $SMENU_FILE ]] || error "The file $SMENU_FILE was not found/readable."

    # Stack the newly found submenu
    # '''''''''''''''''''''''''''''
    MENU_STACK+=($SMENU_FILE)

    # And generate the submenu
    # ''''''''''''''''''''''''
    process_menu $SMENU_FILE
  else
    # An empty selection means than q or ^C has been hit
    # ''''''''''''''''''''''''''''''''''''''''''''''''''
    [[ -z $SEL ]] && exit 0

    # Output the selected menu tag or exit the menu without outputting
    # anything.
    # ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    [[ $SEL == @EXIT@* ]] && exit 0

    # Lauch the user action which has the responsibility to act according
    # to the tag passed as argument.
    # '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    $USER_PROGRAM $SEL

    # And re-generate the current menu
    # ''''''''''''''''''''''''''''''''
    process_menu ${MENU_STACK[-1]}
  fi
done