List.iter (Printf.fprintf stderr "\t%s\n%!") possibilities;
String.concat " " (List.map Parse.shquote cmd)
+ let complete_file cmd f =
+ let dir_files dir =
+ let d = Unix.opendir dir in
+ let rec gen_list () =
+ try
+ let f = Unix.readdir d in
+ f :: gen_list ()
+ with End_of_file -> []
+ in
+ let files = gen_list () in
+ Unix.closedir d;
+ files
+ in
+ let files =
+ match String.rindex_opt f '/' with
+ | Some 0 -> List.map (( ^ ) "/") (dir_files "/")
+ | Some i -> List.map (( ^ ) (String.sub f 0 (i + 1))) (dir_files (String.sub f 0 i))
+ | None -> dir_files "."
+ in
+ real_complete cmd (List.map (fun f -> if (Unix.stat f).st_kind = S_DIR then f ^ "/" else f) files)
+
let autocomplete cmd =
match try Parse.shlex cmd with Parse.NonTerminatedString -> Parse.shlex (cmd ^ "'") with
| ["quit"] -> "quit"
| ["select"] -> real_complete ["select"; ""] (List.map get_name Automata.automata)
| ["select"; n] -> real_complete ["select"; n] (List.map get_name Automata.automata)
- | ["write"] -> "write "
- | ["read"] -> "read "
+ | ["write"] -> complete_file ["write"; ""] ""
+ | ["write"; n] -> complete_file ["write"; n] n
+ | ["read"] -> complete_file ["read"; ""] ""
+ | ["read"; n] -> complete_file ["read"; n] n
| [] -> real_complete [""] ["quit"; "select"; "write"; "read"]
| [cmd] -> real_complete [cmd] ["quit"; "select"; "write"; "read"]
| parsed -> String.concat " " (List.map Parse.shquote parsed)