[elinks-dev] Re: Re: Bug#255871: [patch] Lua 5 compatibility

Miciah D.B. Masters miciah at myrealbox.com
Sat Jun 26 17:24:07 PDT 2004


Sorry for the duplicate message, pasky; I planned to send
the patch to the Debian BTS and the rest to you, then I saw
the elinks-dev, and now I think I'll just send it all to everyone.

On 20040626 14:59:21, Petr Baudis <pasky at ucw.cz> wrote:
> Dear diary, on Sat, Jun 26, 2004 at 10:54:00AM CEST, I got a letter,
> where "Miciah D.B. Masters" <miciah at myrealbox.com> told me, that...
> > Index: contrib/lua/hooks.lua.in
...
> Could you please add this patch to contrib/lua ?

That is an awful solution, but I don't know what is better.
We could put the API compatibility code in lua5-compat.lua
and instructions on converting the upvals in another file.
We can commit the removal of |ret| without loss of Lua 4
compatibility.  How does that sound?

> > Index: src/scripting/lua/core.c
...
> Couldn't we have this committed with some surrounding #ifdef ?

Check whether #ifdef lua_tablibopen or similar? Can autoconf
can take care of this?

Sorry for the half-hearted patch before; here is one
against the 0.9.1 orig.tar.gz with Debian's diff.gz applied,
so the offsets should be correct, and with an additional fix:
Drop the math-lib stuff from hooks.lua.in,
as 0.9.1 still has the mod function defined in Lua code
and doesn't open math-lib in src/scripting/lua/core.c.

BTW, does anybody know how to get os.tmpname working?

 -- Miciah <miciah at myrealbox.com>
-------------- next part --------------
diff -ur elinks-0.9.1/contrib/lua/hooks.lua.in elinks-0.9.1-lua5/contrib/lua/hooks.lua.in
--- elinks-0.9.1/contrib/lua/hooks.lua.in	2003-12-21 21:28:39.000000000 +0000
+++ elinks-0.9.1-lua5/contrib/lua/hooks.lua.in	2004-06-27 00:08:19.000000000 +0000
@@ -6,6 +6,58 @@
 -- Take care about @SOMETHING@, we're processed with autoconf!
 
 ----------------------------------------------------------------------
+--  Lua 5 compatibility (from /usr/share/lua50/compat.lua)
+----------------------------------------------------------------------
+
+getn = table.getn
+tinsert = table.insert
+
+local str = string
+strbyte = str.byte
+strchar = str.char
+strfind = str.find
+format = str.format
+gsub = str.gsub
+strlen = str.len
+strlower = str.lower
+strsub = str.sub
+strupper = str.upper
+
+getenv = os.getenv
+remove = os.remove
+tmpname = os.tmpname
+
+function writeto (name)
+  if name == nil then
+    local f, err, cod = io.close(_OUTPUT)
+    _OUTPUT = io.stdout
+    return f, err, cod
+  else
+    local f, err, cod = io.open(name, "w")
+    _OUTPUT = f or _OUTPUT
+    return f, err, cod
+  end
+end
+
+function write (...)
+  local f = _OUTPUT
+  if type(arg[1]) == 'userdata' then
+    f = tab.remove(arg, 1)
+  end
+  return f:write(unpack(arg))
+end
+
+function do_ (f, err)
+  if not f then print(err); return end
+  local a,b = pcall(f)
+  if not a then print(b); return nil
+  else return b or true
+  end
+end
+
+function dofile(s) return do_(loadfile(s)) end
+
+----------------------------------------------------------------------
 --  Load configuration
 ----------------------------------------------------------------------
 
@@ -91,7 +143,7 @@
 function debian_contents (url)
 	local t = {}
 
-	url = gsub(url, '(%w+):(%w+)', function (key, val) %t[key] = val end)
+	url = gsub(url, '(%w+):(%w+)', function (key, val) t[key] = val end)
 
 	return 'http://packages.debian.org/cgi-bin/search_contents.pl?word='
 	..escape(gsub(url, '%s*([^%s]+)%s*', '%1'))
@@ -111,7 +163,7 @@
 	if replacements == 0 then url = gsub(url, "^(.*):(.*)$", "%1 %2") end
 
 	-- split into words
-	gsub(url, "([^%s]+)", function (w) tinsert(%t, w) end)
+	gsub(url, "([^%s]+)", function (w) tinsert(t, w) end)
 	file, old, new = t[1], t[2], t[3]
 
 	if t[4] then error('this smartprefix takes only two to three arguments') return nil end
@@ -281,15 +333,12 @@
 end
 
 function highlight (url, html)
