关于mutt发送中文附件,foxmail,outlook接受不到的问题

经常在linux下工作,所有日常事务也是在linux处理,邮件处理自然也不例外,我这里环境是arch+mutt。带英文附件名的邮件使用mutt发送没有一点问题,中文附件名的邮件就悲剧了,附件在foxmail中不被识别,在outlook和DreamMail里是被篡改的名字类似“xxxxxx.MSWORD”,当然你强制用word打开还是没有问题的,但是始终是不爽!所以今天折腾了半天这个问题!
google了n多帖子,都基本是说设置

set rfc2047_parameters=yes

但是经过测试,这个只能使本地接收到的附件显示为中文附件名!要使自己发送的中文附件名在win下的客户端显示正常,这个不得行!

又google了n多帖子,就出现了最终的解决方法:

set create_rfc2047_parameters=yes

该设置需要在mutt源码中打patch,named patch-1.5.10.tt.create_rfc2047_params.1 点击下载
再到mutt主页上下在最近的mutt包 named mutt-1.5.21.tar.gz猛击下载
解压mutt-1.5.21.tar.gz和patch,解压出的patch放到mutt-1.5.21目录下,在该目录执行

patch -Np1 -i patch-1.5.10.tt.create_rfc2047_params.1

接着再执行

./configure &make &sudo make install

接个就用吧!mutt发送中文附件邮件,在foxmail中显示就ok了!

PS:这里要说一点,在ubuntu下,发送中文附件是没问题,但是用mutt中中文是乱码!这个可能与系统设置有关系,暂时没有处理!

在AWESOME上加入天气使用googleAPI字体weather.ttf

awesome rc.lua

这个脚本加入了词霸的屏幕取词,绑定PrtSc键抓图,还有天气显示!解决了前两天一直琢磨的字体天气问题!
上张现在桌面图:

awesome desktop screenshot

awesome 的配置文件 rc.lua

-- Standard awesome library
require("awful")
require("awful.autofocus")
require("awful.rules")
-- Theme handling library
require("beautiful")
require("vicious")
-- Notification library
require("naughty")
 
-- Load Debian menu entries
require("debian.menu")
 
-- --- Variable definitions
-- Themes define colours, icons, and wallpapers
beautiful.init("/usr/share/awesome/themes/default/theme.lua")
 
-- This is used later as the default terminal and editor to run.
terminal = "x-terminal-emulator"
editor = os.getenv("EDITOR") or "editor"
editor_cmd = terminal .. " -e " .. editor
 
-- Default modkey.
-- Usually, Mod4 is the key with a logo between Control and Alt.
-- If you do not like this or do not have such a key,
-- I suggest you to remap Mod4 to another key using xmodmap or other tools.
-- However, you can use another modifier like Mod1, but it may interact with others.
modkey = "Mod4"
 
-- Table of layouts to cover with awful.layout.inc, order matters.
layouts =
{
    awful.layout.suit.floating,
    awful.layout.suit.tile,
    awful.layout.suit.tile.left,
    awful.layout.suit.tile.bottom,
    awful.layout.suit.tile.top,
    awful.layout.suit.fair,
    awful.layout.suit.fair.horizontal,
    awful.layout.suit.spiral,
    awful.layout.suit.spiral.dwindle,
    awful.layout.suit.max,
    awful.layout.suit.max.fullscreen,
    awful.layout.suit.magnifier
}
-- ___
 
