Released as 20240522 ('Tbilisi')
[parallel.git] / src / env_parallel.csh
blob3f00e3ab1fbdc0efbbd6f5192c9a7935a9983b98
1 #!/usr/bin/env csh
3 # This file must be sourced in csh:
5 # source `which env_parallel.csh`
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2024 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-2024 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
31 set _parallel_exit_CODE=0
32 if ("`alias env_parallel`" == '' || ! $?PARALLEL_CSH) then
33 # Activate alias
34 alias env_parallel '(setenv PARALLEL_CSH "\!*"; source `which env_parallel.csh`)'
35 else
36 # Get the --env variables if set
37 # --env _ should be ignored
38 # and convert a b c to (a|b|c)
39 # If --env not set: Match everything (.*)
41 # simple 'tempfile': Return nonexisting filename: /tmp/parXXXXX
42 alias _tempfile 'perl -e do\{\$t\=\"/tmp/par\".join\"\",map\{\(0..9,\"a\"..\"z\",\"A\"..\"Z\"\)\[rand\(62\)\]\}\(1..5\)\;\}while\(-e\$t\)\;print\"\$t\\n\"'
43 set _tMpscRIpt=`_tempfile`
45 cat <<'EOF' > $_tMpscRIpt
46 #!/usr/bin/perl
48 for(@ARGV){
49 /^_$/ and $next_is_env = 0;
50 $next_is_env and push @envvar, split/,/, $_;
51 $next_is_env = /^--env$/;
53 $vars = join "|",map { quotemeta $_ } @envvar;
54 print $vars ? "($vars)" : "(.*)";
55 'EOF'
56 set _grep_REGEXP="`perl $_tMpscRIpt -- $PARALLEL_CSH`"
58 # Deal with --env _
59 cat <<'EOF' > $_tMpscRIpt
60 #!/usr/bin/perl
62 for(@ARGV){
63 $next_is_env and push @envvar, split/,/, $_;
64 $next_is_env=/^--env$/;
66 if(grep { /^_$/ } @envvar) {
67 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
68 print STDERR "parallel: Error: ",
69 "Run \"parallel --record-env\" in a clean environment first.\n";
70 } else {
71 chomp(@ignored_vars = <IN>);
72 $vars = join "|",map { quotemeta $_ } @ignored_vars;
73 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
76 'EOF'
77 set _ignore_UNDERSCORE="`perl $_tMpscRIpt -- $PARALLEL_CSH`"
78 rm $_tMpscRIpt
80 # Get the scalar and array variable names
81 set _vARnAmES=(`set | perl -ne 's/\s.*//; /^(#|_|killring|prompt2|command|PARALLEL_ENV|PARALLEL_TMP)$/ and next; /^'"$_grep_REGEXP"'$/ or next; /^'"$_ignore_UNDERSCORE"'$/ and next; print'`)
83 # Make a tmpfile for the variable definitions
84 set _tMpvARfILe=`_tempfile`
85 touch $_tMpvARfILe
86 # Make a tmpfile for the variable definitions + alias
87 set _tMpaLLfILe=`_tempfile`
88 foreach _vARnAmE ($_vARnAmES);
89 # These 3 lines break in csh ver. 20110502-3
90 # if not defined: next
91 eval if'(! $?'$_vARnAmE') continue'
92 # if $#myvar <= 1 echo scalar_myvar=$var
93 eval if'(${#'$_vARnAmE'} <= 1) echo scalar_'$_vARnAmE'='\"\$$_vARnAmE\" >> $_tMpvARfILe;
94 # if $#myvar > 1 echo array_myvar=$var
95 eval if'(${#'$_vARnAmE'} > 1) echo array_'$_vARnAmE'="$'$_vARnAmE'"' >> $_tMpvARfILe;
96 end
97 unset _vARnAmE _vARnAmES
98 # shell quote variables (--plain needed due to ignore if $PARALLEL is set)
99 # Convert 'scalar_myvar=...' to 'set myvar=...'
100 # Convert 'array_myvar=...' to 'set array=(...)'
101 cat $_tMpvARfILe | parallel --plain --shellquote | perl -pe 's/^scalar_(\S+).=/set $1=/ or s/^array_(\S+).=(.*)/set $1=($2)/ && s/\\ / /g;' > $_tMpaLLfILe
102 # Cleanup
103 rm $_tMpvARfILe; unset _tMpvARfILe
105 # ALIAS TO EXPORT ALIASES:
107 # Quote ' by putting it inside "
108 # s/'/'"'"'/g;
109 # ' => \047 " => \042
110 # s/\047/\047\042\047\042\047/g;
111 # Quoted: s/\\047/\\047\\042\\047\\042\\047/g\;
113 # Remove () from second column
114 # s/^(\S+)(\s+)\((.*)\)/\1\2\3/;
115 # Quoted: s/\^\(\\S+\)\(\\s+\)\\\(\(.\*\)\\\)/\\1\\2\\3/\;
117 # Add ' around second column
118 # s/^(\S+)(\s+)(.*)/\1\2'\3'/
119 # \047 => '
120 # s/^(\S+)(\s+)(.*)/\1\2\047\3\047/;
121 # Quoted: s/\^\(\\S+\)\(\\s+\)\(.\*\)/\\1\\2\\047\\3\\047/\;
123 # Quote ! as \!
124 # s/\!/\\\!/g;
125 # Quoted: s/\\\!/\\\\\\\!/g;
127 # Prepend with "\nalias "
128 # s/^/\001alias /;
129 # Quoted: s/\^/\\001alias\ /\;
130 alias | \
131 perl -ne '/^'"$_grep_REGEXP"'/ or next; /^'"$_ignore_UNDERSCORE"'[^_a-zA-Z]/ and next; print' | \
132 perl -pe s/\\047/\\047\\042\\047\\042\\047/g\;s/\^\(\\S+\)\(\\s+\)\\\(\(.\*\)\\\)/\\1\\2\\3/\;s/\^\(\\S+\)\(\\s+\)\(.\*\)/\\1\\2\\047\\3\\047/\;s/\^/\\001alias\ /\;s/\\\!/\\\\\\\!/g >> $_tMpaLLfILe
134 setenv PARALLEL_ENV "`cat $_tMpaLLfILe; rm $_tMpaLLfILe`";
135 unset _tMpaLLfILe;
136 # Use $PARALLEL_CSH set in calling alias
137 parallel
138 set _parallel_exit_CODE=$status
139 setenv PARALLEL_ENV
140 setenv PARALLEL_CSH
141 endif
142 (exit $_parallel_exit_CODE)