Skip to content

Commit

Permalink
improve jobs control
Browse files Browse the repository at this point in the history
  • Loading branch information
ziirish committed Dec 31, 2011
1 parent a8591f6 commit 829695e
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 29 deletions.
75 changes: 75 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,78 @@ Features
- Multiple commands: ls | grep .c && echo OK
- Background commands: sleep 120 &
- Extensible with modules (see README in plugins directory)

Example
-------

ziirish@carbon:~/workspace/shelldone/src$ valgrind --leak-check=full
--show-reachable=yes ./shelldone
==22418== Memcheck, a memory error detector
==22418== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==22418== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for
copyright info
==22418== Command: ./shelldone
==22418==
shell> module load ../plugins/parsing/jobs/jo
jobs.c jobs.so
shell> module load ../plugins/parsing/jobs/jobs.so
Module 'jobs' successfuly loaded.
shell> ../../test/bl
blah blah.c
shell> ../../test/blah
1
2
3
^Z
[1] 22421 (../../test/blah) suspended
shell> ../../test/blah
1
2
3
^Z
[2] 22422 (../../test/blah) suspended
shell> ../../test/blah
1
2
3
^Z
[3] 22423 (../../test/blah) suspended
shell> fg %1
[1] - continued 22421 (../../test/blah)
4
5
6
^Z
[1] 22421 (../../test/blah) suspended
shell> fg %3 %2 %1
[3] - continued 22423 (../../test/blah)
4
5
6
7
8
9
10
[2] - continued 22422 (../../test/blah)
4
5
6
7
8
9
10
[1] + continued 22421 (../../test/blah)
7
8
9
10
shell> quit
==22418==
==22418== HEAP SUMMARY:
==22418== in use at exit: 0 bytes in 0 blocks
==22418== total heap usage: 6,933 allocs, 6,933 frees, 725,868 bytes allocated
==22418==
==22418== All heap blocks were freed -- no leaks are possible
==22418==
==22418== For counts of detected and suppressed errors, rerun with: -v
==22418== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
109 changes: 83 additions & 26 deletions src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,23 @@ sd_exit (int argc, char **argv, int in, int out, int err)
int
sd_jobs (int argc, char **argv, int in, int out, int err)
{
(void) argc;
(void) argv;
(void) in;
(void) out;
(void) err;

list_jobs (TRUE, NULL, 0);
if (argc == 0)
{
list_jobs (TRUE, NULL, 0);
}
else
{
int *tmp = xcalloc (argc, sizeof (int));
int i;
for (i = 0; i < argc; i++)
tmp[i] = strtoul (argv[i], NULL, 10);
list_jobs (TRUE, tmp, argc);
xfree (tmp);
}

return 0;
}
Expand Down Expand Up @@ -267,38 +277,85 @@ sd_fg (int argc, char **argv, int in, int out, int err)

open_filestream ();

command *tmp = get_last_enqueued_job (TRUE);
if (tmp != NULL)
if (argc == 0)
{
int status;
int r;
tmp->continued = TRUE;
if (tmp->stopped)
command *tmp = get_last_enqueued_job (TRUE);
if (tmp != NULL)
{
sd_print ("[%d] + continued %d (%s)\n",
tmp->job,
tmp->pid,
tmp->cmd);
tmp->stopped = FALSE;
kill (tmp->pid, SIGCONT);
}
signal (SIGTSTP, sigstophandler);
curr = tmp;
int status;
int r;
tmp->continued = TRUE;
if (tmp->stopped)
{
sd_print ("[%d] + continued %d (%s)\n",
tmp->job,
tmp->pid,
tmp->cmd);
tmp->stopped = FALSE;
kill (tmp->pid, SIGCONT);
}
signal (SIGTSTP, sigstophandler);
curr = tmp;

pid_t p = tmp->pid;

pid_t p = tmp->pid;
r = waitpid (p, &status, 0);
if (r != -1)
ret_code = WEXITSTATUS(status);
else
ret_code = 254;

r = waitpid (p, &status, 0);
if (r != -1)
ret_code = WEXITSTATUS(status);
free_command (tmp);
}
else
{
sd_printerr ("fg: no jobs enqeued\n");
ret_code = 254;

free_command (tmp);
}
}
else
{
sd_printerr ("fg: no jobs enqeued\n");
ret_code = 254;
int i;
for (i = 0; i < argc; i++)
{
int status;
int r;
int index = index_of (strtoul (argv[i], NULL, 10));
job *tmp = get_job (index);
if (tmp != NULL)
{
tmp->content->continued = TRUE;
if (tmp->content->stopped)
{
const char l = (tmp == get_last_job ()) ? '+' : '-';
sd_print ("[%d] %c continued %d (%s)\n",
tmp->content->job,
l,
tmp->content->pid,
tmp->content->cmd);
tmp->content->stopped = FALSE;
kill (tmp->content->pid, SIGCONT);
}
signal (SIGTSTP, sigstophandler);
curr = copy_command (tmp->content);
remove_job (index);

pid_t p = curr->pid;

r = waitpid (p, &status, 0);
if (r != -1)
ret_code = WEXITSTATUS(status);
else
ret_code = 254;

free_command (curr);
}
else
{
sd_printerr ("fg: '%s' no such process\n", argv[i]);
ret_code = 254;
}
}
}

close_filestream ();
Expand Down
2 changes: 2 additions & 0 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ run_command (command_line *ptrc)
else
argv = (char *[]){ptr->cmd, NULL};
execvp (ptr->cmd, argv);
if (ptr->argcf > 0)
xfree (argv);
err (1, "%s", ptr->cmd);
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ enqueue_job (command *ptr, unsigned int stopped)
tmp->content->job, ptr->pid, ptr->cmd);
}

static int
int
index_of (pid_t pid)
{
job *tmp = list->head;
Expand All @@ -154,7 +154,7 @@ index_of (pid_t pid)
return i;
}

static job *
job *
get_job (int i)
{
if (i < 0)
Expand All @@ -169,7 +169,13 @@ get_job (int i)
return tmp;
}

static void
job *
get_last_job (void)
{
return list->tail;
}

void
remove_job (int i)
{
list_remove_id ((sdlist **)&list, i, (free_c)free_command);
Expand Down
26 changes: 26 additions & 0 deletions src/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,30 @@ command *get_last_enqueued_job (unsigned int flush);
*/
void clear_job (job *ptr);

/**
* Return the index of the job of the given pid
* @param pid PID of the job
* @return Index of the job
*/
int index_of (pid_t pid);

/**
* Return the job of the given index
* @param i Index of the job
* @return The job
*/
job * get_job (int i);

/**
* Return the last job in the list
* @return The last job
*/
job * get_last_job (void);

/**
* Remove a job
* @param i Index of the job
*/
void remove_job (int i);

#endif

0 comments on commit 829695e

Please sign in to comment.