-- --- Tags
-- Define a tag table which hold all screen tags.
tags = {}
for s = 1, screen.count() do
    -- Each screen has its own tag table.
    tags[s] = awful.tag({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, s, layouts[1])
end
-- ___
 
-- --- Menu
-- Create a laucher widget and a main menu
myawesomemenu = {
   { "manual", terminal .. " -e man awesome" },
   { "edit config", editor_cmd .. " " .. awful.util.getdir("config") .. "/rc.lua" },
   { "restart", awesome.restart },
   { "quit", awesome.quit }
}
 
mymainmenu = awful.menu({ items = { { "awesome", myawesomemenu, beautiful.awesome_icon },
                                    { "Debian", debian.menu.Debian_menu.Debian },
                                    { "open terminal", terminal }
                                  }
                        })
 
mylauncher = awful.widget.launcher({ image = image(beautiful.awesome_icon),
                                     menu = mymainmenu })
-- ___
 
-- --- Wibox
-- Create a textclock widget
mytextclock = awful.widget.textclock({ align = "right" },"%x %R",61)
-------------------------------------leave--------------------------
--Spacer
spacer    = widget({ type = "textbox" })
separator = widget({ type = "textbox" })
spacer.text     = " "
separator.text  = "|"
--Cpu text
cpu = widget({ type = "textbox" })
vicious.register(cpu, vicious.widgets.cpu, ' <span color="brown">CPU1:</span> <span color="orange">$2%</span> <span color="brown">CPU2:</span> <span color="orange">$3%</span>', 1)
--Cpu wigth
cpu_g1 = awful.widget.graph()
cpu_g1:set_width(50)
cpu_g1:set_background_color("#333333")
cpu_g1:set_color("#AEC6D8")
cpu_g1:set_border_color("#0a0a0a")
vicious.register(cpu_g1, vicious.widgets.cpu, "$2", 1)
  
cpu_g2 = awful.widget.graph()
cpu_g2:set_width(50)
cpu_g2:set_background_color("#333333")
cpu_g2:set_color("#AEC6D8")
cpu_g2:set_border_color("#0a0a0a")
vicious.register(cpu_g2, vicious.widgets.cpu, "$3", 1)
--MPD
mpd = widget({ type = "textbox" })
vicious.register(mpd, vicious.widgets.mpd,
    function (widget, args)
            local ret_str = ' <span color="brown">NOW Playing:</span>'
        if args["{state}"] == "Stop" then
            return ret_str .. ' <span color="orange"> - </span>'
        else
            return ret_str .. ' <span color="orange">'.. args["{Artist}"]..' - '.. args["{Title}"]..'</span>'
        end
    end, 3)
--Weather
------------------------regex-------------------------
function get_weather(con)
    local s = "<current_conditions>.*<\/forecast_conditions>"
    local i ,j = string.sub(con, string.find(con,s))
    local s = "\"[a-z0-9A-Z%s:%%]*\"" 
    local info
    for word in string.gmatch(i, s) do
            info = tostring(info).."\t"..tostring(word) 
    end
    local i, j = string.gsub(info, "\"","")
    local l = split(i,"\t")
    return l
end
 
------------------------split-------------------------
function split(string, spt)
    local find_index = 1
    local spt_index = 1
    local spt_arr = {}
    while true do
        local find_end = string.find(string, spt, find_index)
        if not find_end then
            spt_arr[spt_index] = string.sub(string, find_index, string.len(string))
            break
        end
        spt_arr[spt_index] = string.sub(string, find_index, find_end - 1)
        find_index = find_end + string.len(spt)
        spt_index = spt_index + 1
    end
    return spt_arr
end
 
------------------------main-------------------------
weathericon = widget({ type = "imagebox" })
weathericon.image = image(beautiful.widget_cloud)
weather = widget({type = "textbox" })
vicious.register(weather, vicious.widgets.weather,
 function (widget, args)
    local http = require("socket.http")
    local night = {["e"]='o',["f"]='p',["v"]='v',["j"]='t',["k"]='u',["h"]='r',["g"]='q',["c"]='m',["b"]='l',["d"]='n',["a"]='a',["N"]='N',["w"]='w'}
    local weather = {["Sandstorm"]='e',["Duststorm"]='e',["Sand"]='e',["Dust"]='e',["Fog"]='e',["Mist"]='e',["Smoke"]='e',["Haze"]='e',["Overcast"]='e',--'e o' 9th 阴霾
    ["Thunderstorms"]='f',["Thundershowers"]='f',["Storm"]='f',["Lightning"]='f',--'f p' 13th 闪电
    ["Hail"]='v',--'v' 14th 冰雹
    ["Blowing Snow"]='j',["Blizzard"]='j',["Snowdrift"]='j',["Snowstorm"]='j',["Snow"]='j',["Heavy Snow"]='j',["Snowfall"]='j', --'j t' 21th 大雪
    ["Snow Showers"]='k',["Flurries"]='k',["Light Snow"]='k',["Sleet"]='k',--'k u' 25th 小雪
    ["Showers"]='h',["Heavy Showers"]='h',["Rainshower"]='h',["Rain"]='h',--'h r' 29th 大雨
    ["Occasional Showers"]='g',["Scattered Showers"]='g',["Isolated Showers"]='g',["Light Showers"]='g',["Freezing Rain"]='g',["Drizzle"]='g',["Light Rain"]='g',--'g q' 36th 小雨
    ["Sunny Interval"]='c',["No Rain"]='c',["Clearing"]='c',--'c m' 39th 多云见晴
    ["Sunny Period"]='b',["Partly Cloudy"]='b',["Partly Bright"]='b',["Mild"]='b',--'b l' 43th 晴见多云
    ["Cloudy"]='d',["Mostly Cloudy"]='d',--'d, n' 45th 多云
    ["Bright"]='a',["Sunny"]='a',["Fair"]='a', --'a' 48th 白天晴天
    ["Fine"]='N',["Clear"]='N',--'N' 50th 晚间晴天
    ["Windy"]='w',["Squall"]='w',["Stormy"]='w',["Chill"]='w',["Gale"]='w'--'w' 55th 风
}
    local url = "http://www.google.com/ig/api?weather=chengdu"
    local con, ret = http.request(url)
    if con == nil then
        return nil
    else
        local l =   get_weather(con)
        local font_weather = weather[l[2]]
--[[        if os.date("%H",os.time())>"17" then
            font_weather = night[font_weather]
        end
        --]]
        local temp_c = 'y'
        if l[4] > "28" then
            temp_c = 'z'
        elseif l[4] < "15" then
            temp_c = 'x'
        end
        return '<span font="Weather regular 16" color="#FFFF00">'..font_weather..'</span><span color="orange"> '..l[4]..'°C</span>'
    end
 end
 
--"${sky} ${tempc} °C"
, 1800,"ZUUU")
 
--UPTIME
uptime = widget({ type = "textbox" })
vicious.register(uptime, vicious.widgets.uptime,' <span color="brown">UPTIME:</span> <span color="orange">$2:$3</span>', 61)
--MEM
mem = widget({ type = "textbox" })
vicious.register(mem, vicious.widgets.mem, ' <span color="brown">MEM:</span> <span color="orange">$1% $2MB</span> <span color="brown">SWAP:</span> <span color="orange">$5% $6MB</span>', 1)
--FS
fs = widget({ type = "textbox" })
vicious.register(fs, vicious.widgets.fs, ' <span color="brown">FS:</span> <span color="orange">${/ used_gb}GB ${/ used_p}%</span>', 5)
--DIO
dio = widget({ type = "textbox" })
vicious.register(dio, vicious.widgets.dio, ' <span color="brown">IO:</span> <span color="orange">R:${read_kb}KB W:${write_kb}KB</span>', 1, "sda")
--NET
net = widget({ type = "textbox" })
vicious.register(net, vicious.widgets.net, ' <span color="brown">NET:</span> <span color="orange">${eth0 down_kb}KB/${eth0 up_kb}KB</span>', 1)
-------------------------------------leave--------------------------
-- Create a systray
mysystray = widget({ type = "systray" })
 
-- Create a wibox for each screen and add it
mywibox_top = {}
mywibox_bottom = {}
mypromptbox = {}
mylayoutbox = {}
mytaglist = {}
mytaglist.buttons = awful.util.table.join(
                    awful.button({ }, 1, awful.tag.viewonly),
                    awful.button({ modkey }, 1, awful.client.movetotag),
                    awful.button({ }, 3, awful.tag.viewtoggle),
                    awful.button({ modkey }, 3, awful.client.toggletag),
                    awful.button({ }, 4, awful.tag.viewnext),
                    awful.button({ }, 5, awful.tag.viewprev)
                    )
mytasklist = {}
mytasklist.buttons = awful.util.table.join(
                     awful.button({ }, 1, function (c)
                                              if not c:isvisible() then
                                                  awful.tag.viewonly(c:tags()[1])
                                              end
                                              client.focus = c
                                              c:raise()
                                          end),
                     awful.button({ }, 3, function ()
                                              if instance then
                                                  instance:hide()
                                                  instance = nil
                                              else
                                                  instance = awful.menu.clients({ width=250 })
                                              end
                                          end),
                     awful.button({ }, 4, function ()
                                              awful.client.focus.byidx(1)
                                              if client.focus then client.focus:raise() end
                                          end),
                     awful.button({ }, 5, function ()
                                              awful.client.focus.byidx(-1)
                                              if client.focus then client.focus:raise() end
                                          end))
 
for s = 1, screen.count() do
    -- Create a promptbox for each screen
    mypromptbox[s] = awful.widget.prompt({ layout = awful.widget.layout.horizontal.leftright })
    -- Create an imagebox widget which will contains an icon indicating which layout we're using.
    -- We need one layoutbox per screen.
    mylayoutbox[s] = awful.widget.layoutbox(s)
    mylayoutbox[s]:buttons(awful.util.table.join(
                           awful.button({ }, 1, function () awful.layout.inc(layouts, 1) end),
                           awful.button({ }, 3, function () awful.layout.inc(layouts, -1) end),
                           awful.button({ }, 4, function () awful.layout.inc(layouts, 1) end),
                           awful.button({ }, 5, function () awful.layout.inc(layouts, -1) end)))
    -- Create a taglist widget
    mytaglist[s] = awful.widget.taglist(s, awful.widget.taglist.label.all, mytaglist.buttons)
 
    -- Create a tasklist widget
    mytasklist[s] = awful.widget.tasklist(function(c)
                                              return awful.widget.tasklist.label.currenttags(c, s)
                                          end, mytasklist.buttons)
 
    -- Create the wibox
    mywibox_top[s] = awful.wibox({ position = "top", screen = s })
    mywibox_bottom[s] = awful.wibox({ position = "bottom", screen = s })
    -- Add widgets to the wibox - order matters
        -- top
         mywibox_top[s].widgets = {
        {
            mylauncher,
            mytaglist[s],
            mypromptbox[s],
            layout = awful.widget.layout.horizontal.leftright
        },
        mylayoutbox[s],
        mytextclock,
        s == 1 and mysystray or nil,
                spacer,
                uptime,
            weather,
                spacer,
                weathericon,
        mytasklist[s],
        layout = awful.widget.layout.horizontal.rightleft
    }
        --bottom
    mywibox_bottom[s].widgets = {
            {
                fs,
                separator,
                mem,
                separator,
                dio,
                separator,
                net,
                separator,
                mpd,
                layout = awful.widget.layout.horizontal.leftright
            },
                cpu_g1.widget,
                spacer,
                cpu_g2.widget,
                spacer,
        layout = awful.widget.layout.horizontal.rightleft
    }
end
-- ___
 
-- --- Mouse bindings
root.buttons(awful.util.table.join(
    awful.button({ }, 3, function () mymainmenu:toggle() end),
    awful.button({ }, 4, awful.tag.viewnext),
    awful.button({ }, 5, awful.tag.viewprev)
))
-- ___
 
-- --- Key bindings
globalkeys = awful.util.table.join(
    awful.key({ modkey,           }, "Left",   awful.tag.viewprev       ),
    awful.key({ modkey,           }, "Right",  awful.tag.viewnext       ),
    awful.key({ modkey,           }, "Escape", awful.tag.history.restore),
 
    awful.key({ modkey,           }, "j",
        function ()
            awful.client.focus.byidx( 1)
            if client.focus then client.focus:raise() end
        end),
    awful.key({ modkey,           }, "k",
        function ()
            awful.client.focus.byidx(-1)
            if client.focus then client.focus:raise() end
        end),
    awful.key({ modkey,           }, "w", function () mymainmenu:show({keygrabber=true}) end),
        -- Unminimize clients
        awful.key({ modkey, "Control" }, "m",
        function ()
            local allclients = client.get(mouse.screen)
            for _, c in ipairs(allclients) do
                if c.minimized and c:tags()[mouse.screen] == awful.tag.selected(mouse.screen) then
                    c.minimized = false
                    client.focus = c
                    c:raise()
                    return
                end
            end
        end),
    -- Layout manipulation
    awful.key({ modkey, "Shift"   }, "j", function () awful.client.swap.byidx(  1)    end),
    awful.key({ modkey, "Shift"   }, "k", function () awful.client.swap.byidx( -1)    end),
    awful.key({ modkey, "Control" }, "j", function () awful.screen.focus_relative( 1) end),
    awful.key({ modkey, "Control" }, "k", function () awful.screen.focus_relative(-1) end),
    awful.key({ modkey,           }, "u", awful.client.urgent.jumpto),
        -- 截图 ---3
        awful.key({ }, "Print", function ()
            -- 截图:全屏
            awful.util.spawn("zsh -c 'cd ~/tmpfs&&scrot fullsc.png'")
            os.execute("sleep .5")
            naughty.notify({title="截图", text="全屏截图已保存。"})
        end),
        awful.key({ "Shift", }, "Print", function ()
            -- 截图:当前窗口
            awful.util.spawn("zsh -c 'cd ~/tmpfs&&scrot -u'")
            os.execute("sleep .5")
            naughty.notify({title="截图", text="当前窗口截图已保存。"})
        end),
 
        -- ---3 sdcv
        awful.key({ modkey }, "d", function ()
            local f = io.popen("xsel -p")
            local new_word = f:read("*a")
            f:close()
 
            if frame ~= nil then
                naughty.destroy(frame)
                frame = nil
                if old_word == new_word then
                    return
                end
            end
            old_word = new_word
 
            local fc = ""
            local f = io.popen("sdcv -n --utf8-output -u 'stardict1.3英汉辞典' "..new_word)
            for line in f:lines() do
                fc = fc .. line .. '\n'
            end
            f:close()
            frame = naughty.notify({ text = fc, timeout = 5, width = 320 })
        end),
    awful.key({ modkey,           }, "Tab",
        function ()
            awful.client.focus.history.previous()
            if client.focus then
                client.focus:raise()
            end
        end),
 
    -- Standard program
    awful.key({ modkey,           }, "Return", function () awful.util.spawn(terminal) end),
    awful.key({ modkey, "Control" }, "r", awesome.restart),
    awful.key({ modkey, "Shift"   }, "q", awesome.quit),
 
    awful.key({ modkey,           }, "l",     function () awful.tag.incmwfact( 0.05)    end),
    awful.key({ modkey,           }, "h",     function () awful.tag.incmwfact(-0.05)    end),
    awful.key({ modkey, "Shift"   }, "h",     function () awful.tag.incnmaster( 1)      end),
    awful.key({ modkey, "Shift"   }, "l",     function () awful.tag.incnmaster(-1)      end),
    awful.key({ modkey, "Control" }, "h",     function () awful.tag.incncol( 1)         end),
    awful.key({ modkey, "Control" }, "l",     function () awful.tag.incncol(-1)         end),
    awful.key({ modkey,           }, "space", function () awful.layout.inc(layouts,  1) end),
    awful.key({ modkey, "Shift"   }, "space", function () awful.layout.inc(layouts, -1) end),
 
    -- Prompt
    awful.key({ modkey },            "r",     function () mypromptbox[mouse.screen]:run() end),
 
    awful.key({ modkey }, "x",
              function ()
                  awful.prompt.run({ prompt = "Run Lua code: " },
                  mypromptbox[mouse.screen].widget,
                  awful.util.eval, nil,
                  awful.util.getdir("cache") .. "/history_eval")
              end)
)
 
clientkeys = awful.util.table.join(
    awful.key({ modkey,           }, "f",      function (c) c.fullscreen = not c.fullscreen  end),
    awful.key({ modkey, "Shift"   }, "c",      function (c) c:kill()                         end),
    awful.key({ modkey, "Control" }, "space",  awful.client.floating.toggle                     ),
    awful.key({ modkey, "Control" }, "Return", function (c) c:swap(awful.client.getmaster()) end),
    awful.key({ modkey,           }, "o",      awful.client.movetoscreen                        ),
    awful.key({ modkey, "Shift"   }, "r",      function (c) c:redraw()                       end),
    awful.key({ modkey,           }, "t",      function (c) c.ontop = not c.ontop            end),
    awful.key({ modkey,           }, "n",      function (c) c.minimized = not c.minimized    end),
    awful.key({ modkey,           }, "m",
        function (c)
            c.maximized_horizontal = not c.maximized_horizontal
            c.maximized_vertical   = not c.maximized_vertical
        end)
)
 
-- Compute the maximum number of digit we need, limited to 9
keynumber = 0
for s = 1, screen.count() do
   keynumber = math.min(9, math.max(#tags[s], keynumber));
end
 
-- Bind all key numbers to tags.
-- Be careful: we use keycodes to make it works on any keyboard layout.
-- This should map on the top row of your keyboard, usually 1 to 9.
for i = 1, keynumber do
    globalkeys = awful.util.table.join(globalkeys,
        awful.key({ modkey }, "#" .. i + 9,
                  function ()
                        local screen = mouse.screen
                        if tags[screen][i] then
                            awful.tag.viewonly(tags[screen][i])
                        end
                  end),
        awful.key({ modkey, "Control" }, "#" .. i + 9,
                  function ()
                      local screen = mouse.screen
                      if tags[screen][i] then
                          awful.tag.viewtoggle(tags[screen][i])
                      end
                  end),
        awful.key({ modkey, "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          awful.client.movetotag(tags[client.focus.screen][i])
                      end
                  end),
        awful.key({ modkey, "Control", "Shift" }, "#" .. i + 9,
                  function ()
                      if client.focus and tags[client.focus.screen][i] then
                          awful.client.toggletag(tags[client.focus.screen][i])
                      end
                  end))
end
 
clientbuttons = awful.util.table.join(
    awful.button({ }, 1, function (c) client.focus = c; c:raise() end),
    awful.button({ modkey }, 1, awful.mouse.client.move),
    awful.button({ modkey }, 3, awful.mouse.client.resize))
 
-- Set keys
root.keys(globalkeys)
-- ___
 
-- --- Rules
awful.rules.rules = {
    -- All clients will match this rule.
    { rule = { },
      properties = { border_width = beautiful.border_width,
                     border_color = beautiful.border_normal,
                     focus = true,
                     keys = clientkeys,
                     buttons = clientbuttons } },
    { rule = { class = "MPlayer" },
      properties = { floating = true } },
    { rule = { class = "pinentry" },
      properties = { floating = true } },
    { rule = { class = "gimp" },
      properties = { floating = true } },
    -- Set Firefox to always map on tags number 2 of screen 1.
    -- { rule = { class = "Firefox" },
    --   properties = { tag = tags[1][2] } },
}
-- ___
 
-- --- Signals
-- Signal function to execute when a new client appears.
client.add_signal("manage", function (c, startup)
    -- Add a titlebar
    -- awful.titlebar.add(c, { modkey = modkey })
 
    -- Enable sloppy focus
    c:add_signal("mouse::enter", function(c)
        if awful.layout.get(c.screen) ~= awful.layout.suit.magnifier
            and awful.client.focus.filter(c) then
            client.focus = c
        end
    end)
 
    if not startup then
        -- Set the windows at the slave,
        -- i.e. put it at the end of others instead of setting it master.
        -- awful.client.setslave(c)
 
        -- Put windows in a smart way, only if they does not set an initial position.
        if not c.size_hints.user_position and not c.size_hints.program_position then
            awful.placement.no_overlap(c)
            awful.placement.no_offscreen(c)
        end
    end
end)
 
client.add_signal("focus", function(c) c.border_color = beautiful.border_focus end)
client.add_signal("unfocus", function(c) c.border_color = beautiful.border_normal end)
-- ___
floatapps =
{
    ["MPlayer"] = true,
    ["gimp"] = true,
    ["smplayer"] = true,
    ["mocp"] = true,
    ["Codeblocks"] = true,
    ["Dialog"] = true,
    ["Download"] = true,
    ["empathy"] = true,
}
 
-- 把指定的程序自动移动到某个特定的屏幕的某个tag上面
apptags =
{
    ["smplayer"] = { screen = 1, tag = 7 },
    ["amarokapp"] = { screen = 1, tag = 8 },
    ["VirtualBox"] = { screen = 1, tag = 9 },
    ["Firefox"] = { screen = 1, tag = 1},
    ["Thunderbird-bin"] = { screen = 1, tag = 7 },
    ["Linux-fetion"] = { screen = 1, tag = 6 },
}

awesome 脚本中加入天气

将这几天做的lua脚本和入到awesome脚本中。
目前缺点:可以获取天气信息,不过天气是文字形式的!

相关函数

------------------------regex-------------------------
function get_weather(con)
    local s = "<current_conditions>.*<\/forecast_conditions>"
    local i ,j = string.sub(con, string.find(con,s))
    local s = "\"[a-z0-9A-Z%s:%%]*\"" 
    local info
    for word in string.gmatch(i, s) do
            info = tostring(info).."\t"..tostring(word) 
    end
    local i, j = string.gsub(info, "\"","")
    local l = split(i,"\t")
    return l
end
 
------------------------split-------------------------
function split(string, spt)
    local find_index = 1
    local spt_index = 1
    local spt_arr = {}
    while true do
        local find_end = string.find(string, spt, find_index)
        if not find_end then
            spt_arr[spt_index] = string.sub(string, find_index, string.len(string))
            break
        end
        spt_arr[spt_index] = string.sub(string, find_index, find_end - 1)
        find_index = find_end + string.len(spt)
        spt_index = spt_index + 1
    end
    return spt_arr
end

添加控件widget

weather = widget({type = "textbox" })
vicious.register(weather, vicious.widgets.weather,
 function (widget, args)
    local http = require("socket.http")
    local url = "http://www.google.com/ig/api?weather=chengdu"
    local con, ret = http.request(url)
    if con == nil then
        return nil
    else
        local l =   get_weather(con)
        return '<span color="brown">WEATHER:</span><span  color="orange">'..l[2]..' '..l[4]..'°C</span>'
    end
 end
, 3600,"ZUUU")

添加到盘pannel

mywibox_top[s].widgets = {
       {
           mylauncher,
           mytaglist[s],
           mypromptbox[s],
           layout = awful.widget.layout.horizontal.leftright
       },
       mylayoutbox[s],
       mytextclock,
       s == 1 and mysystray or nil,
               spacer,
               uptime,
                   weather,
               spacer,
               weathericon,
       mytasklist[s],
       layout = awful.widget.layout.horizontal.rightleft
   }

lua通过google的天气api获得天气

lua获取天气

昨天写了一个lua的天气脚本,主要是获取http://qq.ip138.com天气信息,基本功能都已实现,但是唯一不足之处,就是不能显示当前的天气及气温。所以今天在网上找了下这方面的资料,发现google天气API获得满足了这个需求,于是就有折腾了下。google API特点:当天气和气温是实时的,明后几天是温度区间。

上代码先

#!/usr/bin/lua5.1
local http = require("socket.http")
------------------------regex-------------------------
function get_weather(con)
    local s = "<current_conditions>.*<\/forecast_conditions>"
    local i ,j = string.sub(con, string.find(con,s))
    local s = "\"[a-z0-9A-Z%s:%%]*\"" 
    local info
    for word in string.gmatch(i, s) do
            info = tostring(info).."\t"..tostring(word) 
    end
    local i, j = string.gsub(info, "\"","")
    local l = split(i,"\t")
    return l
end
 
------------------------split-------------------------
function split(string, spt)
    local find_index = 1
    local spt_index = 1
    local spt_arr = {}
    while true do
        local find_end = string.find(string, spt, find_index)
        if not find_end then
            spt_arr[spt_index] = string.sub(string, find_index, string.len(string))
            break
        end
        spt_arr[spt_index] = string.sub(string, find_index, find_end - 1)
        find_index = find_end + string.len(spt)
        spt_index = spt_index + 1
    end
    return spt_arr
end
 
------------------------main-------------------------
function main()
    local url = "http://www.google.com/ig/api?weather=chengdu"
    local con, ret = http.request(url)
    if con == nil then
        print ("nil")
    else
        local l =   get_weather(con)
        print(l[2],l[3],l[4],l[5],l[6])
    end
end
 
main()

数组中包含了最近几天的天气信息,没有像昨天的那个脚本那样进行分类,这是一个遗憾,不过下标的位置是固定的,这也方便了取值!

lua 获得天气信息

其实开始是想做在awesome statbar显示天气的(字体图标的那种),现在终端获得天气已经写好!

明天再着手搞字体的转换问题!

#!/usr/bin/lua5.1
local http = require("socket.http")
------------------------split-------------------------
function get_weather(con)
    local s = "<b>.*<br\/><br\/>"
    local i ,j = string.sub(con, string.find(con,s))
    local i, n = string.gsub(i, "<br\/><br\/><b>","\n")
    local i, j = string.gsub(i, "<br\/>","\t")
    local i, j = string.gsub(i, "<\/b>","")
    local i, j = string.gsub(i, "<b>","")
    local i, j = string.gsub(i, "℃","°C")
    local i, j = string.gsub(i, "~","-")
    --  print(i,n+1)--所有天气
    local list_n = split(i, "\n")
    local list_t = split(list_n[1],"\t")
    return list_t
end
 
------------------------split-------------------------
function split(string, spt)
    local find_index = 1
    local spt_index = 1
    local spt_arr = {}
    while true do
        local find_end = string.find(string, spt, find_index)
        if not find_end then
            spt_arr[spt_index] = string.sub(string, find_index, string.len(string))
            break
        end
        spt_arr[spt_index] = string.sub(string, find_index, find_end - 1)
        find_index = find_end + string.len(spt)
        spt_index = spt_index + 1
    end
    return spt_arr
end
 
------------------------main-------------------------
function main()
    local url = "http://qq.ip138.com/weather/sichuan/chengdu.wml"
    local con, ret = http.request(url)
    if con == nil then
        print ("nil")
    else
    --  print (con)
        local list_t =  get_weather(con)
        print ('今天天气:',list_t[1],list_t[2],list_t[3],list_t[4])
    end
end
 
main()

关于awesome设置时间格式的问题!

支持版本

awesome 3.4.5 以上

背景

今天突然想修改下awesome的系统时间显示,google了下,基本都是手册上所说!

格式%a %b %d, %H:%M 为awesome默认配置( 在/etc/xdg/awesome/rc.lua中并没有这么书写而是

mytextclock = awful.widget.textclock({ align = "right" }) 

后面的格式和时间刷新省略了,默认刷新是60秒),看到%a %b %d, %H:%M,让我想到了系统命令date的参数,之前曾经处理过类似的格式显示,所以这次直接man date,果然,其中的格式控制和上面完全吻合,猜想lua脚本中使用日期格式控制应该是调用系统命令date的格式控制。

具体代码

了解到之后就在rc.lua中修改

mytextclock = awful.widget.textclock({ align = "right" },"%x %X",1)

正如预期结果一样,时间显示为(01/26/11 17:13:48)!看来lua语言还是调用底层的系统函数来实现日期时间的格式的。(lua也在偷懒)

statfs获得硬盘使用情况 模拟linux命令 df (2011.3.24修正)

statfs:

结构:

#include <sys/vfs.h>    /* 或者 <sys/statfs.h> */

int statfs(const char *path, struct statfs *buf);
int fstatfs(int fd, struct statfs *buf);

参数:

`path`: 位于需要查询信息的文件系统的文件路径名。    
`fd`: 位于需要查询信息的文件系统的文件描述词。
`buf`:以下结构体的指针变量,用于储存文件系统相关的信息

struct statfs {
   long    f_type;     /* 文件系统类型  */
   long    f_bsize;    /* 经过优化的传输块大小  */
   long    f_blocks;   /* 文件系统数据块总数 */
   long    f_bfree;    /* 可用块数 */
   long    f_bavail;   /* 非超级用户可获取的块数 */
   long    f_files;    /* 文件结点总数 */
   long    f_ffree;    /* 可用文件结点数 */
   fsid_t  f_fsid;     /* 文件系统标识 */
   long    f_namelen;  /* 文件名的最大长度 */
};

statfs结构中可用空间块数有两种f_bfreef_bavail,前者是硬盘所有剩余空间,后者为非root用户剩余空间,ext3文件系统给root用户分有5%的独享空间,所以这里是不同的地方。这里要强调的是每块的大小一般是4K。因此,要实现与df结果一致的就得在获得块数上乘以4,这样已用、可用、总块数就可以实现。如果还要实现百分比一致,那么要注意的是,df命令获得是整数百分比,没有小数,这里使用的进一法,而不是四舍五入法。所以在程序里直接+1取整。

下面是实现的一个例子:(home目录为一个独立分区)
#include <stdio.h>
#include <sys/vfs.h>

int main()
{
    struct statfs sfs;
    int i = statfs("/home", &sfs);
    int percent = (sfs.f_blocks - sfs.f_bfree ) * 100 / (sfs.f_blocks - sfs.f_bfree + sfs.f_bavail) + 1;
    printf("/dev/sda11            %ld    %ld  %ld   %d%% /home\n",
                           4*sfs. f_blocks, 4*(sfs.f_blocks - sfs.f_bfree),      4*sfs.f_bavail, percent);
    system("df /home ");
    return 0;

}

执行结果:

leave@LEAVE:~/test$ gcc -o df df.c
leave@LEAVE:~/test$ ./df
/dev/sda11            42773008    540356  40059864   2% /home
文件系统           1K-块        已用     可用 已用% 挂载点
/dev/sda11            42773008    540356  40059864   2% /home
leave@LEAVE:~/test$

—————————————–busybox 中使用的挂载分区获取使用率—————————–

#include <stdio.h>
#include <sys/vfs.h>

#include <string.h>


extern int get_free_rate(char *path)
{
 struct statfs str_diskatr;
 long blocks_percent_used=0;
 long blocks_used=0;
 memset( &str_diskatr, 0x00, sizeof(str_diskatr) );

 if ( 0x00 == statfs( path, &str_diskatr ) )
 {
  if ( (str_diskatr.f_blocks != 0x00) ){
   blocks_used = str_diskatr.f_blocks - str_diskatr.f_bfree;
   blocks_percent_used = (((long long) blocks_used) * 100
     + (blocks_used + str_diskatr.f_bavail)/2
     ) / (blocks_used + str_diskatr.f_bavail);
   return blocks_percent_used;
  }
  else
   return -1;
 }
 else if(-1 == statfs( path, &str_diskatr ))
 {
  if(errno == ENOENT)
  {
   return 0;
  }
 }
 else
 {return -1;}

 return -1;
}

上面程序裁剪自busybox,使用

#define IDEDIR  "/mnt/ide/"   //挂载目录

(int )ide_useage = get_free_rate(IDEDIR);

———————————-计算文件夹占用空间大小———————————————–

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

static unsigned int total = 0;

int sum(const char *fpath, const struct stat *sb, int typeflag)
{

    total += sb->st_size;
    return 0;

}


int main(int argc, char **argv)
{

    if (!argv[1] || access(argv[1], R_OK)) {

        return 1;

    }

    if (ftw(argv[1], &sum, 1)) {

        perror("ftw");

        return 2;

    }

    printf("%s: %u\n", argv[1], total);

    return 0;

}

执行结果:

lm@LM:/home/lm/tmpfs/c_c++> gcc -o du  du.c            11-03-24 10:06
lm@LM:/home/lm/tmpfs/c_c++> ./du /home/lm/音乐         11-03-24 10:22
/home/lm/音乐: 726629477
lm@LM:/home/lm/tmpfs/c_c++>                            11-03-24 10:22
  • (2010.6.29修正 添加busybox中的系统算法)
  • (2011.3.24修正 添加获取文件夹的大小)

使用 memleak 检查和调试内存泄漏

memleak

使用范围:

小型的嵌入式应用中经常会出现许多内存问题,很多情况下难以定位问题出现在哪里。
我在 sourceforge上找了些检测 C 内存泄漏的工具,感觉比较易用的是 memleak,下面就来简要介绍一下它的使用。

用法:

下载得到的 memleak 压缩包大小不到 15 kB,解压后只有两个文件:memleak.cmemleak.h。在使用过程中只需要包含头文件 memleak.h 就可以使用 memleak 提供的几个易用而有效的内存检测函数了。

memleak 的原理是利用 C 语言的宏调用来替代原有的函数调用,比如我们在代码中调用 malloc(s),实际是调用了:dbg_malloc(s),这个宏定义在 memleak.h 中给出:
#define malloc(s) (FILE_LINE, dbg_malloc(s))

memleak 维护了一个链表,在这个链表中保存着程序中对内存函数调用的记录,这些函数包括:malloccallocreallocfree。每次调用这些函数时,就会更新这个链表。
有 了这个表,我们就可以在适当的位置调用 memleak 提供的函数,显示一些重要的信息,包括 malloccallocreallocfree调用的次数,申请及分配的内存数,调用的文件和位置等等,信息非常详细。有了这些功能,我们就很容 易定位内存使用的错误源。

由于 memleak 在某些交叉编译器下不能正常编译通过,这里我将 memleak.c 中的结构体 struct head 修改如下:

struct head
{
    struct head *addr;
    size_t size;
    char *file;
    unsigned line;
    /* two addresses took the same space as an address and an integer on many archs => usable */
    union lf {
        struct { struct head*prev, *next; } list;
        struct { char *file; unsigned line; } free;
    } u;
};

memleak.c 文件中其它调用到 head 中共用体 u 的地方也要做相应的修改。
修改后的文件可以点击这里下载。

memleak 提供了以下几个函数接口:

extern void dbg_init(int history_length);
extern int dbg_check_addr(char *msg, void *ptr, int opt);
extern void dbg_mem_stat(void);
extern void dbg_zero_stat(void);
extern void dbg_abort(char *msg);
extern void dbg_heap_dump(char *keyword);
extern void dbg_history_dump(char *keyword);
extern void dbg_catch_sigsegv(void);

详细的介绍请查看 memleak.c 头部的注释或查看源代码理解。

下面举个简单的例子:

#include "memleak.h"
int main(void)
{
    char * s, * t;
    dbg_init(10);
    s = (char *)malloc(100);    // 申请 100 bytes
    t = (char *)malloc(11);     // 再申请 11 bytes
    free(s);                    // 释放 100 bytes
    s = (char *)malloc(80);     // 重新申请 80 bytes
    dbg_heap_dump("");          // 显示调用栈
    dbg_mem_stat();             // 显示调用统计
    free(t);                    // 释放 11 bytes
    free(s);                    // 释放 80 bytes
    dbg_mem_stat();             // 再次显示调用统计
    return 0;
}

编辑后保存为 test.c,与 memleak.cmemleak.h 放于同一目录下。
然后编写一 Makefile

CC = gcc
EXEC = test
CSRC = test.c memleak.c
OBJS = $(patsubst %.c,%.o, $(CSRC))
all: $(EXEC)
$(EXEC): $(OBJS)
$(CC) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $@
$(OBJS): %.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
-rm -f $(EXEC) *.elf *.gdb *.o

也保存于同一目录,在该目录下 make 编译,执行 ./test 后输出如下:

\***** test.c:14: heap dump start
(alloc: test.c:11 size: 11)
(alloc: test.c:13 size: 80)
\***** test.c:14: heap dump end
test.c:15: m: 3, c: 0, r: 0, f: 1, mem: 91
test.c:18: m: 3, c: 0, r: 0, f: 3, mem: 0

怎么样,很简单吧?

memleak 中还有一个函数 dbg_catch_sigsegv(void),可以绑定系统出现 SIGSEGV 信号时的处理函数,我们可以通过修改 memleak.c 中的 sigsegv_handler,自定义这个 SIGSEGV 信号处理函数。不知道 uClinux 下的 SIGSEGV 信号是否也存在,有的话调试一些内存问题就更容易了。

最后从网上摘来一段 SIGSEGV 的介绍:

SIGSEGV --- Segment Fault. The possible cases of your encountering this error are:
1.buffer overflow --- usually caused by a pointer reference out of range.
2.stack overflow --- please keep in mind that the default stack size is 8192K.
3.illegal file access --- file operations are forbidden on our judge system.

A place nearby - Lene Marlin

A place nearby - Lene Marlin 天堂若比邻(琳恩.玛莲)


I entered the room.我走进房间。
Sat by Ur bed all through the night.坐在你床边整夜未眠。
I watched Ur daily fight.回望你曾有的坚强 。
I hardly knew.我终于明白。
The pain was almost more than I couldbear.那种疼痛我无法承受。
And still I hear.而今我仍能听到。
Ur last words 2 me.你最后的话语。

Heaven is a place nearby.天堂并不遥远。
So I won’t be so far away.我也不会走远。
And if U try & look 4 me.你若要找我。
Maybe U’ll find me someday.某一天 你终能把我找到。
So there’s no need 2 say goodbye.所以没必要说再见。
I wanna ask U not 2 cry.请别为我哭泣。
I’ll always be by Ur side ! 我将永远守在你身边!

U just faded away.你已慢慢走远。
U spread Ur wings U had flown.展开羽翼飞向天边。
Away 2 something unknown.在冥冥苍穹。
Wish I could bring U back.我好希望能把你找回。
U are always on my mind.你是我的思绪。
About 2 tear myself apart.与我难以割舍。
U have Ur special place in my heart.占有我心中最特殊的角落。

Always heaven is a place nearby.天堂并不遥远!
And even when I go 2 sleep.即使我睡着了 梦中。
I still can hear Ur voice.你的声音依旧。
And those words.话语依然。
I never will forget.我亦从未忘记。