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

Petr Baudis pasky at ucw.cz
Sat Jun 26 05:59:21 PDT 2004


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
> ===================================================================
> RCS file: /var/cvs/elinks/elinks/contrib/lua/hooks.lua.in,v
> retrieving revision 1.27
> diff -u -r1.27 hooks.lua.in
> --- contrib/lua/hooks.lua.in	1 May 2004 05:29:36 -0000	1.27
> +++ contrib/lua/hooks.lua.in	26 Jun 2004 08:42:59 -0000
> @@ -6,6 +6,61 @@
>  -- 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 math = math
> +mod = math.mod
> +
> +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
>  ----------------------------------------------------------------------
>  
> @@ -84,7 +139,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'))
> @@ -104,7 +159,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
> @@ -274,15 +329,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
> @@ -291,7 +343,6 @@
>  	writeto (tmp) write (html) writeto()
>  	html = pipe_read ("(code2html -l python "..tmp.." - ) 2>/dev/null")
>  	remove(tmp)
> -	ret = 1
>    end
>  
>  -- Perl
> @@ -300,7 +351,6 @@
>  	writeto (tmp) write (html) writeto()
>  	html = pipe_read ("(code2html -l perl "..tmp.." - ) 2>/dev/null")
>  	remove(tmp)
> -	ret = 1
>    end
>  
>  -- awk
> @@ -309,7 +359,6 @@
>  	writeto (tmp) write (html) writeto()
>  	html = pipe_read ("(code2html -l awk "..tmp.." - ) 2>/dev/null")
>  	remove(tmp)
> -	ret = 1
>    end
>  
>  -- hightlight C code
> @@ -318,7 +367,6 @@
>          writeto (tmp) write (html) writeto ()
>          html = pipe_read ("(code2html -l c "..tmp.." - ) 2>/dev/null")
>          remove (tmp)
> -        ret = 1
>      end
>  
>  -- shell
> @@ -327,15 +375,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),
> @@ -346,33 +391,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.
> @@ -394,7 +432,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
> @@ -407,7 +445,7 @@
>          return gsub(html, "javascript:z%('(.-)'%)", "/cg/amg.dll?p=amg&sql=%1")
>      end
>  
> -    return ret and html
> +    return html
>  end
>  
>  

Could you please add this patch to contrib/lua ?

> Index: src/scripting/lua/core.c
> ===================================================================
> RCS file: /var/cvs/elinks/elinks/src/scripting/lua/core.c,v
> retrieving revision 1.139
> diff -u -r1.139 core.c
> --- src/scripting/lua/core.c    21 Dec 2003 14:13:20 -0000      1.139
> +++ src/scripting/lua/core.c    26 Jun 2004 08:55:13 -0000
> @@ -516,6 +524,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);

Couldn't we have this committed with some surrounding #ifdef ?

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
A, B, C, D, E, F, G... I will eat your family.  -- Dane Cook



More information about the elinks-dev mailing list