summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu')
-rwxr-xr-xMaster/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu140
1 files changed, 97 insertions, 43 deletions
diff --git a/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu b/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu
index 2bc31ef26d9..d23c0458ea8 100755
--- a/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu
+++ b/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu
@@ -2,26 +2,55 @@
--*-Lua-*-
-- $Id$
--- Copyright (C) 2008 Reinhard Kotucha.
+-- Copyright (C) 2008-2015 Reinhard Kotucha.
-- You may freely use, modify and/or distribute this file.
-- Replacement for ps2ascii.bat.
--- Convert PostScript to ASCII
-
-function fixwin (args_unix)
- if os.type == 'windows' then
- local args_win={} -- new table
- args_win[0]=args_unix[1]
- for i=1, #args_unix do
- args_win[i]='"'..args_unix[i]..'"'
+
+-- Extract ASCII text from a PostScript file. Usage:
+--
+-- ps2ascii [infile.ps [outfile.txt]]
+--
+-- If outfile is omitted, output goes to stdout.
+-- If both infile and outfile are omitted, ps2ascii acts as a filter,
+-- reading from stdin and writing on stdout.
+
+
+-- We have to pass the command as a string to os.execute() because we
+-- need a shell for i/o-redirection. But we create a table first and
+-- convert it to a string, just to make sure we don't miss any spaces.
+
+local function push (t, ...)
+ local args={...}
+ for _,v in ipairs(args) do
+ if type(v) == 'table' then
+ for _,x in ipairs(v) do
+ t[#t+1]=x
+ end
+ else
+ t[#t+1]=v
end
- return args_win
+ end
+end
+
+
+local function filename (file)
+ -- strip path
+ if string.find(file, '[/\\]') then
+ return string.match(file, '.*[/\\](.*)$')
else
- return args_unix
+ return file
end
end
-function remove_tmpfiles (tmpfiles)
+
+local function remove_tmpfiles (tmpfiles)
+ -- The shell script contains
+ --
+ -- trap "rm -f _temp_.err _temp_.out" 0 1 2 15
+ --
+ -- texlua doesn't support signals (yet). So we remove temporary files
+ -- if possible.
for i=1, #tmpfiles do
if lfs.isfile(tmpfiles[i]) then
os.remove(tmpfiles[i])
@@ -29,46 +58,71 @@ function remove_tmpfiles (tmpfiles)
end
end
-if os.type == 'windows' then
- gs='gswin32c'
-else
- gs='gs'
-end
-command={gs, '-q', '-dNODISPLAY', '-dBATCH', '-dSAFER', '-dDELAYBIND',
- '-dWRITESYSTEMDICT', '-dSIMPLE', '-c', 'save',
- '-f', 'ps2ascii.ps'}
+local function parse_cmdline ()
+ local files={}
+ local options={}
-if #arg < 2 then
- if #arg == 0 then
- command[#command+1]='-'
- elseif #arg == 1 then
- command[#command+1]=arg[1]
+ local progname
+ local basename=filename(arg[0])
+ if basename:find('%.') then
+ progname=basename:match('(.*)%..*')
+ else
+ progname=basename
end
- command=fixwin(command)
-elseif #arg == 2 then
- -- We need a shell for I/O redirection.
- command=gs..' -q -dNODISPLAY -dBATCH -dSAFER -dDELAYBIND '..
- '-dWRITESYSTEMDICT -dSIMPLE -c save '..
- '-f ps2ascii.ps "'..arg[1]..'" > "'..arg[2]..'"'
-end
---[[ prepend an additional hyphen to activate this code
-if type(command) == 'string' then
- print(command)
-else
- for i=0, #command do
- print (command[i])
+ for i=1, #arg do
+ if string.find(arg[i], '^%-.+') then
+ push(options, arg[i])
+ else
+ push(files, arg[i])
+ end
end
+ files.input =files[1]
+ files.output=files[2]
+ return progname, options, files
end
-os.exit(ret)
---]]
-if type(command) == 'string' then
- ret=os.execute(command)
+
+-- main --
+
+local progname, options, files=parse_cmdline ()
+
+-- setup command
+
+local command={gs}
+if os.type == 'unix' then command={'gs'} else command={'gswin32c'} end
+
+push(command, {'-q', '-dNODISPLAY', '-P-', '-dSAFER', '-dDELAYBIND',
+ '-dWRITESYSTEMDICT', '-dSIMPLE'})
+
+push(command, '-c', 'save', '-f', 'ps2ascii.ps')
+
+if #files == 0 then
+ push(command, '-')
else
- ret=os.spawn(command)
+ push(command, '"'..files.input..'"')
end
+push(command, '-c', 'quit')
+
+if #files > 1 then
+ push(command, '>', '"'..files.output..'"')
+end
+
+local cmd_string=table.concat(command, ' ')
+
+--[[ prepend an additional hyphen to activate this code
+print(cmd_string)
+os.exit(0)
+--]]
+
+ret=os.execute(cmd_string)
+
+-- The last character of the ASCII file is a form feed (^L).
+-- Uncomment the following line if this confuses your terminal.
+--
+-- if #files < 2 then io.stdout:write('\r') end
+
remove_tmpfiles{'_temp_.err', '_temp_.out'}
os.exit(ret)