2013年11月18日 星期一

使用 Lua 的應用程式

使用 Lua 的應用程式
  • lua -- Lua interpreter
  • luac -- Lua compiler
  • 許多遊戲,例如 WoW、Angry Bird
  • Web Server
    • LightTPD supports the FastCGI, SCGI, and CGI interfaces to external programs。Understand and harness Lua/FastCGI
    • uHTTPd
    • haserl
    • lighttpd
    • Apache
  • OpenWrt device configuration, iwinfo
  • Security: scripting vulnerability scanners (nmap, Wireshark, Snort), iptables
  • Scripting and template language for Wikipedia
  • Embedded software: printers (Olivetti, Océ), routers (Cisco), telephones (FreeSwitch, Asterisk) and smartphones (several, including Huawei), smart tvs (Samsung), Logitech keyboards, Lego Mindstorms...
  • Adobe Photoshop Lightroom
  • VLC, Tex, vim, nginx...
參考:
  1. http://www.dcc.ufrj.br/~fabiom/lua/00Introduction.pdf
  2. 本站 Lua 相關文章

Lua 課程

http://www.dcc.ufrj.br/~fabiom/lua/

參考:
  1. 本站 Lua 相關文章

Lua C API 概述

Lua 實際上是指 C 函式庫 liblua,必須寄居在某個應用程式下才能使用,所以是一個嵌入式語言 (embedded language)。而 lua 這個獨立 (stand-alone) 應用程式,只是一個 Lua 的小外殼,少於 500 行程式碼,提供一個使用者界面方便使用 Lua,真正執行 Lua 程式碼的不是外殼,而是 Lua。Lua 嵌入式語言的角色 ,使得它成為應用程式的擴展語言 (extension language),讓應用程式得以使用 Lua 的設施 (facilities),來擴展應用程式的功能。

另外,Lua 也是一個可擴展的語言 (extensible language),可在 Lua 環境下用其它語言 (例如 C) 來擴展 Lua 所沒有的新功能。

就 Lua 的擴展語言及可擴展語言兩個角色,皆可使用 C 語言。Lua 可作為 C 語言的擴展語言,C 實作為應用程式,使用 liblua 提供 Lua 環境來擴充 Lua 語言功能。反過來,C 也可以擴展 Lua 語言,提供 Lua 沒有的功能。這兩者使用相同的 API 跟 Lua 溝通,稱為 C API,包括一些函數:讀寫 Lua 全域變數,呼叫 Lua 函數,運行 Lua 程式碼,登記 C 函數好讓能被 Lua 呼叫,等等。

跟 Lua 是完全不同的是,C API 延續 C 嚴謹的特性。在寫 C 語言程式時,必須注意類型 (type) 檢查,錯誤處理,memory-allocation 錯誤等。大多數 API 中的函數不檢查參數的正確性,在呼叫函數之前,必須確保參數是對的。否則,可能出現「segmentation fault」或類似的激烈結果,而不是委婉的告訴你錯誤訊息。此外,API 強調靈活性和簡易性,而不是易用,常見的工作可能要呼叫好幾個 API 函數,但讓你完全控制所有細節,比如錯誤處理,緩​​衝區大小等等。

Lua 和 C 之間通信中的重要組成部分是虛擬堆疊。幾乎所有的 API 都對這個堆疊裡的值操作。所有從 Lua 到 C 及從 C 到 Lua 的資料交換都是經過這個堆疊。此外,可以使用堆疊來存放中間結果。堆疊有助於解決 Lua 和 C 之間的兩個不匹配:一是 Lua 使用垃圾收集 (garbage collection),而 C 需要明確的釋出記憶體;二是來自 Lua 使用動態類型 (dynamic typing),而 C 使用靜態類型 (static typing)。

正如標題所說,本章是一個概述,不用急著去理解所提到的所有細節。稍後會補上那些細節。然而,不要忘了,可以在 Lua 參考手冊找到特定功能的更多細節。此外,可以在 Lua 的分佈 (distribution) 本身找到幾個使用 API 例子。獨立的 Lua 直譯器 (lua.c) 提供應用程式的例子,而標準庫 (lmathlib.c,lstrlib.c 等) 提供 Lua 的函式庫的例子。

參考:
  1. 本文翻譯 Programming in Lua, 1st ed.§24 – An Overview of the C API,並加以修改整理。
  2. 本站 Lua 相關文章

2013年11月17日 星期日

OpenWrt WebUI

OpenWrt 的 WebUI 主要有三種:LuCI、X-Wrt Webif、及 Gargoyle。

LuCI
  • Web Server 預設用 uhttpd
  • 使用 Lua 命令檔語言
