Posted to tcl by evilotto at Wed Jun 26 17:57:48 GMT 2013view raw

  1. ~ Abstract
  2.  
  3. This proposal is to add a cleaner interface to spawning subprocesses
  4.  
  5. ~ Rationale
  6.  
  7. Tcl has long had the ''exec'' command which works for many purposes but
  8. has several areas of inflexibility that make certain edge cases very
  9. difficult to handle, especially its handling of shell metacharacters
  10. internally. A replacement command that provides simpler and more orthogonal
  11. functionality would enable pipeline building in tcl code.
  12.  
  13. ~ Requirements
  14.  
  15. Any replacement for creating subprocesses should support:
  16.  
  17. 1. spawning a subprocess with a given argument list
  18.  
  19. 2. spwaning a subprocess with a different executable than command line argument
  20.  
  21. 3. assigning a set of open tcl channels to fds of a spawned subprocess
  22.  
  23. 4. passing a specific environment to the spawned subprocess
  24.  
  25. 5. capturing the exit status of a spawned subprocess.
  26.  
  27. ~ Specification
  28.  
  29. Handling of subprocesses will be done through a ''process'' ensemble.
  30.  
  31. 1. ''process create ?-executable path? ?-env dict? ?-fds dict? arg0 ?argn ...?''
  32.  
  33. Creates (fork/execs) the new subprocess and returns a handle to it.
  34.  
  35. If the '-executable'' flag is specified, its argument is used as the
  36. executable filename (i.e., the filename argument to execve); if this
  37. flag is not specified then ''arg0'' is used.
  38.  
  39. If the '-env' flag is specified, the dict given is used to create the
  40. environment for the subprocess. If it is not specified, the current
  41. environment ( ::env ) is used.
  42.  
  43. If the '-fds' flag is specified, the dict is expected to have integer keys
  44. and names of open channels with the correct modes as values. These are
  45. passed to the child process. Any fd not specified in the dict will be closed.
  46. If this argument is not present, the default value {0 stdin 1 stdout 2 stderr}
  47. is used.
  48.  
  49. The arguments are passed to the subprocess without further interpretation.
  50.  
  51. 2. ''process wait handle''
  52.  
  53. Waits for the subprocess specified by 'handle' to exit, and returns the
  54. exit status of that process.
  55.  
  56. ~ Examples
  57.  
  58. Current example:
  59. set out [exec cat /tmp/afile]
  60.  
  61. Process example:
  62. set so_pipe [chan pipe]
  63. set se_pipe [chan pipe]
  64. set fds [list 0 {} 1 [lindex $so_pipe 1] 2 [lindex $se_pipe 1]]
  65. set handle [process create -fds $fds cat /tmp/afile]
  66. set out [read [lindex $so_pipe 0]]
  67. process wait $handle
  68.  
  69. Current example:
  70. exec mail sample@example.com << $text
  71.  
  72. Process example
  73. set si_pipe [chan pipe]
  74. set so_pipe [chan pipe]
  75. set se_pipe [chan pipe]
  76. set fds [list 0 [lindex $si_pipe 0] 1 [lindex $so_pipe 1] 2 [lindex $se_pipe 1]]
  77. set handle [process create -fds $fds mail sample@example.com]
  78. puts [lindex $si_pipe 1] $text
  79. close [lindex $si_pipe 1]
  80. process wait $handle
  81.  
  82. While there is considerably more setup needed for the ''process'' usage than
  83. for ''exec'', it is assumed that this will generally be hidden inside a
  84. wrapper proc.
  85.  
  86. ~ Copyright
  87.  
  88. This document has been placed in the public domain.
  89.