Debian Codegraph @

texmf-dist/tex/luatex/luaotfload/luaotfload-configuration.lua

raw
1 -------------------------------------------------------------------------------
2 -- FILE: luaotfload-configuration.lua
3 -- DESCRIPTION: part of luaotfload / luaotfload-tool / config file reader
4 -- AUTHOR: Philipp Gesang, <[email protected]>
5 -- AUTHOR: Dohyun Kim <[email protected]>
6 -------------------------------------------------------------------------------
7
8 assert(luaotfload_module, "This is a part of luaotfload and should not be loaded independently") {
9 name = "luaotfload-configuration",
10 version = "3.29", --TAGVERSION
11 date = "2024-12-03", --TAGDATE
12 description = "luaotfload submodule / config file reader",
13 license = "GPL v2.0"
14 }
15
16 ------------------------------
17
18 local status_file = "luaotfload-status"
19 local luaotfloadstatus = require (status_file)
20
21 local string = string
22 local stringfind = string.find
23 local stringformat = string.format
24 local stringstrip = string.strip
25 local stringsub = string.sub
26
27 local tableappend = table.append
28 local tableconcat = table.concat
29 local tablecopy = table.copy
30 local table = table
31 local tabletohash = table.tohash
32
33 local math = math
34 local mathfloor = math.floor
35
36 local io = io
37 local ioloaddata = io.loaddata
38 local iopopen = io.popen
39 local iowrite = io.write
40
41 local os = os
42 local osdate = os.date
43 local osgetenv = os.getenv
44
45 local lpeg = require "lpeg"
46 local lpegmatch = lpeg.match
47 local commasplitter = lpeg.splitat ","
48 local equalssplitter = lpeg.splitat "="
49
50 local kpseexpand_path = kpse.expand_path
51
52 local lfs = lfs
53 local lfsisfile = lfs.isfile
54 local lfsisdir = lfs.isdir
55
56 local file = file
57 local filejoin = file.join
58 local filereplacesuffix = file.replacesuffix
59
60 local logreport = print -- overloaded later
61 local getwritablepath = luaotfload.fontloader.caches.getwritablepath
62
63
64 local config_parser -- set later during init
65 local stripslashes -- set later during init
66
67 -------------------------------------------------------------------------------
68 --- SETTINGS
69 -------------------------------------------------------------------------------
70
71 local path_t = 0
72 local kpse_t = 1
73
74 local val_home = kpseexpand_path "~"
75 local val_xdg_config_home = kpseexpand_path "$XDG_CONFIG_HOME"
76
77 if val_xdg_config_home == "" then val_xdg_config_home = "~/.config" end
78
79 local config_paths = {
80 --- needs adapting for those other OS
81 { path_t, "./luaotfload.conf" },
82 { path_t, "./luaotfloadrc" },
83 { path_t, filejoin (val_xdg_config_home, "luaotfload/luaotfload.conf") },
84 { path_t, filejoin (val_xdg_config_home, "luaotfload/luaotfloadrc") },
85 { path_t, filejoin (val_home, ".luaotfloadrc") },
86 { kpse_t, "luaotfloadrc" },
87 { kpse_t, "luaotfload.conf" },
88 }
89
90 local valid_formats = tabletohash {
91 "otf", "ttc", "ttf", "afm", "pfb"
92 }
93
94 local default_location_precedence = {
95 "system", "texmf", "local"
96 }
97 local valid_locations = tabletohash {
98 "system", "texmf", "local"
99 }
100
101 local default_anon_sequence = {
102 "tex", "path", "name"
103 }
104
105 local valid_resolvers = tabletohash {
106 "tex", "path", "name", "file", "my"
107 }
108
109 local base_features = tabletohash {
110 -- Adopt the generic default from HarfBuzz.
111 -- The first line is generally enabled there,
112 -- the second only for horizontal text.
113 -- The third line is luaotfload specific
114 "abvm", "blwm", "ccmp", "locl", "mark", "mkmk", "rlig",
115 "calt", "clig", "curs", "dist", "kern", "liga", "rclt",
116 "itlc",
117 }
118 local feature_presets = {
119 arab = table.merged( tabletohash {
120 "isol", "fina", "fin2", "fin3", "medi", "med2", "init",
121 "cswh", "mset",
122 }, base_features),
123 deva = table.merged( tabletohash {
124 "init", "nukt", "akhn", "rphf", "blwf", "half", "pstf",
125 "vatu", "pres", "blws", "abvs", "psts", "haln",
126 }, base_features),
127 khmr = table.merged( tabletohash {
128 "pref", "blwf", "abvf", "pstf", "pres", "blws", "abvs",
129 "psts",
130 }, base_features),
131 hang = table.merged( tabletohash {
132 "ccmp", "ljmo", "vjmo", "tjmo",
133 }, base_features),
134 }
135
136 --[[doc--
137
138 We allow loading of arbitrary fontloaders. Nevertheless we maintain a
139 list of the “official” ones shipped with Luaotfload so we can emit a
140 different log message.
141
142 --doc]]--
143
144 local function default_fontloader ()
145 return luaotfloadstatus and luaotfloadstatus.notes.loader or "reference"
146 end
147
148 local registered_loaders = {
149 default = default_fontloader (),
150 reference = "reference",
151 unpackaged = "unpackaged",
152 context = "context",
153 }
154
155 local function pick_fontloader (s)
156 local ldr = registered_loaders[s]
157 if ldr ~= nil and type (ldr) == "string" then
158 logreport ("log", 2, "conf", "Using predefined fontloader %q.", ldr)
159 return ldr
160 end
161 local idx = stringfind (s, ":")
162 if idx and idx > 2 then
163 if stringsub (s, 1, idx - 1) == "context" then
164 local pth = stringsub (s, idx + 1)
165 pth = stringstrip (pth)
166 logreport ("log", 2, "conf", "Context base path specified at %q.", pth)
167 if lfsisdir (pth) then
168 logreport ("log", 5, "conf", "Context base path exists at %q.", pth)
169 return pth
170 end
171 pth = kpseexpand_path (pth)
172 if lfsisdir (pth) then
173 logreport ("log", 5, "conf", "Context base path exists at %q.", pth)
174 return pth
175 end
176 logreport ("both", 0, "conf", "Context base path not found at %q.", pth)
177 end
178 end
179 return nil
180 end
181
182 --[[doc--
183
184 The ``post_linebreak_filter`` has been made the default callback for
185 hooking the colorizer into. This helps with the linebreaking whose
186 inserted hyphens would remain unaffected by the coloring otherwise.
187
188 http://tex.stackexchange.com/q/238539/14066
189
190 --doc]]--
191
192 local permissible_color_callbacks = {
193 default = "post_linebreak_filter",
194 pre_linebreak_filter = "pre_linebreak_filter",
195 post_linebreak_filter = "post_linebreak_filter",
196 pre_output_filter = "pre_output_filter",
197 }
198
199 local known_dvi_drivers = {
200 xdvipsk = 'xdvipsk',
201 dvisvgm = 'dvisvgm',
202 }
203
204 -------------------------------------------------------------------------------
205 --- DEFAULTS
206 -------------------------------------------------------------------------------
207
208 local default_config = {
209 db = {
210 location_precedence = default_location_precedence,
211 formats = "otf,ttf,ttc",
212 scan_local = false,
213 skip_read = false,
214 strip = true,
215 update_live = true,
216 compress = true,
217 max_fonts = 2^51,
218 designsize_dimen = "bp",
219 },
220 run = {
221 anon_sequence = default_anon_sequence,
222 resolver = "cached",
223 definer = "patch",
224 log_level = 0,
225 color_callback = "post_linebreak_filter",
226 fontloader = default_fontloader (),
227 default_dvi_driver = "dvisvgm",
228 live = true,
229 },
230 misc = {
231 bisect = false,
232 version = luaotfload.version,
233 statistics = false,
234 termwidth = nil,
235 keepnames = true,
236 },
237 paths = {
238 names_dir = "names",
239 cache_dir = "fonts",
240 index_file = "luaotfload-names.lua",
241 lookups_file = "luaotfload-lookup-cache.lua",
242 lookup_path_lua = nil,
243 lookup_path_luc = nil,
244 index_path_lua = nil,
245 index_path_luc = nil,
246 },
247 default_features = {
248 global = { mode = "node" },
249 dflt = base_features,
250
251 arab = feature_presets.arab,
252 syrc = feature_presets.arab,
253 mong = feature_presets.arab,
254 nko = feature_presets.arab,
255
256 deva = feature_presets.deva,
257 beng = feature_presets.deva,
258 guru = feature_presets.deva,
259 gujr = feature_presets.deva,
260 orya = feature_presets.deva,
261 taml = feature_presets.deva,
262 telu = feature_presets.deva,
263 knda = feature_presets.deva,
264 mlym = feature_presets.deva,
265 sinh = feature_presets.deva,
266
267 khmr = feature_presets.khmr,
268 tibt = feature_presets.khmr,
269 thai = feature_presets.thai,
270 lao = feature_presets.thai,
271
272 hang = feature_presets.hang,
273 },
274 status = luaotfloadstatus,
275 }
276
277 -------------------------------------------------------------------------------
278 --- RECONFIGURATION TASKS
279 -------------------------------------------------------------------------------
280
281 --[[doc--
282
283 Procedures to be executed in order to put the new configuration into effect.
284
285 --doc]]--
286
287 local reconf_tasks = nil
288
289 local min_terminal_width = 40
290
291 --- The “termwidth” value is only considered when printing
292 --- short status messages, e.g. when building the database
293 --- online.
294 local function check_termwidth ()
295 if config.luaotfload.misc.termwidth == nil then
296 local tw = 79
297 if not ( os.type == "windows" --- Assume broken terminal.
298 or osgetenv "TERM" == "dumb")
299 then
300 local p = iopopen "tput cols"
301 if p then
302 result = tonumber (p:read "*all")
303 p:close ()
304 if result then
305 tw = result
306 else
307 logreport ("log", 2, "db", "tput returned non-number.")
308 end
309 else
310 logreport ("log", 2, "db", "Shell escape disabled or tput executable missing.")
311 logreport ("log", 2, "db", "Assuming 79 cols terminal width.")
312 end
313 end
314 config.luaotfload.misc.termwidth = tw
315 end
316 return true
317 end
318
319 local function set_font_filter ()
320 local names = fonts.names
321 if names and names.set_font_filter then
322 local formats = config.luaotfload.db.formats
323 if not formats or formats == "" then
324 formats = default_config.db.formats
325 end
326 names.set_font_filter (formats)
327 end
328 return true
329 end
330
331 local function set_location_precedence_list ()
332 local names = fonts.names
333 if names and names.set_location_precedence then
334 local locations = config.luaotfload.db.location_precedence
335 if not locations or locations == "" then
336 locations = default_config.db.location_precedence
337 end
338 fonts.names.set_location_precedence (locations)
339 end
340 return true
341 end
342
343 local function set_size_dimension ()
344 local names = fonts.names
345 if names and names.set_size_dimension then
346 local dim = config.luaotfload.db.designsize_dimen
347 if not dim or dim == "" then
348 dim = default_config.db.designsize_dimen
349 end
350 names.set_size_dimension (dim)
351 end
352 return true
353 end
354
355 local function set_name_resolver ()
356 local names = fonts.names
357 if names and names.resolve_cached then
358 --- replace the resolver from luatex-fonts
359 if config.luaotfload.db.resolver == "cached" then
360 logreport ("both", 2, "cache", "Caching of name: lookups active.")
361 names.resolvespec = fonts.names.lookup_font_name_cached
362 else
363 names.resolvespec = fonts.names.lookup_font_name
364 end
365 end
366 return true
367 end
368
369 local function set_loglevel ()
370 if luaotfload then
371 luaotfload.log.set_loglevel (config.luaotfload.run.log_level)
372 return true
373 end
374 return false
375 end
376
377 local function build_cache_paths ()
378 local paths = config.luaotfload.paths
379 local prefix = getwritablepath (paths.names_dir, "")
380
381 if not prefix then
382 luaotfload.error ("Impossible to find a suitable writeable cache...")
383 return false
384 end
385
386 prefix = lpegmatch (stripslashes, prefix)
387 logreport ("log", 0, "conf", "Root cache directory is %q.", prefix)
388
389 local index_file = filejoin (prefix, paths.index_file)
390 local lookups_file = filejoin (prefix, paths.lookups_file)
391
392 paths.prefix = prefix
393 paths.index_path_lua = filereplacesuffix (index_file, "lua")
394 paths.index_path_luc = filereplacesuffix (index_file, "luc")
395 paths.lookup_path_lua = filereplacesuffix (lookups_file, "lua")
396 paths.lookup_path_luc = filereplacesuffix (lookups_file, "luc")
397 return true
398 end
399
400
401 local function set_default_features ()
402 local default_features = config.luaotfload.default_features
403 luaotfload.features = luaotfload.features or {
404 global = { },
405 defaults = { },
406 }
407 local current_features = luaotfload.features
408 for var, val in next, default_features do
409 if var == "global" then
410 current_features.global = val
411 else
412 current_features.defaults[var] = val
413 end
414 end
415 return true
416 end
417
418 reconf_tasks = {
419 { "Set the log level" , set_loglevel },
420 { "Build cache paths" , build_cache_paths },
421 { "Check terminal dimensions" , check_termwidth },
422 { "Set the font filter" , set_font_filter },
423 { "Set design size dimension" , set_size_dimension },
424 { "Install font name resolver", set_name_resolver },
425 { "Set default features" , set_default_features },
426 { "Set location precedence" , set_location_precedence_list },
427 }
428
429 -------------------------------------------------------------------------------
430 --- OPTION SPECIFICATION
431 -------------------------------------------------------------------------------
432
433 local string_t = "string"
434 local table_t = "table"
435 local number_t = "number"
436 local boolean_t = "boolean"
437 local function_t = "function"
438
439 local function tointeger (n)
440 n = tonumber (n)
441 if n then
442 return mathfloor (n + 0.5)
443 end
444 end
445
446 local function toarray (s)
447 local fields = { lpegmatch (commasplitter, s) }
448 local ret = { }
449 for i = 1, #fields do
450 local field = stringstrip (fields[i])
451 if field and field ~= "" then
452 ret[#ret + 1] = field
453 end
454 end
455 return ret
456 end
457
458 local function tohash (s)
459 local result = { }
460 local fields = toarray (s)
461 for _, field in next, fields do
462 local var, val
463 if stringfind (field, "=") then
464 local tmp
465 var, tmp = lpegmatch (equalssplitter, field)
466 if tmp == "true" or tmp == "yes" then val = true else val = tmp end
467 else
468 var, val = field, true
469 end
470 result[var] = val
471 end
472 return result
473 end
474
475 local option_spec = {
476 db = {
477 formats = {
478 in_t = string_t,
479 out_t = string_t,
480 transform = function (f)
481 local fields = toarray (f)
482
483 --- check validity
484 if not fields then
485 logreport ("both", 0, "conf",
486 "Expected list of identifiers, got %q.", f)
487 return nil
488 end
489
490 --- strip dupes
491 local known = { }
492 local result = { }
493 for i = 1, #fields do
494 local field = fields[i]
495 if known[field] ~= true then
496 --- yet unknown, tag as seen
497 known[field] = true
498 --- include in output if valid
499 if valid_formats[field] == true then
500 result[#result + 1] = field
501 else
502 logreport ("both", 4, "conf",
503 "Invalid font format identifier %q, ignoring.",
504 field)
505 end
506 end
507 end
508 if #result == 0 then
509 --- force defaults
510 return nil
511 end
512 return tableconcat (result, ",")
513 end
514 },
515 location_precedence = {
516 in_t = string_t,
517 out_t = table_t,
518 transform = function (s)
519 local bits = { lpegmatch (commasplitter, s) }
520 if next (bits) then
521 local seq = { }
522 local done = { }
523 for i = 1, #bits do
524 local bit = bits [i]
525 if valid_locations [bit] then
526 if not done [bit] then
527 done [bit] = true
528 seq [#seq + 1] = bit
529 else
530 logreport ("both", 0, "conf",
531 "ignoring duplicate location %s at position %d \z
532 in precedence list",
533 bit, i)
534 end
535 else
536 logreport ("both", 0, "conf",
537 "location precedence list contains invalid item %s \z
538 at position %d.",
539 bit, i)
540 end
541 end
542 if next (seq) then
543 logreport ("both", 2, "conf",
544 "overriding anon lookup sequence %s.",
545 tableconcat (seq, ","))
546 return seq
547 end
548 end
549 logreport ("both", 0, "conf", "no lookup locations enabled, falling back to default precedence list")
550 return default_location_precedence
551 end
552 },
553 scan_local = { in_t = boolean_t, },
554 skip_read = { in_t = boolean_t, },
555 strip = { in_t = boolean_t, },
556 update_live = { in_t = boolean_t, },
557 compress = { in_t = boolean_t, },
558 max_fonts = {
559 in_t = number_t,
560 out_t = number_t, --- TODO int_t from 5.3.x on
561 transform = tointeger,
562 },
563 designsize_dimen = {
564 in_t = string_t,
565 out_t = string_t,
566 },
567 },
568 run = {
569 anon_sequence = {
570 in_t = string_t,
571 out_t = table_t,
572 transform = function (s)
573 if s ~= "default" then
574 local bits = { lpegmatch (commasplitter, s) }
575 if next (bits) then
576 local seq = { }
577 local done = { }
578 for i = 1, #bits do
579 local bit = bits [i]
580 if valid_resolvers [bit] then
581 if not done [bit] then
582 done [bit] = true
583 seq [#seq + 1] = bit
584 else
585 logreport ("both", 0, "conf",
586 "ignoring duplicate resolver %s at position %d \z
587 in lookup sequence",
588 bit, i)
589 end
590 else
591 logreport ("both", 0, "conf",
592 "lookup sequence contains invalid item %s \z
593 at position %d.",
594 bit, i)
595 end
596 end
597 if next (seq) then
598 logreport ("both", 2, "conf",
599 "overriding anon lookup sequence %s.",
600 tableconcat (seq, ","))
601 return seq
602 end
603 end
604 end
605 return default_anon_sequence
606 end
607 },
608 live = { in_t = boolean_t, }, --- false for the tool, true for TeX run
609 resolver = {
610 in_t = string_t,
611 out_t = string_t,
612 transform = function (r) return r == "normal" and r or "cached" end,
613 },
614 definer = {
615 in_t = string_t,
616 out_t = string_t,
617 transform = function (d)
618 if d == "generic" or d == "patch"
619 or d == "info_generic" or d == "info_patch"
620 then
621 return d
622 end
623 return "patch"
624 end,
625 },
626 fontloader = {
627 in_t = string_t,
628 out_t = string_t,
629 transform = function (id)
630 local ldr = pick_fontloader (id)
631 if ldr ~= nil then
632 return ldr
633 end
634 logreport ("log", 0, "conf",
635 "Requested fontloader %q not defined, "
636 .. "use at your own risk.",
637 id)
638 return id
639 end,
640 },
641 log_level = {
642 in_t = number_t,
643 out_t = number_t, --- TODO int_t from 5.3.x on
644 transform = tointeger,
645 },
646 color_callback = {
647 in_t = string_t,
648 out_t = string_t,
649 transform = function (cb_spec)
650 --- These are the two that make sense.
651 local cb = permissible_color_callbacks[cb_spec]
652 if cb then
653 logreport ("log", 3, "conf",
654 "Using callback %q for font colorization.",
655 cb)
656 return cb
657 end
658 logreport ("log", 0, "conf",
659 "Requested callback identifier %q invalid, "
660 .. "falling back to default.",
661 cb_spec)
662 return permissible_color_callbacks.default
663 end,
664 },
665 default_dvi_driver = {
666 in_t = string_t,
667 out_t = string_t,
668 transform = function (driver)
669 local mapped = known_dvi_drivers[driver]
670 if mapped then
671 logreport ("log", 5, "conf",
672 "Using default DVI driver %q if used in DVI mode.",
673 mapped)
674 return mapped
675 end
676 logreport ("log", 0, "conf",
677 "Requested default DVI driver %q invalid, "
678 .. "falling back to dvisvgm.",
679 driver)
680 return known_dvi_drivers.dvisvgm
681 end,
682 },
683 },
684 misc = {
685 bisect = { in_t = boolean_t, }, --- doesn’t make sense in a config file
686 version = { in_t = string_t, },
687 statistics = { in_t = boolean_t, },
688 termwidth = {
689 in_t = number_t,
690 out_t = number_t,
691 transform = function (w)
692 w = tointeger (w)
693 if w < min_terminal_width then
694 return min_terminal_width
695 end
696 return w
697 end,
698 },
699 keepnames = { in_t = boolean_t, },
700 },
701 paths = {
702 names_dir = { in_t = string_t, },
703 cache_dir = { in_t = string_t, },
704 index_file = { in_t = string_t, },
705 lookups_file = { in_t = string_t, },
706 lookup_path_lua = { in_t = string_t, },
707 lookup_path_luc = { in_t = string_t, },
708 index_path_lua = { in_t = string_t, },
709 index_path_luc = { in_t = string_t, },
710 },
711 default_features = {
712 __default = { in_t = string_t, out_t = table_t, transform = tohash, },
713 },
714 }
715
716 -------------------------------------------------------------------------------
717 --- FORMATTERS
718 -------------------------------------------------------------------------------
719
720 local function commented (str)
721 return ";" .. str
722 end
723
724 local underscore_replacer = lpeg.replacer ("_", "-", true)
725
726 local function dashed (var)
727 --- INI spec dictates that dashes are valid in variable names, not
728 --- underscores.
729 return underscore_replacer (var) or var
730 end
731
732 local indent = " "
733 local function format_string (var, val)
734 return stringformat (indent .. "%s = %s", var, val)
735 end
736
737 local function format_integer (var, val)
738 return stringformat (indent .. "%s = %d", var, val)
739 end
740
741 local function format_boolean (var, val)
742 return stringformat (indent .. "%s = %s", var, val == true and "true" or "false")
743 end
744
745 local function format_keyval (var, val)
746 local list = { }
747 local keys = table.sortedkeys (val)
748 for i = 1, #keys do
749 local key = keys[i]
750 local subval = val[key]
751 if subval == true then
752 list[#list + 1] = stringformat ("%s", key)
753 else
754 list[#list + 1] = stringformat ("%s=%s", key, val[key])
755 end
756 end
757 if next (list) then
758 return stringformat (indent .. "%s = %s",
759 var,
760 tableconcat (list, ","))
761 end
762 end
763
764 local function format_list (var, val)
765 local elts = { }
766 for i = 1, #val do elts [i] = val [i] end
767 if next (elts) then
768 return stringformat (indent .. "%s = %s",
769 var, tableconcat (elts, ","))
770 end
771 end
772
773 local function format_section (title)
774 return stringformat ("[%s]", dashed (title))
775 end
776
777 local conf_header = [==[
778 ;;-----------------------------------------------------------------------------
779 ;; Luaotfload Configuration
780 ;;-----------------------------------------------------------------------------
781 ;;
782 ;; This file was generated by luaotfload-tool
783 ;; on %s. Configuration variables
784 ;; are documented in the manual to luaotfload.conf(5).
785 ;;
786 ;;-----------------------------------------------------------------------------
787
788 ]==]
789
790 local conf_footer = [==[
791
792 ;; vim:filetype=dosini:expandtab:shiftwidth=2
793 ]==]
794
795 --- Each dumpable variable (the ones mentioned in the man page) receives a
796 --- formatter that will be used in dumping the variable. Each value receives a
797 --- “commented” flag that indicates whether or not the line should be printed
798 --- as a comment.
799
800 local formatters = {
801 db = {
802 compress = { false, format_boolean },
803 designsize_dimen = { false, format_string },
804 formats = { false, format_string },
805 max_fonts = { false, format_integer },
806 scan_local = { false, format_boolean },
807 skip_read = { false, format_boolean },
808 strip = { false, format_boolean },
809 update_live = { false, format_boolean },
810 },
811 default_features = {
812 __default = { true, format_keyval },
813 },
814 misc = {
815 bisect = { false, format_boolean },
816 statistics = { false, format_boolean },
817 termwidth = { true , format_integer },
818 version = { true , format_string },
819 },
820 paths = {
821 cache_dir = { false, format_string },
822 names_dir = { false, format_string },
823 index_file = { false, format_string },
824 lookups_file = { false, format_string },
825 },
826 run = {
827 anon_sequence = { false, format_list },
828 color_callback = { false, format_string },
829 definer = { false, format_string },
830 fontloader = { true, format_string },
831 log_level = { false, format_integer },
832 resolver = { false, format_string },
833 default_dvi_driver = { false, format_string },
834 },
835 }
836
837 -------------------------------------------------------------------------------
838 --- MAIN FUNCTIONALITY
839 -------------------------------------------------------------------------------
840
841 --[[doc--
842
843 tilde_expand -- Rudimentary tilde expansion; covers just the “substitute ‘~’
844 by the current users’s $HOME” part.
845
846 --doc]]--
847
848 local function tilde_expand (p)
849 if #p > 2 then
850 if stringsub (p, 1, 2) == "~/" then
851 local homedir = osgetenv "HOME"
852 if homedir and lfsisdir (homedir) then
853 p = filejoin (homedir, stringsub (p, 3))
854 end
855 end
856 end
857 return p
858 end
859
860 local function resolve_config_path ()
861 for i = 1, #config_paths do
862 local t, p = unpack (config_paths[i])
863 local fullname
864 if t == kpse_t then
865 fullname = kpse.find_file (p)
866 logreport ("both", 6, "conf", "kpse lookup: %s -> %s.", p, fullname)
867 elseif t == path_t then
868 local expanded = tilde_expand (p)
869 if lfsisfile (expanded) then
870 fullname = expanded
871 end
872 logreport ("both", 6, "conf", "path lookup: %s -> %s.", p, fullname)
873 end
874 if fullname then
875 logreport ("both", 3, "conf", "Reading configuration file at %q.", fullname)
876 return fullname
877 end
878 end
879 logreport ("both", 2, "conf", "No configuration file found.")
880 return false
881 end
882
883 local function add_config_paths (t)
884 if not next (t) then
885 return
886 end
887 local result = { }
888 for i = 1, #t do
889 local path = t[i]
890 result[#result + 1] = { path_t, path }
891 end
892 config_paths = tableappend (result, config_paths)
893 end
894
895 local process_options = function (opts)
896 local new = { }
897 for i = 1, #opts do
898 local section = opts[i]
899 local title = section.section.title
900 local vars = section.variables
901
902 if not title then --- trigger warning: arrow code ahead
903 logreport ("both", 2, "conf", "Section %d lacks a title; skipping.", i)
904 elseif not vars then
905 logreport ("both", 2, "conf", "Section %d (%s) lacks a variable section; skipping.", i, title)
906 else
907 local spec = option_spec[title]
908 if not spec then
909 logreport ("both", 2, "conf", "Section %d (%s) unknown; skipping.", i, title)
910 else
911 local newsection = new[title]
912 if not newsection then
913 newsection = { }
914 new[title] = newsection
915 end
916
917 for var, val in next, vars do
918 local vspec = spec[var] or spec.__default
919 local t_val = type (val)
920 if not vspec then
921 logreport ("both", 2, "conf",
922 "Section %d (%s): invalid configuration variable %q (%q); ignoring.",
923 i, title,
924 var, tostring (val))
925 elseif t_val ~= vspec.in_t then
926 logreport ("both", 2, "conf",
927 "Section %d (%s): type mismatch of input value %q (%q, %s != %s); ignoring.",
928 i, title,
929 var, tostring (val), t_val, vspec.in_t)
930 else --- type matches
931 local transform = vspec.transform
932 if transform then
933 local dval
934 local t_transform = type (transform)
935 if t_transform == function_t then
936 dval = transform (val)
937 elseif t_transform == table_t then
938 dval = transform[val]
939 end
940 if dval then
941 local out_t = vspec.out_t
942 if out_t then
943 local t_dval = type (dval)
944 if t_dval == out_t then
945 newsection[var] = dval
946 else
947 logreport ("both", 2, "conf",
948 "Section %d (%s): type mismatch of derived value of %q (%q, %s != %s); ignoring.",
949 i, title,
950 var, tostring (dval), t_dval, out_t)
951 end
952 else
953 newsection[var] = dval
954 end
955 else
956 logreport ("both", 2, "conf",
957 "Section %d (%s): value of %q could not be derived via %s from input %q; ignoring.",
958 i, title, var, t_transform, tostring (val))
959 end
960 else --- insert as is
961 newsection[var] = val
962 end
963 end
964 end
965 end
966 end
967 end
968 return new
969 end
970
971 local function apply (old, new)
972 if not new then
973 if not old then
974 return false
975 end
976 return tablecopy (old)
977 elseif not old then
978 return tablecopy (new)
979 end
980 local result = tablecopy (old)
981 for name, section in next, new do
982 local t_section = type (section)
983 if t_section ~= table_t then
984 logreport ("both", 1, "conf",
985 "Error applying configuration: entry %s is %s, expected table.",
986 section, t_section)
987 --- ignore
988 else
989 local currentsection = result[name]
990 for var, val in next, section do
991 currentsection[var] = val
992 end
993 end
994 end
995 return result
996 end
997
998 local function reconfigure()
999 for i = 1, #reconf_tasks do
1000 local name, task = unpack (reconf_tasks[i])
1001 logreport ("both", 3, "conf", "Launch post-configuration task %q.", name)
1002 if not task () then
1003 logreport ("both", 0, "conf", "Post-configuration task %q failed.", name)
1004 return false
1005 end
1006 end
1007 return true
1008 end
1009
1010 local function read (extra)
1011 if extra then
1012 add_config_paths (extra)
1013 end
1014
1015 local readme = resolve_config_path ()
1016 if readme == false then
1017 logreport ("both", 2, "conf", "No configuration file.")
1018 return false
1019 end
1020
1021 local raw = ioloaddata (readme)
1022 if not raw then
1023 logreport ("both", 2, "conf", "Error reading the configuration file %q.", readme)
1024 return false
1025 end
1026
1027 local parsed = lpegmatch (config_parser, raw)
1028 if not parsed then
1029 logreport ("both", 2, "conf", "Error parsing configuration file %q.", readme)
1030 return false
1031 end
1032
1033 local ret, msg = process_options (parsed)
1034 if not ret then
1035 logreport ("both", 2, "conf", "File %q is not a valid configuration file.", readme)
1036 logreport ("both", 2, "conf", "Error: %s", msg)
1037 return false
1038 end
1039 return ret
1040 end
1041
1042 local function apply_defaults ()
1043 local defaults = default_config
1044 local vars = read ()
1045 --- Side-effects galore ...
1046 config.luaotfload = apply (defaults, vars)
1047 return reconfigure ()
1048 end
1049
1050 local function dump ()
1051 local sections = table.sortedkeys (config.luaotfload)
1052 local confdata = { }
1053 for i = 1, #sections do
1054 local section = sections[i]
1055 local vars = config.luaotfload[section]
1056 local varnames = table.sortedkeys (vars)
1057 local sformats = formatters[section]
1058 if sformats then
1059 confdata[#confdata + 1] = format_section (section)
1060 for j = 1, #varnames do
1061 local var = varnames[j]
1062 local val = vars[var]
1063 local comment, sformat = unpack (sformats[var] or { })
1064 if not sformat then
1065 comment, sformat = unpack (sformats.__default or { })
1066 end
1067
1068 if sformat then
1069 local dashedvar = dashed (var)
1070 if comment then
1071 confdata[#confdata + 1] = commented (sformat (dashedvar, val))
1072 else
1073 confdata[#confdata + 1] = sformat (dashedvar, val)
1074 end
1075 end
1076
1077 end
1078 confdata[#confdata + 1] = ""
1079 end
1080 end
1081 if next(confdata) then
1082 iowrite (stringformat (conf_header,
1083 osdate ("%Y-%m-%d %H:%M:%S", os.time ())))
1084 iowrite (tableconcat (confdata, "\n"))
1085 iowrite (conf_footer)
1086 end
1087 end
1088
1089 -------------------------------------------------------------------------------
1090 --- EXPORTS
1091 -------------------------------------------------------------------------------
1092
1093 return function ()
1094 config.luaotfload = { }
1095 logreport = luaotfload.log.report
1096 local parsers = luaotfload.parsers
1097 config_parser = parsers.config
1098 stripslashes = parsers.stripslashes
1099
1100 luaotfload.default_config = default_config
1101 config.actions = {
1102 read = read,
1103 apply = apply,
1104 apply_defaults = apply_defaults,
1105 reconfigure = reconfigure,
1106 dump = dump,
1107 }
1108 if not apply_defaults () then
1109 logreport ("log", 0, "load",
1110 "Configuration unsuccessful: error loading default settings.")
1111 end
1112 return true
1113 end