Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functions --hooks #4694

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/builtin_functions.cpp
Expand Up @@ -35,15 +35,18 @@ struct functions_cmd_opts_t {
bool copy = false;
bool report_metadata = false;
bool verbose = false;
bool hooks = false;
wchar_t *hooks_type = NULL;
wchar_t *description = NULL;
};
static const wchar_t *short_options = L":Dacehnqv";
static const wchar_t *short_options = L":oDacehnqv";
static const struct woption long_options[] = {
{L"erase", no_argument, NULL, 'e'}, {L"description", required_argument, NULL, 'd'},
{L"names", no_argument, NULL, 'n'}, {L"all", no_argument, NULL, 'a'},
{L"help", no_argument, NULL, 'h'}, {L"query", no_argument, NULL, 'q'},
{L"copy", no_argument, NULL, 'c'}, {L"details", no_argument, NULL, 'D'},
{L"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}};
{L"verbose", no_argument, NULL, 'v'}, {L"hooks", optional_argument, NULL, 'o'},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seconding @ridiculousfish's comment: "event_handlers" would be more consistent.

{NULL, 0, NULL, 0}};

static int parse_cmd_opts(functions_cmd_opts_t &opts, int *optind, //!OCLINT(high ncss method)
int argc, wchar_t **argv, parser_t &parser, io_streams_t &streams) {
Expand Down Expand Up @@ -88,6 +91,11 @@ static int parse_cmd_opts(functions_cmd_opts_t &opts, int *optind, //!OCLINT(hi
opts.copy = true;
break;
}
case 'o': {
opts.hooks = true;
opts.hooks_type = w.woptarg;
break ;
}
case ':': {
builtin_missing_argument(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
Expand Down Expand Up @@ -313,6 +321,15 @@ int builtin_functions(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
return report_function_metadata(funcname, opts.verbose, streams, false);
}

if (opts.hooks) {
if (opts.hooks_type) {
wcstring tmp = wcstring(opts.hooks_type);
event_print(streams, &tmp);
} else
event_print(streams, NULL);
return STATUS_CMD_OK;
}

if (opts.list || argc == optind) {
wcstring_list_t names = function_get_names(opts.show_hidden);
std::sort(names.begin(), names.end());
Expand Down
63 changes: 63 additions & 0 deletions src/event.cpp
Expand Up @@ -448,6 +448,69 @@ void event_fire(const event_t *event) {
}
}

void event_print(io_streams_t &streams, const wcstring *filter) {
std::vector<shared_ptr<event_t>> tmp;

static std::map<int, wcstring> dico = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just use a switch statement here (an external function or a lambda):

static const wchar_t *name_for_event_type(int which) {
   switch (which) {
     case EVENT_ANY: return L"any";
     ...
   }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also need to do the reverse operation (have a string and get a int)
and for that i can't do a switch statement

{EVENT_ANY, L"any"},
{EVENT_SIGNAL, L"signal"},
{EVENT_VARIABLE, L"variable"},
{EVENT_EXIT, L"exit"},
{EVENT_JOB_ID, L"job-id"},
{EVENT_GENERIC, L"generic"}
};

tmp = s_event_handlers;
std::sort(tmp.begin(), tmp.end(),
[](const shared_ptr<event_t> &e1, const shared_ptr<event_t> &e2) {
if (e1.get()->type == e2.get()->type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI you can write e1->type == e2->type here, no need for get()

switch (e1.get()->type) {
case EVENT_SIGNAL:
return e1.get()->param1.signal < e2.get()->param1.signal;
case EVENT_JOB_ID:
return e1.get()->param1.job_id < e2.get()->param1.job_id;
case EVENT_VARIABLE:
case EVENT_GENERIC:
return e1.get()->str_param1 < e2.get()->str_param1;
}
} else {
return e1.get()->type < e2.get()->type;
}
});

int type = -1;
for (std::vector<shared_ptr<event_t>>::iterator iter = tmp.begin();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (const shared_ptr<event_t> & evt : tmp) {... is nicer. fish has graduated into C++11 :)

iter != tmp.end(); ++iter) {
if (!filter || *filter == dico[iter->get()->type]) {
if (iter->get()->type != type) {
if (type != -1)
streams.out.append(L"\n");
type = iter->get()->type;
streams.out.append_format(L"Event %ls\n", dico[iter->get()->type].c_str());
}
switch (iter->get()->type) {
case EVENT_SIGNAL:
streams.out.append_format(L"%ls %ls\n", sig2wcs(iter->get()->param1.signal),
iter->get()->function_name.c_str());
break;
case EVENT_JOB_ID:
streams.out.append_format(L"%d %ls\n", iter->get()->param1,
iter->get()->function_name.c_str());
break;
case EVENT_VARIABLE:
case EVENT_GENERIC:
streams.out.append_format(L"%ls %ls\n", iter->get()->str_param1.c_str(),
iter->get()->function_name.c_str());
break;
default:
streams.out.append_format(L"%ls\n", iter->get()->function_name.c_str());
break;

}
}
}
}

void event_init() {}

void event_destroy() { s_event_handlers.clear(); }
Expand Down
6 changes: 6 additions & 0 deletions src/event.h
Expand Up @@ -8,10 +8,12 @@

#include <unistd.h>

#include <map>
#include <memory>
#include <vector>

#include "common.h"
#include "io.h"

/// The signal number that is used to match any signal.
#define EVENT_ANY_SIGNAL -1
Expand Down Expand Up @@ -125,6 +127,10 @@ void event_fire(const event_t *event);
/// May be called from signal handlers
void event_fire_signal(int signal);

/// Called by builtin functions
/// Dumped all events
void event_print(io_streams_t &streams, const wcstring *filter);

/// Initialize the event-handling library.
void event_init();

Expand Down