File: ia.check

package info (click to toggle)
shellia 5.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792 kB
  • sloc: sh: 7,840; makefile: 34
file content (422 lines) | stat: -rw-r--r-- 13,588 bytes parent folder | download
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/sh
# vim: set filetype=sh :
#        file: /usr/share/shellia/ia.check
#   copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007-2020)
#     license: GNU General Public License, version 3
# description: ia.check - functions to check stderr, stdout and exit-value of
#   steps. For each list of steps, handled by one ia call, the check-option -c
#   or -C can be choosen.

# ia_find_lib <lib>
# example:
#   program "/nfs/host1/usr/local/bin/program" with line "ia_source_lib /usr/share/shellia/ia" called
#   from directory "/tmp" will try "/tmp/ia", ... "/nfs/host1/usr/share/shellia/ia", ... and
#   "/usr/share/shellia/ia" in the listed order.
ia_find_lib()
{
  local i
  local p

  for i in "$@"; do
    [ -f "./$(basename "$i")" ] && /bin/echo "./$(basename "$i")" && return 0
    p="$0"
    while :; do
      [ "$p" != "$(dirname p)" ] || break
      p="$(dirname "$p")"
      [ -f "$p/$i" ] && /bin/echo "$p/$i" && return 0
    done
    [ -f "$i" ] && /bin/echo "$i" && return 0
    return 1
  done
}

if [ "${ia_prefix:=ia}" != "ia" ] || [ "${dbg_prefix:=dbg}" != "dbg" ]; then
  [ -n "$(eval "/bin/echo \${${ia_prefix}_check_already_sourced:=}")" ] && return
  eval "$(sed -e "s/ia_/${ia_prefix}_/g" -e "s/ia()/${ia_prefix}()/g" \
    -e "s/dbg/${dbg_prefix}/g" "$(ia_find_lib /usr/share/shellia/ia.check)")"
  return
fi

[ "${ia_check_already_sourced:=}" ] && return || ia_check_already_sourced=1

. "$(ia_find_lib /usr/share/shellia/ia.basic)"


ia_check_exitcode()
{
  local ia_ign_awk
  local ia_std_awk
  ia_ign_awk="$(/bin/echo "$ia_ign" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_ignore/{...}
  ia_std_awk="$(/bin/echo "$ia_std" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_stdout/{...}
  ia_awk=""
  [ -n "$ia_std_awk" ] && ia_awk="$ia_awk/$ia_std_awk/{getline;printf(\"s:%s\n\",\$0);next}" # stdout is more important that ignore
  [ -n "$ia_ign_awk" ] && ia_awk="$ia_awk/$ia_ign_awk/{getline;printf(\"i:%s\n\",\$0);next}"
  ia_awk="$ia_awk{getline;printf(\"e:%s\n\",\$0);next}"
  # to grep without color-codes, we add a line without color-codes before each line
  ia_out="$(cat "$ia_out_file" | gawk "$ia_awk")"
  #ia_dev_dbg "ia_check_exitcode: ia_out_file=<$(cat $ia_out_file)> ignore=<$ia_ign> stdout=<$ia_std> ia_awk=<$ia_awk> ia_out=<$ia_out> ia_return=<$ia_return>"
}

ia_check_c()
{
  local ia_ign_awk
  local ia_std_awk
  ia_ign_awk="$(/bin/echo "$ia_ign" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_ignore/{...}
  ia_std_awk="$(/bin/echo "$ia_std" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_stdout/{...}
  ia_awk=""
  ia_awk="$ia_awk/^\+ eval.*ia_init_details\"$/{getline;next}" # delete set +x line from $ia_init
  ia_awk="$ia_awk/^\++ /{getline;print;next}"
  [ -n "$ia_std_awk" ] && ia_awk="$ia_awk/$ia_std_awk/{getline;printf(\"s:%s\n\",\$0);next}" # stdout is more important that ignore
  [ -n "$ia_ign_awk" ] && ia_awk="$ia_awk/$ia_ign_awk/{getline;printf(\"i:%s\n\",\$0);next}"
  ia_awk="$ia_awk{getline;printf(\"e:%s\n\",\$0);next}"
  # to grep without color-codes, we add a line without color-codes before each line
  ia_out="$(cat "$ia_out_file" |
    gawk '{l=$0;gsub(/'$ia_col_regex'/,"");print;print l}' | gawk "$ia_awk")"
  #ia_dev_dbg "ia_check_c: ia_out_file=<$(cat $ia_out_file)> ignore=<$ia_ign> stdout=<$ia_std> ia_awk=<$ia_awk> ia_out=<$ia_out> ia_return=<$ia_return>"
}

