Archive for the '電腦技術' Category

Screen + Unicode 補完計畫 (UAO)

這篇其實很早就有草稿了,只是拖到最近才把它完成。

之前有談過如何設定 Gnu Screen 的方法,如果你常掛在 server 上,那應該對 Screen 這個好用的工具並不陌生。這篇主要是談怎麼 patch 它,來讓它支援 Unicode 補完計畫 (UAO)。

Unicode 補完計畫 (UAO) 對廣大的鄉民來說,可以說是再親切不過的了,BBS 電子佈告欄在台灣相當地盛行,但它底層僅僅支援 Big5,對於鄉民來說,看日文是件再正常不過的事了,但 Big5 畢竟是個急就章推出的編碼 (事實上,它也沒有一個既定的標準,唯一勉強可以稱得上標準的是 Big5-2003),在當初的設計中並沒有加入日文假名等使用需求。所幸 Big5 有所謂的”使用者造字區”,提供給使用者自行定義新字,也有了後來的”倚天擴充字集”加入了包括日文假名等使用者特殊需求字。

然而,Windows 對於這塊由倚天自行定義的區域,在轉換成 Unicode 時也對應到了使用者造字區,而非它們各自對應到實際在 Unicode 的字,於是造成了 mapping 上的錯誤。UAO 便是為了解決這個問題而產生的,它做的事情只是將這段使用者造字區對應到 Unicode 正確的字。

上面說到的,是針對 Big5 <-> Unicode 的轉換發生在 Windows 的情況,可以利用安裝 UAO 來解決;但因為我的環境是 UTF-8,已經習慣用 Screen 提供的 Encoding Translation 功能來上 BBS 瀏覽文章了,但 Screen 預設提供的 Mapping Table 並沒有處理 UAO,因此這種轉換是在 Screen 內部的情況便無法靠安裝 UAO 來解決,所以必須 patch。

其實許多前輩們已經針對這個部份提供了 Screen 的 UAO patch 以及修正過的 Mapping Table,但他們提供的 Mapping Table 是以 UAO 2.42 所產生的 Binary 檔,這意味著我不清楚它是支援到 UAO 2.42 哪個程度,即使完整支援了 (這反而不好),Binary 檔也代表它難以再編修和進 Version Control System。

所以我從 Firefox 3 的版本取出了內建的 Mapping Table 文字檔來做處理,Firefox 在 2 的版本已經內建單向處理 UAO 的功能了,所以可以取其 Big5 -> Unicode 單向的 Mapping Table 來做修改。修改後再利用轉換程式編成 Binary 檔便可替代原本 Screen 當中的 “18″ 檔 (也就是 Big5 <-> Unicode Mapping Table)。

利用修正過後的 Screen 便可看到在日文假名以及某些特殊字框的顯示正常很多了;但是實際使用後會發現一個問題,就是對於某些 Double Mapping 的字會優先轉換到 0×8140-0xA0FE 這段擴充區 (這裡所謂的 Double Mapping 是指多個 Big5 的字對應到同一個 Unicode 的字,例如 0xB86D 和 0×82AA 都是對應到 0×7F6E,一個是正常區的,另一個是擴充區的),而在當初的 Mapping Table 文字檔便是依造 Big5 的字碼來排序,很不巧的 0×8140-0xA0FE 這段擴充區會放置在最前面;而又剛好 Screen 的 Mapping Table 是同時做雙向轉換的,針對這種 Double Mapping 的情況,它會以第一個找到的為優先,這便造成了某些不需 UAO 便可閱讀的字,變成需要 UAO 才能閱讀了,而這篇文章是你打的。解決方式便是將這段擴充區移到 Mapping Table 的最後面,讓正常區的字優先權較高。於是最後的 Mapping Table 表便誕生了,我在每行最後都有加上 UTF-8 所對應到的字。

最後的比較圖如下:
Screen_with_different_mapping_table

參考資料:

P.S. 其實 UAO 在本意上是良好的,但是作法我不太推崇,畢竟它不是一個標準,只是為了配合 BBS 所產生的過渡產物,所以讓 Screen 支援 UAO 僅是為了讓瀏覽 BBS 文章更方便而已,在其他環境下,我還是建議以 Unicode 為主,畢竟它是一個標準,支援度又高,Big5 就讓它慢慢走向歷史吧!

