/Master/texmf-dist/tex/context/base/luat-cod.lua

https://bitbucket.org/preining/tex-live · Lua · 169 lines · 126 code · 31 blank · 12 comment · 36 complexity · 641df5fd60a6d15fb242a174f6535e14 MD5 · raw file

  1. if not modules then modules = { } end modules ['luat-cod'] = {
  2. version = 1.001,
  3. comment = "companion to luat-cod.mkiv",
  4. author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
  5. copyright = "PRAGMA ADE / ConTeXt Development Team",
  6. license = "see context related readme files"
  7. }
  8. local match, gsub, find, format = string.match, string.gsub, string.find, string.format
  9. local texconfig, lua = texconfig, lua
  10. -- some basic housekeeping
  11. texconfig.kpse_init = false
  12. texconfig.shell_escape = 't'
  13. texconfig.max_print_line = 100000
  14. texconfig.max_in_open = 127
  15. -- registering bytecode chunks
  16. lua.bytecode = lua.bytecode or { } -- built in anyway
  17. lua.bytedata = lua.bytedata or { }
  18. lua.bytedone = lua.bytedone or { }
  19. local bytecode, bytedata, bytedone = lua.bytecode, lua.bytedata, lua.bytedone
  20. lua.firstbytecode = 501
  21. lua.lastbytecode = lua.lastbytecode or (lua.firstbytecode - 1) -- as we load ourselves again ... maybe return earlier
  22. function lua.registeredcodes()
  23. return lua.lastbytecode - lua.firstbytecode + 1
  24. end
  25. function lua.registercode(filename,version)
  26. local barename = gsub(filename,"%.[%a%d]+$","")
  27. if barename == filename then filename = filename .. ".lua" end
  28. local basename = match(barename,"^.+[/\\](.-)$") or barename
  29. if not bytedone[barename] then
  30. local code = environment.luafilechunk(filename)
  31. if code then
  32. assert(code)()
  33. bytedone[barename] = true
  34. if environment.initex then
  35. local n = lua.lastbytecode + 1
  36. bytedata[n] = { barename, version }
  37. bytecode[n] = code
  38. lua.lastbytecode = n
  39. end
  40. end
  41. end
  42. end
  43. local finalizers = { }
  44. function lua.registerfinalizer(f,comment)
  45. if type(f) == "function" then
  46. finalizers[#finalizers+1] = { action = f, comment = comment }
  47. else
  48. print(format("fatal error: invalid finalizer, action: %s",finalizer.comment or "unknown"))
  49. os.exit()
  50. end
  51. end
  52. function lua.finalize(logger)
  53. for i=1,#finalizers do
  54. local finalizer = finalizers[i]
  55. finalizer.action()
  56. if logger then
  57. logger("finalizing lua", "action: %s",finalizer.comment)
  58. end
  59. end
  60. end
  61. -- A first start with environments. This will be overloaded later.
  62. environment = environment or { }
  63. local environment = environment
  64. -- no string.unquoted yet
  65. local sourcefile = gsub(arg and arg[1] or "","^\"(.*)\"$","%1")
  66. local sourcepath = find(sourcefile,"/") and gsub(sourcefile,"/[^/]+$","") or ""
  67. local targetpath = "."
  68. -- delayed (via metatable):
  69. --
  70. -- environment.jobname = tex.jobname
  71. -- environment.version = tostring(tex.toks.contextversiontoks)
  72. environment.initex = tex.formatname == ""
  73. if not environment.luafilechunk then
  74. function environment.luafilechunk(filename)
  75. if sourcepath ~= "" then
  76. filename = sourcepath .. "/" .. filename
  77. end
  78. local data = loadfile(filename)
  79. texio.write("<",data and "+ " or "- ",filename,">")
  80. return data
  81. end
  82. end
  83. if not environment.engineflags then
  84. local engineflags = { }
  85. for i=-10,#arg do
  86. local a = arg[i]
  87. if a then
  88. local flag, content = match(a,"^%-%-([^=]+)=?(.-)$")
  89. if flag then
  90. engineflags[flag] = content or ""
  91. end
  92. end
  93. end
  94. environment.engineflags = engineflags
  95. end
  96. -- We need a few premature callbacks in the format generator. We
  97. -- also do this when the format is loaded as otherwise we get
  98. -- a kpse error when disabled. This is an engine issue that will
  99. -- be sorted out in due time.
  100. local isfile = lfs.isfile
  101. local function source_file(name)
  102. local fullname = sourcepath .. "/" .. name
  103. if isfile(fullname) then
  104. return fullname
  105. end
  106. fullname = fullname .. ".tex"
  107. if isfile(fullname) then
  108. return fullname
  109. end
  110. if isfile(name) then
  111. return name
  112. end
  113. name = name .. ".tex"
  114. if isfile(name) then
  115. return name
  116. end
  117. return nil
  118. end
  119. local function target_file(name)
  120. return targetpath .. "/" .. name
  121. end
  122. local function find_read_file (id,name)
  123. return source_file(name)
  124. end
  125. local function find_write_file(id,name)
  126. return target_file(name)
  127. end
  128. local function open_read_file(name)
  129. local f = io.open(name,'rb')
  130. return {
  131. reader = function()
  132. return f:read("*line")
  133. end
  134. }
  135. end
  136. callback.register('find_read_file' , find_read_file )
  137. callback.register('open_read_file' , open_read_file )
  138. callback.register('find_write_file', find_write_file)