1
2
3
4
5
6
7
8
9
10
11
12
13
14 if [[ "$-" == *i* ]] ; then interactive_shell=1; fi
15
16 [ "$interactive_shell" ] && echo "Running .bash_include"
17 [ "$interactive_shell" ] && default_tty_setting="`stty -g`"
18 [ "$interactive_shell" ] && TTY="`tty`" && \
19 if [[ "$TTY" =~ "/dev/tty"[0-9]+ ]]; then
20 tty_short="#v${TTY:8}"
21 elif [[ "$TTY" == "/dev/tty"* ]]; then
22 tty_short="#${TTY:8}"
23 elif [[ "$TTY" == "/dev/pty"* ]]; then
24 tty_short="#p${TTY:8}"
25 elif [[ "$TTY" == "/dev/hvc"* ]]; then
26 tty_short="#h${TTY:8}"
27 elif [[ "$TTY" == "/dev/pts"* ]]; then
28 tty_short="#p${TTY:9}"
29 elif [[ "$TTY" == "/dev/console" ]]; then
30 tty_short="#c${TTY:12}"
31 else
32 unset tty_short
33 fi
34
35
36
37
38 colorprompting='\[\e[1;31m\]\!\[\e[m\] \[\e[35m\]$tty_short\[\e[m\][\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]\[\e[44;37m\] \j \[\e[m\]'
39 nocolorprompting='\! $tty_short[\u@\h \w] \j '
40
41 if [ "$SSH_CONNECTION" ]
42 then
43 colorprompting="\[\e[1;44m\]*\[\e[m\]$colorprompting"
44 nocolorprompting="*$nocolorprompting"
45 fi
46
47 if [ "$EPREFIX" ]
48 then
49 colorprompting="$colorprompting[\[\e[1;35m\]Prefix\[\e[m\]]"
50 nocolorprompting="$nocolorprompting[Prefix]"
51 fi
52
53 if [ "$X_SCLS" ]
54 then
55 scl_enabled="`echo "$X_SCLS" | sed 's/ *$//' | tr ' ' ','`"
56 colorprompting="$colorprompting[\[\e[1;35m\]SCL:$scl_enabled\[\e[m\]]"
57 nocolorprompting="$nocolorprompting[SCL:$scl_enabled]"
58 unset scl_enabled
59 fi
60
61 if [ "$UNDER_JHBUILD" = "true" ] || [ "$CERTIFIED_GNOMIE" = "yes" ]
62 then
63 colorprompting="$colorprompting[\[\e[1;35m\]JHBuild\[\e[m\]]"
64 nocolorprompting="$nocolorprompting[JHBuild]"
65 fi
66
67 if [ "$ANDROID_BUILD_TOP" ]
68 then
69 colorprompting="$colorprompting[\[\e[1;35m\]Android\[\e[m\]]"
70 nocolorprompting="$nocolorprompting[Android]"
71 fi
72
73 if [ "$OSTYPE" = "cygwin" ]
74 then
75 colorprompting="$colorprompting[\[\e[1;35m\]Cygwin\[\e[m\]]"
76 nocolorprompting="$nocolorprompting[Cygwin]"
77 fi
78
79 if [ "$OSTYPE" = "msys" ]
80 then
81 colorprompting="$colorprompting[\[\e[1;35m\]MSYS\[\e[m\]]"
82 nocolorprompting="$nocolorprompting[MSYS]"
83 fi
84
85 if [ -f "$HOME/.bash_addps" ]
86 then
87 exec 3< "$HOME/.bash_addps"
88 read -u 3 -r oneline
89 colorprompting="$oneline $colorprompting"
90 read -u 3 -r oneline
91 nocolorprompting="$oneline $nocolorprompting"
92 exec 3<&-
93 unset oneline
94 fi
95
96 colorprompting="${colorprompting}"'\[\e[41;37m\]$lasterror\[\e[m\]'
97 nocolorprompting="${nocolorprompting}"'$lasterror'
98
99 if [ "$WINDOW" ]
100 then
101 colorprompting="$colorprompting<$WINDOW>"
102 nocolorprompting="$nocolorprompting<$WINDOW>"
103 fi
104
105 if [ "$TMUX_PANE" ]
106 then
107 colorprompting="$colorprompting$TMUX_PANE"
108 nocolorprompting="$nocolorprompting$TMUX_PANE"
109 fi
110
111 if [[ "$PROMPT_COMMAND" != "S="* ]]; then
112 if [ "$PROMPT_COMMAND" ]
113 then
114 PROMPT_COMMAND="; $PROMPT_COMMAND"
115 fi
116 PROMPT_COMMAND='S="$?"; [ "$S" != "0" ] && lasterror="($S)" || unset lasterror'"$PROMPT_COMMAND"
117 fi
118
119 colorprompting="${colorprompting}"'\$ '
120 nocolorprompting="${nocolorprompting}"'\$ '
121 colorsecondprompting="\[\e[36m\]-->\[\e[m\] "
122 nocolorsecondprompting="--> "
123
124 HISTSIZE=2147483647
125 HISTFILESIZE=2147483647
126 HISTCONTROL=ignoredups:ignorespace
127 HISTTIMEFORMAT="%F %T "
128 historycountfile="$HOME/.bash_history.count"
129 historybackupfile="$HOME/.bash_history.bak"
130
131 realpath_program="readlink -f"
132 bgrunfiledir="$HOME/tmp/bgrun-$USER"
133 trashdir="$HOME/trash"
134
135
136
137
138 export EDITOR=vim
139 export FCEDIT=vim
140 export VISUAL=vim
141 export PAGER=less
142 export GCC_COLORS=1
143 export GREP_OPTIONS='--color=auto'
144
145
146
147
148 alias ll='ls -lF'
149 alias lh='ls -lFh'
150 alias la='ls -lFa'
151 alias lA='ls -lFA'
152 alias rm='rm -i'
153 alias cp='cp -pi'
154 alias mv='mv -i'
155 alias jobs='jobs -l'
156 alias less='less -RS'
157
158 case "$OSTYPE" in
159 *gnu*|*cygwin*|*msys*|*solaris*)
160 alias ls='ls --color=always -F'
161 ;;
162 *freebsd*)
163 alias ls='CLICOLOR=1 CLICOLOR_FORCE=1 ls -F'
164 export LSCOLORS='ExGxFxdxCxDxDxhbadacad'
165 ;;
166 esac
167
168 case "$OSTYPE" in
169 *gnu*|*cygwin*|*msys*|*freebsd*|*netbsd*|*solaris*)
170 alias grep='grep --color=always'
171 ;;
172 esac
173
174
175
176
177 alias safe_ls='\ls'
178 alias safe_ln='\ln'
179 alias safe_cp='\cp'
180 alias safe_mv='\mv'
181 alias safe_rm='\rm'
182 alias safe_jobs='\jobs'
183 alias safe_less='\less'
184 alias safe_grep='GREP_OPTIONS= \grep'
185
186 case "$OSTYPE" in
187 *openbsd*)
188 alias safe_mv_verbose='safe_mv'
189 alias safe_ln_verbose='safe_ln'
190 safe_mv_verbose='mv'
191 safe_ln_verbose='ln'
192 ;;
193 *)
194 alias safe_mv_verbose='safe_mv -v'
195 alias safe_ln_verbose='safe_ln -v'
196 safe_mv_verbose='mv -v'
197 safe_ln_verbose='ln -v'
198 ;;
199 esac
200
201
202
203
204 alias startcolor='PS1=$colorprompting; PS2=$colorsecondprompting'
205 alias stopcolor='PS1=$nocolorprompting; PS2=$nocolorsecondprompting'
206
207
208
209
210 alias cccc='LANG=C;LANGUAGE=C;LC_ALL=C'
211 alias enus='LANG=en_US.UTF-8;LANGUAGE=en_US:en;LC_ALL=en_US.UTF-8'
212 alias big5='LANG=zh_TW.Big5;LANGUAGE=zh_TW:zh:en;LC_ALL=zh_TW.Big5'
213 alias zhtw='LANG=zh_TW.UTF-8;LANGUAGE=zh_TW:zh:en;LC_ALL=zh_TW.UTF-8'
214
215
216
217
218 alias ndate='date +%H:%M:%S---%A---%x'
219 alias npasswd="getent passwd | awk 'BEGIN {FS=\":\"} {printf \"%24s%3s%6s%6s %-28s%-18s>> %s\\n\",\$1,\$2,\$3,\$4,\$6,\$7,\$5}' | $PAGER"
220 alias ngroup="getent group | awk 'BEGIN {FS=\":\"} {printf \"%24s%3s%6s >> %s\\n\",\$1,\$2,\$3,\$4}' | $PAGER"
221
222
223
224
225 alias savetty='default_tty_setting=`stty -g`'
226 alias resetty='stty $default_tty_setting'
227
228
229
230
231 alias git_log='git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=iso'
232 alias git_log_color='git log --color --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset" --abbrev-commit'
233 alias screen256='screen -T screen-256color'
234 alias vimhtml='vim -c "set ts=2" -c "set sw=2"'
235
236
237
238
239 function createdir_askmode ()
240 {
241 newdir_mode="$2"
242 if mkdir -p "$1"
243 then
244 echo "Directory $1 is created."
245 printf "Change the mode of the directory... "
246 read -i "$newdir_mode" -p ">>> Mode: " -e newdir_mode
247 chmod "$newdir_mode" "$1"
248 else
249 echo "Cannot create directory $1!"
250 return 1
251 fi
252 }
253
254 function check_command_existent ()
255 {
256 type "$1" &> /dev/null
257 return $?
258 }
259
260 function is_file_type ()
261 {
262 local filename="$1"
263 local typename="$2"
264 shift 2
265 [ "`"$@" find "$filename" -maxdepth 0 -type "$typename"`" ] && return 0
266 return 1
267 }
268
269 function get_file_size ()
270 {
271 split_arguments "$@"
272 "${prefixlist[@]}" du -s "${arglist[@]}" | {
273 read -d " " dirsize
274 echo "$dirsize"
275 read throwaway
276 }
277 unset arglist
278 unset prefixlist
279 }
280
281 function get_executable_extension ()
282 {
283 local lsloc="`command which ls`"
284 local lsalt="`echo ${lsloc}.* | cut -d ' ' -f 1`"
285 cmp "$lsloc" "$lsalt" &> /dev/null && echo ${lsalt:${#lsloc}} && return
286 }
287
288 function split_arguments ()
289 {
290 local argcount=$#
291 local -i i=0
292 local prefix_start=0
293 while [ "$1" ]
294 do
295 if [ "$prefix_start" = "0" ]
296 then
297 if [ "$1" = "--" ]
298 then
299 prefix_start=1
300 i=0
301 shift
302 continue
303 else
304 arglist[$i]="$1"
305 fi
306 else
307 prefixlist[$i]="$1"
308 fi
309 i=$i+1
310 shift
311 done
312 }
313
314
315
316
317 alias bgr=bgrun
318 alias bgv=bgview
319 alias bgl=bglist
320 alias bgc=bgcount
321 alias bgls=bglist
322 alias bgrm=bgclean
323
324 function bgrun ()
325 {
326 [ "$#" = "0" ] && return 1
327 [ '!' -d "$bgrunfiledir" ] && createdir_askmode "$bgrunfiledir" 0750
328 local current_time=`date "+%Y%m%d-%H%M%S"`
329 local cmdname=`echo "$1" | sed -e 's/-/_/g' -e 's/\\//_/g' -e 's/ /_/g'`
330 if [ "`echo "$cmdname" | cut -c 1`" = "_" ]
331 then
332 cmdname=`echo "$cmdname" | cut -c 2-`
333 fi
334 local filename="$bgrunfiledir/$current_time-$cmdname"
335 echo "Writing to $filename"
336 {
337 echo -n "$BASHPID " > "$filename"
338 echo "$@" >> "$filename"
339 exec "$@" &>> "$filename"
340 } &
341 }
342
343 function bglist ()
344 {
345 local viewtime=0
346 [ "$1" = "--full" ] && viewtime=1
347 {
348 for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
349 do
350 [ "$viewtime" = "1" ] && echo "$i"
351 head -n 1 "$i" | {
352 local procpid
353 local cmdline
354 read -d ' ' procpid
355 read cmdline
356 printf "(%5d) %s\n" "$procpid" "$cmdline"
357 }
358 done
359 } | {
360 if [ "$viewtime" = "1" ]
361 then
362 echo " INDEX TIME PID COMMAND"
363 local readstat=0
364 local -i i=1
365 while true
366 do
367 local dateandtime_long
368 local cmdline
369 read dateandtime_long
370 read cmdline
371 [ "$?" '!=' "0" ] && break
372 local dateandtime=`basename "$dateandtime_long"`
373 local part_year="${dateandtime:0:4}"
374 local part_month="${dateandtime:4:2}"
375 local part_date="${dateandtime:6:2}"
376 local part_hour="${dateandtime:9:2}"
377 local part_minute="${dateandtime:11:2}"
378 local part_second="${dateandtime:13:2}"
379 printf '%6d' "$i"
380 echo " $part_year-$part_month-$part_date $part_hour:$part_minute:$part_second $cmdline"
381 i=$i+1
382 done
383 else
384 echo " INDEX PID COMMAND"
385 cat -n
386 fi
387 } | $PAGER
388 }
389
390 function bgview ()
391 {
392 local -i yourchoice
393 if [ "$1" = "" ]
394 then
395 yourchoice=`bgcount`
396 else
397 if [ "$1" -le "0" ]
398 then
399 yourchoice=$((`bgcount`+$1))
400 else
401 yourchoice=$1
402 fi
403 fi
404 echo "Your choice is $yourchoice."
405 local realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${yourchoice}p`
406 head -n 1 "$realfilename" | {
407 read -d ' ' procpid
408 read cmdline
409 echo "PID: $procpid"
410 echo "Command Line: $cmdline"
411 }
412 read -e -p "View '$realfilename' ? " confirm
413 if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]
414 then
415 return 1
416 fi
417 {
418 printf "===> Process Information: "
419 cat "$realfilename"
420 } | $PAGER
421 }
422
423 function bgcount ()
424 {
425 find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}'
426 }
427
428 function bgclean ()
429 {
430 if [ "$1" = "all" ]
431 then
432 echo "Removing the directory $bgrunfiledir"
433 rm -rf "$bgrunfiledir" &
434 return 0
435 else
436 split_arguments "$@"
437 local -i i=0
438 while [ "${arglist[$i]}" ]
439 do
440 arglist[$i]="-e ${arglist[$i]}p"
441 i=$i+1
442 done
443 local oneline
444 find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort | sed -n ${arglist[*]} | {
445 while read oneline
446 do
447 echo "Removing $oneline"
448 rm -f "$oneline"
449 done
450 }
451 fi
452 unset arglist
453 unset prefixlist
454 }
455
456 function bgdu ()
457 {
458 local -i j=1
459 {
460 echo " INDEX SIZE PID COMMAND"
461 for i in `find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | sort`
462 do
463 head -n 1 "$i" | {
464 local procpid
465 local cmdline
466 read -d ' ' procpid
467 read cmdline
468 printf "%6d %8d (%5d) %s\n" "$j" \
469 "`get_file_size "$i"`" \
470 "$procpid" "$cmdline"
471 }
472 j=$j+1
473 done
474 } | $PAGER
475 }
476
477
478
479
480 function fetch_remote_file ()
481 {
482 local rval
483 printf "==> Fetch remote file \e[1;33m$2\e[m as \e[1;35m$1\e[m ...\n"
484
485 if check_command_existent curl; then
486 curl -f -o "$1" "$2"
487 rval=$?
488
489 elif check_command_existent wget; then
490 wget --progress=dot -O "$1" "$2"
491 rval=$?
492
493 elif check_command_existent fetch; then
494 fetch -o "$1" "$2"
495 rval=$?
496
497 elif check_command_existent ftp; then
498 ftp -o "$1" "$2"
499 rval=$?
500 else
501 echo "<== Sorry, I don't know how to fetch remote files on your system"
502 return 1
503 fi
504 if [ "$rval" = "0" ]; then
505 printf "<== \e[1;32mDone\e[m\n"
506 else
507 printf "<== \e[1;31mFailed\e[m\n"
508 fi
509 return $rval
510 }
511
512 function fetch_and_merge ()
513 {
514 local merge_cmd
515 local merge_cmd_prog
516 local local_file="$1"
517 local remote_file="$2"
518 if fetch_remote_file "${local_file}.new" "${remote_file}"
519 then
520 if [ '!' -e "${local_file}" ]; then
521 safe_mv_verbose -f "${local_file}.new" "${local_file}"
522 else
523 while true
524 do
525 echo ""
526 cmp "${local_file}" "${local_file}.new" &> /dev/null && \
527 echo "Downloaded file is the same as installed one." && \
528 break
529 printf "Configuration files update tool: "
530 printf "Updating \e[1;35m${local_file}\e[m\n"
531 echo ""
532 echo " Install: Install the new version"
533 echo " Keep: Keep the old version"
534 echo " Retry: Give up downloaded file and try the next mirror"
535 echo ""
536 echo " Diff: View the difference between versions"
537 echo " Merge: Merge two files by yourself"
538 echo ""
539 read -e -p "[I]nstall/(K)eep/(R)etry/(D)iff/(M)erge ? " merge_cmd
540 case "${merge_cmd}" in
541 ''|I|i)
542 safe_mv_verbose -f "${local_file}.new" "${local_file}"
543 break
544 ;;
545 K|k)
546 rm -f "${local_file}.new"
547 break
548 ;;
549 R|r)
550 rm -f "${local_file}.new"
551 return 1
552 ;;
553 D|d)
554 diff -u "${local_file}" "${local_file}.new" | $PAGER
555 ;;
556 M|m)
557 read -e -p "Merge tool: " -i "$merge_cmd_prog" merge_cmd_prog
558 if $merge_cmd_prog "${local_file}" "${local_file}.new"; then
559 break
560 else
561 echo "Command is exited with error. Please try again."
562 fi
563 ;;
564 *)
565 printf " \e[33m*** Unknown command ***\e[m \n"
566 ;;
567 esac
568 done
569 fi
570 return 0
571 fi
572 return 1
573 }
574
575 function configfile_fetch ()
576 {
577 local cgit_mirror_list=(
578 "http://www.tfcis.org/~lantw44/cgit/cgit.cgi/configfile/plain"
579 "http://phantom.tfcis.org/~lantw44/cgit/cgit.cgi/configfile/plain"
580 "http://master.lant.com.tw/~lantw44/cgit/cgit.cgi/configfile/plain")
581 local github_mirror_list=(
582 "https://raw.github.com/lantw44/configfile"
583 "http://raw.github.com/lantw44/configfile")
584 local args
585 local file_url
586 local file_version
587 local completed
588 local -a file_name
589 local -i i
590 local -i j
591
592 if [ "$1" ]; then
593 file_version="$1"
594 else
595 file_version="master"
596 fi
597
598 args=("$@")
599 for((i=1, j=0; i<=$#; i++, j++)){
600 file_name[$j]="${args[$i]}"
601 }
602
603 if [ -z "$file_name"]; then
604 file_name=("bash_include" "vimrc" "screenrc")
605 fi
606
607 for file in ${file_name[@]}
608 do
609 completed="false"
610
611 for site in ${cgit_mirror_list[@]}
612 do
613 [ "$completed" = "true" ] && break
614 file_url="$site/$file?id=$file_version"
615 if fetch_and_merge "$HOME/.${file}" "$file_url"
616 then
617 completed="true"
618 break
619 fi
620 done
621
622 for site in ${github_mirror_list[@]}
623 do
624 [ "$completed" = "true" ] && break
625 file_url="$site/$file_version/$file"
626 if fetch_and_merge "$HOME/.${file}" "$file_url"
627 then
628 completed="true"
629 break
630 fi
631 done
632
633 done
634 }
635
636 function configfile_initial_setup ()
637 {
638 cat >> ~/.bashrc << "EOF"
639 if [ -f ~/.bash_include ]; then
640 . ~/.bash_include
641 fi
642 EOF
643 if [[ "$OSTYPE" == *freebsd* ]] || [[ "$OSTYPE" == *FreeBSD* ]]; then
644 cat >> ~/.bash_login << "EOF"
645 GET_TTY_NAME=`tty | cut -c 9`
646 [ "$GET_TTY_NAME" = 'v' ] && echo "Login from local virtual terminal: `tty`"
647 [ "$GET_TTY_NAME" = 'p' ] && echo "Login from pseudo terminal: `tty`"
648 [ "$GET_TTY_NAME" = '/' ] && echo "Login from pseudo terminal: `tty`"
649 unset GET_TTY_NAME
650 EOF
651 fi
652 cat >> ~/.bash_login << "EOF"
653 if [ -f ~/.bashrc ]; then
654 . ~/.bashrc
655 fi
656 EOF
657 echo "Completed. Type \`help_function' to know how to use!"
658 for i in ~/.bashrc ~/.bash_login ~/.bash_profile ~/.shrc ~/.profile
659 do
660 grep -q -e HISTSIZE -e HISTFILESIZE "$i" 2>/dev/null && \
661 echo "Warning: HISTSIZE or HISTFILESIZE is set in $i!" && \
662 echo "History file features may not work"
663 done
664 }
665
666
667
668
669 function newpath_init ()
670 {
671 unset newpath
672 local pathsp="$pathorgval"
673 local pathentry
674 local -i i
675 eval pathsp='"${pathsp// /\\}"'
676 pathsp="${pathsp//:/ }"
677 i=0
678 for pathentry in $pathsp
679 do
680 newpath[$i]="${pathentry//\\/ }"
681 i=$i+1
682 done
683 }
684
685 function newpath_gen ()
686 {
687 local -i i
688 pathmodval=""
689 i=0
690 while [ "${newpath[$i]}" ]
691 do
692 if [ "$i" != 0 ]; then pathmodval+=":"; fi
693 pathmodval+="${newpath[$i]}"
694 i=$i+1
695 done
696 }
697
698 function path_editor ()
699 {
700 local newpathvarname
701 local badcommand
702 local command
703 local command_sub
704 local command_sub2
705 local should_continue="yes"
706 local -i i
707
708 if [ -z "$1" ]
709 then
710 newpathvarname="PATH"
711 else
712 newpathvarname="$1"
713 fi
714
715 eval pathorgval=\${${newpathvarname}}
716 newpath_init
717
718 while [ "$should_continue" ]
719 do
720 i=0
721 echo -n $'\e[2J\e[0;0H'
722 if [ "$badcommand" = "yes" ]; then
723 printf " \e[33m*** Command \`$command' is unknown ***\e[m \n"
724 badcommand=""
725 fi
726 echo " New PATH Editor for BASH "
727 echo "========================================"
728 echo "Environment Variable: $newpathvarname"
729 echo "----------------------------------------"
730 while [ "${newpath[$i]}" ]
731 do
732 echo "$i: ${newpath[$i]}"
733 i=$i+1
734 done
735 [ "$i" = '0' ] && echo "(Empty or not declared)"
736 echo "========================================"
737 read -e -p "[A]ppend/(I)nsert/(D)elete/(E)dit/(M)ove/(S)wap/(R)eset/(Q)uit ? " command
738 case "$command" in
739 ''|A|a)
740 read -e -p "Type a new entry: " newpath[$i]
741 ;;
742 I|i)
743 read -e -p "Type a new entry: " command_sub
744 if [ -z "$command_sub" ]; then continue; fi
745 newpath_tmp=( "${newpath[@]}" )
746 unset newpath
747 newpath="$command_sub"
748 i=0
749 while [ "${newpath_tmp[$i]}" ]
750 do
751 newpath[$i+1]="${newpath_tmp[$i]}"
752 i=$i+1
753 done
754 unset newpath_tmp
755 ;;
756 D|d)
757 read -e -p "Index: " command_sub
758 if [ -z "$command_sub" ]; then continue; fi
759 i="$command_sub"
760 i=$i+1
761 while [ "${newpath[$i]}" ]
762 do
763 newpath[$i-1]="${newpath[$i]}"
764 i=$i+1
765 done
766 unset newpath[$i-1]
767 ;;
768 E|e)
769 read -e -p "Index: " command_sub
770 read -e -p "Modify this entry: " -i "${newpath[$command_sub]}" newpath[$command_sub]
771 ;;
772 M|m)
773 read -e -p "From: " command_sub
774 read -e -p "To: " command_sub2
775 if [ "$command_sub" -eq "$command_sub2" ]; then continue; fi
776 cmdsubval="${newpath[$command_sub]}"
777 if [ "$command_sub" -gt "$command_sub2" ]; then
778 i="$command_sub"
779 while [ "$i" -gt "$command_sub2" ]
780 do
781 newpath[$i]="${newpath[$i-1]}"
782 i=$i-1
783 done
784 newpath[$command_sub2]="$cmdsubval"
785 else
786 i="$command_sub"
787 while [ "$i" -lt "$command_sub2" ]
788 do
789 newpath[$i]="${newpath[$i+1]}"
790 i=$i+1
791 done
792 newpath[$command_sub2]="$cmdsubval"
793 fi
794 unset cmdsubval
795 ;;
796 S|s)
797 read -e -p "First entry: " command_sub
798 read -e -p "Second entry: " command_sub2
799 swaptmp="${newpath[$command_sub]}"
800 newpath[$command_sub]="${newpath[$command_sub2]}"
801 newpath[$command_sub2]="$swaptmp"
802 unset swaptmp
803 ;;
804 R|r)
805 read -e -p "Discard all changes [y/N] ? " command_sub
806 if [ "$command_sub" = "y" ]; then
807 eval pathorgval=\${${newpathvarname}}
808 newpath_init
809 fi
810 ;;
811 Q|q)
812 newpath_gen
813 eval export ${newpathvarname}=\"$pathmodval\"
814 echo "${newpathvarname}=$pathmodval"
815 history -s "${newpathvarname}=$pathmodval"
816 should_continue=''
817 ;;
818 *)
819 badcommand="yes"
820 ;;
821 esac
822 done
823 unset newpath
824 unset pathorgval
825 unset pathmodval
826 }
827
828 function ldpath_editor ()
829 {
830 path_editor LD_LIBRARY_PATH
831 }
832
833
834
835
836 function split_path_core ()
837 {
838 echo "$current_path" | {
839 while read -d : oneline
840 do
841 [ '!' "$oneline" = '^' ] && echo "$oneline"
842 done
843 [ '!' "$oneline" = '^' ] && echo "$oneline"
844 }
845 unset oneline
846 }
847
848 function split_path ()
849 {
850 coproc split_path_core
851 readarray -t -u ${COPROC[0]} patharr
852 wait $COPROC_PID
853 }
854
855 function update_path ()
856 {
857 current_path=''
858 local -i i=0
859 local firsttime="yes"
860 while [ "${patharr[$i]}" ]
861 do
862 if [ '!' "${patharr[$i]}" = "^" ]
863 then
864 if [ "$firsttime" ]
865 then
866 firsttime=''
867 else
868 current_path+=':'
869 fi
870 current_path+="${patharr[$i]}"
871 fi
872 i=$i+1
873 done
874 }
875
876 function old_path_editor_core ()
877 {
878 if [ "$1" = "ld" ]
879 then
880 export current_path="$LD_LIBRARY_PATH"
881 else
882 export current_path="$PATH"
883 fi
884 local should_continue="yes"
885 local command
886 local command_sub
887 local command_sub2
888 local -i i
889 while [ "$should_continue" ]
890 do
891 split_path
892 i=0
893 echo "========================================"
894 while [ "${patharr[$i]}" ]
895 do
896 echo "$i: ${patharr[$i]}"
897 i=$i+1
898 done
899 [ "$i" = '0' ] && echo "(Empty or not declared)"
900 echo "========================================"
901 read -e -p "[A]ppend/(D)elete/(E)dit/(M)ove/(R)eset/(Q)uit ? " command
902 case "$command" in
903 ''|A|a)
904 read -e -p "Type a new entry: " patharr[$i]
905 update_path
906 ;;
907 D|d)
908 read -e -p "Index: " command_sub
909 patharr[$command_sub]='^'
910 update_path
911 ;;
912 E|e)
913 read -e -p "Index: " command_sub
914 read -e -p "Modify this entry: " -i "${patharr[$command_sub]}" patharr[$command_sub]
915 update_path
916 ;;
917 M|m)
918 read -e -p "From: " command_sub
919 read -e -p "To: " command_sub2
920 swaptmp="${patharr[$command_sub]}"
921 patharr[$command_sub]="${patharr[$command_sub2]}"
922 patharr[$command_sub2]="$swaptmp"
923 unset swaptmp
924 update_path
925 ;;
926 R|r)
927 if [ "$1" = "ld" ]
928 then
929 current_path="$LD_LIBRARY_PATH"
930 else
931 current_path="$PATH"
932 fi
933 ;;
934 Q|q)
935 if [ "$1" = "ld" ]
936 then
937 export LD_LIBRARY_PATH="$current_path"
938 echo "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
939 history -s "LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
940 else
941 export PATH="$current_path"
942 echo "PATH=$PATH"
943 history -s "PATH=$PATH"
944 fi
945 should_continue=''
946 ;;
947 *)
948 printf " \e[33m*** Unknown command ***\e[m \n"
949 ;;
950 esac
951 done
952 unset patharr
953 unset current_path
954 }
955
956 function old_path_editor ()
957 {
958 old_path_editor_core
959 }
960
961 function old_ldpath_editor ()
962 {
963 old_path_editor_core ld
964 }
965
966
967
968
969 alias trash_put=trash_mv
970 alias trash_add=trash_mv
971 alias trash_list=trash_ls
972 alias trash_ct=trash_count
973 alias trash_restore=trash_recover
974 alias trash_rc=trash_recover
975 alias trash_drop=trash_rm
976 alias trash_clean=trash_rm
977
978 function trash_mv ()
979 {
980 [ "$#" = "0" ] && return 1
981 [ '!' -d "$trashdir" ] && createdir_askmode "$trashdir" 0700
982 local original_path
983 local current_time
984 local -i i=0
985 split_arguments "$@"
986 while [ "${arglist[$i]}" ]
987 do
988 original_path="`"${prefixlist[@]}" $realpath_program "${arglist[$i]}"`"
989 current_time=`date "+%Y%m%d-%H%M%S"`
990 better_time=`date "+%Y-%m-%d %H:%M:%S"`
991 dirname="`basename "${arglist[$i]}" | sed -e 's/-/_/g' -e 's/ /_/g'`"
992 fulldirname="$trashdir/$current_time-$dirname"
993 mkdir -p "$fulldirname"
994 echo "Move: ${arglist[$i]} -> $fulldirname"
995 "${prefixlist[@]}" mv "${arglist[$i]}" "$fulldirname"
996 if [ "$?" = "0" ]
997 then
998 echo "$better_time" > "$fulldirname/information.date"
999 echo "$original_path" > "$fulldirname/information.path"
1000 else
1001 rmdir "$fulldirname"
1002 fi
1003 i=$i+1
1004 shift
1005 done
1006 unset arglist
1007 unset prefixlist
1008 }
1009
1010 function trash_ls ()
1011 {
1012 local -i i=1
1013 local oneline
1014 find "$trashdir" -mindepth 1 -maxdepth 1 | sort | {
1015 while read oneline
1016 do
1017 printf "%6d %s %s\n" "$i" \
1018 "$(< "$oneline/information.date")" \
1019 "$(< "$oneline/information.path")"
1020 i=$i+1
1021 done
1022 } | $PAGER
1023 }
1024
1025 function trash_cd ()
1026 {
1027 [ -z "$1" ] && return 1
1028 cd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
1029 }
1030
1031 function trash_pushd ()
1032 {
1033 [ -z "$1" ] && return 1
1034 pushd `find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n $1p`
1035 }
1036
1037 function trash_recover ()
1038 {
1039 [ -z "$1" ] && return 1
1040 split_arguments "$@"
1041 local -i i=0
1042 while [ "${arglist[$i]}" ]
1043 do
1044 arglist[$i]="-e ${arglist[$i]}p"
1045 i=$i+1
1046 done
1047 find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} | {
1048 while read oneline
1049 do
1050 local fromfile="$oneline/`basename "$(< "$oneline/information.path")"`"
1051 local tofile="`dirname "$(< "$oneline/information.path")"`"
1052 if [ -e "$(< "$oneline/information.path")" ]
1053 then
1054 echo "Destination file exists."
1055 continue
1056 fi
1057 echo "Move: $fromfile -> $tofile"
1058 "${prefixlist[@]}" mv -f "$fromfile" "$tofile"
1059 if [ "$?" = "0" ]
1060 then
1061 echo "Remove: $oneline"
1062 rm -rf "$oneline"
1063 fi
1064 done
1065 }
1066 unset arglist
1067 unset prefixlist
1068 }
1069
1070 function trash_rm ()
1071 {
1072 split_arguments "$@"
1073 local -i i=0
1074 while [ "${arglist[$i]}" ]
1075 do
1076 arglist[$i]="-e ${arglist[$i]}p"
1077 i=$i+1
1078 done
1079 trash_dirname=`find "$trashdir" -mindepth 1 -maxdepth 1 | sort | sed -n ${arglist[*]} `
1080 echo 'Type rm -rf $trash_dirname to remove them.'
1081 unset arglist
1082 unset prefixlist
1083 }
1084
1085 function trash_count ()
1086 {
1087 find "$trashdir" -mindepth 1 -maxdepth 1 | wc | awk '{print $2}'
1088 }
1089
1090 function trash_du ()
1091 {
1092 split_arguments "$@"
1093 local oneline
1094 local -i i=1
1095 find "$trashdir" -maxdepth 1 -mindepth 1 | sort | {
1096 while read oneline
1097 do
1098 printf "%6d %8d %s\n" "$i" \
1099 "`get_file_size "$oneline" -- "${prefixlist[@]}"`" \
1100 "$(< "$oneline/information.path")"
1101 i=$i+1
1102 done
1103 } | $PAGER
1104 unset arglist
1105 unset prefixlist
1106 }
1107
1108
1109
1110
1111 function check_dmesg ()
1112 {
1113 [ "$#" = "0" ] && return 1
1114
1115 while true
1116 do
1117 cdm_previous_dmesg_buf="$cdm_current_dmesg_buf"
1118 cdm_current_dmesg_buf="`dmesg`"
1119 [ "$cdm_previous_dmesg_buf" '!=' "$cdm_current_dmesg_buf" ] && \
1120 [ "$cdm_first_run" = "0" ] && \
1121 echo '===> You should check the system message buffer <==='
1122 sleep $1
1123 [ "$?" '!=' "0" ] && return 1
1124 cdm_first_run=0
1125 done
1126 }
1127
1128 function check_system_status ()
1129 {
1130 [ "$#" = "0" ] && return 1
1131
1132 filename_mail="$MAIL"
1133 filename_messages="/var/log/messages"
1134 filename_audit="/var/log/audit/audit.log"
1135
1136 while true
1137 do
1138 previous_dmesg_buf="$current_dmesg_buf"
1139 current_dmesg_buf="`dmesg`"
1140 previous_mail_info="$current_mail_info"
1141 current_mail_info="`ls -l "$filename_mail"`"
1142 previous_messages_info="$current_messages_info"
1143 current_messages_info="`ls -l "$filename_messages"`"
1144 previous_audit_info="$current_audit_info"
1145 current_audit_info="`ls -l "$filename_audit"`"
1146 if [ "$first_run" = "0" ]
1147 then
1148 [ "$previous_dmesg_buf" '!=' "$current_dmesg_buf" ] && echo "===> The system message buffer is modified (dmesg) <==="
1149 [ "$previous_mail_info" '!=' "$current_mail_info" ] && echo "===> Your mailbox $filename_mail is modified <==="
1150 [ "$previous_messages_info" '!=' "$current_messages_info" ] && echo "===> $filename_messages is modified <==="
1151 [ "$previous_audit_info" '!=' "$current_audit_info" ] && echo "===> $filename_audit is modified <==="
1152 fi
1153 sleep $1
1154 first_run=0
1155 done
1156 }
1157
1158
1159
1160
1161 function backup_file ()
1162 {
1163 split_arguments "$@"
1164 local current_time=`date +%Y%m%d`
1165 local rootfilename
1166 local -i i=0
1167 local -i j
1168 while [ "${arglist[$i]}" ]
1169 do
1170 if [ '!' -f "${arglist[$i]}" ]
1171 then
1172 printf "\e[1;31mError\e[m: ${arglist[$i]} does not exist or it is not a regular file.\n"
1173 i=$i+1
1174 continue
1175 fi
1176 rootfilename="${arglist[$i]}.$current_time"
1177 if [ -e "$rootfilename" ]
1178 then
1179 j=0
1180 while [ "$j" -lt "10" ]
1181 do
1182 if [ -e "$rootfilename.$j" ]
1183 then
1184 j=$j+1
1185 continue
1186 else
1187 history -s "$FUNCNAME" "$@"
1188 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
1189 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename.$j"
1190 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
1191 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename.$j"
1192 break
1193 fi
1194 done
1195 if [ '!' "$j" -lt "10" ]
1196 then
1197 printf "\e[1;31mError\e[m: Can not create a backup file for ${arglist[$i]}.\n"
1198 printf "\e[1;33mPlease delete some backup file because I only use 0 - 9.\e[m\n"
1199 fi
1200 else
1201 history -s "$FUNCNAME" "$@"
1202 "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1203 history -s "${prefixlist[@]}" \cp -p "${arglist[$i]}" "$rootfilename"
1204 "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1205 history -s "${prefixlist[@]}" touch -r "${arglist[$i]}" "$rootfilename"
1206 fi
1207 i=$i+1
1208 done
1209 unset arglist
1210 unset prefixlist
1211 }
1212
1213
1214
1215
1216 function fbterm_chewing ()
1217 {
1218 if [ -z "$1" ]; then
1219 fbterm -s 14 -- uim-fep -u chewing
1220 else
1221 local font_size="$1"
1222 shift
1223 fbterm -s "$font_size" "$@" -- uim-fep -u chewing
1224 fi
1225 }
1226
1227 function gen_ms_inet_shortcut () {
1228 [ "$#" != "2" ] && {
1229 echo "Usage: $FUNCNAME filename url"
1230 } && return 1
1231
1232 {
1233 echo "[InternetShortcut]"
1234 echo "URL=$2"
1235 } > "$1"
1236 }
1237
1238 function unzip_nomac ()
1239 {
1240 unzip "$@" -x '__MACOSX/*' '*.DS_Store'
1241 return $?
1242 }
1243
1244
1245
1246
1247 function mkscreenacl ()
1248 {
1249 local screen_permit_command="select windowlist other meta detach reset hardcopy info redisplay lastmsg next prev xon xoff windows suspend help colon copy paste writebuf readbuf displays stuff attach"
1250 while [ "$1" '!=' '' ]
1251 do
1252 for i in $screen_permit_command
1253 do
1254 echo "aclchg $1 +x $i"
1255 done
1256 echo "aclchg $1 -rw \"\#?\""
1257 shift
1258 done
1259 }
1260
1261
1262
1263
1264
1265
1266 function editlink ()
1267 {
1268 local newdest
1269 local orgdest
1270 split_arguments "$@"
1271 local -i i=0
1272 while [ "${arglist[$i]}" ]
1273 do
1274 if is_file_type "${arglist[$i]}" "l" "${prefixlist[@]}"; then
1275 orgdest="`${prefixlist[@]} readlink "${arglist[$i]}"`"
1276 read -e -p "EditLink: ${arglist[$i]} -> " -i "$orgdest" newdest
1277 else
1278 printf "\e[1;33mWarning\e[m: ${arglist[$i]} is not a symbolic link.\n"
1279 i=$i+1
1280 continue
1281 fi
1282 if [ "$newdest" ] && [ "$newdest" '!=' "$orgdest" ]; then
1283 "${prefixlist[@]}" rm -f "${arglist[$i]}"
1284 "${prefixlist[@]}" ${safe_ln_verbose} -s "$newdest" "${arglist[$i]}"
1285 fi
1286 i=$i+1
1287 done
1288 unset arglist
1289 unset prefixlist
1290 }
1291
1292 function mvfile ()
1293 {
1294 local nocheck=0
1295 [ "$1" = "-n" ] && nocheck=1 && shift
1296 split_arguments "$@"
1297 local -i i=0
1298 while [ "${arglist[$i]}" ]
1299 do
1300 if [ "$nocheck" = "0" ] && [ '!' -e "${arglist[$i]}" ] && [ '!' -h "${arglist[$i]}" ]
1301 then
1302 printf "\e[33mWarning\e[m: ${arglist[$i]} does not exist. (Use -n to override)\n"
1303 i=$i+1
1304 continue
1305 fi
1306 echo "Old name: ${arglist[$i]}"
1307 read -p "New name: " -e -i "${arglist[$i]}" new_file_name
1308 if [ "$new_file_name" ] && [ "${arglist[$i]}" != "$new_file_name" ]
1309 then
1310 history -s "$FUNCNAME" "$@"
1311 "${prefixlist[@]}" ${safe_mv_verbose} -i "${arglist[$i]}" "$new_file_name"
1312 history -s "${prefixlist[@]}" ${safe_mv_verbose} -i "${arglist[$i]}" "$new_file_name"
1313 fi
1314 i=$i+1
1315 done
1316 unset arglist
1317 unset prefixlist
1318 unset new_file_name
1319 }
1320
1321 function varset ()
1322 {
1323 local varoldvalue
1324 local varnewvalue
1325 while [ "$1" ]
1326 do
1327 eval varoldvalue=\${$1}
1328 read -r -e -p "$1=" -i "$varoldvalue" varnewvalue
1329 eval "$1"='"$varnewvalue"'
1330 shift
1331 done
1332 }
1333
1334
1335
1336
1337 function get_memory_info ()
1338 {
1339 case "$OSTYPE" in
1340 *linux*|*gnu*)
1341 local meminfoline="`free -m | safe_grep -i mem`"
1342 local swapinfoline="`free -m | safe_grep -i swap`"
1343 local memtotal="`echo "$meminfoline" | awk '{print $2}'`"
1344 local memused="`echo "$meminfoline" | awk '{print $3}'`"
1345 local membuf="`echo "$meminfoline" | awk '{print $6}'`"
1346 local memcache="`echo "$meminfoline" | awk '{print $7}'`"
1347 local memprog=$(($memused-$membuf-$memcache))
1348 local swaptotal="`echo "$swapinfoline" | awk '{print $2}'`"
1349 local swapused="`echo "$swapinfoline" | awk '{print $3}'`"
1350 echo "Memory: $memused / $memtotal MB (`printf %2d $(($memused*100/$memtotal))`%)"
1351 echo "Detail:"
1352 echo " Used: `printf %5d $memprog` MB (`printf %2d $(($memprog*100/$memtotal))`%)"
1353 echo " Buffers: `printf %5d $membuf` MB (`printf %2d $(($membuf*100/$memtotal))`%)"
1354 echo " Cached: `printf %5d $memcache` MB (`printf %2d $(($memcache*100/$memtotal))`%)"
1355 if [ "$swaptotal" = "0" ]
1356 then
1357 echo "Swap: not available"
1358 else
1359 echo "Swap: $swapused / $swaptotal MB (`printf %2d $(($swapused*100/$swaptotal))`%)"
1360 fi
1361 ;;
1362 *freebsd*|*FreeBSD*)
1363 local mempagesize="`sysctl -n hw.pagesize`"
1364 local mempagecount="`sysctl -n hw.availpages`"
1365 local memactive="`sysctl -n vm.stats.vm.v_active_count`"
1366 local meminactive="`sysctl -n vm.stats.vm.v_inactive_count`"
1367 local memwire="`sysctl -n vm.stats.vm.v_wire_count`"
1368 local memcache="`sysctl -n vm.stats.vm.v_cache_count`"
1369 local memfree="`sysctl -n vm.stats.vm.v_free_count`"
1370 local swapenabled="`sysctl -n vm.swap_enabled`"
1371 echo "Memory (Active): `printf %5d $(($memactive*$mempagesize/1048576))` MB (`printf %2d $(($memactive*100/$mempagecount))`%)"
1372 echo "Memory (Inactive): `printf %5d $(($meminactive*$mempagesize/1048576))` MB (`printf %2d $(($meminactive*100/$mempagecount))`%)"
1373 echo "Memory (Wired): `printf %5d $(($memwire*$mempagesize/1048576))` MB (`printf %2d $(($memwire*100/$mempagecount))`%)"
1374 echo "Memory (Cache): `printf %5d $(($memcache*$mempagesize/1048576))` MB (`printf %2d $(($memcache*100/$mempagecount))`%)"
1375 echo "Memory (Free): `printf %5d $(($memfree*$mempagesize/1048576))` MB (`printf %2d $(($memfree*100/$mempagecount))`%)"
1376 echo "Total Memory: `printf %5d $(($mempagecount*$mempagesize/1048576))` MB"
1377 if [ "$swapenabled" = "1" ]; then
1378 echo ""
1379 echo "Swap devices:"
1380 swapinfo -m
1381 else
1382 echo "Swap: not enabled"
1383 fi
1384 ;;
1385 *)
1386 echo "Unsupported operating system."
1387 ;;
1388 esac
1389 }
1390
1391
1392
1393
1394 function rpmdu () {
1395 local div_base=1
1396 local div_name="KB"
1397 local total=0
1398 if [ "`echo "$1" | cut -c 1`" = "-" ]
1399 then
1400 local optname=`echo "$1" | cut -c 2-`
1401 case "$optname" in
1402 "k")
1403 ;;
1404 "m")
1405 div_base=1024
1406 div_name=MB
1407 ;;
1408 "g")
1409 div_base=1048576
1410 div_name=GB
1411 ;;
1412 *)
1413 echo "Usage: $FUNCNAME [OPTION] package_name..."
1414 echo " -k KB"
1415 echo " -m MB"
1416 echo " -g GB"
1417 return 1
1418 ;;
1419 esac
1420 shift
1421 fi
1422
1423 while [ "$1" ]
1424 do
1425 rpm -ql "$1" | {
1426 while read oneline
1427 do
1428 if [ -f "$oneline" ]
1429 then
1430 du -k "$oneline"
1431 fi
1432 done
1433 } | {
1434 while read -d $'\t' filesize
1435 do
1436 total=$((${total}+${filesize}))
1437 read
1438 done
1439 printf "%9d %s %s\n" "$(($total/$div_base))" "$div_name" "$1"
1440 }
1441
1442 shift
1443 done
1444 }
1445
1446 function rpmsize () {
1447 local div_base=1
1448 local div_name="Bytes"
1449 local total=0
1450 local filesize
1451 if [ "`echo "$1" | cut -c 1`" = "-" ]
1452 then
1453 optname=`echo "$1" | cut -c 2-`
1454 case "$optname" in
1455 "b")
1456 ;;
1457 "k")
1458 div_base=1024
1459 div_name=KB
1460 ;;
1461 "m")
1462 div_base=1048576
1463 div_name=MB
1464 ;;
1465 "g")
1466 div_base=1073741824
1467 div_name=GB
1468 ;;
1469 *)
1470 echo "Usage: $FUNCNAME [OPTION] package_name..."
1471 echo " -b Byte"
1472 echo " -k KB"
1473 echo " -m MB"
1474 echo " -g GB"
1475 return 1
1476 ;;
1477 esac
1478 shift
1479 fi
1480 while [ "$1" ]
1481 do
1482 total=0
1483 filesize=`rpm -q "$1" --queryformat "%{SIZE} "`
1484 for i in $filesize
1485 do
1486 total=$((${total}+${i}))
1487 done
1488 printf "%12d %s %s\n" "$(($total/$div_base))" "$div_name" "$1"
1489 shift
1490 done
1491 }
1492
1493 function freebsd_ports_should_rebuild () {
1494 if [ -f "/var/db/pkg/local.sqlite" ]; then
1495 WITH_PKGNG="true"
1496 pkg_which_cmd="pkg which -q"
1497 else
1498 pkg_which_cmd="pkg_info -q -W"
1499 fi
1500 reqcomp=$(ldd -f '%a %o %p\n' \
1501 /usr/local/bin/* /usr/local/sbin/* \
1502 /usr/local/lib/* /usr/local/libexec/* \
1503 /usr/local/libexec/*/* \
1504 2>/dev/null | safe_grep 'not found' | \
1505 { while read oneline; do echo ${oneline} | cut -d ' ' -f 1; done; } | uniq)
1506 reqpkg=$({ for i in $reqcomp; do $pkg_which_cmd $i; done } | sort | uniq)
1507 echo $reqpkg
1508 }
1509
1510
1511
1512
1513 function check_important_files ()
1514 {
1515 important_files="$HOME/.screenrc $HOME/.vimrc"
1516 for i in $important_files
1517 do
1518 [ '!' -f "$i" ] && printf "\e[1;31mWarning\e[m: \e[1;33m$i\e[m does not exist.\n"
1519 done
1520 }
1521
1522 function prehistory_backup ()
1523 {
1524 echo "Checking your current history file"
1525 local -i currentcount="`wc -l < "$HISTFILE"`"
1526 currentcount="${currentcount/ */}"
1527 [ '!' -f "$historycountfile" ] && touch "$historycountfile"
1528 local -i previoushistorycount="$(< "$historycountfile")"
1529 if [ "$currentcount" -lt "$previoushistorycount" ]
1530 then
1531 printf "\e[1;31mWarning\e[m: Your $HISTFILE may be TRUNCATED OR OVERWRITTEN BY OTHER PROGRAMS!\n"
1532 printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n"
1533 echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed."
1534 echo " 1. Check your $HISTFILE."
1535 echo " 2. Edit your $HISTFILE manually if some unexpected changes are found."
1536 echo " (You may need $historybackupfile to do it) "
1537 echo " 3. Remove the file $historycountfile."
1538 echo " 4. Run the command \`prehistory_backup' again."
1539 return 3
1540 fi
1541 echo -n "Backing up your current history file ($previoushistorycount -> $currentcount, "
1542 if [ "$previoushistorycount" = "$currentcount" ]
1543 then
1544 echo "no modification)"
1545 else
1546 echo "+$[$currentcount-$previoushistorycount])"
1547 fi
1548 echo "$currentcount" > "$historycountfile"
1549 safe_cp -f "$HISTFILE" "$historybackupfile"
1550 }
1551
1552
1553
1554
1555 function chr ()
1556 {
1557 printf $(printf '\\%03o\\n' "$1")
1558 }
1559
1560 function hex ()
1561 {
1562 printf "0x%02x\n" "$1"
1563 }
1564
1565 function ord ()
1566 {
1567 printf "%d 0x%02x 0%03o\n" "'$1" "'$1" "'$1"
1568 }
1569
1570 function argv0 ()
1571 {
1572 local execname="$1"
1573 local argv0="$2"
1574 shift 2
1575 ( exec -a "$argv0" "$execname" "$@" )
1576 }
1577
1578 function compile_all ()
1579 {
1580 local noask=0
1581 local mycc="${CC}"
1582 local mycxx="${CXX}"
1583 local myexe="`get_executable_extension`"
1584 local newCFLAGS
1585 local newCXXFLAGS
1586 local newLDFLAGS
1587 [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1
1588 [ "$1" = "-n" ] && noask=1
1589 if [ "$noask" = "0" ]; then
1590 read -e -p "CFLAGS: " -i "$CFLAGS" newCFLAGS
1591 read -e -p "CXXFLAGS: " -i "$CXXFLAGS" newCXXFLAGS
1592 read -e -p "LDFLAGS: " -i "$LDFLAGS" newLDFLAGS
1593 [ "$newCFLAGS" '!=' '' ] && CFLAGS=$newCFLAGS
1594 [ "$newCXXFLAGS" '!=' '' ] && CXXFLAGS=$newCXXFLAGS
1595 [ "$newLDFLAGS" '!=' '' ] && LDFLAGS=$newLDFLAGS
1596 else
1597 shift
1598 fi
1599 [ -z "${mycc}" ] && mycc=cc
1600 [ -z "${mycxx}" ] && mycxx=c++
1601 while [ "$1" '!=' '' ]
1602 do
1603 local targetfile="`echo "$1" | sed 's|\(.*\)\..*|\1|'`$myexe"
1604 local suffix="`echo "$1" | sed 's|.*\.\(.*\)|\1|'`"
1605 if [ -f "$1" ]; then
1606 true
1607 else
1608 printf \
1609 "\e[1;33mWarning\e[0m: $1 Non-existent file or not a regular file\n"
1610 shift ; continue
1611 fi
1612 [ "$targetfile" = "$1" ] && shift && continue
1613 case "$suffix" in
1614 c)
1615 echo "[${mycc}] $1 -> $targetfile"
1616 ${mycc} $CFLAGS "$1" $LDFLAGS -o "$targetfile"
1617 ;;
1618 cpp|CPP|cp|cxx|cc|c++|C)
1619 echo "[${mycxx}] $1 -> $targetfile"
1620 ${mycxx} $CXXFLAGS "$1" $LDFLAGS -o "$targetfile"
1621 ;;
1622 *)
1623 printf "$1: Unknown suffix (\e[1;33mskipped\e[0m)\n"
1624 ;;
1625 esac
1626 [ "$?" '!=' "0" ] && printf \
1627 '\e[1;31mError\e[0m while compiling file\n'
1628 shift
1629 done
1630 return 0
1631 }
1632
1633 function cc_define ()
1634 {
1635 local -i i
1636 local mycpp="${CPP}"
1637 if [ -z "${mycpp}" ]; then
1638 if [ -z "${CC}" ]; then
1639 mycpp="cpp"
1640 else
1641 mycpp="${CC} -E"
1642 fi
1643 fi
1644
1645 split_arguments "$@"
1646
1647 {
1648 (( i = 0 ))
1649 while [ "${prefixlist[$i]}" ]; do
1650 echo "#include <${prefixlist[$i]}>"
1651 (( i++ ))
1652 done
1653 (( i = 0 ))
1654 while [ "${arglist[$i]}" ]; do
1655 echo "${arglist[$i]}"
1656 (( i++ ))
1657 done
1658 } | ${mycpp} - | tail -n "${#arglist[@]}"
1659 unset arglist
1660 unset prefixlist
1661 }
1662
1663 function cxx_define ()
1664 {
1665 CPP="${CXXCPP}" CC="${CXX:-c++ -x c++}" cc_define "$@"
1666 }
1667
1668
1669
1670
1671 function repeat ()
1672 {
1673 local repeat_times="$1"
1674 shift
1675 for ((i=0; i<repeat_times; i++))
1676 do
1677 "$@"
1678 done
1679 }
1680
1681 function wait_success ()
1682 {
1683 local i=1
1684 until "$@"; do echo "Failed ... $i"; ((i++)) ; done
1685 }
1686
1687
1688
1689
1690 function keep_sudo_credential ()
1691 {
1692 if [ "$1" ]
1693 then
1694 update_sudo_interval="$1"
1695 else
1696 update_sudo_interval="280"
1697 fi
1698 while true
1699 do
1700 sudo -v
1701 sleep "$update_sudo_interval"
1702 done
1703 }
1704
1705
1706
1707
1708 function get_terminal_size ()
1709 {
1710
1711
1712
1713
1714
1715 echo -n $'\e7\e[r\e[999;999H\e[6n\e8' 1>&2
1716 read -s -d R getsize
1717 echo $getsize | sed 's#..\([0-9]*\);\([0-9]*\)#LINES=\1 COLUMNS=\2#'
1718 }
1719
1720 function set_terminal_size ()
1721 {
1722 eval "export `get_terminal_size`"
1723 stty cols $COLUMNS rows $LINES
1724 }
1725
1726 function set_console_title ()
1727 {
1728 case "$TERM" in
1729 screen*)
1730 printf "\033]0;"
1731 echo -n "$*"
1732 printf "\033\\"
1733 ;;
1734 xterm*)
1735 printf "\033]0;"
1736 echo -n "$*"
1737 printf "\007"
1738 ;;
1739 *)
1740 echo "Your terminal may not have the hardstatus line."
1741 echo "Note: TERM=$TERM"
1742 ;;
1743 esac
1744 }
1745
1746
1747
1748
1749 function convert_to_html ()
1750 {
1751 while [ "$1" '!=' '' ]
1752 do
1753 for i in "$1"
1754 do
1755 vim $i -c 'set background=dark' \
1756 -c 'highlight PreProc ctermfg=darkcyan' \
1757 -c "$BEFORE_CONVERT_TO_HTML" \
1758 -c "$BEFORE_CONVERT_TO_HTML1" \
1759 -c "$BEFORE_CONVERT_TO_HTML2" \
1760 -c TOhtml \
1761 -c :w \
1762 -c :qa
1763 done
1764 shift
1765 done
1766 }
1767
1768
1769
1770
1771 alias helpf='help_function'
1772 alias helpm='help_myself'
1773 alias helpa='help_aliases'
1774 alias help_aliases='help_function'
1775
1776 function print_iconv ()
1777 {
1778 [ "$1" = "$2" ] && cat && return 0
1779 iconv -f "$1" -t "$2"
1780 }
1781
1782 function help_myself ()
1783 {
1784 echo "argc = $#"
1785 echo "argv[0] = $0"
1786 i=1
1787 while [ "$1" ]
1788 do
1789 echo "argv[$i] = $1"
1790 i=$(($i+1))
1791 shift
1792 done
1793 }
1794
1795 function help_function ()
1796 {
1797 [ "$#" = "0" ] && {
1798 cat << "ENDHELPMSG"
1799 <<< Help >>>
1800 help_myself [arguments ...] (helpm)
1801 help_function [functions ...] (helpf)
1802 help_aliases (helpa)
1803 help_obsolete
1804 x print_iconv
1805
1806 <<< Group: Background Tasks >>>
1807 bgrun command [arguments ...] (bgr)
1808 bglist [--full] (bgl, bgls)
1809 bgview [number] (bgv)
1810 bgclean [all | numbers ...] (bgrm)
1811 bgcount (bgc)
1812 bgdu
1813
1814 <<< Group: Configuration Files >>>
1815 configfile_fetch [git_tag [file_name_list ...]]
1816 configfile_initial_setup
1817 x fetch_remote_file local_file_name remote_url
1818 x fetch_and_merge local_file_name remote_url
1819
1820 <<< Group: New PATH Editor >>>
1821 path_editor [variable]
1822 ldpath_editor
1823 x newpath_init
1824 x newpath_gen
1825
1826 <<< Group: Trash Manager >>>
1827 trash_mv [filenames ...] [-- sudo_prefix ...] (trash_put, trash_add)
1828 trash_ls (trash_list)
1829 trash_cd number
1830 trash_pushd number
1831 trash_recover numbers ... [-- sudo_prefix ...] (trash_restore, trash_rc)
1832 trash_rm numbers ... (trash_drop, trash_clean)
1833 trash_count (trash_ct)
1834 trash_du [-- sudo_prefix ...]
1835
1836 <<< Tools: Background Notify Daemon >>>
1837 check_dmesg seconds
1838 check_system_status seconds
1839
1840 <<< Tools: Backup >>>
1841 backup_file filename ... [-- sudo_prefix ...]
1842
1843 <<< Tools: Compatibility >>>
1844 fbterm_chewing [size] [arguments ...]
1845 gen_ms_inet_shortcut filename url
1846 unzip_nomac filenames ...
1847
1848 <<< Tools: GNU Screen >>>
1849 mkscreenacl usernames ...
1850
1851 <<< Tools: Interactive >>>
1852 editlink filenames ... [-- sudo_prefix ...]
1853 mvfile [-n] filenames ... [-- sudo_prefix ...]
1854 varset variables ...
1855
1856 <<< Tools: Memory >>>
1857 get_memory_info
1858
1859 <<< Tools: Packages >>>
1860 rpmdu [-kmg] packages ...
1861 rpmsize [-bkmg] packages ...
1862 freebsd_ports_should_rebuild
1863
1864 <<< Tools: Personal Files >>>
1865 check_important_files
1866 prehistory_backup
1867
1868 <<< Tools: Programming >>>
1869 chr number
1870 hex number
1871 ord character
1872 argv0 executable arguments ... (include argv[0])
1873 compile_all [-n] filenames ...
1874 cc_define macro [-- included_headers ...]
1875 cxx_define macro [-- included_headers ...]
1876
1877 <<< Tools: Repeated Tasks >>>
1878 repeat times arguments ...
1879 wait_success arguments ...
1880
1881 <<< Tools: Security >>>
1882 keep_sudo_credential [seconds]
1883
1884 <<< Tools: Terminal >>>
1885 get_terminal_size
1886 set_terminal_size
1887 set_console_title
1888
1889 <<< Tools: Web >>>
1890 convert_to_html filename ...
1891
1892 <<< Shared Internal Functions >>>
1893 x createdir_askmode dirname
1894 x check_command_existent program
1895 x is_file_type filename type [-- sudo_prefix ...]
1896 x get_file_size filename [-- sudo_prefix ...]
1897 x get_executable_extension
1898 x split_arguments [arguments ...]
1899
1900 <<< Aliases: Command Prompt >>>
1901 startcolor - Enable colorful PS1 prompting
1902 stopcolor - Disable colorful PS1 prompting
1903
1904 <<< Aliases: Git >>>
1905 git_log - Show git log in a more compact format
1906 git_log_color - Show git log in a more colorful format
1907
1908 <<< Aliases: GNU Screen >>>
1909 screen256 - Start GNU Screen and set TERM to screen-256color
1910
1911 <<< Aliases: Language >>>
1912 big5 - Set Language and character sets to Taiwan Chinese Big5
1913 cccc - Set Language and character sets to 7-bit ASCII
1914 enus - Set Language and character sets to US English UTF-8
1915 zhtw - Set Language and character sets to Taiwan Chinese UTF-8
1916
1917 <<< Aliases: Nice Format >>>
1918 ndate - Format the output of `date'
1919 npasswd - Format the output of `getent passwd'
1920 ngroup - Format the output of `getent group'
1921
1922 <<< Aliases: Terminal >>>
1923 savetty - Save current terminal mode
1924 resetty - Reset to last saved terminal mode
1925
1926 <<< Aliases: Vim >>>
1927 vimhtml - Start Vim and set tabstop and shiftwidth to 2
1928 ENDHELPMSG
1929 } | $PAGER && return 0
1930 local current_charset=`echo "$LC_ALL" | cut -d . -f 2`
1931 local -i i
1932 while [ "$1" ]
1933 do
1934 case "$1" in
1935 help_myself|helpm)
1936 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1937 help_myself
1938 一個測試命令列的小函式
1939 ENDHELPMSG
1940 ;;
1941 help_function|helpf)
1942 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1943 help_function
1944 顯示 .bash_include 提供的額外函式清單
1945 註:前方加上「x」符號者表示此為內部使用的函式,不宜直接使用
1946 ENDHELPMSG
1947 ;;
1948 bgrun|bgr)
1949 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1950 bgrun command [arguments ...]
1951 執行指令 command 並將輸出導入檔案
1952 註:此函式會自動以目前時間和指令名稱為檔案命名
1953 ENDHELPMSG
1954 ;;
1955 bglist|bgl|bgls)
1956 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1957 bglist [--full]
1958 列出所有使用 bgrun 執行的指令
1959 若加上 --full 選項,則可同時察看時間
1960 ENDHELPMSG
1961 ;;
1962 bgview|bgv)
1963 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1964 bgview [number]
1965 顯示以 bgrun 執行指令的輸出,若省略 number,表示是最近一次執行的指令
1966 若 number > 0,
1967 表示第 number 個指令 (此數值可由 bglist 函式取得)
1968 若 number <= 0,
1969 表示第「指令總數-number」個指令 (指令總數可由 bgcount 函式取得)
1970 ENDHELPMSG
1971 ;;
1972 bgclean|bgrm)
1973 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1974 bgclean [all | numbers ...]
1975 bgclean all 可清除所有指令的輸出檔
1976 bgclean 3 5 7 10 表示清除第 3、5、7、10 個指令輸出檔 (編號可由 bglist 取得)
1977 ENDHELPMSG
1978 ;;
1979 bgcount|bgc)
1980 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1981 bgcount
1982 顯示指令輸出檔總數
1983 ENDHELPMSG
1984 ;;
1985 bgdu)
1986 cat << "ENDHELPMSG" | print_iconv "UTF-8" "$current_charset"
1987 bgdu
1988 顯示每個指令輸出檔的檔案大小 (單位:KB)
1989 ENDHELPMSG
1990 ;;
1991 *)
1992 echo "Help message for $1 is not found"
1993 ;;
1994 esac
1995 shift
1996 done
1997 }
1998
1999 function help_obsolete ()
2000 {
2001 cat << "ENDHELPMSG"
2002 @@@ Obsolete Group: PATH Editor @@@
2003 old_path_editor
2004 old_ldpath_editor
2005 x old_path_editor_core
2006 x split_path
2007 x split_path_core
2008 x update_path
2009 ENDHELPMSG
2010 }
2011
2012
2013
2014
2015 umask 0022
2016
2017 if [ "$interactive_shell" ]
2018 then
2019 echo "Running interactive shell configuration"
2020 check_important_files
2021 startcolor
2022 prehistory_backup
2023 bind '"\e[A":history-search-backward'
2024 bind '"\e[B":history-search-forward'
2025 if [ -e "$HOME/.bash_title" ]; then
2026 case "$TERM" in
2027 xterm*)
2028 PROMPT_COMMAND="$PROMPT_COMMAND"'; printf "\033]0;%s@%s:%s (%s)\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
2029 ;;
2030 screen*)
2031 PROMPT_COMMAND="$PROMPT_COMMAND"'; printf "\033]0;%s@%s:%s (%s)\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}" "`date "+%H:%M:%S"`"'
2032 ;;
2033 esac
2034 fi
2035 [ "$TERM" = xterm ] && TERM=xterm-256color
2036 [ "$TERM" = screen ] && TERM=screen-256color
2037 fi
2038
2039 [ "$interactive_shell" ] && echo "Setting shell options, completions, limits"
2040
2041 shopt -s histappend
2042 shopt -s checkwinsize
2043 shopt -s checkjobs
2044 shopt -s checkhash
2045 shopt -s cmdhist
2046 shopt -s mailwarn
2047
2048 complete -A alias helpa
2049 complete -A alias help_aliases
2050 complete -A command check_command_existent
2051 complete -A directory createdir_askmode
2052 complete -A function helpf
2053 complete -A function help_function
2054 complete -A variable varset
2055 complete -A variable path_editor
2056 complete -A user mkscreenacl
2057
2058 if check_command_existent _command; then
2059 complete -F _command wait_success
2060 fi
2061
2062 if check_command_existent _screen; then
2063 complete -F _screen screen256
2064 fi
2065
2066 if check_command_existent _rpmdev_installed_packages; then
2067 complete -F _rpmdev_installed_packages rpmdu
2068 complete -F _rpmdev_installed_packages rpmsize
2069 fi
2070
2071 ulimit -S -c unlimited 2> /dev/null
2072
2073 [ "$interactive_shell" ] && {
2074 if [ "$WINDOW" ] && type screen &> /dev/null; then
2075 if [ "`screen --version | sed 's/^Screen version 4\.\([0-9]*\).*$/\1/'`" -ge "1" ]; then
2076 echo "Setting options for GNU screen >= 4.1.0"
2077 screen -X cjkwidth off
2078 fi
2079 fi
2080 }
2081
2082 [ "$interactive_shell" ] && {
2083 echo "Done"
2084 if [ "$UID" = "0" ] || [ "$EUID" = "0" ]
2085 then
2086 printf "\nNote: You may be \e[1;32mprivileged\e[m now!\n\n"
2087 fi
2088 }