From af3059ab2ad4ccbfec3c0789b198388cc90fbcbb Mon Sep 17 00:00:00 2001 From: Jan Kanis Date: Thu, 20 Dec 2012 01:11:55 +0100 Subject: Allow 'emit' to accept event arguments --- parser.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'parser.cpp') diff --git a/parser.cpp b/parser.cpp index 0f8950f0..f022f3ea 100644 --- a/parser.cpp +++ b/parser.cpp @@ -2077,6 +2077,7 @@ int parser_t::parse_job(process_t *p, int tmp; const wchar_t *cmd = args.at(0).completion.c_str(); + wcstring_list_t event_args; /* We couldn't find the specified command. @@ -2157,7 +2158,9 @@ int parser_t::parse_job(process_t *p, current_tokenizer_pos=tmp; job_set_flag(j, JOB_SKIP, 1); - event_fire_generic(L"fish_command_not_found", (wchar_t *)(args.at(0).completion.c_str())); + + event_args.push_back(args.at(0).completion); + event_fire_generic(L"fish_command_not_found", &event_args); proc_set_last_status(err==ENOENT?STATUS_UNKNOWN_COMMAND:STATUS_NOT_EXECUTABLE); } } -- cgit v1.2.3 From 30392bf66a79b91ad2fa10b369f151540c40c09e Mon Sep 17 00:00:00 2001 From: Jan Kanis Date: Thu, 20 Dec 2012 11:48:36 +0100 Subject: reference'ize event.cpp/h --- builtin.cpp | 2 +- event.cpp | 142 ++++++++++++++++++++++++++++------------------------------- event.h | 14 ++++-- function.cpp | 4 +- parser.cpp | 2 +- 5 files changed, 82 insertions(+), 82 deletions(-) (limited to 'parser.cpp') diff --git a/builtin.cpp b/builtin.cpp index a25110f6..30421de3 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -1100,7 +1100,7 @@ static void functions_def(const wcstring &name, wcstring &out) search.function_name = name; std::vector ev; - event_get(&search, &ev); + event_get(search, &ev); out.append(L"function "); diff --git a/event.cpp b/event.cpp index 58a2f4fd..196296a0 100644 --- a/event.cpp +++ b/event.cpp @@ -87,45 +87,45 @@ static event_list_t blocked; they must name the same function. */ -static int event_match(const event_t *classv, const event_t *instance) +static int event_match(const event_t &classv, const event_t &instance) { /* If the function names are both non-empty and different, then it's not a match */ - if (! classv->function_name.empty() && - ! instance->function_name.empty() && - classv->function_name != instance->function_name) + if (! classv.function_name.empty() && + ! instance.function_name.empty() && + classv.function_name != instance.function_name) { return 0; } - if (classv->type == EVENT_ANY) + if (classv.type == EVENT_ANY) return 1; - if (classv->type != instance->type) + if (classv.type != instance.type) return 0; - switch (classv->type) + switch (classv.type) { case EVENT_SIGNAL: - if (classv->param1.signal == EVENT_ANY_SIGNAL) + if (classv.param1.signal == EVENT_ANY_SIGNAL) return 1; - return classv->param1.signal == instance->param1.signal; + return classv.param1.signal == instance.param1.signal; case EVENT_VARIABLE: - return instance->str_param1 == classv->str_param1; + return instance.str_param1 == classv.str_param1; case EVENT_EXIT: - if (classv->param1.pid == EVENT_ANY_PID) + if (classv.param1.pid == EVENT_ANY_PID) return 1; - return classv->param1.pid == instance->param1.pid; + return classv.param1.pid == instance.param1.pid; case EVENT_JOB_ID: - return classv->param1.job_id == instance->param1.job_id; + return classv.param1.job_id == instance.param1.job_id; case EVENT_GENERIC: - return instance->str_param1 == classv->str_param1; + return instance.str_param1 == classv.str_param1; } @@ -141,68 +141,66 @@ static int event_match(const event_t *classv, const event_t *instance) /** Test if specified event is blocked */ -static int event_is_blocked(event_t *e) +static int event_is_blocked(const event_t &e) { block_t *block; parser_t &parser = parser_t::principal_parser(); for (block = parser.current_block; block; block = block->outer) { - if (event_block_list_blocks_type(block->event_blocks, e->type)) + if (event_block_list_blocks_type(block->event_blocks, e.type)) return true; } - return event_block_list_blocks_type(parser.global_event_blocks, e->type); + return event_block_list_blocks_type(parser.global_event_blocks, e.type); } -wcstring event_get_desc(const event_t *e) +wcstring event_get_desc(const event_t &e) { - CHECK(e, 0); - wcstring result; - switch (e->type) + 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)); + 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()); + result = format_string(_(L"handler for variable '%ls'"), e.str_param1.c_str()); break; case EVENT_EXIT: - if (e->param1.pid > 0) + if (e.param1.pid > 0) { - result = format_string(_(L"exit handler for process %d"), e->param1.pid); + 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_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); + result = format_string(_(L"exit handler for job with process group %d"), -e.param1.pid); } break; case EVENT_JOB_ID: { - job_t *j = job_get(e->param1.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); + 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()); + result = format_string(_(L"handler for generic event '%ls'"), e.str_param1.c_str()); break; default: - result = format_string(_(L"Unknown event type '0x%x'"), e->type); + result = format_string(_(L"Unknown event type '0x%x'"), e.type); break; } @@ -223,24 +221,24 @@ static void show_all_handlers(void) } #endif - -static wcstring event_type_str(const event_t *event) { + +wcstring event_type_str(const event_t &event) { wcstring res; wchar_t const *temp; int sig; - switch(event->type) { + switch(event.type) { case EVENT_ANY: res = L"EVENT_ANY"; break; case EVENT_VARIABLE: - if(event->str_param1.c_str()) { - res = format_string(L"EVENT_VARIABLE($%ls)", event->str_param1.c_str()); + if(event.str_param1.c_str()) { + res = format_string(L"EVENT_VARIABLE($%ls)", event.str_param1.c_str()); } else { res = L"EVENT_VARIABLE([any])"; } break; case EVENT_SIGNAL: - sig = event->param1.signal; + sig = event.param1.signal; if(sig == EVENT_ANY_SIGNAL) { temp = L"[all signals]"; } else if(sig == 0) { @@ -251,50 +249,49 @@ static wcstring event_type_str(const event_t *event) { res = format_string(L"EVENT_SIGNAL(%ls)", temp); break; case EVENT_EXIT: - if(event->param1.pid == EVENT_ANY_PID) { + if(event.param1.pid == EVENT_ANY_PID) { res = wcstring(L"EVENT_EXIT([all child processes])"); - } else if (event->param1.pid > 0) { - res = format_string(L"EVENT_EXIT(pid %d)", event->param1.pid); + } else if (event.param1.pid > 0) { + res = format_string(L"EVENT_EXIT(pid %d)", event.param1.pid); } else { - job_t *j = job_get_from_pid(-event->param1.pid); + job_t *j = job_get_from_pid(-event.param1.pid); if (j) res = format_string(L"EVENT_EXIT(jobid %d: \"%ls\")", j->job_id, j->command_wcstr()); else - res = format_string(L"EVENT_EXIT(pgid %d)", -event->param1.pid); + res = format_string(L"EVENT_EXIT(pgid %d)", -event.param1.pid); } break; case EVENT_JOB_ID: { - job_t *j = job_get(event->param1.job_id); + job_t *j = job_get(event.param1.job_id); if (j) res = format_string(L"EVENT_JOB_ID(job %d: \"%ls\")", j->job_id, j->command_wcstr()); else - res = format_string(L"EVENT_JOB_ID(jobid %d)", event->param1.job_id); + res = format_string(L"EVENT_JOB_ID(jobid %d)", event.param1.job_id); break; } case EVENT_GENERIC: - res = format_string(L"EVENT_GENERIC(%ls)", event->str_param1.c_str()); + res = format_string(L"EVENT_GENERIC(%ls)", event.str_param1.c_str()); break; default: - res = format_string(L"unknown/illegal event(%x)", event->type); + res = format_string(L"unknown/illegal event(%x)", event.type); } - if(event->function_name.size()) { - return format_string(L"%ls: \"%ls\"", res.c_str(), event->function_name.c_str()); + if(event.function_name.size()) { + return format_string(L"%ls: \"%ls\"", res.c_str(), event.function_name.c_str()); } else { return res; } } -void event_add_handler(const event_t *event) +void event_add_handler(const event_t &event) { event_t *e; - CHECK(event,); if(debug_level >= 3) debug(3, "register: %ls\n", event_type_str(event).c_str()); - e = new event_t(*event); + e = new event_t(event); if (e->type == EVENT_SIGNAL) { @@ -307,13 +304,12 @@ void event_add_handler(const event_t *event) signal_unblock(); } -void event_remove(event_t *criterion) +void event_remove(event_t &criterion) { size_t i; event_list_t new_list; - CHECK(criterion,); if(debug_level >= 3) debug(3, "unregister: %ls\n", event_type_str(criterion).c_str()); @@ -332,7 +328,7 @@ void event_remove(event_t *criterion) for (i=0; itype == EVENT_SIGNAL) { event_t e = event_t::signal_event(n->param1.signal); - if (event_get(&e, 0) == 1) + if (event_get(e, 0) == 1) { signal_handle(e.param1.signal, 0); } @@ -360,7 +356,7 @@ void event_remove(event_t *criterion) signal_unblock(); } -int event_get(event_t *criterion, std::vector *out) +int event_get(const event_t &criterion, std::vector *out) { size_t i; int found = 0; @@ -368,12 +364,10 @@ int event_get(event_t *criterion, std::vector *out) if (events.empty()) return 0; - CHECK(criterion, 0); - for (i=0; ifunction_name; - if (! event->arguments.empty()) + if (! event.arguments.empty()) { - for (j=0; j < event->arguments.size(); j++) + for (j=0; j < event.arguments.size(); j++) { - wcstring arg_esc = escape_string(event->arguments.at(j), 1); + wcstring arg_esc = escape_string(event.arguments.at(j), 1); buffer += L" "; buffer += arg_esc; } @@ -510,7 +504,7 @@ static void event_fire_internal(const event_t *event) prev_status = proc_get_last_status(); parser_t &parser = parser_t::principal_parser(); - block_t *block = new event_block_t(event); + block_t *block = new event_block_t((const event_t*)&event); parser.push_block(block); parser.eval(buffer, io_chain_t(), TOP); parser.pop_block(); @@ -548,13 +542,13 @@ static void event_fire_delayed() for (i=0; isignal[i]; e.arguments.at(0) = sig2wcs(e.param1.signal); - if (event_is_blocked(&e)) + if (event_is_blocked(e)) { blocked.push_back(new event_t(e)); } else { - event_fire_internal(&e); + event_fire_internal(e); } } @@ -636,13 +630,13 @@ void event_fire(event_t *event) if (event) { - if (event_is_blocked(event)) + if (event_is_blocked(*event)) { blocked.push_back(new event_t(*event)); } else { - event_fire_internal(event); + event_fire_internal(*event); } } is_event--; diff --git a/event.h b/event.h index 283a54fb..1c87488e 100644 --- a/event.h +++ b/event.h @@ -102,14 +102,14 @@ struct event_t May not be called by a signal handler, since it may allocate new memory. */ -void event_add_handler(const event_t *event); +void event_add_handler(const event_t &event); /** Remove all events matching the specified criterion. May not be called by a signal handler, since it may free allocated memory. */ -void event_remove(event_t *event); +void event_remove(event_t &event); /** Return all events which match the specified event class @@ -122,7 +122,7 @@ void event_remove(event_t *event); \return the number of found matches */ -int event_get(event_t *criterion, std::vector *out); +int event_get(const event_t &criterion, std::vector *out); /** Returns whether an event listener is registered for the given signal. @@ -169,7 +169,13 @@ void event_free(event_t *e); /** Returns a string describing the specified event. */ -wcstring event_get_desc(const event_t *e); +wcstring event_get_desc(const event_t &e); + +/** + Returns a string describing the event. This version is more concise and + more detailed than event_get_desc. +*/ +wcstring event_type_str(const event_t &e); /** Fire a generic event with the specified name diff --git a/function.cpp b/function.cpp index 99e6218f..08fb8556 100644 --- a/function.cpp +++ b/function.cpp @@ -199,7 +199,7 @@ void function_add(const function_data_t &data, const parser_t &parser) /* Add event handlers */ for (std::vector::const_iterator iter = data.events.begin(); iter != data.events.end(); ++iter) { - event_add_handler(&*iter); + event_add_handler(*iter); } } @@ -229,7 +229,7 @@ static bool function_remove_ignore_autoload(const wcstring &name) { event_t ev(EVENT_ANY); ev.function_name=name; - event_remove(&ev); + event_remove(ev); } return erased; diff --git a/parser.cpp b/parser.cpp index f022f3ea..1fe7bd83 100644 --- a/parser.cpp +++ b/parser.cpp @@ -893,7 +893,7 @@ void parser_t::stack_trace(block_t *b, wcstring &buff) This is an event handler */ const event_block_t *eb = static_cast(b); - wcstring description = event_get_desc(eb->event); + wcstring description = event_get_desc(*eb->event); append_format(buff, _(L"in event handler: %ls\n"), description.c_str()); buff.append(L"\n"); -- cgit v1.2.3 From 8a446f43ff184fad6f7aeca05fca51bb3aac2d91 Mon Sep 17 00:00:00 2001 From: Jan Kanis Date: Sat, 22 Dec 2012 18:38:28 +0100 Subject: include fixes and suggestions from code review --- builtin.cpp | 10 +++++----- event.cpp | 24 ++++++++++++++++-------- event.h | 8 +------- parser.cpp | 4 ++-- parser.h | 4 ++-- tests/test9.err | 1 + tests/test9.in | 3 +++ tests/test9.status | 2 +- 8 files changed, 31 insertions(+), 25 deletions(-) (limited to 'parser.cpp') diff --git a/builtin.cpp b/builtin.cpp index 30421de3..7df552e2 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -1010,12 +1010,12 @@ static int builtin_emit(parser_t &parser, wchar_t **argv) } - wcstring_list_t args; - wchar_t *eventname = argv[woptind]; - for (woptind++; woptind < argc; woptind++) - { - args.push_back(argv[woptind]); + if(!argv[woptind]) { + append_format(stderr_buffer, L"%ls: expected event name\n", argv[0]); + return STATUS_BUILTIN_ERROR; } + wchar_t *eventname = argv[woptind]; + wcstring_list_t args(argv + woptind + 1, argv + argc); event_fire_generic(eventname, &args); return STATUS_BUILTIN_OK; diff --git a/event.cpp b/event.cpp index 196296a0..496eb337 100644 --- a/event.cpp +++ b/event.cpp @@ -221,8 +221,11 @@ static void show_all_handlers(void) } #endif - -wcstring event_type_str(const event_t &event) { +/* + Give a more condensed description of \c event compared to \c event_get_desc. + It includes what function will fire if the \c event is an event handler. + */ +static wcstring event_desc_compact(const event_t &event) { wcstring res; wchar_t const *temp; int sig; @@ -288,8 +291,10 @@ void event_add_handler(const event_t &event) { event_t *e; - if(debug_level >= 3) - debug(3, "register: %ls\n", event_type_str(event).c_str()); + if(debug_level >= 3) { + wcstring desc = event_desc_compact(event); + debug(3, "register: %ls\n", desc.c_str()); + } e = new event_t(event); @@ -304,14 +309,16 @@ void event_add_handler(const event_t &event) signal_unblock(); } -void event_remove(event_t &criterion) +void event_remove(const event_t &criterion) { size_t i; event_list_t new_list; - if(debug_level >= 3) - debug(3, "unregister: %ls\n", event_type_str(criterion).c_str()); + if(debug_level >= 3) { + wcstring desc = event_desc_compact(criterion); + debug(3, "unregister: %ls\n", desc.c_str()); + } /* Because of concurrency issues (env_remove could remove an event @@ -504,7 +511,7 @@ static void event_fire_internal(const event_t &event) prev_status = proc_get_last_status(); parser_t &parser = parser_t::principal_parser(); - block_t *block = new event_block_t((const event_t*)&event); + block_t *block = new event_block_t(event); parser.push_block(block); parser.eval(buffer, io_chain_t(), TOP); parser.pop_block(); @@ -570,6 +577,7 @@ static void event_fire_delayed() Set up */ event_t e = event_t::signal_event(0); + e.arguments.resize(1); lst = &sig_list[1-active_list]; if (lst->overflow) diff --git a/event.h b/event.h index 1c87488e..cd3bb0e3 100644 --- a/event.h +++ b/event.h @@ -109,7 +109,7 @@ void event_add_handler(const event_t &event); May not be called by a signal handler, since it may free allocated memory. */ -void event_remove(event_t &event); +void event_remove(const event_t &event); /** Return all events which match the specified event class @@ -171,12 +171,6 @@ void event_free(event_t *e); */ wcstring event_get_desc(const event_t &e); -/** - Returns a string describing the event. This version is more concise and - more detailed than event_get_desc. -*/ -wcstring event_type_str(const event_t &e); - /** Fire a generic event with the specified name */ diff --git a/parser.cpp b/parser.cpp index 1fe7bd83..a8da7ba0 100644 --- a/parser.cpp +++ b/parser.cpp @@ -893,7 +893,7 @@ void parser_t::stack_trace(block_t *b, wcstring &buff) This is an event handler */ const event_block_t *eb = static_cast(b); - wcstring description = event_get_desc(*eb->event); + wcstring description = event_get_desc(eb->event); append_format(buff, _(L"in event handler: %ls\n"), description.c_str()); buff.append(L"\n"); @@ -3798,7 +3798,7 @@ if_block_t::if_block_t() : { } -event_block_t::event_block_t(const event_t *evt) : +event_block_t::event_block_t(const event_t &evt) : block_t(EVENT), event(evt) { diff --git a/parser.h b/parser.h index 751182c3..b956be4d 100644 --- a/parser.h +++ b/parser.h @@ -158,8 +158,8 @@ struct if_block_t : public block_t struct event_block_t : public block_t { - const event_t * const event; - event_block_t(const event_t *evt); + event_t const &event; + event_block_t(const event_t &evt); }; struct function_block_t : public block_t diff --git a/tests/test9.err b/tests/test9.err index e69de29b..f439e8db 100644 --- a/tests/test9.err +++ b/tests/test9.err @@ -0,0 +1 @@ +emit: expected event name diff --git a/tests/test9.in b/tests/test9.in index 688dfb3f..b8842727 100644 --- a/tests/test9.in +++ b/tests/test9.in @@ -23,3 +23,6 @@ function test3 --on-event test3 end emit test3 foo bar + +# test empty argument +emit \ No newline at end of file diff --git a/tests/test9.status b/tests/test9.status index 573541ac..d00491fd 100644 --- a/tests/test9.status +++ b/tests/test9.status @@ -1 +1 @@ -0 +1 -- cgit v1.2.3