X-Wrt Webif
  • Web Server 用 busybox httpd
  • shell + awk + haserl 命令檔語言
Gargoyle:基於 OpenWrt 的發行版本,有較易用的 GUI 介面及其它特別的功能,例如:可以對特定 IP 限制頻寬、主動式 QoS

haserl 是一個命令檔語言,本身編譯後約只有 20k,可在 HTML 檔嵌入 shell scripts 作為伺服器端命令檔,產生最終的 HTML 檔案。haserl 程式碼:
  • 本身是執行檔,首行是 #!/usr/bin/haserl
  • 其它內容是 HTML 格式,需要動態產生的部份用 <? ?> 包起來,執行 shell script 產生
參考:
  1. http://wiki.openwrt.org/doc/howto/webinterface.overview
  2. Web Server Comparisons -- 一些網頁伺服器比較,其中 thttpd 似乎不錯。而 gargoyle_httpd 是基於用 fork 的 mini_httpd,效能可能較差。
延伸閱讀:十大易用性原則

2013年11月16日 星期六

Lua 文章

Lua 是個輕小的指令檔語言 (Scripting Language),由於巴西在 1977 ~ 1992 年對電腦軟硬體進行嚴格的貿易管制,買不到只好自己開發,Pontifical Catholic 大學的大型實驗室 Tecgraf 在前身 DEL、SOL 發展後而誕生。特性
  • 容易和 C/C++ 間互相呼叫,擴充性極高
  • ...
本站 Lua 相關文章規劃:(參考自 Programming in Lua)
    Part I · The Language
  1. Getting Started (Hello World)
  2. Types and Values
    • 2.1 Nil
    • 2.2 Booleans
    • 2.3 Numbers
    • 2.4 Strings
    • 2.5 Tables
    • 2.6 Functions
    • 2.7 Userdata and Threads
  3. Expressions
    • 3.1 Arithmetic Operators
    • 3.2 Relational Operators
    • 3.3 Logical Operators
    • 3.4 Concatenation
    • 3.5 Precedence
    • 3.6 Table Constructors
  4. Statements
    • 4.1 Assignment (多值指定)
    • 4.2 Local Variables and Blocks
    • 4.3 Control Structures
      • 4.3.1 if then else
      • 4.3.2 while
      • 4.3.3 repeat
      • 4.3.4 Numeric for
      • 4.3.5 Generic for
    • 4.4 break and return
  5. Functions
    • 5.1 Multiple Results
    • 5.2 Variable Number of Arguments
    • 5.3 Named Arguments
  6. More about Functions
    • 6.1 Closures
    • 6.2 Non-Global Functions
    • 6.3 Proper Tail Calls
  7. Iterators and the Generic for
    • 7.1 Iterators and Closures
    • 7.2 The Semantics of the Generic for
    • 7.3 Stateless Iterators
    • 7.4 Iterators with Complex State
    • 7.5 True Iterators
  8. Compilation, Execution, and Errors
    • 8.1 The require Function
    • 8.2 C Code
    • 8.3 Errors
    • 8.4 Error Handling and Exceptions
    • 8.5 Error Messages and Tracebacks
  9. Coroutines
    • 9.1 Coroutine Basics
    • 9.2 Pipes and Filters
    • 9.3 Coroutines as Iterators
    • 9.4 Non-Preemptive Multithreading
  10. Complete Examples
    • 10.1 Data Description
    • 10.2 Markov Chain Algorithm
  11. Part II · Tables and Objects
  12. Data Structures
    • 11.1 Arrays
    • 11.2 Matrices and Multi-Dimensional Arrays
    • 11.3 Linked Lists
    • 11.4 Queues and Double Queues
    • 11.5 Sets and Bags
    • 11.6 String Buffers
    • 11.7 Graphs
  13. Data Files and Persistence
    • 12.1 Data Files
    • 12.2  Serialization
      • 12.2.1 Saving Tables without Cycles
      • 12.2.2 Saving Tables with Cycles
  14. Metatables and Metamethods
    • 13.1 Arithmetic Metamethods
    • 13.2 Relational Metamethods
    • 13.3 Library-Defined Metamethods
    • 13.4 Table-Access Metamethods
      • 13.4.1 The __index Metamethod
      • 13.4.2 The __newindex Metamethod
      • 13.4.3 Tables with Default Values
      • 13.4.4 Tracking Table Accesses
      • 13.4.5 Read-Only Tables
  15. The Environment
    • 14.1 Accessing Global Variables with Dynamic Names
    • 14.2 Declaring Global Variables
    • 14.3 Non-Global Environments
  16. Packages
    • 15.1 The require Function
    • 15.2 The Basic Approach for Writing Modules
    • 15.2 Privacy
    • 15.3 Packages and Files
    • 15.4 Using the Global Table
    • 15.5 Other Facilities
    • Using Environments
    • The module Function
    • Submodules and Packages
  17. Object-Oriented Programming
    • 16.1 Classes
    • 16.2 Inheritance
    • 16.3 Multiple Inheritance
    • 16.4 Privacy
    • 16.5 The Single-Method Approach
  18. Weak Tables
    • 17.1 Memoize Functions
    • 17.2 Object Attributes
    • 17.3 Revisiting Tables with Default Values
  19. Part III · The Standard Libraries
  20. The Mathematical Library
  21. The Table Library
    • 19.1 – Array Size
    • 19.2 – Insert and Remove
    • 19.3 – Sort
    • Concatenation
  22. The String Library
    • Basic String Functions
    • 20.1 Pattern-Matching Functions
    • 20.2 Patterns
    • 20.3 Captures
    • Replacements
    • 20.4 Tricks of the Trade
  23. The I/O Library
    • 21.1 The Simple I/O Model
    • 21.2 The Complete I/O Model
      • 21.2.1 A Small Performance Trick
      • 21.2.2 Binary Files
    • 21.3 Other Operations on Files
  24. The Operating System Library
    • 22.1 Date and Time
    • 22.2 Other System Calls
  25. The Debug Library
    • 23.1 Introspective Facilities
      • 23.1.1 Accessing Local Variables
      • 23.1.2 Accessing Upvalues
    • 23.2 Hooks
    • 23.3 Profiles
  26. Part IV · The C API
  27. An Overview of the C API
    • 24.1 A First Example
    • 24.2 The Stack
      • 24.2.1 Pushing Elements
      • 24.2.2 Querying Elements
      • 24.2.3 Other Stack Operations
    • 24.3 Error Handling with the C API
      • 24.3.1 Error Handling in Application Code
      • 24.3.2 Error Handling in Library Code
  28. Extending your Application
    • The Basics
    • 25.1 Table Manipulation
    • 25.2 Calling Lua Functions
    • 25.3 A Generic Call Function
  29. Calling C from Lua
    • 26.1 C Functions
    • 26.2 C Libraries
  30. Techniques for Writing C Functions
    • 27.1 Array Manipulation
    • 27.2 String Manipulation
    • 27.3 Storing State in C Functions
      • 27.3.1 The Registry
      • 27.3.2 References
      • 27.3.3 Upvalues
  31. User-Defined Types in C
    • 28.1 Userdata
    • 28.2 Metatables
    • 28.3 Object-Oriented Access
    • 28.4 Array Access
    • 28.5 Light Userdata
  32. Managing Resources
    • 29.1 A Directory Iterator
    • 29.2 An XML Parser
  33. Threads and States
    • 30.1 Multiple Threads
    • 30.2 Lua States
  34. Memory Management
    • 31.1 The Allocation Function
    • 31.2 The Garbage Collector