The Tips of Setting Workstation Environment

這份投影片是之前在系計中內部分享的,整理了一下 share 出來,裡面有些嘴砲請自動略過 XD。有錯也請指正 :)

Colorize A 256-Color Mutt

Mutt 是一個我很愛用的 Mail Client,除了平常在用的 GMail 外,其他幾乎都是由它包辦,其實之前就知道 Mutt 支援 256-Color,只是一直沒有時間好好調校,昨天趁著颱風天的下午,在 FreeBSD 上把 Mutt 的 256-Color 設定好了。

在預設的 Mutt color setting 中,單純使用常用的 16-Color 就是用 black, green, red, blue, …, etc 這類的顏色詞來設定;但是在 256-Color 的環境下,必須使用 color0, color1, …, color254, color255 來做設定,關於顏色的對照表,可以參考這邊

這邊提供一下我的設定 (看是要放到 $HOME/.muttrc 或是另放一個檔案,再從 $HOME/.muttrc include 進去):

color normal            default         default         # normal text
color indicator         color214        color237        # actual message
color tree              color99         default         # thread arrows
color status            color118        color237        # status line
color error             color196        default         # errors
color message           color196        default         # info messages
color signature         brightblack     default         # signature
color attachment        brightblack     default         # MIME attachments
color search            brightyellow    red             # search matches
color tilde             brightmagenta   default         # ~ at bottom of msg
color markers           red             default         # + at beginning of wrapped lines

color hdrdefault        color33         default         # default header lines
color bold              red             default         # hiliting bold patterns in body
color underline         green           default         # hiliting underlined patterns in body

color quoted            color107        default         # quoted text
color quoted1           color66         default
color quoted2           color32         default
color quoted3           color30         default
color quoted4           color99         default
color quoted5           color36         default
color quoted6           color114        default
color quoted7           color109        default
color quoted8           color41         default
color quoted9           color138        default
# header
color header            color205        default         "^(From|Subject|To|Cc|Bcc):"

# body
color body              color214        default         "(http|https|ftp|news|telnet|finger)://[^ ]+"
color body              color81         default         "[-a-z_0-9.+]+@[-a-z_0-9.]+"
color body              red             default         "(^| )\\*[-a-z0-9*]+\\*[,.?]?[ \n]"
color body              green           default         "(^| )_[-a-z0-9_]+_[,.?]?[ \n]"

# index
uncolor index *         # unset all color index entries
color index             brightgreen     default         ~F      # Flagged
color index             color74         default         ~N      # New
color index             color169        default         ~T      # Tagged
color index             brightblack     default         ~D      # Deleted

這樣設定完成後,如果你跟我一樣使用 $TERM=xterm,沒意外的話會噴出這樣子的訊息:

Error in /home/yzlin/.mutt/color/color256, line 9: 214: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 10: 99: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 11: 118: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 12: 196: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 13: 196: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 20: 33: color not supported by term
Error in /home/yzlin/.mutt/color/color256, line 24: 107: color not supported by term
...

會出現這樣的問題,是因為 Mutt 使用到 ncurses,會判斷 termcap 中,你所使用的 TERM (在此是 xterm) 是否支援 256-Color,如果不支援那麼高,就會噴出這樣子的訊息,這時候有人就會把 TERM 設成 xterm-256color,如此一來,就會成功!?很遺憾的,照樣噴 XD,在 Linux 下,可以單純只設成 xterm-256color,但是在 FreeBSD 下,卻不行!

為何會有如此的差異?其實這個問題帶有一點點歷史的包袱,FreeBSD 預設使用的是 termcap,而 Linux 多半使用的是 terminfo,也因為如此,FreeBSD termcap 會受限於單一 entry 字元數必須小於 1024 個的限制,如此一來,沒辦法將全部的 feature 都包含進去,xterm-256color 就是一個例子,在 /usr/share/misc/termcap 中有一段說明:

