[PATCH] reworked to remove awk and grep calls

1 view
Skip to first unread message

que...@gmail.com

unread,
Oct 24, 2008, 12:36:44 PM10/24/08
to wmii...@googlegroups.com, ba...@jukie.net, Jan-David Quesel
From: Jan-David Quesel <que...@informatik.uni-oldenburg.de>

- do string processing directly in lua
- changed options such that wired and wireless devices have seperate options
---
plugins/network.lua | 86 ++++++++++++++++++++++++--------------------------
1 files changed, 41 insertions(+), 45 deletions(-)

diff --git a/plugins/network.lua b/plugins/network.lua
index 656bb3a..c0a40d7 100644
--- a/plugins/network.lua
+++ b/plugins/network.lua
@@ -15,7 +15,8 @@ network.lua - wmiirc-lua plugin for monitoring network interfaces

For the options you can define something like

-wmii.set_conf("network.interfaces", "wlan0,true,eth0,false")
+wmii.set_conf("network.interfaces.wired", "eth0")
+wmii.set_conf("network.interfaces.wireless", "wlan0")

which will show informations about the wireless device wlan0 and
the non-wireless device eth0
@@ -47,17 +48,14 @@ local pairs = pairs
module("network")
api_version = 0.1

-wmii.set_conf("network.interfaces", "eth0,false")
+wmii.set_conf("network.interfaces.wired", "eth0")
+wmii.set_conf("network.interfaces.wireless", "")

local devices = { }
-local wireless_devices = { }
-- ------------------------------------------------------------
-- MODULE VARIABLES
-local widget = nil
local timer = nil

-widget = wmii.widget:new ("350_network")
-
local function _command ( cmd )

if (cmd) then
@@ -66,73 +64,71 @@ local function _command ( cmd )
local status = file:read("*a")
file:close()

- return status:match("[^\n]*")
+ return status
+ --return status:match("[^\n]*")
else
return ""
end
end

-local function create_device_string(device,wireless)
- local ip = "ifconfig " .. device .. "| awk -F: '/inet addr/ {print $2}' | awk '{print $1}'"
+local function create_device_string(device)
+ local ip = "ifconfig " .. device["name"]
local ipstr = _command(ip)
- if ipstr == "" then
-
- txt = device .. ": down"
+ ipstr = ipstr:gmatch("inet addr:([0-9.]+)")()
+ if ipstr == nil or ipstr == "" then
+ txt = device["name"] .. ": down"
else
ipstr = ipstr.sub(ipstr, 1, ipstr.len(ipstr))
- txt = device .. ": " .. ipstr
- if wireless then
- local ssid = "iwconfig " .. device .. " |grep ESSID | awk -F: '{print $2}'"
+ txt = device["name"] .. ": " .. ipstr
+ if device["wireless"] then
+ local ssid = "iwconfig " .. device["name"]
local str_ssid = _command(ssid)
+ str_ssid = str_ssid:gmatch("ESSID:\"(.*)\"")()
str_ssid = str_ssid.sub(str_ssid, 2, str_ssid.len(str_ssid)-3)
txt = txt .. "@(" .. str_ssid .. ")"
end
end
- return txt
+
+ device["widget"]:show(txt)
end

-local function update ()
- local txt = ""

- local space = ""
- for _,device in pairs(wireless_devices) do
- txt = txt .. space .. create_device_string(device,true)
- space = " "
+local function generate_lists()
+ wmii.log("generating interface list")
+ local strings = wmii.get_conf("network.interfaces.wired")
+ for str in strings:gmatch("%w+") do
+ devices[#devices+1] = {
+ name = str,
+ widget = wmii.widget:new ("350_network_" .. str),
+ wireless = false
+ }
+ wmii.log("found " .. str)
end
- for _,device in pairs(devices) do
- txt = txt .. space .. create_device_string(device,false)
- space = " "
+ local strings = wmii.get_conf("network.interfaces.wireless")
+ for str in strings:gmatch("%w+") do
+ devices[#devices+1] = {
+ name = str,
+ widget = wmii.widget:new ("350_network_" .. str),
+ wireless = true
+ }
+ wmii.log("found " .. str)
end
- widget:show(txt)
-end

-local function generate_lists()
- local strings = wmii.get_conf("network.interfaces")
-
- local string_list = { }
+end

- for str in strings:gmatch("%w+") do
- string_list[#string_list+1] = str
- end
-
- local i = 1
- while i < #string_list do
- if string_list[i+1] == "true" then
- wireless_devices[#wireless_devices+1] = string_list[i]
- else
- devices[#devices+1] = string_list[i]
- end
- i = i + 2
+local function update ()
+ for _,device in pairs(devices) do
+ create_device_string(device)
end
end

local function network_timer ( timer )
- if #devices == 0 and #wireless_devices == 0 then
+ if #devices == 0 then
generate_lists()
end
update()
return 60
-end

+end

timer = wmii.timer:new (network_timer, 1)
--
1.5.3.4

Reply all
Reply to author
Forward
0 new messages