2012-06-24

stdout and stderr

在使用 exec 執行 git clone B2G 的 source code 時,希望可以看到目前的情況,所以:

set B2G_git [list git clone https://github.com/andreasgal/B2G.git B2G >@stdout 2>@stderr]
puts "Using git clone B2G source code..." 
exec {*}$B2G_git
整個程式如下,不過我只是急救章的寫一個來用,還沒有完整的測試過:

#!/usr/bin/tclsh
#
# Purpose: Get B2G project source code
# Update date: 2012/06/24
#


# global variable
#
set B2GDir [pwd]
append B2GDir "/B2G"

set B2G_GIT "https://github.com/andreasgal/B2G.git"


# 
# get_source procedure: using git clone to get source code
#
proc get_source {} {
  puts "########## get start ##########"

  set B2G_git [list git clone $::B2G_GIT B2G >@stdout 2>@stderr]
  puts "Using git clone B2G source code..." 
  exec {*}$B2G_git

  puts "########## get end ##########"
}


# 
# update_source procedure: using git pull to update source code
#
proc update_source {} {
  puts "B2G folder exists!!! --- try to update"
  set git_pull [list git pull >@stdout 2>@stderr]

  puts "########## upate start ##########"

  cd $::B2GDir
  set fileExist [file exists .git]
  if {$fileExist > 0} {
    puts "Using git pull to update B2G source code..." 
    exec {*}$git_pull
  } else {
    puts "Error!!!"
    puts "B2G folder exists but no .get folder!!!"
  }

  puts "########## upate end ##########"
}


#
# Main procedure, get B2G project source code
#
if {$argc > 0} {
  set argv0 [string toupper [lindex $argv 0]]
  if { [expr [string compare $argv0 "GET" ] == 0] } {  
    set fileExist [file exists $B2GDir]
    if {!$fileExist} {
       get_source
    } else {
       update_source
    }
  } elseif { [expr [string compare $argv0 "UPDATE" ] == 0] } {
    set fileExist [file exists $B2GDir]
    if {!$fileExist} {
       puts "B2G folder does not exist!!!"
    } else {
       update_source
    }
  }
} elseif {$argc == 0} {
  puts "User does not use GET or UPDATE parameter..."
  puts "Check B2G folder..."
  set fileExist [file exists $B2GDir]
  if {!$fileExist} {
     get_source
  } else {
     update_source
  }
}

2012-01-10

tcllib 1.14 is out

 我現在才注意到,在 2011.12.13,tcllib 已經釋出了新版 v1.14。

Overview
========

21 new packages in 7 modules
30 changed packages in 24 modules
8 internally changed packages in 8 modules
328 unchanged packages in 89 modules
393 packages, total in 107 modules, total

2012-01-08

Coroutines

在 8.6 引進新的 Non-Recursive Engine (NRE) 之後,帶進了新的功能 Coroutines。不過 Coroutine 對我而言是很陌生的東西,所以做一下初學筆記。

在看過網路上對於 Coroutine 的介紹以後,我想可以簡單的總結如下:

Croutine 是自 1960 年代就已經現身的多工實做技術,原理相當單純,在 Coroutine 的 "yield" 是屬於程式語言層面,透過特定技巧或機制,讓原本循序執行的陳述指令,得以做出交錯執行的結果,可以說是 Windows 3.X 「合作式多工」的基礎概念(十分類似)。

用簡單的一句話來說 Coroutine,就是可以暫時中斷,之後再繼續執行的程序。


下面是從 Tcl 8.6 manual page copy 出來的例子:

coroutine accumulator apply {{} {
    set x 0
    while 1 {
        # yield $x: 丟資料並且把主控權交給呼叫者
        incr x [yield $x]
    }
}}

for {set i 0} {$i < 10} {incr i} {
    puts "$i -> [accumulator $i]"
}

在 manual page 裡還有其它的例子幫助理解,希望我對 Coroutines 的理解是正確的。

Wub

Wub
wub - Wub pure tcl HTTP1.1 server, client and tool suite


雖然 Wub 發展有一段時間了,不過我是最近才發現的。

截錄自 Tcler Wiki 的介紹:

Wub is a web-server written in pure-Tcl. It runs the Wiki you're now reading. It absolutely requires Tcl 8.6 and tracks the HEAD closely. It should help in creating highly-dynamic (and portable) web applications. It is the successor in spirit of Tclhttpd, aiming to preserve the best of it while using the bleeding-edge latest Tcl facilities to simplify and extend.


Tcler Wiki 就是使用 Wub 架起來的,非常好的 working example。

2012-01-07

備份 (on Windows)

跟之前一樣的程式,只是加上 GUI 表示目前正在備份中,加上練習 thread 使用的部份。

#!/usr/bin/tclsh
#
# This file is used to backup my homepage
#

package require Tk
package require Thread

set ::gThread [thread::create {thread::wait} ]

label .greetings -text "Now backup my homepage..." -bd 4 -relief ridge
pack .greetings -fill both

wm title . "Backup"

thread::send -async $::gThread { 
    set backupfile $env(HOME)
    append backupfile "\\My Documents"
    append backupfile "\\Homepage-"
    append backupfile [clock format [clock seconds] -format %Y%m%d]
    append backupfile ".7z"

    set backupdir $env(HOME)
    append backupdir "\\My Documents"
    append backupdir "\\public_html"

    set fileExist [file exists $backupfile]
    if {$fileExist > 0} {
        puts "Now try to remove old backup file."
        file delete $backupfile
    }

    set var [list 7z a $backupfile $backupdir]
    exec {*}$var        
} result


vwait result
exit

2011-11-07

使用 7-zip 壓縮目錄與檔案 (on Windows)

#!/usr/bin/tclsh
#
# This file is used to backup my homepage
#

set backupfile $env(HOME)
append backupfile "\\My Documents"
append backupfile "\\Homepage-"
append backupfile [clock format [clock seconds] -format %Y%m%d]
append backupfile ".7z"

set backupdir $env(HOME)
append backupdir "\\My Documents"
append backupdir "\\public_html"

set fileExist [file exists $backupfile]
if {$fileExist > 0} {
    puts "Now try to remove old backup file."
    file delete $backupfile
}

set var [list 7z a $backupfile $backupdir]
exec {*}$var

puts "Done."

exit

因為在 Windows XP 上也有需要網頁的備份,所以把之前在 Linux 上用的稍微改寫以後就搬過來了。要注意的是,我已經把 7-ZIP 加進去 PATH 環境變數裡了,所以就不用寫絕對路徑,只要寫 7z 就可以了。

2011-07-20

使用 7-zip 壓縮目錄與檔案

#!/usr/bin/tclsh
#
# This file is used to backup my homepage
#

set backupfile $env(HOME)
append backupfile "/Homepage-"
append backupfile [clock format [clock seconds] -format %Y%m%d]
append backupfile ".7z"

set backupdir $env(HOME)
append backupdir "/public_html"

set fileExist [file exists $backupfile]
if {$fileExist > 0} {
    puts "Now try to remove old backup file."
    file delete $backupfile
}

set var [list 7z a $backupfile $backupdir]
exec {*}$var

puts "Done."

使用 7-zip 建立網頁的備份檔案 (on Linux)。

2011-04-10

file command with ActiveTcl 8.6b5

Safe interp and file command with ActiveTcl 8.6b5

我也遇到 ::tcl::mathfunc::maxunknown hidden command "file" 這個錯誤訊息(在使用 ActiveTcl 8.6b5 執行 TclHttpd 的時候),只是在 Windows XP 上遇到,目前看起來應該是 ActiveTcl 8.6b5 的問題。

2011-01-26

tcllib 1.13 is out

2011.01.24,tcllib 釋出了新版 v1.13。

Overview
========
44 new packages in 10 modules
29 changed packages in 24 modules
62 internally changed packages in 11 modules
207 unchanged packages in 79 modules
348 packages, total in 103 modules, total

相關連結:http://sourceforge.net/projects/tcllib/files/tcllib/1.13/

2010-10-03

apply and procedure

你的程式語言可以這樣做嗎?  讀完以後,用 Tcl 實驗匿名函式能力以後的結果。


寫程式的時候,你注意到有二段 code 一模一樣,除了一個反覆呼叫一個叫 BoomBoom 的函數,另一個反覆呼叫一個喚作 PutInPot 的。

puts "get the lobster"
PutInPot "lobster"
PutInPot "water"

and

puts "get the chicken"
BoomBoom "chicken"
BoomBoom "coconut"

現在你需要一個辦法,使得你可以將一個函數用作另一個函數的參數。這是個重要的能力,因為你更容易將常用的程式碼收藏在一個函數內。而 Tcl 所具有的彈性讓我們可以這樣做:

proc Cook {arg1 arg2 func} {
   puts -nonewline "get the "
   puts $arg1
   
   $func $arg1
   $func $arg2
}

所以我們就可以這樣用:

Cook  "lobster" "water" "PutInPot"
Cook  "chicken" "coconut" "BoomBoom" 

這樣就可以將函數作為參數了。

假設你未定義 PutInPot 或 BoomBoom 這些函數。如果能直接將它寫進一行內,不是比在其他地方宣告它們更好嗎?

proc Cook {arg1 arg2 func} {
   puts -nonewline "get the "
   puts $arg1
   
   set result [info procs $func]

   if {[string length $result] == 0} {
    apply $func $arg1
    apply $func $arg2
   } else {
    $func $arg1
    $func $arg2
   }
}

所以:

Cook "chicken" "coconut" {{food} { 
    puts -nonewline "BoomBoom --- "
    puts $food}
}

這樣是不是很方便?我建立函數時,甚至不用考慮怎麼命名,直接拿起它們,丟到一個函數內就可以了。

apply - Apply an anonymous function
apply func ?arg1 arg2 ...?

其中的 func 定義了參數和 procedure 要執行的東西(procedure body),思考一下 Tcl 如何定義 procedure,就知道該怎麼做:

proc anonymous_function {food} {
    puts -nonewline "BoomBoom --- "
    puts $food}
}

所以

Cook "chicken" "coconut" {{food} {
    puts -nonewline "BoomBoom --- "
    puts $food
  }
}