# These aliases are for compatibility with the terminfo; termcap cannot provide
# the extra features, but termcap applications still want the names.
xterm-16color|xterm alias 1:tc=xterm-xfree86:
xterm-88color|xterm alias 2:tc=xterm-256color:
xterm-256color|xterm alias 3:tc=xterm-xfree86:

從說明可以看到 xterm-256color 其實引入的是 xterm-xfree86,再詳細去看,xterm-xfree86 當中並沒有 Co 的設定 (Co 指的是 “Maximum number of colors on screen”,相當於 terminfo 中的 colors),而是引入 xterm-basic 的設定,當中只支援 Co#8,這也是為什麼 xterm-256color 在 FreeBSD 中並不是真的支援 256 色。

若要知道目前使用的 TERM 所支援的 colors,可以使用:

tput Co

注意不是 co 而是 Co,co 是 columns 的意思。

既然無法針對 termcap 下手,那究竟該如何設定使用 Co#256 呢?好在我們可以自行設定一個環境變數 TERMCAP,來強制指定,在 $HOME/.cshrc 設定:

setenv TERMCAP 'xterm|xterm-color:Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm:tc=xterm-xfree86:'

由上面的設定,可以看到我們覆寫了 Co、AB、AF,這樣的設定跟 .screenrc 當中的設定很像,AF 指的是 foreground color 的表示方式;AB 則是 backgound color 的表示方式。設定完成如果要確定是否成功,可以利用 tput 去檢查 Co,出現 256 便是表示成功了,接著在這樣的環境下執行 Mutt,沒意外會看到這樣子的畫面:
mutt-256color

Zsh - autocomplete error

最近開始在嘗試一套蠻強大的 shell - zsh,我用過的 shell 沒有很多,最常用的還是 tcsh & sh,bash 以前有用過一陣子。試用了一陣子,覺得它真的蠻強的,可以把 prompt 的介面和 auto-complete 的功能弄得很炫。改天再發篇文寫一下自己的設定心得。

這篇主要是講一下之前遇到的一個問題,在 auto-complete 的設定下,會出現:

_alternative:69: command not found: _canonical_paths

雖然好像可以正常運作,但就是覺得怪怪的,後來 google 了一下,發現了別人的解法,原來是 $HOME/.zcompdump 爛掉,把它砍掉,讓 zsh 自動重建就行了。

Make Your VIM & Screen Environment Comfortable

這份 slide 是之前在 Y 社內部 Intern Sharing 所準備的,算是小技巧大集合,分享出來給大家,希望會有幫助。

裡面提到的大多都是建立在「我所認為舒服的環境」的前提上所給的技巧和設定;每個人有每個人認為舒服的設定,這是很主觀的,所以純粹只是當參考。

讓 pure-ftpd 支援 smbfs

pure-ftpd 要能支援 smbfs 這些比較特殊的 File System,必須在 configure 的時候加入 “–without-sendfile” 這個參數,讓它不使用 sendfile 這個 System Call,在 FreeBSD ports 裡,只需要將預設的 Option “SENDFILE       Support for the sendfile syscall” 取消即可。下面是官方的說明:

--without-sendfile: on Linux, Solaris, HPUX and FreeBSD kernels, Pure-FTPd
tries to reduce the CPU/memory usage by using a special system call (sendfile)
. It works very well with most filesystems. However, this optimization is not
implemented for all filesystems in current kernels. Users reported that
downloading files with Pure-FTPd failed with SMBFS (Samba) on FreeBSD and
TmpFS and NTFS on Linux (the error reported by the server is "broken pipe" or
"Error during write to data connection") . If you are planning to serve files
from these filesystems, you have to use the --without-sendfile switch to
enable a workaround. It was also reported that PA-Risc Linux systems need this
flag.

portconf new feature “.undef” 進 ports 了

之前 po 的那篇幫 portconf 加新功能的,後來聽了 gslin 的建議送了 PR 給作者,剛剛收到 reply mail 通知已經 commit 了。

題外話,最近陸續送了好幾個 PR,也慢慢學習到 ports 的正確包法,對 FreeBSD 的 ports 機制也更了解了,能對 FreeBSD 做出貢獻真的很棒!

Update: 看了一下作者最後的作法,改成了只動到 portconf.sh,主要在下面這行:

