Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/shellutil_unix.lua

raw symbols SCIP
1 --[[
2 Copyright 2016,2019 ARATA Mizuki
3
4 This file is part of ClutTeX.
5
6 ClutTeX is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 ClutTeX is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with ClutTeX. If not, see <http://www.gnu.org/licenses/>.
18 ]]
19
20 local assert = assert
21 local string_match = string.match
22 local table = table
23 local table_insert = table.insert
24 local table_concat = table.concat
25 local os_execute = os.execute
26
27 -- s: string
28 local function escape(s)
29 local len = #s
30 local result = {}
31 local t,i = string_match(s, "^([^']*)()")
32 assert(t)
33 if t ~= "" then
34 table_insert(result, "'")
35 table_insert(result, t)
36 table_insert(result, "'")
37 end
38 while i < len do
39 t,i = string_match(s, "^('+)()", i)
40 assert(t)
41 table_insert(result, '"')
42 table_insert(result, t)
43 table_insert(result, '"')
44 t,i = string_match(s, "^([^']*)()", i)
45 assert(t)
46 if t ~= "" then
47 table_insert(result, "'")
48 table_insert(result, t)
49 table_insert(result, "'")
50 end
51 end
52 return table_concat(result, "")
53 end
54
55 -- TEST CODE
56 assert(escape([[Hello world!]]) == [['Hello world!']])
57 assert(escape([[Hello' world!]]) == [['Hello'"'"' world!']])
58 assert(escape([[Hello' world!"]]) == [['Hello'"'"' world!"']])
59 -- END TEST CODE
60
61 local function has_command(name)
62 local result = os_execute("which " .. escape(name) .. " > /dev/null")
63 -- Note that os.execute returns a number on Lua 5.1 or LuaTeX
64 return result == 0 or result == true
65 end
66
67 return {
68 escape = escape,
69 has_command = has_command,
70 }