1/* fg_bg.c, created from fg_bg.def. */ 2#line 23 "fg_bg.def" 3 4#line 32 "fg_bg.def" 5 6#include <config.h> 7 8#include "../bashtypes.h" 9#include <signal.h> 10 11#if defined (HAVE_UNISTD_H) 12# include <unistd.h> 13#endif 14 15#include "../bashintl.h" 16 17#include "../shell.h" 18#include "../jobs.h" 19#include "common.h" 20#include "bashgetopt.h" 21 22#if defined (JOB_CONTROL) 23extern char *this_command_name; 24 25static int fg_bg __P((WORD_LIST *, int)); 26 27/* How to bring a job into the foreground. */ 28int 29fg_builtin (list) 30 WORD_LIST *list; 31{ 32 int fg_bit; 33 register WORD_LIST *t; 34 35 if (job_control == 0) 36 { 37 sh_nojobs ((char *)NULL); 38 return (EXECUTION_FAILURE); 39 } 40 41 if (no_options (list)) 42 return (EX_USAGE); 43 list = loptend; 44 45 /* If the last arg on the line is '&', then start this job in the 46 background. Else, fg the job. */ 47 for (t = list; t && t->next; t = t->next) 48 ; 49 fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0; 50 51 return (fg_bg (list, fg_bit)); 52} 53#endif /* JOB_CONTROL */ 54 55#line 90 "fg_bg.def" 56 57#if defined (JOB_CONTROL) 58/* How to put a job into the background. */ 59int 60bg_builtin (list) 61 WORD_LIST *list; 62{ 63 int r; 64 65 if (job_control == 0) 66 { 67 sh_nojobs ((char *)NULL); 68 return (EXECUTION_FAILURE); 69 } 70 71 if (no_options (list)) 72 return (EX_USAGE); 73 list = loptend; 74 75 /* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts 76 on the first member (if any) of that list. */ 77 r = EXECUTION_SUCCESS; 78 do 79 { 80 if (fg_bg (list, 0) == EXECUTION_FAILURE) 81 r = EXECUTION_FAILURE; 82 if (list) 83 list = list->next; 84 } 85 while (list); 86 87 return r; 88} 89 90/* How to put a job into the foreground/background. */ 91static int 92fg_bg (list, foreground) 93 WORD_LIST *list; 94 int foreground; 95{ 96 sigset_t set, oset; 97 int job, status, old_async_pid; 98 JOB *j; 99 100 BLOCK_CHILD (set, oset); 101 job = get_job_spec (list); 102 103 if (INVALID_JOB (job)) 104 { 105 if (job != DUP_JOB) 106 sh_badjob (list ? list->word->word : "current"); 107 108 goto failure; 109 } 110 111 j = get_job_by_jid (job); 112 /* Or if j->pgrp == shell_pgrp. */ 113 if (IS_JOBCONTROL (job) == 0) 114 { 115 builtin_error (_("job %d started without job control"), job + 1); 116 goto failure; 117 } 118 119 if (foreground == 0) 120 { 121 old_async_pid = last_asynchronous_pid; 122 last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */ 123 } 124 125 status = start_job (job, foreground); 126 127 if (status >= 0) 128 { 129 /* win: */ 130 UNBLOCK_CHILD (oset); 131 return (foreground ? status : EXECUTION_SUCCESS); 132 } 133 else 134 { 135 if (foreground == 0) 136 last_asynchronous_pid = old_async_pid; 137 138 failure: 139 UNBLOCK_CHILD (oset); 140 return (EXECUTION_FAILURE); 141 } 142} 143#endif /* JOB_CONTROL */ 144