3 -- CivetWeb command line completion for bash
7 To use auto-completion for bash, you need to define a command for the bash built-in
8 [*complete*](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html).
10 Create a file called "civetweb" in the completion folder.
11 Depending on Linux distribution and version, this might be
12 /usr/share/bash-completion/completions/, /etc/bash_completion or another folder.
14 The file has to contain just one line:
15 complete -C /path/to/civetweb/resources/complete.lua civetweb
17 The complete command script is implemented in this file.
18 It needs Lua 5.2 to be installed (for Debian based systems: "sudo apt-get install lua5.2").
19 In case lua5.2 is not located in /usr/bin/lua5.2 (see "which lua5.2"),
20 the first line (#!) needs to be adapted accordingly.
24 ---------------------------------------------------------------------------------------------------
26 -- The bash "complete -C" has an awkward interface:
27 -- see https://unix.stackexchange.com/questions/250262/how-to-use-bashs-complete-or-compgen-c-command-option
28 -- Command line arguments
29 cmd = arg[1] -- typically "./civetweb" or whatever path was used
30 this = arg[2] -- characters already typed for the next options
31 last = arg[3] -- option before this one
32 -- Environment variables
33 comp_line = os.getenv("COMP_LINE") -- entire command line
34 comp_point = os.getenv("COMP_POINT") -- position of cursor (index)
35 comp_type = os.getenv("COMP_TYPE") -- type:
36 -- 9 for normal completion
37 -- 33 when listing alternatives on ambiguous completions
38 -- 37 for menu completion
39 -- 63 when tabbing between ambiguous completions
40 -- 64 to list completions after a partial completion
43 -- Debug-Print function (must use absolute path for log file)
45 --[[ comment / uncomment to enable debugging
46 local f = io.open("/tmp/complete.log", "a");
52 -- Helper function: Check if files exist
53 function fileexists(name)
54 local f = io.open(name, "r")
64 dp("complete: cmd=" .. cmd .. ", this=" .. this .. ", last=" .. last .. ", type=" .. comp_type)
67 -- Trim command line (remove spaces)
68 trim_comp_line = string.match(comp_line, "^%s*(.-)%s*$")
70 if (trim_comp_line == cmd) then
71 -- this is the first option
72 dp("Suggest --help argument")
77 is_h = string.find(comp_line, "^%s*" .. cmd .. "%s+%-h%s")
78 is_h = is_h or string.find(comp_line, "^%s*" .. cmd .. "%s+%--help%s")
79 is_h = is_h or string.find(comp_line, "^%s*" .. cmd .. "%s+%-H%s")
82 dp("If --help is used, no additional argument is allowed")
86 is_a = string.find(comp_line, "^%s*" .. cmd .. "%s+%-A%s")
87 is_c = string.find(comp_line, "^%s*" .. cmd .. "%s+%-C%s")
88 is_i = string.find(comp_line, "^%s*" .. cmd .. "%s+%-I%s")
89 is_r = string.find(comp_line, "^%s*" .. cmd .. "%s+%-R%s")
92 dp("If --I is used, no additional argument is allowed")
96 -- -A and -R require the password file as second argument
97 htpasswd_r = ".htpasswd <mydomain.com> <username>"
98 htpasswd_a = htpasswd_r .. " <password>"
99 if (last == "-A") and (this == htpasswd_a:sub(1,#this)) then
100 dp("Fill with option template for -A")
104 if (last == "-R") and (this == htpasswd_r:sub(1,#this)) then
105 dp("Fill with option template for -R")
109 if (is_a or is_r) then
110 dp("Options -A and -R have a fixed number of arguments")
114 -- -C requires an URL, usually starting with http:// or https://
116 if (last == "-C") and (this == http:sub(1,#this)) then
118 print(http.. "localhost/")
122 if (last == "-C") and (this == http:sub(1,#this)) then
124 print(http.. "localhost/")
128 dp("Option -C has just one argument")
133 -- Take options directly from "--help" output of executable
134 optfile = "/tmp/civetweb.options"
135 if not fileexists(optfile) then
136 dp("options file " .. optfile .. " missing")
137 os.execute(cmd .. " --help > " .. optfile .. " 2>&1")
139 dp("options file " .. optfile .. " found")
142 for l in io.lines(optfile) do
143 local lkey, lval = l:match("^%s+(%-[^%s]*)%s*([^%s]*)%s*$")
145 local thislen = string.len(this)
146 if (thislen>0) and (this == lkey:sub(1,thislen)) then
148 dp("match: " .. lkey)
158 -- at least one options matches
163 -- suggest the default value
168 -- No context to determine next argument
169 dp("no specific option")