From 26678682ca90b68f19b993e259bf43384c0a18ec Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 19 Nov 2012 00:31:03 -0800 Subject: [PATCH] Fix indentation of switch statements --- builtin.cpp | 1694 ++++++++++++++-------------- builtin_commandline.cpp | 276 ++--- builtin_complete.cpp | 154 +-- builtin_jobs.cpp | 188 ++-- builtin_set.cpp | 98 +- builtin_test.cpp | 198 ++-- builtin_ulimit.cpp | 220 ++-- color.cpp | 110 +- common.cpp | 1176 +++++++++---------- complete.cpp | 130 +-- env.cpp | 26 +- env_universal_common.cpp | 190 ++-- event.cpp | 94 +- exec.cpp | 1064 +++++++++--------- expand.cpp | 318 +++--- fallback.cpp | 568 +++++----- fish.cpp | 140 +-- fish_indent.cpp | 330 +++--- fish_pager.cpp | 284 ++--- fishd.cpp | 80 +- highlight.cpp | 1298 ++++++++++----------- history.cpp | 84 +- input.cpp | 16 +- input_common.cpp | 80 +- io.cpp | 30 +- mimedb.cpp | 476 ++++---- output.cpp | 8 +- parse_util.cpp | 146 +-- parser.cpp | 2314 +++++++++++++++++++------------------- path.cpp | 24 +- postfork.cpp | 352 +++--- proc.cpp | 78 +- reader.cpp | 1139 +++++++++---------- screen.cpp | 102 +- set_color.cpp | 64 +- tokenizer.cpp | 368 +++--- wildcard.cpp | 16 +- wutil.cpp | 30 +- xdgmimeglob.cpp | 18 +- xdgmimemagic.cpp | 48 +- 40 files changed, 7015 insertions(+), 7014 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 4ca4e30f9..6229eaeb6 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -476,23 +476,23 @@ static int builtin_bind_add(wchar_t *seq, wchar_t *cmd, int terminfo) switch (errno) { - case ENOENT: - { - append_format(stderr_buffer, _(L"%ls: No key with name '%ls' found\n"), L"bind", seq); - break; - } + case ENOENT: + { + append_format(stderr_buffer, _(L"%ls: No key with name '%ls' found\n"), L"bind", seq); + break; + } - case EILSEQ: - { - append_format(stderr_buffer, _(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", seq); - break; - } + case EILSEQ: + { + append_format(stderr_buffer, _(L"%ls: Key with name '%ls' does not have any mapping\n"), L"bind", seq); + break; + } - default: - { - append_format(stderr_buffer, _(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", seq); - break; - } + default: + { + append_format(stderr_buffer, _(L"%ls: Unknown error trying to bind to key named '%ls'\n"), L"bind", seq); + break; + } } @@ -612,45 +612,45 @@ static int builtin_bind(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return STATUS_BUILTIN_ERROR; + + case 'a': + all = 1; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - - case 'a': - all = 1; - break; - - case 'e': - mode = BIND_ERASE; - break; + case 'e': + mode = BIND_ERASE; + break; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; - case 'k': - use_terminfo = 1; - break; + case 'k': + use_terminfo = 1; + break; - case 'K': - mode = BIND_KEY_NAMES; - break; + case 'K': + mode = BIND_KEY_NAMES; + break; - case 'f': - mode = BIND_FUNCTION_NAMES; - break; + case 'f': + mode = BIND_FUNCTION_NAMES; + break; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -659,58 +659,58 @@ static int builtin_bind(parser_t &parser, wchar_t **argv) switch (mode) { - case BIND_ERASE: - { - builtin_bind_erase(&argv[woptind], all); - break; - } - - case BIND_INSERT: - { - switch (argc-woptind) + case BIND_ERASE: { - case 0: - { - builtin_bind_list(); + builtin_bind_erase(&argv[woptind], all); break; } - case 2: + case BIND_INSERT: { - builtin_bind_add(argv[woptind], argv[woptind+1], use_terminfo); + switch (argc-woptind) + { + case 0: + { + builtin_bind_list(); + break; + } + + case 2: + { + builtin_bind_add(argv[woptind], argv[woptind+1], use_terminfo); + break; + } + + default: + { + res = STATUS_BUILTIN_ERROR; + append_format(stderr_buffer, _(L"%ls: Expected zero or two parameters, got %d"), argv[0], argc-woptind); + break; + } + } break; } + case BIND_KEY_NAMES: + { + builtin_bind_key_names(all); + break; + } + + + case BIND_FUNCTION_NAMES: + { + builtin_bind_function_names(); + break; + } + + default: { res = STATUS_BUILTIN_ERROR; - append_format(stderr_buffer, _(L"%ls: Expected zero or two parameters, got %d"), argv[0], argc-woptind); + append_format(stderr_buffer, _(L"%ls: Invalid state\n"), argv[0]); break; } - } - break; - } - - case BIND_KEY_NAMES: - { - builtin_bind_key_names(all); - break; - } - - - case BIND_FUNCTION_NAMES: - { - builtin_bind_function_names(); - break; - } - - - default: - { - res = STATUS_BUILTIN_ERROR; - append_format(stderr_buffer, _(L"%ls: Invalid state\n"), argv[0]); - break; - } } return res; @@ -775,35 +775,35 @@ static int builtin_block(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return STATUS_BUILTIN_ERROR; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + case 'g': + scope = GLOBAL; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; + case 'l': + scope = LOCAL; + break; - case 'g': - scope = GLOBAL; - break; + case 'e': + erase = 1; + break; - case 'l': - scope = LOCAL; - break; - - case 'e': - erase = 1; - break; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -833,23 +833,23 @@ static int builtin_block(parser_t &parser, wchar_t **argv) switch (scope) { - case LOCAL: - { - if (!block->outer) + case LOCAL: + { + if (!block->outer) + block=0; + break; + } + case GLOBAL: + { block=0; - break; - } - case GLOBAL: - { - block=0; - } - case UNSET: - { - while (block && - block->type() != FUNCTION_CALL && - block->type() != FUNCTION_CALL_NO_SHADOW) - block = block->outer; - } + } + case UNSET: + { + while (block && + block->type() != FUNCTION_CALL && + block->type() != FUNCTION_CALL_NO_SHADOW) + block = block->outer; + } } if (block) { @@ -909,28 +909,28 @@ static int builtin_builtin(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + + return STATUS_BUILTIN_ERROR; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + case 'n': + list=1; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - - return STATUS_BUILTIN_ERROR; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - - case 'n': - list=1; - break; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -988,23 +988,23 @@ static int builtin_emit(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) - break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -1059,23 +1059,23 @@ static int builtin_generic(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) - break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -1129,40 +1129,40 @@ static void functions_def(const wcstring &name, wcstring &out) event_t *next = ev.at(i); switch (next->type) { - case EVENT_SIGNAL: - { - append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal)); - break; - } + case EVENT_SIGNAL: + { + append_format(out, L" --on-signal %ls", sig2wcs(next->param1.signal)); + break; + } - case EVENT_VARIABLE: - { - append_format(out, L" --on-variable %ls", next->str_param1.c_str()); - break; - } + case EVENT_VARIABLE: + { + append_format(out, L" --on-variable %ls", next->str_param1.c_str()); + break; + } - case EVENT_EXIT: - { - if (next->param1.pid > 0) - append_format(out, L" --on-process-exit %d", next->param1.pid); - else - append_format(out, L" --on-job-exit %d", -next->param1.pid); - break; - } + case EVENT_EXIT: + { + if (next->param1.pid > 0) + append_format(out, L" --on-process-exit %d", next->param1.pid); + else + append_format(out, L" --on-job-exit %d", -next->param1.pid); + break; + } - case EVENT_JOB_ID: - { - const job_t *j = job_get(next->param1.job_id); - if (j) - append_format(out, L" --on-job-exit %d", j->pgid); - break; - } + case EVENT_JOB_ID: + { + const job_t *j = job_get(next->param1.job_id); + if (j) + append_format(out, L" --on-job-exit %d", j->pgid); + break; + } - case EVENT_GENERIC: - { - append_format(out, L" --on-event %ls", next->str_param1.c_str()); - break; - } + case EVENT_GENERIC: + { + append_format(out, L" --on-event %ls", next->str_param1.c_str()); + break; + } } @@ -1267,49 +1267,49 @@ static int builtin_functions(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + + return STATUS_BUILTIN_ERROR; + + case 'e': + erase=1; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); + case 'd': + desc=woptarg; + break; - return STATUS_BUILTIN_ERROR; + case 'n': + list=1; + break; - case 'e': - erase=1; - break; + case 'a': + show_hidden=1; + break; - case 'd': - desc=woptarg; - break; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; - case 'n': - list=1; - break; + case 'q': + query = 1; + break; - case 'a': - show_hidden=1; - break; + case 'c': + copy = 1; + break; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - - case 'q': - query = 1; - break; - - case 'c': - copy = 1; - break; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -1476,48 +1476,48 @@ static unsigned int builtin_echo_digit(wchar_t wc, unsigned int base) assert(base == 8 || base == 16); switch (wc) { - case L'0': - return 0; - case L'1': - return 1; - case L'2': - return 2; - case L'3': - return 3; - case L'4': - return 4; - case L'5': - return 5; - case L'6': - return 6; - case L'7': - return 7; + case L'0': + return 0; + case L'1': + return 1; + case L'2': + return 2; + case L'3': + return 3; + case L'4': + return 4; + case L'5': + return 5; + case L'6': + return 6; + case L'7': + return 7; } if (base == 16) switch (wc) { - case L'8': - return 8; - case L'9': - return 9; - case L'a': - case L'A': - return 10; - case L'b': - case L'B': - return 11; - case L'c': - case L'C': - return 12; - case L'd': - case L'D': - return 13; - case L'e': - case L'E': - return 14; - case L'f': - case L'F': - return 15; + case L'8': + return 8; + case L'9': + return 9; + case L'a': + case L'A': + return 10; + case L'b': + case L'B': + return 11; + case L'c': + case L'C': + return 12; + case L'd': + case L'D': + return 13; + case L'e': + case L'E': + return 14; + case L'f': + case L'F': + return 15; } return UINT_MAX; } @@ -1639,56 +1639,56 @@ static int builtin_echo(parser_t &parser, wchar_t **argv) size_t consumed = 1; switch (str[j+1]) { - case L'a': - wc = L'\a'; - break; - case L'b': - wc = L'\b'; - break; - case L'e': - wc = L'\e'; - break; - case L'f': - wc = L'\f'; - break; - case L'n': - wc = L'\n'; - break; - case L'r': - wc = L'\r'; - break; - case L't': - wc = L'\t'; - break; - case L'v': - wc = L'\v'; - break; - case L'\\': - wc = L'\\'; - break; - - case L'c': - wc = 0; - continue_output = false; - break; - - default: - { - /* Octal and hex escape sequences */ - unsigned char narrow_val = 0; - if (builtin_echo_parse_numeric_sequence(str + j + 1, &consumed, &narrow_val)) - { - /* Here consumed must have been set to something */ - wc = narrow_val; //is this OK for conversion? - } - else - { - /* Not a recognized escape. We consume only the backslash. */ + case L'a': + wc = L'\a'; + break; + case L'b': + wc = L'\b'; + break; + case L'e': + wc = L'\e'; + break; + case L'f': + wc = L'\f'; + break; + case L'n': + wc = L'\n'; + break; + case L'r': + wc = L'\r'; + break; + case L't': + wc = L'\t'; + break; + case L'v': + wc = L'\v'; + break; + case L'\\': wc = L'\\'; - consumed = 0; + break; + + case L'c': + wc = 0; + continue_output = false; + break; + + default: + { + /* Octal and hex escape sequences */ + unsigned char narrow_val = 0; + if (builtin_echo_parse_numeric_sequence(str + j + 1, &consumed, &narrow_val)) + { + /* Here consumed must have been set to something */ + wc = narrow_val; //is this OK for conversion? + } + else + { + /* Not a recognized escape. We consume only the backslash. */ + wc = L'\\'; + consumed = 0; + } + break; } - break; - } } /* Skip over characters that were part of this escape sequence (but not the backslash, which will be handled by the loop increment */ @@ -1801,152 +1801,152 @@ static int builtin_function(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) - break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - - res = 1; - break; - - case 'd': - desc=woptarg; - break; - - case 's': - { - int sig = wcs2sig(woptarg); - - if (sig < 0) - { + case 0: + if (long_options[opt_index].flag != 0) + break; append_format(stderr_buffer, - _(L"%ls: Unknown signal '%ls'\n"), + BUILTIN_ERR_UNKNOWN, argv[0], - woptarg); - res=1; + long_options[opt_index].name); + + res = 1; break; - } - events.push_back(event_t::signal_event(sig)); - break; - } - case 'v': - { - if (wcsvarname(woptarg)) - { - append_format(stderr_buffer, - _(L"%ls: Invalid variable name '%ls'\n"), - argv[0], - woptarg); - res=STATUS_BUILTIN_ERROR; + case 'd': + desc=woptarg; break; - } - events.push_back(event_t::variable_event(woptarg)); - break; - } - - - case 'e': - { - events.push_back(event_t::generic_event(woptarg)); - break; - } - - case 'j': - case 'p': - { - pid_t pid; - wchar_t *end; - event_t e(EVENT_ANY); - - if ((opt == 'j') && - (wcscasecmp(woptarg, L"caller") == 0)) + case 's': { - int job_id = -1; + int sig = wcs2sig(woptarg); - if (is_subshell) - { - block_t *b = parser.current_block; - - while (b && (b->type() != SUBST)) - b = b->outer; - - if (b) - { - b=b->outer; - } - if (b->job) - { - job_id = b->job->job_id; - } - } - - if (job_id == -1) + if (sig < 0) { append_format(stderr_buffer, - _(L"%ls: Cannot find calling job for event handler\n"), - argv[0]); - res=1; - } - else - { - e.type = EVENT_JOB_ID; - e.param1.job_id = job_id; - } - - } - else - { - errno = 0; - pid = fish_wcstoi(woptarg, &end, 10); - if (errno || !end || *end) - { - append_format(stderr_buffer, - _(L"%ls: Invalid process id %ls\n"), + _(L"%ls: Unknown signal '%ls'\n"), argv[0], woptarg); res=1; break; } - - - e.type = EVENT_EXIT; - e.param1.pid = (opt=='j'?-1:1)*abs(pid); + events.push_back(event_t::signal_event(sig)); + break; } - if (res) + + case 'v': { - /* nothing */ + if (wcsvarname(woptarg)) + { + append_format(stderr_buffer, + _(L"%ls: Invalid variable name '%ls'\n"), + argv[0], + woptarg); + res=STATUS_BUILTIN_ERROR; + break; + } + + events.push_back(event_t::variable_event(woptarg)); + break; } - else + + + case 'e': { - events.push_back(e); + events.push_back(event_t::generic_event(woptarg)); + break; } - break; - } - case 'a': - if (named_arguments.get() == NULL) - named_arguments.reset(new wcstring_list_t); - break; + case 'j': + case 'p': + { + pid_t pid; + wchar_t *end; + event_t e(EVENT_ANY); - case 'S': - shadows = 0; - break; + if ((opt == 'j') && + (wcscasecmp(woptarg, L"caller") == 0)) + { + int job_id = -1; - case 'h': - parser.pop_block(); - parser.push_block(new fake_block_t()); - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; + if (is_subshell) + { + block_t *b = parser.current_block; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - res = 1; - break; + while (b && (b->type() != SUBST)) + b = b->outer; + + if (b) + { + b=b->outer; + } + if (b->job) + { + job_id = b->job->job_id; + } + } + + if (job_id == -1) + { + append_format(stderr_buffer, + _(L"%ls: Cannot find calling job for event handler\n"), + argv[0]); + res=1; + } + else + { + e.type = EVENT_JOB_ID; + e.param1.job_id = job_id; + } + + } + else + { + errno = 0; + pid = fish_wcstoi(woptarg, &end, 10); + if (errno || !end || *end) + { + append_format(stderr_buffer, + _(L"%ls: Invalid process id %ls\n"), + argv[0], + woptarg); + res=1; + break; + } + + + e.type = EVENT_EXIT; + e.param1.pid = (opt=='j'?-1:1)*abs(pid); + } + if (res) + { + /* nothing */ + } + else + { + events.push_back(e); + } + break; + } + + case 'a': + if (named_arguments.get() == NULL) + named_arguments.reset(new wcstring_list_t); + break; + + case 'S': + shadows = 0; + break; + + case 'h': + parser.pop_block(); + parser.push_block(new fake_block_t()); + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + res = 1; + break; } @@ -2111,24 +2111,24 @@ static int builtin_random(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return STATUS_BUILTIN_ERROR; + + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - break; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -2137,51 +2137,51 @@ static int builtin_random(parser_t &parser, wchar_t **argv) switch (argc-woptind) { - case 0: - { - long res; - - if (!seeded) + case 0: { - seeded=1; - srand48_r(time(0), &seed_buffer); + long res; + + if (!seeded) + { + seeded=1; + srand48_r(time(0), &seed_buffer); + } + lrand48_r(&seed_buffer, &res); + + append_format(stdout_buffer, L"%ld\n", labs(res%32767)); + break; } - lrand48_r(&seed_buffer, &res); - append_format(stdout_buffer, L"%ld\n", labs(res%32767)); - break; - } + case 1: + { + long foo; + wchar_t *end=0; - case 1: - { - long foo; - wchar_t *end=0; + errno=0; + foo = wcstol(argv[woptind], &end, 10); + if (errno || *end) + { + append_format(stderr_buffer, + _(L"%ls: Seed value '%ls' is not a valid number\n"), + argv[0], + argv[woptind]); - errno=0; - foo = wcstol(argv[woptind], &end, 10); - if (errno || *end) + return STATUS_BUILTIN_ERROR; + } + seeded=1; + srand48_r(foo, &seed_buffer); + break; + } + + default: { append_format(stderr_buffer, - _(L"%ls: Seed value '%ls' is not a valid number\n"), + _(L"%ls: Expected zero or one argument, got %d\n"), argv[0], - argv[woptind]); - + argc-woptind); + builtin_print_help(parser, argv[0], stderr_buffer); return STATUS_BUILTIN_ERROR; } - seeded=1; - srand48_r(foo, &seed_buffer); - break; - } - - default: - { - append_format(stderr_buffer, - _(L"%ls: Expected zero or one argument, got %d\n"), - argv[0], - argc-woptind); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - } } return STATUS_BUILTIN_OK; } @@ -2267,60 +2267,60 @@ static int builtin_read(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return STATUS_BUILTIN_ERROR; + + case L'x': + place |= ENV_EXPORT; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; + case L'g': + place |= ENV_GLOBAL; + break; - case L'x': - place |= ENV_EXPORT; - break; + case L'l': + place |= ENV_LOCAL; + break; - case L'g': - place |= ENV_GLOBAL; - break; + case L'U': + place |= ENV_UNIVERSAL; + break; - case L'l': - place |= ENV_LOCAL; - break; + case L'u': + place |= ENV_UNEXPORT; + break; - case L'U': - place |= ENV_UNIVERSAL; - break; + case L'p': + prompt = woptarg; + break; - case L'u': - place |= ENV_UNEXPORT; - break; + case L'c': + commandline = woptarg; + break; - case L'p': - prompt = woptarg; - break; + case L'm': + mode_name = woptarg; + break; - case L'c': - commandline = woptarg; - break; + case 's': + shell = 1; + break; - case L'm': - mode_name = woptarg; - break; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; - case 's': - shell = 1; - break; - - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - - case L'?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case L'?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } } @@ -2437,20 +2437,20 @@ static int builtin_read(parser_t &parser, wchar_t **argv) switch (sz) { - case (size_t)(-1): - memset(&state, '\0', sizeof(state)); - break; + case (size_t)(-1): + memset(&state, '\0', sizeof(state)); + break; - case (size_t)(-2): - break; - case 0: - eof=1; - finished = 1; - break; + case (size_t)(-2): + break; + case 0: + eof=1; + finished = 1; + break; - default: - finished=1; - break; + default: + finished=1; + break; } } @@ -2600,73 +2600,73 @@ static int builtin_status(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) - break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - - case 'c': - mode = IS_SUBST; - break; - - case 'b': - mode = IS_BLOCK; - break; - - case 'i': - mode = IS_INTERACTIVE; - break; - - case 'l': - mode = IS_LOGIN; - break; - - case 'f': - mode = CURRENT_FILENAME; - break; - - case 'n': - mode = CURRENT_LINE_NUMBER; - break; - - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - - case 'j': - if (wcscmp(woptarg, L"full") == 0) - job_control_mode = JOB_CONTROL_ALL; - else if (wcscmp(woptarg, L"interactive") == 0) - job_control_mode = JOB_CONTROL_INTERACTIVE; - else if (wcscmp(woptarg, L"none") == 0) - job_control_mode = JOB_CONTROL_NONE; - else - { + case 0: + if (long_options[opt_index].flag != 0) + break; append_format(stderr_buffer, - L"%ls: Invalid job control mode '%ls'\n", - woptarg); - res = 1; - } - mode = DONE; - break; + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; - case 't': - mode = STACK_TRACE; - break; + case 'c': + mode = IS_SUBST; + break; + + case 'b': + mode = IS_BLOCK; + break; + + case 'i': + mode = IS_INTERACTIVE; + break; + + case 'l': + mode = IS_LOGIN; + break; + + case 'f': + mode = CURRENT_FILENAME; + break; + + case 'n': + mode = CURRENT_LINE_NUMBER; + break; + + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + case 'j': + if (wcscmp(woptarg, L"full") == 0) + job_control_mode = JOB_CONTROL_ALL; + else if (wcscmp(woptarg, L"interactive") == 0) + job_control_mode = JOB_CONTROL_INTERACTIVE; + else if (wcscmp(woptarg, L"none") == 0) + job_control_mode = JOB_CONTROL_NONE; + else + { + append_format(stderr_buffer, + L"%ls: Invalid job control mode '%ls'\n", + woptarg); + res = 1; + } + mode = DONE; + break; + + case 't': + mode = STACK_TRACE; + break; - case ':': - builtin_missing_argument(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case ':': + builtin_missing_argument(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } @@ -2677,65 +2677,65 @@ static int builtin_status(parser_t &parser, wchar_t **argv) switch (mode) { - case CURRENT_FILENAME: - { - const wchar_t *fn = parser.current_filename(); + case CURRENT_FILENAME: + { + const wchar_t *fn = parser.current_filename(); - if (!fn) - fn = _(L"Standard input"); + if (!fn) + fn = _(L"Standard input"); - append_format(stdout_buffer, L"%ls\n", fn); + append_format(stdout_buffer, L"%ls\n", fn); - break; - } + break; + } - case CURRENT_LINE_NUMBER: - { - append_format(stdout_buffer, L"%d\n", parser.get_lineno()); - break; - } + case CURRENT_LINE_NUMBER: + { + append_format(stdout_buffer, L"%d\n", parser.get_lineno()); + break; + } - case IS_INTERACTIVE: - return !is_interactive_session; + case IS_INTERACTIVE: + return !is_interactive_session; - case IS_SUBST: - return !is_subshell; + case IS_SUBST: + return !is_subshell; - case IS_BLOCK: - return !is_block; + case IS_BLOCK: + return !is_block; - case IS_LOGIN: - return !is_login; + case IS_LOGIN: + return !is_login; - case IS_FULL_JOB_CONTROL: - return job_control_mode != JOB_CONTROL_ALL; + case IS_FULL_JOB_CONTROL: + return job_control_mode != JOB_CONTROL_ALL; - case IS_INTERACTIVE_JOB_CONTROL: - return job_control_mode != JOB_CONTROL_INTERACTIVE; + case IS_INTERACTIVE_JOB_CONTROL: + return job_control_mode != JOB_CONTROL_INTERACTIVE; - case IS_NO_JOB_CONTROL: - return job_control_mode != JOB_CONTROL_NONE; + case IS_NO_JOB_CONTROL: + return job_control_mode != JOB_CONTROL_NONE; - case STACK_TRACE: - { - parser.stack_trace(parser.current_block, stdout_buffer); - break; - } + case STACK_TRACE: + { + parser.stack_trace(parser.current_block, stdout_buffer); + break; + } - case NORMAL: - { - if (is_login) - append_format(stdout_buffer, _(L"This is a login shell\n")); - else - append_format(stdout_buffer, _(L"This is not a login shell\n")); + case NORMAL: + { + if (is_login) + append_format(stdout_buffer, _(L"This is a login shell\n")); + else + append_format(stdout_buffer, _(L"This is not a login shell\n")); - append_format(stdout_buffer, _(L"Job control: %ls\n"), - job_control_mode==JOB_CONTROL_INTERACTIVE?_(L"Only on interactive jobs"): - (job_control_mode==JOB_CONTROL_NONE ? _(L"Never") : _(L"Always"))); + append_format(stdout_buffer, _(L"Job control: %ls\n"), + job_control_mode==JOB_CONTROL_INTERACTIVE?_(L"Only on interactive jobs"): + (job_control_mode==JOB_CONTROL_NONE ? _(L"Never") : _(L"Always"))); - parser.stack_trace(parser.current_block, stdout_buffer); - break; - } + parser.stack_trace(parser.current_block, stdout_buffer); + break; + } } } @@ -2753,38 +2753,38 @@ static int builtin_exit(parser_t &parser, wchar_t **argv) long ec=0; switch (argc) { - case 1: - { - ec = proc_get_last_status(); - break; - } + case 1: + { + ec = proc_get_last_status(); + break; + } - case 2: - { - wchar_t *end; - errno = 0; - ec = wcstol(argv[1],&end,10); - if (errno || *end != 0) + case 2: + { + wchar_t *end; + errno = 0; + ec = wcstol(argv[1],&end,10); + if (errno || *end != 0) + { + append_format(stderr_buffer, + _(L"%ls: Argument '%ls' must be an integer\n"), + argv[0], + argv[1]); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; + } + break; + } + + default: { append_format(stderr_buffer, - _(L"%ls: Argument '%ls' must be an integer\n"), - argv[0], - argv[1]); + BUILTIN_ERR_TOO_MANY_ARGUMENTS, + argv[0]); + builtin_print_help(parser, argv[0], stderr_buffer); return STATUS_BUILTIN_ERROR; } - break; - } - - default: - { - append_format(stderr_buffer, - BUILTIN_ERR_TOO_MANY_ARGUMENTS, - argv[0]); - - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - } } reader_exit(1, 0); @@ -2961,34 +2961,34 @@ static int builtin_contains(parser_t &parser, wchar_t ** argv) switch (opt) { - case 0: - assert(opt_index >= 0 && (size_t)opt_index < sizeof long_options / sizeof *long_options); - if (long_options[opt_index].flag != 0) + case 0: + assert(opt_index >= 0 && (size_t)opt_index < sizeof long_options / sizeof *long_options); + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; + + + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + + + case ':': + builtin_missing_argument(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; + + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; + + case 'i': + index=1; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; - - - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - - - case ':': - builtin_missing_argument(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; - - case 'i': - index=1; - break; } } @@ -3477,98 +3477,98 @@ static int builtin_end(parser_t &parser, wchar_t **argv) switch (parser.current_block->type()) { - case WHILE: - { - /* - If this is a while loop, we rewind the loop unless - it's the last lap, in which case we continue. - */ - if (!(parser.current_block->skip && (parser.current_block->loop_status != LOOP_CONTINUE))) + case WHILE: { - parser.current_block->loop_status = LOOP_NORMAL; - parser.current_block->skip = 0; - kill_block = 0; - parser.set_pos(parser.current_block->tok_pos); - while_block_t *blk = static_cast(parser.current_block); - blk->status = WHILE_TEST_AGAIN; + /* + If this is a while loop, we rewind the loop unless + it's the last lap, in which case we continue. + */ + if (!(parser.current_block->skip && (parser.current_block->loop_status != LOOP_CONTINUE))) + { + parser.current_block->loop_status = LOOP_NORMAL; + parser.current_block->skip = 0; + kill_block = 0; + parser.set_pos(parser.current_block->tok_pos); + while_block_t *blk = static_cast(parser.current_block); + blk->status = WHILE_TEST_AGAIN; + } + + break; } - break; - } - - case IF: - case SUBST: - case BEGIN: - case SWITCH: - case FAKE: - /* - Nothing special happens at the end of these commands. The scope just ends. - */ - - break; - - case FOR: - { - /* - set loop variable to next element, and rewind to the beginning of the block. - */ - for_block_t *fb = static_cast(parser.current_block); - wcstring_list_t &for_vars = fb->sequence; - if (parser.current_block->loop_status == LOOP_BREAK) - { - for_vars.clear(); - } - - if (! for_vars.empty()) - { - const wcstring val = for_vars.back(); - for_vars.pop_back(); - const wcstring &for_variable = fb->variable; - env_set(for_variable.c_str(), val.c_str(), ENV_LOCAL); - parser.current_block->loop_status = LOOP_NORMAL; - parser.current_block->skip = 0; - - kill_block = 0; - parser.set_pos(parser.current_block->tok_pos); - } - break; - } - - case FUNCTION_DEF: - { - function_def_block_t *fdb = static_cast(parser.current_block); - function_data_t &d = fdb->function_data; - - if (d.name.empty()) - { - /* Disallow empty function names */ - append_format(stderr_buffer, _(L"%ls: No function name given\n"), argv[0]); - - /* Return an error via a crummy way. Don't just return here, since we need to pop the block. */ - proc_set_last_status(STATUS_BUILTIN_ERROR); - } - else - { - /** - Copy the text from the beginning of the function - until the end command and use as the new definition - for the specified function + case IF: + case SUBST: + case BEGIN: + case SWITCH: + case FAKE: + /* + Nothing special happens at the end of these commands. The scope just ends. */ - wchar_t *def = wcsndup(parser.get_buffer()+parser.current_block->tok_pos, - parser.get_job_pos()-parser.current_block->tok_pos); - d.definition = def; + break; - function_add(d, parser); - free(def); + case FOR: + { + /* + set loop variable to next element, and rewind to the beginning of the block. + */ + for_block_t *fb = static_cast(parser.current_block); + wcstring_list_t &for_vars = fb->sequence; + if (parser.current_block->loop_status == LOOP_BREAK) + { + for_vars.clear(); + } + + if (! for_vars.empty()) + { + const wcstring val = for_vars.back(); + for_vars.pop_back(); + const wcstring &for_variable = fb->variable; + env_set(for_variable.c_str(), val.c_str(), ENV_LOCAL); + parser.current_block->loop_status = LOOP_NORMAL; + parser.current_block->skip = 0; + + kill_block = 0; + parser.set_pos(parser.current_block->tok_pos); + } + break; } - } - break; - default: - assert(false); //should never get here + case FUNCTION_DEF: + { + function_def_block_t *fdb = static_cast(parser.current_block); + function_data_t &d = fdb->function_data; + + if (d.name.empty()) + { + /* Disallow empty function names */ + append_format(stderr_buffer, _(L"%ls: No function name given\n"), argv[0]); + + /* Return an error via a crummy way. Don't just return here, since we need to pop the block. */ + proc_set_last_status(STATUS_BUILTIN_ERROR); + } + else + { + /** + Copy the text from the beginning of the function + until the end command and use as the new definition + for the specified function + */ + + wchar_t *def = wcsndup(parser.get_buffer()+parser.current_block->tok_pos, + parser.get_job_pos()-parser.current_block->tok_pos); + d.definition = def; + + function_add(d, parser); + free(def); + } + } break; + default: + assert(false); //should never get here + break; + } if (kill_block) { @@ -3703,30 +3703,30 @@ static int builtin_return(parser_t &parser, wchar_t **argv) switch (argc) { - case 1: - break; - case 2: - { - wchar_t *end; - errno = 0; - status = fish_wcstoi(argv[1],&end,10); - if (errno || *end != 0) + case 1: + break; + case 2: { + wchar_t *end; + errno = 0; + status = fish_wcstoi(argv[1],&end,10); + if (errno || *end != 0) + { + append_format(stderr_buffer, + _(L"%ls: Argument '%ls' must be an integer\n"), + argv[0], + argv[1]); + builtin_print_help(parser, argv[0], stderr_buffer); + return STATUS_BUILTIN_ERROR; + } + break; + } + default: append_format(stderr_buffer, - _(L"%ls: Argument '%ls' must be an integer\n"), - argv[0], - argv[1]); + _(L"%ls: Too many arguments\n"), + argv[0]); builtin_print_help(parser, argv[0], stderr_buffer); return STATUS_BUILTIN_ERROR; - } - break; - } - default: - append_format(stderr_buffer, - _(L"%ls: Too many arguments\n"), - argv[0]); - builtin_print_help(parser, argv[0], stderr_buffer); - return STATUS_BUILTIN_ERROR; } @@ -3874,37 +3874,37 @@ static int builtin_history(parser_t &parser, wchar_t **argv) { switch (opt) { - case 'p': - search_prefix = true; - break; - case 'd': - delete_item = true; - break; - case 's': - search_history = true; - break; - case 'c': - break; - case 'v': - save_history = true; - break; - case 'l': - clear_history = true; - break; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return STATUS_BUILTIN_OK; - break; - case EOF: - /* Remainder are arguments */ - break; - case '?': - append_format(stderr_buffer, BUILTIN_ERR_UNKNOWN, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; - break; - default: - append_format(stderr_buffer, BUILTIN_ERR_UNKNOWN, argv[0], argv[woptind-1]); - return STATUS_BUILTIN_ERROR; + case 'p': + search_prefix = true; + break; + case 'd': + delete_item = true; + break; + case 's': + search_history = true; + break; + case 'c': + break; + case 'v': + save_history = true; + break; + case 'l': + clear_history = true; + break; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return STATUS_BUILTIN_OK; + break; + case EOF: + /* Remainder are arguments */ + break; + case '?': + append_format(stderr_buffer, BUILTIN_ERR_UNKNOWN, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; + break; + default: + append_format(stderr_buffer, BUILTIN_ERR_UNKNOWN, argv[0], argv[woptind-1]); + return STATUS_BUILTIN_ERROR; } } diff --git a/builtin_commandline.cpp b/builtin_commandline.cpp index eabffe236..cb3ee9e3e 100644 --- a/builtin_commandline.cpp +++ b/builtin_commandline.cpp @@ -102,29 +102,29 @@ static void replace_part(const wchar_t *begin, switch (append_mode) { - case REPLACE_MODE: - { + case REPLACE_MODE: + { - out.append(insert); - out_pos = wcslen(insert) + (begin-buff); - break; + out.append(insert); + out_pos = wcslen(insert) + (begin-buff); + break; - } - case APPEND_MODE: - { - out.append(begin, end-begin); - out.append(insert); - break; - } - case INSERT_MODE: - { - long cursor = get_cursor_pos() -(begin-buff); - out.append(begin, cursor); - out.append(insert); - out.append(begin+cursor, end-begin-cursor); - out_pos += wcslen(insert); - break; - } + } + case APPEND_MODE: + { + out.append(begin, end-begin); + out.append(insert); + break; + } + case INSERT_MODE: + { + long cursor = get_cursor_pos() -(begin-buff); + out.append(begin, cursor); + out.append(insert); + out.append(begin+cursor, end-begin-cursor); + out_pos += wcslen(insert); + break; + } } out.append(end); reader_set_buffer(out, out_pos); @@ -166,12 +166,12 @@ static void write_part(const wchar_t *begin, switch (tok_last_type(&tok)) { - case TOK_STRING: - { - out.append(escape_string(tok_last(&tok), UNESCAPE_INCOMPLETE)); - out.push_back(L'\n'); - break; - } + case TOK_STRING: + { + out.append(escape_string(tok_last(&tok), UNESCAPE_INCOMPLETE)); + out.push_back(L'\n'); + break; + } } } @@ -333,82 +333,82 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return 1; + + case L'a': + append_mode = APPEND_MODE; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return 1; - - case L'a': - append_mode = APPEND_MODE; - break; - - case L'b': - buffer_part = STRING_MODE; - break; + case L'b': + buffer_part = STRING_MODE; + break; - case L'i': - append_mode = INSERT_MODE; - break; + case L'i': + append_mode = INSERT_MODE; + break; - case L'r': - append_mode = REPLACE_MODE; - break; + case L'r': + append_mode = REPLACE_MODE; + break; - case 'c': - cut_at_cursor=1; - break; + case 'c': + cut_at_cursor=1; + break; - case 't': - buffer_part = TOKEN_MODE; - break; + case 't': + buffer_part = TOKEN_MODE; + break; - case 'j': - buffer_part = JOB_MODE; - break; + case 'j': + buffer_part = JOB_MODE; + break; - case 'p': - buffer_part = PROCESS_MODE; - break; + case 'p': + buffer_part = PROCESS_MODE; + break; - case 'f': - function_mode=1; - break; + case 'f': + function_mode=1; + break; - case 'o': - tokenize=1; - break; + case 'o': + tokenize=1; + break; - case 'I': - current_buffer = woptarg; - current_cursor_pos = wcslen(woptarg); - break; + case 'I': + current_buffer = woptarg; + current_cursor_pos = wcslen(woptarg); + break; - case 'C': - cursor_mode = 1; - break; + case 'C': + cursor_mode = 1; + break; - case 'L': - line_mode = 1; - break; + case 'L': + line_mode = 1; + break; - case 'S': - search_mode = 1; - break; + case 'S': + search_mode = 1; + break; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return 0; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return 0; - case L'?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return 1; + case L'?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return 1; } } @@ -574,72 +574,72 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) switch (buffer_part) { - case STRING_MODE: - { - begin = get_buffer(); - end = begin+wcslen(begin); - break; - } + case STRING_MODE: + { + begin = get_buffer(); + end = begin+wcslen(begin); + break; + } - case PROCESS_MODE: - { - parse_util_process_extent(get_buffer(), + case PROCESS_MODE: + { + parse_util_process_extent(get_buffer(), + get_cursor_pos(), + &begin, + &end); + break; + } + + case JOB_MODE: + { + parse_util_job_extent(get_buffer(), get_cursor_pos(), &begin, &end); - break; - } + break; + } - case JOB_MODE: - { - parse_util_job_extent(get_buffer(), - get_cursor_pos(), - &begin, - &end); - break; - } - - case TOKEN_MODE: - { - parse_util_token_extent(get_buffer(), - get_cursor_pos(), - &begin, - &end, - 0, 0); - break; - } + case TOKEN_MODE: + { + parse_util_token_extent(get_buffer(), + get_cursor_pos(), + &begin, + &end, + 0, 0); + break; + } } switch (argc-woptind) { - case 0: - { - write_part(begin, end, cut_at_cursor, tokenize); - break; - } - - case 1: - { - replace_part(begin, end, argv[woptind], append_mode); - break; - } - - default: - { - wcstring sb = argv[woptind]; - int i; - - for (i=woptind+1; ijob_id, j->pgid); + if (header) + { + /* + Print table header before first job + */ + stdout_buffer.append(_(L"Job\tGroup\t")); +#ifdef HAVE__PROC_SELF_STAT + stdout_buffer.append(_(L"CPU\t")); +#endif + stdout_buffer.append(_(L"State\tCommand\n")); + } + + append_format(stdout_buffer, L"%d\t%d\t", j->job_id, j->pgid); #ifdef HAVE__PROC_SELF_STAT - append_format(stdout_buffer, L"%d%%\t", cpu_use(j)); + append_format(stdout_buffer, L"%d%%\t", cpu_use(j)); #endif - stdout_buffer.append(job_is_stopped(j)?_(L"stopped"):_(L"running")); - stdout_buffer.append(L"\t"); - stdout_buffer.append(j->command_wcstr()); - stdout_buffer.append(L"\n"); - break; - } - - case JOBS_PRINT_GROUP: - { - if (header) - { - /* - Print table header before first job - */ - stdout_buffer.append(_(L"Group\n")); - } - append_format(stdout_buffer, L"%d\n", j->pgid); - break; - } - - case JOBS_PRINT_PID: - { - if (header) - { - /* - Print table header before first job - */ - stdout_buffer.append(_(L"Procces\n")); + stdout_buffer.append(job_is_stopped(j)?_(L"stopped"):_(L"running")); + stdout_buffer.append(L"\t"); + stdout_buffer.append(j->command_wcstr()); + stdout_buffer.append(L"\n"); + break; } - for (p=j->first_process; p; p=p->next) + case JOBS_PRINT_GROUP: { - append_format(stdout_buffer, L"%d\n", p->pid); - } - break; - } - - case JOBS_PRINT_COMMAND: - { - if (header) - { - /* - Print table header before first job - */ - stdout_buffer.append(_(L"Command\n")); + if (header) + { + /* + Print table header before first job + */ + stdout_buffer.append(_(L"Group\n")); + } + append_format(stdout_buffer, L"%d\n", j->pgid); + break; } - for (p=j->first_process; p; p=p->next) + case JOBS_PRINT_PID: { - append_format(stdout_buffer, L"%ls\n", p->argv0()); + if (header) + { + /* + Print table header before first job + */ + stdout_buffer.append(_(L"Procces\n")); + } + + for (p=j->first_process; p; p=p->next) + { + append_format(stdout_buffer, L"%d\n", p->pid); + } + break; + } + + case JOBS_PRINT_COMMAND: + { + if (header) + { + /* + Print table header before first job + */ + stdout_buffer.append(_(L"Command\n")); + } + + for (p=j->first_process; p; p=p->next) + { + append_format(stdout_buffer, L"%ls\n", p->argv0()); + } + break; } - break; - } } } @@ -212,45 +212,45 @@ static int builtin_jobs(parser_t &parser, wchar_t **argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + + builtin_print_help(parser, argv[0], stderr_buffer); + + + return 1; + + + case 'p': + mode=JOBS_PRINT_PID; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); + case 'c': + mode=JOBS_PRINT_COMMAND; + break; + case 'g': + mode=JOBS_PRINT_GROUP; + break; - return 1; + case 'l': + { + print_last = 1; + break; + } + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return 0; - case 'p': - mode=JOBS_PRINT_PID; - break; - - case 'c': - mode=JOBS_PRINT_COMMAND; - break; - - case 'g': - mode=JOBS_PRINT_GROUP; - break; - - case 'l': - { - print_last = 1; - break; - } - - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return 0; - - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return 1; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return 1; } } diff --git a/builtin_set.cpp b/builtin_set.cpp index 8c6ed593b..1b20ee913 100644 --- a/builtin_set.cpp +++ b/builtin_set.cpp @@ -141,19 +141,19 @@ static int my_env_set(const wchar_t *key, const wcstring_list_t &val, int scope) switch (env_set(key, val_str, scope | ENV_USER)) { - case ENV_PERM: - { - append_format(stderr_buffer, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key); - retcode=1; - break; - } + case ENV_PERM: + { + append_format(stderr_buffer, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key); + retcode=1; + break; + } - case ENV_INVALID: - { - append_format(stderr_buffer, _(L"%ls: Unknown error"), L"set"); - retcode=1; - break; - } + case ENV_INVALID: + { + append_format(stderr_buffer, _(L"%ls: Unknown error"), L"set"); + retcode=1; + break; + } } return retcode; @@ -470,55 +470,55 @@ static int builtin_set(parser_t &parser, wchar_t **argv) switch (c) { - case 0: - break; + case 0: + break; - case 'e': - erase = 1; - break; + case 'e': + erase = 1; + break; - case 'n': - list = 1; - break; + case 'n': + list = 1; + break; - case 'x': - exportv = 1; - break; + case 'x': + exportv = 1; + break; - case 'l': - local = 1; - break; + case 'l': + local = 1; + break; - case 'g': - global = 1; - break; + case 'g': + global = 1; + break; - case 'u': - unexport = 1; - break; + case 'u': + unexport = 1; + break; - case 'U': - universal = 1; - break; + case 'U': + universal = 1; + break; - case 'L': - shorten_ok = false; - break; + case 'L': + shorten_ok = false; + break; - case 'q': - query = 1; - break; + case 'q': + query = 1; + break; - case 'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return 0; + case 'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return 0; - case '?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return 1; + case '?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return 1; - default: - break; + default: + break; } } diff --git a/builtin_test.cpp b/builtin_test.cpp index ad52f6c0c..bfd9bfa09 100644 --- a/builtin_test.cpp +++ b/builtin_test.cpp @@ -592,12 +592,12 @@ bool unary_operator::evaluate(wcstring_list_t &errors) { switch (token) { - case test_bang: - assert(subject.get()); - return ! subject->evaluate(errors); - default: - errors.push_back(format_string(L"Unknown token type in %s", __func__)); - return false; + case test_bang: + assert(subject.get()); + return ! subject->evaluate(errors); + default: + errors.push_back(format_string(L"Unknown token type in %s", __func__)); + return false; } } @@ -606,51 +606,51 @@ bool combining_expression::evaluate(wcstring_list_t &errors) { switch (token) { - case test_combine_and: - case test_combine_or: - { - /* One-element case */ - if (subjects.size() == 1) - return subjects.at(0)->evaluate(errors); - - /* Evaluate our lists, remembering that AND has higher precedence than OR. We can visualize this as a sequence of OR expressions of AND expressions. */ - assert(combiners.size() + 1 == subjects.size()); - assert(! subjects.empty()); - - size_t idx = 0, max = subjects.size(); - bool or_result = false; - while (idx < max) + case test_combine_and: + case test_combine_or: { - if (or_result) - { - /* Short circuit */ - break; - } + /* One-element case */ + if (subjects.size() == 1) + return subjects.at(0)->evaluate(errors); - /* Evaluate a stream of AND starting at given subject index. It may only have one element. */ - bool and_result = true; - for (; idx < max; idx++) - { - /* Evaluate it, short-circuiting */ - and_result = and_result && subjects.at(idx)->evaluate(errors); + /* Evaluate our lists, remembering that AND has higher precedence than OR. We can visualize this as a sequence of OR expressions of AND expressions. */ + assert(combiners.size() + 1 == subjects.size()); + assert(! subjects.empty()); - /* If the combiner at this index (which corresponding to how we combine with the next subject) is not AND, then exit the loop */ - if (idx + 1 < max && combiners.at(idx) != test_combine_and) + size_t idx = 0, max = subjects.size(); + bool or_result = false; + while (idx < max) + { + if (or_result) { - idx++; + /* Short circuit */ break; } + + /* Evaluate a stream of AND starting at given subject index. It may only have one element. */ + bool and_result = true; + for (; idx < max; idx++) + { + /* Evaluate it, short-circuiting */ + and_result = and_result && subjects.at(idx)->evaluate(errors); + + /* If the combiner at this index (which corresponding to how we combine with the next subject) is not AND, then exit the loop */ + if (idx + 1 < max && combiners.at(idx) != test_combine_and) + { + idx++; + break; + } + } + + /* OR it in */ + or_result = or_result || and_result; } - - /* OR it in */ - or_result = or_result || and_result; + return or_result; } - return or_result; - } - default: - errors.push_back(format_string(L"Unknown token type in %s", __func__)); - return BUILTIN_TEST_FAIL; + default: + errors.push_back(format_string(L"Unknown token type in %s", __func__)); + return BUILTIN_TEST_FAIL; } } @@ -675,33 +675,33 @@ static bool binary_primary_evaluate(test_expressions::token_t token, const wcstr long long left_num, right_num; switch (token) { - case test_string_equal: - return left == right; + case test_string_equal: + return left == right; - case test_string_not_equal: - return left != right; + case test_string_not_equal: + return left != right; - case test_number_equal: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num == right_num; + case test_number_equal: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num == right_num; - case test_number_not_equal: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num != right_num; + case test_number_not_equal: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num != right_num; - case test_number_greater: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num > right_num; + case test_number_greater: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num > right_num; - case test_number_greater_equal: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num >= right_num; + case test_number_greater_equal: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num >= right_num; - case test_number_lesser: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num < right_num; + case test_number_lesser: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num < right_num; - case test_number_lesser_equal: - return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num <= right_num; + case test_number_lesser_equal: + return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num <= right_num; - default: - errors.push_back(format_string(L"Unknown token type in %s", __func__)); - return false; + default: + errors.push_back(format_string(L"Unknown token type in %s", __func__)); + return false; } } @@ -713,61 +713,61 @@ static bool unary_primary_evaluate(test_expressions::token_t token, const wcstri long long num; switch (token) { - case test_filetype_b: // "-b", for block special files - return !wstat(arg, &buf) && S_ISBLK(buf.st_mode); + case test_filetype_b: // "-b", for block special files + return !wstat(arg, &buf) && S_ISBLK(buf.st_mode); - case test_filetype_c: // "-c" for character special files - return !wstat(arg, &buf) && S_ISCHR(buf.st_mode); + case test_filetype_c: // "-c" for character special files + return !wstat(arg, &buf) && S_ISCHR(buf.st_mode); - case test_filetype_d: // "-d" for directories - return !wstat(arg, &buf) && S_ISDIR(buf.st_mode); + case test_filetype_d: // "-d" for directories + return !wstat(arg, &buf) && S_ISDIR(buf.st_mode); - case test_filetype_e: // "-e" for files that exist - return !wstat(arg, &buf); + case test_filetype_e: // "-e" for files that exist + return !wstat(arg, &buf); - case test_filetype_f: // "-f" for for regular files - return !wstat(arg, &buf) && S_ISREG(buf.st_mode); + case test_filetype_f: // "-f" for for regular files + return !wstat(arg, &buf) && S_ISREG(buf.st_mode); - case test_filetype_g: // "-g" for set-group-id - return !wstat(arg, &buf) && (S_ISGID & buf.st_mode); + case test_filetype_g: // "-g" for set-group-id + return !wstat(arg, &buf) && (S_ISGID & buf.st_mode); - case test_filetype_h: // "-h" for symbolic links - case test_filetype_L: // "-L", same as -h - return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode); + case test_filetype_h: // "-h" for symbolic links + case test_filetype_L: // "-L", same as -h + return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode); - case test_filetype_p: // "-p", for FIFO - return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode); + case test_filetype_p: // "-p", for FIFO + return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode); - case test_filetype_S: // "-S", socket - return !wstat(arg, &buf) && S_ISSOCK(buf.st_mode); + case test_filetype_S: // "-S", socket + return !wstat(arg, &buf) && S_ISSOCK(buf.st_mode); - case test_filesize_s: // "-s", size greater than zero - return !wstat(arg, &buf) && buf.st_size > 0; + case test_filesize_s: // "-s", size greater than zero + return !wstat(arg, &buf) && buf.st_size > 0; - case test_filedesc_t: // "-t", whether the fd is associated with a terminal - return parse_number(arg, &num) && num == (int)num && isatty((int)num); + case test_filedesc_t: // "-t", whether the fd is associated with a terminal + return parse_number(arg, &num) && num == (int)num && isatty((int)num); - case test_fileperm_r: // "-r", read permission - return !waccess(arg, R_OK); + case test_fileperm_r: // "-r", read permission + return !waccess(arg, R_OK); - case test_fileperm_u: // "-u", whether file is setuid - return !wstat(arg, &buf) && (S_ISUID & buf.st_mode); + case test_fileperm_u: // "-u", whether file is setuid + return !wstat(arg, &buf) && (S_ISUID & buf.st_mode); - case test_fileperm_w: // "-w", whether file write permission is allowed - return !waccess(arg, W_OK); + case test_fileperm_w: // "-w", whether file write permission is allowed + return !waccess(arg, W_OK); - case test_fileperm_x: // "-x", whether file execute/search is allowed - return !waccess(arg, X_OK); + case test_fileperm_x: // "-x", whether file execute/search is allowed + return !waccess(arg, X_OK); - case test_string_n: // "-n", non-empty string - return ! arg.empty(); + case test_string_n: // "-n", non-empty string + return ! arg.empty(); - case test_string_z: // "-z", true if length of string is 0 - return arg.empty(); + case test_string_z: // "-z", true if length of string is 0 + return arg.empty(); - default: - errors.push_back(format_string(L"Unknown token type in %s", __func__)); - return false; + default: + errors.push_back(format_string(L"Unknown token type in %s", __func__)); + return false; } } diff --git a/builtin_ulimit.cpp b/builtin_ulimit.cpp index 4a244ad60..37bebd61f 100644 --- a/builtin_ulimit.cpp +++ b/builtin_ulimit.cpp @@ -344,83 +344,83 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) switch (opt) { - case 0: - if (long_options[opt_index].flag != 0) + case 0: + if (long_options[opt_index].flag != 0) + break; + append_format(stderr_buffer, + BUILTIN_ERR_UNKNOWN, + argv[0], + long_options[opt_index].name); + builtin_print_help(parser, argv[0], stderr_buffer); + + return 1; + + case L'a': + report_all=1; break; - append_format(stderr_buffer, - BUILTIN_ERR_UNKNOWN, - argv[0], - long_options[opt_index].name); - builtin_print_help(parser, argv[0], stderr_buffer); - return 1; + case L'H': + hard=1; + break; - case L'a': - report_all=1; - break; + case L'S': + soft=1; + break; - case L'H': - hard=1; - break; + case L'c': + what=RLIMIT_CORE; + break; - case L'S': - soft=1; - break; + case L'd': + what=RLIMIT_DATA; + break; - case L'c': - what=RLIMIT_CORE; - break; - - case L'd': - what=RLIMIT_DATA; - break; - - case L'f': - what=RLIMIT_FSIZE; - break; + case L'f': + what=RLIMIT_FSIZE; + break; #ifdef RLIMIT_MEMLOCK - case L'l': - what=RLIMIT_MEMLOCK; - break; + case L'l': + what=RLIMIT_MEMLOCK; + break; #endif #ifdef RLIMIT_RSS - case L'm': - what=RLIMIT_RSS; - break; + case L'm': + what=RLIMIT_RSS; + break; #endif - case L'n': - what=RLIMIT_NOFILE; - break; + case L'n': + what=RLIMIT_NOFILE; + break; - case L's': - what=RLIMIT_STACK; - break; + case L's': + what=RLIMIT_STACK; + break; - case L't': - what=RLIMIT_CPU; - break; + case L't': + what=RLIMIT_CPU; + break; #ifdef RLIMIT_NPROC - case L'u': - what=RLIMIT_NPROC; - break; + case L'u': + what=RLIMIT_NPROC; + break; #endif #ifdef RLIMIT_AS - case L'v': - what=RLIMIT_AS; - break; + case L'v': + what=RLIMIT_AS; + break; #endif - case L'h': - builtin_print_help(parser, argv[0], stdout_buffer); - return 0; + case L'h': + builtin_print_help(parser, argv[0], stdout_buffer); + return 0; - case L'?': - builtin_unknown_option(parser, argv[0], argv[woptind-1]); - return 1; + case L'?': + builtin_unknown_option(parser, argv[0], argv[woptind-1]); + return 1; } } @@ -443,69 +443,69 @@ static int builtin_ulimit(parser_t &parser, wchar_t ** argv) switch (argc - woptind) { - case 0: - { - /* - Show current limit value - */ - print(what, hard); - break; - } - - case 1: - { - /* - Change current limit value - */ - rlim_t new_limit; - wchar_t *end; - - /* - Set both hard and soft limits if nothing else was specified - */ - if (!(hard+soft)) + case 0: { - hard=soft=1; + /* + Show current limit value + */ + print(what, hard); + break; } - if (wcscasecmp(argv[woptind], L"unlimited")==0) + case 1: { - new_limit = RLIM_INFINITY; - } - else if (wcscasecmp(argv[woptind], L"hard")==0) - { - new_limit = get(what, 1); - } - else if (wcscasecmp(argv[woptind], L"soft")==0) - { - new_limit = get(what, soft); - } - else - { - errno=0; - new_limit = wcstol(argv[woptind], &end, 10); - if (errno || *end) + /* + Change current limit value + */ + rlim_t new_limit; + wchar_t *end; + + /* + Set both hard and soft limits if nothing else was specified + */ + if (!(hard+soft)) { - append_format(stderr_buffer, - L"%ls: Invalid limit '%ls'\n", - argv[0], - argv[woptind]); - builtin_print_help(parser, argv[0], stderr_buffer); - return 1; + hard=soft=1; } - new_limit *= get_multiplier(what); + + if (wcscasecmp(argv[woptind], L"unlimited")==0) + { + new_limit = RLIM_INFINITY; + } + else if (wcscasecmp(argv[woptind], L"hard")==0) + { + new_limit = get(what, 1); + } + else if (wcscasecmp(argv[woptind], L"soft")==0) + { + new_limit = get(what, soft); + } + else + { + errno=0; + new_limit = wcstol(argv[woptind], &end, 10); + if (errno || *end) + { + append_format(stderr_buffer, + L"%ls: Invalid limit '%ls'\n", + argv[0], + argv[woptind]); + builtin_print_help(parser, argv[0], stderr_buffer); + return 1; + } + new_limit *= get_multiplier(what); + } + + return set(what, hard, soft, new_limit); } - return set(what, hard, soft, new_limit); - } - - default: - { - stderr_buffer.append(argv[0]); - stderr_buffer.append(L": Too many arguments\n"); - builtin_print_help(parser, argv[0], stderr_buffer); - return 1; - } + default: + { + stderr_buffer.append(argv[0]); + stderr_buffer.append(L": Too many arguments\n"); + builtin_print_help(parser, argv[0], stderr_buffer); + return 1; + } } return 0; diff --git a/color.cpp b/color.cpp index f0926b271..d4dd9453e 100644 --- a/color.cpp +++ b/color.cpp @@ -31,46 +31,46 @@ static int parse_hex_digit(wchar_t x) { switch (x) { - case L'0': - return 0x0; - case L'1': - return 0x1; - case L'2': - return 0x2; - case L'3': - return 0x3; - case L'4': - return 0x4; - case L'5': - return 0x5; - case L'6': - return 0x6; - case L'7': - return 0x7; - case L'8': - return 0x8; - case L'9': - return 0x9; - case L'a': - case L'A': - return 0xA; - case L'b': - case L'B': - return 0xB; - case L'c': - case L'C': - return 0xC; - case L'd': - case L'D': - return 0xD; - case L'e': - case L'E': - return 0xE; - case L'f': - case L'F': - return 0xF; - default: - return -1; + case L'0': + return 0x0; + case L'1': + return 0x1; + case L'2': + return 0x2; + case L'3': + return 0x3; + case L'4': + return 0x4; + case L'5': + return 0x5; + case L'6': + return 0x6; + case L'7': + return 0x7; + case L'8': + return 0x8; + case L'9': + return 0x9; + case L'a': + case L'A': + return 0xA; + case L'b': + case L'B': + return 0xB; + case L'c': + case L'C': + return 0xC; + case L'd': + case L'D': + return 0xD; + case L'e': + case L'E': + return 0xE; + case L'f': + case L'F': + return 0xF; + default: + return -1; } } @@ -334,20 +334,20 @@ wcstring rgb_color_t::description() const { switch (type) { - case type_none: - return L"none"; - case type_named: - return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx)); - case type_rgb: - return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]); - case type_reset: - return L"reset"; - case type_normal: - return L"normal"; - case type_ignore: - return L"ignore"; - default: - abort(); - return L""; + case type_none: + return L"none"; + case type_named: + return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx)); + case type_rgb: + return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]); + case type_reset: + return L"reset"; + case type_normal: + return L"normal"; + case type_ignore: + return L"ignore"; + default: + abort(); + return L""; } } diff --git a/common.cpp b/common.cpp index 9147805a3..1e8806ac9 100644 --- a/common.cpp +++ b/common.cpp @@ -144,19 +144,19 @@ int fgetws2(wcstring *s, FILE *f) switch (c) { - /* End of line */ - case WEOF: - case L'\n': - case L'\0': - return i; - /* Ignore carriage returns */ - case L'\r': - break; + /* End of line */ + case WEOF: + case L'\n': + case L'\0': + return i; + /* Ignore carriage returns */ + case L'\r': + break; - default: - i++; - s->push_back((wchar_t)c); - break; + default: + i++; + s->push_back((wchar_t)c); + break; } } } @@ -225,25 +225,25 @@ wchar_t *str2wcs_internal(const char *in, wchar_t *out) switch (res) { - case (size_t)(-2): - case (size_t)(-1): - { - out[out_pos] = ENCODE_DIRECT_BASE + (unsigned char)in[in_pos]; - in_pos++; - memset(&state, 0, sizeof(state)); - break; - } + case (size_t)(-2): + case (size_t)(-1): + { + out[out_pos] = ENCODE_DIRECT_BASE + (unsigned char)in[in_pos]; + in_pos++; + memset(&state, 0, sizeof(state)); + break; + } - case 0: - { - return out; - } + case 0: + { + return out; + } - default: - { - in_pos += res; - break; - } + default: + { + in_pos += res; + break; + } } out_pos++; } @@ -957,107 +957,107 @@ wchar_t *escape(const wchar_t *in_orig, escape_flags_t flags) wchar_t c = *in; switch (c) { - case L'\t': - *(pos++) = L'\\'; - *(pos++) = L't'; - need_escape=need_complex_escape=1; - break; + case L'\t': + *(pos++) = L'\\'; + *(pos++) = L't'; + need_escape=need_complex_escape=1; + break; - case L'\n': - *(pos++) = L'\\'; - *(pos++) = L'n'; - need_escape=need_complex_escape=1; - break; + case L'\n': + *(pos++) = L'\\'; + *(pos++) = L'n'; + need_escape=need_complex_escape=1; + break; - case L'\b': - *(pos++) = L'\\'; - *(pos++) = L'b'; - need_escape=need_complex_escape=1; - break; + case L'\b': + *(pos++) = L'\\'; + *(pos++) = L'b'; + need_escape=need_complex_escape=1; + break; - case L'\r': - *(pos++) = L'\\'; - *(pos++) = L'r'; - need_escape=need_complex_escape=1; - break; + case L'\r': + *(pos++) = L'\\'; + *(pos++) = L'r'; + need_escape=need_complex_escape=1; + break; - case L'\x1b': - *(pos++) = L'\\'; - *(pos++) = L'e'; - need_escape=need_complex_escape=1; - break; + case L'\x1b': + *(pos++) = L'\\'; + *(pos++) = L'e'; + need_escape=need_complex_escape=1; + break; - case L'\\': - case L'\'': - { - need_escape=need_complex_escape=1; - if (escape_all) - *pos++ = L'\\'; - *pos++ = *in; - break; - } - - case L'&': - case L'$': - case L' ': - case L'#': - case L'^': - case L'<': - case L'>': - case L'(': - case L')': - case L'[': - case L']': - case L'{': - case L'}': - case L'?': - case L'*': - case L'|': - case L';': - case L'"': - case L'%': - case L'~': - { - if (! no_tilde || c != L'~') + case L'\\': + case L'\'': { - need_escape=1; + need_escape=need_complex_escape=1; if (escape_all) *pos++ = L'\\'; - } - *pos++ = *in; - break; - } - - default: - { - if (*in < 32) - { - if (*in <27 && *in > 0) - { - *(pos++) = L'\\'; - *(pos++) = L'c'; - *(pos++) = L'a' + *in -1; - - need_escape=need_complex_escape=1; - break; - - } - - - int tmp = (*in)%16; - *pos++ = L'\\'; - *pos++ = L'x'; - *pos++ = ((*in>15)? L'1' : L'0'); - *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; - need_escape=need_complex_escape=1; - } - else - { *pos++ = *in; + break; + } + + case L'&': + case L'$': + case L' ': + case L'#': + case L'^': + case L'<': + case L'>': + case L'(': + case L')': + case L'[': + case L']': + case L'{': + case L'}': + case L'?': + case L'*': + case L'|': + case L';': + case L'"': + case L'%': + case L'~': + { + if (! no_tilde || c != L'~') + { + need_escape=1; + if (escape_all) + *pos++ = L'\\'; + } + *pos++ = *in; + break; + } + + default: + { + if (*in < 32) + { + if (*in <27 && *in > 0) + { + *(pos++) = L'\\'; + *(pos++) = L'c'; + *(pos++) = L'a' + *in -1; + + need_escape=need_complex_escape=1; + break; + + } + + + int tmp = (*in)%16; + *pos++ = L'\\'; + *pos++ = L'x'; + *pos++ = ((*in>15)? L'1' : L'0'); + *pos++ = tmp > 9? L'a'+(tmp-10):L'0'+tmp; + need_escape=need_complex_escape=1; + } + else + { + *pos++ = *in; + } + break; } - break; - } } } @@ -1116,503 +1116,503 @@ wchar_t *unescape(const wchar_t * orig, int flags) switch (mode) { - /* - Mode 0 means unquoted string - */ - case 0: - { - if (c == L'\\') + /* + Mode 0 means unquoted string + */ + case 0: { - switch (in[++in_pos]) + if (c == L'\\') { - - /* - A null character after a backslash is an - error, return null - */ - case L'\0': - { - if (!allow_incomplete) + switch (in[++in_pos]) { - free(in); - return 0; + + /* + A null character after a backslash is an + error, return null + */ + case L'\0': + { + if (!allow_incomplete) + { + free(in); + return 0; + } + } + + /* + Numeric escape sequences. No prefix means + octal escape, otherwise hexadecimal. + */ + + case L'0': + case L'1': + case L'2': + case L'3': + case L'4': + case L'5': + case L'6': + case L'7': + case L'u': + case L'U': + case L'x': + case L'X': + { + int i; + long long res=0; + int chars=2; + int base=16; + + int byte = 0; + wchar_t max_val = ASCII_MAX; + + switch (in[in_pos]) + { + case L'u': + { + chars=4; + max_val = UCS2_MAX; + break; + } + + case L'U': + { + chars=8; + max_val = WCHAR_MAX; + break; + } + + case L'x': + { + break; + } + + case L'X': + { + byte=1; + max_val = BYTE_MAX; + break; + } + + default: + { + base=8; + chars=3; + // note in_pod must be larger than 0 since we incremented it above + assert(in_pos > 0); + in_pos--; + break; + } + } + + for (i=0; i= L'a' && + in[in_pos] <= (L'a'+32)) + { + in[out_pos]=in[in_pos]-L'a'+1; + } + else if (in[in_pos] >= L'A' && + in[in_pos] <= (L'A'+32)) + { + in[out_pos]=in[in_pos]-L'A'+1; + } + else + { + free(in); + return 0; + } + break; + + } + + /* + \x1b means escape + */ + case L'e': + { + in[out_pos]=L'\x1b'; + break; + } + + /* + \f means form feed + */ + case L'f': + { + in[out_pos]=L'\f'; + break; + } + + /* + \n means newline + */ + case L'n': + { + in[out_pos]=L'\n'; + break; + } + + /* + \r means carriage return + */ + case L'r': + { + in[out_pos]=L'\r'; + break; + } + + /* + \t means tab + */ + case L't': + { + in[out_pos]=L'\t'; + break; + } + + /* + \v means vertical tab + */ + case L'v': + { + in[out_pos]=L'\v'; + break; + } + + default: + { + if (unescape_special) + in[out_pos++] = INTERNAL_SEPARATOR; + in[out_pos]=in[in_pos]; + break; + } } } - - /* - Numeric escape sequences. No prefix means - octal escape, otherwise hexadecimal. - */ - - case L'0': - case L'1': - case L'2': - case L'3': - case L'4': - case L'5': - case L'6': - case L'7': - case L'u': - case L'U': - case L'x': - case L'X': + else { - int i; - long long res=0; - int chars=2; - int base=16; - - int byte = 0; - wchar_t max_val = ASCII_MAX; - switch (in[in_pos]) { - case L'u': + case L'~': + { + if (unescape_special && (in_pos == 0)) + { + in[out_pos]=HOME_DIRECTORY; + } + else + { + in[out_pos] = L'~'; + } + break; + } + + case L'%': + { + if (unescape_special && (in_pos == 0)) + { + in[out_pos]=PROCESS_EXPAND; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'*': + { + if (unescape_special) + { + if (out_pos > 0 && in[out_pos-1]==ANY_STRING) + { + out_pos--; + in[out_pos] = ANY_STRING_RECURSIVE; + } + else + in[out_pos]=ANY_STRING; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'?': + { + if (unescape_special) + { + in[out_pos]=ANY_CHAR; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'$': + { + if (unescape_special) + { + in[out_pos]=VARIABLE_EXPAND; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'{': + { + if (unescape_special) + { + bracket_count++; + in[out_pos]=BRACKET_BEGIN; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'}': + { + if (unescape_special) + { + bracket_count--; + in[out_pos]=BRACKET_END; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L',': + { + if (unescape_special && bracket_count && prev!=BRACKET_SEP) + { + in[out_pos]=BRACKET_SEP; + } + else + { + in[out_pos]=in[in_pos]; + } + break; + } + + case L'\'': + { + mode = 1; + if (unescape_special) + in[out_pos] = INTERNAL_SEPARATOR; + else + out_pos--; + break; + } + + case L'\"': + { + mode = 2; + if (unescape_special) + in[out_pos] = INTERNAL_SEPARATOR; + else + out_pos--; + break; + } + + default: + { + in[out_pos] = in[in_pos]; + break; + } + } + } + break; + } + + /* + Mode 1 means single quoted string, i.e 'foo' + */ + case 1: + { + if (c == L'\\') + { + switch (in[++in_pos]) { - chars=4; - max_val = UCS2_MAX; + case '\\': + case L'\'': + case L'\n': + { + in[out_pos]=in[in_pos]; + break; + } + + case 0: + { + if (!allow_incomplete) + { + free(in); + return 0; + } + else + { + //We may ever escape a NULL character, but still appending a \ in case I am wrong. + in[out_pos] = L'\\'; + } + } + break; + default: + { + in[out_pos++] = L'\\'; + in[out_pos]= in[in_pos]; + } + } + + } + if (c == L'\'') + { + if (unescape_special) + in[out_pos] = INTERNAL_SEPARATOR; + else + out_pos--; + mode = 0; + } + else + { + in[out_pos] = in[in_pos]; + } + + break; + } + + /* + Mode 2 means double quoted string, i.e. "foo" + */ + case 2: + { + switch (c) + { + case '"': + { + mode = 0; + if (unescape_special) + in[out_pos] = INTERNAL_SEPARATOR; + else + out_pos--; break; } - case L'U': + case '\\': { - chars=8; - max_val = WCHAR_MAX; + switch (in[++in_pos]) + { + case L'\0': + { + if (!allow_incomplete) + { + free(in); + return 0; + } + else + { + //We probably don't need it since NULL character is always appended before ending this function. + in[out_pos]=in[in_pos]; + } + } + break; + case '\\': + case L'$': + case '"': + case '\n': + { + in[out_pos]=in[in_pos]; + break; + } + + default: + { + in[out_pos++] = L'\\'; + in[out_pos] = in[in_pos]; + break; + } + } break; } - case L'x': + case '$': { - break; - } - - case L'X': - { - byte=1; - max_val = BYTE_MAX; + if (unescape_special) + { + in[out_pos]=VARIABLE_EXPAND_SINGLE; + } + else + { + in[out_pos]=in[in_pos]; + } break; } default: { - base=8; - chars=3; - // note in_pod must be larger than 0 since we incremented it above - assert(in_pos > 0); - in_pos--; + in[out_pos] = in[in_pos]; break; } - } - for (i=0; i= L'a' && - in[in_pos] <= (L'a'+32)) - { - in[out_pos]=in[in_pos]-L'a'+1; - } - else if (in[in_pos] >= L'A' && - in[in_pos] <= (L'A'+32)) - { - in[out_pos]=in[in_pos]-L'A'+1; - } - else - { - free(in); - return 0; - } - break; - - } - - /* - \x1b means escape - */ - case L'e': - { - in[out_pos]=L'\x1b'; - break; - } - - /* - \f means form feed - */ - case L'f': - { - in[out_pos]=L'\f'; - break; - } - - /* - \n means newline - */ - case L'n': - { - in[out_pos]=L'\n'; - break; - } - - /* - \r means carriage return - */ - case L'r': - { - in[out_pos]=L'\r'; - break; - } - - /* - \t means tab - */ - case L't': - { - in[out_pos]=L'\t'; - break; - } - - /* - \v means vertical tab - */ - case L'v': - { - in[out_pos]=L'\v'; - break; - } - - default: - { - if (unescape_special) - in[out_pos++] = INTERNAL_SEPARATOR; - in[out_pos]=in[in_pos]; - break; - } - } - } - else - { - switch (in[in_pos]) - { - case L'~': - { - if (unescape_special && (in_pos == 0)) - { - in[out_pos]=HOME_DIRECTORY; - } - else - { - in[out_pos] = L'~'; - } - break; - } - - case L'%': - { - if (unescape_special && (in_pos == 0)) - { - in[out_pos]=PROCESS_EXPAND; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'*': - { - if (unescape_special) - { - if (out_pos > 0 && in[out_pos-1]==ANY_STRING) - { - out_pos--; - in[out_pos] = ANY_STRING_RECURSIVE; - } - else - in[out_pos]=ANY_STRING; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'?': - { - if (unescape_special) - { - in[out_pos]=ANY_CHAR; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'$': - { - if (unescape_special) - { - in[out_pos]=VARIABLE_EXPAND; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'{': - { - if (unescape_special) - { - bracket_count++; - in[out_pos]=BRACKET_BEGIN; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'}': - { - if (unescape_special) - { - bracket_count--; - in[out_pos]=BRACKET_END; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L',': - { - if (unescape_special && bracket_count && prev!=BRACKET_SEP) - { - in[out_pos]=BRACKET_SEP; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - case L'\'': - { - mode = 1; - if (unescape_special) - in[out_pos] = INTERNAL_SEPARATOR; - else - out_pos--; - break; - } - - case L'\"': - { - mode = 2; - if (unescape_special) - in[out_pos] = INTERNAL_SEPARATOR; - else - out_pos--; - break; - } - - default: - { - in[out_pos] = in[in_pos]; - break; - } - } - } - break; - } - - /* - Mode 1 means single quoted string, i.e 'foo' - */ - case 1: - { - if (c == L'\\') - { - switch (in[++in_pos]) - { - case '\\': - case L'\'': - case L'\n': - { - in[out_pos]=in[in_pos]; - break; - } - - case 0: - { - if (!allow_incomplete) - { - free(in); - return 0; - } - else - { - //We may ever escape a NULL character, but still appending a \ in case I am wrong. - in[out_pos] = L'\\'; - } - } - break; - default: - { - in[out_pos++] = L'\\'; - in[out_pos]= in[in_pos]; - } - } - - } - if (c == L'\'') - { - if (unescape_special) - in[out_pos] = INTERNAL_SEPARATOR; - else - out_pos--; - mode = 0; - } - else - { - in[out_pos] = in[in_pos]; - } - - break; - } - - /* - Mode 2 means double quoted string, i.e. "foo" - */ - case 2: - { - switch (c) - { - case '"': - { - mode = 0; - if (unescape_special) - in[out_pos] = INTERNAL_SEPARATOR; - else - out_pos--; - break; - } - - case '\\': - { - switch (in[++in_pos]) - { - case L'\0': - { - if (!allow_incomplete) - { - free(in); - return 0; - } - else - { - //We probably don't need it since NULL character is always appended before ending this function. - in[out_pos]=in[in_pos]; - } - } - break; - case '\\': - case L'$': - case '"': - case '\n': - { - in[out_pos]=in[in_pos]; - break; - } - - default: - { - in[out_pos++] = L'\\'; - in[out_pos] = in[in_pos]; - break; - } } break; } - - case '$': - { - if (unescape_special) - { - in[out_pos]=VARIABLE_EXPAND_SINGLE; - } - else - { - in[out_pos]=in[in_pos]; - } - break; - } - - default: - { - in[out_pos] = in[in_pos]; - break; - } - - } - break; - } } } diff --git a/complete.cpp b/complete.cpp index 77018810d..ce0944854 100644 --- a/complete.cpp +++ b/complete.cpp @@ -702,20 +702,20 @@ int complete_is_valid_option(const wcstring &str, switch (opt.size()) { - case 0: - case 1: - { - return true; - } - - case 2: - { - if (opt == L"--") + case 0: + case 1: { return true; } - break; - } + + case 2: + { + if (opt == L"--") + { + return true; + } + break; + } } if (opt.at(0) != L'-') @@ -1816,73 +1816,73 @@ void complete(const wcstring &cmd, std::vector &comps, complete_ty switch (tok_last_type(&tok)) { - case TOK_STRING: - { - - const wcstring ncmd = tok_last(&tok); - int is_ddash = (ncmd == L"--") && ((tok_get_pos(&tok)+2) < (long)pos); - - if (!had_cmd) + case TOK_STRING: { - if (parser_keywords_is_subcommand(ncmd)) + const wcstring ncmd = tok_last(&tok); + int is_ddash = (ncmd == L"--") && ((tok_get_pos(&tok)+2) < (long)pos); + + if (!had_cmd) { - if (ncmd == L"builtin") + + if (parser_keywords_is_subcommand(ncmd)) { - use_function = 0; - use_command = 0; - use_builtin = 1; + if (ncmd == L"builtin") + { + use_function = 0; + use_command = 0; + use_builtin = 1; + } + else if (ncmd == L"command") + { + use_command = 1; + use_function = 0; + use_builtin = 0; + } + break; } - else if (ncmd == L"command") + + + if (!is_ddash || + ((use_command && use_function && use_builtin))) { - use_command = 1; - use_function = 0; - use_builtin = 0; + current_command = ncmd; + + size_t token_end = tok_get_pos(&tok) + ncmd.size(); + + on_command = (pos <= token_end); + had_cmd=1; + } + + } + else + { + if (is_ddash) + { + had_ddash = 1; } - break; - } - - - if (!is_ddash || - ((use_command && use_function && use_builtin))) - { - current_command = ncmd; - - size_t token_end = tok_get_pos(&tok) + ncmd.size(); - - on_command = (pos <= token_end); - had_cmd=1; } + break; } - else + + case TOK_END: + case TOK_PIPE: + case TOK_BACKGROUND: { - if (is_ddash) - { - had_ddash = 1; - } + had_cmd=0; + had_ddash = 0; + use_command = 1; + use_function = 1; + use_builtin = 1; + break; } - break; - } - - case TOK_END: - case TOK_PIPE: - case TOK_BACKGROUND: - { - had_cmd=0; - had_ddash = 0; - use_command = 1; - use_function = 1; - use_builtin = 1; - break; - } - - case TOK_ERROR: - { - end_loop=1; - break; - } + case TOK_ERROR: + { + end_loop=1; + break; + } } diff --git a/env.cpp b/env.cpp index 3c12da348..035fee4ec 100644 --- a/env.cpp +++ b/env.cpp @@ -393,21 +393,21 @@ static void universal_callback(fish_message_type_t type, switch (type) { - case SET: - case SET_EXPORT: - { - str=L"SET"; - break; - } + case SET: + case SET_EXPORT: + { + str=L"SET"; + break; + } - case ERASE: - { - str=L"ERASE"; - break; - } + case ERASE: + { + str=L"ERASE"; + break; + } - default: - break; + default: + break; } if (str) diff --git a/env_universal_common.cpp b/env_universal_common.cpp index 1f78705d9..0a8bcfaae 100644 --- a/env_universal_common.cpp +++ b/env_universal_common.cpp @@ -215,17 +215,17 @@ static wchar_t *utf2wcs(const char *in) switch (sizeof(wchar_t)) { - case 2: - to_name = iconv_wide_names_2; - break; + case 2: + to_name = iconv_wide_names_2; + break; - case 4: - to_name = iconv_wide_names_4; - break; + case 4: + to_name = iconv_wide_names_4; + break; - default: - to_name = iconv_wide_names_unknown; - break; + default: + to_name = iconv_wide_names_unknown; + break; } @@ -336,17 +336,17 @@ static char *wcs2utf(const wchar_t *in) switch (sizeof(wchar_t)) { - case 2: - from_name = iconv_wide_names_2; - break; + case 2: + from_name = iconv_wide_names_2; + break; - case 4: - from_name = iconv_wide_names_4; - break; + case 4: + from_name = iconv_wide_names_4; + break; - default: - from_name = iconv_wide_names_unknown; - break; + default: + from_name = iconv_wide_names_unknown; + break; } const char **to_name = iconv_utf8_names; @@ -478,34 +478,34 @@ void read_message(connection_t *src) switch (ib) { - case ENV_UNIVERSAL_AGAIN: - { - return; - } - - case ENV_UNIVERSAL_ERROR: - { - debug(2, L"Read error on fd %d, set killme flag", src->fd); - if (debug_level > 2) - wperror(L"read"); - src->killme = 1; - return; - } - - case ENV_UNIVERSAL_EOF: - { - src->killme = 1; - debug(3, L"Fd %d has reached eof, set killme flag", src->fd); - if (src->input.size() > 0) + case ENV_UNIVERSAL_AGAIN: { - char c = 0; - src->input.push_back(c); - debug(1, - L"Universal variable connection closed while reading command. Partial command recieved: '%s'", - &src->input.at(0)); + return; + } + + case ENV_UNIVERSAL_ERROR: + { + debug(2, L"Read error on fd %d, set killme flag", src->fd); + if (debug_level > 2) + wperror(L"read"); + src->killme = 1; + return; + } + + case ENV_UNIVERSAL_EOF: + { + src->killme = 1; + debug(3, L"Fd %d has reached eof, set killme flag", src->fd); + if (src->input.size() > 0) + { + char c = 0; + src->input.push_back(c); + debug(1, + L"Universal variable connection closed while reading command. Partial command recieved: '%s'", + &src->input.at(0)); + } + return; } - return; - } } b = (char)ib; @@ -712,17 +712,17 @@ static int try_send(message_t *msg, { switch (errno) { - case EAGAIN: - return 0; + case EAGAIN: + return 0; - default: - debug(2, - L"Error while sending universal variable message to fd %d. Closing connection", - fd); - if (debug_level > 2) - wperror(L"write"); + default: + debug(2, + L"Error while sending universal variable message to fd %d. Closing connection", + fd); + if (debug_level > 2) + wperror(L"write"); - return -1; + return -1; } } msg->count--; @@ -743,18 +743,18 @@ void try_send_all(connection_t *c) { switch (try_send(c->unsent->front(), c->fd)) { - case 1: - c->unsent->pop(); - break; + case 1: + c->unsent->pop(); + break; - case 0: - debug(4, - L"Socket full, send rest later"); - return; + case 0: + debug(4, + L"Socket full, send rest later"); + return; - case -1: - c->killme = 1; - return; + case -1: + c->killme = 1; + return; } } } @@ -852,44 +852,44 @@ message_t *create_message(fish_message_type_t type, switch (type) { - case SET: - case SET_EXPORT: - { - if (!val_in) + case SET: + case SET_EXPORT: { - val_in=L""; + if (!val_in) + { + val_in=L""; + } + + wcstring esc = full_escape(val_in); + char *val = wcs2utf(esc.c_str()); + set_body(msg, (type==SET?SET_MBS:SET_EXPORT_MBS), " ", key, ":", val, "\n", NULL); + free(val); + + break; } - wcstring esc = full_escape(val_in); - char *val = wcs2utf(esc.c_str()); - set_body(msg, (type==SET?SET_MBS:SET_EXPORT_MBS), " ", key, ":", val, "\n", NULL); - free(val); + case ERASE: + { + set_body(msg, ERASE_MBS, " ", key, "\n", NULL); + break; + } - break; - } + case BARRIER: + { + set_body(msg, BARRIER_MBS, "\n", NULL); + break; + } - case ERASE: - { - set_body(msg, ERASE_MBS, " ", key, "\n", NULL); - break; - } + case BARRIER_REPLY: + { + set_body(msg, BARRIER_REPLY_MBS, "\n", NULL); + break; + } - case BARRIER: - { - set_body(msg, BARRIER_MBS, "\n", NULL); - break; - } - - case BARRIER_REPLY: - { - set_body(msg, BARRIER_REPLY_MBS, "\n", NULL); - break; - } - - default: - { - debug(0, L"create_message: Unknown message type"); - } + default: + { + debug(0, L"create_message: Unknown message type"); + } } free(key); diff --git a/event.cpp b/event.cpp index 8f4f6b173..8d6fdbc36 100644 --- a/event.cpp +++ b/event.cpp @@ -107,24 +107,24 @@ static int event_match(const event_t *classv, const event_t *instance) switch (classv->type) { - case EVENT_SIGNAL: - if (classv->param1.signal == EVENT_ANY_SIGNAL) - return 1; - return classv->param1.signal == instance->param1.signal; + case EVENT_SIGNAL: + if (classv->param1.signal == EVENT_ANY_SIGNAL) + return 1; + return classv->param1.signal == instance->param1.signal; - case EVENT_VARIABLE: - return instance->str_param1 == classv->str_param1; + case EVENT_VARIABLE: + return instance->str_param1 == classv->str_param1; - case EVENT_EXIT: - if (classv->param1.pid == EVENT_ANY_PID) - return 1; - return classv->param1.pid == instance->param1.pid; + case EVENT_EXIT: + if (classv->param1.pid == EVENT_ANY_PID) + return 1; + return classv->param1.pid == instance->param1.pid; - case EVENT_JOB_ID: - return classv->param1.job_id == instance->param1.job_id; + case EVENT_JOB_ID: + return classv->param1.job_id == instance->param1.job_id; - case EVENT_GENERIC: - return instance->str_param1 == classv->str_param1; + case EVENT_GENERIC: + return instance->str_param1 == classv->str_param1; } @@ -176,48 +176,48 @@ wcstring event_get_desc(const event_t *e) switch (e->type) { - case EVENT_SIGNAL: - result = format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal), signal_get_desc(e->param1.signal)); - break; + case EVENT_SIGNAL: + result = format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal), signal_get_desc(e->param1.signal)); + break; - case EVENT_VARIABLE: - result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str()); - break; + case EVENT_VARIABLE: + result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str()); + break; - case EVENT_EXIT: - if (e->param1.pid > 0) + case EVENT_EXIT: + if (e->param1.pid > 0) + { + result = format_string(_(L"exit handler for process %d"), e->param1.pid); + } + else + { + job_t *j = job_get_from_pid(-e->param1.pid); + if (j) + result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr()); + else + result = format_string(_(L"exit handler for job with process group %d"), -e->param1.pid); + } + + break; + + case EVENT_JOB_ID: { - result = format_string(_(L"exit handler for process %d"), e->param1.pid); - } - else - { - job_t *j = job_get_from_pid(-e->param1.pid); + job_t *j = job_get(e->param1.job_id); if (j) result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr()); else - result = format_string(_(L"exit handler for job with process group %d"), -e->param1.pid); + result = format_string(_(L"exit handler for job with job id %d"), e->param1.job_id); + + break; } - break; + case EVENT_GENERIC: + result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str()); + break; - case EVENT_JOB_ID: - { - job_t *j = job_get(e->param1.job_id); - if (j) - result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr()); - else - result = format_string(_(L"exit handler for job with job id %d"), e->param1.job_id); - - break; - } - - case EVENT_GENERIC: - result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str()); - break; - - default: - result = format_string(_(L"Unknown event type")); - break; + default: + result = format_string(_(L"Unknown event type")); + break; } diff --git a/exec.cpp b/exec.cpp index 2f1506f14..3063b02e6 100644 --- a/exec.cpp +++ b/exec.cpp @@ -383,51 +383,51 @@ static bool io_transmogrify(const io_chain_t &in_chain, io_chain_t &out_chain, s switch (in->io_mode) { - default: - /* Unknown type, should never happen */ - fprintf(stderr, "Unknown io_mode %ld\n", (long)in->io_mode); - abort(); - break; + default: + /* Unknown type, should never happen */ + fprintf(stderr, "Unknown io_mode %ld\n", (long)in->io_mode); + abort(); + break; - /* - These redirections don't need transmogrification. They can be passed through. - */ - case IO_PIPE: - case IO_FD: - case IO_BUFFER: - case IO_CLOSE: - { - out = new io_data_t(*in); - break; - } - - /* - Transmogrify file redirections - */ - case IO_FILE: - { - out = new io_data_t(); - out->fd = in->fd; - out->io_mode = IO_FD; - out->param2.close_old = 1; - - int fd; - if ((fd=open(in->filename_cstr, in->param2.flags, OPEN_MASK))==-1) + /* + These redirections don't need transmogrification. They can be passed through. + */ + case IO_PIPE: + case IO_FD: + case IO_BUFFER: + case IO_CLOSE: { - debug(1, - FILE_ERROR, - in->filename_cstr); - - wperror(L"open"); - success = false; + out = new io_data_t(*in); break; } - opened_fds.push_back(fd); - out->param1.old_fd = fd; + /* + Transmogrify file redirections + */ + case IO_FILE: + { + out = new io_data_t(); + out->fd = in->fd; + out->io_mode = IO_FD; + out->param2.close_old = 1; - break; - } + int fd; + if ((fd=open(in->filename_cstr, in->param2.flags, OPEN_MASK))==-1) + { + debug(1, + FILE_ERROR, + in->filename_cstr); + + wperror(L"open"); + success = false; + break; + } + + opened_fds.push_back(fd); + out->param1.old_fd = fd; + + break; + } } /* Record this IO redirection even if we failed (so we can free it) */ @@ -784,234 +784,234 @@ void exec(parser_t &parser, job_t *j) switch (p->type) { - case INTERNAL_FUNCTION: - { - wchar_t * def=0; - int shadows; - - - /* - Calls to function_get_definition might need to - source a file as a part of autoloading, hence there - must be no blocks. - */ - - signal_unblock(); - wcstring orig_def; - function_get_definition(p->argv0(), &orig_def); - - // function_get_named_arguments may trigger autoload, which deallocates the orig_def. - // We should make function_get_definition return a wcstring (but how to handle NULL...) - if (! orig_def.empty()) - def = wcsdup(orig_def.c_str()); - - wcstring_list_t named_arguments = function_get_named_arguments(p->argv0()); - shadows = function_get_shadows(p->argv0()); - - signal_block(); - - if (def == NULL) + case INTERNAL_FUNCTION: { - debug(0, _(L"Unknown function '%ls'"), p->argv0()); - break; - } - function_block_t *newv = new function_block_t(p, p->argv0(), shadows); - parser.push_block(newv); + wchar_t * def=0; + int shadows; - /* - set_argv might trigger an event - handler, hence we need to unblock - signals. - */ - signal_unblock(); - parse_util_set_argv(p->get_argv()+1, named_arguments); - signal_block(); - - parser.forbid_function(p->argv0()); - - if (p->next) - { - io_buffer = io_buffer_create(0); - j->io.push_back(io_buffer); - } - - internal_exec_helper(parser, def, TOP, j->io); - - parser.allow_function(); - parser.pop_block(); - free(def); - - break; - } - - case INTERNAL_BLOCK: - { - if (p->next) - { - io_buffer = io_buffer_create(0); - j->io.push_back(io_buffer); - } - - internal_exec_helper(parser, p->argv0(), TOP, j->io); - break; - - } - - case INTERNAL_BUILTIN: - { - int builtin_stdin=0; - int fg; - int close_stdin=0; - - /* - If this is the first process, check the io - redirections and see where we should be reading - from. - */ - if (p == j->first_process) - { - const io_data_t *in = io_chain_get(j->io, 0); - - if (in) - { - switch (in->io_mode) - { - - case IO_FD: - { - builtin_stdin = in->param1.old_fd; - break; - } - case IO_PIPE: - { - builtin_stdin = in->param1.pipe_fd[0]; - break; - } - - case IO_FILE: - { - /* Do not set CLO_EXEC because child needs access */ - builtin_stdin=open(in->filename_cstr, - in->param2.flags, OPEN_MASK); - if (builtin_stdin == -1) - { - debug(1, - FILE_ERROR, - in->filename_cstr); - wperror(L"open"); - } - else - { - close_stdin = 1; - } - - break; - } - - case IO_CLOSE: - { - /* - FIXME: - - When - requesting - that - stdin - be - closed, - we - really - don't - do - anything. How - should - this - be - handled? - */ - builtin_stdin = -1; - - break; - } - - default: - { - builtin_stdin=-1; - debug(1, - _(L"Unknown input redirection type %d"), - in->io_mode); - break; - } - - } - } - } - else - { - builtin_stdin = pipe_read.param1.pipe_fd[0]; - } - - if (builtin_stdin == -1) - { - exec_error = true; - break; - } - else - { - int old_out = builtin_out_redirect; - int old_err = builtin_err_redirect; - /* - Since this may be the foreground job, and since - a builtin may execute another foreground job, - we need to pretend to suspend this job while - running the builtin, in order to avoid a - situation where two jobs are running at once. - - The reason this is done here, and not by the - relevant builtins, is that this way, the - builtin does not need to know what job it is - part of. It could probably figure that out by - walking the job list, but it seems more robust - to make exec handle things. + Calls to function_get_definition might need to + source a file as a part of autoloading, hence there + must be no blocks. */ - builtin_push_io(parser, builtin_stdin); - - builtin_out_redirect = has_fd(j->io, 1); - builtin_err_redirect = has_fd(j->io, 2); - - fg = job_get_flag(j, JOB_FOREGROUND); - job_set_flag(j, JOB_FOREGROUND, 0); - signal_unblock(); + wcstring orig_def; + function_get_definition(p->argv0(), &orig_def); - p->status = builtin_run(parser, p->get_argv(), j->io); + // function_get_named_arguments may trigger autoload, which deallocates the orig_def. + // We should make function_get_definition return a wcstring (but how to handle NULL...) + if (! orig_def.empty()) + def = wcsdup(orig_def.c_str()); - builtin_out_redirect=old_out; - builtin_err_redirect=old_err; + wcstring_list_t named_arguments = function_get_named_arguments(p->argv0()); + shadows = function_get_shadows(p->argv0()); signal_block(); + if (def == NULL) + { + debug(0, _(L"Unknown function '%ls'"), p->argv0()); + break; + } + function_block_t *newv = new function_block_t(p, p->argv0(), shadows); + parser.push_block(newv); + + /* - Restore the fg flag, which is temporarily set to - false during builtin execution so as not to confuse - some job-handling builtins. + set_argv might trigger an event + handler, hence we need to unblock + signals. */ - job_set_flag(j, JOB_FOREGROUND, fg); + signal_unblock(); + parse_util_set_argv(p->get_argv()+1, named_arguments); + signal_block(); + + parser.forbid_function(p->argv0()); + + if (p->next) + { + io_buffer = io_buffer_create(0); + j->io.push_back(io_buffer); + } + + internal_exec_helper(parser, def, TOP, j->io); + + parser.allow_function(); + parser.pop_block(); + free(def); + + break; } - /* - If stdin has been redirected, close the redirection - stream. - */ - if (close_stdin) + case INTERNAL_BLOCK: { - exec_close(builtin_stdin); + if (p->next) + { + io_buffer = io_buffer_create(0); + j->io.push_back(io_buffer); + } + + internal_exec_helper(parser, p->argv0(), TOP, j->io); + break; + + } + + case INTERNAL_BUILTIN: + { + int builtin_stdin=0; + int fg; + int close_stdin=0; + + /* + If this is the first process, check the io + redirections and see where we should be reading + from. + */ + if (p == j->first_process) + { + const io_data_t *in = io_chain_get(j->io, 0); + + if (in) + { + switch (in->io_mode) + { + + case IO_FD: + { + builtin_stdin = in->param1.old_fd; + break; + } + case IO_PIPE: + { + builtin_stdin = in->param1.pipe_fd[0]; + break; + } + + case IO_FILE: + { + /* Do not set CLO_EXEC because child needs access */ + builtin_stdin=open(in->filename_cstr, + in->param2.flags, OPEN_MASK); + if (builtin_stdin == -1) + { + debug(1, + FILE_ERROR, + in->filename_cstr); + wperror(L"open"); + } + else + { + close_stdin = 1; + } + + break; + } + + case IO_CLOSE: + { + /* + FIXME: + + When + requesting + that + stdin + be + closed, + we + really + don't + do + anything. How + should + this + be + handled? + */ + builtin_stdin = -1; + + break; + } + + default: + { + builtin_stdin=-1; + debug(1, + _(L"Unknown input redirection type %d"), + in->io_mode); + break; + } + + } + } + } + else + { + builtin_stdin = pipe_read.param1.pipe_fd[0]; + } + + if (builtin_stdin == -1) + { + exec_error = true; + break; + } + else + { + int old_out = builtin_out_redirect; + int old_err = builtin_err_redirect; + + /* + Since this may be the foreground job, and since + a builtin may execute another foreground job, + we need to pretend to suspend this job while + running the builtin, in order to avoid a + situation where two jobs are running at once. + + The reason this is done here, and not by the + relevant builtins, is that this way, the + builtin does not need to know what job it is + part of. It could probably figure that out by + walking the job list, but it seems more robust + to make exec handle things. + */ + + builtin_push_io(parser, builtin_stdin); + + builtin_out_redirect = has_fd(j->io, 1); + builtin_err_redirect = has_fd(j->io, 2); + + fg = job_get_flag(j, JOB_FOREGROUND); + job_set_flag(j, JOB_FOREGROUND, 0); + + signal_unblock(); + + p->status = builtin_run(parser, p->get_argv(), j->io); + + builtin_out_redirect=old_out; + builtin_err_redirect=old_err; + + signal_block(); + + /* + Restore the fg flag, which is temporarily set to + false during builtin execution so as not to confuse + some job-handling builtins. + */ + job_set_flag(j, JOB_FOREGROUND, fg); + } + + /* + If stdin has been redirected, close the redirection + stream. + */ + if (close_stdin) + { + exec_close(builtin_stdin); + } + break; } - break; - } } if (exec_error) @@ -1022,56 +1022,109 @@ void exec(parser_t &parser, job_t *j) switch (p->type) { - case INTERNAL_BLOCK: - case INTERNAL_FUNCTION: - { - int status = proc_get_last_status(); - - /* - Handle output from a block or function. This usually - means do nothing, but in the case of pipes, we have - to buffer such io, since otherwise the internal pipe - buffer might overflow. - */ - if (!io_buffer) + case INTERNAL_BLOCK: + case INTERNAL_FUNCTION: { + int status = proc_get_last_status(); + /* - No buffer, so we exit directly. This means we - have to manually set the exit status. + Handle output from a block or function. This usually + means do nothing, but in the case of pipes, we have + to buffer such io, since otherwise the internal pipe + buffer might overflow. */ - if (p->next == 0) + if (!io_buffer) { - proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); + /* + No buffer, so we exit directly. This means we + have to manually set the exit status. + */ + if (p->next == 0) + { + proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); + } + p->completed = 1; + break; } - p->completed = 1; + + io_remove(j->io, io_buffer); + + io_buffer_read(io_buffer); + + const char *buffer = io_buffer->out_buffer_ptr(); + size_t count = io_buffer->out_buffer_size(); + + if (io_buffer->out_buffer_size() > 0) + { + /* We don't have to drain threads here because our child process is simple */ + if (g_log_forks) + { + printf("Executing fork for internal block or function for '%ls'\n", p->argv0()); + } + pid = execute_fork(false); + if (pid == 0) + { + + /* + This is the child process. Write out the contents of the pipeline. + */ + p->pid = getpid(); + setup_child_process(j, p); + + exec_write_and_exit(io_buffer->fd, buffer, count, status); + } + else + { + /* + This is the parent process. Store away + information on the child, and possibly give + it control over the terminal. + */ + p->pid = pid; + set_child_group(j, p, 0); + + } + + } + else + { + if (p->next == 0) + { + proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); + } + p->completed = 1; + } + + io_buffer_destroy(io_buffer); + + io_buffer=0; break; + } - io_remove(j->io, io_buffer); - io_buffer_read(io_buffer); - - const char *buffer = io_buffer->out_buffer_ptr(); - size_t count = io_buffer->out_buffer_size(); - - if (io_buffer->out_buffer_size() > 0) + case INTERNAL_BUFFER: { + + const char *buffer = input_redirect->out_buffer_ptr(); + size_t count = input_redirect->out_buffer_size(); + /* We don't have to drain threads here because our child process is simple */ if (g_log_forks) { - printf("Executing fork for internal block or function for '%ls'\n", p->argv0()); + printf("fork #%d: Executing fork for internal buffer for '%ls'\n", g_fork_count, p->argv0() ? p->argv0() : L"(null)"); } pid = execute_fork(false); if (pid == 0) { - /* - This is the child process. Write out the contents of the pipeline. + This is the child process. Write out the + contents of the pipeline. */ p->pid = getpid(); setup_child_process(j, p); - exec_write_and_exit(io_buffer->fd, buffer, count, status); + exec_write_and_exit(1, buffer, count, 0); } else { @@ -1082,283 +1135,230 @@ void exec(parser_t &parser, job_t *j) */ p->pid = pid; set_child_group(j, p, 0); - } - } - else - { - if (p->next == 0) - { - proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); - } - p->completed = 1; - } - - io_buffer_destroy(io_buffer); - - io_buffer=0; - break; - - } - - - case INTERNAL_BUFFER: - { - - const char *buffer = input_redirect->out_buffer_ptr(); - size_t count = input_redirect->out_buffer_size(); - - /* We don't have to drain threads here because our child process is simple */ - if (g_log_forks) - { - printf("fork #%d: Executing fork for internal buffer for '%ls'\n", g_fork_count, p->argv0() ? p->argv0() : L"(null)"); - } - pid = execute_fork(false); - if (pid == 0) - { - /* - This is the child process. Write out the - contents of the pipeline. - */ - p->pid = getpid(); - setup_child_process(j, p); - - exec_write_and_exit(1, buffer, count, 0); - } - else - { - /* - This is the parent process. Store away - information on the child, and possibly give - it control over the terminal. - */ - p->pid = pid; - set_child_group(j, p, 0); - } - - break; - } - - case INTERNAL_BUILTIN: - { - int skip_fork; - - /* - Handle output from builtin commands. In the general - case, this means forking of a worker process, that - will write out the contents of the stdout and stderr - buffers to the correct file descriptor. Since - forking is expensive, fish tries to avoid it wehn - possible. - */ - - /* - If a builtin didn't produce any output, and it is - not inside a pipeline, there is no need to fork - */ - skip_fork = - get_stdout_buffer().empty() && - get_stderr_buffer().empty() && - !p->next; - - /* - If the output of a builtin is to be sent to an internal - buffer, there is no need to fork. This helps out the - performance quite a bit in complex completion code. - */ - - io_data_t *io = io_chain_get(j->io, 1); - bool buffer_stdout = io && io->io_mode == IO_BUFFER; - - if ((get_stderr_buffer().empty()) && - (!p->next) && - (! get_stdout_buffer().empty()) && - (buffer_stdout)) - { - const std::string res = wcs2string(get_stdout_buffer()); - io->out_buffer_append(res.c_str(), res.size()); - skip_fork = 1; - } - - if (! skip_fork && j->io.empty()) - { - /* PCA for some reason, fish forks a lot, even for basic builtins like echo just to write out their buffers. I'm certain a lot of this is unnecessary, but I am not sure exactly when. If j->io is NULL, then it means there's no pipes or anything, so we can certainly just write out our data. Beyond that, we may be able to do the same if io_get returns 0 for STDOUT_FILENO and STDERR_FILENO. */ - if (g_log_forks) - { - printf("fork #-: Skipping fork for internal builtin for '%ls'\n", p->argv0()); - } - const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer(); - char *outbuff = wcs2str(out.c_str()), *errbuff = wcs2str(err.c_str()); - do_builtin_io(outbuff, errbuff); - free(outbuff); - free(errbuff); - skip_fork = 1; - } - - for (io_chain_t::iterator iter = j->io.begin(); iter != j->io.end(); iter++) - { - io_data_t *tmp_io = *iter; - if (tmp_io->io_mode == IO_FILE && strcmp(tmp_io->filename_cstr, "/dev/null") != 0) - { - skip_fork = 0; - break; - } - } - - - if (skip_fork) - { - p->completed=1; - if (p->next == 0) - { - debug(3, L"Set status of %ls to %d using short circut", j->command_wcstr(), p->status); - - int status = p->status; - proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); - } break; } - - /* Ok, unfortunatly, we have to do a real fork. Bummer. We work hard to make sure we don't have to wait for all our threads to exit, by arranging things so that we don't have to allocate memory or do anything except system calls in the child. */ - - /* Get the strings we'll write before we fork (since they call malloc) */ - const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer(); - char *outbuff = wcs2str(out.c_str()), *errbuff = wcs2str(err.c_str()); - - fflush(stdout); - fflush(stderr); - if (g_log_forks) - { - printf("fork #%d: Executing fork for internal builtin for '%ls'\n", g_fork_count, p->argv0()); - io_print(io_chain_t(io)); - } - pid = execute_fork(false); - if (pid == 0) + case INTERNAL_BUILTIN: { + int skip_fork; + /* - This is the child process. Setup redirections, - print correct output to stdout and stderr, and - then exit. + Handle output from builtin commands. In the general + case, this means forking of a worker process, that + will write out the contents of the stdout and stderr + buffers to the correct file descriptor. Since + forking is expensive, fish tries to avoid it wehn + possible. */ - p->pid = getpid(); - setup_child_process(j, p); - do_builtin_io(outbuff, errbuff); - exit_without_destructors(p->status); + /* + If a builtin didn't produce any output, and it is + not inside a pipeline, there is no need to fork + */ + skip_fork = + get_stdout_buffer().empty() && + get_stderr_buffer().empty() && + !p->next; + + /* + If the output of a builtin is to be sent to an internal + buffer, there is no need to fork. This helps out the + performance quite a bit in complex completion code. + */ + + io_data_t *io = io_chain_get(j->io, 1); + bool buffer_stdout = io && io->io_mode == IO_BUFFER; + + if ((get_stderr_buffer().empty()) && + (!p->next) && + (! get_stdout_buffer().empty()) && + (buffer_stdout)) + { + const std::string res = wcs2string(get_stdout_buffer()); + io->out_buffer_append(res.c_str(), res.size()); + skip_fork = 1; + } + + if (! skip_fork && j->io.empty()) + { + /* PCA for some reason, fish forks a lot, even for basic builtins like echo just to write out their buffers. I'm certain a lot of this is unnecessary, but I am not sure exactly when. If j->io is NULL, then it means there's no pipes or anything, so we can certainly just write out our data. Beyond that, we may be able to do the same if io_get returns 0 for STDOUT_FILENO and STDERR_FILENO. */ + if (g_log_forks) + { + printf("fork #-: Skipping fork for internal builtin for '%ls'\n", p->argv0()); + } + const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer(); + char *outbuff = wcs2str(out.c_str()), *errbuff = wcs2str(err.c_str()); + do_builtin_io(outbuff, errbuff); + free(outbuff); + free(errbuff); + skip_fork = 1; + } + + for (io_chain_t::iterator iter = j->io.begin(); iter != j->io.end(); iter++) + { + io_data_t *tmp_io = *iter; + if (tmp_io->io_mode == IO_FILE && strcmp(tmp_io->filename_cstr, "/dev/null") != 0) + { + skip_fork = 0; + break; + } + } + + + if (skip_fork) + { + p->completed=1; + if (p->next == 0) + { + debug(3, L"Set status of %ls to %d using short circut", j->command_wcstr(), p->status); + + int status = p->status; + proc_set_last_status(job_get_flag(j, JOB_NEGATE)?(!status):status); + } + break; + } + + + /* Ok, unfortunatly, we have to do a real fork. Bummer. We work hard to make sure we don't have to wait for all our threads to exit, by arranging things so that we don't have to allocate memory or do anything except system calls in the child. */ + + /* Get the strings we'll write before we fork (since they call malloc) */ + const wcstring &out = get_stdout_buffer(), &err = get_stderr_buffer(); + char *outbuff = wcs2str(out.c_str()), *errbuff = wcs2str(err.c_str()); + + fflush(stdout); + fflush(stderr); + if (g_log_forks) + { + printf("fork #%d: Executing fork for internal builtin for '%ls'\n", g_fork_count, p->argv0()); + io_print(io_chain_t(io)); + } + pid = execute_fork(false); + if (pid == 0) + { + /* + This is the child process. Setup redirections, + print correct output to stdout and stderr, and + then exit. + */ + p->pid = getpid(); + setup_child_process(j, p); + do_builtin_io(outbuff, errbuff); + exit_without_destructors(p->status); + + } + else + { + /* Free the strings in the parent */ + free(outbuff); + free(errbuff); + + /* + This is the parent process. Store away + information on the child, and possibly give + it control over the terminal. + */ + p->pid = pid; + + set_child_group(j, p, 0); + + } + + break; } - else + + case EXTERNAL: { - /* Free the strings in the parent */ - free(outbuff); - free(errbuff); + /* Get argv and envv before we fork */ + null_terminated_array_t argv_array = convert_wide_array_to_narrow(p->get_argv_array()); + + null_terminated_array_t envv_array; + env_export_arr(false, envv_array); + + char **envv = envv_array.get(); + char **argv = argv_array.get(); + + std::string actual_cmd_str = wcs2string(p->actual_cmd); + const char *actual_cmd = actual_cmd_str.c_str(); + + const wchar_t *reader_current_filename(); + if (g_log_forks) + { + const wchar_t *file = reader_current_filename(); + const wchar_t *func = parser_t::principal_parser().is_function(); + printf("fork #%d: forking for '%s' in '%ls:%ls'\n", g_fork_count, actual_cmd, file ? file : L"", func ? func : L"?"); + + fprintf(stderr, "IO chain for %s:\n", actual_cmd); + io_print(j->io); + } + +#if FISH_USE_POSIX_SPAWN + /* Prefer to use posix_spawn, since it's faster on some systems like OS X */ + bool use_posix_spawn = g_use_posix_spawn && can_use_posix_spawn_for_job(j, p); + if (use_posix_spawn) + { + /* Create posix spawn attributes and actions */ + posix_spawnattr_t attr = posix_spawnattr_t(); + posix_spawn_file_actions_t actions = posix_spawn_file_actions_t(); + bool made_it = fork_actions_make_spawn_properties(&attr, &actions, j, p); + if (made_it) + { + /* We successfully made the attributes and actions; actually call posix_spawn */ + int spawn_ret = posix_spawn(&pid, actual_cmd, &actions, &attr, argv, envv); + + /* This usleep can be used to test for various race conditions (https://github.com/fish-shell/fish-shell/issues/360) */ + //usleep(10000); + + if (spawn_ret != 0) + { + safe_report_exec_error(spawn_ret, actual_cmd, argv, envv); + /* Make sure our pid isn't set */ + pid = 0; + } + + /* Clean up our actions */ + posix_spawn_file_actions_destroy(&actions); + posix_spawnattr_destroy(&attr); + } + + /* A 0 pid means we failed to posix_spawn. Since we have no pid, we'll never get told when it's exited, so we have to mark the process as failed. */ + if (pid == 0) + { + job_mark_process_as_failed(j, p); + exec_error = true; + } + } + else +#endif + { + pid = execute_fork(false); + if (pid == 0) + { + /* This is the child process. */ + p->pid = getpid(); + setup_child_process(j, p); + safe_launch_process(p, actual_cmd, argv, envv); + + /* + safe_launch_process _never_ returns... + */ + } + } + /* This is the parent process. Store away - information on the child, and possibly give + information on the child, and possibly fice it control over the terminal. */ p->pid = pid; set_child_group(j, p, 0); + break; } - break; - } - - case EXTERNAL: - { - /* Get argv and envv before we fork */ - null_terminated_array_t argv_array = convert_wide_array_to_narrow(p->get_argv_array()); - - null_terminated_array_t envv_array; - env_export_arr(false, envv_array); - - char **envv = envv_array.get(); - char **argv = argv_array.get(); - - std::string actual_cmd_str = wcs2string(p->actual_cmd); - const char *actual_cmd = actual_cmd_str.c_str(); - - const wchar_t *reader_current_filename(); - if (g_log_forks) - { - const wchar_t *file = reader_current_filename(); - const wchar_t *func = parser_t::principal_parser().is_function(); - printf("fork #%d: forking for '%s' in '%ls:%ls'\n", g_fork_count, actual_cmd, file ? file : L"", func ? func : L"?"); - - fprintf(stderr, "IO chain for %s:\n", actual_cmd); - io_print(j->io); - } - -#if FISH_USE_POSIX_SPAWN - /* Prefer to use posix_spawn, since it's faster on some systems like OS X */ - bool use_posix_spawn = g_use_posix_spawn && can_use_posix_spawn_for_job(j, p); - if (use_posix_spawn) - { - /* Create posix spawn attributes and actions */ - posix_spawnattr_t attr = posix_spawnattr_t(); - posix_spawn_file_actions_t actions = posix_spawn_file_actions_t(); - bool made_it = fork_actions_make_spawn_properties(&attr, &actions, j, p); - if (made_it) - { - /* We successfully made the attributes and actions; actually call posix_spawn */ - int spawn_ret = posix_spawn(&pid, actual_cmd, &actions, &attr, argv, envv); - - /* This usleep can be used to test for various race conditions (https://github.com/fish-shell/fish-shell/issues/360) */ - //usleep(10000); - - if (spawn_ret != 0) - { - safe_report_exec_error(spawn_ret, actual_cmd, argv, envv); - /* Make sure our pid isn't set */ - pid = 0; - } - - /* Clean up our actions */ - posix_spawn_file_actions_destroy(&actions); - posix_spawnattr_destroy(&attr); - } - - /* A 0 pid means we failed to posix_spawn. Since we have no pid, we'll never get told when it's exited, so we have to mark the process as failed. */ - if (pid == 0) - { - job_mark_process_as_failed(j, p); - exec_error = true; - } - } - else -#endif - { - pid = execute_fork(false); - if (pid == 0) - { - /* This is the child process. */ - p->pid = getpid(); - setup_child_process(j, p); - safe_launch_process(p, actual_cmd, argv, envv); - - /* - safe_launch_process _never_ returns... - */ - } - } - - - /* - This is the parent process. Store away - information on the child, and possibly fice - it control over the terminal. - */ - p->pid = pid; - - set_child_group(j, p, 0); - - break; - } - } if (p->type == INTERNAL_BUILTIN) diff --git a/expand.cpp b/expand.cpp index f81c4c0ee..838eb5796 100644 --- a/expand.cpp +++ b/expand.cpp @@ -169,18 +169,18 @@ static int is_quotable(const wchar_t *str) { switch (*str) { - case 0: - return 1; + case 0: + return 1; - case L'\n': - case L'\t': - case L'\r': - case L'\b': - case L'\x1b': - return 0; + case L'\n': + case L'\t': + case L'\r': + case L'\b': + case L'\x1b': + return 0; - default: - return is_quotable(str+1); + default: + return is_quotable(str+1); } return 0; @@ -201,35 +201,15 @@ wcstring expand_escape_variable(const wcstring &in) switch (lst.size()) { - case 0: - buff.append(L"''"); - break; + case 0: + buff.append(L"''"); + break; - case 1: - { - const wcstring &el = lst.at(0); + case 1: + { + const wcstring &el = lst.at(0); - if (el.find(L' ') != wcstring::npos && is_quotable(el)) - { - buff.append(L"'"); - buff.append(el); - buff.append(L"'"); - } - else - { - buff.append(escape_string(el, 1)); - } - break; - } - default: - { - for (size_t j=0; j &output, expa switch (wc_res) { - case 0: - { - if (!(flags & ACCEPT_INCOMPLETE)) + case 0: { - if (res == EXPAND_OK) - res = EXPAND_WILDCARD_NO_MATCH; + if (!(flags & ACCEPT_INCOMPLETE)) + { + if (res == EXPAND_OK) + res = EXPAND_WILDCARD_NO_MATCH; + break; + } + } + + case 1: + { + size_t j; + res = EXPAND_WILDCARD_MATCH; + sort_completions(*out); + + for (j=0; j< out->size(); j++) + { + output.push_back(out->at(j)); + } + out->clear(); break; } - } - case 1: - { - size_t j; - res = EXPAND_WILDCARD_MATCH; - sort_completions(*out); - - for (j=0; j< out->size(); j++) + case -1: { - output.push_back(out->at(j)); + return EXPAND_ERROR; } - out->clear(); - break; - } - - case -1: - { - return EXPAND_ERROR; - } } } diff --git a/fallback.cpp b/fallback.cpp index 65fccf740..69348db1e 100644 --- a/fallback.cpp +++ b/fallback.cpp @@ -203,338 +203,338 @@ static int vgwprintf(void (*writer)(wchar_t), switch (*filter) { - case L'l': - /* Long variable */ - is_long++; - filter++; - break; + case L'l': + /* Long variable */ + is_long++; + filter++; + break; - case L'*': - /* Set minimum field width */ - width = va_arg(va, int); - filter++; - break; + case L'*': + /* Set minimum field width */ + width = va_arg(va, int); + filter++; + break; - case L'-': - filter++; - pad_left=0; - break; + case L'-': + filter++; + pad_left=0; + break; - case L'.': - /* - Set precision. - */ - filter++; - if (*filter == L'*') - { - precision = va_arg(va, int); - } - else - { - precision=0; - while ((*filter >= L'0') && (*filter <= L'9')) + case L'.': + /* + Set precision. + */ + filter++; + if (*filter == L'*') { - precision=10*precision+(*filter++ - L'0'); + precision = va_arg(va, int); } - } - break; + else + { + precision=0; + while ((*filter >= L'0') && (*filter <= L'9')) + { + precision=10*precision+(*filter++ - L'0'); + } + } + break; - default: - loop=0; - break; + default: + loop=0; + break; } } switch (*filter) { - case L'c': - { - wchar_t c; - - if ((width >= 0) && pad_left) + case L'c': { - pad(writer, width-1); - count += maxi(width-1, 0); - } + wchar_t c; - c = is_long?va_arg(va, wint_t):btowc(va_arg(va, int)); - if (precision != 0) - writer(c); - - - if ((width >= 0) && !pad_left) - { - pad(writer, width-1); - count += maxi(width-1, 0); - } - - count++; - - break; - } - case L's': - { - - wchar_t *ss=0; - if (is_long) - { - ss = va_arg(va, wchar_t *); - } - else - { - char *ns = va_arg(va, char*); - - if (ns) + if ((width >= 0) && pad_left) { - ss = str2wcs(ns); + pad(writer, width-1); + count += maxi(width-1, 0); } - } - if (!ss) - { - return -1; - } + c = is_long?va_arg(va, wint_t):btowc(va_arg(va, int)); + if (precision != 0) + writer(c); - if ((width >= 0) && pad_left) - { - pad(writer, width-wcslen(ss)); - count += maxi(width-wcslen(ss), 0); - } - wchar_t *s=ss; - int precount = count; + if ((width >= 0) && !pad_left) + { + pad(writer, width-1); + count += maxi(width-1, 0); + } - while (*s) - { - if ((precision > 0) && (precision <= (count-precount))) - break; - - writer(*(s++)); count++; - } - if ((width >= 0) && !pad_left) - { - pad(writer, width-wcslen(ss)); - count += maxi(width-wcslen(ss), 0); - } - - if (!is_long) - free(ss); - - break; - } - - case L'd': - case L'i': - case L'o': - case L'u': - case L'x': - case L'X': - { - char str[33]; - char *pos; - char format[16]; - int len; - - format[0]=0; - strcat(format, "%"); - if (precision >= 0) - strcat(format, ".*"); - switch (is_long) - { - case 2: - strcat(format, "ll"); - break; - case 1: - strcat(format, "l"); break; } - - len = strlen(format); - format[len++]=(char)*filter; - format[len]=0; - - switch (*filter) - { - case L'd': - case L'i': + case L's': { - switch (is_long) + wchar_t *ss=0; + if (is_long) { - case 0: + ss = va_arg(va, wchar_t *); + } + else { - int d = va_arg(va, int); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); + char *ns = va_arg(va, char*); - break; + if (ns) + { + ss = str2wcs(ns); + } } - case 1: + if (!ss) { - long d = va_arg(va, long); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); - break; - } - - case 2: - { - long long d = va_arg(va, long long); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); - break; - } - - default: - debug(0, L"Invalid length modifier in string %ls\n", filter_org); return -1; } - break; + if ((width >= 0) && pad_left) + { + pad(writer, width-wcslen(ss)); + count += maxi(width-wcslen(ss), 0); + } + + wchar_t *s=ss; + int precount = count; + + while (*s) + { + if ((precision > 0) && (precision <= (count-precount))) + break; + + writer(*(s++)); + count++; + } + + if ((width >= 0) && !pad_left) + { + pad(writer, width-wcslen(ss)); + count += maxi(width-wcslen(ss), 0); + } + + if (!is_long) + free(ss); + + break; } - case L'u': + case L'd': + case L'i': case L'o': + case L'u': case L'x': case L'X': { + char str[33]; + char *pos; + char format[16]; + int len; + format[0]=0; + strcat(format, "%"); + if (precision >= 0) + strcat(format, ".*"); switch (is_long) { - case 0: - { - unsigned d = va_arg(va, unsigned); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); - break; + case 2: + strcat(format, "ll"); + break; + case 1: + strcat(format, "l"); + break; } - case 1: + len = strlen(format); + format[len++]=(char)*filter; + format[len]=0; + + switch (*filter) { - unsigned long d = va_arg(va, unsigned long); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); - break; + case L'd': + case L'i': + { + + switch (is_long) + { + case 0: + { + int d = va_arg(va, int); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + + break; + } + + case 1: + { + long d = va_arg(va, long); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + break; + } + + case 2: + { + long long d = va_arg(va, long long); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + break; + } + + default: + debug(0, L"Invalid length modifier in string %ls\n", filter_org); + return -1; + } + break; + + } + + case L'u': + case L'o': + case L'x': + case L'X': + { + + switch (is_long) + { + case 0: + { + unsigned d = va_arg(va, unsigned); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + break; + } + + case 1: + { + unsigned long d = va_arg(va, unsigned long); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + break; + } + + case 2: + { + unsigned long long d = va_arg(va, unsigned long long); + if (precision >= 0) + snprintf(str, 32, format, precision, d); + else + snprintf(str, 32, format, d); + break; + } + + default: + debug(0, L"Invalid length modifier in string %ls\n", filter_org); + return -1; + } + break; + + } + + default: + debug(0, L"Invalid filter %ls in string %ls\n", *filter, filter_org); + return -1; + } - case 2: + if ((width >= 0) && pad_left) { - unsigned long long d = va_arg(va, unsigned long long); - if (precision >= 0) - snprintf(str, 32, format, precision, d); - else - snprintf(str, 32, format, d); - break; + int l = maxi(width-strlen(str), 0); + pad(writer, l); + count += l; } - default: - debug(0, L"Invalid length modifier in string %ls\n", filter_org); - return -1; + pos = str; + + while (*pos) + { + writer(*(pos++)); + count++; } + + if ((width >= 0) && !pad_left) + { + int l = maxi(width-strlen(str), 0); + pad(writer, l); + count += l; + } + break; - } + case L'f': + { + char str[32]; + char *pos; + double val = va_arg(va, double); + + if (precision>= 0) + { + if (width>= 0) + { + snprintf(str, 32, "%*.*f", width, precision, val); + } + else + { + snprintf(str, 32, "%.*f", precision, val); + } + } + else + { + if (width>= 0) + { + snprintf(str, 32, "%*f", width, val); + } + else + { + snprintf(str, 32, "%f", val); + } + } + + pos = str; + + while (*pos) + { + writer(*(pos++)); + count++; + } + + break; + } + + case L'n': + { + int *n = va_arg(va, int *); + + *n = count; + break; + } + case L'%': + { + writer('%'); + count++; + break; + } default: - debug(0, L"Invalid filter %ls in string %ls\n", *filter, filter_org); + debug(0, L"Unknown switch %lc in string %ls\n", *filter, filter_org); return -1; - - } - - if ((width >= 0) && pad_left) - { - int l = maxi(width-strlen(str), 0); - pad(writer, l); - count += l; - } - - pos = str; - - while (*pos) - { - writer(*(pos++)); - count++; - } - - if ((width >= 0) && !pad_left) - { - int l = maxi(width-strlen(str), 0); - pad(writer, l); - count += l; - } - - break; - } - - case L'f': - { - char str[32]; - char *pos; - double val = va_arg(va, double); - - if (precision>= 0) - { - if (width>= 0) - { - snprintf(str, 32, "%*.*f", width, precision, val); - } - else - { - snprintf(str, 32, "%.*f", precision, val); - } - } - else - { - if (width>= 0) - { - snprintf(str, 32, "%*f", width, val); - } - else - { - snprintf(str, 32, "%f", val); - } - } - - pos = str; - - while (*pos) - { - writer(*(pos++)); - count++; - } - - break; - } - - case L'n': - { - int *n = va_arg(va, int *); - - *n = count; - break; - } - case L'%': - { - writer('%'); - count++; - break; - } - default: - debug(0, L"Unknown switch %lc in string %ls\n", *filter, filter_org); - return -1; } } else @@ -673,16 +673,16 @@ wint_t fgetwc(FILE *stream) switch (sz) { - case -1: - memset(&state, '\0', sizeof(state)); - return WEOF; + case -1: + memset(&state, '\0', sizeof(state)); + return WEOF; - case -2: - break; - case 0: - return 0; - default: - return res; + case -2: + break; + case 0: + return 0; + default: + return res; } } diff --git a/fish.cpp b/fish.cpp index 08afe9478..0d37d742d 100644 --- a/fish.cpp +++ b/fish.cpp @@ -306,81 +306,81 @@ static int fish_parse_opt(int argc, char **argv, const char **cmd_ptr) switch (opt) { - case 0: - { - break; - } - - case 'c': - { - *cmd_ptr = optarg; - is_interactive_session = 0; - break; - } - - case 'd': - { - char *end; - long tmp; - - errno = 0; - tmp = strtol(optarg, &end, 10); - - if (tmp >= 0 && tmp <=10 && !*end && !errno) + case 0: { - debug_level = (int)tmp; + break; } - else + + case 'c': + { + *cmd_ptr = optarg; + is_interactive_session = 0; + break; + } + + case 'd': + { + char *end; + long tmp; + + errno = 0; + tmp = strtol(optarg, &end, 10); + + if (tmp >= 0 && tmp <=10 && !*end && !errno) + { + debug_level = (int)tmp; + } + else + { + debug(0, _(L"Invalid value '%s' for debug level switch"), optarg); + exit_without_destructors(1); + } + break; + } + + case 'h': + { + *cmd_ptr = "__fish_print_help fish"; + break; + } + + case 'i': + { + force_interactive = 1; + break; + } + + case 'l': + { + is_login=1; + break; + } + + case 'n': + { + no_exec=1; + break; + } + + case 'p': + { + profile = optarg; + break; + } + + case 'v': + { + fwprintf(stderr, + _(L"%s, version %s\n"), + PACKAGE_NAME, + PACKAGE_VERSION); + exit_without_destructors(0); + } + + case '?': { - debug(0, _(L"Invalid value '%s' for debug level switch"), optarg); exit_without_destructors(1); } - break; - } - - case 'h': - { - *cmd_ptr = "__fish_print_help fish"; - break; - } - - case 'i': - { - force_interactive = 1; - break; - } - - case 'l': - { - is_login=1; - break; - } - - case 'n': - { - no_exec=1; - break; - } - - case 'p': - { - profile = optarg; - break; - } - - case 'v': - { - fwprintf(stderr, - _(L"%s, version %s\n"), - PACKAGE_NAME, - PACKAGE_VERSION); - exit_without_destructors(0); - } - - case '?': - { - exit_without_destructors(1); - } } } diff --git a/fish_indent.cpp b/fish_indent.cpp index 4d13c8350..dedf11f27 100644 --- a/fish_indent.cpp +++ b/fish_indent.cpp @@ -101,157 +101,157 @@ static int indent(wcstring &out, const wcstring &in, int flags) switch (type) { - case TOK_STRING: - { - if (is_command) + case TOK_STRING: { - int next_indent = indent; - is_command = 0; + if (is_command) + { + int next_indent = indent; + is_command = 0; - wcstring unesc = last; - unescape_string(unesc, UNESCAPE_SPECIAL); + wcstring unesc = last; + unescape_string(unesc, UNESCAPE_SPECIAL); - if (parser_keywords_is_block(unesc)) - { - next_indent++; - } - else if (unesc == L"else") - { - indent--; - } - /* case should have the same indent level as switch*/ - else if (unesc == L"case") - { - indent--; - } - else if (unesc == L"end") - { - indent--; - next_indent--; - } + if (parser_keywords_is_block(unesc)) + { + next_indent++; + } + else if (unesc == L"else") + { + indent--; + } + /* case should have the same indent level as switch*/ + else if (unesc == L"case") + { + indent--; + } + else if (unesc == L"end") + { + indent--; + next_indent--; + } - if (do_indent && flags && prev_type != TOK_PIPE) + if (do_indent && flags && prev_type != TOK_PIPE) + { + insert_tabs(out, indent); + } + + append_format(out, L"%ls", last); + + indent = next_indent; + + } + else + { + if (prev_type != TOK_REDIRECT_FD) + out.append(L" "); + out.append(last); + } + + break; + } + + case TOK_END: + { + if (prev_type != TOK_END || prev_prev_type != TOK_END) + out.append(L"\n"); + do_indent = 1; + is_command = 1; + break; + } + + case TOK_PIPE: + { + out.append(L" "); + if (last[0] == '2' && !last[1]) + { + out.append(L"^"); + } + else if (last[0] != '1' || last[1]) + { + out.append(last); + out.append(L">"); + } + out.append(L" | "); + is_command = 1; + break; + } + + case TOK_REDIRECT_OUT: + { + out.append(L" "); + if (wcscmp(last, L"2") == 0) + { + out.append(L"^"); + } + else + { + if (wcscmp(last, L"1") != 0) + out.append(last); + out.append(L"> "); + } + break; + } + + case TOK_REDIRECT_APPEND: + { + out.append(L" "); + if (wcscmp(last, L"2") == 0) + { + out.append(L"^^"); + } + else + { + if (wcscmp(last, L"1") != 0) + out.append(last); + out.append(L">> "); + } + break; + } + + case TOK_REDIRECT_IN: + { + out.append(L" "); + if (wcscmp(last, L"0") != 0) + out.append(last); + out.append(L"< "); + break; + } + + case TOK_REDIRECT_FD: + { + out.append(L" "); + if (wcscmp(last, L"1") != 0) + out.append(last); + out.append(L">& "); + break; + } + + case TOK_BACKGROUND: + { + out.append(L"&\n"); + do_indent = 1; + is_command = 1; + break; + } + + case TOK_COMMENT: + { + if (do_indent && flags) { insert_tabs(out, indent); } append_format(out, L"%ls", last); - - indent = next_indent; - + do_indent = 1; + break; } - else + + default: { - if (prev_type != TOK_REDIRECT_FD) - out.append(L" "); - out.append(last); + debug(0, L"Unknown token '%ls'", last); + exit(1); } - - break; - } - - case TOK_END: - { - if (prev_type != TOK_END || prev_prev_type != TOK_END) - out.append(L"\n"); - do_indent = 1; - is_command = 1; - break; - } - - case TOK_PIPE: - { - out.append(L" "); - if (last[0] == '2' && !last[1]) - { - out.append(L"^"); - } - else if (last[0] != '1' || last[1]) - { - out.append(last); - out.append(L">"); - } - out.append(L" | "); - is_command = 1; - break; - } - - case TOK_REDIRECT_OUT: - { - out.append(L" "); - if (wcscmp(last, L"2") == 0) - { - out.append(L"^"); - } - else - { - if (wcscmp(last, L"1") != 0) - out.append(last); - out.append(L"> "); - } - break; - } - - case TOK_REDIRECT_APPEND: - { - out.append(L" "); - if (wcscmp(last, L"2") == 0) - { - out.append(L"^^"); - } - else - { - if (wcscmp(last, L"1") != 0) - out.append(last); - out.append(L">> "); - } - break; - } - - case TOK_REDIRECT_IN: - { - out.append(L" "); - if (wcscmp(last, L"0") != 0) - out.append(last); - out.append(L"< "); - break; - } - - case TOK_REDIRECT_FD: - { - out.append(L" "); - if (wcscmp(last, L"1") != 0) - out.append(last); - out.append(L">& "); - break; - } - - case TOK_BACKGROUND: - { - out.append(L"&\n"); - do_indent = 1; - is_command = 1; - break; - } - - case TOK_COMMENT: - { - if (do_indent && flags) - { - insert_tabs(out, indent); - } - - append_format(out, L"%ls", last); - do_indent = 1; - break; - } - - default: - { - debug(0, L"Unknown token '%ls'", last); - exit(1); - } } prev_prev_type = prev_type; @@ -331,38 +331,38 @@ int main(int argc, char **argv) switch (opt) { - case 0: - { - break; - } + case 0: + { + break; + } - case 'h': - { - print_help("fish_indent", 1); - exit(0); - break; - } + case 'h': + { + print_help("fish_indent", 1); + exit(0); + break; + } - case 'v': - { - fwprintf(stderr, - _(L"%ls, version %s\n"), - program_name, - PACKAGE_VERSION); - exit(0); - } + case 'v': + { + fwprintf(stderr, + _(L"%ls, version %s\n"), + program_name, + PACKAGE_VERSION); + exit(0); + } - case 'i': - { - do_indent = 0; - break; - } + case 'i': + { + do_indent = 0; + break; + } - case '?': - { - exit(1); - } + case '?': + { + exit(1); + } } } diff --git a/fish_pager.cpp b/fish_pager.cpp index d8dd42107..3f24010ce 100644 --- a/fish_pager.cpp +++ b/fish_pager.cpp @@ -717,107 +717,107 @@ static int completion_try_print(int cols, switch (c) { - case LINE_UP: - { - if (pos > 0) + case LINE_UP: { - pos--; - writembs(tparm(cursor_address, 0, 0)); - writembs(scroll_reverse); - completion_print(cols, - width, - pos, - pos+1, - prefix, - is_quoted, - lst); - writembs(tparm(cursor_address, - termsize.ws_row-1, 0)); - writembs(clr_eol); + if (pos > 0) + { + pos--; + writembs(tparm(cursor_address, 0, 0)); + writembs(scroll_reverse); + completion_print(cols, + width, + pos, + pos+1, + prefix, + is_quoted, + lst); + writembs(tparm(cursor_address, + termsize.ws_row-1, 0)); + writembs(clr_eol); + + } + + break; + } + + case LINE_DOWN: + { + if (pos <= (rows - termsize.ws_row)) + { + pos++; + completion_print(cols, + width, + pos+termsize.ws_row-2, + pos+termsize.ws_row-1, + prefix, + is_quoted, + lst); + } + break; + } + + case PAGE_DOWN: + { + + npos = mini((int)(rows - termsize.ws_row+1), (int)(pos + termsize.ws_row-1)); + if (npos != pos) + { + pos = npos; + completion_print(cols, + width, + pos, + pos+termsize.ws_row-1, + prefix, + is_quoted, + lst); + } + else + { + if (flash_screen) + writembs(flash_screen); + } + + break; + } + + case PAGE_UP: + { + npos = maxi(0, + pos - termsize.ws_row+1); + + if (npos != pos) + { + pos = npos; + completion_print(cols, + width, + pos, + pos+termsize.ws_row-1, + prefix, + is_quoted, + lst); + } + else + { + if (flash_screen) + writembs(flash_screen); + } + break; + } + + case R_NULL: + { + do_loop=0; + res=PAGER_RESIZE; + break; } - break; - } - - case LINE_DOWN: - { - if (pos <= (rows - termsize.ws_row)) + default: { - pos++; - completion_print(cols, - width, - pos+termsize.ws_row-2, - pos+termsize.ws_row-1, - prefix, - is_quoted, - lst); + out_buff.push_back(c); + do_loop = 0; + break; } - break; - } - - case PAGE_DOWN: - { - - npos = mini((int)(rows - termsize.ws_row+1), (int)(pos + termsize.ws_row-1)); - if (npos != pos) - { - pos = npos; - completion_print(cols, - width, - pos, - pos+termsize.ws_row-1, - prefix, - is_quoted, - lst); - } - else - { - if (flash_screen) - writembs(flash_screen); - } - - break; - } - - case PAGE_UP: - { - npos = maxi(0, - pos - termsize.ws_row+1); - - if (npos != pos) - { - pos = npos; - completion_print(cols, - width, - pos, - pos+termsize.ws_row-1, - prefix, - is_quoted, - lst); - } - else - { - if (flash_screen) - writembs(flash_screen); - } - break; - } - - case R_NULL: - { - do_loop=0; - res=PAGER_RESIZE; - break; - - } - - default: - { - out_buff.push_back(c); - do_loop = 0; - break; - } } } writembs(clr_eol); @@ -1273,45 +1273,45 @@ int main(int argc, char **argv) switch (opt) { - case 0: - { - break; - } + case 0: + { + break; + } - case 'r': - { - result_fd = get_fd(optarg); - break; - } + case 'r': + { + result_fd = get_fd(optarg); + break; + } - case 'c': - { - completion_fd = get_fd(optarg); - break; - } + case 'c': + { + completion_fd = get_fd(optarg); + break; + } - case 'p': - { - prefix = str2wcs(optarg); - break; - } + case 'p': + { + prefix = str2wcs(optarg); + break; + } - case 'h': - { - print_help(argv[0], 1); - exit(0); - } + case 'h': + { + print_help(argv[0], 1); + exit(0); + } - case 'v': - { - debug(0, L"%ls, version %s\n", program_name, PACKAGE_VERSION); - exit(0); - } + case 'v': + { + debug(0, L"%ls, version %s\n", program_name, PACKAGE_VERSION); + exit(0); + } - case 'q': - { - is_quoted = 1; - } + case 'q': + { + is_quoted = 1; + } } } @@ -1411,22 +1411,22 @@ int main(int argc, char **argv) switch (completion_try_print(i, prefix, is_quoted, completions)) { - case PAGER_RETRY: - break; + case PAGER_RETRY: + break; - case PAGER_DONE: - i=0; - break; + case PAGER_DONE: + i=0; + break; - case PAGER_RESIZE: - /* - This means we got a resize event, so we start - over from the beginning. Since it the screen got - bigger, we might be able to fit all completions - on-screen. - */ - i=PAGER_MAX_COLS+1; - break; + case PAGER_RESIZE: + /* + This means we got a resize event, so we start + over from the beginning. Since it the screen got + bigger, we might be able to fit all completions + on-screen. + */ + i=PAGER_MAX_COLS+1; + break; } } diff --git a/fishd.cpp b/fishd.cpp index 5fdf78fdd..0383365f8 100644 --- a/fishd.cpp +++ b/fishd.cpp @@ -535,41 +535,41 @@ static void daemonize() */ switch (fork()) { - case -1: - debug(0, L"Could not put fishd in background. Quitting"); - wperror(L"fork"); - exit(1); + case -1: + debug(0, L"Could not put fishd in background. Quitting"); + wperror(L"fork"); + exit(1); - case 0: - { - /* Ordinarily there's very limited things we will do after fork, due to multithreading. But fishd is safe because it's single threaded. So don't die in is_forked_child. */ - setup_fork_guards(); + case 0: + { + /* Ordinarily there's very limited things we will do after fork, due to multithreading. But fishd is safe because it's single threaded. So don't die in is_forked_child. */ + setup_fork_guards(); - /* - Make fishd ignore the HUP signal. - */ - struct sigaction act; - sigemptyset(& act.sa_mask); - act.sa_flags=0; - act.sa_handler=SIG_IGN; - sigaction(SIGHUP, &act, 0); + /* + Make fishd ignore the HUP signal. + */ + struct sigaction act; + sigemptyset(& act.sa_mask); + act.sa_flags=0; + act.sa_handler=SIG_IGN; + sigaction(SIGHUP, &act, 0); - /* - Make fishd save and exit on the TERM signal. - */ - sigfillset(& act.sa_mask); - act.sa_flags=0; - act.sa_handler=&handle_term; - sigaction(SIGTERM, &act, 0); - break; + /* + Make fishd save and exit on the TERM signal. + */ + sigfillset(& act.sa_mask); + act.sa_flags=0; + act.sa_handler=&handle_term; + sigaction(SIGTERM, &act, 0); + break; - } + } - default: - { - debug(0, L"Parent process exiting (This is normal)"); - exit(0); - } + default: + { + debug(0, L"Parent process exiting (This is normal)"); + exit(0); + } } /* @@ -792,19 +792,19 @@ int main(int argc, char ** argv) switch (opt) { - case 0: - break; + case 0: + break; - case 'h': - print_help(argv[0], 1); - exit(0); + case 'h': + print_help(argv[0], 1); + exit(0); - case 'v': - debug(0, L"%ls, version %s\n", program_name, PACKAGE_VERSION); - exit(0); + case 'v': + debug(0, L"%ls, version %s\n", program_name, PACKAGE_VERSION); + exit(0); - case '?': - return 1; + case '?': + return 1; } } diff --git a/highlight.cpp b/highlight.cpp index a7ec044e7..543f33aaf 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -73,13 +73,13 @@ static wcstring apply_working_directory(const wcstring &path, const wcstring &wo bool prepend_wd; switch (path.at(0)) { - case L'/': - case L'~': - prepend_wd = false; - break; - default: - prepend_wd = true; - break; + case L'/': + case L'~': + prepend_wd = false; + break; + default: + prepend_wd = true; + break; } if (! prepend_wd) @@ -158,30 +158,30 @@ bool is_potential_path(const wcstring &const_path, const wcstring_list_t &direct wchar_t c = path.at(i); switch (c) { - case PROCESS_EXPAND: - case VARIABLE_EXPAND: - case VARIABLE_EXPAND_SINGLE: - case BRACKET_BEGIN: - case BRACKET_END: - case BRACKET_SEP: - case ANY_CHAR: - case ANY_STRING: - case ANY_STRING_RECURSIVE: - { - has_magic = 1; - break; - } + case PROCESS_EXPAND: + case VARIABLE_EXPAND: + case VARIABLE_EXPAND_SINGLE: + case BRACKET_BEGIN: + case BRACKET_END: + case BRACKET_SEP: + case ANY_CHAR: + case ANY_STRING: + case ANY_STRING_RECURSIVE: + { + has_magic = 1; + break; + } - case INTERNAL_SEPARATOR: - { - break; - } + case INTERNAL_SEPARATOR: + { + break; + } - default: - { - clean_path.push_back(c); - break; - } + default: + { + clean_path.push_back(c); + break; + } } @@ -392,276 +392,276 @@ static void highlight_param(const wcstring &buffstr, std::vector &colors, w wchar_t c = buffstr.at(in_pos); switch (mode) { - /* - Mode 0 means unquoted string - */ - case e_unquoted: - { - if (c == L'\\') + /* + Mode 0 means unquoted string + */ + case e_unquoted: { - size_t start_pos = in_pos; - in_pos++; - - if (wcschr(L"~%", buff[in_pos])) + if (c == L'\\') { - if (in_pos == 1) + size_t start_pos = in_pos; + in_pos++; + + if (wcschr(L"~%", buff[in_pos])) { - colors.at(start_pos) = HIGHLIGHT_ESCAPE; - colors.at(in_pos+1) = normal_status; + if (in_pos == 1) + { + colors.at(start_pos) = HIGHLIGHT_ESCAPE; + colors.at(in_pos+1) = normal_status; + } } - } - else if (buff[in_pos]==L',') - { - if (bracket_count) + else if (buff[in_pos]==L',') { - colors.at(start_pos) = HIGHLIGHT_ESCAPE; - colors.at(in_pos+1) = normal_status; + if (bracket_count) + { + colors.at(start_pos) = HIGHLIGHT_ESCAPE; + colors.at(in_pos+1) = normal_status; + } } - } - else if (wcschr(L"abefnrtv*?$(){}[]'\"<>^ \\#;|&", buff[in_pos])) - { - colors.at(start_pos)=HIGHLIGHT_ESCAPE; - colors.at(in_pos+1)=normal_status; - } - else if (wcschr(L"c", buff[in_pos])) - { - colors.at(start_pos)=HIGHLIGHT_ESCAPE; - if (in_pos+2 < colors.size()) - colors.at(in_pos+2)=normal_status; - } - else if (wcschr(L"uUxX01234567", buff[in_pos])) - { - int i; - long long res=0; - int chars=2; - int base=16; + else if (wcschr(L"abefnrtv*?$(){}[]'\"<>^ \\#;|&", buff[in_pos])) + { + colors.at(start_pos)=HIGHLIGHT_ESCAPE; + colors.at(in_pos+1)=normal_status; + } + else if (wcschr(L"c", buff[in_pos])) + { + colors.at(start_pos)=HIGHLIGHT_ESCAPE; + if (in_pos+2 < colors.size()) + colors.at(in_pos+2)=normal_status; + } + else if (wcschr(L"uUxX01234567", buff[in_pos])) + { + int i; + long long res=0; + int chars=2; + int base=16; - wchar_t max_val = ASCII_MAX; + wchar_t max_val = ASCII_MAX; + switch (buff[in_pos]) + { + case L'u': + { + chars=4; + max_val = UCS2_MAX; + break; + } + + case L'U': + { + chars=8; + max_val = WCHAR_MAX; + break; + } + + case L'x': + { + break; + } + + case L'X': + { + max_val = BYTE_MAX; + break; + } + + default: + { + base=8; + chars=3; + in_pos--; + break; + } + } + + for (i=0; i &color, const switch (last_type) { - case TOK_STRING: - { - if (had_cmd) + case TOK_STRING: { - - /*Parameter */ - wchar_t *param = tok_last(&tok); - if (param[0] == L'-') + if (had_cmd) { - if (wcscmp(param, L"--") == 0) + + /*Parameter */ + wchar_t *param = tok_last(&tok); + if (param[0] == L'-') { - accept_switches = 0; - color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; - } - else if (accept_switches) - { - if (complete_is_valid_option(last_cmd, param, error, false /* no autoload */)) + if (wcscmp(param, L"--") == 0) + { + accept_switches = 0; color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; + } + else if (accept_switches) + { + if (complete_is_valid_option(last_cmd, param, error, false /* no autoload */)) + color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; + else + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + } else - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; + } } else { color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; } - } - else - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; - } - if (cmd == L"cd") - { - wcstring dir = tok_last(&tok); - if (expand_one(dir, EXPAND_SKIP_CMDSUBST)) + if (cmd == L"cd") { - int is_help = string_prefixes_string(dir, L"--help") || string_prefixes_string(dir, L"-h"); - if (!is_help && ! is_potential_cd_path(dir, working_directory, PATH_EXPAND_TILDE, NULL)) + wcstring dir = tok_last(&tok); + if (expand_one(dir, EXPAND_SKIP_CMDSUBST)) { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + int is_help = string_prefixes_string(dir, L"--help") || string_prefixes_string(dir, L"-h"); + if (!is_help && ! is_potential_cd_path(dir, working_directory, PATH_EXPAND_TILDE, NULL)) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + } } } - } - /* Highlight the parameter. highlight_param wants to write one more color than we have characters (hysterical raisins) so allocate one more in the vector. But don't copy it back. */ - const wcstring param_str = param; - size_t tok_pos = tok_get_pos(&tok); + /* Highlight the parameter. highlight_param wants to write one more color than we have characters (hysterical raisins) so allocate one more in the vector. But don't copy it back. */ + const wcstring param_str = param; + size_t tok_pos = tok_get_pos(&tok); - std::vector::const_iterator where = color.begin() + tok_pos; - std::vector subcolors(where, where + param_str.size()); - subcolors.push_back(-1); - highlight_param(param_str, subcolors, error); + std::vector::const_iterator where = color.begin() + tok_pos; + std::vector subcolors(where, where + param_str.size()); + subcolors.push_back(-1); + highlight_param(param_str, subcolors, error); - /* Copy the subcolors back into our colors array */ - std::copy(subcolors.begin(), subcolors.begin() + param_str.size(), color.begin() + tok_pos); - } - else - { - /* - Command. First check that the command actually exists. - */ - cmd = tok_last(&tok); - bool expanded = expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS); - if (! expanded || has_expand_reserved(cmd.c_str())) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + /* Copy the subcolors back into our colors array */ + std::copy(subcolors.begin(), subcolors.begin() + param_str.size(), color.begin() + tok_pos); } else { - bool is_cmd = false; - int is_subcommand = 0; - int mark = tok_get_pos(&tok); - color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMAND; - - if (parser_keywords_is_subcommand(cmd)) + /* + Command. First check that the command actually exists. + */ + cmd = tok_last(&tok); + bool expanded = expand_one(cmd, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES | EXPAND_SKIP_JOBS); + if (! expanded || has_expand_reserved(cmd.c_str())) { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + } + else + { + bool is_cmd = false; + int is_subcommand = 0; + int mark = tok_get_pos(&tok); + color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMAND; - int sw; + if (parser_keywords_is_subcommand(cmd)) + { - if (cmd == L"builtin") - { - use_function = 0; - use_command = 0; - use_builtin = 1; - } - else if (cmd == L"command") - { - use_command = 1; - use_function = 0; - use_builtin = 0; + int sw; + + if (cmd == L"builtin") + { + use_function = 0; + use_command = 0; + use_builtin = 1; + } + else if (cmd == L"command") + { + use_command = 1; + use_function = 0; + use_builtin = 0; + } + + tok_next(&tok); + + sw = parser_keywords_is_switch(tok_last(&tok)); + + if (!parser_keywords_is_block(cmd) && + sw == ARG_SWITCH) + { + /* + The 'builtin' and 'command' builtins + are normally followed by another + command, but if they are invoked + with a switch, they aren't. + + */ + use_command = 1; + use_function = 1; + use_builtin = 2; + } + else + { + if (sw == ARG_SKIP) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; + mark = tok_get_pos(&tok); + } + + is_subcommand = 1; + } + tok_set_pos(&tok, mark); } - tok_next(&tok); - - sw = parser_keywords_is_switch(tok_last(&tok)); - - if (!parser_keywords_is_block(cmd) && - sw == ARG_SWITCH) + if (!is_subcommand) { /* - The 'builtin' and 'command' builtins - are normally followed by another - command, but if they are invoked - with a switch, they aren't. - + OK, this is a command, it has been + successfully expanded and everything + looks ok. Lets check if the command + exists. */ - use_command = 1; - use_function = 1; - use_builtin = 2; - } - else - { - if (sw == ARG_SKIP) + + /* + First check if it is a builtin or + function, since we don't have to stat + any files for that + */ + if (! is_cmd && use_builtin) + is_cmd = builtin_exists(cmd); + + if (! is_cmd && use_function) + is_cmd = function_exists_no_autoload(cmd, vars); + + /* + Moving on to expensive tests + */ + + /* + Check if this is a regular command + */ + if (! is_cmd && use_command) { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_PARAM; - mark = tok_get_pos(&tok); + is_cmd = path_get_path(cmd, NULL, vars); } - is_subcommand = 1; - } - tok_set_pos(&tok, mark); - } - - if (!is_subcommand) - { - /* - OK, this is a command, it has been - successfully expanded and everything - looks ok. Lets check if the command - exists. - */ - - /* - First check if it is a builtin or - function, since we don't have to stat - any files for that - */ - if (! is_cmd && use_builtin) - is_cmd = builtin_exists(cmd); - - if (! is_cmd && use_function) - is_cmd = function_exists_no_autoload(cmd, vars); - - /* - Moving on to expensive tests - */ - - /* - Check if this is a regular command - */ - if (! is_cmd && use_command) - { - is_cmd = path_get_path(cmd, NULL, vars); - } - - /* Maybe it is a path for a implicit cd command. */ - if (! is_cmd) - { - if (use_builtin || (use_function && function_exists_no_autoload(L"cd", vars))) - is_cmd = path_can_be_implicit_cd(cmd, NULL, working_directory.c_str(), vars); - } - - if (is_cmd) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMAND; - } - else - { - if (error) + /* Maybe it is a path for a implicit cd command. */ + if (! is_cmd) { - error->push_back(format_string(L"Unknown command \'%ls\'", cmd.c_str())); + if (use_builtin || (use_function && function_exists_no_autoload(L"cd", vars))) + is_cmd = path_can_be_implicit_cd(cmd, NULL, working_directory.c_str(), vars); } - color.at(tok_get_pos(&tok)) = (HIGHLIGHT_ERROR); + + if (is_cmd) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMAND; + } + else + { + if (error) + { + error->push_back(format_string(L"Unknown command \'%ls\'", cmd.c_str())); + } + color.at(tok_get_pos(&tok)) = (HIGHLIGHT_ERROR); + } + had_cmd = 1; + } + + if (had_cmd) + { + last_cmd = tok_last(&tok); } - had_cmd = 1; } - if (had_cmd) - { - last_cmd = tok_last(&tok); - } } - - } - break; - } - - case TOK_REDIRECT_NOCLOB: - case TOK_REDIRECT_OUT: - case TOK_REDIRECT_IN: - case TOK_REDIRECT_APPEND: - case TOK_REDIRECT_FD: - { - if (!had_cmd) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - if (error) - error->push_back(L"Redirection without a command"); break; } - wcstring target_str; - const wchar_t *target=NULL; - - color.at(tok_get_pos(&tok)) = HIGHLIGHT_REDIRECTION; - tok_next(&tok); - - /* - Check that we are redirecting into a file - */ - - switch (tok_last_type(&tok)) + case TOK_REDIRECT_NOCLOB: + case TOK_REDIRECT_OUT: + case TOK_REDIRECT_IN: + case TOK_REDIRECT_APPEND: + case TOK_REDIRECT_FD: { - case TOK_STRING: - { - target_str = tok_last(&tok); - if (expand_one(target_str, EXPAND_SKIP_CMDSUBST)) + if (!had_cmd) { - target = target_str.c_str(); + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + if (error) + error->push_back(L"Redirection without a command"); + break; } - /* - Redirect filename may contain a cmdsubst. - If so, it will be ignored/not flagged. - */ - } - break; - default: - { - size_t pos = tok_get_pos(&tok); - if (pos < color.size()) - { - color.at(pos) = HIGHLIGHT_ERROR; - } - if (error) - error->push_back(L"Invalid redirection"); - } - } + wcstring target_str; + const wchar_t *target=NULL; - if (target != 0) - { - wcstring dir = target; - size_t slash_idx = dir.find_last_of(L'/'); - struct stat buff; - /* - If file is in directory other than '.', check - that the directory exists. - */ - if (slash_idx != wcstring::npos) - { - dir.resize(slash_idx); - if (wstat(dir, &buff) == -1) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - if (error) - error->push_back(format_string(L"Directory \'%ls\' does not exist", dir.c_str())); - - } - } + color.at(tok_get_pos(&tok)) = HIGHLIGHT_REDIRECTION; + tok_next(&tok); /* - If the file is read from or appended to, check - if it exists. + Check that we are redirecting into a file */ - if (last_type == TOK_REDIRECT_IN || - last_type == TOK_REDIRECT_APPEND) - { - if (wstat(target, &buff) == -1) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - if (error) - error->push_back(format_string(L"File \'%ls\' does not exist", target)); - } - } - if (last_type == TOK_REDIRECT_NOCLOB) - { - if (wstat(target, &buff) != -1) - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - if (error) - error->push_back(format_string(L"File \'%ls\' exists", target)); - } - } - } - break; - } - case TOK_PIPE: - case TOK_BACKGROUND: - { - if (had_cmd) + switch (tok_last_type(&tok)) + { + case TOK_STRING: + { + target_str = tok_last(&tok); + if (expand_one(target_str, EXPAND_SKIP_CMDSUBST)) + { + target = target_str.c_str(); + } + /* + Redirect filename may contain a cmdsubst. + If so, it will be ignored/not flagged. + */ + } + break; + default: + { + size_t pos = tok_get_pos(&tok); + if (pos < color.size()) + { + color.at(pos) = HIGHLIGHT_ERROR; + } + if (error) + error->push_back(L"Invalid redirection"); + } + + } + + if (target != 0) + { + wcstring dir = target; + size_t slash_idx = dir.find_last_of(L'/'); + struct stat buff; + /* + If file is in directory other than '.', check + that the directory exists. + */ + if (slash_idx != wcstring::npos) + { + dir.resize(slash_idx); + if (wstat(dir, &buff) == -1) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + if (error) + error->push_back(format_string(L"Directory \'%ls\' does not exist", dir.c_str())); + + } + } + + /* + If the file is read from or appended to, check + if it exists. + */ + if (last_type == TOK_REDIRECT_IN || + last_type == TOK_REDIRECT_APPEND) + { + if (wstat(target, &buff) == -1) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + if (error) + error->push_back(format_string(L"File \'%ls\' does not exist", target)); + } + } + if (last_type == TOK_REDIRECT_NOCLOB) + { + if (wstat(target, &buff) != -1) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + if (error) + error->push_back(format_string(L"File \'%ls\' exists", target)); + } + } + } + break; + } + + case TOK_PIPE: + case TOK_BACKGROUND: + { + if (had_cmd) + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_END; + had_cmd = 0; + use_command = 1; + use_function = 1; + use_builtin = 1; + accept_switches = 1; + } + else + { + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + if (error) + error->push_back(L"No job to put in background"); + } + + break; + } + + case TOK_END: { color.at(tok_get_pos(&tok)) = HIGHLIGHT_END; had_cmd = 0; @@ -1262,45 +1281,26 @@ static void tokenize(const wchar_t * const buff, std::vector &color, const use_function = 1; use_builtin = 1; accept_switches = 1; + break; } - else + + case TOK_COMMENT: { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - if (error) - error->push_back(L"No job to put in background"); + color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMENT; + break; } - break; - } - - case TOK_END: - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_END; - had_cmd = 0; - use_command = 1; - use_function = 1; - use_builtin = 1; - accept_switches = 1; - break; - } - - case TOK_COMMENT: - { - color.at(tok_get_pos(&tok)) = HIGHLIGHT_COMMENT; - break; - } - - case TOK_ERROR: - default: - { - /* - If the tokenizer reports an error, highlight it as such. - */ - if (error) - error->push_back(tok_last(&tok)); - color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; - break; - } + case TOK_ERROR: + default: + { + /* + If the tokenizer reports an error, highlight it as such. + */ + if (error) + error->push_back(tok_last(&tok)); + color.at(tok_get_pos(&tok)) = HIGHLIGHT_ERROR; + break; + } } } tok_destroy(&tok); @@ -1465,44 +1465,44 @@ static void highlight_universal_internal(const wcstring &buffstr, std::vector term.size() && contents.find(term) != wcstring::npos; + case HISTORY_SEARCH_TYPE_CONTAINS: + /* We consider equal strings to NOT match a contains search (so that you don't have to see history equal to what you typed). The length check ensures that. */ + return contents.size() > term.size() && contents.find(term) != wcstring::npos; - case HISTORY_SEARCH_TYPE_PREFIX: - /* We consider equal strings to match a prefix search, so that autosuggest will allow suggesting what you've typed */ - return string_prefixes_string(term, contents); + case HISTORY_SEARCH_TYPE_PREFIX: + /* We consider equal strings to match a prefix search, so that autosuggest will allow suggesting what you've typed */ + return string_prefixes_string(term, contents); - default: - sanity_lose(); - return false; + default: + sanity_lose(); + return false; } } @@ -388,25 +388,25 @@ static size_t offset_of_next_item_fish_1_x(const char *begin, size_t mmap_length switch (*pos) { - case '\\': - { - pos++; - break; - } + case '\\': + { + pos++; + break; + } - case '\n': - { - if (ignore_newline) + case '\n': { - ignore_newline = false; + if (ignore_newline) + { + ignore_newline = false; + } + else + { + /* Note: pos will be left pointing just after this newline, because of the ++ in the loop */ + all_done = true; + } + break; } - else - { - /* Note: pos will be left pointing just after this newline, because of the ++ in the loop */ - all_done = true; - } - break; - } } } *inout_cursor = (pos - begin); @@ -419,19 +419,19 @@ static size_t offset_of_next_item(const char *begin, size_t mmap_length, history size_t result; switch (mmap_type) { - case history_type_fish_2_0: - result = offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp); - break; + case history_type_fish_2_0: + result = offset_of_next_item_fish_2_0(begin, mmap_length, inout_cursor, cutoff_timestamp); + break; - case history_type_fish_1_x: - result = offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp); - break; + case history_type_fish_1_x: + result = offset_of_next_item_fish_1_x(begin, mmap_length, inout_cursor, cutoff_timestamp); + break; - default: - case history_type_unknown: - // Oh well - result = (size_t)(-1); - break; + default: + case history_type_unknown: + // Oh well + result = (size_t)(-1); + break; } return result; } @@ -706,12 +706,12 @@ history_item_t history_t::decode_item(const char *base, size_t len, history_file { switch (type) { - case history_type_fish_1_x: - return history_t::decode_item_fish_1_x(base, len); - case history_type_fish_2_0: - return history_t::decode_item_fish_2_0(base, len); - default: - return history_item_t(L""); + case history_type_fish_1_x: + return history_t::decode_item_fish_1_x(base, len); + case history_type_fish_2_0: + return history_t::decode_item_fish_2_0(base, len); + default: + return history_item_t(L""); } } diff --git a/input.cpp b/input.cpp index c6125ef20..631ca560a 100644 --- a/input.cpp +++ b/input.cpp @@ -400,15 +400,15 @@ static wint_t input_exec_binding(const input_mapping_t &m, const wcstring &seq) switch (code) { - case R_SELF_INSERT: - { - return seq[0]; - } + case R_SELF_INSERT: + { + return seq[0]; + } - default: - { - return code; - } + default: + { + return code; + } } } diff --git a/input_common.cpp b/input_common.cpp index 7795629fd..a4f5743c8 100644 --- a/input_common.cpp +++ b/input_common.cpp @@ -105,34 +105,34 @@ static wint_t readb() { switch (errno) { - case EINTR: - case EAGAIN: - { - if (interrupt_handler) + case EINTR: + case EAGAIN: { - int res = interrupt_handler(); - if (res) + if (interrupt_handler) { - return res; - } - if (lookahead_count) - { - return lookahead_arr[--lookahead_count]; + int res = interrupt_handler(); + if (res) + { + return res; + } + if (lookahead_count) + { + return lookahead_arr[--lookahead_count]; + } + } + + do_loop = true; + break; + } + default: + { + /* + The terminal has been closed. Save and exit. + */ + return R_EOF; } - - - do_loop = true; - break; - } - default: - { - /* - The terminal has been closed. Save and exit. - */ - return R_EOF; - } } } else @@ -198,14 +198,14 @@ wchar_t input_common_readch(int timed) switch (count) { - case 0: - return WEOF; + case 0: + return WEOF; - case -1: - return WEOF; - break; - default: - break; + case -1: + return WEOF; + break; + default: + break; } } @@ -229,17 +229,17 @@ wchar_t input_common_readch(int timed) switch (sz) { - case (size_t)(-1): - memset(&state, '\0', sizeof(state)); - debug(2, L"Illegal input"); - return R_NULL; - case (size_t)(-2): - break; - case 0: - return 0; - default: + case (size_t)(-1): + memset(&state, '\0', sizeof(state)); + debug(2, L"Illegal input"); + return R_NULL; + case (size_t)(-2): + break; + case 0: + return 0; + default: - return res; + return res; } } } diff --git a/io.cpp b/io.cpp index 704c4c056..ef2a0493f 100644 --- a/io.cpp +++ b/io.cpp @@ -224,21 +224,21 @@ void io_print(const io_chain_t &chain) fprintf(stderr, "\t%lu: fd:%d, input:%s, ", (unsigned long)i, io->fd, io->is_input ? "yes" : "no"); switch (io->io_mode) { - case IO_FILE: - fprintf(stderr, "file (%s)\n", io->filename_cstr); - break; - case IO_PIPE: - fprintf(stderr, "pipe {%d, %d}\n", io->param1.pipe_fd[0], io->param1.pipe_fd[1]); - break; - case IO_FD: - fprintf(stderr, "FD map %d -> %d\n", io->param1.old_fd, io->fd); - break; - case IO_BUFFER: - fprintf(stderr, "buffer %p (size %lu)\n", io->out_buffer_ptr(), io->out_buffer_size()); - break; - case IO_CLOSE: - fprintf(stderr, "close %d\n", io->fd); - break; + case IO_FILE: + fprintf(stderr, "file (%s)\n", io->filename_cstr); + break; + case IO_PIPE: + fprintf(stderr, "pipe {%d, %d}\n", io->param1.pipe_fd[0], io->param1.pipe_fd[1]); + break; + case IO_FD: + fprintf(stderr, "FD map %d -> %d\n", io->param1.old_fd, io->fd); + break; + case IO_BUFFER: + fprintf(stderr, "buffer %p (size %lu)\n", io->out_buffer_ptr(), io->out_buffer_size()); + break; + case IO_CLOSE: + fprintf(stderr, "close %d\n", io->fd); + break; } } } diff --git a/mimedb.cpp b/mimedb.cpp index ea7e74f5e..9f8d93809 100644 --- a/mimedb.cpp +++ b/mimedb.cpp @@ -434,28 +434,28 @@ static char *munge(char *in) switch (*in) { - case ' ': - case '\n': - case '\t': - case '\r': - { - had_whitespace = 1; - break; - } - case '\0': - *p = '\0'; - return out; - default: - { - if (printed && had_whitespace) + case ' ': + case '\n': + case '\t': + case '\r': { - *(p++)=' '; + had_whitespace = 1; + break; + } + case '\0': + *p = '\0'; + return out; + default: + { + if (printed && had_whitespace) + { + *(p++)=' '; + } + printed=1; + had_whitespace=0; + *(p++)=*in; + break; } - printed=1; - had_whitespace=0; - *(p++)=*in; - break; - } } in++; } @@ -486,22 +486,22 @@ static char *get_lang_re() { switch (*lang) { - case '@': - case '.': - case '_': - if (close) - { - *out++ = ')'; - *out++ = '?'; - } + case '@': + case '.': + case '_': + if (close) + { + *out++ = ')'; + *out++ = '?'; + } - close=1; - *out++ = '('; - *out++ = *lang; - break; + close=1; + *out++ = '('; + *out++ = *lang; + break; - default: - *out++ = *lang; + default: + *out++ = *lang; } } @@ -969,59 +969,59 @@ static void write_file(const char *file, int print_path) { switch (*str) { - case ')': - case '(': - case '-': - case '#': - case '$': - case '}': - case '{': - case ']': - case '[': - case '*': - case '?': - case ' ': - case '|': - case '<': - case '>': - case '^': - case '&': - case '\\': - case '`': - case '\'': - case '\"': - writer('\\'); - writer(*str); - break; + case ')': + case '(': + case '-': + case '#': + case '$': + case '}': + case '{': + case ']': + case '[': + case '*': + case '?': + case ' ': + case '|': + case '<': + case '>': + case '^': + case '&': + case '\\': + case '`': + case '\'': + case '\"': + writer('\\'); + writer(*str); + break; - case '\n': - writer('\\'); - writer('n'); - break; + case '\n': + writer('\\'); + writer('n'); + break; - case '\r': - writer('\\'); - writer('r'); - break; + case '\r': + writer('\\'); + writer('r'); + break; - case '\t': - writer('\\'); - writer('t'); - break; + case '\t': + writer('\\'); + writer('t'); + break; - case '\b': - writer('\\'); - writer('b'); - break; + case '\b': + writer('\\'); + writer('b'); + break; - case '\v': - writer('\\'); - writer('v'); - break; + case '\v': + writer('\\'); + writer('v'); + break; - default: - writer(*str); - break; + default: + writer(*str); + break; } str++; } @@ -1056,106 +1056,106 @@ static void launch(char *filter, const string_list_t &files, size_t fileno) filter++; switch (*filter) { - case 'u': - { - launch_again = 1; - write_url(files.at(fileno).c_str()); - break; - } - case 'U': - { - for (size_t i=0; i= pos) + { + finished=1; + if (b) + { + *b = (wchar_t *)buff + tok_begin; + } + } + else + { + if (a) + { + *a = (wchar_t *)buff + tok_begin+1; + } + } + break; } } - - case TOK_END: - case TOK_BACKGROUND: - { - - if (tok_begin >= pos) - { - finished=1; - if (b) - { - *b = (wchar_t *)buff + tok_begin; - } - } - else - { - if (a) - { - *a = (wchar_t *)buff + tok_begin+1; - } - } - - break; - } - } } free(buffcpy); @@ -611,50 +611,50 @@ wchar_t *parse_util_unescape_wildcards(const wchar_t *str) { switch (*in) { - case L'\\': - { - switch (*(in + 1)) - { - case L'*': - case L'?': - { - in++; - *(out++)=*in; - break; - } case L'\\': { - in++; - *(out++)=L'\\'; - *(out++)=L'\\'; + switch (*(in + 1)) + { + case L'*': + case L'?': + { + in++; + *(out++)=*in; + break; + } + case L'\\': + { + in++; + *(out++)=L'\\'; + *(out++)=L'\\'; + break; + } + default: + { + *(out++)=*in; + break; + } + } break; } + + case L'*': + { + *(out++)=ANY_STRING; + break; + } + + case L'?': + { + *(out++)=ANY_CHAR; + break; + } + default: { *(out++)=*in; break; } - } - break; - } - - case L'*': - { - *(out++)=ANY_STRING; - break; - } - - case L'?': - { - *(out++)=ANY_CHAR; - break; - } - - default: - { - *(out++)=*in; - break; - } } } *out = *in; @@ -786,17 +786,17 @@ wcstring parse_util_escape_string_with_quote(const wcstring &cmd, wchar_t quote) wchar_t c = cmd.at(i); switch (c) { - case L'\n': - case L'\t': - case L'\b': - case L'\r': - unescapable = true; - break; - default: - if (c == quote) - result.push_back(L'\\'); - result.push_back(c); - break; + case L'\n': + case L'\t': + case L'\b': + case L'\r': + unescapable = true; + break; + default: + if (c == quote) + result.push_back(L'\\'); + result.push_back(c); + break; } } diff --git a/parser.cpp b/parser.cpp index 520316a80..6713fddf0 100644 --- a/parser.cpp +++ b/parser.cpp @@ -540,55 +540,55 @@ static const wchar_t *parser_find_end(const wchar_t * buff) int last_type = tok_last_type(&tok); switch (last_type) { - case TOK_STRING: - { - if (!had_cmd) + case TOK_STRING: { - if (wcscmp(tok_last(&tok), L"end")==0) + if (!had_cmd) { - count--; - } - else if (parser_keywords_is_block(tok_last(&tok))) - { - count++; - } + if (wcscmp(tok_last(&tok), L"end")==0) + { + count--; + } + else if (parser_keywords_is_block(tok_last(&tok))) + { + count++; + } - if (count < 0) + if (count < 0) + { + error = 1; + } + had_cmd = 1; + } + break; + } + + case TOK_END: + { + had_cmd = 0; + break; + } + + case TOK_PIPE: + case TOK_BACKGROUND: + { + if (had_cmd) + { + had_cmd = 0; + } + else { error = 1; } - had_cmd = 1; - } - break; - } + break; - case TOK_END: - { - had_cmd = 0; - break; - } - - case TOK_PIPE: - case TOK_BACKGROUND: - { - if (had_cmd) - { - had_cmd = 0; } - else - { + + case TOK_ERROR: error = 1; - } - break; + break; - } - - case TOK_ERROR: - error = 1; - break; - - default: - break; + default: + break; } if (!count) @@ -830,45 +830,45 @@ int parser_t::eval_args(const wchar_t *line, std::vector &args) current_tokenizer_pos = tok_get_pos(&tok); switch (tok_last_type(&tok)) { - case TOK_STRING: - { - const wcstring tmp = tok_last(&tok); - if (expand_string(tmp, args, eflags) == EXPAND_ERROR) + case TOK_STRING: { - err_pos=tok_get_pos(&tok); - do_loop=0; + const wcstring tmp = tok_last(&tok); + if (expand_string(tmp, args, eflags) == EXPAND_ERROR) + { + err_pos=tok_get_pos(&tok); + do_loop=0; + } + break; } - break; - } - case TOK_END: - { - break; - } + case TOK_END: + { + break; + } - case TOK_ERROR: - { - if (show_errors) - error(SYNTAX_ERROR, - tok_get_pos(&tok), - TOK_ERR_MSG, - tok_last(&tok)); + case TOK_ERROR: + { + if (show_errors) + error(SYNTAX_ERROR, + tok_get_pos(&tok), + TOK_ERR_MSG, + tok_last(&tok)); - do_loop=0; - break; - } + do_loop=0; + break; + } - default: - { - if (show_errors) - error(SYNTAX_ERROR, - tok_get_pos(&tok), - UNEXPECTED_TOKEN_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); + default: + { + if (show_errors) + error(SYNTAX_ERROR, + tok_get_pos(&tok), + UNEXPECTED_TOKEN_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); - do_loop=0; - break; - } + do_loop=0; + break; + } } } @@ -925,27 +925,27 @@ void parser_t::stack_trace(block_t *b, wcstring &buff) switch (b->type()) { - case SOURCE: - { - const source_block_t *sb = static_cast(b); - const wchar_t *source_dest = sb->source_file; - append_format(buff, _(L"in . (source) call of file '%ls',\n"), source_dest); - break; - } - case FUNCTION_CALL: - { - const function_block_t *fb = static_cast(b); - append_format(buff, _(L"in function '%ls',\n"), fb->name.c_str()); - break; - } - case SUBST: - { - append_format(buff, _(L"in command substitution\n")); - break; - } + case SOURCE: + { + const source_block_t *sb = static_cast(b); + const wchar_t *source_dest = sb->source_file; + append_format(buff, _(L"in . (source) call of file '%ls',\n"), source_dest); + break; + } + case FUNCTION_CALL: + { + const function_block_t *fb = static_cast(b); + append_format(buff, _(L"in function '%ls',\n"), fb->name.c_str()); + break; + } + case SUBST: + { + append_format(buff, _(L"in command substitution\n")); + break; + } - default: /* Can't get here */ - break; + default: /* Can't get here */ + break; } const wchar_t *file = b->src_filename; @@ -1346,323 +1346,323 @@ void parser_t::parse_job_argument_list(process_t *p, switch (tok_last_type(tok)) { - case TOK_PIPE: - { - wchar_t *end; - - if (p->type == INTERNAL_EXEC) + case TOK_PIPE: { - error(SYNTAX_ERROR, - tok_get_pos(tok), - EXEC_ERR_MSG); - return; - } + wchar_t *end; - errno = 0; - p->pipe_write_fd = fish_wcstoi(tok_last(tok), &end, 10); - if (p->pipe_write_fd < 0 || errno || *end) - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - ILLEGAL_FD_ERR_MSG, - tok_last(tok)); - return; - } - - p->set_argv(completions_to_wcstring_list(args)); - p->next = new process_t(); - - tok_next(tok); - - /* - Don't do anything on failiure. parse_job will notice - the error flag and report any errors for us - */ - parse_job(p->next, j, tok); - - is_finished = 1; - break; - } - - case TOK_BACKGROUND: - { - job_set_flag(j, JOB_FOREGROUND, 0); - } - - case TOK_END: - { - if (!p->get_argv()) - p->set_argv(completions_to_wcstring_list(args)); - if (tok_has_next(tok)) - tok_next(tok); - - is_finished = 1; - - break; - } - - case TOK_STRING: - { - int skip=0; - - if (job_get_flag(j, JOB_SKIP)) - { - skip = 1; - } - else if (current_block->skip && ! unskip) - { - /* - If this command should be skipped, we do not expand the arguments - */ - skip=1; - - /* But if this is in fact a case statement or an elseif statement, then it should be evaluated */ - block_type_t type = current_block->type(); - if (type == SWITCH && args.at(0).completion == L"case" && p->type == INTERNAL_BUILTIN) - { - skip=0; - } - else if (job_get_flag(j, JOB_ELSEIF) && ! job_should_skip_elseif(j, current_block)) - { - skip=0; - } - } - else - { - /* If this is an else if, and we should skip it, then don't expand any arguments */ - if (job_get_flag(j, JOB_ELSEIF) && job_should_skip_elseif(j, current_block)) - { - skip = 1; - } - } - - if (!skip) - { - if ((proc_is_count) && - (args.size() == 1) && - (parser_t::is_help(tok_last(tok), 0)) && - (p->type == INTERNAL_BUILTIN)) - { - /* - Display help for count - */ - p->count_help_magic = 1; - } - - switch (expand_string(tok_last(tok), args, 0)) - { - case EXPAND_ERROR: - { - err_pos=tok_get_pos(tok); - if (error_code == 0) - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - _(L"Could not expand string '%ls'"), - tok_last(tok)); - - } - break; - } - - case EXPAND_WILDCARD_NO_MATCH: - { - unmatched_wildcard = 1; - if (unmatched.empty()) - { - unmatched = tok_last(tok); - unmatched_pos = tok_get_pos(tok); - } - - break; - } - - case EXPAND_WILDCARD_MATCH: - { - matched_wildcard = 1; - break; - } - - case EXPAND_OK: - { - break; - } - - } - - } - - break; - } - - case TOK_REDIRECT_OUT: - case TOK_REDIRECT_IN: - case TOK_REDIRECT_APPEND: - case TOK_REDIRECT_FD: - case TOK_REDIRECT_NOCLOB: - { - int type = tok_last_type(tok); - std::auto_ptr new_io; - wcstring target; - bool has_target = false; - wchar_t *end; - - /* - Don't check redirections in skipped part - - Otherwise, bogus errors may be the result. (Do check - that token is string, though) - */ - if (current_block->skip && ! unskip) - { - tok_next(tok); - if (tok_last_type(tok) != TOK_STRING) + if (p->type == INTERNAL_EXEC) { error(SYNTAX_ERROR, tok_get_pos(tok), - REDIRECT_TOKEN_ERR_MSG, - tok_get_desc(tok_last_type(tok))); + EXEC_ERR_MSG); + return; + } + + errno = 0; + p->pipe_write_fd = fish_wcstoi(tok_last(tok), &end, 10); + if (p->pipe_write_fd < 0 || errno || *end) + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + ILLEGAL_FD_ERR_MSG, + tok_last(tok)); + return; + } + + p->set_argv(completions_to_wcstring_list(args)); + p->next = new process_t(); + + tok_next(tok); + + /* + Don't do anything on failiure. parse_job will notice + the error flag and report any errors for us + */ + parse_job(p->next, j, tok); + + is_finished = 1; + break; + } + + case TOK_BACKGROUND: + { + job_set_flag(j, JOB_FOREGROUND, 0); + } + + case TOK_END: + { + if (!p->get_argv()) + p->set_argv(completions_to_wcstring_list(args)); + if (tok_has_next(tok)) + tok_next(tok); + + is_finished = 1; + + break; + } + + case TOK_STRING: + { + int skip=0; + + if (job_get_flag(j, JOB_SKIP)) + { + skip = 1; + } + else if (current_block->skip && ! unskip) + { + /* + If this command should be skipped, we do not expand the arguments + */ + skip=1; + + /* But if this is in fact a case statement or an elseif statement, then it should be evaluated */ + block_type_t type = current_block->type(); + if (type == SWITCH && args.at(0).completion == L"case" && p->type == INTERNAL_BUILTIN) + { + skip=0; + } + else if (job_get_flag(j, JOB_ELSEIF) && ! job_should_skip_elseif(j, current_block)) + { + skip=0; + } + } + else + { + /* If this is an else if, and we should skip it, then don't expand any arguments */ + if (job_get_flag(j, JOB_ELSEIF) && job_should_skip_elseif(j, current_block)) + { + skip = 1; + } + } + + if (!skip) + { + if ((proc_is_count) && + (args.size() == 1) && + (parser_t::is_help(tok_last(tok), 0)) && + (p->type == INTERNAL_BUILTIN)) + { + /* + Display help for count + */ + p->count_help_magic = 1; + } + + switch (expand_string(tok_last(tok), args, 0)) + { + case EXPAND_ERROR: + { + err_pos=tok_get_pos(tok); + if (error_code == 0) + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + _(L"Could not expand string '%ls'"), + tok_last(tok)); + + } + break; + } + + case EXPAND_WILDCARD_NO_MATCH: + { + unmatched_wildcard = 1; + if (unmatched.empty()) + { + unmatched = tok_last(tok); + unmatched_pos = tok_get_pos(tok); + } + + break; + } + + case EXPAND_WILDCARD_MATCH: + { + matched_wildcard = 1; + break; + } + + case EXPAND_OK: + { + break; + } + + } + } break; } - new_io.reset(new io_data_t); - - errno = 0; - new_io->fd = fish_wcstoi(tok_last(tok), - &end, - 10); - if (new_io->fd < 0 || errno || *end) - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - ILLEGAL_FD_ERR_MSG, - tok_last(tok)); - } - else + case TOK_REDIRECT_OUT: + case TOK_REDIRECT_IN: + case TOK_REDIRECT_APPEND: + case TOK_REDIRECT_FD: + case TOK_REDIRECT_NOCLOB: { + int type = tok_last_type(tok); + std::auto_ptr new_io; + wcstring target; + bool has_target = false; + wchar_t *end; - tok_next(tok); + /* + Don't check redirections in skipped part - switch (tok_last_type(tok)) + Otherwise, bogus errors may be the result. (Do check + that token is string, though) + */ + if (current_block->skip && ! unskip) { - case TOK_STRING: - { - target = tok_last(tok); - has_target = expand_one(target, no_exec ? EXPAND_SKIP_VARIABLES : 0); - - if (! has_target && error_code == 0) + tok_next(tok); + if (tok_last_type(tok) != TOK_STRING) { error(SYNTAX_ERROR, tok_get_pos(tok), REDIRECT_TOKEN_ERR_MSG, - tok_last(tok)); - + tok_get_desc(tok_last_type(tok))); } + break; } - default: + new_io.reset(new io_data_t); + + errno = 0; + new_io->fd = fish_wcstoi(tok_last(tok), + &end, + 10); + if (new_io->fd < 0 || errno || *end) + { error(SYNTAX_ERROR, tok_get_pos(tok), - REDIRECT_TOKEN_ERR_MSG, - tok_get_desc(tok_last_type(tok))); - } - - if (! has_target || target.empty()) - { - if (error_code == 0) - error(SYNTAX_ERROR, - tok_get_pos(tok), - _(L"Invalid IO redirection")); - tok_next(tok); + ILLEGAL_FD_ERR_MSG, + tok_last(tok)); } else { - switch (type) + tok_next(tok); + + switch (tok_last_type(tok)) { - case TOK_REDIRECT_APPEND: - new_io->io_mode = IO_FILE; - new_io->param2.flags = O_CREAT | O_APPEND | O_WRONLY; - new_io->set_filename(target); - break; - - case TOK_REDIRECT_OUT: - new_io->io_mode = IO_FILE; - new_io->param2.flags = O_CREAT | O_WRONLY | O_TRUNC; - new_io->set_filename(target); - break; - - case TOK_REDIRECT_NOCLOB: - new_io->io_mode = IO_FILE; - new_io->param2.flags = O_CREAT | O_EXCL | O_WRONLY; - new_io->set_filename(target); - break; - - case TOK_REDIRECT_IN: - new_io->io_mode = IO_FILE; - new_io->param2.flags = O_RDONLY; - new_io->set_filename(target); - break; - - case TOK_REDIRECT_FD: - { - if (target == L"-") + case TOK_STRING: { - new_io->io_mode = IO_CLOSE; - } - else - { - wchar_t *end; + target = tok_last(tok); + has_target = expand_one(target, no_exec ? EXPAND_SKIP_VARIABLES : 0); - new_io->io_mode = IO_FD; - errno = 0; - - new_io->param1.old_fd = fish_wcstoi(target.c_str(), &end, 10); - - if ((new_io->param1.old_fd < 0) || - errno || *end) + if (! has_target && error_code == 0) { error(SYNTAX_ERROR, tok_get_pos(tok), - _(L"Requested redirection to something that is not a file descriptor %ls"), - target.c_str()); + REDIRECT_TOKEN_ERR_MSG, + tok_last(tok)); - tok_next(tok); + } + break; + } + + default: + error(SYNTAX_ERROR, + tok_get_pos(tok), + REDIRECT_TOKEN_ERR_MSG, + tok_get_desc(tok_last_type(tok))); + } + + if (! has_target || target.empty()) + { + if (error_code == 0) + error(SYNTAX_ERROR, + tok_get_pos(tok), + _(L"Invalid IO redirection")); + tok_next(tok); + } + else + { + + switch (type) + { + case TOK_REDIRECT_APPEND: + new_io->io_mode = IO_FILE; + new_io->param2.flags = O_CREAT | O_APPEND | O_WRONLY; + new_io->set_filename(target); + break; + + case TOK_REDIRECT_OUT: + new_io->io_mode = IO_FILE; + new_io->param2.flags = O_CREAT | O_WRONLY | O_TRUNC; + new_io->set_filename(target); + break; + + case TOK_REDIRECT_NOCLOB: + new_io->io_mode = IO_FILE; + new_io->param2.flags = O_CREAT | O_EXCL | O_WRONLY; + new_io->set_filename(target); + break; + + case TOK_REDIRECT_IN: + new_io->io_mode = IO_FILE; + new_io->param2.flags = O_RDONLY; + new_io->set_filename(target); + break; + + case TOK_REDIRECT_FD: + { + if (target == L"-") + { + new_io->io_mode = IO_CLOSE; + } + else + { + wchar_t *end; + + new_io->io_mode = IO_FD; + errno = 0; + + new_io->param1.old_fd = fish_wcstoi(target.c_str(), &end, 10); + + if ((new_io->param1.old_fd < 0) || + errno || *end) + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + _(L"Requested redirection to something that is not a file descriptor %ls"), + target.c_str()); + + tok_next(tok); + } + } + break; } } - break; - } - } + } } + + j->io.push_back(new_io.release()); + + } + break; + + case TOK_ERROR: + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + TOK_ERR_MSG, + tok_last(tok)); + + return; } - j->io.push_back(new_io.release()); + default: + error(SYNTAX_ERROR, + tok_get_pos(tok), + UNEXPECTED_TOKEN_ERR_MSG, + tok_get_desc(tok_last_type(tok))); - } - break; - - case TOK_ERROR: - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - TOK_ERR_MSG, - tok_last(tok)); - - return; - } - - default: - error(SYNTAX_ERROR, - tok_get_pos(tok), - UNEXPECTED_TOKEN_ERR_MSG, - tok_get_desc(tok_last_type(tok))); - - tok_next(tok); - break; + tok_next(tok); + break; } if ((is_finished) || (error_code != 0)) @@ -1742,67 +1742,67 @@ int parser_t::parse_job(process_t *p, switch (tok_last_type(tok)) { - case TOK_STRING: - { - nxt = tok_last(tok); - has_nxt = expand_one(nxt, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES); + case TOK_STRING: + { + nxt = tok_last(tok); + has_nxt = expand_one(nxt, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES); - if (! has_nxt) + if (! has_nxt) + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + ILLEGAL_CMD_ERR_MSG, + tok_last(tok)); + + current_tokenizer_pos = prev_tokenizer_pos; + return 0; + } + break; + } + + case TOK_ERROR: { error(SYNTAX_ERROR, tok_get_pos(tok), - ILLEGAL_CMD_ERR_MSG, + TOK_ERR_MSG, tok_last(tok)); current_tokenizer_pos = prev_tokenizer_pos; return 0; } - break; - } - case TOK_ERROR: - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - TOK_ERR_MSG, - tok_last(tok)); - - current_tokenizer_pos = prev_tokenizer_pos; - return 0; - } - - case TOK_PIPE: - { - const wchar_t *str = tok_string(tok); - if (tok_get_pos(tok)>0 && str[tok_get_pos(tok)-1] == L'|') + case TOK_PIPE: { - error(SYNTAX_ERROR, - tok_get_pos(tok), - CMD_OR_ERR_MSG, - tok_get_desc(tok_last_type(tok))); + const wchar_t *str = tok_string(tok); + if (tok_get_pos(tok)>0 && str[tok_get_pos(tok)-1] == L'|') + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + CMD_OR_ERR_MSG, + tok_get_desc(tok_last_type(tok))); + } + else + { + error(SYNTAX_ERROR, + tok_get_pos(tok), + CMD_ERR_MSG, + tok_get_desc(tok_last_type(tok))); + } + + current_tokenizer_pos = prev_tokenizer_pos; + return 0; } - else + + default: { error(SYNTAX_ERROR, tok_get_pos(tok), CMD_ERR_MSG, tok_get_desc(tok_last_type(tok))); + + current_tokenizer_pos = prev_tokenizer_pos; + return 0; } - - current_tokenizer_pos = prev_tokenizer_pos; - return 0; - } - - default: - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - CMD_ERR_MSG, - tok_get_desc(tok_last_type(tok))); - - current_tokenizer_pos = prev_tokenizer_pos; - return 0; - } } mark = tok_get_pos(tok); @@ -2209,34 +2209,34 @@ int parser_t::parse_job(process_t *p, switch (tok_last_type(&subtok)) { - case TOK_END: - done = 1; - break; + case TOK_END: + done = 1; + break; - case TOK_REDIRECT_OUT: - case TOK_REDIRECT_NOCLOB: - case TOK_REDIRECT_APPEND: - case TOK_REDIRECT_IN: - case TOK_REDIRECT_FD: - case TOK_PIPE: - { - done = 1; - make_sub_block = 1; - break; - } + case TOK_REDIRECT_OUT: + case TOK_REDIRECT_NOCLOB: + case TOK_REDIRECT_APPEND: + case TOK_REDIRECT_IN: + case TOK_REDIRECT_FD: + case TOK_PIPE: + { + done = 1; + make_sub_block = 1; + break; + } - case TOK_STRING: - { - break; - } + case TOK_STRING: + { + break; + } - default: - { - done = 1; - error(SYNTAX_ERROR, - current_tokenizer_pos, - BLOCK_END_ERR_MSG); - } + default: + { + done = 1; + error(SYNTAX_ERROR, + current_tokenizer_pos, + BLOCK_END_ERR_MSG); + } } } @@ -2413,212 +2413,212 @@ void parser_t::eval_job(tokenizer *tok) switch (tok_last_type(tok)) { - case TOK_STRING: - { - j = this->job_create(); - job_set_flag(j, JOB_FOREGROUND, 1); - job_set_flag(j, JOB_TERMINAL, job_get_flag(j, JOB_CONTROL)); - job_set_flag(j, JOB_TERMINAL, job_get_flag(j, JOB_CONTROL) \ - && (!is_subshell && !is_event)); - job_set_flag(j, JOB_SKIP_NOTIFICATION, is_subshell \ - || is_block \ - || is_event \ - || (!get_is_interactive())); - - current_block->job = j; - - if (get_is_interactive()) + case TOK_STRING: { - if (tcgetattr(0, &j->tmodes)) + j = this->job_create(); + job_set_flag(j, JOB_FOREGROUND, 1); + job_set_flag(j, JOB_TERMINAL, job_get_flag(j, JOB_CONTROL)); + job_set_flag(j, JOB_TERMINAL, job_get_flag(j, JOB_CONTROL) \ + && (!is_subshell && !is_event)); + job_set_flag(j, JOB_SKIP_NOTIFICATION, is_subshell \ + || is_block \ + || is_event \ + || (!get_is_interactive())); + + current_block->job = j; + + if (get_is_interactive()) { - tok_next(tok); - wperror(L"tcgetattr"); + if (tcgetattr(0, &j->tmodes)) + { + tok_next(tok); + wperror(L"tcgetattr"); + job_free(j); + break; + } + } + + j->first_process = new process_t(); + job_begin_pos = tok_get_pos(tok); + + if (parse_job(j->first_process, j, tok) && + j->first_process->get_argv()) + { + if (job_start_pos < tok_get_pos(tok)) + { + long stop_pos = tok_get_pos(tok); + const wchar_t *newline = wcschr(tok_string(tok)+start_pos, L'\n'); + if (newline) + stop_pos = mini(stop_pos, newline - tok_string(tok)); + + j->set_command(wcstring(tok_string(tok)+start_pos, stop_pos-start_pos)); + } + else + j->set_command(L""); + + if (do_profile) + { + t2 = get_time(); + profile_item->cmd = wcsdup(j->command_wcstr()); + profile_item->skipped=current_block->skip; + } + + /* If we're an ELSEIF, then we may want to unskip, if we're skipping because of an IF */ + if (job_get_flag(j, JOB_ELSEIF)) + { + bool skip_elseif = job_should_skip_elseif(j, current_block); + + /* Record that we're entering an elseif */ + if (! skip_elseif) + { + /* We must be an IF block here */ + assert(current_block->type() == IF); + static_cast(current_block)->is_elseif_entry = true; + } + + /* Record that in the block too. This is similar to what builtin_else does. */ + current_block->skip = skip_elseif; + } + + skip = skip || current_block->skip; + skip = skip || job_get_flag(j, JOB_WILDCARD_ERROR); + skip = skip || job_get_flag(j, JOB_SKIP); + + if (!skip) + { + int was_builtin = 0; + if (j->first_process->type==INTERNAL_BUILTIN && !j->first_process->next) + was_builtin = 1; + prev_tokenizer_pos = current_tokenizer_pos; + current_tokenizer_pos = job_begin_pos; + exec(*this, j); + current_tokenizer_pos = prev_tokenizer_pos; + + /* Only external commands require a new fishd barrier */ + if (!was_builtin) + set_proc_had_barrier(false); + } + else + { + this->skipped_exec(j); + } + + if (do_profile) + { + t3 = get_time(); + profile_item->level=eval_level; + profile_item->parse = (int)(t2-t1); + profile_item->exec=(int)(t3-t2); + } + + if (current_block->type() == WHILE) + { + while_block_t *wb = static_cast(current_block); + switch (wb->status) + { + case WHILE_TEST_FIRST: + { + // PCA I added the 'wb->skip ||' part because we couldn't reliably + // control-C out of loops like this: while test 1 -eq 1; end + wb->skip = wb->skip || proc_get_last_status()!= 0; + wb->status = WHILE_TESTED; + } + break; + } + } + + if (current_block->type() == IF) + { + if_block_t *ib = static_cast(current_block); + + if (ib->skip) + { + /* Nothing */ + } + else if (! ib->if_expr_evaluated) + { + /* Execute the IF */ + bool if_result = (proc_get_last_status() == 0); + ib->any_branch_taken = if_result; + + /* Don't execute if the expression failed */ + current_block->skip = ! if_result; + ib->if_expr_evaluated = true; + } + else if (ib->is_elseif_entry && ! ib->any_branch_taken) + { + /* Maybe mark an ELSEIF branch as taken */ + bool elseif_taken = (proc_get_last_status() == 0); + ib->any_branch_taken = elseif_taken; + current_block->skip = ! elseif_taken; + ib->is_elseif_entry = false; + } + } + + } + else + { + /* + This job could not be properly parsed. We free it + instead, and set the status to 1. This should be + rare, since most errors should be detected by the + ahead of time validator. + */ job_free(j); - break; + + proc_set_last_status(1); } + current_block->job = 0; + break; } - j->first_process = new process_t(); - job_begin_pos = tok_get_pos(tok); - - if (parse_job(j->first_process, j, tok) && - j->first_process->get_argv()) + case TOK_END: { - if (job_start_pos < tok_get_pos(tok)) + if (tok_has_next(tok)) + tok_next(tok); + break; + } + + case TOK_BACKGROUND: + { + const wchar_t *str = tok_string(tok); + if (tok_get_pos(tok)>0 && str[tok_get_pos(tok)-1] == L'&') { - long stop_pos = tok_get_pos(tok); - const wchar_t *newline = wcschr(tok_string(tok)+start_pos, L'\n'); - if (newline) - stop_pos = mini(stop_pos, newline - tok_string(tok)); - - j->set_command(wcstring(tok_string(tok)+start_pos, stop_pos-start_pos)); - } - else - j->set_command(L""); - - if (do_profile) - { - t2 = get_time(); - profile_item->cmd = wcsdup(j->command_wcstr()); - profile_item->skipped=current_block->skip; - } - - /* If we're an ELSEIF, then we may want to unskip, if we're skipping because of an IF */ - if (job_get_flag(j, JOB_ELSEIF)) - { - bool skip_elseif = job_should_skip_elseif(j, current_block); - - /* Record that we're entering an elseif */ - if (! skip_elseif) - { - /* We must be an IF block here */ - assert(current_block->type() == IF); - static_cast(current_block)->is_elseif_entry = true; - } - - /* Record that in the block too. This is similar to what builtin_else does. */ - current_block->skip = skip_elseif; - } - - skip = skip || current_block->skip; - skip = skip || job_get_flag(j, JOB_WILDCARD_ERROR); - skip = skip || job_get_flag(j, JOB_SKIP); - - if (!skip) - { - int was_builtin = 0; - if (j->first_process->type==INTERNAL_BUILTIN && !j->first_process->next) - was_builtin = 1; - prev_tokenizer_pos = current_tokenizer_pos; - current_tokenizer_pos = job_begin_pos; - exec(*this, j); - current_tokenizer_pos = prev_tokenizer_pos; - - /* Only external commands require a new fishd barrier */ - if (!was_builtin) - set_proc_had_barrier(false); + error(SYNTAX_ERROR, + tok_get_pos(tok), + CMD_AND_ERR_MSG, + tok_get_desc(tok_last_type(tok))); } else { - this->skipped_exec(j); - } - - if (do_profile) - { - t3 = get_time(); - profile_item->level=eval_level; - profile_item->parse = (int)(t2-t1); - profile_item->exec=(int)(t3-t2); - } - - if (current_block->type() == WHILE) - { - while_block_t *wb = static_cast(current_block); - switch (wb->status) - { - case WHILE_TEST_FIRST: - { - // PCA I added the 'wb->skip ||' part because we couldn't reliably - // control-C out of loops like this: while test 1 -eq 1; end - wb->skip = wb->skip || proc_get_last_status()!= 0; - wb->status = WHILE_TESTED; - } - break; - } - } - - if (current_block->type() == IF) - { - if_block_t *ib = static_cast(current_block); - - if (ib->skip) - { - /* Nothing */ - } - else if (! ib->if_expr_evaluated) - { - /* Execute the IF */ - bool if_result = (proc_get_last_status() == 0); - ib->any_branch_taken = if_result; - - /* Don't execute if the expression failed */ - current_block->skip = ! if_result; - ib->if_expr_evaluated = true; - } - else if (ib->is_elseif_entry && ! ib->any_branch_taken) - { - /* Maybe mark an ELSEIF branch as taken */ - bool elseif_taken = (proc_get_last_status() == 0); - ib->any_branch_taken = elseif_taken; - current_block->skip = ! elseif_taken; - ib->is_elseif_entry = false; - } + error(SYNTAX_ERROR, + tok_get_pos(tok), + CMD_ERR_MSG, + tok_get_desc(tok_last_type(tok))); } + return; } - else - { - /* - This job could not be properly parsed. We free it - instead, and set the status to 1. This should be - rare, since most errors should be detected by the - ahead of time validator. - */ - job_free(j); - proc_set_last_status(1); - } - current_block->job = 0; - break; - } - - case TOK_END: - { - if (tok_has_next(tok)) - tok_next(tok); - break; - } - - case TOK_BACKGROUND: - { - const wchar_t *str = tok_string(tok); - if (tok_get_pos(tok)>0 && str[tok_get_pos(tok)-1] == L'&') + case TOK_ERROR: { error(SYNTAX_ERROR, tok_get_pos(tok), - CMD_AND_ERR_MSG, - tok_get_desc(tok_last_type(tok))); + TOK_ERR_MSG, + tok_last(tok)); + + return; } - else + + default: { error(SYNTAX_ERROR, tok_get_pos(tok), CMD_ERR_MSG, tok_get_desc(tok_last_type(tok))); + + return; } - - return; - } - - case TOK_ERROR: - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - TOK_ERR_MSG, - tok_last(tok)); - - return; - } - - default: - { - error(SYNTAX_ERROR, - tok_get_pos(tok), - CMD_ERR_MSG, - tok_get_desc(tok_last_type(tok))); - - return; - } } job_reap(0); @@ -2810,47 +2810,47 @@ int parser_t::parser_test_argument(const wchar_t *arg, wcstring *out, const wcha ¶n_end, 0)) { - case -1: - err=1; - if (out) + case -1: + err=1; + if (out) + { + error(SYNTAX_ERROR, + offset, + L"Mismatched parenthesis"); + this->print_errors(*out, prefix); + } + free(arg_cpy); + return err; + + case 0: + do_loop = 0; + break; + + case 1: { - error(SYNTAX_ERROR, - offset, - L"Mismatched parenthesis"); - this->print_errors(*out, prefix); - } - free(arg_cpy); - return err; - case 0: - do_loop = 0; - break; + wchar_t *subst = wcsndup(paran_begin+1, paran_end-paran_begin-1); + wcstring tmp; - case 1: - { - - wchar_t *subst = wcsndup(paran_begin+1, paran_end-paran_begin-1); - wcstring tmp; - - tmp.append(arg_cpy, paran_begin - arg_cpy); - tmp.push_back(INTERNAL_SEPARATOR); - tmp.append(paran_end+1); + tmp.append(arg_cpy, paran_begin - arg_cpy); + tmp.push_back(INTERNAL_SEPARATOR); + tmp.append(paran_end+1); // debug( 1, L"%ls -> %ls %ls", arg_cpy, subst, tmp.buff ); - err |= parser_t::test(subst, 0, out, prefix); + err |= parser_t::test(subst, 0, out, prefix); - free(subst); - free(arg_cpy); - arg_cpy = wcsdup(tmp.c_str()); + free(subst); + free(arg_cpy); + arg_cpy = wcsdup(tmp.c_str()); - /* - Do _not_ call sb_destroy on this stringbuffer - it's - buffer is used as the new 'arg_cpy'. It is free'd at - the end of the loop. - */ - break; - } + /* + Do _not_ call sb_destroy on this stringbuffer - it's + buffer is used as the new 'arg_cpy'. It is free'd at + the end of the loop. + */ + break; + } } } @@ -2875,25 +2875,25 @@ int parser_t::parser_test_argument(const wchar_t *arg, wcstring *out, const wcha { switch (*pos) { - case VARIABLE_EXPAND: - case VARIABLE_EXPAND_SINGLE: - { - wchar_t n = *(pos+1); - - if (n != VARIABLE_EXPAND && - n != VARIABLE_EXPAND_SINGLE && - !wcsvarchr(n)) + case VARIABLE_EXPAND: + case VARIABLE_EXPAND_SINGLE: { - err=1; - if (out) - { - expand_variable_error(*this, unesc, pos-unesc, offset); - print_errors(*out, prefix); - } - } + wchar_t n = *(pos+1); - break; - } + if (n != VARIABLE_EXPAND && + n != VARIABLE_EXPAND_SINGLE && + !wcsvarchr(n)) + { + err=1; + if (out) + { + expand_variable_error(*this, unesc, pos-unesc, offset); + print_errors(*out, prefix); + } + } + + break; + } } } } @@ -2925,46 +2925,46 @@ int parser_t::test_args(const wchar_t * buff, wcstring *out, const wchar_t *pre switch (tok_last_type(&tok)) { - case TOK_STRING: - { - err |= parser_test_argument(tok_last(&tok), out, prefix, tok_get_pos(&tok)); - break; - } - - case TOK_END: - { - break; - } - - case TOK_ERROR: - { - if (out) + case TOK_STRING: { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - TOK_ERR_MSG, - tok_last(&tok)); - print_errors(*out, prefix); + err |= parser_test_argument(tok_last(&tok), out, prefix, tok_get_pos(&tok)); + break; } - err=1; - do_loop=0; - break; - } - default: - { - if (out) + case TOK_END: { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - UNEXPECTED_TOKEN_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); - print_errors(*out, prefix); + break; + } + + case TOK_ERROR: + { + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + TOK_ERR_MSG, + tok_last(&tok)); + print_errors(*out, prefix); + } + err=1; + do_loop=0; + break; + } + + default: + { + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + UNEXPECTED_TOKEN_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); + print_errors(*out, prefix); + } + err=1; + do_loop=0; + break; } - err=1; - do_loop=0; - break; - } } } @@ -3056,573 +3056,573 @@ int parser_t::test(const wchar_t * buff, switch (last_type) { - case TOK_STRING: - { - if (!had_cmd) + case TOK_STRING: { - int mark = tok_get_pos(&tok); - had_cmd = 1; - arg_count=0; - - command = tok_last(&tok); - has_command = expand_one(command, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES); - if (!has_command) + if (!had_cmd) { - command = L""; - err=1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - ILLEGAL_CMD_ERR_MSG, - tok_last(&tok)); + int mark = tok_get_pos(&tok); + had_cmd = 1; + arg_count=0; - print_errors(*out, prefix); - } - break; - } - - if (needs_cmd) - { - /* - end is not a valid command when a followup - command is needed, such as after 'and' or - 'while' - */ - if (contains(command, - L"end")) + command = tok_last(&tok); + has_command = expand_one(command, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_VARIABLES); + if (!has_command) { + command = L""; err=1; if (out) { error(SYNTAX_ERROR, tok_get_pos(&tok), - COND_ERR_MSG); + ILLEGAL_CMD_ERR_MSG, + tok_last(&tok)); print_errors(*out, prefix); } + break; } - needs_cmd = false; - } - - /* - Decrement block count on end command - */ - if (command == L"end") - { - tok_next(&tok); - count--; - tok_set_pos(&tok, mark); - } - - /* - Store the block level. This needs to be done - _after_ checking for end commands, but _before_ - checking for block opening commands. - */ - bool is_else_or_elseif = (command == L"else"); - if (block_level) - { - block_level[tok_get_pos(&tok)] = count + (is_else_or_elseif?-1:0); - } - - /* - Handle block commands - */ - if (parser_keywords_is_block(command)) - { - if (count >= BLOCK_MAX_COUNT) + if (needs_cmd) { - if (out) + /* + end is not a valid command when a followup + command is needed, such as after 'and' or + 'while' + */ + if (contains(command, + L"end")) { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - BLOCK_ERR_MSG); + err=1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + COND_ERR_MSG); - print_errors(*out, prefix); + print_errors(*out, prefix); + } } + + needs_cmd = false; } - else + + /* + Decrement block count on end command + */ + if (command == L"end") { - block_type[count] = parser_get_block_type(command); - block_pos[count] = current_tokenizer_pos; tok_next(&tok); - count++; + count--; tok_set_pos(&tok, mark); } - } - /* - If parser_keywords_is_subcommand is true, the command - accepts a second command as it's first - argument. If parser_skip_arguments is true, the - second argument is optional. - */ - if (parser_keywords_is_subcommand(command) && !parser_keywords_skip_arguments(command)) - { - needs_cmd = true; - had_cmd = 0; - } - - if (contains(command, - L"or", - L"and")) - { /* - 'or' and 'and' can not be used inside pipelines + Store the block level. This needs to be done + _after_ checking for end commands, but _before_ + checking for block opening commands. */ - if (is_pipeline) + bool is_else_or_elseif = (command == L"else"); + if (block_level) { - err=1; - if (out) + block_level[tok_get_pos(&tok)] = count + (is_else_or_elseif?-1:0); + } + + /* + Handle block commands + */ + if (parser_keywords_is_block(command)) + { + if (count >= BLOCK_MAX_COUNT) { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - EXEC_ERR_MSG); - - print_errors(*out, prefix); + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + BLOCK_ERR_MSG); + print_errors(*out, prefix); + } + } + else + { + block_type[count] = parser_get_block_type(command); + block_pos[count] = current_tokenizer_pos; + tok_next(&tok); + count++; + tok_set_pos(&tok, mark); } } - } - /* - There are a lot of situations where pipelines - are forbidden, including when using the exec - builtin. - */ - if (parser_is_pipe_forbidden(command)) - { - if (is_pipeline) + /* + If parser_keywords_is_subcommand is true, the command + accepts a second command as it's first + argument. If parser_skip_arguments is true, the + second argument is optional. + */ + if (parser_keywords_is_subcommand(command) && !parser_keywords_skip_arguments(command)) { - err=1; - if (out) + needs_cmd = true; + had_cmd = 0; + } + + if (contains(command, + L"or", + L"and")) + { + /* + 'or' and 'and' can not be used inside pipelines + */ + if (is_pipeline) { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - EXEC_ERR_MSG); + err=1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + EXEC_ERR_MSG); - print_errors(*out, prefix); + print_errors(*out, prefix); + } } } - forbid_pipeline = 1; - } - /* - Test that the case builtin is only used directly in a switch block - */ - if (command == L"case") - { - if (!count || block_type[count-1]!=SWITCH) + /* + There are a lot of situations where pipelines + are forbidden, including when using the exec + builtin. + */ + if (parser_is_pipe_forbidden(command)) { - err=1; + if (is_pipeline) + { + err=1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + EXEC_ERR_MSG); + print_errors(*out, prefix); + + } + } + forbid_pipeline = 1; + } + + /* + Test that the case builtin is only used directly in a switch block + */ + if (command == L"case") + { + if (!count || block_type[count-1]!=SWITCH) + { + err=1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + INVALID_CASE_ERR_MSG); + + print_errors(*out, prefix); + const wcstring h = builtin_help_get(*this, L"case"); + if (h.size()) + append_format(*out, L"%ls", h.c_str()); + } + } + } + + /* + Test that the return bultin is only used within function definitions + */ + if (command == L"return") + { + int found_func=0; + int i; + for (i=count-1; i>=0; i--) + { + if (block_type[i]==FUNCTION_DEF) + { + found_func=1; + break; + } + } + + if (!found_func) + { + /* + Peek to see if the next argument is + --help, in which case we'll allow it to + show the help. + */ + + int old_pos = tok_get_pos(&tok); + int is_help = 0; + + tok_next(&tok); + if (tok_last_type(&tok) == TOK_STRING) + { + wcstring first_arg = tok_last(&tok); + if (expand_one(first_arg, EXPAND_SKIP_CMDSUBST) && parser_t::is_help(first_arg.c_str(), 3)) + { + is_help = 1; + } + } + + tok_set_pos(&tok, old_pos); + + if (!is_help) + { + err=1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + INVALID_RETURN_ERR_MSG); + print_errors(*out, prefix); + } + } + } + } + + + /* + Test that break and continue are only used within loop blocks + */ + if (contains(command, L"break", L"continue")) + { + int found_loop=0; + int i; + for (i=count-1; i>=0; i--) + { + if ((block_type[i]==WHILE) || + (block_type[i]==FOR)) + { + found_loop=1; + break; + } + } + + if (!found_loop) + { + /* + Peek to see if the next argument is + --help, in which case we'll allow it to + show the help. + */ + + int old_pos = tok_get_pos(&tok); + int is_help = 0; + + tok_next(&tok); + if (tok_last_type(&tok) == TOK_STRING) + { + wcstring first_arg = tok_last(&tok); + if (expand_one(first_arg, EXPAND_SKIP_CMDSUBST) && parser_t::is_help(first_arg.c_str(), 3)) + { + is_help = 1; + } + } + + tok_set_pos(&tok, old_pos); + + if (!is_help) + { + err=1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + INVALID_LOOP_ERR_MSG); + print_errors(*out, prefix); + } + } + } + } + + /* + Test that else and else-if are only used directly in an if-block + */ + if (command == L"else") + { + if (!count || block_type[count-1]!=IF) + { + err=1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + INVALID_ELSE_ERR_MSG, + command.c_str()); + + print_errors(*out, prefix); + } + } + } + + /* + Test that end is not used when not inside any block + */ + if (count < 0) + { + err = 1; if (out) { error(SYNTAX_ERROR, tok_get_pos(&tok), - INVALID_CASE_ERR_MSG); - + INVALID_END_ERR_MSG); print_errors(*out, prefix); - const wcstring h = builtin_help_get(*this, L"case"); + const wcstring h = builtin_help_get(*this, L"end"); if (h.size()) append_format(*out, L"%ls", h.c_str()); } } - } - /* - Test that the return bultin is only used within function definitions - */ - if (command == L"return") + } + else { - int found_func=0; - int i; - for (i=count-1; i>=0; i--) + err |= parser_test_argument(tok_last(&tok), out, prefix, tok_get_pos(&tok)); + + /* If possible, keep track of number of supplied arguments */ + if (arg_count >= 0 && expand_is_clean(tok_last(&tok))) { - if (block_type[i]==FUNCTION_DEF) - { - found_func=1; - break; - } + arg_count++; + } + else + { + arg_count = -1; } - if (!found_func) + if (has_command) { + /* - Peek to see if the next argument is - --help, in which case we'll allow it to - show the help. + Try to make sure the second argument to 'for' is 'in' */ - - int old_pos = tok_get_pos(&tok); - int is_help = 0; - - tok_next(&tok); - if (tok_last_type(&tok) == TOK_STRING) + if (command == L"for") { - wcstring first_arg = tok_last(&tok); - if (expand_one(first_arg, EXPAND_SKIP_CMDSUBST) && parser_t::is_help(first_arg.c_str(), 3)) + if (arg_count == 1) { - is_help = 1; + + if (wcsvarname(tok_last(&tok))) + { + + err = 1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + BUILTIN_FOR_ERR_NAME, + L"for", + tok_last(&tok)); + + print_errors(*out, prefix); + } + } + + } + else if (arg_count == 2) + { + if (wcscmp(tok_last(&tok), L"in") != 0) + { + err = 1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + BUILTIN_FOR_ERR_IN, + L"for"); + + print_errors(*out, prefix); + } + } } } - - tok_set_pos(&tok, old_pos); - - if (!is_help) + else if (command == L"else") { - err=1; - - if (out) + if (arg_count == 1) { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - INVALID_RETURN_ERR_MSG); - print_errors(*out, prefix); + /* Any second argument must be "if" */ + if (wcscmp(tok_last(&tok), L"if") != 0) + { + err = 1; + + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + BUILTIN_ELSEIF_ERR_ARGUMENT, + L"else"); + print_errors(*out, prefix); + } + } + else + { + /* Successfully detected "else if". Now we need a new command. */ + needs_cmd = true; + had_cmd = false; + } } } } + } + break; + } - /* - Test that break and continue are only used within loop blocks - */ - if (contains(command, L"break", L"continue")) - { - int found_loop=0; - int i; - for (i=count-1; i>=0; i--) - { - if ((block_type[i]==WHILE) || - (block_type[i]==FOR)) - { - found_loop=1; - break; - } - } - - if (!found_loop) - { - /* - Peek to see if the next argument is - --help, in which case we'll allow it to - show the help. - */ - - int old_pos = tok_get_pos(&tok); - int is_help = 0; - - tok_next(&tok); - if (tok_last_type(&tok) == TOK_STRING) - { - wcstring first_arg = tok_last(&tok); - if (expand_one(first_arg, EXPAND_SKIP_CMDSUBST) && parser_t::is_help(first_arg.c_str(), 3)) - { - is_help = 1; - } - } - - tok_set_pos(&tok, old_pos); - - if (!is_help) - { - err=1; - - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - INVALID_LOOP_ERR_MSG); - print_errors(*out, prefix); - } - } - } - } - - /* - Test that else and else-if are only used directly in an if-block - */ - if (command == L"else") - { - if (!count || block_type[count-1]!=IF) - { - err=1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - INVALID_ELSE_ERR_MSG, - command.c_str()); - - print_errors(*out, prefix); - } - } - } - - /* - Test that end is not used when not inside any block - */ - if (count < 0) + case TOK_REDIRECT_OUT: + case TOK_REDIRECT_IN: + case TOK_REDIRECT_APPEND: + case TOK_REDIRECT_FD: + case TOK_REDIRECT_NOCLOB: + { + if (!had_cmd) { err = 1; if (out) { error(SYNTAX_ERROR, tok_get_pos(&tok), - INVALID_END_ERR_MSG); + INVALID_REDIRECTION_ERR_MSG); print_errors(*out, prefix); - const wcstring h = builtin_help_get(*this, L"end"); - if (h.size()) - append_format(*out, L"%ls", h.c_str()); } } - + break; } - else - { - err |= parser_test_argument(tok_last(&tok), out, prefix, tok_get_pos(&tok)); - /* If possible, keep track of number of supplied arguments */ - if (arg_count >= 0 && expand_is_clean(tok_last(&tok))) + case TOK_END: + { + if (needs_cmd && !had_cmd) { - arg_count++; + err = 1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + CMD_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); + print_errors(*out, prefix); + } + } + needs_cmd = false; + had_cmd = 0; + is_pipeline=0; + forbid_pipeline=0; + end_of_cmd = 1; + + break; + } + + case TOK_PIPE: + { + if (!had_cmd) + { + err=1; + if (out) + { + if (tok_get_pos(&tok)>0 && buff[tok_get_pos(&tok)-1] == L'|') + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + CMD_OR_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); + + } + else + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + CMD_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); + } + + print_errors(*out, prefix); + } + } + else if (forbid_pipeline) + { + err=1; + if (out) + { + error(SYNTAX_ERROR, + tok_get_pos(&tok), + EXEC_ERR_MSG); + + print_errors(*out, prefix); + } } else { - arg_count = -1; + needs_cmd = true; + is_pipeline=1; + had_cmd=0; + end_of_cmd = 1; + } + break; + } - if (has_command) + case TOK_BACKGROUND: + { + if (!had_cmd) { - - /* - Try to make sure the second argument to 'for' is 'in' - */ - if (command == L"for") + err = 1; + if (out) { - if (arg_count == 1) + if (tok_get_pos(&tok)>0 && buff[tok_get_pos(&tok)-1] == L'&') { - - if (wcsvarname(tok_last(&tok))) - { - - err = 1; - - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - BUILTIN_FOR_ERR_NAME, - L"for", - tok_last(&tok)); - - print_errors(*out, prefix); - } - } + error(SYNTAX_ERROR, + tok_get_pos(&tok), + CMD_AND_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); } - else if (arg_count == 2) + else { - if (wcscmp(tok_last(&tok), L"in") != 0) - { - err = 1; - - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - BUILTIN_FOR_ERR_IN, - L"for"); - - print_errors(*out, prefix); - } - } + error(SYNTAX_ERROR, + tok_get_pos(&tok), + CMD_ERR_MSG, + tok_get_desc(tok_last_type(&tok))); } - } - else if (command == L"else") - { - if (arg_count == 1) - { - /* Any second argument must be "if" */ - if (wcscmp(tok_last(&tok), L"if") != 0) - { - err = 1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - BUILTIN_ELSEIF_ERR_ARGUMENT, - L"else"); - print_errors(*out, prefix); - } - } - else - { - /* Successfully detected "else if". Now we need a new command. */ - needs_cmd = true; - had_cmd = false; - } - } + print_errors(*out, prefix); } } - } - - break; - } - - case TOK_REDIRECT_OUT: - case TOK_REDIRECT_IN: - case TOK_REDIRECT_APPEND: - case TOK_REDIRECT_FD: - case TOK_REDIRECT_NOCLOB: - { - if (!had_cmd) - { - err = 1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - INVALID_REDIRECTION_ERR_MSG); - print_errors(*out, prefix); - } - } - break; - } - - case TOK_END: - { - if (needs_cmd && !had_cmd) - { - err = 1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - CMD_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); - print_errors(*out, prefix); - } - } - needs_cmd = false; - had_cmd = 0; - is_pipeline=0; - forbid_pipeline=0; - end_of_cmd = 1; - - break; - } - - case TOK_PIPE: - { - if (!had_cmd) - { - err=1; - if (out) - { - if (tok_get_pos(&tok)>0 && buff[tok_get_pos(&tok)-1] == L'|') - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - CMD_OR_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); - - } - else - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - CMD_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); - } - - print_errors(*out, prefix); - } - } - else if (forbid_pipeline) - { - err=1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - EXEC_ERR_MSG); - - print_errors(*out, prefix); - } - } - else - { - needs_cmd = true; - is_pipeline=1; - had_cmd=0; + had_cmd = 0; end_of_cmd = 1; + break; } - break; - } - case TOK_BACKGROUND: - { - if (!had_cmd) - { - err = 1; - if (out) + case TOK_ERROR: + default: + if (tok_get_error(&tok) == TOK_UNTERMINATED_QUOTE) { - if (tok_get_pos(&tok)>0 && buff[tok_get_pos(&tok)-1] == L'&') + unfinished = 1; + } + else + { + err = 1; + if (out) { error(SYNTAX_ERROR, tok_get_pos(&tok), - CMD_AND_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); + TOK_ERR_MSG, + tok_last(&tok)); + + print_errors(*out, prefix); } - else - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - CMD_ERR_MSG, - tok_get_desc(tok_last_type(&tok))); - } - - print_errors(*out, prefix); } - } - had_cmd = 0; - end_of_cmd = 1; - - break; - } - - case TOK_ERROR: - default: - if (tok_get_error(&tok) == TOK_UNTERMINATED_QUOTE) - { - unfinished = 1; - } - else - { - err = 1; - if (out) - { - error(SYNTAX_ERROR, - tok_get_pos(&tok), - TOK_ERR_MSG, - tok_last(&tok)); - - - print_errors(*out, prefix); - } - } - - break; + break; } if (end_of_cmd) diff --git a/path.cpp b/path.cpp index bea3fe3de..ef53b1f98 100644 --- a/path.cpp +++ b/path.cpp @@ -110,18 +110,18 @@ static bool path_get_path_core(const wcstring &cmd, wcstring *out_path, const en { switch (errno) { - case ENOENT: - case ENAMETOOLONG: - case EACCES: - case ENOTDIR: - break; - default: - { - debug(1, - MISSING_COMMAND_ERR_MSG, - nxt_path.c_str()); - wperror(L"access"); - } + case ENOENT: + case ENAMETOOLONG: + case EACCES: + case ENOTDIR: + break; + default: + { + debug(1, + MISSING_COMMAND_ERR_MSG, + nxt_path.c_str()); + wperror(L"access"); + } } } } diff --git a/postfork.cpp b/postfork.cpp index 32dd44558..495845e8b 100644 --- a/postfork.cpp +++ b/postfork.cpp @@ -176,36 +176,55 @@ static int handle_child_io(io_chain_t &io_chain) switch (io->io_mode) { - case IO_CLOSE: - { - if (close(io->fd)) + case IO_CLOSE: { - debug_safe_int(0, "Failed to close file descriptor %s", io->fd); - wperror(L"close"); + if (close(io->fd)) + { + debug_safe_int(0, "Failed to close file descriptor %s", io->fd); + wperror(L"close"); + } + break; } - break; - } - case IO_FILE: - { - // Here we definitely do not want to set CLO_EXEC because our child needs access - if ((tmp=open(io->filename_cstr, - io->param2.flags, OPEN_MASK))==-1) + case IO_FILE: { - if ((io->param2.flags & O_EXCL) && - (errno ==EEXIST)) + // Here we definitely do not want to set CLO_EXEC because our child needs access + if ((tmp=open(io->filename_cstr, + io->param2.flags, OPEN_MASK))==-1) { - debug_safe(1, NOCLOB_ERROR, io->filename_cstr); - } - else - { - debug_safe(1, FILE_ERROR, io->filename_cstr); - perror("open"); - } + if ((io->param2.flags & O_EXCL) && + (errno ==EEXIST)) + { + debug_safe(1, NOCLOB_ERROR, io->filename_cstr); + } + else + { + debug_safe(1, FILE_ERROR, io->filename_cstr); + perror("open"); + } - return -1; + return -1; + } + else if (tmp != io->fd) + { + /* + This call will sometimes fail, but that is ok, + this is just a precausion. + */ + close(io->fd); + + if (dup2(tmp, io->fd) == -1) + { + debug_safe_int(1, FD_ERROR, io->fd); + perror("dup2"); + return -1; + } + exec_close(tmp); + } + break; } - else if (tmp != io->fd) + + case IO_FD: { /* This call will sometimes fail, but that is ok, @@ -213,61 +232,42 @@ static int handle_child_io(io_chain_t &io_chain) */ close(io->fd); - if (dup2(tmp, io->fd) == -1) + if (dup2(io->param1.old_fd, io->fd) == -1) { - debug_safe_int(1, FD_ERROR, io->fd); + debug_safe_int(1, FD_ERROR, io->fd); + wperror(L"dup2"); + return -1; + } + break; + } + + case IO_BUFFER: + case IO_PIPE: + { + /* If write_pipe_idx is 0, it means we're connecting to the read end (first pipe fd). If it's 1, we're connecting to the write end (second pipe fd). */ + unsigned int write_pipe_idx = (io->is_input ? 0 : 1); + /* + debug( 0, + L"%ls %ls on fd %d (%d %d)", + write_pipe?L"write":L"read", + (io->io_mode == IO_BUFFER)?L"buffer":L"pipe", + io->fd, + io->param1.pipe_fd[0], + io->param1.pipe_fd[1]); + */ + if (dup2(io->param1.pipe_fd[write_pipe_idx], io->fd) != io->fd) + { + debug_safe(1, LOCAL_PIPE_ERROR); perror("dup2"); return -1; } - exec_close(tmp); + + if (io->param1.pipe_fd[0] >= 0) + exec_close(io->param1.pipe_fd[0]); + if (io->param1.pipe_fd[1] >= 0) + exec_close(io->param1.pipe_fd[1]); + break; } - break; - } - - case IO_FD: - { - /* - This call will sometimes fail, but that is ok, - this is just a precausion. - */ - close(io->fd); - - if (dup2(io->param1.old_fd, io->fd) == -1) - { - debug_safe_int(1, FD_ERROR, io->fd); - wperror(L"dup2"); - return -1; - } - break; - } - - case IO_BUFFER: - case IO_PIPE: - { - /* If write_pipe_idx is 0, it means we're connecting to the read end (first pipe fd). If it's 1, we're connecting to the write end (second pipe fd). */ - unsigned int write_pipe_idx = (io->is_input ? 0 : 1); - /* - debug( 0, - L"%ls %ls on fd %d (%d %d)", - write_pipe?L"write":L"read", - (io->io_mode == IO_BUFFER)?L"buffer":L"pipe", - io->fd, - io->param1.pipe_fd[0], - io->param1.pipe_fd[1]); - */ - if (dup2(io->param1.pipe_fd[write_pipe_idx], io->fd) != io->fd) - { - debug_safe(1, LOCAL_PIPE_ERROR); - perror("dup2"); - return -1; - } - - if (io->param1.pipe_fd[0] >= 0) - exec_close(io->param1.pipe_fd[0]); - if (io->param1.pipe_fd[1] >= 0) - exec_close(io->param1.pipe_fd[1]); - break; - } } } @@ -456,52 +456,52 @@ bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr, posix_spawn_fil } switch (io->io_mode) { - case IO_CLOSE: - { - if (! err) - err = posix_spawn_file_actions_addclose(actions, io->fd); - break; - } - - case IO_FILE: - { - if (! err) - err = posix_spawn_file_actions_addopen(actions, io->fd, io->filename_cstr, io->param2.flags /* mode */, OPEN_MASK); - break; - } - - case IO_FD: - { - if (! err) - err = posix_spawn_file_actions_adddup2(actions, io->param1.old_fd /* from */, io->fd /* to */); - break; - } - - case IO_BUFFER: - case IO_PIPE: - { - unsigned int write_pipe_idx = (io->is_input ? 0 : 1); - int from_fd = io->param1.pipe_fd[write_pipe_idx]; - int to_fd = io->fd; - if (! err) - err = posix_spawn_file_actions_adddup2(actions, from_fd, to_fd); - - - if (write_pipe_idx > 0) + case IO_CLOSE: { if (! err) - err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]); - if (! err) - err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[1]); + err = posix_spawn_file_actions_addclose(actions, io->fd); + break; } - else + + case IO_FILE: { if (! err) - err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]); - + err = posix_spawn_file_actions_addopen(actions, io->fd, io->filename_cstr, io->param2.flags /* mode */, OPEN_MASK); + break; + } + + case IO_FD: + { + if (! err) + err = posix_spawn_file_actions_adddup2(actions, io->param1.old_fd /* from */, io->fd /* to */); + break; + } + + case IO_BUFFER: + case IO_PIPE: + { + unsigned int write_pipe_idx = (io->is_input ? 0 : 1); + int from_fd = io->param1.pipe_fd[write_pipe_idx]; + int to_fd = io->fd; + if (! err) + err = posix_spawn_file_actions_adddup2(actions, from_fd, to_fd); + + + if (write_pipe_idx > 0) + { + if (! err) + err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]); + if (! err) + err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[1]); + } + else + { + if (! err) + err = posix_spawn_file_actions_addclose(actions, io->param1.pipe_fd[0]); + + } + break; } - break; - } } } @@ -523,81 +523,81 @@ void safe_report_exec_error(int err, const char *actual_cmd, char **argv, char * switch (err) { - case E2BIG: - { - char sz1[128], sz2[128]; - - long arg_max = -1; - - size_t sz = 0; - char **p; - for (p=argv; *p; p++) + case E2BIG: { - sz += strlen(*p)+1; + char sz1[128], sz2[128]; + + long arg_max = -1; + + size_t sz = 0; + char **p; + for (p=argv; *p; p++) + { + sz += strlen(*p)+1; + } + + for (p=envv; *p; p++) + { + sz += strlen(*p)+1; + } + + format_size_safe(sz1, sz); + arg_max = sysconf(_SC_ARG_MAX); + + if (arg_max > 0) + { + format_size_safe(sz2, sz); + debug_safe(0, "The total size of the argument and environment lists %s exceeds the operating system limit of %s.", sz1, sz2); + } + else + { + debug_safe(0, "The total size of the argument and environment lists (%s) exceeds the operating system limit.", sz1); + } + + debug_safe(0, "Try running the command again with fewer arguments."); + break; } - for (p=envv; *p; p++) + case ENOEXEC: { - sz += strlen(*p)+1; + /* Hope strerror doesn't allocate... */ + const char *err = strerror(errno); + debug_safe(0, "exec: %s", err); + + debug_safe(0, "The file '%s' is marked as an executable but could not be run by the operating system.", actual_cmd); + break; } - format_size_safe(sz1, sz); - arg_max = sysconf(_SC_ARG_MAX); - - if (arg_max > 0) + case ENOENT: { - format_size_safe(sz2, sz); - debug_safe(0, "The total size of the argument and environment lists %s exceeds the operating system limit of %s.", sz1, sz2); - } - else - { - debug_safe(0, "The total size of the argument and environment lists (%s) exceeds the operating system limit.", sz1); + /* ENOENT is returned by exec() when the path fails, but also returned by posix_spawn if an open file action fails. These cases appear to be impossible to distinguish. We address this by not using posix_spawn for file redirections, so all the ENOENTs we find must be errors from exec(). */ + char interpreter_buff[128] = {}, *interpreter; + interpreter = get_interpreter(actual_cmd, interpreter_buff, sizeof interpreter_buff); + if (interpreter && 0 != access(interpreter, X_OK)) + { + debug_safe(0, "The file '%s' specified the interpreter '%s', which is not an executable command.", actual_cmd, interpreter); + } + else + { + debug_safe(0, "The file '%s' does not exist or could not be executed.", actual_cmd); + } + break; } - debug_safe(0, "Try running the command again with fewer arguments."); - break; - } - - case ENOEXEC: - { - /* Hope strerror doesn't allocate... */ - const char *err = strerror(errno); - debug_safe(0, "exec: %s", err); - - debug_safe(0, "The file '%s' is marked as an executable but could not be run by the operating system.", actual_cmd); - break; - } - - case ENOENT: - { - /* ENOENT is returned by exec() when the path fails, but also returned by posix_spawn if an open file action fails. These cases appear to be impossible to distinguish. We address this by not using posix_spawn for file redirections, so all the ENOENTs we find must be errors from exec(). */ - char interpreter_buff[128] = {}, *interpreter; - interpreter = get_interpreter(actual_cmd, interpreter_buff, sizeof interpreter_buff); - if (interpreter && 0 != access(interpreter, X_OK)) + case ENOMEM: { - debug_safe(0, "The file '%s' specified the interpreter '%s', which is not an executable command.", actual_cmd, interpreter); + debug_safe(0, "Out of memory"); + break; } - else + + default: { - debug_safe(0, "The file '%s' does not exist or could not be executed.", actual_cmd); + /* Hope strerror doesn't allocate... */ + const char *err = strerror(errno); + debug_safe(0, "exec: %s", err); + + // debug(0, L"The file '%ls' is marked as an executable but could not be run by the operating system.", p->actual_cmd); + break; } - break; - } - - case ENOMEM: - { - debug_safe(0, "Out of memory"); - break; - } - - default: - { - /* Hope strerror doesn't allocate... */ - const char *err = strerror(errno); - debug_safe(0, "exec: %s", err); - - // debug(0, L"The file '%ls' is marked as an executable but could not be run by the operating system.", p->actual_cmd); - break; - } } } diff --git a/proc.cpp b/proc.cpp index 9a1a0cdc7..b061bdd82 100644 --- a/proc.cpp +++ b/proc.cpp @@ -566,16 +566,16 @@ void job_handle_signal(int signal, siginfo_t *info, void *con) { switch (pid=waitpid(-1,&status,WUNTRACED|WNOHANG)) { - case 0: - case -1: - { - errno=errno_old; - return; - } - default: + case 0: + case -1: + { + errno=errno_old; + return; + } + default: - handle_child_status(pid, status); - break; + handle_child_status(pid, status); + break; } } kill(0, SIGIO); @@ -1123,45 +1123,45 @@ void job_continue(job_t *j, int cont) // debug( 1, L"select_try()" ); switch (select_try(j)) { - case 1: - { - read_try(j); - break; - } - - case -1: - { - /* - If there is no funky IO magic, we can use - waitpid instead of handling child deaths - through signals. This gives a rather large - speed boost (A factor 3 startup time - improvement on my 300 MHz machine) on - short-lived jobs. - */ - int status; - pid_t pid = waitpid(-1, &status, WUNTRACED); - if (pid > 0) + case 1: { - handle_child_status(pid, status); + read_try(j); + break; } - else + + case -1: { /* - This probably means we got a - signal. A signal might mean that the - terminal emulator sent us a hup - signal to tell is to close. If so, - we should exit. + If there is no funky IO magic, we can use + waitpid instead of handling child deaths + through signals. This gives a rather large + speed boost (A factor 3 startup time + improvement on my 300 MHz machine) on + short-lived jobs. */ - if (reader_exit_forced()) + int status; + pid_t pid = waitpid(-1, &status, WUNTRACED); + if (pid > 0) { - quit = 1; + handle_child_status(pid, status); } + else + { + /* + This probably means we got a + signal. A signal might mean that the + terminal emulator sent us a hup + signal to tell is to close. If so, + we should exit. + */ + if (reader_exit_forced()) + { + quit = 1; + } + } + break; } - break; - } } } diff --git a/reader.cpp b/reader.cpp index 71f49dd3d..79775dab2 100644 --- a/reader.cpp +++ b/reader.cpp @@ -1443,35 +1443,35 @@ static bool handle_completions(const std::vector &comp) */ switch (comp.size()) { - /* No suitable completions found, flash screen and return */ - case 0: - { - reader_flash(); - done = true; - success = false; - break; - } - - /* Exactly one suitable completion found - insert it */ - case 1: - { - - const completion_t &c = comp.at(0); - - /* - If this is a replacement completion, check - that we know how to replace it, e.g. that - the token doesn't contain evil operators - like {} - */ - if (! c.is_case_insensitive() || reader_can_replace(tok, c.flags)) + /* No suitable completions found, flash screen and return */ + case 0: { - completion_insert(c.completion.c_str(), c.flags); + reader_flash(); + done = true; + success = false; + break; + } + + /* Exactly one suitable completion found - insert it */ + case 1: + { + + const completion_t &c = comp.at(0); + + /* + If this is a replacement completion, check + that we know how to replace it, e.g. that + the token doesn't contain evil operators + like {} + */ + if (! c.is_case_insensitive() || reader_can_replace(tok, c.flags)) + { + completion_insert(c.completion.c_str(), c.flags); + } + done = true; + success = true; + break; } - done = true; - success = true; - break; - } } @@ -1902,27 +1902,27 @@ static void handle_token_history(int forward, int reset) { switch (tok_last_type(&tok)) { - case TOK_STRING: - { - if (wcsstr(tok_last(&tok), data->search_buff.c_str())) + case TOK_STRING: { - //debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) ); - if (tok_get_pos(&tok) >= current_pos) + if (wcsstr(tok_last(&tok), data->search_buff.c_str())) { - break; - } - //debug( 3, L"ok pos" ); + //debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) ); + if (tok_get_pos(&tok) >= current_pos) + { + break; + } + //debug( 3, L"ok pos" ); - const wcstring last_tok = tok_last(&tok); - if (find(data->search_prev.begin(), data->search_prev.end(), last_tok) == data->search_prev.end()) - { - data->token_history_pos = tok_get_pos(&tok); - str = wcsdup(tok_last(&tok)); - } + const wcstring last_tok = tok_last(&tok); + if (find(data->search_prev.begin(), data->search_prev.end(), last_tok) == data->search_prev.end()) + { + data->token_history_pos = tok_get_pos(&tok); + str = wcsdup(tok_last(&tok)); + } + } } } - } } tok_destroy(&tok); @@ -1966,24 +1966,24 @@ public: /* Note fall-through in all of these! */ switch (state) { - case s_whitespace: - if (iswspace(c)) - return true; - state = s_punctuation; + case s_whitespace: + if (iswspace(c)) + return true; + state = s_punctuation; - case s_punctuation: - if (iswpunct(c)) - return true; - state = s_alphanumeric_or_punctuation_except_slash; + case s_punctuation: + if (iswpunct(c)) + return true; + state = s_alphanumeric_or_punctuation_except_slash; - case s_alphanumeric_or_punctuation_except_slash: - if (c != L'/' && (iswalnum(c) || iswpunct(c))) - return true; - state = s_end; + case s_alphanumeric_or_punctuation_except_slash: + if (c != L'/' && (iswalnum(c) || iswpunct(c))) + return true; + state = s_end; - case s_end: - default: - return false; + case s_end: + default: + return false; } } }; @@ -2006,6 +2006,7 @@ public: Interesting test case: /foo/bar/baz/ -> /foo/bar/ -> /foo/ -> / echo --foo --bar -> echo --foo -> echo + echo hi>/dev/null -> echo hi>/dev/ -> echo hi >/ -> echo hi > -> echo hi -> echo */ enum move_word_dir_t { @@ -2750,168 +2751,191 @@ const wchar_t *reader_readline() switch (c) { - /* go to beginning of line*/ - case R_BEGINNING_OF_LINE: - { - while ((data->buff_pos>0) && - (buff[data->buff_pos-1] != L'\n')) + /* go to beginning of line*/ + case R_BEGINNING_OF_LINE: { - data->buff_pos--; - } - - reader_repaint(); - break; - } - - case R_END_OF_LINE: - { - while (buff[data->buff_pos] && - buff[data->buff_pos] != L'\n') - { - data->buff_pos++; - } - - reader_repaint(); - break; - } - - - case R_BEGINNING_OF_BUFFER: - { - data->buff_pos = 0; - - reader_repaint(); - break; - } - - /* go to EOL*/ - case R_END_OF_BUFFER: - { - data->buff_pos = data->command_length(); - - reader_repaint(); - break; - } - - case R_NULL: - { - reader_repaint_if_needed(); - break; - } - - case R_REPAINT: - { - exec_prompt(); - write_loop(1, "\r", 1); - s_reset(&data->screen, false); - reader_repaint(); - break; - } - - case R_EOF: - { - exit_forced = 1; - data->end_loop=1; - break; - } - - /* complete */ - case R_COMPLETE: - { - - if (!data->complete_func) - break; - - if (! comp_empty && last_char == R_COMPLETE) - { - /* The user typed R_COMPLETE more than once in a row. Cycle through our available completions */ - const completion_t *next_comp = cycle_competions(comp, cycle_command_line, &completion_cycle_idx); - if (next_comp != NULL) + while ((data->buff_pos>0) && + (buff[data->buff_pos-1] != L'\n')) { - size_t cursor_pos = cycle_cursor_pos; - const wcstring new_cmd_line = completion_apply_to_command_line(next_comp->completion, next_comp->flags, cycle_command_line, &cursor_pos); - reader_set_buffer(new_cmd_line, cursor_pos); - - /* Since we just inserted a completion, don't immediately do a new autosuggestion */ - data->suppress_autosuggestion = true; - } - } - else - { - /* Either the user hit tab only once, or we had no visible completion list. */ - const wchar_t *begin, *end; - const wchar_t *token_begin, *token_end; - const wchar_t *buff = data->command_line.c_str(); - long cursor_steps; - - /* Clear the completion list */ - comp.clear(); - - parse_util_cmdsubst_extent(buff, data->buff_pos, &begin, &end); - - parse_util_token_extent(begin, data->buff_pos - (begin-buff), &token_begin, &token_end, 0, 0); - - cursor_steps = token_end - buff- data->buff_pos; - data->buff_pos += cursor_steps; - if (is_backslashed(buff, data->buff_pos)) - { - remove_backward(); + data->buff_pos--; } reader_repaint(); - - size_t len = data->buff_pos - (begin-buff); - const wcstring buffcpy = wcstring(begin, len); - - data->complete_func(buffcpy, comp, COMPLETE_DEFAULT, NULL); - - /* Munge our completions */ - sort(comp.begin(), comp.end()); - remove_duplicates(comp); - prioritize_completions(comp); - - /* Record our cycle_command_line */ - cycle_command_line = data->command_line; - cycle_cursor_pos = data->buff_pos; - - comp_empty = handle_completions(comp); - - /* Start the cycle at the beginning */ - completion_cycle_idx = (size_t)(-1); + break; } - break; - } - - /* kill */ - case R_KILL_LINE: - { - const wchar_t *buff = data->command_line.c_str(); - const wchar_t *begin = &buff[data->buff_pos]; - const wchar_t *end = begin; - - while (*end && *end != L'\n') - end++; - - if (end==begin && *end) - end++; - - size_t len = end-begin; - if (len) + case R_END_OF_LINE: { - reader_kill(begin - buff, len, KILL_APPEND, last_char!=R_KILL_LINE); + while (buff[data->buff_pos] && + buff[data->buff_pos] != L'\n') + { + data->buff_pos++; + } + + reader_repaint(); + break; } - break; - } - case R_BACKWARD_KILL_LINE: - { - if (data->buff_pos > 0) + case R_BEGINNING_OF_BUFFER: + { + data->buff_pos = 0; + + reader_repaint(); + break; + } + + /* go to EOL*/ + case R_END_OF_BUFFER: + { + data->buff_pos = data->command_length(); + + reader_repaint(); + break; + } + + case R_NULL: + { + reader_repaint_if_needed(); + break; + } + + case R_REPAINT: + { + exec_prompt(); + write_loop(1, "\r", 1); + s_reset(&data->screen, false); + reader_repaint(); + break; + } + + case R_EOF: + { + exit_forced = 1; + data->end_loop=1; + break; + } + + /* complete */ + case R_COMPLETE: + { + + if (!data->complete_func) + break; + + if (! comp_empty && last_char == R_COMPLETE) + { + /* The user typed R_COMPLETE more than once in a row. Cycle through our available completions */ + const completion_t *next_comp = cycle_competions(comp, cycle_command_line, &completion_cycle_idx); + if (next_comp != NULL) + { + size_t cursor_pos = cycle_cursor_pos; + const wcstring new_cmd_line = completion_apply_to_command_line(next_comp->completion, next_comp->flags, cycle_command_line, &cursor_pos); + reader_set_buffer(new_cmd_line, cursor_pos); + + /* Since we just inserted a completion, don't immediately do a new autosuggestion */ + data->suppress_autosuggestion = true; + } + } + else + { + /* Either the user hit tab only once, or we had no visible completion list. */ + const wchar_t *begin, *end; + const wchar_t *token_begin, *token_end; + const wchar_t *buff = data->command_line.c_str(); + long cursor_steps; + + /* Clear the completion list */ + comp.clear(); + + parse_util_cmdsubst_extent(buff, data->buff_pos, &begin, &end); + + parse_util_token_extent(begin, data->buff_pos - (begin-buff), &token_begin, &token_end, 0, 0); + + cursor_steps = token_end - buff- data->buff_pos; + data->buff_pos += cursor_steps; + if (is_backslashed(buff, data->buff_pos)) + { + remove_backward(); + } + + reader_repaint(); + + size_t len = data->buff_pos - (begin-buff); + const wcstring buffcpy = wcstring(begin, len); + + data->complete_func(buffcpy, comp, COMPLETE_DEFAULT, NULL); + + /* Munge our completions */ + sort(comp.begin(), comp.end()); + remove_duplicates(comp); + prioritize_completions(comp); + + /* Record our cycle_command_line */ + cycle_command_line = data->command_line; + cycle_cursor_pos = data->buff_pos; + + comp_empty = handle_completions(comp); + + /* Start the cycle at the beginning */ + completion_cycle_idx = (size_t)(-1); + } + + break; + } + + /* kill */ + case R_KILL_LINE: + { + const wchar_t *buff = data->command_line.c_str(); + const wchar_t *begin = &buff[data->buff_pos]; + const wchar_t *end = begin; + + while (*end && *end != L'\n') + end++; + + if (end==begin && *end) + end++; + + size_t len = end-begin; + if (len) + { + reader_kill(begin - buff, len, KILL_APPEND, last_char!=R_KILL_LINE); + } + + break; + } + + case R_BACKWARD_KILL_LINE: + { + if (data->buff_pos > 0) + { + const wchar_t *buff = data->command_line.c_str(); + const wchar_t *end = &buff[data->buff_pos]; + const wchar_t *begin = end; + + while (begin > buff && *begin != L'\n') + begin--; + + if (*begin == L'\n') + begin++; + + size_t len = maxi(end-begin, 1); + begin = end - len; + + reader_kill(begin - buff, len, KILL_PREPEND, last_char!=R_BACKWARD_KILL_LINE); + + } + break; + + } + + case R_KILL_WHOLE_LINE: { const wchar_t *buff = data->command_line.c_str(); const wchar_t *end = &buff[data->buff_pos]; const wchar_t *begin = end; + size_t len; while (begin > buff && *begin != L'\n') begin--; @@ -2919,419 +2943,396 @@ const wchar_t *reader_readline() if (*begin == L'\n') begin++; - size_t len = maxi(end-begin, 1); + len = maxi(end-begin, 0); begin = end - len; - reader_kill(begin - buff, len, KILL_PREPEND, last_char!=R_BACKWARD_KILL_LINE); + while (*end && *end != L'\n') + end++; - } - break; + if (begin == end && *end) + end++; - } + len = end-begin; - case R_KILL_WHOLE_LINE: - { - const wchar_t *buff = data->command_line.c_str(); - const wchar_t *end = &buff[data->buff_pos]; - const wchar_t *begin = end; - size_t len; + if (len) + { + reader_kill(begin - buff, len, KILL_APPEND, last_char!=R_KILL_WHOLE_LINE); + } - while (begin > buff && *begin != L'\n') - begin--; - - if (*begin == L'\n') - begin++; - - len = maxi(end-begin, 0); - begin = end - len; - - while (*end && *end != L'\n') - end++; - - if (begin == end && *end) - end++; - - len = end-begin; - - if (len) - { - reader_kill(begin - buff, len, KILL_APPEND, last_char!=R_KILL_WHOLE_LINE); + break; } - break; - } - - /* yank*/ - case R_YANK: - { - yank_str = kill_yank(); - insert_string(yank_str); - yank_len = wcslen(yank_str); - break; - } - - /* rotate killring*/ - case R_YANK_POP: - { - if (yank_len) + /* yank*/ + case R_YANK: { - for (size_t i=0; isearch_mode) - { - data->search_mode= NO_SEARCH; - - if (data->token_history_pos==-1) - { - //history_reset(); - data->history_search.go_to_end(); - reader_set_buffer(data->search_buff.c_str(), data->search_buff.size()); - } - else - { - reader_replace_current_token(data->search_buff.c_str()); - } - data->search_buff.clear(); - reader_super_highlight_me_plenty(data->buff_pos); - reader_repaint(); - - } - - break; - } - - /* delete backward*/ - case R_BACKWARD_DELETE_CHAR: - { - remove_backward(); - break; - } - - /* delete forward*/ - case R_DELETE_CHAR: - { - /** - Remove the current character in the character buffer and on the - screen using syntax highlighting, etc. - */ - if (data->buff_pos < data->command_length()) - { - data->buff_pos++; - remove_backward(); - } - break; - } - - /* - Evaluate. If the current command is unfinished, or if - the charater is escaped using a backslash, insert a - newline - */ - case R_EXECUTE: - { - /* Delete any autosuggestion */ - data->autosuggestion.clear(); - - /* - Allow backslash-escaped newlines - */ - if (is_backslashed(data->command_line.c_str(), data->buff_pos)) - { - insert_char('\n'); break; } - switch (data->test_func(data->command_line.c_str())) + /* rotate killring*/ + case R_YANK_POP: { - - case 0: - { - /* - Finished commend, execute it - */ - if (! data->command_line.empty()) + if (yank_len) { - if (data->history) + for (size_t i=0; isearch_mode) + { + data->search_mode= NO_SEARCH; + + if (data->token_history_pos==-1) { - data->history->add_with_file_detection(data->command_line); - } - } - finished=1; - data->buff_pos=data->command_length(); - reader_repaint(); - break; - } - - /* - We are incomplete, continue editing - */ - case PARSER_TEST_INCOMPLETE: - { - insert_char('\n'); - break; - } - - /* - Result must be some combination including an - error. The error message will already be - printed, all we need to do is repaint - */ - default: - { - s_reset(&data->screen, true); - reader_repaint(); - break; - } - - } - - break; - } - - /* History functions */ - case R_HISTORY_SEARCH_BACKWARD: - case R_HISTORY_TOKEN_SEARCH_BACKWARD: - case R_HISTORY_SEARCH_FORWARD: - case R_HISTORY_TOKEN_SEARCH_FORWARD: - { - int reset = 0; - - if (data->search_mode == NO_SEARCH) - { - reset = 1; - if ((c == R_HISTORY_SEARCH_BACKWARD) || - (c == R_HISTORY_SEARCH_FORWARD)) - { - data->search_mode = LINE_SEARCH; - } - else - { - data->search_mode = TOKEN_SEARCH; - } - - data->search_buff.append(data->command_line); - data->history_search = history_search_t(*data->history, data->search_buff, HISTORY_SEARCH_TYPE_CONTAINS); - - /* Skip the autosuggestion as history */ - const wcstring &suggest = data->autosuggestion; - if (! suggest.empty()) - { - data->history_search.skip_matches(wcstring_list_t(&suggest, 1 + &suggest)); - } - } - - switch (data->search_mode) - { - - case LINE_SEARCH: - { - if ((c == R_HISTORY_SEARCH_BACKWARD) || - (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) - { - data->history_search.go_backwards(); - } - else - { - if (! data->history_search.go_forwards()) - { - /* If you try to go forwards past the end, we just go to the end */ + //history_reset(); data->history_search.go_to_end(); + reader_set_buffer(data->search_buff.c_str(), data->search_buff.size()); + } + else + { + reader_replace_current_token(data->search_buff.c_str()); + } + data->search_buff.clear(); + reader_super_highlight_me_plenty(data->buff_pos); + reader_repaint(); + + } + + break; + } + + /* delete backward*/ + case R_BACKWARD_DELETE_CHAR: + { + remove_backward(); + break; + } + + /* delete forward*/ + case R_DELETE_CHAR: + { + /** + Remove the current character in the character buffer and on the + screen using syntax highlighting, etc. + */ + if (data->buff_pos < data->command_length()) + { + data->buff_pos++; + remove_backward(); + } + break; + } + + /* + Evaluate. If the current command is unfinished, or if + the charater is escaped using a backslash, insert a + newline + */ + case R_EXECUTE: + { + /* Delete any autosuggestion */ + data->autosuggestion.clear(); + + /* + Allow backslash-escaped newlines + */ + if (is_backslashed(data->command_line.c_str(), data->buff_pos)) + { + insert_char('\n'); + break; + } + + switch (data->test_func(data->command_line.c_str())) + { + + case 0: + { + /* + Finished commend, execute it + */ + if (! data->command_line.empty()) + { + if (data->history) + { + data->history->add_with_file_detection(data->command_line); + } + } + finished=1; + data->buff_pos=data->command_length(); + reader_repaint(); + break; + } + + /* + We are incomplete, continue editing + */ + case PARSER_TEST_INCOMPLETE: + { + insert_char('\n'); + break; + } + + /* + Result must be some combination including an + error. The error message will already be + printed, all we need to do is repaint + */ + default: + { + s_reset(&data->screen, true); + reader_repaint(); + break; + } + + } + + break; + } + + /* History functions */ + case R_HISTORY_SEARCH_BACKWARD: + case R_HISTORY_TOKEN_SEARCH_BACKWARD: + case R_HISTORY_SEARCH_FORWARD: + case R_HISTORY_TOKEN_SEARCH_FORWARD: + { + int reset = 0; + + if (data->search_mode == NO_SEARCH) + { + reset = 1; + if ((c == R_HISTORY_SEARCH_BACKWARD) || + (c == R_HISTORY_SEARCH_FORWARD)) + { + data->search_mode = LINE_SEARCH; + } + else + { + data->search_mode = TOKEN_SEARCH; + } + + data->search_buff.append(data->command_line); + data->history_search = history_search_t(*data->history, data->search_buff, HISTORY_SEARCH_TYPE_CONTAINS); + + /* Skip the autosuggestion as history */ + const wcstring &suggest = data->autosuggestion; + if (! suggest.empty()) + { + data->history_search.skip_matches(wcstring_list_t(&suggest, 1 + &suggest)); } } - wcstring new_text; - if (data->history_search.is_at_end()) + switch (data->search_mode) { - new_text = data->search_buff; - } - else - { - new_text = data->history_search.current_string(); - } - set_command_line_and_position(new_text, new_text.size()); + case LINE_SEARCH: + { + if ((c == R_HISTORY_SEARCH_BACKWARD) || + (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) + { + data->history_search.go_backwards(); + } + else + { + if (! data->history_search.go_forwards()) + { + /* If you try to go forwards past the end, we just go to the end */ + data->history_search.go_to_end(); + } + } + + wcstring new_text; + if (data->history_search.is_at_end()) + { + new_text = data->search_buff; + } + else + { + new_text = data->history_search.current_string(); + } + set_command_line_and_position(new_text, new_text.size()); + + break; + } + + case TOKEN_SEARCH: + { + if ((c == R_HISTORY_SEARCH_BACKWARD) || + (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) + { + handle_token_history(SEARCH_BACKWARD, reset); + } + else + { + handle_token_history(SEARCH_FORWARD, reset); + } + + break; + } + + } break; } - case TOKEN_SEARCH: + + /* Move left*/ + case R_BACKWARD_CHAR: { - if ((c == R_HISTORY_SEARCH_BACKWARD) || - (c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) + if (data->buff_pos > 0) { - handle_token_history(SEARCH_BACKWARD, reset); + data->buff_pos--; + reader_repaint(); + } + break; + } + + /* Move right*/ + case R_FORWARD_CHAR: + { + if (data->buff_pos < data->command_length()) + { + data->buff_pos++; + reader_repaint(); } else { - handle_token_history(SEARCH_FORWARD, reset); + accept_autosuggestion(); + } + break; + } + + /* kill one word left */ + case R_BACKWARD_KILL_WORD: + { + move_word(MOVE_DIR_LEFT, true /* erase */, last_char!=R_BACKWARD_KILL_WORD); + break; + } + + /* kill one word right */ + case R_KILL_WORD: + { + move_word(MOVE_DIR_RIGHT, true /* erase */, last_char!=R_KILL_WORD); + break; + } + + /* move one word left*/ + case R_BACKWARD_WORD: + { + move_word(MOVE_DIR_LEFT, false /* do not erase */, false); + break; + } + + /* move one word right*/ + case R_FORWARD_WORD: + { + move_word(MOVE_DIR_RIGHT, false /* do not erase */, false); + break; + } + + case R_BEGINNING_OF_HISTORY: + { + data->history_search = history_search_t(*data->history, data->command_line, HISTORY_SEARCH_TYPE_PREFIX); + data->history_search.go_to_beginning(); + if (! data->history_search.is_at_end()) + { + wcstring new_text = data->history_search.current_string(); + set_command_line_and_position(new_text, new_text.size()); } break; } - } - break; - } - - - /* Move left*/ - case R_BACKWARD_CHAR: - { - if (data->buff_pos > 0) + case R_END_OF_HISTORY: { - data->buff_pos--; - reader_repaint(); + data->history_search.go_to_end(); + break; } - break; - } - /* Move right*/ - case R_FORWARD_CHAR: - { - if (data->buff_pos < data->command_length()) + case R_UP_LINE: + case R_DOWN_LINE: { - data->buff_pos++; - reader_repaint(); + int line_old = parse_util_get_line_from_offset(data->command_line, data->buff_pos); + int line_new; + + if (c == R_UP_LINE) + line_new = line_old-1; + else + line_new = line_old+1; + + int line_count = parse_util_lineno(data->command_line.c_str(), data->command_length())-1; + + if (line_new >= 0 && line_new <= line_count) + { + size_t base_pos_new; + size_t base_pos_old; + + int indent_old; + int indent_new; + size_t line_offset_old; + size_t total_offset_new; + + base_pos_new = parse_util_get_offset_from_line(data->command_line, line_new); + + base_pos_old = parse_util_get_offset_from_line(data->command_line, line_old); + + assert(base_pos_new != (size_t)(-1) && base_pos_old != (size_t)(-1)); + indent_old = data->indents.at(base_pos_old); + indent_new = data->indents.at(base_pos_new); + + line_offset_old = data->buff_pos - parse_util_get_offset_from_line(data->command_line, line_old); + total_offset_new = parse_util_get_offset(data->command_line, line_new, line_offset_old - 4*(indent_new-indent_old)); + data->buff_pos = total_offset_new; + reader_repaint(); + } + + break; } - else + + case R_SUPPRESS_AUTOSUGGESTION: + { + data->suppress_autosuggestion = true; + data->autosuggestion.clear(); + reader_repaint(); + break; + } + + case R_ACCEPT_AUTOSUGGESTION: { accept_autosuggestion(); + break; } - break; - } - /* kill one word left */ - case R_BACKWARD_KILL_WORD: - { - move_word(MOVE_DIR_LEFT, true /* erase */, last_char!=R_BACKWARD_KILL_WORD); - break; - } - - /* kill one word right */ - case R_KILL_WORD: - { - move_word(MOVE_DIR_RIGHT, true /* erase */, last_char!=R_KILL_WORD); - break; - } - - /* move one word left*/ - case R_BACKWARD_WORD: - { - move_word(MOVE_DIR_LEFT, false /* do not erase */, false); - break; - } - - /* move one word right*/ - case R_FORWARD_WORD: - { - move_word(MOVE_DIR_RIGHT, false /* do not erase */, false); - break; - } - - case R_BEGINNING_OF_HISTORY: - { - data->history_search = history_search_t(*data->history, data->command_line, HISTORY_SEARCH_TYPE_PREFIX); - data->history_search.go_to_beginning(); - if (! data->history_search.is_at_end()) + /* Other, if a normal character, we add it to the command */ + default: { - wcstring new_text = data->history_search.current_string(); - set_command_line_and_position(new_text, new_text.size()); + + if ((!wchar_private(c)) && (((c>31) || (c==L'\n'))&& (c != 127))) + { + /* Regular character */ + insert_char(c); + } + else + { + /* + Low priority debug message. These can happen if + the user presses an unefined control + sequnece. No reason to report. + */ + debug(2, _(L"Unknown keybinding %d"), c); + } + break; } - break; - } - - case R_END_OF_HISTORY: - { - data->history_search.go_to_end(); - break; - } - - case R_UP_LINE: - case R_DOWN_LINE: - { - int line_old = parse_util_get_line_from_offset(data->command_line, data->buff_pos); - int line_new; - - if (c == R_UP_LINE) - line_new = line_old-1; - else - line_new = line_old+1; - - int line_count = parse_util_lineno(data->command_line.c_str(), data->command_length())-1; - - if (line_new >= 0 && line_new <= line_count) - { - size_t base_pos_new; - size_t base_pos_old; - - int indent_old; - int indent_new; - size_t line_offset_old; - size_t total_offset_new; - - base_pos_new = parse_util_get_offset_from_line(data->command_line, line_new); - - base_pos_old = parse_util_get_offset_from_line(data->command_line, line_old); - - assert(base_pos_new != (size_t)(-1) && base_pos_old != (size_t)(-1)); - indent_old = data->indents.at(base_pos_old); - indent_new = data->indents.at(base_pos_new); - - line_offset_old = data->buff_pos - parse_util_get_offset_from_line(data->command_line, line_old); - total_offset_new = parse_util_get_offset(data->command_line, line_new, line_offset_old - 4*(indent_new-indent_old)); - data->buff_pos = total_offset_new; - reader_repaint(); - } - - break; - } - - case R_SUPPRESS_AUTOSUGGESTION: - { - data->suppress_autosuggestion = true; - data->autosuggestion.clear(); - reader_repaint(); - break; - } - - case R_ACCEPT_AUTOSUGGESTION: - { - accept_autosuggestion(); - break; - } - - /* Other, if a normal character, we add it to the command */ - default: - { - - if ((!wchar_private(c)) && (((c>31) || (c==L'\n'))&& (c != 127))) - { - /* Regular character */ - insert_char(c); - } - else - { - /* - Low priority debug message. These can happen if - the user presses an unefined control - sequnece. No reason to report. - */ - debug(2, _(L"Unknown keybinding %d"), c); - } - break; - } - } if ((c != R_HISTORY_SEARCH_BACKWARD) && diff --git a/screen.cpp b/screen.cpp index f0848fe1e..402ebb080 100644 --- a/screen.cpp +++ b/screen.cpp @@ -457,68 +457,68 @@ static void s_desired_append_char(screen_t *s, switch (b) { - case L'\n': - { - int i; - /* Current line is definitely hard wrapped */ - s->desired.line(s->desired.cursor.y).is_soft_wrapped = false; - s->desired.create_line(s->desired.line_count()); - s->desired.cursor.y++; - s->desired.cursor.x=0; - for (i=0; i < prompt_width+indent*INDENT_STEP; i++) + case L'\n': { - s_desired_append_char(s, L' ', 0, indent, prompt_width); - } - break; - } - - case L'\r': - { - line_t ¤t = s->desired.line(line_no); - current.clear(); - s->desired.cursor.x = 0; - break; - } - - default: - { - int screen_width = common_get_width(); - int cw = fish_wcwidth(b); - - s->desired.create_line(line_no); - - /* - Check if we are at the end of the line. If so, continue on the next line. - */ - if ((s->desired.cursor.x + cw) > screen_width) - { - /* Current line is soft wrapped (assuming we support it) */ - s->desired.line(s->desired.cursor.y).is_soft_wrapped = true; - //fprintf(stderr, "\n\n1 Soft wrapping %d\n\n", s->desired.cursor.y); - - line_no = (int)s->desired.line_count(); - s->desired.add_line(); + int i; + /* Current line is definitely hard wrapped */ + s->desired.line(s->desired.cursor.y).is_soft_wrapped = false; + s->desired.create_line(s->desired.line_count()); s->desired.cursor.y++; s->desired.cursor.x=0; - for (size_t i=0; i < prompt_width; i++) + for (i=0; i < prompt_width+indent*INDENT_STEP; i++) { s_desired_append_char(s, L' ', 0, indent, prompt_width); } + break; } - line_t &line = s->desired.line(line_no); - line.append(b, c); - s->desired.cursor.x+= cw; - - /* Maybe wrap the cursor to the next line, even if the line itself did not wrap. This avoids wonkiness in the last column. */ - if (s->desired.cursor.x >= screen_width) + case L'\r': { - line.is_soft_wrapped = true; + line_t ¤t = s->desired.line(line_no); + current.clear(); s->desired.cursor.x = 0; - s->desired.cursor.y++; + break; + } + + default: + { + int screen_width = common_get_width(); + int cw = fish_wcwidth(b); + + s->desired.create_line(line_no); + + /* + Check if we are at the end of the line. If so, continue on the next line. + */ + if ((s->desired.cursor.x + cw) > screen_width) + { + /* Current line is soft wrapped (assuming we support it) */ + s->desired.line(s->desired.cursor.y).is_soft_wrapped = true; + //fprintf(stderr, "\n\n1 Soft wrapping %d\n\n", s->desired.cursor.y); + + line_no = (int)s->desired.line_count(); + s->desired.add_line(); + s->desired.cursor.y++; + s->desired.cursor.x=0; + for (size_t i=0; i < prompt_width; i++) + { + s_desired_append_char(s, L' ', 0, indent, prompt_width); + } + } + + line_t &line = s->desired.line(line_no); + line.append(b, c); + s->desired.cursor.x+= cw; + + /* Maybe wrap the cursor to the next line, even if the line itself did not wrap. This avoids wonkiness in the last column. */ + if (s->desired.cursor.x >= screen_width) + { + line.is_soft_wrapped = true; + s->desired.cursor.x = 0; + s->desired.cursor.y++; + } + break; } - break; - } } } diff --git a/set_color.cpp b/set_color.cpp index ff8abe173..eee452066 100644 --- a/set_color.cpp +++ b/set_color.cpp @@ -261,35 +261,35 @@ int main(int argc, char **argv) switch (opt) { - case 0: - break; + case 0: + break; - case 'b': - bgcolor = optarg; - break; - case 'h': - print_help(argv[0], 1); - exit(0); + case 'b': + bgcolor = optarg; + break; + case 'h': + print_help(argv[0], 1); + exit(0); - case 'o': - bold=true; - break; + case 'o': + bold=true; + break; - case 'u': - underline=true; - break; + case 'u': + underline=true; + break; - case 'v': - check_locale_init(); - fprintf(stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION); - exit(0); + case 'v': + check_locale_init(); + fprintf(stderr, _("%s, version %s\n"), SET_COLOR, PACKAGE_VERSION); + exit(0); - case 'c': - print_colors(); - exit(0); + case 'c': + print_colors(); + exit(0); - case '?': - return 1; + case '?': + return 1; } @@ -297,19 +297,19 @@ int main(int argc, char **argv) switch (argc-optind) { - case 0: + case 0: // printf( "no fg\n" ); - break; + break; - case 1: - fgcolor=argv[optind]; + case 1: + fgcolor=argv[optind]; // printf( "fg %s\n", fgcolor ); - break; + break; - default: - check_locale_init(); - printf(_("%s: Too many arguments\n"), SET_COLOR); - return 1; + default: + check_locale_init(); + printf(_("%s: Too many arguments\n"), SET_COLOR); + return 1; } /* Infer term256 support */ diff --git a/tokenizer.cpp b/tokenizer.cpp index 9d6bc0c83..1f8e97cf9 100644 --- a/tokenizer.cpp +++ b/tokenizer.cpp @@ -228,26 +228,26 @@ static bool is_string_char(wchar_t c, bool is_first) { switch (c) { - /* Unconditional separators */ - case L'\0': - case L' ': - case L'\n': - case L'|': - case L'\t': - case L';': - case L'#': - case L'\r': - case L'<': - case L'>': - case L'&': - return false; + /* Unconditional separators */ + case L'\0': + case L' ': + case L'\n': + case L'|': + case L'\t': + case L';': + case L'#': + case L'\r': + case L'<': + case L'>': + case L'&': + return false; - /* Conditional separator */ - case L'^': - return ! is_first; + /* Conditional separator */ + case L'^': + return ! is_first; - default: - return true; + default: + return true; } } @@ -323,118 +323,118 @@ static void read_string(tokenizer *tok) */ switch (mode) { - case 0: - { - switch (*tok->buff) + case 0: { - case L'(': - { - paran_count=1; - mode = 1; - break; - } - - case L'[': - { - if (tok->buff != start) - mode=2; - break; - } - - case L'\'': - case L'"': - { - - const wchar_t *end = quote_end(tok->buff); - tok->last_quote = *tok->buff; - if (end) + switch (*tok->buff) { - tok->buff=(wchar_t *)end; - } - else - { - tok->buff += wcslen(tok->buff); - - if ((!tok->accept_unfinished)) + case L'(': { - TOK_CALL_ERROR(tok, TOK_UNTERMINATED_QUOTE, QUOTE_ERROR); - return; + paran_count=1; + mode = 1; + break; } - do_loop = 0; - } - break; - } - - default: - { - if (!is_string_char(*(tok->buff), is_first)) - { - do_loop=0; - } - } - } - break; - } - - case 3: - case 1: - switch (*tok->buff) - { - case L'\'': - case L'\"': - { - const wchar_t *end = quote_end(tok->buff); - if (end) - { - tok->buff=(wchar_t *)end; - } - else - { - tok->buff += wcslen(tok->buff); - if ((!tok->accept_unfinished)) + case L'[': { - TOK_CALL_ERROR(tok, TOK_UNTERMINATED_QUOTE, QUOTE_ERROR); - return; + if (tok->buff != start) + mode=2; + break; } - do_loop = 0; - } + case L'\'': + case L'"': + { + + const wchar_t *end = quote_end(tok->buff); + tok->last_quote = *tok->buff; + if (end) + { + tok->buff=(wchar_t *)end; + } + else + { + tok->buff += wcslen(tok->buff); + + if ((!tok->accept_unfinished)) + { + TOK_CALL_ERROR(tok, TOK_UNTERMINATED_QUOTE, QUOTE_ERROR); + return; + } + do_loop = 0; + + } + break; + } + + default: + { + if (!is_string_char(*(tok->buff), is_first)) + { + do_loop=0; + } + } + } break; } - case L'(': - paran_count++; - break; - case L')': - paran_count--; - if (paran_count == 0) + case 3: + case 1: + switch (*tok->buff) { - mode--; + case L'\'': + case L'\"': + { + const wchar_t *end = quote_end(tok->buff); + if (end) + { + tok->buff=(wchar_t *)end; + } + else + { + tok->buff += wcslen(tok->buff); + if ((!tok->accept_unfinished)) + { + TOK_CALL_ERROR(tok, TOK_UNTERMINATED_QUOTE, QUOTE_ERROR); + return; + } + do_loop = 0; + } + + break; + } + + case L'(': + paran_count++; + break; + case L')': + paran_count--; + if (paran_count == 0) + { + mode--; + } + break; + case L'\0': + do_loop = 0; + break; } break; - case L'\0': - do_loop = 0; - break; - } - break; - case 2: - switch (*tok->buff) - { - case L'(': - paran_count=1; - mode = 3; - break; + case 2: + switch (*tok->buff) + { + case L'(': + paran_count=1; + mode = 3; + break; - case L']': - mode=0; - break; + case L']': + mode=0; + break; - case L'\0': - do_loop = 0; + case L'\0': + do_loop = 0; + break; + } break; - } - break; } } @@ -643,62 +643,62 @@ void tok_next(tokenizer *tok) switch (*tok->buff) { - case L'\0': - tok->last_type = TOK_END; - /*fwprintf( stderr, L"End of string\n" );*/ - tok->has_next = false; - break; - case 13: - case L'\n': - case L';': - tok->last_type = TOK_END; - tok->buff++; - break; - case L'&': - tok->last_type = TOK_BACKGROUND; - tok->buff++; - break; + case L'\0': + tok->last_type = TOK_END; + /*fwprintf( stderr, L"End of string\n" );*/ + tok->has_next = false; + break; + case 13: + case L'\n': + case L';': + tok->last_type = TOK_END; + tok->buff++; + break; + case L'&': + tok->last_type = TOK_BACKGROUND; + tok->buff++; + break; - case L'|': - check_size(tok, 2); + case L'|': + check_size(tok, 2); - tok->last[0]=L'1'; - tok->last[1]=L'\0'; - tok->last_type = TOK_PIPE; - tok->buff++; - break; + tok->last[0]=L'1'; + tok->last[1]=L'\0'; + tok->last_type = TOK_PIPE; + tok->buff++; + break; - case L'>': - read_redirect(tok, 1); - return; - case L'<': - read_redirect(tok, 0); - return; - case L'^': - read_redirect(tok, 2); - return; + case L'>': + read_redirect(tok, 1); + return; + case L'<': + read_redirect(tok, 0); + return; + case L'^': + read_redirect(tok, 2); + return; - default: - { - if (iswdigit(*tok->buff)) + default: { - const wchar_t *orig = tok->buff; - int fd = 0; - while (iswdigit(*tok->buff)) - fd = (fd*10) + (*(tok->buff++) - L'0'); - - switch (*(tok->buff)) + if (iswdigit(*tok->buff)) { - case L'^': - case L'>': - case L'<': - read_redirect(tok, fd); - return; + const wchar_t *orig = tok->buff; + int fd = 0; + while (iswdigit(*tok->buff)) + fd = (fd*10) + (*(tok->buff++) - L'0'); + + switch (*(tok->buff)) + { + case L'^': + case L'>': + case L'<': + read_redirect(tok, fd); + return; + } + tok->buff = orig; } - tok->buff = orig; + read_string(tok); } - read_string(tok); - } } @@ -720,12 +720,12 @@ wchar_t *tok_first(const wchar_t *str) switch (tok_last_type(&t)) { - case TOK_STRING: + case TOK_STRING: // fwprintf( stderr, L"Got token %ls\n", tok_last( &t )); - res = wcsdup(tok_last(&t)); - break; - default: - break; + res = wcsdup(tok_last(&t)); + break; + default: + break; } tok_destroy(&t); @@ -766,24 +766,24 @@ int main(int argc, char **argv) { switch (tok_last_type(&tok)) { - case TOK_INVALID: - wprintf(L"Type: INVALID\n"); - break; - case TOK_STRING: - wprintf(L"Type: STRING\t Value: %ls\n", tok_last(&tok)); - break; - case TOK_PIPE: - wprintf(L"Type: PIPE\n"); - break; - case TOK_END: - wprintf(L"Type: END\n"); - break; - case TOK_ERROR: - wprintf(L"Type: ERROR\n"); - break; - default: - wprintf(L"Type: Unknown\n"); - break; + case TOK_INVALID: + wprintf(L"Type: INVALID\n"); + break; + case TOK_STRING: + wprintf(L"Type: STRING\t Value: %ls\n", tok_last(&tok)); + break; + case TOK_PIPE: + wprintf(L"Type: PIPE\n"); + break; + case TOK_END: + wprintf(L"Type: END\n"); + break; + case TOK_ERROR: + wprintf(L"Type: ERROR\n"); + break; + default: + wprintf(L"Type: Unknown\n"); + break; } } tok_destroy(&tok); diff --git a/wildcard.cpp b/wildcard.cpp index da7a7a1cc..e17f6cd15 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -477,15 +477,15 @@ static wcstring file_get_desc(const wcstring &filename, { switch (err) { - case ENOENT: - { - return COMPLETE_ROTTEN_SYMLINK_DESC; - } + case ENOENT: + { + return COMPLETE_ROTTEN_SYMLINK_DESC; + } - case ELOOP: - { - return COMPLETE_LOOP_SYMLINK_DESC; - } + case ELOOP: + { + return COMPLETE_LOOP_SYMLINK_DESC; + } } /* On unknown errors we do nothing. The file will be diff --git a/wutil.cpp b/wutil.cpp index 730ad3d37..f5b1e2434 100644 --- a/wutil.cpp +++ b/wutil.cpp @@ -153,21 +153,21 @@ FILE *wfopen(const wcstring &path, const char *mode) size_t idx = 0; switch (mode[idx++]) { - case 'r': - permissions = O_RDONLY; - break; - case 'w': - permissions = O_WRONLY; - options = O_CREAT | O_TRUNC; - break; - case 'a': - permissions = O_WRONLY; - options = O_CREAT | O_APPEND; - break; - default: - errno = EINVAL; - return NULL; - break; + case 'r': + permissions = O_RDONLY; + break; + case 'w': + permissions = O_WRONLY; + options = O_CREAT | O_TRUNC; + break; + case 'a': + permissions = O_WRONLY; + options = O_CREAT | O_APPEND; + break; + default: + errno = EINVAL; + return NULL; + break; } /* Skip binary */ if (mode[idx] == 'b') diff --git a/xdgmimeglob.cpp b/xdgmimeglob.cpp index 01245b5f0..49d098bf0 100644 --- a/xdgmimeglob.cpp +++ b/xdgmimeglob.cpp @@ -398,15 +398,15 @@ _xdg_glob_hash_append_glob(XdgGlobHash *glob_hash, switch (type) { - case XDG_GLOB_LITERAL: - glob_hash->literal_list = _xdg_glob_list_append(glob_hash->literal_list, strdup(glob), strdup(mime_type)); - break; - case XDG_GLOB_SIMPLE: - glob_hash->simple_node = _xdg_glob_hash_insert_text(glob_hash->simple_node, glob + 1, strdup(mime_type)); - break; - case XDG_GLOB_FULL: - glob_hash->full_list = _xdg_glob_list_append(glob_hash->full_list, strdup(glob), strdup(mime_type)); - break; + case XDG_GLOB_LITERAL: + glob_hash->literal_list = _xdg_glob_list_append(glob_hash->literal_list, strdup(glob), strdup(mime_type)); + break; + case XDG_GLOB_SIMPLE: + glob_hash->simple_node = _xdg_glob_hash_insert_text(glob_hash->simple_node, glob + 1, strdup(mime_type)); + break; + case XDG_GLOB_FULL: + glob_hash->full_list = _xdg_glob_list_append(glob_hash->full_list, strdup(glob), strdup(mime_type)); + break; } } diff --git a/xdgmimemagic.cpp b/xdgmimemagic.cpp index 8074cc433..b4f9e14d9 100644 --- a/xdgmimemagic.cpp +++ b/xdgmimemagic.cpp @@ -731,30 +731,30 @@ _xdg_mime_magic_read_magic_file(XdgMimeMagic *mime_magic, { switch (state) { - case XDG_MIME_MAGIC_SECTION: - match = _xdg_mime_magic_match_new(); - state = _xdg_mime_magic_parse_header(magic_file, match); - if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) - _xdg_mime_magic_match_free(match); - break; - case XDG_MIME_MAGIC_MAGIC: - state = _xdg_mime_magic_parse_magic_line(magic_file, match); - if (state == XDG_MIME_MAGIC_SECTION || - (state == XDG_MIME_MAGIC_EOF && match->mime_type)) - { - match->matchlet = _xdg_mime_magic_matchlet_mirror(match->matchlet); - _xdg_mime_magic_insert_match(mime_magic, match); - } - else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) - _xdg_mime_magic_match_free(match); - break; - case XDG_MIME_MAGIC_ERROR: - state = _xdg_mime_magic_parse_error(magic_file); - break; - case XDG_MIME_MAGIC_EOF: - default: - /* Make the compiler happy */ - assert(0); + case XDG_MIME_MAGIC_SECTION: + match = _xdg_mime_magic_match_new(); + state = _xdg_mime_magic_parse_header(magic_file, match); + if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) + _xdg_mime_magic_match_free(match); + break; + case XDG_MIME_MAGIC_MAGIC: + state = _xdg_mime_magic_parse_magic_line(magic_file, match); + if (state == XDG_MIME_MAGIC_SECTION || + (state == XDG_MIME_MAGIC_EOF && match->mime_type)) + { + match->matchlet = _xdg_mime_magic_matchlet_mirror(match->matchlet); + _xdg_mime_magic_insert_match(mime_magic, match); + } + else if (state == XDG_MIME_MAGIC_EOF || state == XDG_MIME_MAGIC_ERROR) + _xdg_mime_magic_match_free(match); + break; + case XDG_MIME_MAGIC_ERROR: + state = _xdg_mime_magic_parse_error(magic_file); + break; + case XDG_MIME_MAGIC_EOF: + default: + /* Make the compiler happy */ + assert(0); } } _xdg_mime_update_mime_magic_extents(mime_magic);