elua: sanitize keywords and extract info in xgettext (include default keywords for...
authorDaniel Kolesa <d.kolesa@samsung.com>
Fri, 23 May 2014 13:13:34 +0000 (14:13 +0100)
committerDaniel Kolesa <d.kolesa@samsung.com>
Tue, 10 Jun 2014 14:48:51 +0000 (15:48 +0100)
src/bin/elua/apps/xgettext.lua
src/bin/elua/core/gettext.lua

index 5953fef..0f02c32 100644 (file)
@@ -188,13 +188,95 @@ if not opts or opts["h"] or opts["v"] then
     return true
 end
 
+-- default keywords
+if #keywords == 0 then
+    keywords = { "_", "gettext.gettext"      , "gettext.dgettext:2",
+                      "gettext.dcgettext:2"  , "gettext.ngettext:1,2",
+                      "gettext.dngettext:2,3", "gettext.dcngettext:2,3" }
+end
+
 -- transform some lists into mappings
 for i = 1, #excluded_files do
     excluded_files[excluded_files[i]] = true
     excluded_files[i] = nil
 end
 for i = 1, #keywords do
-    keywords[keywords[i]] = true
+    local  kw = keywords[i]
+    local  kwb, specs = kw:match("^(.+):(.+)$")
+    local  n1, n1c, n2, n2c, n3, n3c, xcmt, argnum
+    if not kwb then
+        kwb = kw
+    else
+        n1, n1c, n2, n2c, n3, n3c, xcmt
+            = specs:match("^(%d+)(c?),(%d+)(c?),(%d+)(c?)(.*)$")
+        if not n1 then
+            n1, n1c, n2, n2c, xcmt = specs:match("^(%d+)(c?),(%d+)(c?)(.*)$")
+            if not n1 then
+                n1, n1c, xcmt = specs:match("^(%d+)(c?)(.*)$")
+                if not n1 then error("invalid keyword specifier") end
+            end
+        end
+    end
+    -- all matched, sanitize now
+    if n1c == "" then n1c = nil end
+    if n2c == "" then n2c = nil end
+    if n3c == "" then n3c = nil end
+    -- sanitize/retrieve comment and potential total number of args
+    if n3 and xcmt == "t" then
+        if n3c then error("invalid keyword specifier") end
+        argnum = n3
+        n3 = nil
+    elseif n2 and xcmt == "t" then
+        if n2c then error("invalid keyword specifier") end
+        argnum = n2
+        n2 = nil
+    elseif n1 and xcmt == "t" then
+        if n1c then error("invalid keyword specifier") end
+        argnum = n1
+        n1 = nil
+    elseif xcmt ~= "" then
+        local xcmtm, rest = xcmt:match('^,"(.+)"(.*)$')
+        if not xcmtm then
+            xcmtm, rest = xcmt:match("^,'(.+)'(.*)$")
+        end
+        if xcmtm then
+            xcmt = xcmtm
+        else
+            rest = xcmt
+            xcmt = nil
+        end
+        argnum = rest:match("^,(%d+)t$")
+        -- the rest doesn't match either comment nor argnum nor both
+        if not xcmt and not argnum then
+            error("invalid keyword specifier")
+        end
+    else
+        xcmt = nil
+    end
+    -- allow only one context arg
+    if (n1c and n2c) or (n2c and n3c) or (n1c and n3c) then
+        error("invalid keyword specifier")
+    end
+    -- retrieve context
+    local context
+    if n1c then
+        context = tonumber(n1)
+        n1 = n2
+        n2 = n3
+        n3 = nil
+    elseif n2c then
+        context = tonumber(n2)
+        n2 = n3
+        n3 = nil
+    elseif n3c then
+        context = tonumber(n3)
+    elseif n1 and n2 and n3 then -- 3 regular args, forbidden
+        error("invalid keyword specifier")
+    end
+
+    -- all sanitized, store :)
+    keywords[kwb] = { context = context, argnum = tonumber(argnum),
+        xcomment = xcmt, tonumber(n1), tonumber(n2) }
     keywords[i] = nil
 end
 
index b993e54..8f446e5 100644 (file)
@@ -6,9 +6,10 @@ local M = {}
 
 local gettext = ...
 
-local bind_textdomain = gettext.bind_textdomain
-local dgettext        = gettext.dgettext
-local dngettext       = gettext.dngettext
+local bind_textdomain         = gettext.bind_textdomain
+local bind_textdomain_codeset = gettext.bind_textdomain_codeset
+local dgettext                = gettext.dgettext
+local dngettext               = gettext.dngettext
 
 if  dgettext then
     dgettext  = ffi.cast("char *(*)(const char*, const char*)", dgettext)
@@ -57,6 +58,7 @@ if dgettext then
         end
         return ffistr(lmsgid)
     end
+    M.dgettext = M.gettext
     M.ngettext = function(dom, msgid, plmsgid, n)
         if not n then
             plmsgid = msgid
@@ -78,6 +80,7 @@ if dgettext then
         end
         return ffistr(lmsgid)
     end
+    M.dngettext = M.ngettext
 else
     M.gettext  = function(dom, msgid) return msgid end
     M.ngettext = function(dom, msgid, plmsgid, n)