3 # This file must be sourced in zsh:
5 # source =env_parallel.zsh
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2021 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 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
35 print
-l ${(k)aliases}
37 _bodies_of_ALIASES
() {
40 echo 'alias '"$(alias $_i)"
43 _names_of_FUNCTIONS
() {
44 print
-l ${(k)functions}
46 _bodies_of_FUNCTIONS
() {
49 _names_of_VARIABLES
() {
50 print
-l ${(k)parameters}
52 _bodies_of_VARIABLES
() {
56 # These names cannot be detected
57 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 typeset
-pr | perl
-e '@r = map {
62 # sh on UnixWare: readonly TIMEOUT
63 # ash: readonly var='val
'
65 s/^(readonly )?([^= ]*)(=.*|)$/$2/ or
66 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
67 # zsh: typeset -r var='val
'
68 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
70 $vars = join "|",map { quotemeta $_ } @r;
71 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
75 # Do not transfer vars and funcs from env_parallel
76 _ignore_RO
="`_ignore_READONLY`"
77 _ignore_HARD
="`_ignore_HARDCODED`"
78 # Macos-grep does not like long patterns
79 # Old Solaris grep does not support -E
81 # grep -Ev '^(...)$' |
99 _list_function_BODIES|
100 _list_variable_VALUES|
105 _names_of_maybe_FUNCTIONS|
107 _prefix_PARALLEL_ENV|
108 _prefix_PARALLEL_ENV|
113 _which_PAR)$/x and next;
114 # Filter names matching --env
115 /^'"$_grep_REGEXP"'$/ or next;
116 /^'"$_ignore_UNDERSCORE"'$/ and next;
117 # Remove readonly variables
118 /^'"$_ignore_RO"'$/ and next;
119 /^'"$_ignore_HARD"'$/ and next;
122 _get_ignored_VARS
() {
125 $next_is_env and push @envvar, split/,/, $_;
126 $next_is_env=/^--env$/;
128 if(grep { /^_$/ } @envvar) {
129 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
130 print STDERR "parallel: Error: ",
131 "Run \"parallel --record-env\" in a clean environment first.\n";
133 chomp(@ignored_vars = <IN>);
136 if($ENV{PARALLEL_IGNORED_NAMES}) {
137 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
140 $vars = join "|",map { quotemeta $_ } @ignored_vars;
141 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
145 # Get the --env variables if set
146 # --env _ should be ignored
147 # and convert a b c to (a|b|c)
148 # If --env not set: Match everything (.*)
149 _make_grep_REGEXP
() {
152 /^_$/ and $next_is_env = 0;
153 $next_is_env and push @envvar, split/,/, $_;
154 $next_is_env = /^--env$/;
156 $vars = join "|",map { quotemeta $_ } @envvar;
157 print $vars ? "($vars)" : "(.*)";
162 # ll is an alias for ls -l (in ash)
163 # bash is a tracked alias for /bin/bash
164 # true is a shell builtin (in bash)
165 # myfunc is a function (in bash)
166 # myfunc is a shell function (in zsh)
167 # which is /usr/bin/which (in sh, bash)
168 # which is hashed (/usr/bin/which)
169 # gi is aliased to `grep -i' (in bash)
170 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
171 # Return 0 if found, 1 otherwise
173 perl
-pe '$exit += (s/ is an alias for .*// ||
174 s/ is aliased to .*// ||
175 s/ is a function// ||
176 s/ is a shell function// ||
177 s/ is a shell builtin// ||
178 s/.* is hashed .(\S+).$/$1/ ||
179 s/.* is (a tracked alias for )?//);
180 END { exit not $exit }'
183 echo "env_parallel: Warning: $@" >&2
186 echo "env_parallel: Error: $@" >&2
189 if _which_PAR parallel
>/dev
/null
; then
190 true parallel found
in path
192 _error_PAR
'parallel must be in $PATH.'
196 # Grep regexp for vars given by --env
197 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
198 unset _make_grep_REGEXP
201 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
202 unset _get_ignored_VARS
205 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
210 _names_of_VARIABLES
) |
211 cat > "$HOME"/.parallel
/ignored_vars
216 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
219 # Insert ::: between each level of session
220 # so you can pop off the last ::: at --end-session
221 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
225 _names_of_VARIABLES) | perl -ne '
227 map { $ignored_vars{$_}++ }
228 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
232 if(not $ignored_vars{$_}) {
237 export PARALLEL_IGNORED_NAMES
240 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
243 # Pop off last ::: from PARALLEL_IGNORED_NAMES
244 PARALLEL_IGNORED_NAMES
="`perl -e '
245 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
246 print $ENV{PARALLEL_IGNORED_NAMES}
251 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
252 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
253 if [ "$_alias_NAMES" = "" ] ; then
254 # no aliases selected
255 _list_alias_BODIES
="true"
259 # Grep function names
260 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
261 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
262 if [ "$_function_NAMES" = "" ] ; then
263 # no functions selected
264 _list_function_BODIES
="true"
266 unset _function_NAMES
268 # Grep variable names
269 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
270 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
271 if [ "$_variable_NAMES" = "" ] ; then
272 # no variables selected
273 _list_variable_VALUES
="true"
275 unset _variable_NAMES
278 eval $_list_alias_BODIES;
279 eval $_list_function_BODIES;
280 eval $_list_variable_VALUES;
283 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
284 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
285 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
286 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
287 unset _remove_bad_NAMES _grep_REGEXP
288 unset _prefix_PARALLEL_ENV
289 # Test if environment is too big
290 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
292 _parallel_exit_CODE
=$?
293 # Clean up variables/functions
295 unset _which_PAR _which_TRUE
296 unset _warning_PAR _error_PAR
297 # Unset _parallel_exit_CODE before return
298 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
301 _error_PAR
"Your environment is too big."
302 _error_PAR
"You can try 3 different approaches:"
303 _error_PAR
"1. Run 'env_parallel --session' before you set"
304 _error_PAR
" variables or define functions."
305 _error_PAR
"2. Use --env and only mention the names to copy."
306 _error_PAR
"3. Try running this in a clean environment once:"
307 _error_PAR
" env_parallel --record-env"
308 _error_PAR
" And then use '--env _'"
309 _error_PAR
"For details see: man env_parallel"
315 _parset_PARALLEL_PRG
=parallel
320 _parset_PARALLEL_PRG
=env_parallel
325 # If $1 contains ',' or space:
326 # Split on , to get the destination variable names
327 # If $1 is a single destination variable name:
328 # Treat it as the name of an array
330 # # Create array named myvar
331 # parset myvar echo ::: {1..10}
334 # # Put output into $var_a $var_b $var_c
335 # varnames=(var_a var_b var_c)
336 # parset "${varnames[*]}" echo ::: {1..3}
339 # # Put output into $var_a4 $var_b4 $var_c4
340 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
344 if [ "$_parset_NAME" = "" ] ; then
345 echo parset
: Error
: No destination variable given.
>&2
346 echo parset
: Error
: Try
: >&2
347 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
350 if [ "$_parset_NAME" = "--help" ] ; then
351 echo parset
: Error
: Usage
: >&2
352 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
357 if [ "$_parset_NAME" = "--version" ] ; then
358 echo "parset 20210922 (GNU parallel `parallel --minversion 1`)"
359 echo "Copyright (C) 2007-2021 Ole Tange, http://ole.tange.dk and Free Software"
360 echo "Foundation, Inc."
361 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
362 echo "This is free software: you are free to change and redistribute it."
363 echo "GNU parallel comes with no warranty."
365 echo "Web site: https://www.gnu.org/software/parallel"
367 echo "When using programs that use GNU Parallel to process data for publication"
368 echo "please cite as described in 'parallel --citation'."
374 # Bash: declare -A myassoc=( )
375 # Zsh: typeset -A myassoc=( )
376 # Ksh: typeset -A myassoc=( )
377 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
378 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
379 # This is an associative array
380 eval "`$_parset_PARALLEL_PRG -k --parset assoc,"$_parset_NAME" "$@
"`"
381 # The eval returns the function!
383 # This is a normal array or a list of variable names
384 eval "`$_parset_PARALLEL_PRG -k --parset var,"$_parset_NAME" "$@
"`"
385 # The eval returns the function!