Skip to content

Commit

Permalink
Handle exit status of processes terminated by signals
Browse files Browse the repository at this point in the history
darcs-hash:20090221164656-ac50b-7bcbf6cb0bb8384560fbf9bf1059480cb4089def.gz
  • Loading branch information
liljencrantz committed Feb 21, 2009
1 parent b1357d1 commit f5be301
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
4 changes: 3 additions & 1 deletion doc_src/index.hdr.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 31 additions & 16 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
}
}

Expand Down Expand Up @@ -1106,16 +1105,17 @@ 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
and the last process in a pipe, and it is not a short circuted builtin
*/
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);
}
}
}
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f5be301

Please sign in to comment.