summaryrefslogtreecommitdiff
path: root/Master
diff options
context:
space:
mode:
authorManuel Pégourié-Gonnard <mpg@elzevir.fr>2008-07-06 14:44:02 +0000
committerManuel Pégourié-Gonnard <mpg@elzevir.fr>2008-07-06 14:44:02 +0000
commit810e0ee8b415d39879770c31373e38b64212b182 (patch)
treed86b5f728fc1ee538a77a7c3583bb7619986cc28 /Master
parent29b5920f218d79857d10aaed352b9d0524d5fbe0 (diff)
merging in Reinhard's conffile parser; quite untested atm
git-svn-id: svn://tug.org/texlive/trunk@9290 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master')
-rwxr-xr-xMaster/texmf/scripts/texlive/texdoc.tlu238
1 files changed, 160 insertions, 78 deletions
diff --git a/Master/texmf/scripts/texlive/texdoc.tlu b/Master/texmf/scripts/texlive/texdoc.tlu
index 7d50c59170f..871717c1c10 100755
--- a/Master/texmf/scripts/texlive/texdoc.tlu
+++ b/Master/texmf/scripts/texlive/texdoc.tlu
@@ -31,13 +31,22 @@ notfound_msg = [[
Sorry, I'm not smart enough to find documentation for PKGNAME. Please see
http://tug.org/pkginfo/PKGNAME for information about the package.]]
+-- [[ for our eyes only ]]
+opt_debug = false
+-- print debugging messages to stderr
+function debug (message)
+ if opt_debug then
+ io.stderr:write(arg[0]..': '..message..'\n')
+ end
+end
+
-- must have at least one arg
if not arg[1] then
print (usage_msg)
return
end
-opt_debug = false
+-- reading and setting options
mode = 'view'
verbose = false
while table.maxn(arg) > 0 and string.match(arg[1],'^%-') do
@@ -230,90 +239,150 @@ function set_listvar_from_expand_braces(oldvalue,texmfvar)
end
end -- set_listvar_from_expand_braces
---[[ initialize kpathsea ]]
+
+-- [[ set values from config files or defaults ]]
+
+config = {} -- everything is stored in this table
+-- newer values don't override older ones
+function set_config_element (key, value)
+ if config[key] == nil then
+ config[key] = value
+ end
+end
+-- set a whole list
+function set_config_list (conf)
+ for key, value in pairs(conf) do
+ set_config_element (key, value)
+ end
+end
+
+-- initialize kpathsea
kpse.set_program_name(arg[-1], "texdoc")
--- [[ initialize some variables ]]
-texdoc_formats = {'dvi','pdf','ps','txt','html'}
+-- read a particular config file if it exists. Used by setup_config() only.
+function read_config_file(configfile)
+ if (lfs.isfile(configfile)) then -- configfile exists
+ local cnf=assert(io.open(configfile, 'r'))
+ while true do
+ local key, val
+ local line=cnf:read('*line')
+ if line == nil then break end -- EOF
+ line=string.gsub(line, '%s*#.*$', '') -- comments begin with #.
+ line=string.gsub(line, '%s*$', '') -- remove trailing spaces.
+ if (string.match(line, '[%a_]+%s*=%s*.+')) then
+ key, val=string.match(line, '([%a_]+)%s*=%s*(.+)')
+ set_config_element(key, val)
+ else
+ if (not string.match (line, '^%s*$')) then
+ debug (configfile..': Syntax error: '..line)
+ end
+ end
+ end
+ cnf:close()
+ else
+ debug ('config file "'..configfile..'" not found')
+ end
+end
+
+-- read all config files.
+function setup_config ()
+ local TEXMFHOME, TEXMFLOCAL
+ local platform, configfile, configfiles
+ local ext, command, default_viewer
+
+ platform=string.match(kpse.var_value('SELFAUTOLOC'), '.*/(.*)$')
+ TEXMFHOME=kpse.var_value('TEXMFHOME')
+ TEXMFLOCAL=kpse.var_value('TEXMFLOCAL')
+
+ -- the following config files are read if they exist.
+ configfiles={
+ TEXMFHOME ..'/texdoc/texdoc-'..platform..'.cnf',
+ TEXMFHOME ..'/texdoc/texdoc.cnf',
+ TEXMFLOCAL..'/texdoc/texdoc-'..platform..'.cnf',
+ TEXMFLOCAL..'/texdoc/texdoc.cnf'
+ }
+
+ for i, configfile in ipairs(configfiles) do -- process all config files
+ debug ('processing "'..configfile..'"')
+ read_config_file(configfile)
+ end
+end
+-- some fall-back default values
if (os.type == "windows") then
-- probably Windows (or OS/2)
-- which commands should we use for unzipping?
- texdoc_unzip = {
- gz = "gzip -d -c ",
- bz2 = "bzip2 -d -c "
- }
- texdoc_viewer = {
- dvi = '(start /wait %s ) &',
- html = '(start /wait %s) &',
- pdf = '(start /wait %s) &',
- ps = '(start /wait %s) &',
- txt = '(start /wait %s) &',
- tex = '(start /wait %s) &'
+ set_config_list {
+ unzip_gz = "gzip -d -c ",
+ unzip_bz2 = "bzip2 -d -c ",
+ rm_file = 'del /F ',
+ rm_dir = 'rmdir ',
+ viewer_dvi = '(start /wait %s) &',
+ viewer_html = '(start /wait %s) &',
+ viewer_pdf = '(start /wait %s) &',
+ viewer_ps = '(start /wait %s) &',
+ viewer_txt = '(start /wait %s) &',
+ viewer_tex = '(start /wait %s) &'
}
- rmfile_command = 'del /F '
- rmdir_command = 'rmdir '
-else
- -- since we don't support msdos, if os.type is not windows, it's unix
- texdoc_unzip = {
- gz = "gzip -d -c ",
- bz2 = "bzip2 -d -c "
+else -- since we don't support msdos, if os.type is not windows, it's unix
+ set_config_list {
+ unzip_gz = "gzip -d -c ",
+ unzip_bz2 = "bzip2 -d -c ",
+ rm_file = 'rm -f ',
+ rm_dir = 'rmdir '
}
if (os.name == 'macosx') then
- texdoc_viewer = {
- dvi = '(open %s)',
- html = '(open %s)',
- pdf = '(open %s)',
- ps = '(open %s)',
- txt = '(less %s )',
- tex = '(less %s )'
+ set_config_list {
+ viewer_dvi = '(open %s)',
+ viewer_html = '(open %s)',
+ viewer_pdf = '(open %s)',
+ viewer_ps = '(open %s)',
+ viewer_txt = '(less %s)',
+ viewer_tex = '(less %s)'
}
else
- texdoc_viewer = {
- dvi = '(xdvi %s ) &',
- html = '(see %s) &',
- pdf = '(xpdf %s) &',
- ps = '(gv %s) &',
- txt = '(less %s )',
- tex = '(less %s )'
+ set_config_list {
+ viewer_dvi = '(xdvi %s) &',
+ viewer_html = '(see %s) &',
+ viewer_pdf = '(xpdf %s) &',
+ viewer_ps = '(gv %s) &',
+ viewer_txt = '(less %s)',
+ viewer_tex = '(less %s)'
}
end
- rmfile_command = 'rm -f '
- rmdir_command = 'rmdir '
end
-texdoc_zipformats = {'','gz','bz2'}
-texdoc_formats = {'','dvi','html','pdf','ps','txt','tex'}
-extlist = {'','.dvi', '.dvi.gz', '.dvi.bz2', '.pdf', '.pdf.gz', '.pdf.bz2',
-'.ps', '.ps.gz', '.ps.bz2', '.txt', '.txt.gz', '.txt.bz2', '.html'}
-
--- [[ override hardcoded variables with values from texmf.cnf ]]
-rmfile_command = set_var_from_texmf(rmfile_command,'TEXDOC_RMFILE')
-rmdir_command = set_var_from_texmf(rmdir_command,'TEXDOC_RMDIR')
-texdoc_formats = set_listvar_from_texmf(texdoc_formats,'TEXDOC_FORMATS')
-for format in list_iter(texdoc_formats) do
- viewer_var = 'TEXDOC_VIEWER_' .. string.upper(format)
- texdoc_viewer[format] = set_var_from_texmf(texdoc_viewer[format],viewer_var)
-end
-texdoc_zipformats = set_listvar_from_texmf(texdoc_zipformats,'TEXDOC_ZIPFORMATS')
-for zipext in list_iter(texdoc_zipformats) do
- viewer_var = 'TEXDOC_UNZIP_' .. string.upper(zipext)
- texdoc_unzip[zipext] = set_var_from_texmf(texdoc_unzip[zipext],viewer_var)
-end
-extlist = set_listvar_from_expand_braces(extlist,'$TEXDOCEXT')
+set_config_list {
+ zipformats = {'','gz','bz2'},
+ formats = {'dvi','pdf','ps','txt','html'},
+ extlist = {
+ '','.dvi', '.dvi.gz', '.dvi.bz2', '.pdf', '.pdf.gz', '.pdf.bz2',
+ '.ps', '.ps.gz', '.ps.bz2', '.txt', '.txt.gz', '.txt.bz2', '.html'
+ }
+}
+
-- we want an empty string for ext at the beginning, so that it works
-- to specify the complete filename. Doesn't matter when there's one
-- more empty string, but we can easily avoid two in a row
-if not extlist[1] == '' then
- insert(extlist,1,'')
+if not config.extlist[1] == '' then
+ insert(config.extlist,1,'')
end
--- [[ uncompress if needed ]]
+--[[
+print "-- Begin config"
+for key, value in pairs(config) do
+ print(string.format('config.%-11s ==> "%s"', key, value))
+end
+print "-- End config"
+]]
+-- [[ functions for viewing ]]
+
+-- uncompress if needed
function unzip_if_needed (ext)
zipext = string.match(ext,'%..*%.(.*)')
if zipext then
- unzip_command = texdoc_unzip[zipext]
+ unzip_command = config['unzip_'..zipext]
viewext = string.match(ext,'%.(.*)%..*$')
basebame_pattern = '.*/(.*%.' .. viewext .. ')'
basename = string.match(filename,basebame_pattern)
@@ -328,8 +397,8 @@ function unzip_if_needed (ext)
else
print("Error executing \n" .. unzip_commandline)
end
- viewer_replacement = filename .. ';' .. rmfile_command
- .. filename .. ';' .. rmdir_command .. tmpdir
+ viewer_replacement = filename .. ';' .. config.rm_file
+ .. filename .. ';' .. config.rm_dir .. tmpdir
else
if ext == '' then
-- fallback if complete filename has been specified
@@ -341,40 +410,53 @@ function unzip_if_needed (ext)
end
viewer_replacement = filename
viewext = string.match(ext,'%.(.*)$')
- if not viewext or not texdoc_viewer[viewext] then
+ if not viewext or not config['viewer_'..viewext] then
-- complete filename specified, unknown extension, use "txt"
viewext = 'txt'
end
end -- zipped or not
end
+-- view a file, if possible
+function try_viewing ()
+ if config['viewer_'..viewext] == nil then
+ print ("Sorry, no "..viewext.."viewer found. You can choose one\n"..
+ "with the viewer_"..viewext" variable in texdoc.cnf.\n"..
+ "Please report the problem so we can add your viewer as a default.\n")
+ else
+ view_command = string.gsub(
+ config['viewer_'..viewext], '%%s',viewer_replacement)
+ if verbose then
+ print(view_command)
+ end
+ view_result = os.execute(view_command)
+ if view_result then
+ return true
+ else
+ print("Error executing \n" .. view_command)
+ end
+ end
+ return false
+end
+
+-- [[ the main loop ]]
for docname in list_iter (arg) do
local docfound = false
if string.match(mode,'search') then
docfound = true
pattern_search(docname)
elseif string.match(mode,'view') or string.match(mode,'list') then
- for ext in list_iter(extlist) do
+ for ext in list_iter(config.extlist) do
filename = kpse.find_file(docname .. ext,
"TeX system documentation")
if filename then
docfound = true
if string.match (mode, 'list') then
print(filename)
- else
- -- mode is view, is unzipping needed?
+ else -- mode is view, is unzipping needed?
unzip_if_needed(ext)
- view_command = string.gsub(
- texdoc_viewer[viewext], '%%s',viewer_replacement)
- if verbose then
- print(view_command)
- end
- view_result = os.execute(view_command)
- if view_result then
- do break end
- else
- print("Error executing \n" .. view_command)
- end
+ local ok = try_viewing()
+ if ok then break end
end -- list or view
end -- found a filename with that extension or not?
end -- for ext