ia_check_C()
{
  local ia_ign_awk
  local ia_std_awk
  ia_ign_awk="$(/bin/echo "$ia_ign" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_ignore/{...}
  ia_std_awk="$(/bin/echo "$ia_std" | sed -e 's#/#\\/#g')" # escape / in gawk /$ia_stdout/{...}
  ia_awk=""
  ia_awk="$ia_awk/^\+ eval.*ia_init_details\"$/{getline;next}" # delete set +x line from $ia_init
  ia_awk="$ia_awk/^\++ /{getline;print;next}"
  [ -n "$ia_ign_awk" ] && ia_awk="$ia_awk/$ia_ign_awk/{getline;printf(\"i:%s\n\",\$0);next}"
  ia_awk="$ia_awk{getline;printf(\"e:%s\n\",\$0);next}"
  # to grep without color-codes, we add a line without color-codes before each line
  ia_out="$(cat "$ia_stderr_file" |
    gawk '{l=$0;gsub(/'$ia_col_regex'/,"");print;print l}' | gawk "$ia_awk"
    sed "s/^/s:/" "$ia_stdout_file")"
  #ia_dev_dbg "ia_check_C: ia_stdout_file=<$(cat $ia_stdout_file)> ia_stderr_file=<$(cat $ia_stderr_file)> ignore=<$ia_ign> ia_awk=<$ia_awk> ia_out=<$ia_out> ia_return=<$ia_return>"
}

ia_dev_expr()
{
  local ia_ignore
  local ia_stdout
  while [ "$ia_input" = "d" ]; do

    if [ "$ia_use_check" = "--exitcode" ]; then
      ia_check_exitcode
    elif [ "$ia_use_check" = "-c" ]; then
      ia_check_c
    else # ia_use_check = "-C"
      ia_check_C
    fi

    cat <<END > /tmp/ia_dev_expr || ia_exiterr $? "shellia: /tmp/ia_dev_expr is not writeable"
# This file is for development of shellia scripts.
END
    cat <<END >>/tmp/ia_dev_expr
# The actual command was:
END
    /bin/echo "$ia_cmd" | sed "s/^/#  /" >> /tmp/ia_dev_expr
    cat <<END >>/tmp/ia_dev_expr
# The actual output of the command was:
END
    /bin/echo "$ia_out" | sed "s/^/#  /" >> /tmp/ia_dev_expr
  cat << END >> /tmp/ia_dev_expr
# Now try to fix lines below.
# Do not forget to fix it in your script.

END
    /bin/echo "ia_ignore \"$(/bin/echo "$ia_ign" | ia_easy_backslash)\"" >> /tmp/ia_dev_expr
    /bin/echo "ia_stdout \"$(/bin/echo "$ia_std" | ia_easy_backslash)\"" >> /tmp/ia_dev_expr
    ia_input="$(ia_ask -f "(d)evelop /tmp/ia_dev_expr (r)etry (q)uit (i)gnore (s)witch interactive" "" "q" "? " "^(q|i|s|r|d)$")"

    ia_ignore=""
    ia_stdout=""
    . /tmp/ia_dev_expr
    ia_ign="$ia_ignore"
    ia_std="$ia_stdout"
  done
}

