From 4837a2d0dfef1f2a7370b2765a9442a5deab4f36 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Fri, 23 Nov 2012 11:12:22 -0800 Subject: [PATCH] Replaced some usage of wchar_t * with wcstring in complete(). Some style fixes. --- common.cpp | 31 ++++++++--------- complete.cpp | 90 ++++++++++++++++++++++---------------------------- fish_tests.cpp | 5 +-- reader.cpp | 2 +- tokenizer.cpp | 9 ++--- 5 files changed, 64 insertions(+), 73 deletions(-) diff --git a/common.cpp b/common.cpp index dff67592f..d1f273b1f 100644 --- a/common.cpp +++ b/common.cpp @@ -1095,8 +1095,8 @@ wchar_t *unescape(const wchar_t * orig, int flags) int bracket_count=0; wchar_t prev=0; wchar_t *in; - bool unescape_special = !! (flags & UNESCAPE_SPECIAL); - bool allow_incomplete = !! (flags & UNESCAPE_INCOMPLETE); + bool unescape_special = !!(flags & UNESCAPE_SPECIAL); + bool allow_incomplete = !!(flags & UNESCAPE_INCOMPLETE); CHECK(orig, 0); @@ -1105,8 +1105,9 @@ wchar_t *unescape(const wchar_t * orig, int flags) if (!in) DIE_MEM(); - - enum { + + enum + { mode_unquoted, mode_single_quotes, mode_double_quotes @@ -1120,9 +1121,9 @@ wchar_t *unescape(const wchar_t * orig, int flags) switch (mode) { - /* - Mode 0 means unquoted string - */ + /* + Mode 0 means unquoted string + */ case mode_unquoted: { if (c == L'\\') @@ -1130,10 +1131,10 @@ wchar_t *unescape(const wchar_t * orig, int flags) switch (in[++in_pos]) { - /* - A null character after a backslash is an - error, return null - */ + /* + A null character after a backslash is an + error, return null + */ case L'\0': { if (!allow_incomplete) @@ -1321,7 +1322,7 @@ wchar_t *unescape(const wchar_t * orig, int flags) in[out_pos]=L'\t'; break; } - + /* \v means vertical tab */ @@ -1330,7 +1331,7 @@ wchar_t *unescape(const wchar_t * orig, int flags) in[out_pos]=L'\v'; break; } - + /* If a backslash is followed by an actual newline, swallow them both */ case L'\n': out_pos--; @@ -1522,7 +1523,7 @@ wchar_t *unescape(const wchar_t * orig, int flags) } } break; - + default: { in[out_pos++] = L'\\'; @@ -1589,7 +1590,7 @@ wchar_t *unescape(const wchar_t * orig, int flags) in[out_pos]=in[in_pos]; break; } - + case '\n': { out_pos--; diff --git a/complete.cpp b/complete.cpp index 985107b96..5efa53b3b 100644 --- a/complete.cpp +++ b/complete.cpp @@ -1765,7 +1765,7 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty completer_t completer(cmd, type); const wchar_t *tok_begin, *tok_end, *cmdsubst_begin, *cmdsubst_end, *prev_begin, *prev_end; - const wchar_t *current_token=0, *prev_token=0; + wcstring current_token, prev_token; wcstring current_command; int on_command=0; size_t pos; @@ -1789,7 +1789,7 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty /** If we are completing a variable name or a tilde expansion user - name, we do that and return. No need for any other competions. + name, we do that and return. No need for any other completions. */ if (!done) @@ -1806,7 +1806,7 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty int had_cmd=0; int end_loop=0; - + tokenizer_t tok(buff.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS); while (tok_has_next(&tok) && !end_loop) { @@ -1897,7 +1897,14 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty current_token = wcsndup(tok_begin, cursor_pos-(tok_begin-cmd_cstr)); - prev_token = prev_begin ? wcsndup(prev_begin, prev_end - prev_begin): wcsdup(L""); + if (prev_begin) + { + prev_token.assign(prev_begin, prev_end - prev_begin); + } + else + { + prev_token.clear(); + } // debug( 0, L"on_command: %d, %ls %ls\n", on_command, current_command, current_token ); @@ -1908,8 +1915,8 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty subcommand. */ - if ((on_command || (wcscmp(current_token, L"--") == 0)) && - (current_token[0] == L'-') && + if ((on_command || current_token == L"--") && + string_prefixes_string(L"-", current_token) && !(use_command && use_function && use_builtin)) { if (use_command == 0) @@ -1929,63 +1936,44 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty on_command=1; } - /* - We don't want these to be null - */ - if (!current_token) + if (on_command) { - current_token = wcsdup(L""); + /* Complete command filename */ + completer.complete_cmd(current_token, use_function, use_builtin, use_command); } - - if (!prev_token) + else { - prev_token = wcsdup(L""); - } + int do_file=0; - if (current_token && prev_token) - { - if (on_command) + wcstring current_command_unescape = current_command; + wcstring prev_token_unescape = prev_token; + wcstring current_token_unescape = current_token; + + if (unescape_string(current_command_unescape, 0) && + unescape_string(prev_token_unescape, 0) && + unescape_string(current_token_unescape, UNESCAPE_INCOMPLETE)) { - /* Complete command filename */ - completer.complete_cmd(current_token, use_function, use_builtin, use_command); + do_file = completer.complete_param(current_command_unescape, + prev_token_unescape, + current_token_unescape, + !had_ddash); } - else - { - int do_file=0; - wcstring current_command_unescape = current_command; - wcstring prev_token_unescape = prev_token; - wcstring current_token_unescape = current_token; + /* + If we have found no command specific completions at + all, fall back to using file completions. + */ + if (completer.empty()) + do_file = 1; - if (unescape_string(current_command_unescape, 0) && - unescape_string(prev_token_unescape, 0) && - unescape_string(current_token_unescape, UNESCAPE_INCOMPLETE)) - { - do_file = completer.complete_param(current_command_unescape, - prev_token_unescape, - current_token_unescape, - !had_ddash); - } - - /* - If we have found no command specific completions at - all, fall back to using file completions. - */ - if (completer.empty()) - do_file = 1; - - /* - This function wants the unescaped string - */ - completer.complete_param_expand(current_token, do_file); - } + /* + This function wants the unescaped string + */ + completer.complete_param_expand(current_token, do_file); } } - free((void *)current_token); - free((void *)prev_token); - comps = completer.get_completions(); completer.get_commands_to_load(commands_to_load); } diff --git a/fish_tests.cpp b/fish_tests.cpp index d442ebaac..ccf453377 100644 --- a/fish_tests.cpp +++ b/fish_tests.cpp @@ -326,9 +326,10 @@ static void test_tok() }; say(L"Test correct tokenization"); - + tokenizer_t t(str, 0); - for (size_t i=0; i < sizeof types / sizeof *types; i++, tok_next(&t)) { + for (size_t i=0; i < sizeof types / sizeof *types; i++, tok_next(&t)) + { if (types[i] != tok_last_type(&t)) { err(L"Tokenization error:"); diff --git a/reader.cpp b/reader.cpp index af7e2e979..70a1897cc 100644 --- a/reader.cpp +++ b/reader.cpp @@ -1187,7 +1187,7 @@ struct autosuggestion_context_t if (! cursor_at_end && iswspace(last_char)) return 0; - // On the other hand, if the line ends with a quote, don't go dumping stuff after the quote + /* On the other hand, if the line ends with a quote, don't go dumping stuff after the quote */ if (wcschr(L"'\"", last_char) && cursor_at_end) return 0; diff --git a/tokenizer.cpp b/tokenizer.cpp index e2e515bfc..01c562ad2 100644 --- a/tokenizer.cpp +++ b/tokenizer.cpp @@ -227,7 +227,8 @@ static void read_string(tokenizer_t *tok) start = tok->buff; bool is_first = true; - enum tok_mode_t { + enum tok_mode_t + { mode_regular_text = 0, // regular text mode_subshell = 1, // inside of subshell mode_array_brackets = 2, // inside of array brackets @@ -260,7 +261,7 @@ static void read_string(tokenizer_t *tok) tok->buff++; continue; } - + switch (mode) { case mode_regular_text: @@ -358,7 +359,7 @@ static void read_string(tokenizer_t *tok) break; } break; - + case mode_array_brackets: switch (*tok->buff) { @@ -410,7 +411,7 @@ static void read_comment(tokenizer_t *tok) start = tok->buff; while (*(tok->buff)!= L'\n' && *(tok->buff)!= L'\0') tok->buff++; - + size_t len = tok->buff - start; tok->last_token.assign(start, len);