From f5be301a2f3eb568fcb712c110aa116452573c73 Mon Sep 17 00:00:00 2001 From: axel Date: Sun, 22 Feb 2009 02:46:56 +1000 Subject: [PATCH] Handle exit status of processes terminated by signals darcs-hash:20090221164656-ac50b-7bcbf6cb0bb8384560fbf9bf1059480cb4089def.gz --- doc_src/index.hdr.in | 4 +++- exec.c | 3 ++- proc.c | 47 +++++++++++++++++++++++++++++--------------- proc.h | 6 ++++++ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/doc_src/index.hdr.in b/doc_src/index.hdr.in index 3e2f9f4..52ce8de 100644 --- a/doc_src/index.hdr.in +++ b/doc_src/index.hdr.in @@ -928,7 +928,7 @@ values of most of these variables. - \c history, which is an array containing the last commands that where entered. - \c HOME, which is the users home directory. This variable can only be changed by the root user. - \c PWD, which is the current working directory. -- \c status, which is the exit status of the last foreground job to exit. +- \c status, which is the exit status of the last foreground job to exit. If the job was terminated through a signal, the exit status will be 128 plus the signal number. - \c USER, which is the username. This variable can only be changed by the root user. The names of these variables are mostly derived from the csh family of @@ -967,6 +967,8 @@ variable may also be set to a specific value: - 126 means that while a file with the specified name was located, it was not executable - 127 means that no function, builtin or command with the given name could be located +If a process exits through a signal, the exit status will be 128 plus the number of the signal. + \subsection variables-color Variables for changing highlighting colors The colors used by fish for syntax highlighting can be configured by diff --git a/exec.c b/exec.c index 5534f7c..a6b9899 100644 --- a/exec.c +++ b/exec.c @@ -1556,7 +1556,8 @@ void exec( job_t *j ) { debug( 3, L"Set status of %ls to %d using short circut", j->command, p->status ); - proc_set_last_status( job_get_flag( j, JOB_NEGATE )?(!p->status):p->status ); + int status = proc_format_status(p->status); + proc_set_last_status( job_get_flag( j, JOB_NEGATE )?(!status):status ); } break; } diff --git a/proc.c b/proc.c index 3b134b7..0b9c4ba 100644 --- a/proc.c +++ b/proc.c @@ -343,30 +343,29 @@ static void mark_process_status( job_t *j, process_t *p, int status ) { - p->status = status; // debug( 0, L"Process %ls %ls", p->argv[0], WIFSTOPPED (status)?L"stopped":(WIFEXITED( status )?L"exited":(WIFSIGNALED( status )?L"signaled to exit":L"BLARGH")) ); + p->status = status; if (WIFSTOPPED (status)) { p->stopped = 1; } + else if (WIFSIGNALED(status) || WIFEXITED(status)) + { + p->completed = 1; + } else { + /* This should never be reached */ p->completed = 1; + char mess[MESS_SIZE]; + snprintf( mess, + MESS_SIZE, + "Process %d exited abnormally\n", + (int) p->pid ); - if (( !WIFEXITED( status ) ) && - (! WIFSIGNALED( status )) ) - { - /* This should never be reached */ - char mess[MESS_SIZE]; - snprintf( mess, - MESS_SIZE, - "Process %d exited abnormally\n", - (int) p->pid ); - - write( 2, mess, strlen(mess) ); - } + write( 2, mess, strlen(mess) ); } } @@ -1106,7 +1105,7 @@ void job_continue (job_t *j, int cont) while( p->next ) p = p->next; - if( WIFEXITED( p->status ) ) + if( WIFEXITED( p->status ) || WIFSIGNALED(p->status)) { /* Mark process status only if we are in the foreground @@ -1114,8 +1113,9 @@ void job_continue (job_t *j, int cont) */ if( p->pid ) { - debug( 3, L"Set status of %ls to %d", j->command, WEXITSTATUS(p->status) ); - proc_set_last_status( job_get_flag( j, JOB_NEGATE )?(WEXITSTATUS(p->status)?0:1):WEXITSTATUS(p->status) ); + int status = proc_format_status(p->status); + + proc_set_last_status( job_get_flag( j, JOB_NEGATE )?!status:status); } } } @@ -1140,6 +1140,21 @@ void job_continue (job_t *j, int cont) } +int proc_format_status(int status) +{ + if( WIFSIGNALED( status ) ) + { + return 128+WTERMSIG(status); + } + else if( WIFEXITED( status ) ) + { + return WEXITSTATUS(status); + } + return status; + +} + + void proc_sanity_check() { job_t *j; diff --git a/proc.h b/proc.h index 4e92c26..e9c87da 100644 --- a/proc.h +++ b/proc.h @@ -485,4 +485,10 @@ void proc_push_interactive( int value ); */ void proc_pop_interactive(); +/** + Format an exit status code as returned by e.g. wait into a fish exit code number as accepted by proc_set_last_status. + */ +int proc_format_status(int status) ; + + #endif