Replaced some usage of wchar_t * with wcstring in complete(). Some style fixes.

This commit is contained in:
ridiculousfish 2012-11-23 11:12:22 -08:00
parent be80e1a863
commit 4837a2d0df
5 changed files with 64 additions and 73 deletions

View file

@ -1095,8 +1095,8 @@ wchar_t *unescape(const wchar_t * orig, int flags)
int bracket_count=0; int bracket_count=0;
wchar_t prev=0; wchar_t prev=0;
wchar_t *in; wchar_t *in;
bool unescape_special = !! (flags & UNESCAPE_SPECIAL); bool unescape_special = !!(flags & UNESCAPE_SPECIAL);
bool allow_incomplete = !! (flags & UNESCAPE_INCOMPLETE); bool allow_incomplete = !!(flags & UNESCAPE_INCOMPLETE);
CHECK(orig, 0); CHECK(orig, 0);
@ -1105,8 +1105,9 @@ wchar_t *unescape(const wchar_t * orig, int flags)
if (!in) if (!in)
DIE_MEM(); DIE_MEM();
enum { enum
{
mode_unquoted, mode_unquoted,
mode_single_quotes, mode_single_quotes,
mode_double_quotes mode_double_quotes
@ -1120,9 +1121,9 @@ wchar_t *unescape(const wchar_t * orig, int flags)
switch (mode) switch (mode)
{ {
/* /*
Mode 0 means unquoted string Mode 0 means unquoted string
*/ */
case mode_unquoted: case mode_unquoted:
{ {
if (c == L'\\') if (c == L'\\')
@ -1130,10 +1131,10 @@ wchar_t *unescape(const wchar_t * orig, int flags)
switch (in[++in_pos]) switch (in[++in_pos])
{ {
/* /*
A null character after a backslash is an A null character after a backslash is an
error, return null error, return null
*/ */
case L'\0': case L'\0':
{ {
if (!allow_incomplete) if (!allow_incomplete)
@ -1321,7 +1322,7 @@ wchar_t *unescape(const wchar_t * orig, int flags)
in[out_pos]=L'\t'; in[out_pos]=L'\t';
break; break;
} }
/* /*
\v means vertical tab \v means vertical tab
*/ */
@ -1330,7 +1331,7 @@ wchar_t *unescape(const wchar_t * orig, int flags)
in[out_pos]=L'\v'; in[out_pos]=L'\v';
break; break;
} }
/* If a backslash is followed by an actual newline, swallow them both */ /* If a backslash is followed by an actual newline, swallow them both */
case L'\n': case L'\n':
out_pos--; out_pos--;
@ -1522,7 +1523,7 @@ wchar_t *unescape(const wchar_t * orig, int flags)
} }
} }
break; break;
default: default:
{ {
in[out_pos++] = L'\\'; in[out_pos++] = L'\\';
@ -1589,7 +1590,7 @@ wchar_t *unescape(const wchar_t * orig, int flags)
in[out_pos]=in[in_pos]; in[out_pos]=in[in_pos];
break; break;
} }
case '\n': case '\n':
{ {
out_pos--; out_pos--;

View file

@ -1765,7 +1765,7 @@ void complete(const wcstring &cmd, std::vector<completion_t> &comps, complete_ty
completer_t completer(cmd, type); completer_t completer(cmd, type);
const wchar_t *tok_begin, *tok_end, *cmdsubst_begin, *cmdsubst_end, *prev_begin, *prev_end; 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; wcstring current_command;
int on_command=0; int on_command=0;
size_t pos; size_t pos;
@ -1789,7 +1789,7 @@ void complete(const wcstring &cmd, std::vector<completion_t> &comps, complete_ty
/** /**
If we are completing a variable name or a tilde expansion user 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) if (!done)
@ -1806,7 +1806,7 @@ void complete(const wcstring &cmd, std::vector<completion_t> &comps, complete_ty
int had_cmd=0; int had_cmd=0;
int end_loop=0; int end_loop=0;
tokenizer_t tok(buff.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS); tokenizer_t tok(buff.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS);
while (tok_has_next(&tok) && !end_loop) while (tok_has_next(&tok) && !end_loop)
{ {
@ -1897,7 +1897,14 @@ void complete(const wcstring &cmd, std::vector<completion_t> &comps, complete_ty
current_token = wcsndup(tok_begin, cursor_pos-(tok_begin-cmd_cstr)); 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 ); // 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<completion_t> &comps, complete_ty
subcommand. subcommand.
*/ */
if ((on_command || (wcscmp(current_token, L"--") == 0)) && if ((on_command || current_token == L"--") &&
(current_token[0] == L'-') && string_prefixes_string(L"-", current_token) &&
!(use_command && use_function && use_builtin)) !(use_command && use_function && use_builtin))
{ {
if (use_command == 0) if (use_command == 0)
@ -1929,63 +1936,44 @@ void complete(const wcstring &cmd, std::vector<completion_t> &comps, complete_ty
on_command=1; 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);
} }
else
if (!prev_token)
{ {
prev_token = wcsdup(L""); int do_file=0;
}
if (current_token && prev_token) wcstring current_command_unescape = current_command;
{ wcstring prev_token_unescape = prev_token;
if (on_command) 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 */ do_file = completer.complete_param(current_command_unescape,
completer.complete_cmd(current_token, use_function, use_builtin, use_command); 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; If we have found no command specific completions at
wcstring current_token_unescape = current_token; 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) && This function wants the unescaped string
unescape_string(current_token_unescape, UNESCAPE_INCOMPLETE)) */
{ completer.complete_param_expand(current_token, do_file);
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);
}
} }
} }
free((void *)current_token);
free((void *)prev_token);
comps = completer.get_completions(); comps = completer.get_completions();
completer.get_commands_to_load(commands_to_load); completer.get_commands_to_load(commands_to_load);
} }

