File: parse-menu-sh

package info (click to toggle)
dosemu 1.2.1-3
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 10,252 kB
  • ctags: 22,395
  • sloc: ansic: 138,996; sh: 4,211; asm: 3,599; perl: 1,427; makefile: 981; tcl: 602; awk: 404; yacc: 300; lex: 157
file content (388 lines) | stat: -rw-r--r-- 10,192 bytes parent folder | download | duplicates (9)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#
# BASH specific functions
#

function write_start () {
  print "start() { handle_" start_name "; }"
}

function write_menu_function_header () {

  if ( menu_class == "template" ) {
    print "handle_" menu_name "_template () {"

  } else {
    print "handle_" menu_name " () {"

  }

  print "local FINISHED=0 CURRENT RESULT=0 DATA NUM_ITEMS HEIGHT"

  if ( menu_class == "template" ) {
    print "local TEMPLATE=$1"

  }
}

function write_menu_boolean_init ( i ) {
  print "local ITEM" i "=off"

  print "get_value ITEM" i " " var_name( menu_var[i])

  return i
}

function write_menu_input_init ( i ) {
  print "local ITEM" i 

  print "get_value ITEM" i " " var_name( menu_var[i])

  return i
}

function write_menu_list_init ( i ) {
  # This next step is probably a bit naughty, but it looks like the
  # best solution ....

  print "local TEMP_" menu_var[i] " LIST_" menu_var[i]

  list_item = i
  line = ""

  print "get_value TEMP_" menu_var[i] " " var_name( menu_var[i])

  list = menu_var[i]

  while (menu_type[i] == "list" && list == menu_var[i]) {
    print "local ITEM" i "=off"
    line = line menu_data[i] ") ITEM" i "=on\n"

    line = line " LIST_" menu_var[i] "=" i " ;;\n"

    i++
  }

  # The while loop overshoots the mark.
  i --

  print "case $TEMP_" menu_var[i] " in "
  print line "esac"

  return i
}

function write_menu_submenu_init ( i ) {
  return i
}

function write_menu_text_init ( i ) {
  return i
}

function write_menu_menu_header () {
  print "if [ \"@$DIALOG_SUPPORTS_CURRENT\" = \"@true\" ]; then"
  print "CURRENT=0"
  print "fi"

  print "while [ $FINISHED -ne 1 ]"
  print "do"
  print "MENU_STRING=\"\""
  print "NUM_ITEMS=0"
}

function write_menu_entry_submenu ( i ) {
  print "MENU_STRING=\"$MENU_STRING " i " \'" escape_string(menu_text[i]) " ---> \'\""
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  return i  
}

function write_menu_entry_boolean ( i ) {
  print "if [ $ITEM" i " = on ]"
  print "then"
  print "MENU_STRING=\"$MENU_STRING " i " \'[X] " escape_string(menu_text[i]) "\'\""
  print "else"
  print "MENU_STRING=\"$MENU_STRING " i " \'[ ] " escape_string(menu_text[i]) "\'\""
  print "fi"
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  return i
}

function write_menu_entry_input ( i ) {
  print "MENU_STRING=\"$MENU_STRING " i " \'" escape_string(menu_text[i]) " \($ITEM" i "\)\'\""
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  return i
}

function write_menu_entry_list ( i ) {
  print "if [ $ITEM" i " = on ]"
  print "then"
  print "MENU_STRING=\"$MENU_STRING " i " \'(X) " escape_string(menu_text[i]) "\'\""
  print "else"
  print "MENU_STRING=\"$MENU_STRING " i " \'( ) " escape_string(menu_text[i]) "\'\""
  print "fi"
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  return i
}

function write_menu_entry_text ( i ) {
  print "MENU_STRING=\"$MENU_STRING " i " \'" escape_string(menu_text[i]) "\'\""
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  return i
}

function write_menu_menu () {
  if ( menu_class == "template" ) {
    print "MENU_STRING=\"$MENU_STRING -1 \'Remove " escape_string(template_text) "\'\""
    print "NUM_ITEMS=$[NUM_ITEMS + 1]"
  }

  # Rule of thumb - Allow 8 lines for the decorations on the menu ... the
  # screen size has already been reduced by the size detection code.
  print "if [ $[$NUM_ITEMS + 8] -gt $ROWS ]"
  print "then"
  print "HEIGHT=$ROWS"
  print "NUM_ITEMS=$[$HEIGHT - 8]"
  print "else"
  print "HEIGHT=$[$NUM_ITEMS + 8]"
  print "fi"

  print "eval $DIALOG --backtitle \'\"" main_title "\"\' --title \'\"" escape_string(menu_title) "\"\' --menu \'\"" escape_string(menu_description) "\"\' $HEIGHT 60 $NUM_ITEMS $CURRENT $MENU_STRING 2> $TEMP"
}

