|
-- Shebang for NYAOS 3.x |
|
-- |
|
-- Maintainer: DeaR <[email protected]> |
|
-- Last Change: 13-Aug-2013. |
|
-- License: MIT License {{{ |
|
-- Copyright (c) 2013 DeaR <[email protected]> |
|
-- Copyright (c) 2010 wantora |
|
-- |
|
-- Permission is hereby granted, free of charge, to any person obtaining a |
|
-- copy of this software and associated documentation files (the |
|
-- "Software"), to deal in the Software without restriction, including |
|
-- without limitation the rights to use, copy, modify, merge, publish, |
|
-- distribute, sublicense, and/or sell copies of the Software, and to |
|
-- permit persons to whom the Software is furnished to do so, subject to |
|
-- the following conditions: |
|
-- |
|
-- The above copyright notice and this permission notice shall be included |
|
-- in all copies or substantial portions of the Software. |
|
-- |
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
|
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
-- }}} |
|
|
|
local filemap = {} |
|
local dirmap = {} |
|
|
|
function nyaos.command.shebang(src, dest) |
|
if not src then |
|
for k, v in pairs(dirmap) do |
|
print(k .. ' ' .. v) |
|
end |
|
for k, v in pairs(filemap) do |
|
print(k .. ' ' .. v) |
|
end |
|
else |
|
local p = dest:gsub('[/\\]$', '') |
|
local stat = nyaos.stat(p) |
|
if stat and stat.directory then |
|
dirmap[src] = dest |
|
else |
|
filemap[src] = dest |
|
end |
|
end |
|
end |
|
|
|
function nyaos.command.unshebang(src) |
|
if not src then |
|
for k, v in pairs(dirmap) do |
|
print(k .. ' ' .. v) |
|
end |
|
for k, v in pairs(filemap) do |
|
print(k .. ' ' .. v) |
|
end |
|
else |
|
local p = dest:gsub('[/\\]$', '') |
|
local stat = nyaos.stat(p) |
|
if stat and stat.directory then |
|
dirmap[src] = nil |
|
else |
|
filemap[src] = nil |
|
end |
|
end |
|
end |
|
|
|
local cmdutils = {} |
|
function cmdutils.split(cmdline, sep) |
|
local quote = false |
|
local pos = 1 |
|
|
|
while pos <= #cmdline do |
|
local s = cmdline:sub(pos, pos) |
|
|
|
if s == '\034' then |
|
quote = not quote |
|
elseif s:match('^[\129-\159\224-\252]$') then |
|
pos = pos + 1 |
|
elseif (not quote) and s:match(sep) then |
|
break |
|
end |
|
|
|
pos = pos + 1 |
|
end |
|
|
|
return cmdline:sub(1, pos - 1), cmdline:sub(pos + 1), cmdline:sub(pos, pos) |
|
end |
|
|
|
local function append_ext(name) |
|
if nyaos.access(name, 0) then |
|
return name |
|
elseif nyaos.access(name .. '.exe', 0) then |
|
return name .. '.exe' |
|
elseif nyaos.access(name .. '.com', 0) then |
|
return name .. '.com' |
|
end |
|
return name |
|
end |
|
|
|
local function namefilter(name) |
|
for file, path in pairs(filemap) do |
|
if name == file then |
|
return path |
|
end |
|
end |
|
for prefix, path in pairs(dirmap) do |
|
if name:sub(1, #prefix) == prefix then |
|
return path .. name:sub(#prefix + 1) |
|
end |
|
end |
|
return name |
|
end |
|
|
|
local function drop_first_token(args) |
|
local _, param, sep = cmdutils.split(args, '%s') |
|
return sep .. param |
|
end |
|
|
|
function nyaos.filter3.shebang(name, param) |
|
local suffix = string.lower(string.sub(name, #name - 3)) |
|
|
|
if suffix ~= '.exe' and suffix ~= '.com' then |
|
local f = io.open(name, 'r') |
|
if f:read(2) == '#!' then |
|
local shebang = f:read():gsub('^%s*', '') |
|
f:close() |
|
|
|
local new_name = append_ext(namefilter(cmdutils.split(shebang, '%s'))) |
|
local new_param = shebang .. ' \034' .. name .. '\034' .. drop_first_token(param) |
|
return new_name, new_param |
|
else |
|
f:close() |
|
end |
|
end |
|
end |
|
|
|
-- vim: ft=lua |