summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/tlgs/gswin32/ps2ascii.tlu
blob: d23c0458ea8c0c8485e6c7ee70e77e5c32aa64bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env texlua
--*-Lua-*-
-- $Id$

-- Copyright (C) 2008-2015 Reinhard Kotucha.
-- You may freely use, modify and/or distribute this file.

-- Replacement for ps2ascii.bat.

-- 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
   end
end


local function filename (file)
   -- strip path
   if string.find(file, '[/\\]') then
      return string.match(file, '.*[/\\](.*)$')
   else
      return file
   end
end


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])
      end
   end
end


local function parse_cmdline ()
   local files={}
   local options={}

   local progname
   local basename=filename(arg[0])
   if basename:find('%.') then
      progname=basename:match('(.*)%..*')
   else
      progname=basename
   end

   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


-- 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
   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)