author | A.M. Thurnherr <athurnherr@yahoo.com> |
Thu, 05 Mar 2015 12:51:48 +0000 | |
changeset 10 | 3dfa16523886 |
parent 4 | ff72b00b4342 |
child 15 | ebd8a4ddd7f2 |
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 |
|
10 | 5 |
# dlm: Sat Jan 31 15:49:23 2015 |
0 | 6 |
# (c) 1998 A.M. Thurnherr |
10 | 7 |
# uE-Info: 652 28 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) |
10 | 158 |
# Jan 30, 2015: - added &antsFunOpt() |
159 |
# Jan 31, 2015: - made it work |
|
0 | 160 |
|
161 |
# NOTES: |
|
162 |
# - ksh expands {}-arguments with commas in them!!! Use + instead |
|
163 |
||
164 |
use Getopt::Std; |
|
165 |
||
166 |
sub antsUsageError() { # die with Usage error |
|
167 |
if (defined($antsSummary)) { |
|
168 |
print(STDERR "\n$0 -- $antsSummary\n\n") |
|
169 |
} else { |
|
170 |
print(STDERR "\n$0\n\n") |
|
171 |
} |
|
172 |
if ($opt_U) { |
|
173 |
print(STDERR "Options & Arguments: $antsCurUsage$_[0]\n\nCommon options:\n" . |
|
174 |
"\t[-F)ields {%P|f|[\$@]|[f]=expr}[,...]]\n" . |
|
175 |
"\t[num for-M)at] [-C)anonical numbers] [-G)eographic lat/lon]\n" . |
|
176 |
"\t[-A)ctivate output] [LaTeX -T)able output]\n" . |
|
177 |
"\t[-S)elect <addr-expr>] [-N)ums f[,...]] [-H)ead <n lines>]\n" . |
|
178 |
"\t[-P)ass comments] [-Q)uiet (no headers)] [-X (no new header)]\n" . |
|
179 |
"\t[suppress -D)ependency checks & addition of new dependencies]\n" . |
|
180 |
"\t[-L)oad <lib,...>]\n" . |
|
181 |
"\t[-I)n field-sep] [-O)ut field-sep] [-R)ecord sep]\n"); |
|
182 |
} else { |
|
183 |
print(STDERR "Options & Arguments: $antsCurUsage$_[0]\n"); |
|
184 |
} |
|
185 |
croak("\n"); |
|
186 |
} |
|
187 |
||
188 |
# NB: "-" as first char in opts string disables common-option processing |
|
189 |
||
190 |
sub antsUsage($$@) { # handle options |
|
191 |
my($opts,$min,@usage) = @_; |
|
192 |
my($cOpts) = 'ADM:QN:XCGPH:UI:O:R:L:F:S:T'; |
|
193 |
my($d,$p); |
|
194 |
$antsCurUsage .= "\n\t[print full -U)sage]"; |
|
195 |
foreach my $uln (@usage) { |
|
196 |
$antsCurUsage .= "\n\t$uln" # suppress emtpy, e.g. |
|
197 |
unless ($uln eq ''); # for interp. model usage |
|
198 |
} |
|
199 |
||
200 |
&antsUsageError() # no args && tty stdin |
|
201 |
if (!$antsInteractive && $min == 0 && @ARGV == 0 && -t 0); |
|
202 |
||
203 |
unshift(@ARGV,'--') if ($ARGV[0] =~ /^-\d/); # -ve number heuristics |
|
204 |
||
205 |
chomp($0 = `basename $0`); # set scriptname |
|
206 |
chop($d = `date +%D`); # build header line |
|
207 |
chop($p = `pwd`); |
|
208 |
$p = "..." . substr($p,-17) if (length($p) > 20); |
|
209 |
$antsCurHeader = "#ANTS# [$d $p] $0"; |
|
210 |
my($i,$eoo); |
|
211 |
for ($i=0; $i<=$#ARGV; $i++) { |
|
212 |
$antsCurHeader .= " '$ARGV[$i]'"; |
|
213 |
$eoo = 1 if ($ARGV[$i] eq '--'); # -- handling |
|
214 |
$ARGV[$i] = "!$ARGV[$i+1]" if ($eoo); # make -ve non-options |
|
215 |
} |
|
216 |
$antsCurHeader .= "\n"; |
|
217 |
pop(@ARGV) if ($eoo); # remove last ARG |
|
218 |
||
219 |
if ($opts =~ m{^-}) { # no common options processing |
|
220 |
$opts = $'; |
|
221 |
undef($cOpts); |
|
222 |
} |
|
223 |
&antsUsageError(), exit(1) # parse options |
|
224 |
unless (&getopts($cOpts . $opts)); |
|
225 |
||
226 |
unless ($antsParseHeader) { |
|
227 |
croak("$0: -S not supported (implementation restriction)\n") |
|
228 |
if defined($opt_S); |
|
229 |
croak("$0: -N not supported (implementation restriction)\n") |
|
230 |
if defined($opt_N); |
|
231 |
} |
|
232 |
||
233 |
if ($eoo) { # reset args |
|
234 |
for ($i=0; $i<=$#ARGV; $i++) { |
|
235 |
$ARGV[$i] = substr($ARGV[$i],1); |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
if (defined($cOpts)) { # process common options |
|
240 |
||
241 |
croak("$0: illegal option combination (-P & -Q)\n") |
|
242 |
if ($opt_P && $opt_Q); |
|
243 |
||
244 |
&antsActivateOut() if ($opt_A); # activate output |
|
245 |
||
246 |
if ($opt_T) { # LaTeX table output |
|
247 |
croak("$0: illegal option combination (-T & -G)\n") |
|
248 |
if ($opt_G); |
|
249 |
croak("$0: illegal option combination (-T & -O)\n") |
|
250 |
if defined($opt_O); |
|
251 |
$opt_O = ' & '; |
|
252 |
croak("$0: illegal option combination (-T & -R)\n") |
|
253 |
if defined($opt_R); |
|
254 |
$opt_R = ' \\\\\\\\\n'; |
|
255 |
} |
|
256 |
||
257 |
if (defined($opt_I)) { # defaults |
|
258 |
eval('$opt_I = "' . $opt_I .'";'); # interpret strings |
|
259 |
} else { # ... as perl strings |
|
260 |
$opt_I = '\s+'; |
|
261 |
} |
|
262 |
if (defined($opt_O)) { |
|
263 |
eval('$opt_O = "' . $opt_O .'";'); |
|
264 |
} else { |
|
265 |
$opt_O = "\t"; |
|
266 |
} |
|
267 |
if (defined($opt_R)) { |
|
268 |
eval('$opt_R = "' . $opt_R .'";'); |
|
269 |
} else { |
|
270 |
$opt_R = "\n"; |
|
271 |
} |
|
272 |
||
273 |
if (defined($opt_L)) { # load libraries |
|
274 |
foreach $lib (split(',',$opt_L)) { |
|
275 |
if (-r "lib$lib.pl") { |
|
276 |
require "lib$lib.pl"; |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
277 |
} elsif (-r "$ANTS/lib$lib.pl") { |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
278 |
require "$ANTS/lib$lib.pl"; |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
279 |
} elsif (-r "$ANTSLIBS/lib$lib.pl") { |
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
280 |
require "$ANTSLIBS/lib$lib.pl"; |
0 | 281 |
} else { |
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
282 |
croak("$0: cannot load {.,$ANTS,$ANTSLIBS}/lib$lib.pl\n"); |
0 | 283 |
} |
284 |
} |
|
285 |
} |
|
286 |
||
287 |
if (defined($opt_N)) { # parse -N)ums |
|
288 |
@antsNFNames = split(',',$opt_N); |
|
289 |
} |
|
290 |
||
291 |
if (defined($opt_F)) { # parse -F)ields |
|
292 |
$opt_F =~ s/\\,/aNtScOmMa/g; |
|
293 |
@antsOutExprs = split(',',$opt_F); |
|
294 |
push(@antsOutExprs,'$@') if ($opt_F =~ /,$/); |
|
295 |
foreach my $e (@antsOutExprs) { |
|
296 |
$e =~ s/aNtScOmMa/,/; |
|
297 |
} |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
my($ai); |
|
302 |
for ($ai=0; $ai<=$#ARGV; $ai++) { # parse special args |
|
303 |
my(@exp); |
|
304 |
if ($ARGV[$ai] =~ /^@([^:]+):(.+)/) { # @file:field |
|
305 |
&antsAddDeps($1); |
|
306 |
@exp = `Cat -QF$2 $1`; |
|
307 |
croak("(...while expanding $ARGV[$ai])\n") if ($?); |
|
308 |
} elsif ($ARGV[$ai] =~ /^#(-?[\d\.]+)-(-?[\d\.]+):?(-?[\d\.]+)?/) { |
|
309 |
my($step) = 1; # #num-num:step |
|
310 |
if (defined($3)) { |
|
311 |
$step = $3; |
|
312 |
} elsif ($2 < $1) { |
|
313 |
$step = -1; |
|
314 |
} |
|
315 |
if ($step > 0) { |
|
316 |
for (my($c)=0,my($i)=$1; $i<=$2+$step/1e6; $c++,$i=$1+$c*$step) { |
|
317 |
$i = 0 if (abs($i) < abs($step) / 1e6); |
|
318 |
push(@exp,$i); |
|
319 |
} |
|
320 |
} else { |
|
321 |
for (my($c)=0,my($i)=$1; $i>=$2+$step/1e6; $c++,$i=$1+$c*$step) { |
|
322 |
$i = 0 if (abs($i) < abs($step) / 1e6); |
|
323 |
push(@exp,$i); |
|
324 |
} |
|
325 |
} |
|
326 |
# } elsif ($ARGV[$ai] =~ m{\{([^\}]+)\}}) { # pref{list of ranges}suff |
|
327 |
} elsif ($ARGV[$ai] =~ m{\{([-\+,\d]+)\}}) { # pref{list of ranges}suff |
|
328 |
my($pref) = $`; my($suff) = $'; |
|
329 |
foreach my $range (split('[,\+]',$1)) { |
|
330 |
if ($range =~ /^(\d+)-(\d+)$/) { |
|
331 |
my($fmt) = length($1)==length($2) ? |
|
332 |
sprintf("$pref%%0%dd$suff",length($1)) : "$pref%d$suff"; |
|
333 |
if ($2 > $1) { |
|
334 |
for (my($i)=$1; $i<=$2; $i++) { |
|
335 |
my($f) = sprintf($fmt,$i); |
|
336 |
if (-f $f) { push(@exp,$f); } |
|
3 | 337 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 338 |
} |
339 |
} else { |
|
340 |
for (my($i)=$1; $i>=$2; $i--) { |
|
341 |
my($f) = sprintf($fmt,$i); |
|
342 |
if (-f $f) { push(@exp,$f); } |
|
3 | 343 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 344 |
} |
345 |
} |
|
346 |
} else { |
|
347 |
my($f) = "$pref$range$suff"; |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
348 |
# print(STDERR "f = $pref . $range . $suff\n"); |
0 | 349 |
if (-f $f) { push(@exp,$f); } |
3 | 350 |
# else { &antsInfo("$ARGV[$ai]: no file <$f>"); } |
0 | 351 |
} |
352 |
} |
|
4
ff72b00b4342
after adding $pi and some other stuff
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
353 |
@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
|
354 |
unless (@exp); |
0 | 355 |
} else { # regular argument |
356 |
next; |
|
357 |
} |
|
358 |
&antsInfo("WARNING: special arg $ARGV[$ai] expands to nothing"), |
|
359 |
push(@exp,$ARGV[$ai]) |
|
360 |
unless ($#exp >= 0); |
|
361 |
splice(@ARGV,$ai,1,@exp); |
|
362 |
} |
|
363 |
||
364 |
my($nargs) = $#ARGV + 1; # check arg count |
|
365 |
&antsUsageError() if ($opt_U || ($min > 0) && ($nargs < $min)); |
|
366 |
||
367 |
$antsARGV0 = $ARGV[0]; # save 1st filename |
|
368 |
&antsParseHeader(); # get fields & params |
|
369 |
||
370 |
for (my($i)=0; $i<=$#ARGV; $i++) { # remove leading ! from args |
|
371 |
$ARGV[$i] =~ s/^!//; |
|
372 |
} |
|
373 |
||
374 |
return $nargs; |
|
375 |
} |
|
376 |
||
377 |
#====================================================================== |
|
378 |
# argument typechecking |
|
379 |
#====================================================================== |
|
380 |
||
381 |
sub antsFieldInFileArg($) |
|
382 |
{ |
|
383 |
my($fn) = @_; |
|
384 |
my($fnr); |
|
385 |
||
386 |
&antsUsageError() unless defined($ARGV[0]); |
|
387 |
open(F,$fn) || croak("$fn: $!\n"); |
|
388 |
if ($ARGV[0] =~ /^%/) { |
|
389 |
croak("$0: no PARAM $ARGV[0] in $fn\n") |
|
390 |
unless (defined(&antsFileScanParam(F,$'))); |
|
391 |
$fnr = $ARGV[0]; |
|
392 |
} else { |
|
393 |
$fnr = &antsFileScanFnr(F,$ARGV[0]); |
|
394 |
unless (defined($fnr)) { |
|
395 |
print(STDERR "$0: WARNING: no field $ARGV[0] in $fn\n"); |
|
396 |
$fnr = &fnr($ARGV[0]); |
|
397 |
} |
|
398 |
} |
|
399 |
close(F); |
|
400 |
||
401 |
shift(@ARGV); |
|
402 |
return $fnr; |
|
403 |
} |
|
404 |
||
405 |
sub antsFieldArg($) |
|
406 |
{ |
|
407 |
&antsUsageError() unless defined($ARGV[0]); |
|
408 |
my($paramsAllowed) = @_; |
|
409 |
my($fnr) = &fnr($ARGV[0]); |
|
410 |
croak("$0: $ARGV[0] is not a field\n") |
|
411 |
unless (numberp($fnr) || $paramsAllowed); |
|
412 |
shift(@ARGV); |
|
413 |
return $fnr; |
|
414 |
} |
|
415 |
||
416 |
sub antsFieldOpt(@) |
|
417 |
{ |
|
418 |
my($opt,$default) = @_; |
|
419 |
if (ref($opt)) { # reference => set |
|
420 |
if (defined(${$opt})) { # defined => check,set |
|
421 |
${$opt} = &fnr(${$opt}); |
|
422 |
} elsif (defined($default)) { # not defined => default |
|
423 |
${$opt} = &fnr($default); |
|
424 |
} |
|
425 |
return ${$opt}; |
|
426 |
} else { # not ref => do not set |
|
427 |
return defined($opt) ? &fnr($opt) : |
|
428 |
defined($default) ? &fnr($default) : $opt; |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
sub antsFieldListOpt($) |
|
433 |
{ |
|
434 |
my($opt) = @_; |
|
435 |
my(@fn) = split(',',$opt); |
|
436 |
my(@fi); |
|
437 |
||
438 |
for (my($i)=0; $i<@fn; $i++) { |
|
439 |
$fi[$i] = &fnr($fn[$i]); |
|
440 |
} |
|
441 |
return @fi; |
|
442 |
} |
|
443 |
||
444 |
sub antsNewField($) # allocate if needed |
|
445 |
{ |
|
446 |
my($fname) = @_; |
|
447 |
my($fnr); |
|
448 |
||
449 |
$fnr = &fnrNoErr($fname,1); # exact match |
|
450 |
unless (defined($fnr)) { |
|
451 |
return $antsBufNFields++ # external layout |
|
452 |
unless ($antsBufNFields==0 || @antsLayout); |
|
453 |
@antsNewLayout = @antsLayout |
|
454 |
unless (@antsNewLayout); |
|
455 |
push(@antsNewLayout,$fname); |
|
456 |
$fnr = $#antsNewLayout; |
|
457 |
} |
|
458 |
$antsBufNFields = $fnr+1 if ($fnr >= $antsBufNFields); |
|
459 |
return $fnr; |
|
460 |
} |
|
461 |
||
462 |
sub antsNewFieldOpt(@) # allocate if does not exist |
|
463 |
{ |
|
464 |
my($opt,$default) = @_; |
|
465 |
my($fname,$fnr); |
|
466 |
||
467 |
if (ref($opt)) { # reference => set |
|
468 |
if (defined(${$opt})) { # defined => check,set |
|
469 |
$fname = ${$opt}; |
|
470 |
} elsif (defined($default)) { # not defined => default |
|
471 |
$fname = $default; |
|
472 |
} |
|
473 |
if (defined($fname)) { |
|
474 |
$fnr = &antsNewField($fname); |
|
475 |
${$opt} = $fnr; |
|
476 |
return $fnr; |
|
477 |
} else { return undef; } |
|
478 |
} else { # not ref => do not set |
|
479 |
if (defined($opt)) { |
|
480 |
$fname = $opt; |
|
481 |
} elsif (defined($default)) { |
|
482 |
$fname = $default; |
|
483 |
} |
|
484 |
return defined($fname) ? &antsNewField($fname) : undef; |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
sub antsNoFileErr($$) |
|
489 |
{ |
|
490 |
croak("$0: $_[0] $_[1] is not a valid file\n") |
|
491 |
unless (-r $_[1]); |
|
492 |
&antsAddDeps($_[1]); |
|
493 |
} |
|
494 |
||
495 |
sub antsFileArg() # arg 1 => do not shift |
|
496 |
{ |
|
497 |
&antsUsageError() unless defined($ARGV[0]); |
|
498 |
&antsNoFileErr("Argument",$ARGV[0]); |
|
499 |
my($res) = $ARGV[0]; |
|
500 |
shift(@ARGV) unless ($_[0]); |
|
501 |
return $res; |
|
502 |
} |
|
503 |
||
504 |
sub antsFileOpt($) |
|
505 |
{ |
|
506 |
my($opt) = @_; |
|
507 |
&antsNoFileErr("Option Argument",$opt) |
|
508 |
if (defined($opt)); |
|
509 |
} |
|
510 |
||
511 |
sub antsParamArg() |
|
512 |
{ |
|
513 |
&antsUsageError() unless defined($ARGV[0]); |
|
514 |
croak("$0: Argument $ARGV[0] is not a valid PARAM\n") |
|
515 |
unless ($ARGV[0] =~ /^%/); |
|
516 |
shift(@ARGV); |
|
517 |
return $'; |
|
518 |
} |
|
519 |
||
520 |
sub antsNoCardErr($$) |
|
521 |
{ |
|
522 |
croak("$0: $_[0] $_[1] is not a cardinal number\n") |
|
523 |
unless (cardinalp($_[1])); |
|
524 |
} |
|
525 |
||
526 |
sub antsCardArg() |
|
527 |
{ |
|
528 |
&antsUsageError() unless defined($ARGV[0]); |
|
529 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
530 |
if ($ARGV[0] =~ m{^=}); |
|
531 |
&antsNoCardErr("Argument",$ARGV[0]); |
|
532 |
my($res) = 1.0*$ARGV[0]; |
|
533 |
shift(@ARGV); |
|
534 |
return $res; |
|
535 |
} |
|
536 |
||
537 |
sub antsCardOpt(@) |
|
538 |
{ |
|
539 |
my($opt,$default) = @_; |
|
540 |
if (ref($opt)) { # reference => set |
|
541 |
if (defined(${$opt})) { # defined => check |
|
542 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
543 |
&antsNoCardErr("Option Argument",${$opt}); |
|
544 |
} else { # not defined => default |
|
545 |
${$opt} = $default; |
|
546 |
} |
|
547 |
return ${$opt}; |
|
548 |
} else { # not ref => do not set |
|
549 |
if (defined($opt)) { |
|
550 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
551 |
&antsNoCardErr("Option Argument",$opt); |
|
552 |
return $opt; |
|
553 |
} else { |
|
554 |
return $default; |
|
555 |
} |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
sub antsNoIntErr($$) |
|
560 |
{ |
|
561 |
croak("$0: $_[0] $_[1] is not an integer\n") |
|
562 |
unless (integerp($_[1])); |
|
563 |
} |
|
564 |
||
565 |
sub antsIntArg() |
|
566 |
{ |
|
567 |
&antsUsageError() unless defined($ARGV[0]); |
|
568 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
569 |
if ($ARGV[0] =~ m{^=}); |
|
570 |
&antsNoIntErr("Argument",$ARGV[0]); |
|
571 |
my($res) = 1.0*$ARGV[0]; |
|
572 |
shift(@ARGV); |
|
573 |
return $res; |
|
574 |
} |
|
575 |
||
576 |
sub antsIntOpt(@) |
|
577 |
{ |
|
578 |
my($opt,$default) = @_; |
|
579 |
if (ref($opt)) { # reference => set |
|
580 |
if (defined(${$opt})) { # defined => check |
|
581 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
582 |
&antsNoIntErr("Option Argument",${$opt}); |
|
583 |
} else { # not defined => default |
|
584 |
${$opt} = $default; |
|
585 |
} |
|
586 |
return ${$opt}; |
|
587 |
} else { # not ref => do not set |
|
588 |
if (defined($opt)) { |
|
589 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
590 |
&antsNoIntErr("Option Argument",$opt); |
|
591 |
return $opt; |
|
592 |
} else { |
|
593 |
return $default; |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
sub antsNoFloatErr($$) |
|
599 |
{ |
|
600 |
croak("$0: $_[0] $_[1] is not a number\n") |
|
601 |
unless (numberp($_[1])); |
|
602 |
} |
|
603 |
||
604 |
sub antsFloatArg() |
|
605 |
{ |
|
606 |
&antsUsageError() unless defined($ARGV[0]); |
|
607 |
$ARGV[0] = &{&antsCompileConstExpr($')} |
|
608 |
if ($ARGV[0] =~ m{^=}); |
|
609 |
my($res) = str2num($ARGV[0]); |
|
610 |
&antsNoFloatErr("Argument",$res); |
|
611 |
shift(@ARGV); |
|
612 |
return $res; |
|
613 |
} |
|
614 |
||
615 |
sub antsFloatOpt(@) |
|
616 |
{ |
|
617 |
my($opt,$default) = @_; |
|
618 |
if (ref($opt)) { # reference => set |
|
619 |
if (defined(${$opt})) { # defined => check |
|
620 |
$$opt = &{&antsCompileConstExpr($')} if ($$opt =~ m{^=}); |
|
621 |
&antsNoFloatErr("Option Argument",${$opt}); |
|
622 |
} else { # not defined => default |
|
623 |
${$opt} = $default; |
|
624 |
} |
|
625 |
return ${$opt}; |
|
626 |
} else { # not ref => do not set |
|
627 |
if (defined($opt)) { |
|
628 |
$opt = &{&antsCompileConstExpr($')} if ($opt =~ m{^=}); |
|
629 |
&antsNoFloatErr("Option Argument",$opt); |
|
630 |
return $opt; |
|
631 |
} else { |
|
632 |
return $default; |
|
633 |
} |
|
634 |
} |
|
635 |
} |
|
636 |
||
10 | 637 |
#---------------------------------------------------------------------- |
638 |
# antsFunOpt(\$opt_x) parses the contents of $opt_x as follows: |
|
639 |
# |
|
640 |
# name(value) => $opt_x = 'name'; $name = "value"; |
|
641 |
# name => no change |
|
642 |
#---------------------------------------------------------------------- |
|
643 |
||
644 |
sub antsFunOpt(@) |
|
645 |
{ |
|
646 |
my($opt) = @_; |
|
647 |
return unless defined($opt); |
|
648 |
croak("antsusage.pl: antsFunOpt(@_): argument is not a ref\n") |
|
649 |
unless ref($opt); |
|
650 |
my($name,$param) = (${$opt} =~ m{^([^\)]+)\(([^\)]+)\)$}); |
|
651 |
return unless defined($param); |
|
652 |
eval(sprintf('$%s = "%s";',$name,$param)); |
|
653 |
${$opt} = $name; |
|
654 |
} |
|
655 |
||
0 | 656 |
1; # return true |