/** \file builtin_complete.c Functions defining the complete builtin Functions used for implementing the complete builtin. */ #include "config.h" #include #include #include #include #include #include #include #include "fallback.h" #include "util.h" #include "wutil.h" #include "builtin.h" #include "common.h" #include "complete.h" #include "wgetopt.h" #include "parser.h" #include "reader.h" /* builtin_complete_* are a set of rather silly looping functions that make sure that all the proper combinations of complete_add or complete_remove get called. This is needed since complete allows you to specify multiple switches on a single commandline, like 'complete -s a -s b -s c', but the complete_add function only accepts one short switch and one long switch. */ /** Silly function */ static void builtin_complete_add2(const wchar_t *cmd, int cmd_type, const wchar_t *short_opt, const wcstring_list_t &gnu_opt, const wcstring_list_t &old_opt, int result_mode, const wchar_t *condition, const wchar_t *comp, const wchar_t *desc, int flags) { size_t i; const wchar_t *s; for (s=short_opt; *s; s++) { complete_add(cmd, cmd_type, *s, 0, 0, result_mode, condition, comp, desc, flags); } for (i=0; i comp; complete(do_complete_param, comp, COMPLETION_REQUEST_DEFAULT); for (size_t i=0; i< comp.size() ; i++) { const completion_t &next = comp.at(i); /* Make a fake commandline, and then apply the completion to it. */ const wcstring faux_cmdline = token; size_t tmp_cursor = faux_cmdline.size(); wcstring faux_cmdline_with_completion = completion_apply_to_command_line(next.completion, next.flags, faux_cmdline, &tmp_cursor, false); /* completion_apply_to_command_line will append a space unless COMPLETE_NO_SPACE is set. We don't want to set COMPLETE_NO_SPACE because that won't close quotes. What we want is to close the quote, but not append the space. So we just look for the space and clear it. */ if (!(next.flags & COMPLETE_NO_SPACE) && string_suffixes_string(L" ", faux_cmdline_with_completion)) { faux_cmdline_with_completion.resize(faux_cmdline_with_completion.size() - 1); } /* The input data is meant to be something like you would have on the command line, e.g. includes backslashes. The output should be raw, i.e. unescaped. So we need to unescape the command line. See #1127 */ unescape_string_in_place(&faux_cmdline_with_completion, UNESCAPE_DEFAULT); stdout_buffer.append(faux_cmdline_with_completion); /* Append any description */ if (! next.description.empty()) { stdout_buffer.push_back(L'\t'); stdout_buffer.append(next.description); } stdout_buffer.push_back(L'\n'); } recursion_level--; } } else if (woptind != argc) { append_format(stderr_buffer, _(L"%ls: Too many arguments\n"), argv[0]); builtin_print_help(parser, argv[0], stderr_buffer); res = true; } else if (cmd.empty() && path.empty()) { /* No arguments specified, meaning we print the definitions of * all specified completions to stdout.*/ complete_print(stdout_buffer); } else { int flags = COMPLETE_AUTO_SPACE; if (remove) { builtin_complete_remove(cmd, path, short_opt.c_str(), gnu_opt, old_opt); } else { builtin_complete_add(cmd, path, short_opt.c_str(), gnu_opt, old_opt, result_mode, authoritative, condition, comp, desc, flags); } // Handle wrap targets (probably empty) // We only wrap commands, not paths for (size_t w=0; w < wrap_targets.size(); w++) { const wcstring &wrap_target = wrap_targets.at(w); for (size_t i=0; i < cmd.size(); i++) { (remove ? complete_remove_wrapper : complete_add_wrapper)(cmd.at(i), wrap_target); } } } } return res ? 1 : 0; }