fonks.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/bin/sh
  2. . /usr/bin/gettext.sh
  3. alias sed="busybox sed"
  4. # Internal
  5. lgettext() { gettext -d 'x' "$@"; }
  6. translate_query() {
  7. case $1 in
  8. y) lgettext "y";;
  9. Y) lgettext "Y";;
  10. n) lgettext "n";;
  11. N) lgettext "N";;
  12. # Support other cases but keep them untranslated.
  13. *) echo "$1" ;;
  14. esac
  15. }
  16. okmsg="$(lgettext 'Done')"
  17. ermsg="$(lgettext 'Failed')"
  18. : ${okcolor=32}
  19. : ${ercolor=31}
  20. : ${decolor=36}
  21. # Parse cmdline options and store values in a variable.
  22. for opt in "$@"; do
  23. opt_name="${opt%%=*}"; opt_name="$(echo -n "${opt_name#--}" | tr -c 'a-zA-Z0-9' '_')"
  24. case "$opt" in
  25. --[0-9]*=*) export _$opt_name="${opt#*=}" ;;
  26. --[0-9]*) export _$opt_name=yes ;;
  27. --*=*) export $opt_name="${opt#*=}" ;;
  28. --*) export $opt_name=yes ;;
  29. esac
  30. done
  31. [ "$HTTP_REFERER" ] && output='html'
  32. # i18n functions
  33. _() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; echo; }
  34. _n() { local T="$1"; shift; printf "$(eval_gettext "$T")" "$@"; }
  35. _p() { local S="$1" P="$2" N="$3"; shift 3; printf "$(ngettext "$S" "$P" "$N")" "$@"; }
  36. # Get terminal columns
  37. get_cols() { stty size 2>/dev/null | awk -vc=$cols 'END{print c?c:($2 && $2<80)?$2:80}'; }
  38. # Last command status
  39. status() {
  40. local ret_code=$?
  41. [ -n "$quiet" -a "$ret_code" -eq 0 ] && return
  42. [ -n "$quiet" ] && action "$saved_action" no-quiet
  43. case $ret_code in
  44. 0) local msg="$okmsg" color="$okcolor";;
  45. *) local msg="$ermsg" color="$ercolor";;
  46. esac
  47. case $output in
  48. raw|gtk) echo " $msg";;
  49. html) echo " <span class=\"float-right color$color\">$msg</span>";;
  50. *) echo -e "[ \\033[1;${color}m$msg\\033[0;39m ]";;
  51. esac
  52. }
  53. # Line separator
  54. separator() {
  55. [ -n "$quiet" ] && return
  56. case $output in
  57. gtk) echo '--------';;
  58. html) echo -n '<hr/>';;
  59. *) printf "%$(get_cols)s\n" | tr ' ' "${1:-=}";;
  60. esac
  61. }
  62. # New line
  63. newline() {
  64. [ -z "$quiet" ] && echo
  65. }
  66. # Display a bold message
  67. boldify() {
  68. [ -n "$quiet" ] && return
  69. case $output in
  70. raw) echo "$@" ;;
  71. gtk) echo "<b>$@</b>" ;;
  72. html) echo "<strong>$@</strong>" ;;
  73. *) echo -e "\\033[1m$@\\033[0m" ;;
  74. esac
  75. }
  76. # renkli mesaj
  77. ryaz() {
  78. [ -n "$quiet" ] && return
  79. : ${color=$1}
  80. shift
  81. case $output in
  82. raw|gtk) echo "$@";;
  83. html) echo -n "<span class=\"color$color\">$@</span>";;
  84. *) case $color in
  85. 0*) echo -e "\\033[${color:-38}m$@\\033[39m";;
  86. *) echo -e "\\033[1;${color:-38}m$@\\033[0;39m";;
  87. esac;;
  88. esac
  89. unset color
  90. }
  91. # Indent text
  92. indent() {
  93. [ -n "$quiet" ] && return
  94. local in="$1"
  95. shift
  96. echo -e "\033["$in"G $@";
  97. }
  98. # Extended MeSsaGe output
  99. emsg() {
  100. [ -n "$quiet" ] && return
  101. local sep="\n$(separator)\n"
  102. case $output in
  103. raw)
  104. echo "$@" | sed -e 's|<b>||g; s|</b>||g; s|<c [0-9]*>||g; \
  105. s|</c>||g; s|<->|'$sep'|g; s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
  106. gtk)
  107. echo "$@" | sed -e 's|<c [0-9]*>||g; s|</c>||g; s|<->|'$sep'|g; \
  108. s|<n>|\n|g; s|<i [0-9]*>| |g' ;;
  109. html)
  110. echo "$@" | sed -e 's|<b>|<strong>|g; s|</b>|</strong>|g; \
  111. s|<c \([0-9]*\)>|<span class="color\1">|g; s|</c>|</span>|g; \
  112. s|<n>|<br/>|g; s|<->|<hr/>|g; s|<i [0-9]*>| |g' ;;
  113. *)
  114. echo -en "$(echo "$@" | sed -e 's|<b>|\\033[1m|g; s|</b>|\\033[0m|g;
  115. s|<c 0\([0-9]*\)>|\\033[\1m|g; s|<c \([1-9][0-9]*\)>|\\033[1;\1m|g;
  116. s|</c>|\\033[0;39m|g; s|<n>|\n|g;
  117. s|<->|'$sep'|g; s|<i \([0-9]*\)>|\\033[\1G|g')"
  118. [ "$1" != "-n" ] && echo
  119. ;;
  120. esac
  121. }
  122. # Check if user is logged as root
  123. check_root() {
  124. if [ $(id -u) -ne 0 ]; then
  125. lgettext "You must be root to execute:"; echo " $(basename $0) $@"
  126. exit 1
  127. fi
  128. }
  129. # Display debug info when --debug is used.
  130. debug() {
  131. [ -n "$debug" ] && echo "$(colorize $decolor 'DEBUG:') $1"
  132. }
  133. # Confirmation
  134. confirm() {
  135. if [ -n "$yes" ]; then
  136. true
  137. else
  138. if [ -n "$1" ]; then
  139. echo -n "$1 "
  140. else
  141. echo -n " ($(translate_query y)/$(translate_query N)) ? "
  142. fi
  143. read answer
  144. [ "$answer" == "$(translate_query y)" ]
  145. fi
  146. }
  147. # Log islemleri
  148. log() {
  149. echo "$(date '+%F %R') : $@" >> ${faaliyet:-/var/log/islem.log}
  150. }
  151. # Print two-column list of options with descriptions
  152. optlist() {
  153. [ -n "$quiet" ] && return
  154. local in="$(echo "$1" | sed 's| *| |g')" w=$(get_cols) col1=1 line
  155. IFS=$'\n'
  156. for line in $in; do
  157. col=$(echo -n "$line" | cut -f1 | wc -m)
  158. [ $col -gt $col1 ] && col1=$col
  159. done
  160. echo "$in" | sed 's|\t|&\n|' | fold -sw$((w - col1 - 4)) | \
  161. sed "/\t/!{s|^.*$|[$((col1 + 4))G&|g}" | sed "/\t$/{N;s|.*| &|;s|\t\n||}"
  162. }
  163. # Wrap words in long terminal message
  164. longline() {
  165. [ -n "$quiet" ] && return
  166. local w=$(get_cols)
  167. echo -e "$@" | fold -sw$w
  168. }
  169. # Print localized title
  170. title() {
  171. [ -n "$quiet" ] && return
  172. case $output in
  173. html) echo "<section><header>$(_ "$@")</header><pre class=\"scroll\">";;
  174. *) newline; boldify "$(_ "$@")"; separator;;
  175. esac
  176. }
  177. # Print footer
  178. footer() {
  179. [ -n "$quiet" ] && return
  180. case $output in
  181. html) echo "</pre><footer>$1</footer></section>";;
  182. *) separator; echo "$1"; [ -n "$1" ] && newline;;
  183. esac
  184. }
  185. # Print current action
  186. saved_action=''
  187. action() {
  188. saved_action="$1"
  189. [ -n "$quiet" -a -z "$2" ] && return
  190. local w c scol msg chars
  191. w=$(_ 'w'); w=${w/w/10}
  192. c=$(get_cols)
  193. scol=$(( $c - $w ))
  194. msg="$(_n "$@" | fold -sw$scol)"
  195. chars=$(echo -n "$msg" | tail -n1 | wc -m)
  196. msg="$(printf '%s%'$(( $scol - $chars ))'s' "$msg" '')"
  197. case $output in
  198. raw|gtk|html) echo -n "$msg";;
  199. *) echo -ne "\033[0;33m$msg\033[0m";;
  200. esac
  201. }
  202. # Print long line as list item
  203. itemize() {
  204. [ -n "$quiet" ] && return
  205. case $output in
  206. gtk) echo "$@";;
  207. *)
  208. local inp="$@" w=$(get_cols) first offset
  209. first="$(echo -e "$inp" | fold -sw$w | head -n1)"
  210. echo "$first"
  211. cols1="$(echo "${first:1}" | wc -c)"
  212. offset=$(echo "$first" | sed -n 's|^\([^:\*-]*[:\*-]\).*$|\1|p' | wc -m)
  213. echo "${inp:$cols1}" | fold -sw$((w - offset)) | awk \
  214. '($0){printf "%'$offset's%s\n","",$0}'
  215. ;;
  216. esac
  217. }