cdesktopenv/src/cmd/ksh93/sh
Martijn Dekker 41ebb55a3a Fix most of job control (-m/-o monitor) in scripts
If I haven't missed anything, this should make the non-interactive
aspects of job control in scripts work as expected, except for the
"<command unknown>" issue in the output of 'bg', 'fg' and 'jobs'
(which is not such a high priority as those commands are really
designed for interactive use).

Plus, I believe I now finally understand what these three are for:
* The job.jobcontrol variable is set to nonzero by job_init() in
  jobs.c if, and only if, the shell is interactive *and* managed to
  get control of the terminal. Therefore, any changing of terminal
  settings (tcsetpgrp(3), tty_set()) should only be done if
  job.jobcontrol is nonzero. This commit changes several checks for
  sh_isoption(SH_INTERACTIVE) to checks for job.jobcontrol for
  better consistency with this.
* The state flag, sh_isstate(SH_MONITOR), determines whether the
  bits of job control that are relevant for both scripts and
  interactive shells are active, which is mostly making sure that a
  background job gets its own process group (setpgid(3)).
* The shell option, sh_isoption(SH_MONITOR), is just that. When the
  user turns it on or off, the state flag is synched with it. It
  should usually not be directly checked for, as the state may be
  temporarily turned off without turning off the option.

Prior discussion:
https://www.mail-archive.com/austin-group-l@opengroup.org/msg06456.html

src/cmd/ksh93/bltins/typeset.c, src/cmd/ksh93/sh/args.c:
- Move synching the SH_MONITOR state flag with the SH_MONITOR
  shell option from b_set() (the 'set' builtin) to sh_applyopts()
  which is indirectly called from b_set() and is also used when
  parsing the shell invocation command line. This ensures -m is
  properly enabled in both scenarios.

src/cmd/ksh93/sh/jobs.c:
- job_init(): Do not refuse to initialise job control on
  non-interactive shells. Instead, skip everything that should only
  be done on interactive shells (i.e., everything to do with the
  terminal). This function is now even more of a mess than it was
  before, so refactoring may be desirabe at some point.
- job_close(), job_set(), job_reset(), job_wait(): Do not reset the
  terminal process group (tcsetpgrp()) if job.jobcontrol isn't on.

src/cmd/ksh93/sh/xec.c:
- sh_exec(): TFORK: For SIGINT handling, check the SH_MONITOR
  state flag, not the shell option.
- sh_exec(): TFORK: Do not turn off the SH_MONITOR state flag in
  forked children. The non-interactive part of job control should
  stay active. Instead, turn off the SH_INTERACTIVE state flag so
  we don't get interactive shell behaviour (i.e. job control noise
  on the terminal) in forked subshells.
- _sh_fork(), sh_ntfork(): Do not reset the terminal process group
  (tcsetpgrp()) if job.jobcontrol isn't on. Do not turn off the
  SH_MONITOR state flag in forked children.

src/cmd/ksh93/sh/subshell.c: sh_subfork():
- Do not turn off the monitor option and state in forked subshells.
  The non-interactive part of job control should stay active.

src/cmd/ksh93/bltins/misc.c: b_bg():
- Check isstate(SH_MONITOR) instead of sh_isoption(SH_MONITOR) &&
  job.jobcontrol before throwing a 'no job control' error.
  This fixes a minor bug: fg, bg and disown could quietly fail.

src/cmd/ksh93/tests/jobs.sh:
- Add tests for 'fg' with job control IDs (%%, %1) in scripts.
- Add test checking that a background job launched from a subsell
  with job control enabled correctly becomes the leader of its own
  process group.

Makes progress on: https://github.com/ksh93/ksh/issues/119
2021-02-12 06:51:27 +00:00
..
args.c Fix most of job control (-m/-o monitor) in scripts 2021-02-12 06:51:27 +00:00
arith.c libast: Update cdt(3): Allow empty strings in (dt)trees 2021-01-28 02:44:52 +00:00
array.c Correctly block invalid values for arrays of an enum type 2021-02-01 16:57:43 +00:00
defs.c
deparse.c
expand.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
fault.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
fcin.c
init.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
io.c Replace safe FD fix with Solaris/ksh2020 version (re: 045fe6a1) 2021-01-08 16:35:26 +00:00
jobs.c Fix most of job control (-m/-o monitor) in scripts 2021-02-12 06:51:27 +00:00
lex.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
macro.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
main.c libast: Update cdt(3): Allow empty strings in (dt)trees 2021-01-28 02:44:52 +00:00
name.c Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
nvdisc.c Fix various possible uses of uninitialised variables 2021-01-28 04:54:41 +00:00
nvtree.c Fix disabling SHOPT_FIXEDARRAY (re: 2182ecfa) 2021-02-10 04:48:56 +00:00
nvtype.c Fix typos in <customtypecommand> --man self-documentation 2021-02-10 16:25:11 +00:00
parse.c Disallow >;word and <>;word for 'redirect' (re: 7b59fb80, 7b82c338) 2021-02-07 03:23:56 +00:00
path.c autoload: Add loop detection 2021-02-04 16:28:19 +00:00
pmain.c
shcomp.c resolve/remove USAGE_LICENSE macros; remove repetitive (c) strings 2021-01-31 11:00:49 +00:00
streval.c
string.c Fix garbled output from Ctrl+Alt+V (#135) 2020-09-25 03:37:22 +01:00
subshell.c Fix most of job control (-m/-o monitor) in scripts 2021-02-12 06:51:27 +00:00
suid_exec.c rm redundant getpid(2) syscalls (re: 9de65210) 2020-09-23 04:19:02 +02:00
tdump.c
timers.c rm redundant getpid(2) syscalls (re: 9de65210) 2020-09-23 04:19:02 +02:00
trestore.c
waitevent.c
xec.c Fix most of job control (-m/-o monitor) in scripts 2021-02-12 06:51:27 +00:00