Released as 20210922 ('Vindelev')
[parallel.git] / src / env_parallel.ksh
blobeb5eb9ed3f3a28cfe470524b2b5da4664b379814
1 #!/usr/bin/env ksh
3 # This file must be sourced in ksh:
5 # source `which env_parallel.ksh`
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
31 env_parallel() {
32 # env_parallel.ksh
34 _names_of_ALIASES() {
35 alias | perl -pe 's/=.*//'
37 _bodies_of_ALIASES() {
38 alias "$@" | perl -pe 's/^/alias /;
39 sub warning { print STDERR "env_parallel: Warning: @_\n"; }
40 if(/^alias (\S+)=\$.*\\n/) {
41 warning("Alias \"$1\" contains newline.");
42 warning("Make sure the command has at least one newline after \"$1\".");
43 warning("See BUGS in \"man env_parallel\".");
47 _names_of_maybe_FUNCTIONS() {
48 true not used
50 _names_of_FUNCTIONS() {
51 typeset +p -f | perl -pe 's/\(\).*//'
53 _bodies_of_FUNCTIONS() {
54 functions "$@"
56 _names_of_VARIABLES() {
57 typeset +p | perl -pe 's/^typeset .. //'
59 _bodies_of_VARIABLES() {
60 typeset -p "$@"
62 _ignore_HARDCODED() {
63 # These names cannot be detected
64 echo '(_|TIMEOUT|IFS)'
66 _ignore_READONLY() {
67 readonly | perl -e '@r = map {
68 chomp;
69 # sh on UnixWare: readonly TIMEOUT
70 # ash: readonly var='val'
71 # ksh: var='val'
72 # mksh: PIPESTATUS[0]
73 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
74 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
75 # zsh: typeset -r var='val'
76 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
77 $_ } <>;
78 $vars = join "|",map { quotemeta $_ } @r;
79 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
82 _remove_bad_NAMES() {
83 # Do not transfer vars and funcs from env_parallel
84 _ignore_RO="`_ignore_READONLY`"
85 _ignore_HARD="`_ignore_HARDCODED`"
86 # Macos-grep does not like long patterns
87 # Old Solaris grep does not support -E
88 # Perl Version of:
89 # grep -Ev '^(...)$' |
90 perl -ne '/^(
91 PARALLEL_ENV|
92 PARALLEL_TMP|
93 _alias_NAMES|
94 _bodies_of_ALIASES|
95 _bodies_of_FUNCTIONS|
96 _bodies_of_VARIABLES|
97 _error_PAR|
98 _function_NAMES|
99 _get_ignored_VARS|
100 _grep_REGEXP|
101 _ignore_HARD|
102 _ignore_HARDCODED|
103 _ignore_READONLY|
104 _ignore_RO|
105 _ignore_UNDERSCORE|
106 _list_alias_BODIES|
107 _list_function_BODIES|
108 _list_variable_VALUES|
109 _make_grep_REGEXP|
110 _names_of_ALIASES|
111 _names_of_FUNCTIONS|
112 _names_of_VARIABLES|
113 _names_of_maybe_FUNCTIONS|
114 _parallel_exit_CODE|
115 _prefix_PARALLEL_ENV|
116 _prefix_PARALLEL_ENV|
117 _remove_bad_NAMES|
118 _remove_readonly|
119 _variable_NAMES|
120 _warning_PAR|
121 _which_PAR)$/x and next;
122 # Filter names matching --env
123 /^'"$_grep_REGEXP"'$/ or next;
124 /^'"$_ignore_UNDERSCORE"'$/ and next;
125 # Remove readonly variables
126 /^'"$_ignore_RO"'$/ and next;
127 /^'"$_ignore_HARD"'$/ and next;
128 print;'
130 _get_ignored_VARS() {
131 perl -e '
132 for(@ARGV){
133 $next_is_env and push @envvar, split/,/, $_;
134 $next_is_env=/^--env$/;
136 if(grep { /^_$/ } @envvar) {
137 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
138 print STDERR "parallel: Error: ",
139 "Run \"parallel --record-env\" in a clean environment first.\n";
140 } else {
141 chomp(@ignored_vars = <IN>);
144 if($ENV{PARALLEL_IGNORED_NAMES}) {
145 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
146 chomp @ignored_vars;
148 $vars = join "|",map { quotemeta $_ } @ignored_vars;
149 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
150 ' -- "$@"
153 # Get the --env variables if set
154 # --env _ should be ignored
155 # and convert a b c to (a|b|c)
156 # If --env not set: Match everything (.*)
157 _make_grep_REGEXP() {
158 perl -e '
159 for(@ARGV){
160 /^_$/ and $next_is_env = 0;
161 $next_is_env and push @envvar, split/,/, $_;
162 $next_is_env = /^--env$/;
164 $vars = join "|",map { quotemeta $_ } @envvar;
165 print $vars ? "($vars)" : "(.*)";
166 ' -- "$@"
168 _which_PAR() {
169 # type returns:
170 # ll is an alias for ls -l (in ash)
171 # bash is a tracked alias for /bin/bash
172 # true is a shell builtin (in bash)
173 # myfunc is a function (in bash)
174 # myfunc is a shell function (in zsh)
175 # which is /usr/bin/which (in sh, bash)
176 # which is hashed (/usr/bin/which)
177 # gi is aliased to `grep -i' (in bash)
178 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
179 # Return 0 if found, 1 otherwise
180 LANG=C type "$@" |
181 perl -pe '$exit += (s/ is an alias for .*// ||
182 s/ is aliased to .*// ||
183 s/ is a function// ||
184 s/ is a shell function// ||
185 s/ is a shell builtin// ||
186 s/.* is hashed .(\S+).$/$1/ ||
187 s/.* is (a tracked alias for )?//);
188 END { exit not $exit }'
190 _warning_PAR() {
191 echo "env_parallel: Warning: $@" >&2
193 _error_PAR() {
194 echo "env_parallel: Error: $@" >&2
197 if _which_PAR parallel >/dev/null; then
198 true parallel found in path
199 else
200 _error_PAR 'parallel must be in $PATH.'
201 return 255
204 # Grep regexp for vars given by --env
205 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
206 unset _make_grep_REGEXP
208 # Deal with --env _
209 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
210 unset _get_ignored_VARS
212 # --record-env
213 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
214 true skip
215 else
216 (_names_of_ALIASES;
217 _names_of_FUNCTIONS;
218 _names_of_VARIABLES) |
219 cat > "$HOME"/.parallel/ignored_vars
220 return 0
223 # --session
224 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
225 true skip
226 else
227 # Insert ::: between each level of session
228 # so you can pop off the last ::: at --end-session
229 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
230 echo :::;
231 (_names_of_ALIASES;
232 _names_of_FUNCTIONS;
233 _names_of_VARIABLES) | perl -ne '
234 BEGIN{
235 map { $ignored_vars{$_}++ }
236 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
238 chomp;
239 for(split/\s+/) {
240 if(not $ignored_vars{$_}) {
241 print $_,\"\\n\";
245 export PARALLEL_IGNORED_NAMES
246 return 0
248 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
249 true skip
250 else
251 # Pop off last ::: from PARALLEL_IGNORED_NAMES
252 PARALLEL_IGNORED_NAMES="`perl -e '
253 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
254 print $ENV{PARALLEL_IGNORED_NAMES}
256 return 0
258 # Grep alias names
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 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
269 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
270 if [ "$_function_NAMES" = "" ] ; then
271 # no functions selected
272 _list_function_BODIES="true"
274 unset _function_NAMES
276 # Grep variable names
277 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
278 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
279 if [ "$_variable_NAMES" = "" ] ; then
280 # no variables selected
281 _list_variable_VALUES="true"
283 unset _variable_NAMES
285 PARALLEL_ENV="`
286 $_list_alias_BODIES;
287 $_list_function_BODIES;
288 $_list_variable_VALUES;
290 export PARALLEL_ENV
291 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
292 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
293 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
294 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
295 unset _remove_bad_NAMES _grep_REGEXP
296 unset _prefix_PARALLEL_ENV
297 # Test if environment is too big
298 if `_which_PAR true` >/dev/null 2>/dev/null ; then
299 parallel "$@"
300 _parallel_exit_CODE=$?
301 # Clean up variables/functions
302 unset PARALLEL_ENV
303 unset _which_PAR _which_TRUE
304 unset _warning_PAR _error_PAR
305 # Unset _parallel_exit_CODE before return
306 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
307 else
308 unset PARALLEL_ENV;
309 _error_PAR "Your environment is too big."
310 _error_PAR "You can try 3 different approaches:"
311 _error_PAR "1. Run 'env_parallel --session' before you set"
312 _error_PAR " variables or define functions."
313 _error_PAR "2. Use --env and only mention the names to copy."
314 _error_PAR "3. Try running this in a clean environment once:"
315 _error_PAR " env_parallel --record-env"
316 _error_PAR " And then use '--env _'"
317 _error_PAR "For details see: man env_parallel"
318 return 255
322 parset() {
323 _parset_PARALLEL_PRG=parallel
324 _parset_main "$@"
327 env_parset() {
328 _parset_PARALLEL_PRG=env_parallel
329 _parset_main "$@"
332 _parset_main() {
333 # If $1 contains ',' or space:
334 # Split on , to get the destination variable names
335 # If $1 is a single destination variable name:
336 # Treat it as the name of an array
338 # # Create array named myvar
339 # parset myvar echo ::: {1..10}
340 # echo ${myvar[5]}
342 # # Put output into $var_a $var_b $var_c
343 # varnames=(var_a var_b var_c)
344 # parset "${varnames[*]}" echo ::: {1..3}
345 # echo $var_c
347 # # Put output into $var_a4 $var_b4 $var_c4
348 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
349 # echo $var_c4
351 _parset_NAME="$1"
352 if [ "$_parset_NAME" = "" ] ; then
353 echo parset: Error: No destination variable given. >&2
354 echo parset: Error: Try: >&2
355 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
356 return 255
358 if [ "$_parset_NAME" = "--help" ] ; then
359 echo parset: Error: Usage: >&2
360 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
361 echo
362 parallel --help
363 return 255
365 if [ "$_parset_NAME" = "--version" ] ; then
366 echo "parset 20210922 (GNU parallel `parallel --minversion 1`)"
367 echo "Copyright (C) 2007-2021 Ole Tange, http://ole.tange.dk and Free Software"
368 echo "Foundation, Inc."
369 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
370 echo "This is free software: you are free to change and redistribute it."
371 echo "GNU parallel comes with no warranty."
372 echo
373 echo "Web site: https://www.gnu.org/software/parallel"
374 echo
375 echo "When using programs that use GNU Parallel to process data for publication"
376 echo "please cite as described in 'parallel --citation'."
377 echo
378 return 255
380 shift
382 # Bash: declare -A myassoc=( )
383 # Zsh: typeset -A myassoc=( )
384 # Ksh: typeset -A myassoc=( )
385 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
386 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
387 # This is an associative array
388 eval "`$_parset_PARALLEL_PRG -k --parset assoc,"$_parset_NAME" "$@"`"
389 # The eval returns the function!
390 else
391 # This is a normal array or a list of variable names
392 eval "`$_parset_PARALLEL_PRG -k --parset var,"$_parset_NAME" "$@"`"
393 # The eval returns the function!