-  local ret=nil
-
 -- highlight patches
   if strfind (url, "%.patch$") then
 	local tmp = tmpname ()
 	writeto (tmp) write (html) writeto()
 	html = pipe_read ("(code2html -l patch "..tmp.." - ) 2>/dev/null")
 	remove(tmp)
-	ret = 1
   end
 
 -- Python
@@ -298,7 +347,6 @@
 	writeto (tmp) write (html) writeto()
 	html = pipe_read ("(code2html -l python "..tmp.." - ) 2>/dev/null")
 	remove(tmp)
-	ret = 1
   end
 
 -- Perl
@@ -307,7 +355,6 @@
 	writeto (tmp) write (html) writeto()
 	html = pipe_read ("(code2html -l perl "..tmp.." - ) 2>/dev/null")
 	remove(tmp)
-	ret = 1
   end
 
 -- awk
@@ -316,7 +363,6 @@
 	writeto (tmp) write (html) writeto()
 	html = pipe_read ("(code2html -l awk "..tmp.." - ) 2>/dev/null")
 	remove(tmp)
-	ret = 1
   end
 
 -- hightlight C code
@@ -325,7 +371,6 @@
         writeto (tmp) write (html) writeto ()
         html = pipe_read ("(code2html -l c "..tmp.." - ) 2>/dev/null")
         remove (tmp)
-        ret = 1
     end
 
 -- shell
@@ -334,15 +379,12 @@
         writeto (tmp) write (html) writeto ()
         html = pipe_read ("(code2html -l sh "..tmp.." - ) 2>/dev/null")
         remove (tmp)
-        ret = 1
     end
-    return ret, html
+    return html
 end
 
 
 function pre_format_html_hook (url, html)
-    local ret = nil
-
     -- Handle gzip'd files within reasonable size.
     -- Note that this is not needed anymore since we have a support for this
     -- in core ELinks. I still keep it here for a reference (as an example),
@@ -353,33 +395,26 @@
 --        writeto (tmp) write (html) writeto ()
 --        html = pipe_read ("(gzip -dc "..tmp.." || cat "..tmp..") 2>/dev/null")
 --        remove (tmp)
---        ret = 1
 --    end
 
     -- Highlight the source code
     if highlight_enable then
-        ret, html = highlight(url, html)
+        html = highlight(url, html)
     end
 
     -- Mangle ALT="" in IMG tags.
     if mangle_blank_alt then
-	local n
-	html, n = gisub (html, '(<img.-) alt=""', '%1 alt=" "')
-	ret = ret or (n > 0)
+	html = gisub (html, '(<img.-) alt=""', '%1 alt=" "')
     end
 
     -- Fix unclosed INPUT tags.
     if 1 then
-        local n
-	html, n = gisub (html, '(<input[^>]-[^=]")<', '%1><')
-	ret = ret or (n > 0)
+	html = gisub (html, '(<input[^>]-[^=]")<', '%1><')
     end
 
     -- Fix unclosed A tags.
     if 1 then
-        local n
-	html, n = gisub (html, '(<a[^>]-[^=]")<', '%1><')
-	ret = ret or (n > 0)
+	html = gisub (html, '(<a[^>]-[^=]")<', '%1><')
     end
 
     -- These quick 'n dirty patterns don't maintain proper HTML.
@@ -401,7 +436,7 @@
     elseif sstrfind (url, "dictionary.com/cgi-bin/dict.pl") then
 	local t = { t = "" }
 	local _, n = gsub (html, "resultItemStart %-%-%>(.-)%<%!%-%- resultItemEnd",
-			   function (x) %t.t = %t.t.."<tr><td>"..x.."</td></tr>" end)
+			   function (x) t.t = t.t.."<tr><td>"..x.."</td></tr>" end)
 	if n == 0 then
 	    -- we've already mangled this page before
 	    return html
@@ -414,7 +449,7 @@
         return gsub(html, "javascript:z%('(.-)'%)", "/cg/amg.dll?p=amg&sql=%1")
     end
 
-    return ret and html
+    return html
 end
 
 
diff -ur elinks-0.9.1/src/scripting/lua/core.c elinks-0.9.1-lua5/src/scripting/lua/core.c
--- elinks-0.9.1/src/scripting/lua/core.c	2003-12-21 14:13:20.000000000 +0000
+++ elinks-0.9.1-lua5/src/scripting/lua/core.c	2004-06-26 23:34:03.000000000 +0000
@@ -516,6 +516,7 @@
 	L = lua_open(0);
 	lua_baselibopen(L);
 	lua_strlibopen(L);
+	lua_tablibopen(L);
 	lua_register(L, LUA_ALERT, l_alert);
 	lua_register(L, "current_url", l_current_url);
 	lua_register(L, "current_link", l_current_link);


More information about the elinks-dev mailing list