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 # ==============================================================================
9 # ============= Variables =============
10 MISC_IS_READONLY_MODE=no
11 MISC_DEFAULT_CONSOLE_SIZE='25 80'
13 # ============= Check whether the current process is by the superuser privilege =============
14 misc_is_superuser_privilege ()
16 [ `id -u` -eq 0 -a "$MISC_IS_READONLY_MODE" = no ]
19 # ============= Lock for duplicated executions =============
20 misc_lock_duplicated_executions ()
24 MISC_IS_READONLY_MODE=no
25 [ `id -u` -eq 0 ] || return 0
26 if mktemp -q "$lockfile" > /dev/null
29 elif [ x`cat "$lockfile" 2> /dev/null` != x$$ ]
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
34 MISC_IS_READONLY_MODE=yes
39 # ============= Check the superuser privilege and abort if not =============
42 misc_is_superuser_privilege && return
43 message_echo "ERROR: The superuser privilege is required for this command." >&2
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 ()
52 set | sed -E '/^[^ ]+ \(\) *$/,/^\}$/d'
55 # ============= Initialize shell variable definitions =============
58 eval `misc_get_all_vardefs | grep -E '^[a-z_][a-zA-Z0-9_]+=' | sed 's/=.*//;s/^/unset /'`
61 # ============= Get the size of the current console =============
62 misc_get_console_size ()
65 size=`stty -f /dev/stdin size 2> /dev/null || :`
68 echo "$size" > ${TMPDIR}/misc_get_console_size::size
70 if [ ! -e "${TMPDIR}/misc_get_console_size::size" ]
72 size=`stty -f /dev/console size 2> /dev/null || :`
74 size=`cat "${TMPDIR}/misc_get_console_size::size"`
77 [ -n "$size" ] || size=$MISC_DEFAULT_CONSOLE_SIZE
81 # ============= Get the number of columns for the current console =============
82 misc_get_console_column_size ()
84 misc_get_console_size | sed -E 's/^[^ ]+ +([^ ]+)/\1/'
87 # ============= Get the number of rows for the current console =============
88 misc_get_console_row_size ()
90 misc_get_console_size | sed -E 's/^([^ ]+) +[^ ]+/\1/'
93 # ============= Selection of removing leaf ports =============
94 # Box options for dialog(1) are given via stdin.
95 misc_dialog_checklist ()
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
102 cmdscript=${TMPDIR}/misc_dialog_checklist::command
103 if [ `wc -l < $itemlist` -eq 0 ]
105 cp /dev/null "$dstfile"
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`
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`
126 while [ $iline -le $nlines ]
128 tag=`sed -n "${iline}p" "$itemlist" | cut -f 1`
130 len_tag_cur=`echo -n "$tag" | wc -c`
131 [ $len_tag_cur -gt $len_tag ] && len_tag=$len_tag_cur
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 ]
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`
144 len_item=`echo -n "$item" | wc -c`
145 if [ $len_item -gt $len_item_max ]
147 item=`echo -n "$item" | head -c $len_item_trim`...\"
149 echo -n "$tag $item $status "
151 echo ' 2> ${TMPDIR}/misc_dialog_checklist::selected_items || :' >> $cmdscript
153 tr -d \" < ${TMPDIR}/misc_dialog_checklist::selected_items | tr ' ' '\n' > $dstfile