2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
16 use Time::HiRes qw(gettimeofday tv_interval);
20 use vars qw(@month %patmap $pi $d2r $r2d @ISA @EXPORT);
24 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
25 parray parraypairs phex phash shellregex readfilestr writefilestr
27 print_all_fields cltounix unpad is_callsign is_latlong
28 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
29 is_prefix dd is_ipaddr $pi $d2r $r2d localdata localdata_mv
30 diffms _diffms difft parraydifft
34 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
47 # a full time for logging and other purposes
51 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
53 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
57 # get a zulu time in cluster format (2300Z)
61 $t = defined $t ? $t : time;
63 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
64 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
68 # get a cluster format date (23-Jun-1998)
72 $t = defined $t ? $t : time;
74 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
76 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
80 # return a cluster style date time
85 my $date = cldate($t, $dst);
86 my $time = ztime($t, $dst);
90 # return a unix date from a cluster date and time
95 my ($thisyear) = (gmtime)[5] + 1900;
97 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
98 return 0 if $3 > 2036;
99 return 0 unless abs($thisyear-$3) <= 1;
101 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
102 $time = "$1:$2 +0000";
103 my $r = str2time("$date $time");
105 return $r == -1 ? undef : $r;
108 # turn a latitude in degrees into a string
112 my ($deg, $min, $let);
113 $let = $n >= 0 ? 'N' : 'S';
116 $min = int ((($n - $deg) * 60) + 0.5);
117 return "$deg $min $let";
120 # turn a longitude in degrees into a string
124 my ($deg, $min, $let);
125 $let = $n >= 0 ? 'E' : 'W';
128 $min = int ((($n - $deg) * 60) + 0.5);
129 return "$deg $min $let";
132 # turn a true into 'yes' and false into 'no'
136 return $n ? $main::yes : $main::no;
139 # provide a data dumpered version of the object passed
143 my $dd = new Data::Dumper([$value]);
147 $dd->Quotekeys($] < 5.005 ? 1 : 0);
148 $value = $dd->Dumpxs;
149 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
150 $value =~ s/^\s*\[//;
151 $value =~ s/\]\s*$//;
156 # format a prompt with its current value and return it with its privilege
159 my ($line, $value) = @_;
160 my ($priv, $prompt, $action) = split ',', $line;
162 # if there is an action treat it as a subroutine and replace $value
164 my $q = qq{\$value = $action(\$value)};
166 } elsif (ref $value) {
169 $prompt = sprintf "%15s: %s", $prompt, $value;
170 return ($priv, $prompt);
173 # turn a hex field into printed hex
177 return sprintf '%X', $val;
180 # take an arg as a hash of call=>time pairs and print it
185 for (sort keys %$ref) {
186 $out .= "$_=" . atime($ref->{$_}) . ", ";
193 # take an arg as an array list and print it
197 return ref $ref ? join(', ', @{$ref}) : $ref;
200 # take the arg as an array reference and print as a list of pairs
207 for ($i = 0; $i < @$ref; $i += 2) {
209 my $r2 = @$ref[$i+1];
212 chop $out; # remove last space
213 chop $out; # remove last comma
217 # take the arg as a hash reference and print it out as such
223 while (my ($k,$v) = each %$ref) {
224 $out .= "${k}=>$v, ";
226 chop $out; # remove last space
227 chop $out; # remove last comma
234 my @a = split /,/, $ref->field_prompt(shift);
235 my @b = split /,/, $ref->field_prompt(shift);
236 return lc $a[1] cmp lc $b[1];
239 # print all the fields for a record according to privilege
241 # The prompt record is of the format '<priv>,<prompt>[,<action>'
242 # and is expanded by promptf above
246 my $self = shift; # is a dxchan
247 my $ref = shift; # is a thingy with field_prompt and fields methods defined
249 my @fields = $ref->fields;
251 my $width = $self->width - 1;
254 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
255 if (defined $ref->{$field}) {
256 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
258 if (length $ans > $width) {
259 my ($p, $a) = split /: /, $ans, 2;
260 my $l = (length $p) + 2;
261 my $al = ($width - 1) - $l;
263 while (length $a > $al ) {
264 ($bit, $a) = unpack "A$al A*", $a;
265 push @tmp, "$p: $bit";
268 push @tmp, "$p: $a" if length $a;
272 push @out, @tmp if ($self->priv >= $priv);
278 # generate a regex from a shell type expression
279 # see 'perl cookbook' 6.9
283 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
284 return '^' . $in . "\$";
287 # read in a file into a string and return it.
288 # the filename can be split into a dir and file and the
289 # file can be in upper or lower case.
290 # there can also be a suffix
293 my ($dir, $file, $suffix) = @_;
298 $fn = "$dir/$f.$suffix";
301 $fn = "$dir/$file.$suffix";
314 my $fh = new IO::File $fn;
324 # write out a file in the format required for reading
325 # in via readfilestr, it expects the same arguments
326 # and a reference to an object
336 confess('no object to write in writefilestr') unless $obj;
337 confess('object not a reference in writefilestr') unless ref $obj;
341 $fn = "$dir/$f.$suffix";
344 $fn = "$dir/$file.$suffix";
357 my $fh = new IO::File ">$fn";
359 my $dd = new Data::Dumper([ $obj ]);
363 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
364 $fh->print($dd->Dumpxs);
371 copy(@_) or return $!;
374 # remove leading and trailing spaces from an input string
383 # check that a field only has callsign characters in it
387 (?:\d?[A-Z]{1,2}\d*/)? # out of area prefix /
388 (?:\d?[A-Z]{1,2}\d+) # main prefix one (required)
389 [A-Z]{1,5} # callsign letters (required)
390 (?:-(?:\d{1,2}|\#))? # - nn possibly (eg G8BPQ-8) or -# (an RBN spot)
391 (?:/[0-9A-Z]{1,7})? # / another prefix, callsign or special label (including /MM, /P as well as /EURO or /LGT) possibly
394 # longest callign allowed is 1X11/1Y11XXXXX-11/XXXXXXX
399 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}}\d+)!x # basic prefix
403 # check that a PC protocol field is valid text
406 return undef unless length $_[0];
407 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
411 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
414 return $_[0] =~ /^[01\*\-]+$/;
417 # check that a thing is a frequency
420 return $_[0] =~ /^\d+(?:\.\d+)?$/;
423 # check that a thing is just digits
426 return $_[0] =~ /^[\d]+$/;
429 # does it look like a qra locator?
432 return unless length $_[0] == 4 || length $_[0] == 6;
433 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d(?:[A-Xa-x][A-Xa-x])?$/;
436 # does it look like a valid lat/long
439 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
442 # is it an ip address?
445 return $_[0] =~ /^\d+\.\d+\.\d+\.\d+$/ || $_[0] =~ /^[0-9a-f:,]+$/;
448 # insert an item into a list if it isn't already there returns 1 if there 0 if not
454 return 1 if grep {$_ eq $item } @$list;
459 # delete an item from a list if it is there returns no deleted
466 @$list = grep {$_ ne $item } @$list;
470 # find the correct local_data directory
471 # basically, if there is a local_data directory with this filename and it is younger than the
472 # equivalent one in the (system) data directory then return that name rather than the system one
476 my $ofn = "$main::local_data/$ifn";
479 if (-e "$main::local_data") {
480 $tfn = "$main::data/$ifn";
481 if ((-e $tfn) && (-e $ofn)) {
482 $ofn = $tfn if -M $ofn < -M $tfn;
484 $ofn = $tfn if -e $tfn;
491 # move a file or a directory from data -> local_data if isn't there already
495 if (-e "$main::data/$ifn" ) {
496 unless (-e "$main::local_data/$ifn") {
497 move("$main::data/$ifn", "$main::local_data/$ifn") or die "localdata_mv: cannot move $ifn from '$main::data' -> '$main::local_data' $!\n";
502 # measure the time taken for something to happen; use Time::HiRes qw(gettimeofday tv_interval);
506 my $tb = shift || [gettimeofday];
507 my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000);
508 my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
520 my $msecs = _diffms($ta, $tb);
523 my $s = "$pkg subprocess stats cmd: '$line' $call ${msecs}mS";
524 $s .= " $no lines" if $no;
528 # expects either an array reference or two times (in the correct order [start, end])
533 if (ref $b eq 'ARRAY') {
534 $t = $b->[1] - $b->[0];
538 return '-(ve)' if $t < 0;
542 $out .= "${d}d" if $d;
545 $out .= "${h}h" if $h || $d;
548 $out .= "${m}m" if $m || $h || $d;
554 # print an array ref of difft refs
560 my $s = $_->[2] ? "($_->[2])" : '';
561 $out .= sprintf "%s=%s$s, ", atime($_->[0]), difft($_->[0], $_->[1]);