summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <phg42.2a@gmail.com>2014-07-24 22:00:28 +0200
committerPhilipp Gesang <phg42.2a@gmail.com>2014-07-24 22:10:47 +0200
commiteb9d41d52ea5168284472a7290bce7a579ba1794 (patch)
treeab8d0b2ac65a98d2c21e321a78bfbee5f29f8609
parent04dc6c632f7bc85b48d12af695392fb5e4d0faef (diff)
downloadluaotfload-eb9d41d52ea5168284472a7290bce7a579ba1794.tar.gz
[conf,tool] implement configuration writer
Now ``luaotfload-tool --dumpconf`` can be used to output the current configuration to a file.
-rw-r--r--src/luaotfload-configuration.lua136
-rwxr-xr-xsrc/luaotfload-tool.lua34
2 files changed, 153 insertions, 17 deletions
diff --git a/src/luaotfload-configuration.lua b/src/luaotfload-configuration.lua
index dfa222c..10e791a 100644
--- a/src/luaotfload-configuration.lua
+++ b/src/luaotfload-configuration.lua
@@ -5,7 +5,7 @@
-- REQUIREMENTS: Luaotfload 2.5 or above
-- AUTHOR: Philipp Gesang (Phg), <phg42.2a@gmail.com>
-- VERSION: same as Luaotfload
--- MODIFIED: 2014-07-13 14:19:32+0200
+-- MODIFIED: 2014-07-24 21:49:31+0200
-------------------------------------------------------------------------------
--
@@ -24,16 +24,17 @@ config.luaotfload = { }
local status_file = "luaotfload-status"
local luaotfloadstatus = require (status_file)
-local string = string
-local stringsub = string.sub
local stringexplode = string.explode
-local stringstrip = string.strip
local stringfind = string.find
+local stringformat = string.format
+local string = string
+local stringstrip = string.strip
+local stringsub = string.sub
-local table = table
local tableappend = table.append
-local tablecopy = table.copy
local tableconcat = table.concat
+local tablecopy = table.copy
+local table = table
local tabletohash = table.tohash
local math = math
@@ -42,8 +43,10 @@ local mathfloor = math.floor
local io = io
local ioloaddata = io.loaddata
local iopopen = io.popen
+local iowrite = io.write
local os = os
+local osdate = os.date
local osgetenv = os.getenv
local lpeg = require "lpeg"
@@ -475,6 +478,92 @@ local option_spec = {
}
-------------------------------------------------------------------------------
+--- FORMATTERS
+-------------------------------------------------------------------------------
+
+local indent = " "
+local format_string = function (var, val)
+ return stringformat (indent .. "%s = %s", var, val)
+end
+
+local format_integer = function (var, val)
+ return stringformat (indent .. "%s = %d", var, val)
+end
+
+local format_boolean = function (var, val)
+ return stringformat (indent .. "%s = %s", var, val == true and "true" or "false")
+end
+
+local format_section = function (title)
+ return stringformat ("[%s]", title)
+end
+
+local commented = function (str)
+ return ";" .. str
+end
+
+local underscore_replacer = lpeg.replacer ("_", "-", true)
+
+local dashed = function (var)
+ --- INI spec dictates that dashes are valid in variable names, not
+ --- underscores.
+ return underscore_replacer (var) or var
+end
+
+local conf_header = [==[
+;;-----------------------------------------------------------------------------
+;; Luaotfload Configuration
+;;-----------------------------------------------------------------------------
+;;
+;; This file was generated by luaotfload-tool
+;; on %s. Configuration variables
+;; are documented in the manual to luaotfload.conf(5).
+;;
+;;-----------------------------------------------------------------------------
+
+]==]
+
+local conf_footer = [==[
+
+;; vim:filetype=dosini:expandtab:shiftwidth=2
+]==]
+
+--- Each dumpable variable (the ones mentioned in the man page) receives a
+--- formatter that will be used in dumping the variable. Each value receives a
+--- “commented” flag that indicates whether or not the line should be printed
+--- as a comment.
+
+local formatters = {
+ db = {
+ compress = { false, format_boolean },
+ formats = { false, format_string },
+ max_fonts = { false, format_integer },
+ scan_local = { false, format_boolean },
+ skip_read = { false, format_boolean },
+ strip = { false, format_boolean },
+ update_live = { false, format_boolean },
+ },
+ misc = {
+ bisect = { false, format_boolean },
+ statistics = { false, format_boolean },
+ termwidth = { true , format_integer },
+ version = { true , format_string },
+ },
+ paths = {
+ cache_dir = { false, format_string },
+ names_dir = { false, format_string },
+ index_file = { false, format_string },
+ lookups_file = { false, format_string },
+ },
+ run = {
+ color_callback = { false, format_string },
+ definer = { false, format_string },
+ log_level = { false, format_integer },
+ resolver = { false, format_string },
+ },
+}
+
+-------------------------------------------------------------------------------
--- MAIN FUNCTIONALITY
-------------------------------------------------------------------------------
@@ -689,6 +778,40 @@ local apply_defaults = function ()
return reconfigure ()
end
+local dump = function ()
+ local sections = table.sortedkeys (config.luaotfload)
+ local confdata = { }
+ for i = 1, #sections do
+ local section = sections[i]
+ local vars = config.luaotfload[section]
+ local varnames = table.sortedkeys (vars)
+ local sformats = formatters[section]
+ if sformats then
+ confdata[#confdata + 1] = format_section (section)
+ for j = 1, #varnames do
+ local var = varnames[j]
+ local val = vars[var]
+ local comment, sformat = unpack (sformats[var] or { })
+ if sformat then
+ local dashedvar = dashed (var)
+ if comment then
+ confdata[#confdata + 1] = commented (sformat (dashedvar, val))
+ else
+ confdata[#confdata + 1] = sformat (dashedvar, val)
+ end
+ end
+ end
+ confdata[#confdata + 1] = ""
+ end
+ end
+ if next(confdata) then
+ iowrite (stringformat (conf_header,
+ osdate ("%Y-%m-d %H:%M:%S", os.time ())))
+ iowrite (tableconcat (confdata, "\n"))
+ iowrite (conf_footer)
+ end
+end
+
-------------------------------------------------------------------------------
--- EXPORTS
-------------------------------------------------------------------------------
@@ -700,5 +823,6 @@ config.actions = {
apply = apply,
apply_defaults = apply_defaults,
reconfigure = reconfigure,
+ dump = dump,
}
diff --git a/src/luaotfload-tool.lua b/src/luaotfload-tool.lua
index 754f5c7..02c5a45 100755
--- a/src/luaotfload-tool.lua
+++ b/src/luaotfload-tool.lua
@@ -6,7 +6,7 @@
-- AUTHOR: Khaled Hosny, Élie Roux, Philipp Gesang
-- VERSION: 2.5
-- LICENSE: GPL v2.0
--- MODIFIED: 2014-07-24 22:07:35+0200
+-- MODIFIED: 2014-07-24 22:10:32+0200
-----------------------------------------------------------------------
luaotfload = luaotfload or { }
@@ -739,9 +739,10 @@ set.
--]]--
local action_sequence = {
- "config", "loglevel", "help", "version",
- "diagnose", "blacklist", "cache", "flush",
- "bisect", "generate", "list", "query",
+ "config" , "loglevel" , "help" , "version" ,
+ "dumpconf" , "diagnose" , "blacklist" , "cache" ,
+ "flush" , "bisect" , "generate" , "list" ,
+ "query" ,
}
local action_pending = tabletohash(action_sequence, false)
@@ -781,6 +782,11 @@ actions.version = function (job)
return true, false
end
+actions.dumpconf = function (job)
+ config.actions.dump ()
+ return true, false
+end
+
actions.help = function (job)
help_msg (job.help_version or "luaotfload-tool")
return true, false
@@ -1413,6 +1419,7 @@ local process_cmdline = function ( ) -- unit -> jobspec
cache = 1,
conf = 1,
diagnose = 1,
+ dumpconf = 0,
["dry-run"] = "D",
["flush-lookups"] = "l",
fields = 1,
@@ -1551,15 +1558,20 @@ local process_cmdline = function ( ) -- unit -> jobspec
result.bisect = optarg[n]
action_pending.bisect = true
elseif v == "conf" then
- local extra = stringexplode (optarg[n], ",+")
- if extra then
- local extra_config = result.extra_config
- if extra_config then
- table.append (extra_config, extra)
- else
- result.extra_config = extra
+ local confname = optarg[n]
+ if confname then
+ local extra = stringexplode (optarg[n], ",+")
+ if extra then
+ local extra_config = result.extra_config
+ if extra_config then
+ table.append (extra_config, extra)
+ else
+ result.extra_config = extra
+ end
end
end
+ elseif v == "dumpconf" then
+ action_pending["dumpconf"] = true
elseif v == "print-conf" then
result.print_config = true
end