File: functions-common.sh

package info (click to toggle)
mousepad 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,560 kB
  • sloc: ansic: 17,039; xml: 661; sh: 535; makefile: 6
file content (185 lines) | stat: -rw-r--r-- 3,673 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
#!/bin/bash

# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA



#
# Functions common to the whole script: they modify only global variables (script variables)
# in addition to their own local variables
#
printerr ()
{
  local prefix

  case $1 in
  'error')
    prefix='\033[1;31mError: \033[0m'
    shift
  ;;
  'warning')
    prefix='\033[1;33mWarning: \033[0m'
    shift
  ;;
  'message')
    prefix='\033[1;32mMessage: \033[0m'
    shift
  ;;
  *)
    prefix=''
  ;;
  esac

  printf '%b%s\n' "$prefix" "$1" >&2
}

duperr ()
{
  if ((quiet)); then
    tee /dev/null
  else
    tee >(cat >&2)
  fi
}

cleanup ()
{
  local f

  for f in "${tempfiles[@]}"; do
    [ -f "$f" ] && rm "$f"
  done

  [ -f "$temp_logfile" ] && rm "$temp_logfile"

  [ "$(ps -o comm --no-headers $gdbus_pid)" = 'gdbus' ] && kill $gdbus_pid
}

abort ()
{
  local -a files

  printf '\n%s\n' '*** Aborted ***' | duperr

  case $1 in
  'running')
    echo 'A previous Mousepad instance did not terminate'
  ;;
  'file')
    echo 'Creation of temporary file failed'
  ;;
  *)
    echo "$1"
  ;;
  esac | duperr

  # see if some backup files were kept
  shopt -s nullglob
  files=("$logdir/"*.bak)
  [ -n "${files[*]}" ] \
    && echo "Some '.bak' files to restore Mousepad configuration were kept in $logdir" | duperr

  cleanup

  exit 1
}

filter ()
{
  if [ -z "${ignored[*]}" ]; then
    cat
  else
    grep -E -v "${ignored[@]}"
  fi
}

indent ()
{
  sed 's/^/  /'
}



#
# Functions common only to test functions: they can (but should not) modify local variables
# of these test functions in addition to their own local variables
#
log_and_run_mousepad ()
{
  ((n_cmds++))
  echo "Command $n_cmds: $mousepad $*" | duperr

  if [ "$1" = '--quit' ]; then
    "$mousepad" --quit 2> >(filter | indent) 1>/dev/null &
    $timeout $pidwait -x mousepad 2> >(indent)
  else
    "$mousepad" "$@" 2> >(filter >"$temp_logfile") 1>/dev/null &
  fi
}

kill_mousepad ()
{
  local pid

  # use a subshell to disable kill builtin only locally
  if pid=$(pgrep -x mousepad); then
    (
      enable -n kill
      kill --timeout 5000 KILL -- $pid
    )
    return 1
  else
    return 0
  fi
}

purge_logs ()
{
  while IFS='' read -r; do
    if [[ $REPLY == Command:* ]]; then
      ((n_cmds++))
      echo "Command $n_cmds:${REPLY#Command:}" | duperr
    else
      echo "  $REPLY"
      ((n_warns == 0 || warnings[-1] != n_cmds)) && warnings[n_warns++]=$n_cmds
    fi
  done <"$temp_logfile"

  : >"$temp_logfile"
}

log_results ()
{
  purge_logs
  ((n_warns != 0 && warnings[-1] == n_cmds)) && printf '%s\n\n' "Exit code: $1"

  (($1 == 0)) || {
    errors[n_errs++]=$n_cmds
    ((n_warns != 0 && warnings[-1] == n_cmds)) || printf '%s\n\n' "Exit code: $1"
  }
}

shopt -s nullglob extglob
session_restore_cleanup ()
{
  local -a files

  ((session_restore)) || return 0

  gsettings reset org.xfce.mousepad.state.application session

  files=("${XDG_DATA_HOME:-$HOME/.local/share}/Mousepad/autosave-"+([0-9]))
  [ -z "${files[*]}" ] || rm "${files[@]}"
}