aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2005-12-12 09:30:01 +1000
committerGravatar axel <axel@liljencrantz.se>2005-12-12 09:30:01 +1000
commit5262f3ce99fed8ac31e8c07d0678413972a1a55e (patch)
tree65b7a39bfc1c40b4a3ce16c0d0bc1ef5d5bafdae
parent28dd48a76aa5355b166c372ee374256bb98ad40e (diff)
Revamp the jobs builtin
darcs-hash:20051211233001-ac50b-24547ca41d4db05f87d59dd3e396c3190a66ef89.gz
-rw-r--r--builtin.c244
-rw-r--r--doc_src/doc.hdr1
-rw-r--r--doc_src/jobs.txt15
-rw-r--r--event.c5
-rw-r--r--input.c2
5 files changed, 180 insertions, 87 deletions
diff --git a/builtin.c b/builtin.c
index f5ebb5b7..7438aa08 100644
--- a/builtin.c
+++ b/builtin.c
@@ -73,6 +73,18 @@
/**
Table of all builtins
*/
+
+
+ enum
+ {
+ JOBS_DEFAULT,
+ JOBS_PRINT_PID,
+ JOBS_PRINT_COMMAND,
+ JOBS_PRINT_GROUP,
+ }
+ ;
+
+
static hash_table_t builtin;
int builtin_out_redirect;
@@ -2400,25 +2412,103 @@ static int cpu_use( job_t *j )
}
#endif
+static void builtin_jobs_print( job_t *j, int mode, int header )
+{
+ process_t *p;
+ switch( mode )
+ {
+ case JOBS_DEFAULT:
+ {
+
+ if( header )
+ {
+ /*
+ Print table header before first job
+ */
+ sb_append( sb_out, L"Job\tGroup\t");
+#ifdef HAVE__PROC_SELF_STAT
+ sb_append( sb_out, L"CPU\t" );
+#endif
+ sb_append( sb_out, L"State\tCommand\n" );
+ }
+
+ sb_printf( sb_out, L"%d\t%d\t", j->job_id, j->pgid );
+
+#ifdef HAVE__PROC_SELF_STAT
+ sb_printf( sb_out, L"%d%%\t", cpu_use(j) );
+#endif
+ sb_append2( sb_out, job_is_stopped(j)?L"stopped\t":L"running\t",
+// job_is_completed(j)?L"completed\t":L"unfinished\t",
+ j->command, L"\n", (void *)0 );
+ }
+
+ case JOBS_PRINT_GROUP:
+ {
+ if( header )
+ {
+ /*
+ Print table header before first job
+ */
+ sb_append( sb_out, L"Group\n");
+ }
+ sb_printf( sb_out, L"%d\n", j->pgid );
+ break;
+ }
+
+ case JOBS_PRINT_PID:
+ {
+ if( header )
+ {
+ /*
+ Print table header before first job
+ */
+ sb_append( sb_out, L"Procces\n");
+ }
+
+ for( p=j->first_process; p; p=p->next )
+ {
+ sb_printf( sb_out, L"%d\n", p->pid );
+ }
+ break;
+ }
+
+ case JOBS_PRINT_COMMAND:
+ {
+ if( header )
+ {
+ /*
+ Print table header before first job
+ */
+ sb_append( sb_out, L"Command\n");
+ }
+
+ for( p=j->first_process; p; p=p->next )
+ {
+ sb_printf( sb_out, L"%ls\n", p->argv[0] );
+ }
+ break;
+ }
+
+ }
+
+}
+
+
+
/**
Builtin for printing running jobs
*/
static int builtin_jobs( wchar_t **argv )
{
-
- enum
- {
- DEFAULT,
- PRINT_PID,
- PRINT_COMMAND
- }
- ;
int argc=0;
- job_t *j;
int found=0;
- int mode=DEFAULT;
+ int mode=JOBS_DEFAULT;
+
+ job_t *print_me=0;
+
+
argc = builtin_count_args( argv );
woptind=0;
@@ -2436,6 +2526,14 @@ static int builtin_jobs( wchar_t **argv )
L"command", no_argument, 0, 'c'
}
,
+ {
+ L"group", no_argument, 0, 'g'
+ }
+ ,
+ {
+ L"last", no_argument, 0, 'l'
+ }
+ ,
{
0, 0, 0, 0
}
@@ -2446,7 +2544,7 @@ static int builtin_jobs( wchar_t **argv )
int opt = wgetopt_long( argc,
argv,
- L"pc",
+ L"pclg",
long_options,
&opt_index );
if( opt == -1 )
@@ -2472,13 +2570,33 @@ static int builtin_jobs( wchar_t **argv )
case 'p':
- mode=PRINT_PID;
+ mode=JOBS_PRINT_PID;
break;
case 'c':
- mode=PRINT_COMMAND;
+ mode=JOBS_PRINT_COMMAND;
+ break;
+
+ case 'g':
+ mode=JOBS_PRINT_GROUP;
break;
+ case 'l':
+ {
+ job_t *j;
+ for( j=first_job; j; j=j->next )
+ {
+ if( j->constructed )
+ {
+ print_me = j;
+ break;
+ }
+ }
+ break;
+
+ }
+
+
case '?':
// builtin_print_help( argv[0], sb_err );
@@ -2487,58 +2605,16 @@ static int builtin_jobs( wchar_t **argv )
}
}
- if( mode==DEFAULT )
+ if( woptind < argc-1 )
{
-
- for( j= first_job; j; j=j->next )
- {
- /*
- Ignore unconstructed jobs, i.e. ourself.
- */
- if( j->constructed /*&& j->skip_notification*/ )
- {
- if( !found )
- {
- /*
- Print table header before first job
- */
- sb_append( sb_out, L"Job\tGroup\t");
-#ifdef HAVE__PROC_SELF_STAT
- sb_append( sb_out, L"CPU\t" );
-#endif
- sb_append( sb_out, L"State\tCommand\n" );
- }
-
- found = 1;
-
- sb_printf( sb_out, L"%d\t%d\t", j->job_id, j->pgid );
-
-#ifdef HAVE__PROC_SELF_STAT
- sb_printf( sb_out, L"%d%%\t", cpu_use(j) );
-#endif
- sb_append2( sb_out, job_is_stopped(j)?L"stopped\t":L"running\t",
-// job_is_completed(j)?L"completed\t":L"unfinished\t",
- j->command, L"\n", (void *)0 );
-
- }
- }
- if( !found )
- {
- sb_append2( sb_out, argv[0], L": There are no running jobs\n", (void *)0 );
- }
+ sb_append2( sb_err, argv[0], L": zero or one arguments\n", (void *)0 );
+ return 1;
}
- else
+
+ if( woptind == argc-1 )
{
long pid;
wchar_t *end;
- job_t *j;
-
- if( woptind != argc-1 )
- {
- sb_append2( sb_err, argv[0], L": Expected exactly one argument\n", (void *)0 );
- }
-
-
errno=0;
pid=wcstol( argv[woptind], &end, 10 );
if( errno || *end )
@@ -2548,30 +2624,44 @@ static int builtin_jobs( wchar_t **argv )
}
- j = job_get_from_pid( pid );
- if( !j )
+ print_me = job_get_from_pid( pid );
+ if( !print_me )
{
sb_printf( sb_err, L"%ls: No suitable job: %d\n", argv[0], pid );
return 1;
}
- process_t *p;
- for( p=j->first_process; p; p=p->next )
+
+ }
+
+ /*
+ Do not babble if not interactive
+ */
+ if( builtin_out_redirect )
+ found=1;
+
+ if( !print_me )
+ {
+ job_t *j;
+
+ for( j= first_job; j; j=j->next )
{
- switch( mode )
+ /*
+ Ignore unconstructed jobs, i.e. ourself.
+ */
+ if( j->constructed /*&& j->skip_notification*/ )
{
- case PRINT_PID:
- {
- sb_printf( sb_out, L"%d\n", p->pid );
- break;
- }
-
- case PRINT_COMMAND:
- {
- sb_printf( sb_out, L"%ls\n", p->argv[0] );
- break;
- }
+ builtin_jobs_print( j, mode, !found );
+ found = 1;
}
- }
+ }
+ if( !found )
+ {
+ sb_append2( sb_out, argv[0], L": There are no running jobs\n", (void *)0 );
+ }
+ }
+ else
+ {
+ builtin_jobs_print( print_me, mode, !found );
}
return 0;
diff --git a/doc_src/doc.hdr b/doc_src/doc.hdr
index b8bf3856..64b165e5 100644
--- a/doc_src/doc.hdr
+++ b/doc_src/doc.hdr
@@ -997,7 +997,6 @@ g++, javac, java, gcj, lpr, doxygen, whois, find)
- The jobs builtin should be able to give information on a specific job, such as the pids of the processes in the job
- Syntax highlighting should mark cd to non-existing directories as an error
- the code for printing the prompt should know about the most common escape sequences
-- block builtin
- redo the jobs command
- wait shellscript
diff --git a/doc_src/jobs.txt b/doc_src/jobs.txt
index 021edd31..c424267b 100644
--- a/doc_src/jobs.txt
+++ b/doc_src/jobs.txt
@@ -1,13 +1,20 @@
\section jobs jobs - print currently running jobs
\subsection jobs-synopsis
-<tt>jobs</tt>
+<tt>jobs [OPTIONS] [PID]</tt>
\subsection jobs-description Description
The <tt>jobs</tt> builtin causes fish to print a list of the currently
running jobs and their status.
-On systems that supports this feature, jobs will also print the CPU
-usage of each job since the last command was executed. The CPU usage
-is expressed as a percentage of full CPU activity. Note that on
+jobs accepts the following switches:
+
+- <tt>-l</tt> or <tt>--last</tt> only the last job to be started is printed
+- <tt>-g</tt> or <tt>--group</tt> only print the group id of each job
+- <tt>-c</tt> or <tt>--command</tt> print the commandname for each process in all jobs
+- <tt>-p</tt> or <tt>--process</tt> print the procces id for each process in all jobs
+
+On systems that supports this feature, jobs will print the CPU usage
+of each job since the last command was executed. The CPU usage is
+expressed as a percentage of full CPU activity. Note that on
multiprocessor systems, the total activity may be more than 100\%.
diff --git a/event.c b/event.c
index 9c2633ed..4abdaf00 100644
--- a/event.c
+++ b/event.c
@@ -462,8 +462,6 @@ static void event_fire_delayed()
free( blocked );
blocked = new_blocked;
}
-
-
while( sig_list[active_list].count > 0 )
{
@@ -518,7 +516,6 @@ static void event_fire_delayed()
void event_fire( event_t *event )
{
- //int is_event_old = is_event;
is_event++;
if( event && (event->type == EVENT_SIGNAL) )
@@ -556,7 +553,7 @@ void event_fire( event_t *event )
}
}
- is_event--;// = is_event_old;
+ is_event--;
}
diff --git a/input.c b/input.c
index 78fec9a2..fbf3c136 100644
--- a/input.c
+++ b/input.c
@@ -698,7 +698,7 @@ static wchar_t *input_expand_sequence( const wchar_t *in )
}
debug( 1, L"Invalid sequence - Control-nothing?\n" );
error = 1;
-
+
break;
}