# ia_result <cmd>
ia_result()
{
  # global ia_use_silent -- non-empty means do not allow any interactive questions in case of an error
  # global ia_use_minctrl -- non-empty means do not allow to switch to interacive mode in case of an error
  local ia_cmd
  local ia_input

  ia_cmd="$1"
  #ia_dev_dbg "ia_result: ia_cmd=<$ia_cmd>"

  if [ -z "$(/bin/echo "$ia_out" | grep -e "^e:")" ]; then
    ia_out="$(/bin/echo "$ia_out" | sed "s/^e:\(.*\)$/e:${ia_red}\1${ia_no_color}/")"
    #ia_dev_dbg "ia_result: ia_out1=<$ia_out>"
    /bin/echo "$ia_out" | grep -e "^+\+ " | ia_err
    [ -n "$ia_out" ] && ia_log "$ia_out"
    /bin/echo "$ia_out" | sed -n "s/^s://p"
    return 1
  elif [ -n "$ia_use_learn" ]; then
    ia_out="$(/bin/echo "$ia_out" | sed "s/^e:\(.*\)$/l:${ia_orange}\1${ia_no_color}/")"
    #ia_dev_dbg "ia_result: ia_out2=<$ia_out>"
    /bin/echo "$ia_out" | grep -e "^+\+ " | ia_err
    /bin/echo "$ia_out" | grep -v -e "^l:" | ia_log
    /bin/echo "--- output to LEARN from <$ia_head> running <$(/bin/echo "$ia_cmd" | head -n 1)> ---" | ia_logerr
    /bin/echo "$ia_out" | grep -e "^l:" | ia_logerr
    /bin/echo "$ia_out" | sed -n -e "s/^s://p"
    return 1
  else
    ia_out="$(/bin/echo "$ia_out" | sed "s/^e:\(.*\)$/e:${ia_red}\1${ia_no_color}/")"
    #ia_dev_dbg "ia_result: ia_out3=<$ia_out>"
    ia_question="--- output with ERROR from <$ia_head> running <$(/bin/echo "$ia_cmd" | head -n 1)> ---"
    ia_question="$ia_question
$ia_out"
    if [ -z "${ia_use_silent:=}" ] && [ -z "${ia_use_minctrl:=}" ] && [ -z "$ia_use_interactive" ]; then
      ia_question="$ia_question
$(printf "%s" "--- (q)uit (r)edo (i)gnore (s)witch interactive ---")"
    elif [ -z "$ia_use_silent" ]; then
      ia_question="$ia_question
$(printf "%s" "--- (q)uit (r)edo (i)gnore ---")"
    else
      [ $ia_return -ne 0 ] || ia_return=2 # assert nonzero exit
      ia_exiterr $ia_return "$ia_question
Exit because of ERROR and no interactive question allowed."
    fi

    # ia_ask <question> <default> <eof> <prompt> <regex>
    ia_input="$(ia_ask "$ia_question" "" "q" "? " \
      "$([ -n "$ia_use_minctrl" ] && /bin/echo "^(q|i|r)$" || /bin/echo "^(q|i|s|r|d)$")")"
    if [ "$ia_input" = "d" ]; then
      ia_dev_expr
    fi
    if [ "$ia_input" = "q" ]; then
      [ $ia_return -ne 0 ] || ia_return=1 # assert nonzero exit
      exit $ia_return
    elif [ "$ia_input" = "i" ]; then
      # we ignore the error, and use what we have as stdout
      ia_return=0
      /bin/echo "$ia_out" | sed -n "s/^s://p"
      return 1
    # switch to interactive mode
    elif [ "$ia_input" = "s" ] && ! [ "$ia_use_minctrl" ]; then
      /bin/echo
      /bin/echo "switching to interactive mode to point ($ia_answer) which failed"
      /bin/echo
      ia_answer=$((ia_answer-1))
      ia_use_interactive="-i"
      return 1
    fi
  fi
  return 0
}

# ia_check_and_results <cmd> <ignore> <stdout>
# ia_check_and_results -c|-C|--exitcode [-l] <cmd> <ignore> <stdout>
ia_check_and_result()
{
  local ia_out
  local ia_question

  local ia_cmd
  local ia_ign
  local ia_std

  local ia_use_check
  local ia_use_learn

  local ia_out_file
  local ia_stdout_file
  local ia_stderr_file

  local ia_early_exit # 1 if ia_cmd exited early, 0 else

  ia_use_check="$1"
  shift

  ia_use_learn=""
  if [ "$1" = "--learn" ]; then
    ia_use_learn="$1"
    shift
  fi

  ia_cmd="$1"
  ia_ign="$2"
  ia_std="$3"


  ia_activate_extrap

  while :
  do
    if [ "$ia_use_check" = "--exitcode" ]; then
      ia_mktemp ia_out_file
      ia_call_run
      ia_early_exit=$?
      if [ -n "BUG#929178" ]; then
        # Workaround to not crash, when running bash with this bug
        [ "$ia_return" -eq 0 ] && touch $ia_out_file || /bin/echo "exit=$ia_return" >>"$ia_out_file"
      else
        [ "$ia_return" -eq 0 ] || /bin/echo "exit=$ia_return" >>"$ia_out_file"
      fi
      ia_check_exitcode
      ia_result "$ia_cmd" || break
      ia_rmtemp "$ia_out_file"
    elif [ "$ia_use_check" = "-c" ]; then
      ia_mktemp ia_out_file
      ia_call_run "$ia_out_file"
      ia_early_exit=$?
      [ "$ia_return" -eq 0 ] || /bin/echo "exit=$ia_return" >>"$ia_out_file"
      ia_check_c
      ia_result "$ia_cmd" || break
      ia_rmtemp "$ia_out_file"
    else # ia_use_check = "-C"
      ia_mktemp ia_stdout_file
      ia_mktemp ia_stderr_file
      ia_call_run "$ia_stdout_file" "$ia_stderr_file"
      ia_early_exit=$?
      [ "$ia_return" -eq 0 ] || /bin/echo "exit=$ia_return" >>"$ia_stderr_file"
      ia_check_C
      ia_result "$ia_cmd" || break
      ia_rmtemp "$ia_stdout_file"
      ia_rmtemp "$ia_stderr_file"
    fi
  done
  return $ia_early_exit
}

