- 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...
2013年11月18日 星期一
使用 Lua 的應用程式
使用 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 的函式庫的例子。
參考:
另外,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 的函式庫的例子。
參考:
- 本文翻譯 Programming in Lua, 1st ed. 的 §24 – An Overview of the C API,並加以修改整理。
- 本站 Lua 相關文章
2013年11月17日 星期日
OpenWrt WebUI
OpenWrt 的 WebUI 主要有三種:LuCI、X-Wrt Webif、及 Gargoyle。
LuCI
haserl 是一個命令檔語言,本身編譯後約只有 20k,可在 HTML 檔嵌入 shell scripts 作為伺服器端命令檔,產生最終的 HTML 檔案。haserl 程式碼:
LuCI
- Web Server 預設用 uhttpd
- 使用 Lua 命令檔語言
- Web Server 用 busybox httpd
- shell + awk + haserl 命令檔語言
- Web Server 用 gargoyle_httpd,採用 fork 處理每個連結,效能可能較差。
- 前端 JavaScript + 後端 haserl 命令檔語言
- http://lwn.net/Articles/420066/
haserl 是一個命令檔語言,本身編譯後約只有 20k,可在 HTML 檔嵌入 shell scripts 作為伺服器端命令檔,產生最終的 HTML 檔案。haserl 程式碼:
- 本身是執行檔,首行是 #!/usr/bin/haserl
- 其它內容是 HTML 格式,需要動態產生的部份用 <? ?> 包起來,執行 shell script 產生
- http://wiki.openwrt.org/doc/howto/webinterface.overview
- 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++ 間互相呼叫,擴充性極高
- ...
-
Part I · The Language
- Getting Started (Hello World)
- 1.1 Chunks
- 1.2 Some Lexical Conventions (識別字、註解)
- 1.3 Global Variables
- 1.4 The Stand-Alone Interpreter
- Types and Values
- Expressions
- 3.1 Arithmetic Operators
- 3.2 Relational Operators
- 3.3 Logical Operators
- 3.4 Concatenation
- 3.5 Precedence
- 3.6 Table Constructors
- 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
- Functions
- 5.1 Multiple Results
- 5.2 Variable Number of Arguments
- 5.3 Named Arguments
- More about Functions
- 6.1 Closures
- 6.2 Non-Global Functions
- 6.3 Proper Tail Calls
- 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
- Compilation, Execution, and Errors
- 8.1 The
requireFunction - 8.2 C Code
- 8.3 Errors
- 8.4 Error Handling and Exceptions
- 8.5 Error Messages and Tracebacks
- 8.1 The
- Coroutines
- 9.1 Coroutine Basics
- 9.2 Pipes and Filters
- 9.3 Coroutines as Iterators
- 9.4 Non-Preemptive Multithreading
- Complete Examples
- 10.1 Data Description
- 10.2 Markov Chain Algorithm
Part II · Tables and Objects
- 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
- 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
- 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
__indexMetamethod - 13.4.2 The
__newindexMetamethod - 13.4.3 Tables with Default Values
- 13.4.4 Tracking Table Accesses
- 13.4.5 Read-Only Tables
- 13.4.1 The
- The Environment
- 14.1 Accessing Global Variables with Dynamic Names
- 14.2 Declaring Global Variables
- 14.3 Non-Global Environments
- 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
- Object-Oriented Programming
- 16.1 Classes
- 16.2 Inheritance
- 16.3 Multiple Inheritance
- 16.4 Privacy
- 16.5 The Single-Method Approach
- Weak Tables
- 17.1 Memoize Functions
- 17.2 Object Attributes
- 17.3 Revisiting Tables with Default Values
Part III · The Standard Libraries
- The Mathematical Library
- The Table Library
- 19.1 – Array Size
- 19.2 – Insert and Remove
- 19.3 – Sort
- Concatenation
- The String Library
- Basic String Functions
- 20.1 Pattern-Matching Functions
- 20.2 Patterns
- 20.3 Captures
- Replacements
- 20.4 Tricks of the Trade
- 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
- The Operating System Library
- 22.1 Date and Time
- 22.2 Other System Calls
- The Debug Library
- 23.1 Introspective Facilities
- 23.1.1 Accessing Local Variables
- 23.1.2 Accessing Upvalues
- 23.2 Hooks
- 23.3 Profiles
Part IV · The C API
- 23.1 Introspective Facilities
- 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
- Extending your Application
- The Basics
- 25.1 Table Manipulation
- 25.2 Calling Lua Functions
- 25.3 A Generic Call Function
- Calling C from Lua
- 26.1 C Functions
- 26.2 C Libraries
- 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
- User-Defined Types in C
- 28.1 Userdata
- 28.2 Metatables
- 28.3 Object-Oriented Access
- 28.4 Array Access
- 28.5 Light Userdata
- Managing Resources
- 29.1 A Directory Iterator
- 29.2 An XML Parser
- Threads and States
- 30.1 Multiple Threads
- 30.2 Lua States
- Memory Management
- 31.1 The Allocation Function
- 31.2 The Garbage Collector
其它
- 簡介:http://programmermagazine.github.io/201402/htm/people1.html
- 應用程式
- 課程
- http://yhhuang1966.blogspot.com/2015/07/lua_14.html
- https://www.linuxlinks.com/best-free-books-learn-lua/ (免費電子書)
- Programming in Lua
- https://michaelchen.tech/review/programming-in-lua-4th-edition/
- https://sites.google.com/a/kimicat.com/www/lua教學
- Lua 5.3 Reference Manual
- https://github.com/brndnmtthws/conky/wiki (桌面系統監控內建 Lua 支援)
- CirnOS (在 Raspberry Pi 專門提供 Lua 執行環境的作業系統)
romfs
romfs 是一個非常精簡、唯讀、block-based 的檔案系統,沒壓縮,所有檔案 root 擁有。
genromfs
genromfs
特性
- 只保留作為一個檔案系統的最小需求:
- No modification dates
- No unix permissions
用途
每 個 Linux 系統都至少需要配合一個檔案系統來執行,而大多其他檔案系統含有許多功能,不適合在某些資源較吃緊的情況下使用,這時改用 romfs 就很完美。例如:可以把壓縮過的 Linux kernel 放在 romfs 裡,給認得 romfs 的 bootloader 解壓縮放到 RAM 中執行。延伸閱讀
- http://romfs.sourceforge.net/
- Kernel /Documentation/filesystems
2013年11月1日 星期五
ATtiny
| ATtiny | pin | IO | insts | registers | Flash | SRAM | EEPROM | Peripheral | Hardware |
|---|---|---|---|---|---|---|---|---|---|
| ATtiny4/9 | 6 | 4 | 54 | 16x8 | 512/1024 | 32 | 0 | PWM | http://hackaday.com/tag/attiny9/ |
| ATtiny5/10 | PWM, ADC | http://hackaday.com/tag/attiny10/ | |||||||
| ATtiny13 ATinty13A | 8 | 6 | 120 | 32x8 | 1K | 64 | 64 | PWM, ADC | http://hackaday.com/tag/attiny13/ |
| ATtiny25 ATtiny45 ATtiny85 | 2/4/8K | 128/256/512 | PWM, ADC, USI | i2c-tiny-usb Digispark | |||||
| ATtiny24 ATtiny44 ATtiny84 | 14 | 12 | PWM, ADC, USI, Temperature | http://hackaday.com/tag/attiny24/ http://hackaday.com/tag/attiny44/ http://hackaday.com/tag/attiny84/ | |||||
| ATtiny20 | 112 | 16x8 | 2K | 128 | 0 | PWM, ADC, SPI, TWI | PWM | ||
| ATtiny2313 | 20 | 18 | 120 | 32x8 | 2K | 128 | 128 | PWM, ADC, USI, USART | USBtinyISP |
訂閱:
文章 (Atom)
SIP header Via
所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...
-
網路界面驅動程式有兩種接收封包的方式 -- 中斷驅動和 NAPI。傳統是用中斷驅動,在中斷服務程式裡接收封包,但封包量大時會有中斷次數太多而效能變差的問題。NAPI (意思是 New API) 改成中斷服務程式只通知有封包,Kernel 再排程一次接收多個封包來提昇效能。 中...
-
busybox ntpd 是 NTP client,也可作為 NTP server。 參數 -d:Verbose -n:不常駐 -q:(quit) 時間設完後結束 -N:高優先權執行 -w:不設定時間,只是詢問 peers,隱含 -n -l:作為 NTP ser...
-
驅動程式除了 read()/write() 外,通常可以透過 ioctl() 系統呼叫進行一些控制或設定,如鎖門、退片、回報錯誤、改變 baud rate、或 self destruct 等。 在 user space 格式如下: #include <sys/ioctl...
