summaryrefslogtreecommitdiff
path: root/scripts/context/ruby/base/kpse.rb
blob: ec0b8810ba546f42466285569375a8ba9429a708 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# module    : base/kpse
# copyright : PRAGMA Advanced Document Engineering
# version   : 2002-2005
# author    : Hans Hagen
#
# project   : ConTeXt / eXaMpLe
# concept   : Hans Hagen
# info      : j.hagen@xs4all.nl
# www       : www.pragma-ade.com

# rename this one to environment
#
# todo: web2c vs miktex module and include in kpse

require 'rbconfig'
require 'fileutils'

# beware $engine is lowercase in kpse
#
# miktex has mem|fmt|base paths

class String

    def split_path
        if self =~ /\;/o || self =~ /^[a-z]\:/io then
            self.split(";")
        else
            self.split(":")
        end
    end

    def sane_path
        self.gsub(/\\/,'/')
    end

end

class Array

    def join_path
        self.join(File::PATH_SEPARATOR)
    end

    def non_empty
        self.delete_if do |i|
            (i == nil || i.empty?) rescue false
        end
    end

end

module Kpse

    @@located       = Hash.new
    @@paths         = Hash.new
    @@scripts       = Hash.new
    @@formats       = ['tex','texmfscripts','other text files']
    @@progname      = 'context'
    @@ownpath       = $0.sub(/[\\\/][a-z0-9\-]*?\.rb/i,'')
    @@problems      = false
    @@tracing       = false
    @@distribution  = 'web2c'
    @@crossover     = true
    @@mswindows     = RbConfig::CONFIG['host_os'] =~ /mswin/

    # @@distribution  = 'miktex' if ENV['PATH'] =~ /miktex[\\\/]bin/o

    # if ENV['PATH'] =~ /(.*?)miktex[\\\/]bin/i then
        # @@distribution = 'miktex' unless $1 =~ /(texmf\-mswin[\/\\]bin|bin[\/\\]win32)/i
    # end

    if @@mswindows && (ENV['PATH'] =~ /(.*?)miktex[\\\/]bin/i) then
         @@distribution = 'miktex' unless $1 =~ /(texmf\-mswin[\/\\]bin|bin[\/\\]win32)/i
    end

    @@re_true = /yes|on|true|1/i

    if (ENV['KPSEFAST'] =~ @@re_true) || (ENV['CTXMINIMAL'] =~ @@re_true) then
        @@usekpserunner = true
        require 'base/kpsefast'
        require 'base/kpserunner'
    else
        @@usekpserunner = false
    end

    if @@crossover then
        ENV.keys.each do |k|
            case k
                when /\_CTX\_KPSE\_V\_(.*?)\_/io then @@located[$1] = ENV[k].dup
                when /\_CTX\_KPSE\_P\_(.*?)\_/io then @@paths  [$1] = ENV[k].dup.split(';')
                when /\_CTX\_KPSE\_S\_(.*?)\_/io then @@scripts[$1] = ENV[k].dup
            end
        end
    end

    def Kpse.distribution
        @@distribution
    end

    def Kpse.miktex?
        @@distribution == 'miktex'
    end

    def Kpse.web2c?
        @@distribution == 'web2c'
    end

    def Kpse.inspect
        @@located.keys.sort.each do |k| puts("located : #{k} -> #{@@located[k]}\n") end
        @@paths  .keys.sort.each do |k| puts("paths   : #{k} -> #{@@paths  [k]}\n") end
        @@scripts.keys.sort.each do |k| puts("scripts : #{k} -> #{@@scripts[k]}\n") end
    end

    def Kpse.used_path(varname)
        begin
            if @@mswindows then
                path = run("--expand-path=\$#{varname}") rescue ''
            else
                path = run("--expand-path='$#{varname}'") rescue ''
            end
        rescue
            path = ''
        end
        return path.sane_path
    end

    def Kpse.found(filename, progname=nil, format=nil)
        begin
            tag = Kpse.key(filename) # all
            if @@located.key?(tag) then
                return @@located[tag].sane_path
            elsif FileTest.file?(filename) then
                setvariable(tag,filename)
                return filename
            elsif FileTest.file?(File.join(@@ownpath,filename)) then
                setvariable(tag,File.join(@@ownpath,filename))
                return @@located[tag].sane_path
            else
                [progname,@@progname].flatten.compact.uniq.each do |prg|
                    [format,@@formats].flatten.compact.uniq.each do |fmt|
                        begin
                            tag = Kpse.key(filename,prg,fmt)
                            if @@located.key?(tag) then
                                return @@located[tag].sane_path
                            elsif p = Kpse.kpsewhich(filename,prg,fmt) then
                                setvariable(tag,p.chomp)
                                return @@located[tag].sane_path
                            end
                        rescue
                        end
                    end
                end
                setvariable(tag,filename)
                return filename.sane_path
            end
        rescue
            filename.sane_path
        end
    end

    def Kpse.kpsewhich(filename,progname,format)
        p = if progname && ! progname.empty? then "-progname=#{progname}" else '' end
        f = if format   && ! format.empty?   then "-format=\"#{format}\"" else '' end
        Kpse.run("#{p} #{f} #{filename}")
    end

    def Kpse.which
        Kpse.kpsewhich
    end

    def Kpse.run(arguments)
        puts arguments if @@tracing
        begin
            if @@problems then
                results = ''
            elsif @@usekpserunner then
                results = KpseRunner.kpsewhich(arguments).chomp
            else
                results = `kpsewhich #{arguments}`.chomp
            end
        rescue
            puts "unable to run kpsewhich" if @@tracing
            @@problems, results = true, ''
        end
        puts results if @@tracing
        return results
    end


    def Kpse.formatpaths
        # maybe we should check for writeability
        unless @@paths.key?('formatpaths') then
            begin
                setpath('formatpaths',run("--show-path=fmt").sane_path.split_path)
            rescue
                setpath('formatpaths',[])
            end
        end
        return @@paths['formatpaths']
    end

    def Kpse.key(filename='',progname='all',format='all')
        [progname,format,filename].join('-')
    end

    def Kpse.formatpath(engine='pdftex',enginepath=true)

        # because engine support in distributions is not always
        # as we expect, we need to check for it;

        # todo: miktex

        if miktex? then
            return '.'
        else
            unless @@paths.key?(engine) then
                # savedengine = ENV['engine']
                if ENV['TEXFORMATS'] && ! ENV['TEXFORMATS'].empty? then
                    # make sure that we have a lowercase entry
                    ENV['TEXFORMATS'] = ENV['TEXFORMATS'].sub(/\$engine/io,"\$engine")
                    # well, we will append anyway, so we could also strip it
                    # ENV['TEXFORMATS'] = ENV['TEXFORMATS'].sub(/\$engine/io,"")
                end
                # use modern method
                if enginepath then
                    formatpath = run("--engine=#{engine} --show-path=fmt")
                else
                    # ENV['engine'] = engine if engine
                    formatpath = run("--show-path=fmt")
                end
                # use ancient method
                if formatpath.empty? then
                    if enginepath then
                        if @@mswindows then
                            formatpath = run("--engine=#{engine} --expand-path=\$TEXFORMATS")
                        else
                            formatpath = run("--engine=#{engine} --expand-path=\\\$TEXFORMATS")
                        end
                    end
                    # either no enginepath or failed run
                    if formatpath.empty? then
                        if @@mswindows then
                            formatpath = run("--expand-path=\$TEXFORMATS")
                        else
                            formatpath = run("--expand-path=\\\$TEXFORMATS")
                        end
                    end
                end
                # locate writable path
                if ! formatpath.empty? then
                    formatpaths, done = formatpath.split_path, false
                    formatpaths.collect! do |fp|
                        fp.gsub!(/\\/o,'/')
                        fp.gsub!(/\/\/$/o,'/')
                        # remove funny patterns
                        fp.sub!(/^!!/o,'')
                        fp.sub!(/\/+$/o,'')
                        fp.sub!(/(unsetengine|unset)/o,if enginepath then engine else '' end)
                        fp
                    end
                    formatpaths.delete_if do |fp|
                        fp.empty? || fp == '.'
                    end
                    # the engine path may not yet be present, find first writable
                    formatpaths.each do |fp|
                        # strip (possible engine) and test for writeability
                        fpp = fp.sub(/#{engine}\/*$/o,'')
                        if FileTest.directory?(fpp) && FileTest.writable?(fpp) then
                            # use this path
                            formatpath, done = fp.dup, true
                            break
                        end
                    end
                    unless done then
                        formatpaths.each do |fp|
                            fpp = fp.sub(/#{engine}\/*$/o,'')
                            FileUtils.makedirs(fpp) rescue false # maybe we don't have an path yet
                            if FileTest.directory?(fpp) && FileTest.writable?(fpp) then
                                # use this path
                                formatpath, done = fp.dup, true
                                break
                            end
                        end
                    end
                    unless done then
                        formatpath = '.'
                    end
                end
                # needed !
                FileUtils.makedirs(formatpath) rescue false
                # fall back to current path
                formatpath = '.' if formatpath.empty? || ! FileTest.writable?(formatpath)
                # append engine but prevent duplicates
                formatpath = File.join(formatpath.sub(/\/*#{engine}\/*$/,''), engine) if enginepath
                FileUtils.makedirs(formatpath) rescue false
                setpath(engine,formatpath)
                # ENV['engine'] = savedengine
            end
            return @@paths[engine].first
        end
    end

    def Kpse.update
        system('initexmf -u') if Kpse.miktex?
        system('mktexlsr')
    end

    # engine support is either broken of not implemented in some
    # distributions, so we need to take care of it ourselves (without
    # delays due to kpse calls); there can be many paths in the string
    #
    # in a year or so, i will drop this check

    def Kpse.fixtexmfvars(engine=nil)
        ENV['ENGINE'] = engine if engine
        texformats = if ENV['TEXFORMATS'] then ENV['TEXFORMATS'].dup else '' end
        if texformats.empty? then
            if engine then
                if @@mswindows then
                    texformats = `kpsewhich --engine=#{engine} --expand-var=\$TEXFORMATS`.chomp
                else
                    texformats = `kpsewhich --engine=#{engine} --expand-var=\\\$TEXFORMATS`.chomp
                end
            else
                if @@mswindows then
                    texformats = `kpsewhich --expand-var=\$TEXFORMATS`.chomp
                else
                    texformats = `kpsewhich --expand-var=\\\$TEXFORMATS`.chomp
                end
            end
        end
        if engine then
            texformats.sub!(/unsetengine/,engine)
        else
            texformats.sub!(/unsetengine/,"\$engine")
        end
        if engine && (texformats =~ /web2c[\/\\].*#{engine}/o) then
            # ok, engine is seen
            return false
        elsif texformats =~ /web2c[\/\\].*\$engine/io then
            # shouldn't happen
            return false
        else
            ENV['TEXFORMATS'] = texformats.gsub(/(web2c\/\{)(,\})/o) do
                "#{$1}\$engine#{$2}"
            end
            if texformats !~ /web2c[\/\\].*\$engine/io then
                ENV['TEXFORMATS'] = texformats.gsub(/web2c\/*/, "web2c/{\$engine,}")
            end
            return true
        end
    end

    # def Kpse.runscript(name,filename=[],options=[])
        # setscript(name,`texmfstart --locate #{name}`) unless @@scripts.key?(name)
        # cmd = "#{@@scripts[name]} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}"
        # system(cmd)
    # end

    # def Kpse.pipescript(name,filename=[],options=[])
        # setscript(name,`texmfstart --locate #{name}`) unless @@scripts.key?(name)
        # cmd = "#{@@scripts[name]} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}"
        # `#{cmd}`
    # end

    def Kpse.runscript(name,filename=[],options=[])
        cmd = "mtxrun --script #{name} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}"
        system(cmd)
    end

    def Kpse.pipescript(name,filename=[],options=[])
        cmd = "mtxrun --script #{name} #{[options].flatten.join(' ')} #{[filename].flatten.join(' ')}"
        `#{cmd}`
    end

    def Kpse.searchmethod
        if @@usekpserunner then 'kpsefast' else 'kpsewhich' end
    end

    private

    def Kpse.setvariable(key,value)
        @@located[key] = value
        ENV["_CTX_K_V_#{key}_"] = @@located[key] if @@crossover
    end

    def Kpse.setscript(key,value)
        @@scripts[key] = value
        ENV["_CTX_K_S_#{key}_"] = @@scripts[key] if @@crossover
    end

    def Kpse.setpath(key,value)
        @@paths[key] = [value].flatten.uniq.collect do |p|
            p.sub(/^!!/,'').sub(/\/*$/,'')
        end
        ENV["_CTX_K_P_#{key}_"] = @@paths[key].join(';') if @@crossover
    end

end