aboutsummaryrefslogtreecommitdiffstats
path: root/bash_include
blob: 99936d067b8e197f72aedaa8f4d8464d358d1666 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/bin/bash
#
# --- UTF-8 ---
#
# 檔案製作者:lantw44
# 更新日期:2011/11/28
#

echo "Running .bash_include"

# Internal Variables

default_tty_setting=`stty -g`

colorprompting='\[\e[1;31m\]\!\[\e[m\] [\[\e[1;33m\]\u\[\e[m\]@\[\e[1;32m\]\h\[\e[m\] \[\e[1;36m\]\w\[\e[m\]]\$ '
nocolorprompting='\! [\u@\h \w]\$ '

historycountfile="$HOME/.bash_history.count"
historybackupfile="$HOME/.bash_history.bak"
bgrunfiledir="$HOME/tmp/bgrun"

HISTSIZE=1000000
HISTFILESIZE=1000000
HISTCONTROL=ignoredups:ignorespace




# Environmental Variables

export EDITOR=vim
export FCEDIT=vim
export PAGER=less




# Aliases

alias startcolor='PS1=$colorprompting'
alias stopcolor='PS1=$nocolorprompting'

alias ll='ls -l'
alias grep='grep --color=always'
alias rm='rm -i'
alias cp='cp -pi'
alias mv='mv -i'
alias jobs='jobs -l'
alias less='less -RS'

alias cccc='LANG=C;LC_ALL=C'
alias enus='LANG=en_US.UTF-8;LC_ALL=en_US.UTF-8'
alias big5='LANG=zh_TW.Big5;LC_ALL=zh_TW.Big5'
alias zhtw='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8'
alias utf8='LANG=zh_TW.UTF-8;LC_ALL=zh_TW.UTF-8'

alias savetty='default_tty_setting=`stty -g`'
alias resetty='stty $default_tty_setting'




# Functions

function compile_all ()
{
    noask=0
    [ "$1" = '' ] && echo "Which file(s) do you want to compile? " && return 1
    [ "$1" = "-n" ] && noask=1
    if [ "$noask" = "0" ]; then
        read -p "CFLAGS [$CFLAGS] ? " NEWCFLAGS
        read -p "LDFLAGS [$LDFLAGS] ? " NEWLDFLAGS
        [ "$NEWCFLAGS" '!=' '' ] && CFLAGS=$NEWCFLAGS
        [ "$NEWLDFLAGS" '!=' '' ] && LDFLAGS=$NEWLDFLAGS
    else
        shift
    fi
    while [ "$1" '!=' '' ]
    do
        TARGETFILE="`echo "$1" | cut -d . -f 1`"
        SUFFIX="`echo "$1" | cut -d . -f 2`"
        if [ -f "$1" ]; then
            true
        else
            printf\
            '\e[1;33mWarning\e[0m: Non-existent file or not a regular file\n'
            shift ; continue
        fi
        [ "$TARGETFILE" = "$1" ] && shift && continue
        if [ "$SUFFIX" = "c" ]; then
            echo "[CC] $1 -> $TARGETFILE"
            gcc $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
        elif [ "$SUFFIX" = "cpp" ]; then
            echo "[CXX] $1 -> $TARGETFILE"
            g++ $CFLAGS "$1" $LDFLAGS -o "$TARGETFILE"
        else
            printf 'Unknown suffix (\e[1;33mskipped\e[0m)\n'
        fi
        [ "$?" '!=' "0" ] && printf\
            '\e[1;31mError\e[0m while compiling file\n'
        shift
    done
    return 0
}


function convert_to_html ()
{
    while [ "$1" '!=' '' ]
    do
        for i in "$1"
        do 
            vim $i -c 'set background=dark' \
                -c 'highlight PreProc ctermfg=darkcyan' \
                -c "$BEFORE_CONVERT_TO_HTML" \
                -c TOhtml \
                -c :w \
                -c :qa 
        done
        shift
    done
}

function mkscreenacl ()
{
    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"
    while [ "$1" '!=' '' ]
    do
        for i in $PERMIT_COMMAND
        do
            echo "aclchg $1 +x $i"
        done
        echo "aclchg $1 -rw \"#?\"" 
        shift
    done
}

########## Background ##########

function bgrun ()
{
    [ "$#" = "0" ] && return 1
    [ '!' -d "$bgrunfiledir" ] && mkdir -p "$bgrunfiledir"
    current_time=`date "+%Y%m%d-%H%M%S"`
    filename="$bgrunfiledir/$1-$current_time"
    echo "Writing to $filename"
    "$@" &> "$filename" &
}

function bgview ()
{
    if [ "$1" = "" ]
    then
        yourchoice=1
    fi
    rootfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | sort -r | sed -n ${yourchoice}p`
    realfilename=`find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | grep "$rootfilename"`
    if [ "$PAGER" = "" ]
    then
        PAGER=less
    fi
    read -p "View '$realfilename' ? " confirm
    if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]
    then
        return 1
    fi
    "$PAGER" "$realfilename"
}

function bgcount ()
{
    find "$bgrunfiledir" -maxdepth 1 -mindepth 1 | cut -d - -f 2,3 | wc | awk '{print $2}'
}

########## Background ##########

function check_dmesg ()
{
    [ "$#" = "0" ] && return 1

    while true
    do
        PREVIOS_DMESG_BUF="$DMESG_BUF"
        DMESG_BUF="`dmesg`"
        [ "$PREVIOS_DMESG_BUF" '!=' "$DMESG_BUF" ] && [ "$FIRST_RUN" = "0" ] && echo '===> system message buffer was modified <==='
        sleep $1
        [ "$?" '!=' "0" ] && return 1
        FIRST_RUN=0
    done 
}

function prehistory_backup ()
{
    echo "Running prehistory_backup"
    currentcount=`wc "$HISTFILE" | awk '{print $1}'`
    [ '!' -f "$historycountfile" ] && touch "$historycountfile"
    declare -i previoushistorycount
    previoushistorycount=`cat "$historycountfile"`
    if [ "$currentcount" -lt "$previoushistorycount" ]
    then
        printf "\e[1;31mWarning\e[m: Your $HISTFILE may be MODIFIED BY OTHER PROGRAMS!\n"
        printf "Note: \e[1;33m$currentcount\e[m < $previoushistorycount\n"
        echo "Your $historycountfile and $historybackupfile will not be overwritten until this problem is fixed."
        echo " 1. Check your $HISTFILE."
        echo " 2. Edit your $HISTFILE manually if some unexpected changes are found." 
        echo "    (You may need $historybackupfile to do it) "
        echo " 3. Remove the file $historycountfile."
        echo " 4. Run the command \`prehistory_backup' again."
        return 1
    fi
    echo "$currentcount" > "$historycountfile"
    cp -f "$HISTFILE" "$historybackupfile"
}




# Starting doing something

startcolor
prehistory_backup

umask 0022

tty -s
if [ "$?" = "0" ]
then
    bind '"\e[A":history-search-backward'
    bind '"\e[B":history-search-forward'
fi

shopt -s histappend
shopt -s checkwinsize
shopt -s checkjobs
shopt -s checkhash
shopt -s cmdhist
shopt -s mailwarn

echo "Done"

# check_dmesg.sh 5 &