echo ${_line#*:} | sed -E 's/([A-Z0-9_]+)(=([^|]+))?/\1=\3/g;s/!([A-Z0-9_]+)=([^|]+)?/.undef \1/g;s/ *\| */|/g;s/ /%/g'

果然高明多了。

portconf 中的 .undef 功能

ports-mgmt/portconf 是個很棒的工具,它的用法就不多說了,google 一下都可以找到很多篇教學,這篇主要是幫 portconf 加入 .undef 的功能。

在 ports 中常常用到一個作法,就是利用 .ifdef 來判斷 WITHOUT_XXX 是否已定義,如果定義了,便加入 XXX 的設定,一個最常見的便是 WITHOUT_X11,對於那些不想裝 XWindow 的機器來說很有用,只需要在 ports.conf 加入:

*: WITHOUT_X11=yes    # WITHOUT_X11 也可

如此一來,所有的 ports 預設便會設定 WITHOUT_X11;但是對於某些套件來說,則必須去掉這個變數才能用到一些 library,最近遇到的例子是,我需要用到 textproc/wv,它 depend on x11-toolkits/gtk20,而 x11-toolkits/gtk20 depend on graphics/cairo,如果 cairo 設定 WITHOUT_X11 來編的話,gtk20 便會編不過出現:

gdkdrawable-x11.c:32:24: cairo-xlib.h: No such file or directory

所以 cairo 必須去掉 WITHOUT_X11 來重編。為了這些特定的套件的需要,我把 /usr/local/libexec/portconf 改成:

_conf=/usr/local/etc/ports.conf
if [ ! -r "${_conf}" ]; then
    exit
fi
_pwd=`pwd`
sed '/^#/d;/^[[:space:]]*$/d' "${_conf}" | while read _line; do
    for _port in ${_line%%:*}; do
        if [ "${_pwd%%${_port}}" != "${_pwd}" ]; then
            echo ${_line#*:} | sed -E 's/(!?)([A-Z0-9_]+)((\?)?=([^|]+))?/\1\2\4=\5/g;s/ *\| */|/g;s/ /%/g'
        fi
    done
done

至於 /etc/make.conf 裡 portconf 的設定改成(我的 /etc/make.conf 已經是改過的了):

# Begin portconf settings
# Do not touch these lines
_PORTSDIR!=/bin/realpath /usr/ports
_MATCHDIR!=echo `echo ${.CURDIR} | /usr/bin/grep ^${_PORTSDIR}`
.if !empty(_MATCHDIR) && exists(/usr/local/libexec/portconf)
_PORTCONF!=/usr/local/libexec/portconf
.for i in ${_PORTCONF:S/|/ /g}
${i:C/!([A-Z0-9_]+)(=([^ ]+)?)?/.undef \1/g}    # 主要是這行
${i:S/%/ /g}
.endfor
.endif
# End portconf settings

如此一來,我可以在 ports.conf 設定:

*: WITHOUT_X11=yes
graphics/cairo: !WITHOUT_X11

任何變數前面加上 ‘!’ 都會自動 .undef,以上面為例:

$ make -V _PORTCONF
WITHOUT_X11=yes !WITHOUT_X11=

最後會變成:

WITHOUT_X11=yes
.undef WITHOUT_X11

如此一來,便可針對某些套件設定相對於 Global 的設定了。
但要注意,不能寫成下面這樣:

graphics/cairo: !WITHOUT_X11
*: WITHOUT_X11=yes

否則會變成:

.undef WITHOUT_X11
WITHOUT_X11=yes

Firefox 3 即將推出!快來衝人氣

Firefox 3 火熱上映
Firefox 3 真是千呼萬喚始出來!終於要在後天 Release 正式版了,雖然我已經從 Beta 用到 RC3 了,不過這次要搞到創紀錄,身為 Firefox 的忠實用戶,一定要參加的,已經在用的,一起來共襄盛舉吧!不過記得要用下載安裝的,不要用自動更新的,不然不會記錄下來;還沒開始用的,相信我!這次不會再有惱人的”記憶體肥大症”,更高的執行效能,那…你還在等什麼?!

portconf 處理 soft link 的問題

ports-mgmt/portconf 是一個很好用的工具,可以設定一個 port.conf,內容大致上的形式是這樣:

editor/vim*: WITH_CSCOPE=yes | WITH_EXUBERANT_CTAGS=yes

如此一來,portconf 便會在 /etc/make.conf 自動去 parse port.conf,然後將對應的參數加入,以上面的例子,如果我安裝 editor/vim or editor/vim-lite 都會自動加入 “WITH_CSCOPE=yes WITH_EXUBERANT_CTAGS=yes”,這樣我們便可以更方便地管理各種不同 ports 的安裝參數。

但是,原本的 portconf 只是單純地判斷是否在 /usr/ports 下執行 make 的,如果 /usr/ports 是使用 soft link 的方式,其 real path 卻是在其它地方,那麼 portconf 便不會運作,它判斷的程式碼是加在 /etc/make.conf:

.if !empty(.CURDIR:M/usr/ports*) && exists(/usr/local/libexec/portconf)
_PORTCONF!=/usr/local/libexec/portconf
.for i in ${_PORTCONF:S/|/ /g}
${i:S/%/ /g}
.endfor
.endif

從上面可以看到,它是利用 make 的 modifier 去比對,但是 modifier 其後接的 pattern 是不接受變數展開的,也就是說我們不能這樣寫:

_PORTDIR!=/bin/realpath /usr/ports
.if !empty(.CURDIR:M${_PORTDIR}*) && exists(/usr/local/libexec/portconf)
_PORTCONF!=/usr/local/libexec/portconf
.for i in ${_PORTCONF:S/|/ /g}
${i:S/%/ /g}
.endfor
.endif

這是 make 本身的限制,rafan 後來送了個 patch,讓 portconf 可以在安裝的時候自動將 make.conf 裡 /usr/ports 換成它的 real path,但是只保證安裝時候判斷,如果 real path 換地方了,便得手動修改或是乾脆重裝 portconf,其實 /usr/ports 的 real path 多半很少換,只要記得換了要修改或重裝就好,所以也還可以,但是我在想有什麼方式可以達到真正的動態判斷 real path,後來我改出了兩個版本:

Version 1

第一個版本想法是既然直接在 make.conf 做判斷有困難,那不如都改到 portconf 那隻 shell script 去判斷吧!

/etc/make.conf

.if exists(/usr/local/libexec/portconf)
_PORTCONF!=/usr/local/libexec/portconf
.for i in ${_PORTCONF:S/|/ /g}
${i:S/%/ /g}
.endfor
.endif

/usr/local/libexec/portconf 前面加入

_portdir=`/bin/realpath /usr/ports`
_curdir=`pwd`
if [ "${_curdir##${_portdir}*}" != "" ]; then
    exit
fi

Version 2

第二個版本是直接在 make.conf 利用外部工具做判斷,只需要修改 make.conf,不用動到 portconf 那隻 shell script。

/etc/make.conf

_PORTSDIR!=/bin/realpath /usr/ports
_MATCHDIR!=echo `echo ${.CURDIR} | /usr/bin/grep ^${_PORTSDIR}`
.if !empty(_MATCHDIR) && exists(/usr/local/libexec/portconf)
_PORTCONF!=/usr/local/libexec/portconf
.for i in ${_PORTCONF:S/|/ /g}
${i:S/%/ /g}
.endfor
.endif

上面兩個版本都可以達到動態判斷 real path,但是有一利必有一弊…原本 portconf 利用 make 本身的 modifier 便是為了減少每次呼叫 make 多花費在判斷的時間,這對於一般的 ports 安裝來說,不會有多大的影響,但是對於 make buildworld 來說,便有可能造成很大的影響,所以我做個了測試,看看上面兩種修改在 make buildworld 增加多少時間,我的測試環境是:

  • CPU:Intel(R) Core(TM)2 Duo CPU E6550 @ 2.33GHz (2333.35-MHz K8-class CPU)
  • OS:FreeBSD 7.0R amd64

測試出來的結果:

  • Original:60m57.49s
  • Version 1:62m6.19s
  • Version 2:61m29.74s

這樣的結果我還算可以接受,所以我選擇了 Version 2 的版本 (雖然它做法挺髒的 XD)。