aboutsummaryrefslogtreecommitdiffhomepage
path: root/event.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-11-18 16:30:30 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-11-18 16:30:30 -0800
commit9992b8eb0e3366ff8a3948aa0b66a19c3c12c737 (patch)
tree6dda0fef85812016fbba9ea067c9d586092b506d /event.cpp
parentbab69f26724028d16054a3daf5c78aad7c67bb2d (diff)
Apply new indentation, brace, and whitespace style
Diffstat (limited to 'event.cpp')
-rw-r--r--event.cpp617
1 files changed, 311 insertions, 306 deletions
diff --git a/event.cpp b/event.cpp
index 371a3a34..8f4f6b17 100644
--- a/event.cpp
+++ b/event.cpp
@@ -36,27 +36,27 @@
*/
typedef struct
{
- /**
- Number of delivered signals
- */
- int count;
- /**
- Whether signals have been skipped
- */
- int overflow;
- /**
- Array of signal events
- */
- int signal[SIG_UNHANDLED_MAX];
+ /**
+ Number of delivered signals
+ */
+ int count;
+ /**
+ Whether signals have been skipped
+ */
+ int overflow;
+ /**
+ Array of signal events
+ */
+ int signal[SIG_UNHANDLED_MAX];
}
- signal_list_t;
+signal_list_t;
/**
The signal event list. Actually two separate lists. One which is
active, which is the one that new events is written to. The inactive
one contains the events that are currently beeing performed.
*/
-static signal_list_t sig_list[]={{0,0},{0,0}};
+static signal_list_t sig_list[]= {{0,0},{0,0}};
/**
The index of sig_list that is the list of signals currently written to
@@ -86,52 +86,52 @@ 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 )
- return 1;
+ if (classv->type == EVENT_ANY)
+ return 1;
- if( classv->type != instance->type )
- return 0;
+ if (classv->type != instance->type)
+ return 0;
- switch( classv->type )
- {
+ switch (classv->type)
+ {
case EVENT_SIGNAL:
- if( classv->param1.signal == EVENT_ANY_SIGNAL )
- return 1;
- return classv->param1.signal == instance->param1.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;
+ 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;
+ 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;
+ 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;
- }
+ }
- /**
- This should never be reached
- */
- return 0;
+ /**
+ This should never be reached
+ */
+ return 0;
}
@@ -139,95 +139,97 @@ static int event_match( const event_t *classv, const event_t *instance )
Create an identical copy of an event. Use deep copying, i.e. make
duplicates of any strings used as well.
*/
-static event_t *event_copy( const event_t *event, int copy_arguments )
+static event_t *event_copy(const event_t *event, int copy_arguments)
{
event_t *e = new event_t(*event);
e->arguments.reset(new wcstring_list_t);
- if( copy_arguments && event->arguments.get() != NULL )
- {
+ if (copy_arguments && event->arguments.get() != NULL)
+ {
*(e->arguments) = *(event->arguments);
- }
+ }
- return e;
+ return e;
}
/**
Test if specified event is blocked
*/
-static int event_is_blocked( event_t *e )
+static int event_is_blocked(event_t *e)
{
- block_t *block;
- parser_t &parser = parser_t::principal_parser();
- for( block = parser.current_block; block; block = block->outer )
- {
+ 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))
return true;
- }
+ }
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 );
+ CHECK(e, 0);
- wcstring result;
- switch( e->type )
- {
+ wcstring result;
+ 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;
+ 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;
+ result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str());
+ break;
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() );
+ if (e->param1.pid > 0)
+ {
+ result = format_string(_(L"exit handler for process %d"), e->param1.pid);
+ }
else
- result = format_string(_(L"exit handler for job with process group %d"), -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);
+ }
- break;
+ 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 );
+ 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;
+ break;
}
case EVENT_GENERIC:
- result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str() );
- break;
+ result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str());
+ break;
default:
- result = format_string(_(L"Unknown event type") );
- break;
+ result = format_string(_(L"Unknown event type"));
+ break;
- }
+ }
- return result;
+ return result;
}
#if 0
-static void show_all_handlers(void) {
+static void show_all_handlers(void)
+{
puts("event handlers:");
- for (event_list_t::const_iterator iter = events.begin(); iter != events.end(); ++iter) {
+ for (event_list_t::const_iterator iter = events.begin(); iter != events.end(); ++iter)
+ {
const event_t *foo = *iter;
wcstring tmp = event_get_desc(foo);
printf(" handler now %ls\n", tmp.c_str());
@@ -235,18 +237,18 @@ static void show_all_handlers(void) {
}
#endif
-void event_add_handler( const event_t *event )
+void event_add_handler(const event_t *event)
{
- event_t *e;
+ event_t *e;
- CHECK( event, );
+ CHECK(event,);
- e = event_copy( event, 0 );
+ e = event_copy(event, 0);
- if( e->type == EVENT_SIGNAL )
- {
- signal_handle( e->param1.signal, 1 );
- }
+ if (e->type == EVENT_SIGNAL)
+ {
+ signal_handle(e->param1.signal, 1);
+ }
// Block around updating the events vector
signal_block();
@@ -254,78 +256,78 @@ 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;
+ size_t i;
+ event_list_t new_list;
- CHECK( criterion, );
+ CHECK(criterion,);
- /*
- Because of concurrency issues (env_remove could remove an event
- that is currently being executed), env_remove does not actually
- free any events - instead it simply moves all events that should
- be removed from the event list to the killme list, and the ones
- that shouldn't be killed to new_list, and then drops the empty
- events-list.
- */
+ /*
+ Because of concurrency issues (env_remove could remove an event
+ that is currently being executed), env_remove does not actually
+ free any events - instead it simply moves all events that should
+ be removed from the event list to the killme list, and the ones
+ that shouldn't be killed to new_list, and then drops the empty
+ events-list.
+ */
- if( events.empty() )
- return;
+ if (events.empty())
+ return;
- for( i=0; i<events.size(); i++ )
- {
- event_t *n = events.at(i);
- if( event_match( criterion, n ) )
+ for (i=0; i<events.size(); i++)
{
+ event_t *n = events.at(i);
+ if (event_match(criterion, n))
+ {
killme.push_back(n);
- /*
- If this event was a signal handler and no other handler handles
- the specified signal type, do not handle that type of signal any
- more.
- */
- if( n->type == EVENT_SIGNAL )
- {
+ /*
+ If this event was a signal handler and no other handler handles
+ the specified signal type, do not handle that type of signal any
+ more.
+ */
+ if (n->type == EVENT_SIGNAL)
+ {
event_t e = event_t::signal_event(n->param1.signal);
- if( event_get( &e, 0 ) == 1 )
- {
- signal_handle( e.param1.signal, 0 );
+ if (event_get(&e, 0) == 1)
+ {
+ signal_handle(e.param1.signal, 0);
+ }
+ }
}
- }
- }
- else
- {
+ else
+ {
new_list.push_back(n);
+ }
}
- }
signal_block();
- events.swap(new_list);
+ events.swap(new_list);
signal_unblock();
}
-int event_get( event_t *criterion, std::vector<event_t *> *out )
+int event_get(event_t *criterion, std::vector<event_t *> *out)
{
- size_t i;
- int found = 0;
+ size_t i;
+ int found = 0;
- if( events.empty() )
- return 0;
+ if (events.empty())
+ return 0;
- CHECK( criterion, 0 );
+ CHECK(criterion, 0);
- for( i=0; i<events.size(); i++ )
- {
- event_t *n = events.at(i);
- if( event_match(criterion, n ) )
+ for (i=0; i<events.size(); i++)
{
- found++;
- if( out )
+ event_t *n = events.at(i);
+ if (event_match(criterion, n))
+ {
+ found++;
+ if (out)
out->push_back(n);
+ }
}
- }
- return found;
+ return found;
}
bool event_is_signal_observed(int sig)
@@ -343,7 +345,7 @@ bool event_is_signal_observed(int sig)
}
else if (event->type == EVENT_SIGNAL)
{
- if( event->param1.signal == EVENT_ANY_SIGNAL || event->param1.signal == sig)
+ if (event->param1.signal == EVENT_ANY_SIGNAL || event->param1.signal == sig)
return true;
}
}
@@ -362,7 +364,7 @@ static void event_free_kills()
/**
Test if the specified event is waiting to be killed
*/
-static int event_is_killed( event_t *e )
+static int event_is_killed(event_t *e)
{
return std::find(killme.begin(), killme.end(), e) != killme.end();
}
@@ -373,71 +375,71 @@ static int event_is_killed( event_t *e )
optimize the 'no matches' path. This means that nothing is
allocated/initialized unless needed.
*/
-static void event_fire_internal( const event_t *event )
+static void event_fire_internal(const event_t *event)
{
- size_t i, j;
- event_list_t fire;
+ size_t i, j;
+ event_list_t fire;
- /*
- First we free all events that have been removed
- */
- event_free_kills();
-
- if( events.empty() )
- return;
+ /*
+ First we free all events that have been removed
+ */
+ event_free_kills();
- /*
- Then we iterate over all events, adding events that should be
- fired to a second list. We need to do this in a separate step
- since an event handler might call event_remove or
- event_add_handler, which will change the contents of the \c
- events list.
- */
- for( i=0; i<events.size(); i++ )
- {
- event_t *criterion = events.at(i);
+ if (events.empty())
+ return;
/*
- Check if this event is a match
+ Then we iterate over all events, adding events that should be
+ fired to a second list. We need to do this in a separate step
+ since an event handler might call event_remove or
+ event_add_handler, which will change the contents of the \c
+ events list.
*/
- if(event_match( criterion, event ) )
+ for (i=0; i<events.size(); i++)
{
+ event_t *criterion = events.at(i);
+
+ /*
+ Check if this event is a match
+ */
+ if (event_match(criterion, event))
+ {
fire.push_back(criterion);
+ }
}
- }
-
- /*
- No matches. Time to return.
- */
- if( fire.empty() )
- return;
-
- /*
- Iterate over our list of matching events
- */
-
- for( i=0; i<fire.size(); i++ )
- {
- event_t *criterion = fire.at(i);
- int prev_status;
/*
- Check if this event has been removed, if so, dont fire it
+ No matches. Time to return.
*/
- if( event_is_killed( criterion ) )
- continue;
+ if (fire.empty())
+ return;
/*
- Fire event
+ Iterate over our list of matching events
*/
- wcstring buffer = criterion->function_name;
+
+ for (i=0; i<fire.size(); i++)
+ {
+ event_t *criterion = fire.at(i);
+ int prev_status;
+
+ /*
+ Check if this event has been removed, if so, dont fire it
+ */
+ if (event_is_killed(criterion))
+ continue;
+
+ /*
+ Fire event
+ */
+ wcstring buffer = criterion->function_name;
if (event->arguments.get())
{
- 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;
}
@@ -445,26 +447,26 @@ static void event_fire_internal( const event_t *event )
// debug( 1, L"Event handler fires command '%ls'", buffer.c_str() );
- /*
- Event handlers are not part of the main flow of code, so
- they are marked as non-interactive
- */
- proc_push_interactive(0);
- prev_status = proc_get_last_status();
+ /*
+ Event handlers are not part of the main flow of code, so
+ they are marked as non-interactive
+ */
+ proc_push_interactive(0);
+ prev_status = proc_get_last_status();
parser_t &parser = parser_t::principal_parser();
block_t *block = new event_block_t(event);
- parser.push_block(block);
- parser.eval( buffer, io_chain_t(), TOP );
- parser.pop_block();
- proc_pop_interactive();
- proc_set_last_status( prev_status );
- }
-
- /*
- Free killed events
- */
- event_free_kills();
+ parser.push_block(block);
+ parser.eval(buffer, io_chain_t(), TOP);
+ parser.pop_block();
+ proc_pop_interactive();
+ proc_set_last_status(prev_status);
+ }
+
+ /*
+ Free killed events
+ */
+ event_free_kills();
}
@@ -474,78 +476,78 @@ static void event_fire_internal( const event_t *event )
static void event_fire_delayed()
{
- size_t i;
-
- /*
- If is_event is one, we are running the event-handler non-recursively.
+ size_t i;
- When the event handler has called a piece of code that triggers
- another event, we do not want to fire delayed events because of
- concurrency problems.
- */
- if( ! blocked.empty() && is_event==1)
- {
- event_list_t new_blocked;
+ /*
+ If is_event is one, we are running the event-handler non-recursively.
- for( i=0; i<blocked.size(); i++ )
+ When the event handler has called a piece of code that triggers
+ another event, we do not want to fire delayed events because of
+ concurrency problems.
+ */
+ if (! blocked.empty() && is_event==1)
{
- event_t *e = blocked.at(i);
- if( event_is_blocked( e ) )
- {
+ event_list_t new_blocked;
+
+ for (i=0; i<blocked.size(); i++)
+ {
+ event_t *e = blocked.at(i);
+ if (event_is_blocked(e))
+ {
new_blocked.push_back(e);
- }
- else
- {
- event_fire_internal( e );
- event_free( e );
- }
- }
+ }
+ else
+ {
+ event_fire_internal(e);
+ event_free(e);
+ }
+ }
blocked.swap(new_blocked);
- }
-
- while( sig_list[active_list].count > 0 )
- {
- signal_list_t *lst;
-
- /*
- Switch signal lists
- */
- sig_list[1-active_list].count=0;
- sig_list[1-active_list].overflow=0;
- active_list=1-active_list;
+ }
- /*
- Set up
- */
+ while (sig_list[active_list].count > 0)
+ {
+ signal_list_t *lst;
+
+ /*
+ Switch signal lists
+ */
+ sig_list[1-active_list].count=0;
+ sig_list[1-active_list].overflow=0;
+ active_list=1-active_list;
+
+ /*
+ Set up
+ */
event_t e = event_t::signal_event(0);
e.arguments.reset(new wcstring_list_t(1)); //one element
- lst = &sig_list[1-active_list];
+ lst = &sig_list[1-active_list];
- if( lst->overflow )
- {
- debug( 0, _( L"Signal list overflow. Signals have been ignored." ) );
- }
+ if (lst->overflow)
+ {
+ debug(0, _(L"Signal list overflow. Signals have been ignored."));
+ }
- /*
- Send all signals in our private list
- */
- for( int i=0; i < lst->count; i++ )
- {
- e.param1.signal = lst->signal[i];
- e.arguments->at(0) = sig2wcs( e.param1.signal );
- if( event_is_blocked( &e ) )
- {
+ /*
+ Send all signals in our private list
+ */
+ for (int i=0; i < lst->count; i++)
+ {
+ e.param1.signal = lst->signal[i];
+ e.arguments->at(0) = sig2wcs(e.param1.signal);
+ if (event_is_blocked(&e))
+ {
blocked.push_back(event_copy(&e, 1));
- }
- else
- {
- event_fire_internal( &e );
- }
- }
+ }
+ else
+ {
+ event_fire_internal(&e);
+ }
+ }
e.arguments.reset(NULL);
- }
+ }
}
void event_fire_signal(int signal)
@@ -556,42 +558,42 @@ void event_fire_signal(int signal)
allocation or something else that might be bad when in a
signal handler.
*/
- if( sig_list[active_list].count < SIG_UNHANDLED_MAX )
+ if (sig_list[active_list].count < SIG_UNHANDLED_MAX)
sig_list[active_list].signal[sig_list[active_list].count++]=signal;
else
sig_list[active_list].overflow=1;
}
-void event_fire( event_t *event )
+void event_fire(event_t *event)
{
- if( event && (event->type == EVENT_SIGNAL) )
- {
+ if (event && (event->type == EVENT_SIGNAL))
+ {
event_fire_signal(event->param1.signal);
- }
- else
- {
+ }
+ else
+ {
is_event++;
- /*
- Fire events triggered by signals
- */
- event_fire_delayed();
+ /*
+ Fire events triggered by signals
+ */
+ event_fire_delayed();
- if( event )
- {
- if( event_is_blocked( event ) )
- {
+ if (event)
+ {
+ if (event_is_blocked(event))
+ {
blocked.push_back(event_copy(event, 1));
- }
- else
- {
- event_fire_internal( event );
- }
- }
+ }
+ else
+ {
+ event_fire_internal(event);
+ }
+ }
is_event--;
- }
+ }
}
@@ -609,57 +611,60 @@ void event_destroy()
killme.clear();
}
-void event_free( event_t *e )
+void event_free(event_t *e)
{
- CHECK( e, );
+ CHECK(e,);
delete e;
}
void event_fire_generic_internal(const wchar_t *name, ...)
{
- va_list va;
- wchar_t *arg;
+ va_list va;
+ wchar_t *arg;
- CHECK( name, );
+ CHECK(name,);
- event_t ev(EVENT_GENERIC);
- ev.str_param1 = name;
+ event_t ev(EVENT_GENERIC);
+ ev.str_param1 = name;
ev.arguments.reset(new wcstring_list_t);
- va_start( va, name );
- while( (arg=va_arg(va, wchar_t *) )!= 0 )
- {
+ va_start(va, name);
+ while ((arg=va_arg(va, wchar_t *))!= 0)
+ {
ev.arguments->push_back(arg);
- }
- va_end( va );
+ }
+ va_end(va);
- event_fire( &ev );
+ event_fire(&ev);
ev.arguments.reset(NULL);
}
-event_t event_t::signal_event(int sig) {
+event_t event_t::signal_event(int sig)
+{
event_t event(EVENT_SIGNAL);
event.param1.signal = sig;
return event;
}
-event_t event_t::variable_event(const wcstring &str) {
+event_t event_t::variable_event(const wcstring &str)
+{
event_t event(EVENT_VARIABLE);
event.str_param1 = str;
return event;
}
-event_t event_t::generic_event(const wcstring &str) {
+event_t event_t::generic_event(const wcstring &str)
+{
event_t event(EVENT_GENERIC);
event.str_param1 = str;
return event;
}
event_t::event_t(const event_t &x) :
- type(x.type),
- param1(x.param1),
- str_param1(x.str_param1),
- function_name(x.function_name)
+ type(x.type),
+ param1(x.param1),
+ str_param1(x.str_param1),
+ function_name(x.function_name)
{
const wcstring_list_t *ptr = x.arguments.get();
if (ptr)