function write_menu_action () {
  print "RESULT=$?"

  print "if [ $RESULT -eq 0 -o $RESULT -eq 3 -o $RESULT -eq 6 ]; then "
  print "RESULT=`cat $TEMP`"

  print "if [ \"@$DIALOG_SUPPORTS_CURRENT\" = \"@true\" ]; then"
  print "CURRENT=$RESULT"
  print "fi"

  print "case $RESULT in "
  for (i = 0; i < menu_items; i++ ) {
    print i ") "
    if ( menu_type[i] == "submenu" ) {
      print "handle_" menu_data[i] " || $DIALOG --backtitle \'Missing Menu Error\' --msgbox \'The menu for " menu_data[i] " appears to be missing.\' 8 60 "
      print ";;"

    } else if ( menu_type[i] == "boolean") {

      print "if [ $ITEM" i " = on ]"
      print "then"
      print "set_value " var_name( menu_var[i]) " \"off\" "
      print "ITEM" i "=off"
      print "else"
      print "set_value " var_name( menu_var[i]) " \"on\" "
      print "ITEM" i "=on"
      print "fi ;;"

    } else if ( menu_type[i] == "input") {
      print "if [ \"@$DIALOG_SUPPORTS_VALUE\" = \"@true\" ]; then"
      print "eval $DIALOG --backtitle \\\'" main_title "\\\' --title \\\'" menu_title " - " menu_text[i] "\\\' --inputbox \\\'Please enter the value for " menu_text[i] "\\\' 9 60 \\\"$ITEM" i "\\\" 2> $TEMP "
      print "else"
      print "eval $DIALOG --backtitle \\\'" main_title "\\\' --title \\\'" menu_title " - " menu_text[i] "\\\' --inputbox \\\'Please enter the value for " menu_text[i] "\\\($ITEM" i "\\\)\\\' 9 60 2> $TEMP "
      print "fi"
      print "RESULT=$?"

      print "if [ $RESULT -eq 0 -o $RESULT -eq 3 -o $RESULT -eq 6 ]; then "
      print "RESULT=`cat $TEMP`"
      print "set_value " var_name( menu_var[i]) " \"$RESULT\""
      print "ITEM" i "=\"$RESULT\""
      print "fi"
      print ";;"

    } else if ( menu_type[i] == "list") {
      print "set_value " var_name( menu_var[i]) " \"" menu_data[i] "\" "
      print "eval ITEM$LIST_" menu_var[i] "=off"
      print "ITEM" i "=on"
      print "LIST_" menu_var[i] "=" i
      print ";;"

    } else if ( menu_type[i] == "text") {
      print ";;"

    }
  }
  if (menu_class == "template") {
    print "-1)"
    print "set_value " var_name(template_deleted) " \"on\""
    print "FINISHED=1"
    print ";;"
  }

  print "esac" 

  print "elif [ $RESULT -eq 2 ]"
  print "then"
  print "$DIALOG --backtitle \'Missing Help\' --msgbox \'Sorry: The help system is not yet written.\' 8 60 "
  print "else"
  print "FINISHED=1"
  print "fi"
}

function write_menu_menu_footer () {
  print "done"
}

function write_menu_exit () {
  if (menu_class == "menu") {
    if ( menu_exit != "" ) {
      print menu_exit " || $DIALOG --backtitle \'Exit Function Error\' --msgbox \'The exit function " menu_exit " appears to be missing or has been aborted.\' 8 60 "

    }
  }
}


function write_menu_function_footer () {
  print "}"
}