View file

@ -326,9 +326,10 @@ static void test_tok()
}; };
say(L"Test correct tokenization"); say(L"Test correct tokenization");
tokenizer_t t(str, 0); 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)) if (types[i] != tok_last_type(&t))
{ {
err(L"Tokenization error:"); err(L"Tokenization error:");

View file

@ -1187,7 +1187,7 @@ struct autosuggestion_context_t
if (! cursor_at_end && iswspace(last_char)) if (! cursor_at_end && iswspace(last_char))
return 0; 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) if (wcschr(L"'\"", last_char) && cursor_at_end)
return 0; return 0;

View file

@ -227,7 +227,8 @@ static void read_string(tokenizer_t *tok)
start = tok->buff; start = tok->buff;
bool is_first = true; bool is_first = true;
enum tok_mode_t { enum tok_mode_t
{
mode_regular_text = 0, // regular text mode_regular_text = 0, // regular text
mode_subshell = 1, // inside of subshell mode_subshell = 1, // inside of subshell
mode_array_brackets = 2, // inside of array brackets mode_array_brackets = 2, // inside of array brackets
@ -260,7 +261,7 @@ static void read_string(tokenizer_t *tok)
tok->buff++; tok->buff++;
continue; continue;
} }
switch (mode) switch (mode)
{ {
case mode_regular_text: case mode_regular_text:
@ -358,7 +359,7 @@ static void read_string(tokenizer_t *tok)
break; break;
} }
break; break;
case mode_array_brackets: case mode_array_brackets:
switch (*tok->buff) switch (*tok->buff)
{ {
@ -410,7 +411,7 @@ static void read_comment(tokenizer_t *tok)
start = tok->buff; start = tok->buff;
while (*(tok->buff)!= L'\n' && *(tok->buff)!= L'\0') while (*(tok->buff)!= L'\n' && *(tok->buff)!= L'\0')
tok->buff++; tok->buff++;
size_t len = tok->buff - start; size_t len = tok->buff - start;
tok->last_token.assign(start, len); tok->last_token.assign(start, len);