其它

romfs

romfs 是一個非常精簡、唯讀、block-based 的檔案系統,沒壓縮,所有檔案 root 擁有。

genromfs

特性

  • 只保留作為一個檔案系統的最小需求:
    • No modification dates
    • No unix permissions

用途

每 個 Linux 系統都至少需要配合一個檔案系統來執行,而大多其他檔案系統含有許多功能,不適合在某些資源較吃緊的情況下使用,這時改用 romfs 就很完美。例如:可以把壓縮過的 Linux kernel 放在 romfs 裡,給認得 romfs 的 bootloader 解壓縮放到 RAM 中執行。

延伸閱讀

  1. http://romfs.sourceforge.net/
  2. Kernel /Documentation/filesystems

2013年11月1日 星期五

ATtiny


ATtinypinIOinstsregistersFlashSRAMEEPROMPeripheralHardware
ATtiny4/9645416x8512/1024320PWMhttp://hackaday.com/tag/attiny9/
ATtiny5/10PWM, ADChttp://hackaday.com/tag/attiny10/
ATtiny13
ATinty13A
8612032x81K6464PWM, ADChttp://hackaday.com/tag/attiny13/
ATtiny25
ATtiny45
ATtiny85
2/4/8K128/256/512PWM, ADC, USIi2c-tiny-usb
Digispark
ATtiny24
ATtiny44
ATtiny84
1412PWM, ADC, USI, Temperaturehttp://hackaday.com/tag/attiny24/
http://hackaday.com/tag/attiny44/
http://hackaday.com/tag/attiny84/
ATtiny2011216x82K1280PWM, ADC, SPI, TWIPWM
ATtiny2313201812032x82K128128PWM, ADC, USI, USARTUSBtinyISP


SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...