function write_menu (  line_length, i, line, list, list_item) {
  # Write out the header
  write_menu_function_header()

  # Initialise Values
  for (i = 0; i < menu_items; i++) {
    if ( menu_type[i] == "boolean") {
      i = write_menu_boolean_init(i)

    } else if ( menu_type[i] == "input") {
      i = write_menu_input_init(i)

    } else if ( menu_type[i] == "list") {
      i = write_menu_list_init(i)

    } else if ( menu_type[i] == "submenu") {
      i = write_menu_submenu_init(i)

    } else if ( menu_type[i] == "text") {
      i = write_menu_text_init(i)

    }
  }

  # Create Menu ...
  write_menu_menu_header()

  for (i = 0; i < menu_items; i++) {

    if (menu_type[i] == "submenu") {
      i = write_menu_entry_submenu(i)

    } else if (menu_type[i] == "boolean") {
      i = write_menu_entry_boolean(i)

    } else if (menu_type[i] == "input") {
      i = write_menu_entry_input(i)

    } else if (menu_type[i] == "list") {
      i = write_menu_entry_list(i)

    } else if (menu_type[i] == "text") {
      i = write_menu_entry_text(i)

    }

  }

  # Do Menu ...
  write_menu_menu()

  # Handle Results ...
  write_menu_action()

  write_menu_menu_footer()

  write_menu_exit()

  write_menu_function_footer()
}


function var_name (  base) {
  if (menu_class == "template" ) {
    return base "_${TEMPLATE}"

  } else {
    return base

  }
}

function write_template () {
  print "handle_" menu_name " () {"
  print "local FINISHED=0 CURRENT RESULT=0 DATA NUM_ITEMS HEIGHT COUNT"
  print "local TEXT TEMPLATE_SIZE DELETED"

  print "if [ \"@$DIALOG_SUPPORTS_CURRENT\" = \"@true\" ]; then"
  print "CURRENT=0"
  print "fi"

  print "while [ $FINISHED -ne 1 ]"
  print "do"
  print "MENU_STRING=\"\""
  print "NUM_ITEMS=0"

  print "COUNT=0"
  print "get_value TEMPLATE_SIZE " template_counter
  print "while [ $COUNT -lt $TEMPLATE_SIZE ]"
  print "do"
  print "COUNT=$[$COUNT + 1]"
  print "DELETED=\"\""
  print "get_value DELETED " template_deleted "_${COUNT}"
  print "if [ \"@$DELETED\" != \"@on\" ]; then"
  print "TEXT=\"" template_text " $COUNT\""
  print "MENU_STRING=\"$MENU_STRING $COUNT \'$TEXT\' \""
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"
  print "fi"
  print "done"

  # Always offer "Add ..."
  print "MENU_STRING=\"$MENU_STRING $[$TEMPLATE_SIZE +1] \'Add " escape_string(template_text) "\' \""
  print "NUM_ITEMS=$[$NUM_ITEMS + 1]"

  # Rule of thumb - Allow 8 lines for the decorations on the menu ... the
  # screen size has already been reduced by the size detection code.
  print "if [ $[$NUM_ITEMS + 8] -gt $ROWS ]"
  print "then"
  print "HEIGHT=$ROWS"
  print "NUM_ITEMS=$[$HEIGHT - 8]"
  print "else"
  print "HEIGHT=$[$NUM_ITEMS + 8]"
  print "fi"

  print "eval \"$DIALOG --backtitle \'\"" main_title "\"\' --title \'\"" escape_string(menu_title) "\"\' --menu \'\"" escape_string(template_description) "\"\' $HEIGHT 60 $NUM_ITEMS $CURRENT $MENU_STRING \" 2>$TEMP"

  print "RESULT=$?"

  print "if [ $RESULT -eq 0 -o $RESULT -eq 3 -o $RESULT -eq 6 ]; then "
  print "RESULT=`cat $TEMP`"

  print "if [ \"@$DIALOG_SUPPORTS_CURRENT\" = \"@true\" ]; then"
  print "CURRENT=$RESULT"
  print "fi"

  print "if [ $RESULT -eq $[$TEMPLATE_SIZE + 1] ]; then "
  print "TEMPLATE_SIZE=$[$TEMPLATE_SIZE + 1]"
  print "set_value " template_counter " $TEMPLATE_SIZE"
  print "fi"  

  print "handle_" menu_name "_template $RESULT || $DIALOG --backtitle \'Missing Menu Error\' --msgbox \'The template for " menu_name " appears to be missing.\' 8 60 "

  print "elif [ $RESULT -eq 2 ]"
  print "then"
  print "$DIALOG --backtitle \'Missing Help\' --msgbox \'The help system is not yet written. Please read etc/config.dist for full details of the options.\' 8 60 "
  print "else"
  print "FINISHED=1"
  print "fi"

  print "done"
  print "}"
}