Released as 20230522 ('Charles')
[parallel.git] / src / env_parallel.zsh
blob69a6a769b0d5248ce193243ea6e3584bcf9d1bc3
1 #!/usr/bin/env zsh
3 # This file must be sourced in zsh:
5 # source =env_parallel.zsh
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2023 Ole Tange, http://ole.tange.dk and Free
11 # Software Foundation, Inc.
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, see <http://www.gnu.org/licenses/>
25 # or write to the Free Software Foundation, Inc., 51 Franklin St,
26 # Fifth Floor, Boston, MA 02110-1301 USA
28 # SPDX-FileCopyrightText: 2021-2023 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
30 # shellcheck disable=SC2006
32 env_parallel() {
33 # env_parallel.zsh
35 _names_of_ALIASES() {
36 print -l ${(k)aliases}
38 _bodies_of_ALIASES() {
39 local _i
40 for _i ($@); do
41 echo 'alias '"$(alias $_i)"
42 done
44 _names_of_FUNCTIONS() {
45 print -l ${(k)functions}
47 _bodies_of_FUNCTIONS() {
48 typeset -f "$@"
50 _names_of_VARIABLES() {
51 print -l ${(k)parameters}
53 _bodies_of_VARIABLES() {
54 typeset -p "$@"
56 _ignore_HARDCODED() {
57 # These names cannot be detected
58 echo '([-\?\#\!\$\*\@\_0]|zsh_eval_context|ZSH_EVAL_CONTEXT|LINENO|IFS|commands|functions|options|aliases|EUID|EGID|UID|GID|dis_patchars|patchars|terminfo|galiases|keymaps|parameters|jobdirs|dirstack|functrace|funcsourcetrace|zsh_scheduled_events|dis_aliases|dis_reswords|dis_saliases|modules|reswords|saliases|widgets|userdirs|historywords|nameddirs|termcap|dis_builtins|dis_functions|jobtexts|funcfiletrace|dis_galiases|builtins|history|jobstates|funcstack|run-help)'
60 _ignore_READONLY() {
61 typeset -pr | perl -e '@r = map {
62 chomp;
63 # sh on UnixWare: readonly TIMEOUT
64 # ash: readonly var='val'
65 # ksh: var='val'
66 # mksh: PIPESTATUS[0]
67 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
68 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
69 # zsh: typeset -r var='val'
70 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
71 $_ } <>;
72 $vars = join "|",map { quotemeta $_ } @r;
73 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
76 _remove_bad_NAMES() {
77 # Do not transfer vars and funcs from env_parallel
78 # shellcheck disable=SC2006
79 _ignore_RO="`_ignore_READONLY`"
80 # shellcheck disable=SC2006
81 _ignore_HARD="`_ignore_HARDCODED`"
82 # To avoid depending on grep dialect, use Perl version of:
83 # grep -Ev '^(...)$' |
84 perl -ne '/^(
85 PARALLEL_ENV|
86 PARALLEL_TMP|
87 _alias_NAMES|
88 _bodies_of_ALIASES|
89 _bodies_of_FUNCTIONS|
90 _bodies_of_VARIABLES|
91 _error_PAR|
92 _function_NAMES|
93 _get_ignored_VARS|
94 _grep_REGEXP|
95 _ignore_HARD|
96 _ignore_HARDCODED|
97 _ignore_READONLY|
98 _ignore_RO|
99 _ignore_UNDERSCORE|
100 _list_alias_BODIES|
101 _list_function_BODIES|
102 _list_variable_VALUES|
103 _make_grep_REGEXP|
104 _names_of_ALIASES|
105 _names_of_FUNCTIONS|
106 _names_of_VARIABLES|
107 _names_of_maybe_FUNCTIONS|
108 _parallel_exit_CODE|
109 _prefix_PARALLEL_ENV|
110 _prefix_PARALLEL_ENV|
111 _remove_bad_NAMES|
112 _remove_readonly|
113 _variable_NAMES|
114 _warning_PAR|
115 _which_PAR)$/x and next;
116 # Filter names matching --env
117 /^'"$_grep_REGEXP"'$/ or next;
118 /^'"$_ignore_UNDERSCORE"'$/ and next;
119 # Remove readonly variables
120 /^'"$_ignore_RO"'$/ and next;
121 /^'"$_ignore_HARD"'$/ and next;
122 print;'
124 _get_ignored_VARS() {
125 perl -e '
126 for(@ARGV){
127 $next_is_env and push @envvar, split/,/, $_;
128 $next_is_env=/^--env$/;
130 if(grep { /^_$/ } @envvar) {
131 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
132 print STDERR "parallel: Error: ",
133 "Run \"parallel --record-env\" in a clean environment first.\n";
134 } else {
135 chomp(@ignored_vars = <IN>);
138 if($ENV{PARALLEL_IGNORED_NAMES}) {
139 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
140 chomp @ignored_vars;
142 $vars = join "|",map { quotemeta $_ } @ignored_vars;
143 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
144 ' -- "$@"
147 # Get the --env variables if set
148 # --env _ should be ignored
149 # and convert a b c to (a|b|c)
150 # If --env not set: Match everything (.*)
151 _make_grep_REGEXP() {
152 perl -e '
153 for(@ARGV){
154 /^_$/ and $next_is_env = 0;
155 $next_is_env and push @envvar, split/,/, $_;
156 $next_is_env = /^--env$/;
158 $vars = join "|",map { quotemeta $_ } @envvar;
159 print $vars ? "($vars)" : "(.*)";
160 ' -- "$@"
162 _which_PAR() {
163 # type returns:
164 # ll is an alias for ls -l (in ash)
165 # bash is a tracked alias for /bin/bash
166 # true is a shell builtin (in bash)
167 # myfunc is a function (in bash)
168 # myfunc is a shell function (in zsh)
169 # which is /usr/bin/which (in sh, bash)
170 # which is hashed (/usr/bin/which)
171 # gi is aliased to `grep -i' (in bash)
172 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
173 # Return 0 if found, 1 otherwise
174 LANG=C type "$@" |
175 perl -pe '$exit += (s/ is an alias for .*// ||
176 s/ is aliased to .*// ||
177 s/ is a function// ||
178 s/ is a shell function// ||
179 s/ is a shell builtin// ||
180 s/.* is hashed .(\S+).$/$1/ ||
181 s/.* is (a tracked alias for )?//);
182 END { exit not $exit }'
184 _warning_PAR() {
185 echo "env_parallel: Warning: $*" >&2
187 _error_PAR() {
188 echo "env_parallel: Error: $*" >&2
191 if _which_PAR parallel >/dev/null; then
192 true parallel found in path
193 else
194 # shellcheck disable=SC2016
195 _error_PAR 'parallel must be in $PATH.'
196 return 255
199 # Grep regexp for vars given by --env
200 # shellcheck disable=SC2006
201 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
202 unset _make_grep_REGEXP
204 # Deal with --env _
205 # shellcheck disable=SC2006
206 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
207 unset _get_ignored_VARS
209 # --record-env
210 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
211 true skip
212 else
213 (_names_of_ALIASES;
214 _names_of_FUNCTIONS;
215 _names_of_VARIABLES) |
216 cat > "$HOME"/.parallel/ignored_vars
217 return 0
220 # --session
221 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
222 true skip
223 else
224 # Insert ::: between each level of session
225 # so you can pop off the last ::: at --end-session
226 # shellcheck disable=SC2006
227 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
228 echo :::;
229 (_names_of_ALIASES;
230 _names_of_FUNCTIONS;
231 _names_of_VARIABLES) | perl -ne '
232 BEGIN{
233 map { $ignored_vars{$_}++ }
234 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
236 chomp;
237 for(split/\s+/) {
238 if(not $ignored_vars{$_}) {
239 print $_,\"\\n\";
243 export PARALLEL_IGNORED_NAMES
244 return 0
246 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
247 true skip
248 else
249 # Pop off last ::: from PARALLEL_IGNORED_NAMES
250 # shellcheck disable=SC2006
251 PARALLEL_IGNORED_NAMES="`perl -e '
252 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
253 print $ENV{PARALLEL_IGNORED_NAMES}
255 return 0
257 # Grep alias names
258 # shellcheck disable=SC2006
259 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
260 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
261 if [ "$_alias_NAMES" = "" ] ; then
262 # no aliases selected
263 _list_alias_BODIES="true"
265 unset _alias_NAMES
267 # Grep function names
268 # shellcheck disable=SC2006
269 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
270 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
271 if [ "$_function_NAMES" = "" ] ; then
272 # no functions selected
273 _list_function_BODIES="true"
275 unset _function_NAMES
277 # Grep variable names
278 # shellcheck disable=SC2006
279 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
280 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
281 if [ "$_variable_NAMES" = "" ] ; then
282 # no variables selected
283 _list_variable_VALUES="true"
285 unset _variable_NAMES
287 # shellcheck disable=SC2006
288 PARALLEL_ENV="`
289 eval $_list_alias_BODIES;
290 eval $_list_function_BODIES;
291 eval $_list_variable_VALUES;
293 export PARALLEL_ENV
294 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
295 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
296 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
297 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
298 unset _remove_bad_NAMES _grep_REGEXP
299 unset _prefix_PARALLEL_ENV
300 # Test if environment is too big by running 'true'
301 # shellcheck disable=SC2006,SC2092
302 if `_which_PAR true` >/dev/null 2>/dev/null ; then
303 parallel "$@"
304 _parallel_exit_CODE=$?
305 # Clean up variables/functions
306 unset PARALLEL_ENV
307 unset _which_PAR _which_TRUE
308 unset _warning_PAR _error_PAR
309 # Unset _parallel_exit_CODE before return
310 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
311 else
312 unset PARALLEL_ENV;
313 _error_PAR "Your environment is too big."
314 _error_PAR "You can try 3 different approaches:"
315 _error_PAR "1. Run 'env_parallel --session' before you set"
316 _error_PAR " variables or define functions."
317 _error_PAR "2. Use --env and only mention the names to copy."
318 _error_PAR "3. Try running this in a clean environment once:"
319 _error_PAR " env_parallel --record-env"
320 _error_PAR " And then use '--env _'"
321 _error_PAR "For details see: man env_parallel"
322 return 255
326 parset() {
327 _parset_PARALLEL_PRG=parallel
328 _parset_main "$@"
331 env_parset() {
332 _parset_PARALLEL_PRG=env_parallel
333 _parset_main "$@"
336 _parset_main() {
337 # If $1 contains ',' or space:
338 # Split on , to get the destination variable names
339 # If $1 is a single destination variable name:
340 # Treat it as the name of an array
342 # # Create array named myvar
343 # parset myvar echo ::: {1..10}
344 # echo ${myvar[5]}
346 # # Put output into $var_a $var_b $var_c
347 # varnames=(var_a var_b var_c)
348 # parset "${varnames[*]}" echo ::: {1..3}
349 # echo $var_c
351 # # Put output into $var_a4 $var_b4 $var_c4
352 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
353 # echo $var_c4
355 _parset_NAME="$1"
356 if [ "$_parset_NAME" = "" ] ; then
357 echo parset: Error: No destination variable given. >&2
358 echo parset: Error: Try: >&2
359 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
360 return 255
362 if [ "$_parset_NAME" = "--help" ] ; then
363 echo parset: Error: Usage: >&2
364 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
365 echo
366 parallel --help
367 return 255
369 if [ "$_parset_NAME" = "--version" ] ; then
370 # shellcheck disable=SC2006
371 echo "parset 20230522 (GNU parallel `parallel --minversion 1`)"
372 echo "Copyright (C) 2007-2023 Ole Tange, http://ole.tange.dk and Free Software"
373 echo "Foundation, Inc."
374 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
375 echo "This is free software: you are free to change and redistribute it."
376 echo "GNU parallel comes with no warranty."
377 echo
378 echo "Web site: https://www.gnu.org/software/parallel"
379 echo
380 echo "When using programs that use GNU Parallel to process data for publication"
381 echo "please cite as described in 'parallel --citation'."
382 echo
383 return 255
385 shift
387 # Bash: declare -A myassoc=( )
388 # Zsh: typeset -A myassoc=( )
389 # Ksh: typeset -A myassoc=( )
390 # shellcheck disable=SC2039,SC2169
391 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
392 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
393 # This is an associative array
394 # shellcheck disable=SC2006
395 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@"`"
396 # The eval returns the function!
397 else
398 # This is a normal array or a list of variable names
399 # shellcheck disable=SC2006
400 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@"`"
401 # The eval returns the function!