author | A.M. Thurnherr <athurnherr@yahoo.com> |
Tue, 27 Nov 2018 15:40:03 -0500 | |
changeset 37 | b24d11f7dfc4 |
parent 35 | d3f6ca34c4ea |
child 47 | dde46143288c |
permissions | -rw-r--r-- |
0 | 1 |
#/usr/bin/perl |
2 |
#====================================================================== |
|
3 |
# A N T S U S A G E . P L |
|
4 |
# doc: Fri Jun 19 13:43:05 1998 |
|
35 | 5 |
# dlm: Wed Dec 13 09:09:58 2017 |
0 | 6 |
# (c) 1998 A.M. Thurnherr |
35 | 7 |
# uE-Info: 167 49 NIL 0 0 70 2 2 4 NIL ofnI |
0 | 8 |
#====================================================================== |
9 |
||
10 |
# HISTORY: |
|
11 |
# Dec 30, 1998: - removed directory from $0 |
|
12 |
# - added global -P option (pass comments) |
|
13 |
# Jan 02, 1999: - changed -P to -T and added -P)refix for [./fnr] |
|
14 |
# Feb 08, 1999: - added &antsUsageError() |
|
15 |
# Feb 17, 1999: - added -N |
|
16 |
# Feb 28, 1999: - forced string interpretation for -O, -R, -I |
|
17 |
# Mar 08, 1999: - added -L, $antsLibs |
|
18 |
# Mar 20, 1999: - added exit(1) for unknown options |
|
19 |
# - added optional argument to &antsUsageError() |
|
20 |
# May 28, 1999: - library loading generated headers even on -Q |
|
21 |
# Jun 31, 1999: - added &antsDescription() |
|
22 |
# Jul 31, 1999: - added parameter typechecking (field, float, card, int, file) |
|
23 |
# Aug 02, 1999: - changed &antsDescription() to be option-dependent |
|
24 |
# Sep 18, 1999: - added option delimiter -- |
|
25 |
# - treat cardinals & integers differently |
|
26 |
# - added option typechecking funs, e.g. &antsIntOpt() |
|
27 |
# - auto set -Q if stdout is tty |
|
28 |
# Sep 19, 1999: - changed &getopts() from Perl4 to Perl5 |
|
29 |
# Sep 21, 1999: - load local libraries first |
|
30 |
# Mar 06, 2000: - added for-M)at |
|
31 |
# Mar 07, 2000: - worked on -M |
|
32 |
# Aug 24, 2000: - removed setting -Q on tty (bad for [yoyo] and [Split]) |
|
33 |
# Aug 28, 2000: - added -Z |
|
34 |
# - changed -P to -A |
|
35 |
# - added new -P |
|
36 |
# Sep 19, 2000: - set opt_M (dodgily) if not specifically set (only affects |
|
37 |
# [count] so far) |
|
38 |
# Sep 20, 2000: - added []-syntax to -P |
|
39 |
# - added workaround for -M %0xd bug |
|
40 |
# Sep 25, 2000: - changed order of -P and -L processing |
|
41 |
# Sep 26, 2000: - cosmetics |
|
42 |
# Nov 13, 2000: - BUG: -- had left a single empty argument |
|
43 |
# Nov 15, 2000: - added &antsParamParam() |
|
44 |
# - added &antsFileOpt() |
|
45 |
# Nov 17, 2000: - made -P override any header PARAMs |
|
46 |
# Jan 4, 2001: - moved -L processing before -P |
|
47 |
# Feb 8, 2001: - added -G)range option |
|
48 |
# Mar 17, 2001: - param->arg |
|
49 |
# - added @file:field argument syntax |
|
50 |
# - added #num-num[:step] syntax |
|
51 |
# Mar 23, 2001: - added prefix{#-#}suff syntax |
|
52 |
# Mar 31, 2001: - changed -G)range to #[..#]{,#[..#} |
|
53 |
# - added -F)ields f{,f} |
|
54 |
# Apr 3, 2001: - added +f syntax to -F)ields |
|
55 |
# Apr 5, 2001: - added f+ syntax to -F)ields |
|
56 |
# Apr 24, 2001: - removed err msg in case of -G f:* (select numbers) |
|
57 |
# Apr 30, 2001: - shortened date, added pwd |
|
58 |
# Jun 23, 2001: - removed default setting of $opt_M |
|
59 |
# Jul 6, 2001: - added degree notation to &antsFloatArg() via str2num() |
|
60 |
# Jul 10, 2001: - added select field names (for V.3) |
|
61 |
# Jul 13, 2001: - store ONLY field names (replaced on 1st use) |
|
62 |
# - added quotes to usage history |
|
63 |
# Jul 15, 2001: - added &antsNewFieldOpt() |
|
64 |
# Jul 16, 2001: - made it work with Description again |
|
65 |
# Jul 24, 2001: - removed fnr lookup on -G |
|
66 |
# Jul 30, 2001: - BUG: made parseHeader conditional on $antsFixedFormat |
|
67 |
# Aug 9, 2001: - chgd pref{#-#}suff syntax to expand only to exist files |
|
68 |
# Oct 28, 2001: - BUG: added -K handling before parseHeader |
|
69 |
# Nov 22, 2001: - moved logic into &antsParseHeader() |
|
70 |
# Nov 28, 2001: - cosmetics |
|
71 |
# Jan 18, 2002: - old -N => -X; new -N |
|
72 |
# Mar 24, 2002: - BUG: &antsFieldArg('file') did not handle %PARAMs correctly |
|
73 |
# Jul 26, 2002: - removed common usage from antsUsageError unless -U is set |
|
74 |
# Jan 6, 2003: - added regexp option to -G |
|
75 |
# Feb 9, 2003: - BUG: {103-103}.ens hung |
|
76 |
# Jun 26, 2004: - made sure that near-zero \#args are rounded to zero |
|
77 |
# Jun 27, 2004: - BUG: \#22-14 did not work correctly any more |
|
78 |
# Jul 12, 2004: - removed &antsDescription() |
|
79 |
# May 5, 2005: - added &antsNewField() |
|
80 |
# May 17, 2005: - allowed &antsFieldArg() to check [Layout] |
|
81 |
# Nov 1, 2005: - disallowed numeric options by adding -- if first argument |
|
82 |
# begins with -[0-9] |
|
83 |
# Nov 8, 2005: - removed -P, -T => -P, -Z => -T, added -Z |
|
84 |
# Nov 17, 2005: - removed $antsLibs |
|
85 |
# - removed remainder of -D |
|
86 |
# - added $antsARGV0 for [yoyo] |
|
87 |
# - added !<arg> quoting (for filenames) |
|
88 |
# Nov 18, 2005: - finally allowed %PARAMs in -G |
|
89 |
# Nov 21, 2005: - BUG: had not been allowed in -G fieldname |
|
90 |
# Dec 7, 2005: - antsFName -> antsLayout (not tested) |
|
91 |
# Dec 9, 2005: - Version 3.2 (see [HISTORY]) |
|
92 |
# Dec 11, 2005: - error on 0 args & tty stdin |
|
93 |
# Dec 20, 2005: - created &antsFieldInFileArg() & added flag to &antsFieldArg |
|
94 |
# - simplified opt_M, because it now works w/o $# |
|
95 |
# Dec 22, 2005: - added $antsInteractive for [abc] |
|
96 |
# Dec 23, 2005: - replaced defined(@array) (c.f. perlfunc(1)) |
|
97 |
# Dec 31, 2005: - BUG: @-notation was broken (used antique [fields]!!!) |
|
98 |
# Jan 3, 2006: - added support for -S)elect |
|
99 |
# Jan 9, 2006: - removed old line-masking code |
|
100 |
# Jan 12, 2006: - removed -A support |
|
101 |
# - removed support for $ENV{ANTS} |
|
102 |
# - changed from old -H)eader <skip> to -H)ead <n lines> |
|
103 |
# Jan 13, 2006: - moved -G handling to -S |
|
104 |
# - BUG: -G regexpr did not allow : |
|
105 |
# - renamed -T)rim to -C)anonical |
|
106 |
# - removed warnings on -M/-C |
|
107 |
# - removed weird -Z)ap |
|
108 |
# Jan 14, 2006: - removed -G (now handled by -S) |
|
109 |
# - changed semantics of pref{#-#}suff special arg to |
|
110 |
# expand non-existing file names |
|
111 |
# Jul 28, 2006: - made special arg #-#:# numerically more robust |
|
112 |
# Aug 18, 2006: - improved special arg to pref{#,#-#,...} and allow / instead of , |
|
113 |
# Dec 14, 2006: - exported handling of -X to [antsio.pl] |
|
114 |
# - disallow -P & -Q |
|
115 |
# May 31, 2007: - added -G |
|
116 |
# Nov 28, 2007: - replaced / by + to separate ranges in {} arguments |
|
117 |
# Mar 4, 2008: - disallow partial fname matches in antsNewField*() |
|
118 |
# Mar 24, 2008: - new usage formatting (glorious!) |
|
119 |
# Mar 25, 2008: - added $antsSummary |
|
120 |
# Mar 26, 2008: - extended -F syntax |
|
121 |
# Mar 27, 2008: - modified &antsUsage() to allow disabling common options |
|
122 |
# Apr 24, 2008: - added &antsFieldListOpt() |
|
123 |
# May 7, 2008: - disabled -N/-S for utilities without header parsing |
|
124 |
# May 13, 2008: - moved -U to standard usage message |
|
125 |
# Aug 5, 2008: - suppress empty usage lines |
|
126 |
# Nov 12, 2008: - added opt_T |
|
127 |
# Aug 24, 2009: - added V4 dependency on @file:field special args, file args & opts |
|
128 |
# Oct 3, 2009: - BUG: sometime recently I had changed the pref{}suff semantics to be |
|
129 |
# much more permissive; this led to problems as args like {print $0} |
|
130 |
# were erroneously expanded; changed => pref{}suff is only expanded |
|
131 |
# if first expanded element is existing file |
|
132 |
# - special args expanding to zilch are not expanded any more |
|
133 |
# Aug 16, 2010: - added -A)ctivate output (F/S Poseidon, P403, Lucky Strike) |
|
134 |
# Aug 28, 2010: - added suppress -D)ependency check option |
|
135 |
# - improve common-options usage help |
|
136 |
# Oct 15, 2010: - removed diagnostic output about loading libs |
|
137 |
# Oct 29, 2010: - replaced list by Cat in expansion of @-special args |
|
138 |
# Dec 21, 2010: - made $@ at end of -F list optional (i.e. -F can end with ,) |
|
139 |
# Jul 21, 2011: - modified -D usage info |
|
140 |
# Sep 19, 2011: - SEMANTICS: pref{#-#}suff does now produce warning on missing files |
|
141 |
# Oct 3, 2011: - BUG: pref{}suff special args were (again) too permissive and matched |
|
142 |
# output formats; this time, I solved problem by making regexp |
|
143 |
# more restrictive; if this does not work, I can go back to |
|
144 |
# earlier solution (see BUG Oct 3 2009) |
|
145 |
# Oct 16, 2011: - added support for \, escape in -F to protect commas used, e.g. in |
|
146 |
# function calls, from splitting the opt_F argument string |
|
147 |
# Nov 11, 2011: - BUG: antsNewField did not work for external layouts |
|
148 |
# Dec 29, 2011: - BUG: antsNewField did not work Cat -f c=1,1,10 without input |
|
149 |
# - BUG: antsNewField did still not work for external layouts (the bug |
|
150 |
# resulted in always extending the layout, even when the field already |
|
151 |
# existed) |
|
152 |
# Feb 13, 2012: - antsNewFieldOpt simplified by using 2nd arg to fnrNoErr |
|
3 | 153 |
# Oct 29, 2012: - diabled "no file" messages on special args |
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
154 |
# Mar 29, 2013: - added support for $ANTSLIBS |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
155 |
# Apr 2, 2013: - BUG: pref{}suff special args did sometimes produce unexpanded as well |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
156 |
# as expanded output (unexpanded should be produced only if the |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
157 |
# expansion is empty) |
13 | 158 |
# Jul 30, 2014: - added special args to -U)sage output |
10 | 159 |
# Jan 30, 2015: - added &antsFunOpt() |
160 |
# Jan 31, 2015: - made it work |
|
20 | 161 |
# May 15, 2015: - changed (()) semantics to expand only to existing files |
30 | 162 |
# Aug 24, 2016: - removed -ve number heuristics (unshifting a -- under certain conditions), |
163 |
# which I believe is ancient |
|
32 | 164 |
# Aug 30, 2017: - BUG: new (()) semantics did not work for single-value ranges |
35 | 165 |
# Dec 9, 2017: - added -E, $antsSuppressCommonOptions |
166 |
# - common options cosmetics |
|
167 |
# Dec 13, 2017: - BUG: common options cosmetics |
|
0 | 168 |
|
169 |
# NOTES: |
|
170 |
# - ksh expands {}-arguments with commas in them!!! Use + instead |
|
171 |
||
172 |
use Getopt::Std; |
|
173 |
||
174 |
sub antsUsageError() { # die with Usage error |
|
175 |
if (defined($antsSummary)) { |
|
176 |
print(STDERR "\n$0 -- $antsSummary\n\n") |
|
177 |
} else { |
|
178 |
print(STDERR "\n$0\n\n") |
|
179 |
} |
|
180 |
if ($opt_U) { |
|
13 | 181 |
print(STDERR "Options & Arguments: $antsCurUsage$_[0]\n\n" . |
35 | 182 |
"Common Input Options:\n" . |
183 |
"\t[-I)nput <field-separator>]\n" . |
|
184 |
"\t[-S)elect <addr-expr>] [-N)ums f[,...]] [-H)ead <n lines>]\n" . |
|
185 |
"\t[-L)oad <lib,...>]\n" . |
|
186 |
"\t[allows -E)mbedded layout changes]\n\n" . |
|
187 |
"Common Output Options:\n" . |
|
13 | 188 |
"\t[-F)ields {%P|f|[\$@]|[f]=expr}[,...]]\n" . |
189 |
"\t[num for-M)at] [-C)anonical numbers] [-G)eographic lat/lon]\n" . |
|
190 |
"\t[-A)ctivate output] [LaTeX -T)able output]\n" . |
|
191 |
"\t[-P)ass comments] [-Q)uiet (no headers)] [-X (no new header)]\n" . |
|
192 |
"\t[suppress -D)ependency checks & addition of new dependencies]\n" . |
|
35 | 193 |
"\t[-O)utput <field-separator>] [output -R)ecord <separator>]\n\n" . |
13 | 194 |
"Special Argument Expansion:\n" . |
195 |
"\t@<file>:<field>\t\t\t<field> values in <file>\n" . |
|
196 |
"\t#<from>-<to>[:<step>]\t\tenumerated values\n" . |
|
197 |
"\t[prefix]{<ranges>}[suffix]\texisting files (ranges: <from>[-<to>][+...])\n" . |
|
198 |
"\t[prefix]((<file>))[suffix]\tfiles, using <file> with ranges\n"); |
|
0 | 199 |
} else { |
200 |
print(STDERR "Options & Arguments: $antsCurUsage$_[0]\n"); |
|
201 |
} |
|
202 |
croak("\n"); |
|
203 |
} |
|
204 |
||
205 |
# NB: "-" as first char in opts string disables common-option processing |
|
206 |
||
207 |
sub antsUsage($$@) { # handle options |
|
208 |
my($opts,$min,@usage) = @_; |
|
35 | 209 |
my($cOpts) = 'ADEM:QN:XCGPH:UI:O:R:L:F:S:T'; |
0 | 210 |
my($d,$p); |
35 | 211 |
|
212 |
if ($antsSuppressCommonOptions) { # non-core utilites (e.g. LADCP_w) do not use common options |
|
213 |
$cOpts = '' |
|
214 |
} else { |
|
215 |
$antsCurUsage .= "\n\t[print full -U)sage]" |
|
216 |
} |
|
217 |
||
0 | 218 |
foreach my $uln (@usage) { |
219 |
$antsCurUsage .= "\n\t$uln" # suppress emtpy, e.g. |
|
220 |
unless ($uln eq ''); # for interp. model usage |
|
221 |
} |
|
222 |
||
223 |
&antsUsageError() # no args && tty stdin |
|
224 |
if (!$antsInteractive && $min == 0 && @ARGV == 0 && -t 0); |
|
225 |
||
30 | 226 |
# The following line of code, disabled Aug 24, 2016, prevents |
227 |
# numerical options from working, e.g. in LADCP_w. |
|
228 |
# |
|
229 |
# unshift(@ARGV,'--') if ($ARGV[0] =~ /^-\d/); # -ve number heuristics |
|
0 | 230 |
|
231 |
chomp($0 = `basename $0`); # set scriptname |
|
232 |
chop($d = `date +%D`); # build header line |
|
233 |
chop($p = `pwd`); |
|
234 |
$p = "..." . substr($p,-17) if (length($p) > 20); |
|
235 |
$antsCurHeader = "#ANTS# [$d $p] $0"; |
|
236 |
my($i,$eoo); |
|
237 |
for ($i=0; $i<=$#ARGV; $i++) { |
|
238 |
$antsCurHeader .= " '$ARGV[$i]'"; |
|
239 |
$eoo = 1 if ($ARGV[$i] eq '--'); # -- handling |
|
240 |
$ARGV[$i] = "!$ARGV[$i+1]" if ($eoo); # make -ve non-options |
|
241 |
} |
|
242 |
$antsCurHeader .= "\n"; |
|
243 |
pop(@ARGV) if ($eoo); # remove last ARG |
|
244 |
||
245 |
if ($opts =~ m{^-}) { # no common options processing |
|
246 |
$opts = $'; |
|
247 |
undef($cOpts); |
|
248 |
} |
|
249 |
&antsUsageError(), exit(1) # parse options |
|
250 |
unless (&getopts($cOpts . $opts)); |
|
251 |
||
252 |
unless ($antsParseHeader) { |
|
253 |
croak("$0: -S not supported (implementation restriction)\n") |
|
254 |
if defined($opt_S); |
|
255 |
croak("$0: -N not supported (implementation restriction)\n") |
|
256 |
if defined($opt_N); |
|
257 |
} |
|
258 |
||
259 |
if ($eoo) { # reset args |
|
260 |
for ($i=0; $i<=$#ARGV; $i++) { |
|
261 |
$ARGV[$i] = substr($ARGV[$i],1); |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
if (defined($cOpts)) { # process common options |
|
266 |
||
267 |
croak("$0: illegal option combination (-P & -Q)\n") |
|
268 |
if ($opt_P && $opt_Q); |
|
269 |
||
270 |
&antsActivateOut() if ($opt_A); # activate output |
|
271 |
||
35 | 272 |
$antsAllowEmbeddedLayoutChange = $opt_E; # allow embedded layout changes |
273 |
||
0 | 274 |
if ($opt_T) { # LaTeX table output |
275 |
croak("$0: illegal option combination (-T & -G)\n") |
|
276 |
if ($opt_G); |
|
277 |
croak("$0: illegal option combination (-T & -O)\n") |
|
278 |
if defined($opt_O); |
|
279 |
$opt_O = ' & '; |
|
280 |
croak("$0: illegal option combination (-T & -R)\n") |
|
281 |
if defined($opt_R); |
|
282 |
$opt_R = ' \\\\\\\\\n'; |
|
283 |
} |
|
284 |
||
285 |
if (defined($opt_I)) { # defaults |
|
286 |
eval('$opt_I = "' . $opt_I .'";'); # interpret strings |
|
287 |
} else { # ... as perl strings |
|
288 |
$opt_I = '\s+'; |
|
289 |
} |
|
290 |
if (defined($opt_O)) { |
|
291 |
eval('$opt_O = "' . $opt_O .'";'); |
|
292 |
} else { |
|
293 |
$opt_O = "\t"; |
|
294 |
} |
|
295 |
if (defined($opt_R)) { |
|
296 |
eval('$opt_R = "' . $opt_R .'";'); |
|
297 |
} else { |
|
298 |
$opt_R = "\n"; |
|
299 |
} |
|
300 |
||
301 |
if (defined($opt_L)) { # load libraries |
|
302 |
foreach $lib (split(',',$opt_L)) { |
|
303 |
if (-r "lib$lib.pl") { |
|
304 |
require "lib$lib.pl"; |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
305 |
} elsif (-r "$ANTS/lib$lib.pl") { |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
306 |
require "$ANTS/lib$lib.pl"; |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
307 |
} elsif (-r "$ANTSLIBS/lib$lib.pl") { |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
308 |
require "$ANTSLIBS/lib$lib.pl"; |
0 | 309 |
} else { |
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
310 |
croak("$0: cannot load {.,$ANTS,$ANTSLIBS}/lib$lib.pl\n"); |
0 | 311 |
} |
312 |
} |
|
313 |
} |
|
314 |
||
315 |
if (defined($opt_N)) { # parse -N)ums |
|
316 |
@antsNFNames = split(',',$opt_N); |
|
317 |
} |
|
318 |
||
319 |
if (defined($opt_F)) { # parse -F)ields |
|
320 |
$opt_F =~ s/\\,/aNtScOmMa/g; |
|
321 |
@antsOutExprs = split(',',$opt_F); |
|
322 |
push(@antsOutExprs,'$@') if ($opt_F =~ /,$/); |
|
323 |
foreach my $e (@antsOutExprs) { |
|
324 |
$e =~ s/aNtScOmMa/,/; |
|
325 |
} |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
my($ai); |
|
330 |
for ($ai=0; $ai<=$#ARGV; $ai++) { # parse special args |
|
331 |
my(@exp); |
|
332 |
if ($ARGV[$ai] =~ /^@([^:]+):(.+)/) { # @file:field |
|
333 |
&antsAddDeps($1); |
|
334 |
@exp = `Cat -QF$2 $1`; |
|
335 |
croak("(...while expanding $ARGV[$ai])\n") if ($?); |
|
336 |
} elsif ($ARGV[$ai] =~ /^#(-?[\d\.]+)-(-?[\d\.]+):?(-?[\d\.]+)?/) { |
|
337 |
my($step) = 1; # #num-num:step |
|
338 |
if (defined($3)) { |
|
339 |
$step = $3; |
|
340 |
} elsif ($2 < $1) { |
|
341 |
$step = -1; |
|
342 |
} |
|
343 |
if ($step > 0) { |
|
344 |
for (my($c)=0,my($i)=$1; $i<=$2+$step/1e6; $c++,$i=$1+$c*$step) { |
|
345 |
$i = 0 if (abs($i) < abs($step) / 1e6); |
|
346 |
push(@exp,$i); |
|
347 |
} |
|
348 |
} else { |
|
349 |
for (my($c)=0,my($i)=$1; $i>=$2+$step/1e6; $c++,$i=$1+$c*$step) { |
|
350 |
$i = 0 if (abs($i) < abs($step) / 1e6); |
|
351 |
push(@exp,$i); |
|
352 |
} |
|
353 |
} |
|
354 |
} elsif ($ARGV[$ai] =~ m{\{([-\+,\d]+)\}}) { # pref{list of ranges}suff |
|
355 |
my($pref) = $`; my($suff) = $'; |
|
356 |
foreach my $range (split('[,\+]',$1)) { |
|
357 |
if ($range =~ /^(\d+)-(\d+)$/) { |
|
358 |
my($fmt) = length($1)==length($2) ? |
|
359 |
sprintf("$pref%%0%dd$suff",length($1)) : "$pref%d$suff"; |
|
360 |
if ($2 > $1) { |
|
361 |
for (my($i)=$1; $i<=$2; $i++) { |
|
362 |
my($f) = sprintf($fmt,$i); |
|
363 |
if (-f $f) { push(@exp,$f); } |
|
3 | 364 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 365 |
} |
366 |
} else { |
|
367 |
for (my($i)=$1; $i>=$2; $i--) { |
|
368 |
my($f) = sprintf($fmt,$i); |
|
369 |
if (-f $f) { push(@exp,$f); } |
|
3 | 370 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 371 |
} |
372 |
} |
|
373 |
} else { |
|
374 |
my($f) = "$pref$range$suff"; |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
375 |
# print(STDERR "f = $pref . $range . $suff\n"); |
0 | 376 |
if (-f $f) { push(@exp,$f); } |
3 | 377 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 378 |
} |
379 |
} |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
380 |
@exp = ($ARGV[$ai]) # make sure it *was* special arg |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
381 |
unless (@exp); |
13 | 382 |
} elsif ($ARGV[$ai] =~ m{\(\(([^\)]+)\)\)}) { # pref((file))suff |
383 |
my($pref) = $`; my($suff) = $'; |
|
384 |
&antsAddDeps($1); |
|
385 |
open(F,$1) || croak("$1: $!\n"); |
|
386 |
while (<F>) { |
|
387 |
s/#.*//g; next if /^\s+$/; # handle comments and empty lines |
|
388 |
s/\s*//g; |
|
389 |
chomp($_); |
|
390 |
if ($_ =~ /^(\d+)-(\d+)$/) { |
|
391 |
my($fmt) = length($1)==length($2) ? |
|
392 |
sprintf("$pref%%0%dd$suff",length($1)) : "$pref%d$suff"; |
|
393 |
if ($2 > $1) { |
|
394 |
for (my($i)=$1; $i<=$2; $i++) { |
|
20 | 395 |
my($f) = sprintf($fmt,$i); |
396 |
push(@exp,$f) if (-f $f); |
|
13 | 397 |
} |
398 |
} else { |
|
399 |
for (my($i)=$1; $i>=$2; $i--) { |
|
20 | 400 |
my($f) = sprintf($fmt,$i); |
401 |
push(@exp,$f) if (-f $f); |
|
13 | 402 |
} |
403 |
} |
|
404 |
} else { |
|
32 | 405 |
push(@exp,"$pref$_$suff") if (-f "$pref$_$suff"); |
13 | 406 |
} |
407 |
} |
|
408 |
close(F); |
|
0 | 409 |
} else { # regular argument |
410 |
next; |
|
411 |
} |
|
412 |
&antsInfo("WARNING: special arg $ARGV[$ai] expands to nothing"), |
|
413 |
push(@exp,$ARGV[$ai]) |
|
414 |
unless ($#exp >= 0); |
|
415 |
splice(@ARGV,$ai,1,@exp); |
|
416 |
} |
|
417 |
||
418 |
my($nargs) = $#ARGV + 1; # check arg count |
|
419 |
&antsUsageError() if ($opt_U || ($min > 0) && ($nargs < $min)); |
|
420 |
||
421 |
$antsARGV0 = $ARGV[0]; # save 1st filename |
|
422 |
&antsParseHeader(); # get fields & params |
|
423 |
||
424 |
for (my($i)=0; $i<=$#ARGV; $i++) { # remove leading ! from args |
|
425 |
$ARGV[$i] =~ s/^!//; |
|
426 |
} |
|
427 |
||
428 |
return $nargs; |
|
429 |
} |
|
430 |
||
431 |
#====================================================================== |
|
432 |
# argument typechecking |
|
433 |
#====================================================================== |
|
434 |
||
435 |
sub antsFieldInFileArg($) |
|
436 |
{ |
|
437 |
my($fn) = @_; |
|
438 |
my($fnr); |
|
439 |
||
440 |
&antsUsageError() unless defined($ARGV[0]); |
|
441 |
open(F,$fn) || croak("$fn: $!\n"); |
|
442 |
if ($ARGV[0] =~ /^%/) { |
|
443 |
croak("$0: no PARAM $ARGV[0] in $fn\n") |
|
444 |
unless (defined(&antsFileScanParam(F,$'))); |
|
445 |
$fnr = $ARGV[0]; |
|
446 |
} else { |
|
447 |
$fnr = &antsFileScanFnr(F,$ARGV[0]); |
|
448 |
unless (defined($fnr)) { |
|
449 |
print(STDERR "$0: WARNING: no field $ARGV[0] in $fn\n"); |
|
450 |
$fnr = &fnr($ARGV[0]); |
|
451 |
} |
|
452 |
} |
|
453 |
close(F); |
|
454 |
||
455 |
shift(@ARGV); |
|
456 |
return $fnr; |
|
457 |
} |
|
458 |
||
459 |
sub antsFieldArg($) |
|
460 |
{ |
|
461 |
&antsUsageError() unless defined($ARGV[0]); |
|
462 |
my($paramsAllowed) = @_; |
|
463 |
my($fnr) = &fnr($ARGV[0]); |
|
464 |
croak("$0: $ARGV[0] is not a field\n") |
|
465 |
unless (numberp($fnr) || $paramsAllowed); |
|
466 |
shift(@ARGV); |
|
467 |
return $fnr; |
|
468 |
} |
|
469 |
||
470 |
sub antsFieldOpt(@) |
|
471 |
{ |
|
472 |
my($opt,$default) = @_; |
|
473 |
if (ref($opt)) { # reference => set |
|
474 |
if (defined(${$opt})) { # defined => check,set |
|
475 |
${$opt} = &fnr(${$opt}); |
|
476 |
} elsif (defined($default)) { # not defined => default |
|
477 |
${$opt} = &fnr($default); |
|
478 |
} |
|
479 |
return ${$opt}; |
|
480 |
} else { # not ref => do not set |
|
481 |
return defined($opt) ? &fnr($opt) : |
|
482 |
defined($default) ? &fnr($default) : $opt; |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
sub antsFieldListOpt($) |
|
487 |
{ |
|
488 |
my($opt) = @_; |
|
489 |
my(@fn) = split(',',$opt); |
|
490 |
my(@fi); |
|
491 |
||
492 |
for (my($i)=0; $i<@fn; $i++) { |
|
493 |
$fi[$i] = &fnr($fn[$i]); |
|
494 |
} |
|
495 |
return @fi; |
|
496 |
} |
|
497 |
||
498 |
sub antsNewField($) # allocate if needed |
|
499 |
{ |
|
500 |
my($fname) = @_; |
|
501 |
my($fnr); |
|
502 |
||
503 |
$fnr = &fnrNoErr($fname,1); # exact match |
|
504 |
unless (defined($fnr)) { |
|
505 |
return $antsBufNFields++ # external layout |
|
506 |
unless ($antsBufNFields==0 || @antsLayout); |
|
507 |
@antsNewLayout = @antsLayout |
|
508 |
unless (@antsNewLayout); |
|
509 |
push(@antsNewLayout,$fname); |
|
510 |
$fnr = $#antsNewLayout; |
|
511 |
} |
|
512 |
$antsBufNFields = $fnr+1 if ($fnr >= $antsBufNFields); |
|
513 |
return $fnr; |
|
514 |
} |
|
515 |
||
516 |
sub antsNewFieldOpt(@) # allocate if does not exist |
|
517 |
{ |
|
518 |
my($opt,$default) = @_; |
|
519 |
my($fname,$fnr); |
|
520 |
||
521 |
if (ref($opt)) { # reference => set |
|
522 |
if (defined(${$opt})) { # defined => check,set |
|
523 |
$fname = ${$opt}; |
|
524 |
} elsif (defined($default)) { # not defined => default |
|
525 |
$fname = $default; |
|
526 |
} |
|
527 |
if (defined($fname)) { |
|
528 |
$fnr = &antsNewField($fname); |
|
529 |
${$opt} = $fnr; |
|
530 |
return $fnr; |
|
531 |
} else { return undef; } |
|
532 |
} else { # not ref => do not set |
|
533 |
if (defined($opt)) { |
|
534 |
$fname = $opt; |
|
535 |
} elsif (defined($default)) { |
|
536 |
$fname = $default; |
|
537 |
} |
|
538 |
return defined($fname) ? &antsNewField($fname) : undef; |
|
539 |
} |
|
540 |
} |
|
541 |
||
542 |
sub antsNoFileErr($$) |
|
543 |
{ |
|
544 |
croak("$0: $_[0] $_[1] is not a valid file\n") |
|
545 |
unless (-r $_[1]); |
|
546 |
&antsAddDeps($_[1]); |
|
547 |
} |
|
548 |
||
549 |
sub antsFileArg() # arg 1 => do not shift |
|
550 |
{ |
|
551 |
&antsUsageError() unless defined($ARGV[0]); |
|
552 |
&antsNoFileErr("Argument",$ARGV[0]); |
|
553 |
my($res) = $ARGV[0]; |
|
554 |
shift(@ARGV) unless ($_[0]); |
|
555 |
return $res; |
|
556 |
} |
|
557 |
||
558 |
sub antsFileOpt($) |
|
559 |
{ |
|
560 |
my($opt) = @_; |
|
561 |
&antsNoFileErr("Option Argument",$opt) |
|
562 |
if (defined($opt)); |
|
563 |
} |
|
564 |
||
565 |
sub antsParamArg() |
|
566 |
{ |
|
567 |
&antsUsageError() unless defined($ARGV[0]); |
|
568 |
croak("$0: Argument $ARGV[0] is not a valid PARAM\n") |
|
569 |
unless ($ARGV[0] =~ /^%/); |
|
570 |
shift(@ARGV); |
|
571 |
return $'; |
|
572 |
} |
|
573 |
||
574 |
sub antsNoCardErr($$) |
|
575 |
{ |
|
576 |
croak("$0: $_[0] $_[1] is not a cardinal number\n") |
|
577 |
unless (cardinalp($_[1])); |
|
578 |
} |
|
579 |
||
580 |
sub antsCardArg() |
|
581 |
{ |
|
582 |
&antsUsageError() unless defined($ARGV[0]); |
|
583 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
584 |
if ($ARGV[0] =~ m{^=}); |
|
585 |
&antsNoCardErr("Argument",$ARGV[0]); |
|
586 |
my($res) = 1.0*$ARGV[0]; |
|
587 |
shift(@ARGV); |
|
588 |
return $res; |
|
589 |
} |
|
590 |
||
591 |
sub antsCardOpt(@) |
|
592 |
{ |
|
593 |
my($opt,$default) = @_; |
|
594 |
if (ref($opt)) { # reference => set |
|
595 |
if (defined(${$opt})) { # defined => check |
|
596 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
597 |
&antsNoCardErr("Option Argument",${$opt}); |
|
598 |
} else { # not defined => default |
|
599 |
${$opt} = $default; |
|
600 |
} |
|
601 |
return ${$opt}; |
|
602 |
} else { # not ref => do not set |
|
603 |
if (defined($opt)) { |
|
604 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
605 |
&antsNoCardErr("Option Argument",$opt); |
|
606 |
return $opt; |
|
607 |
} else { |
|
608 |
return $default; |
|
609 |
} |
|
610 |
} |
|
611 |
} |
|
612 |
||
613 |
sub antsNoIntErr($$) |
|
614 |
{ |
|
615 |
croak("$0: $_[0] $_[1] is not an integer\n") |
|
616 |
unless (integerp($_[1])); |
|
617 |
} |
|
618 |
||
619 |
sub antsIntArg() |
|
620 |
{ |
|
621 |
&antsUsageError() unless defined($ARGV[0]); |
|
622 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
623 |
if ($ARGV[0] =~ m{^=}); |
|
624 |
&antsNoIntErr("Argument",$ARGV[0]); |
|
625 |
my($res) = 1.0*$ARGV[0]; |
|
626 |
shift(@ARGV); |
|
627 |
return $res; |
|
628 |
} |
|
629 |
||
630 |
sub antsIntOpt(@) |
|
631 |
{ |
|
632 |
my($opt,$default) = @_; |
|
633 |
if (ref($opt)) { # reference => set |
|
634 |
if (defined(${$opt})) { # defined => check |
|
635 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
636 |
&antsNoIntErr("Option Argument",${$opt}); |
|
637 |
} else { # not defined => default |
|
638 |
${$opt} = $default; |
|
639 |
} |
|
640 |
return ${$opt}; |
|
641 |
} else { # not ref => do not set |
|
642 |
if (defined($opt)) { |
|
643 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
644 |
&antsNoIntErr("Option Argument",$opt); |
|
645 |
return $opt; |
|
646 |
} else { |
|
647 |
return $default; |
|
648 |
} |
|
649 |
} |
|
650 |
} |
|
651 |
||
652 |
sub antsNoFloatErr($$) |
|
653 |
{ |
|
654 |
croak("$0: $_[0] $_[1] is not a number\n") |
|
655 |
unless (numberp($_[1])); |
|
656 |
} |
|
657 |
||
658 |
sub antsFloatArg() |
|
659 |
{ |
|
660 |
&antsUsageError() unless defined($ARGV[0]); |
|
661 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
662 |
if ($ARGV[0] =~ m{^=}); |
|
663 |
my($res) = str2num($ARGV[0]); |
|
664 |
&antsNoFloatErr("Argument",$res); |
|
665 |
shift(@ARGV); |
|
666 |
return $res; |
|
667 |
} |
|
668 |
||
669 |
sub antsFloatOpt(@) |
|
670 |
{ |
|
671 |
my($opt,$default) = @_; |
|
672 |
if (ref($opt)) { # reference => set |
|
673 |
if (defined(${$opt})) { # defined => check |
|
674 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
675 |
&antsNoFloatErr("Option Argument",${$opt}); |
|
676 |
} else { # not defined => default |
|
677 |
${$opt} = $default; |
|
678 |
} |
|
679 |
return ${$opt}; |
|
680 |
} else { # not ref => do not set |
|
681 |
if (defined($opt)) { |
|
682 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
683 |
&antsNoFloatErr("Option Argument",$opt); |
|
684 |
return $opt; |
|
685 |
} else { |
|
686 |
return $default; |
|
687 |
} |
|
688 |
} |
|
689 |
} |
|
690 |
||
10 | 691 |
#---------------------------------------------------------------------- |
692 |
# antsFunOpt(\$opt_x) parses the contents of $opt_x as follows: |
|
693 |
# |
|
694 |
# name(value) => $opt_x = 'name'; $name = "value"; |
|
695 |
# name => no change |
|
696 |
#---------------------------------------------------------------------- |
|
697 |
||
698 |
sub antsFunOpt(@) |
|
699 |
{ |
|
700 |
my($opt) = @_; |
|
701 |
return unless defined($opt); |
|
702 |
croak("antsusage.pl: antsFunOpt(@_): argument is not a ref\n") |
|
703 |
unless ref($opt); |
|
704 |
my($name,$param) = (${$opt} =~ m{^([^\)]+)\(([^\)]+)\)$}); |
|
705 |
return unless defined($param); |
|
706 |
eval(sprintf('$%s = "%s";',$name,$param)); |
|
707 |
${$opt} = $name; |
|
708 |
} |
|
709 |
||
0 | 710 |
1; # return true |