OSDN Git Service

[BUG FIX] Comments on termination were unavailable in some phases.
[portsreinstall/current.git] / lib / libmisc.sh
1 #!/bin/sh -e
2 # ==============================================================================
3 # portsreinstall library script
4 # - Miscellaneous functions -
5 # Copyright (C) 2013-2016 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
8
9 # ============= Variables =============
10 MISC_IS_READONLY_MODE=no
11 MISC_DEFAULT_CONSOLE_SIZE='25 80'
12
13 # ============= Check whether the current process is by the superuser privilege =============
14 misc_is_superuser_privilege ()
15 {
16         [ `id -u` -eq 0 -a "$MISC_IS_READONLY_MODE" = no ]
17 }
18
19 # ============= Lock for duplicated executions =============
20 misc_lock_duplicated_executions ()
21 {
22         local lockfile
23         lockfile=$1
24         MISC_IS_READONLY_MODE=no
25         [ `id -u` -eq 0 ] || return 0
26         if mktemp -q "$lockfile" > /dev/null
27         then
28                 echo $$ > $lockfile
29         elif [ x`cat "$lockfile" 2> /dev/null` != x$$ ]
30         then
31                 message_echo "WARNING: Another ${APPNAME} process is running; commands requiring the superuser privilege is unavailable." >&2
32                 message_echo "If this report is incorrect, remove an obsolete lock file ($lockfile)." >&2
33                 message_echo >&2
34                 MISC_IS_READONLY_MODE=yes
35         fi
36         :
37 }
38
39 # ============= Check the superuser privilege and abort if not =============
40 misc_chk_privilege ()
41 {
42         misc_is_superuser_privilege && return
43         message_echo "ERROR: The superuser privilege is required for this command." >&2
44         exit 1
45 }
46
47 # ============= Get all shell variable definitions (without functions) =============
48 # Actually, this filtering is unnecessary for Bourne shell and only required for Bash.
49 # Since the overhead is not harmful, I will keep this for correspondence to possible future changes.
50 misc_get_all_vardefs ()
51 {
52         set | sed -E '/^[^ ]+ \(\) *$/,/^\}$/d'
53 }
54
55 # ============= Initialize shell variable definitions =============
56 misc_init_vardefs ()
57 {
58         eval `misc_get_all_vardefs | grep -E '^[a-z_][a-zA-Z0-9_]+=' | sed 's/=.*//;s/^/unset /'`
59 }
60
61 # ============= Get the size of the current console =============
62 misc_get_console_size ()
63 {
64         local size
65         size=`stty -f /dev/stdin size 2> /dev/null || :`
66         if [ -n "$size" ]
67         then
68                 echo "$size" > ${TMPDIR}/misc_get_console_size::size
69         else
70                 if [ ! -e "${TMPDIR}/misc_get_console_size::size" ]
71                 then
72                         size=`stty -f /dev/console size 2> /dev/null || :`
73                 else
74                         size=`cat "${TMPDIR}/misc_get_console_size::size"`
75                 fi
76         fi
77         [ -n "$size" ] || size=$MISC_DEFAULT_CONSOLE_SIZE
78         echo "$size"
79 }
80
81 # ============= Get the number of columns for the current console =============
82 misc_get_console_column_size ()
83 {
84         misc_get_console_size | sed -E 's/^[^ ]+ +([^ ]+)/\1/'
85 }
86
87 # ============= Get the number of rows for the current console =============
88 misc_get_console_row_size ()
89 {
90         misc_get_console_size | sed -E 's/^([^ ]+) +[^ ]+/\1/'
91 }
92
93 # ============= Selection of removing leaf ports =============
94 # Box options for dialog(1) are given via stdin.
95 misc_dialog_checklist ()
96 {
97         local title desc dstfile itemlist cmdscript LINES COLUMNS hight width width_desc lines_desc hight_list width_list nlines iline len_tag tag len_tag_cur len_item_max len_item_trim
98         title=$1
99         desc=$2
100         dstfile=$3
101         itemlist=$4
102         cmdscript=${TMPDIR}/misc_dialog_checklist::command
103         if [ `wc -l < $itemlist` -eq 0 ]
104         then
105                 cp /dev/null "$dstfile"
106                 return 0
107         fi
108         echo -n 'dialog --title "$title" --checklist "$desc" $hight $width $hight_list ' > $cmdscript
109         COLUMNS=`misc_get_console_column_size`
110         LINES=`misc_get_console_row_size`
111         hight=$(($LINES-3))
112         [ $hight -gt 0 ] || hight=$LINES
113         width=$(($COLUMNS-5))
114         [ $width -gt 0 ] || width=$COLUMNS
115         width_desc=$(($width-4))
116         [ $width_desc -gt 0 ] || width_desc=$COLUMNS
117         lines_desc=`echo "$desc" | sed -E 's/\\n/\
118 /g' | fold -s -w $width_desc | wc -l`
119         hight_list=$(($hight-$lines_desc-6))
120         [ $hight_list -gt 0 ] || hight_list=$LINES
121         width_list=$(($width-6))
122         [ $width_list -gt 0 ] || width_list=$COLUMNS
123         nlines=`wc -l < $itemlist`
124         iline=1
125         len_tag=0
126         while [ $iline -le $nlines ]
127         do
128                 tag=`sed -n "${iline}p" "$itemlist" | cut -f 1`
129                 iline=$(($iline+1))
130                 len_tag_cur=`echo -n "$tag" | wc -c`
131                 [ $len_tag_cur -gt $len_tag ] && len_tag=$len_tag_cur
132         done
133         iline=1
134         len_item_max=$(($width_list-$len_tag-6))
135         [ $len_item_max -gt 0 ] || len_item_max=$width_list
136         len_item_trim=$(($len_item_max-3))
137         [ $len_item_trim -gt 0 ] || len_item_trim=$len_item_max
138         while [ $iline -le $nlines ]
139         do
140                 tag=`sed -n "${iline}p" "$itemlist" | cut -f 1`
141                 item=`sed -n "${iline}p" "$itemlist" | cut -f 2`
142                 status=`sed -n "${iline}p" "$itemlist" | cut -f 3`
143                 iline=$(($iline+1))
144                 len_item=`echo -n "$item" | wc -c`
145                 if [ $len_item -gt $len_item_max ]
146                 then
147                         item=`echo -n "$item" | head -c $len_item_trim`...\"
148                 fi
149                 echo -n "$tag $item $status "
150         done >> $cmdscript
151         echo ' 2> ${TMPDIR}/misc_dialog_checklist::selected_items || :' >> $cmdscript
152         . "$cmdscript"
153         tr -d \" < ${TMPDIR}/misc_dialog_checklist::selected_items | tr ' ' '\n' > $dstfile
154         echo >> $dstfile
155 }
156