From 980af4aa5becc2dfcf4379d8c68b3a9366e6e5cd Mon Sep 17 00:00:00 2001 From: Marc Garcia Sastre Date: Thu, 13 Apr 2017 00:34:25 +0200 Subject: [PATCH] status returns the function name when called with -u parameter Fixes #1743 --- doc_src/status.txt | 3 +++ src/builtin.cpp | 10 ++++++++++ src/parser.cpp | 4 ++++ src/parser.h | 3 +++ tests/status.in | 10 ++++++++++ tests/status.out | 3 +++ 6 files changed, 33 insertions(+) diff --git a/doc_src/status.txt b/doc_src/status.txt index b93502059..e9ab98b96 100644 --- a/doc_src/status.txt +++ b/doc_src/status.txt @@ -11,6 +11,7 @@ status is-no-job-control status is-full-job-control status is-interactive-job-control status current-filename +status current-function status current-line-number status print-stack-trace status job-control CONTROL-TYPE @@ -38,6 +39,8 @@ The following operations (sub-commands) are available: - `current-filename` prints the filename of the currently running script. Also `-f` or `--current-filename`. +- `current-function` prints the name of the currently called function if able, when missing displays "Not a function". Also `-u` or `--current-function`. + - `current-line-number` prints the line number of the currently running script. Also `-n` or `--current-line-number`. - `job-control CONTROL-TYPE` sets the job control type, which can be `none`, `full`, or `interactive`. Also `-j CONTROL-TYPE` or `--job-control=CONTROL-TYPE`. diff --git a/src/builtin.cpp b/src/builtin.cpp index f26015fe7..c2cf31931 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -2368,6 +2368,7 @@ enum status_cmd_t { STATUS_IS_INTERACTIVE_JOB_CTRL, STATUS_IS_NO_JOB_CTRL, STATUS_CURRENT_FILENAME, + STATUS_CURRENT_FUNCTION, STATUS_CURRENT_LINE_NUMBER, STATUS_SET_JOB_CONTROL, STATUS_PRINT_STACK_TRACE, @@ -2376,6 +2377,7 @@ enum status_cmd_t { // Must be sorted by string, not enum or random. const enum_map status_enum_map[] = { {STATUS_CURRENT_FILENAME, L"current-filename"}, + {STATUS_CURRENT_FUNCTION, L"current-function"}, {STATUS_CURRENT_LINE_NUMBER, L"current-line-number"}, {STATUS_IS_BLOCK, L"is-block"}, {STATUS_IS_COMMAND_SUB, L"is-command-substitution"}, @@ -2608,6 +2610,14 @@ static int builtin_status(parser_t &parser, io_streams_t &streams, wchar_t **arg streams.out.append_format(L"%ls\n", fn); break; } + case STATUS_CURRENT_FUNCTION: { + CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd) + const wchar_t *fn = parser.get_function_name(); + + if (!fn) fn = _(L"Not a function"); + streams.out.append_format(L"%ls\n", fn); + break; + } case STATUS_CURRENT_LINE_NUMBER: { CHECK_FOR_UNEXPECTED_STATUS_ARGS(status_cmd) streams.out.append_format(L"%d\n", parser.get_lineno()); diff --git a/src/parser.cpp b/src/parser.cpp index 34e0606c6..ce5da0c9e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -440,6 +440,10 @@ const wchar_t *parser_t::is_function() const { return result; } +const wchar_t *parser_t::get_function_name() { + return this->is_function(); +} + int parser_t::get_lineno() const { int lineno = -1; if (!execution_contexts.empty()) { diff --git a/src/parser.h b/src/parser.h index 2ac09c7d2..7df2fdc52 100644 --- a/src/parser.h +++ b/src/parser.h @@ -306,6 +306,9 @@ class parser_t { /// Return a description of the given blocktype. const wchar_t *get_block_desc(int block) const; + /// Return the current function name. + const wchar_t *get_function_name(); + /// Removes a job. bool job_remove(job_t *job); diff --git a/tests/status.in b/tests/status.in index cdda0d76a..3049e9184 100644 --- a/tests/status.in +++ b/tests/status.in @@ -39,3 +39,13 @@ status --job-control=1none # Now set it to a valid mode. status job-control none + +# Check status -u outside functions +status current-function + +function test_function + status current-function +end + +test_function +eval test_function \ No newline at end of file diff --git a/tests/status.out b/tests/status.out index e69de29bb..989264396 100644 --- a/tests/status.out +++ b/tests/status.out @@ -0,0 +1,3 @@ +Not a function +test_function +test_function