ia_ignore()
{
  [ -n "${ia_ignore:=}" ] && ia_ignore="$ia_ignore|"
  ia_ignore="$ia_ignore$1"
  #ia_dev_dbg "ia_ignore: new=<$ia_ignore>"
}

ia_nocheck()
{
  if [ "${1:-}" = "-f" ]; then
    ia_ignore="-f"
    ia_stdout="-f"
  else
    ia_ignore="-1"
    ia_stdout="-1"
  fi
}

ia_is_nocheck()
{
  if [ "$ia_ignore" = "$ia_stdout" -a \( "$ia_ignore" = "-f" -o "$ia_ignore" = "-1" \) ]; then
    /bin/echo "$ia_ignore"
  fi
}

ia_stdout()
{
  #ia_dev_dbg "ia_stdout: old=<$ia_stdout>"
  [ -n "${ia_stdout:=}" ] && ia_stdout="$ia_stdout|"
  ia_stdout="$ia_stdout$1"
  #ia_dev_dbg "ia_stdout: new=<$ia_stdout>"
}

# ia_get_exit_trap <varname>
# do not call as subshell, to not loose trap
ia_get_exit_trap()
{
  local file
  file="$(mktemp)"
  {
    rm -f -- "$file"
    trap
    # get current EXIT trap, probably empty, one line or multiple lines
    eval "$1=\"$(gawk 'BEGIN{l=""}
      /trap.*EXIT$/{print;exit}
      /EXIT$/{if(l);print l;print;exit}
      /^trap/{l=$0;next}{if(l)l=sprintf("%s\n%s",l,$0)}' |
      ia_easy_backslash)\""
  } > "$file" < "$file"
}

ia_extrap()
{
  local cmd
  local out
  local i

  set +e

  if [ -n "${ia_premature_exit:=}" ]; then
    cmd="$(/bin/echo "$ia_premature_exit" | sed "\${/^#$/d;}")"
    ia_err_ignore_silent "ERROR: ia premature exit of <$cmd>"
    if [ -n "${ia_tmp_files:=}" ]; then
      out="$(cat $ia_tmp_files 2>/dev/null)"
      if [ -n "$out" ]; then
        ia_err_ignore_silent "unchecked output: <$out>"
      fi
    fi
  fi

  rm -f $ia_tmp_files
  ia_tmp_files=""

  # enable stdout and stderr for users's own EXIT trap
  if [ -n "${ia_direct_stderr_fd:-}" ]; then
    eval "exec 2>&${ia_direct_stderr_fd}"
    eval "exec 1>&${ia_direct_stderr_fd}"
  fi
}

ia_activate_extrap()
{
  local extrap
  ia_get_exit_trap extrap # fill variable extrap
  #ia_dev_dbg "ia_activate_extrap old extrap=<$extrap>"
  if [ "$extrap" = "" ]; then
    trap -- "ia_extrap;" EXIT # do not delete ;
  elif [ -z "$(/bin/echo "$extrap" | grep "'ia_extrap;")" ]; then
    extrap="$(/bin/echo "$extrap" | sed "s:':'ia_extrap;:")"
    eval "$extrap"
  else
    # next line only sets the existing trap unchanged again, but it is
    # needed by bash, while dash works without it. (testcase: test.ia_mktemp)
    eval "$extrap"
  fi
}

# ia_mktemp <varname> - returns tempfilename in variable <varname>
ia_mktemp()
{
  # global ia_mktemp_global_last
  local myp
  local ia_mktemp_local_file # make sure this name is not used as $1

  ia_shpid myp
  ia_mktemp_local_file=$(mktemp -t shellia.XXXXXXXXXX)
  if [ "${ia_mktemp_global_last:-}" = "$myp" ]; then
    # we are in the same subshell and share the same global exit variable ia_tmp_files
    if [ -n "${ia_tmp_files:-}" ]; then
      ia_tmp_files="$ia_tmp_files $ia_mktemp_local_file"
    else
     ia_tmp_files="$ia_mktemp_local_file"
    fi
  else
    # we just started a new subshell with a new global exit trap variable ia_tmp_files
    ia_tmp_files="$ia_mktemp_local_file"
  fi
  ia_mktemp_global_last=$myp

  eval "$1=$ia_mktemp_local_file"

  ia_activate_extrap
}

# ia_rmtemp <tempfilename>
ia_rmtemp()
{
  local extrap
  rm -f $1
  ia_tmp_files="$(/bin/echo "$ia_tmp_files" | sed "s: $1\>::")"
}

# ia_shpid <varname>
# this function provides something like the value of BASHPID in bash
# this function normally should not be executed in a subshell
ia_shpid()
{
  eval "$1=\"$(exec sh -c "/bin/echo \$PPID")\""
}