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