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
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]);
146 $dd->Quotekeys($] < 5.005 ? 1 : 0);
147 $value = $dd->Dumpxs;
148 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
149 $value =~ s/^\s*\[//;
150 $value =~ s/\]\s*$//;
155 # format a prompt with its current value and return it with its privilege
158 my ($line, $value) = @_;
159 my ($priv, $prompt, $action) = split ',', $line;
161 # if there is an action treat it as a subroutine and replace $value
163 my $q = qq{\$value = $action(\$value)};
165 } elsif (ref $value) {
168 $prompt = sprintf "%15s: %s", $prompt, $value;
169 return ($priv, $prompt);
172 # turn a hex field into printed hex
176 return sprintf '%X', $val;
179 # take an arg as a hash of call=>time pairs and print it
184 for (sort keys %$ref) {
185 $out .= "$_=$ref->{$_}, ";
192 # take an arg as an array list and print it
196 return ref $ref ? join(', ', @{$ref}) : $ref;
199 # take the arg as an array reference and print as a list of pairs
206 for ($i = 0; $i < @$ref; $i += 2) {
208 my $r2 = @$ref[$i+1];
211 chop $out; # remove last space
212 chop $out; # remove last comma
216 # take the arg as a hash reference and print it out as such
222 while (my ($k,$v) = each %$ref) {
223 $out .= "${k}=>$v, ";
225 chop $out; # remove last space
226 chop $out; # remove last comma
233 my @a = split /,/, $ref->field_prompt(shift);
234 my @b = split /,/, $ref->field_prompt(shift);
235 return lc $a[1] cmp lc $b[1];
238 # print all the fields for a record according to privilege
240 # The prompt record is of the format '<priv>,<prompt>[,<action>'
241 # and is expanded by promptf above
245 my $self = shift; # is a dxchan
246 my $ref = shift; # is a thingy with field_prompt and fields methods defined
248 my @fields = $ref->fields;
250 my $width = $self->width - 1;
253 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
254 if (defined $ref->{$field}) {
255 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
257 if (length $ans > $width) {
258 my ($p, $a) = split /: /, $ans, 2;
259 my $l = (length $p) + 2;
260 my $al = ($width - 1) - $l;
262 while (length $a > $al ) {
263 ($bit, $a) = unpack "A$al A*", $a;
264 push @tmp, "$p: $bit";
267 push @tmp, "$p: $a" if length $a;
271 push @out, @tmp if ($self->priv >= $priv);
277 # generate a regex from a shell type expression
278 # see 'perl cookbook' 6.9
282 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
283 return '^' . $in . "\$";
286 # read in a file into a string and return it.
287 # the filename can be split into a dir and file and the
288 # file can be in upper or lower case.
289 # there can also be a suffix
292 my ($dir, $file, $suffix) = @_;
297 $fn = "$dir/$f.$suffix";
300 $fn = "$dir/$file.$suffix";
313 my $fh = new IO::File $fn;
323 # write out a file in the format required for reading
324 # in via readfilestr, it expects the same arguments
325 # and a reference to an object
335 confess('no object to write in writefilestr') unless $obj;
336 confess('object not a reference in writefilestr') unless ref $obj;
340 $fn = "$dir/$f.$suffix";
343 $fn = "$dir/$file.$suffix";
356 my $fh = new IO::File ">$fn";
358 my $dd = new Data::Dumper([ $obj ]);
362 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
363 $fh->print($dd->Dumpxs);
370 copy(@_) or return $!;
373 # remove leading and trailing spaces from an input string
382 # check that a field only has callsign characters in it
386 (?:\d?[A-Z]{1,2}\d*/)? # out of area prefix /
387 (?:\d?[A-Z]{1,2}\d+) # main prefix one (required)
388 [A-Z]{1,5} # callsign letters (required)
389 (?:-(?:\d{1,2}|\#))? # - nn possibly (eg G8BPQ-8) or -# (an RBN spot)
390 (?:/[0-9A-Z]{1,7})? # / another prefix, callsign or special label (including /MM, /P as well as /EURO or /LGT) possibly
393 # longest callign allowed is 1X11/1Y11XXXXX-11/XXXXXXX
398 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}}\d+)!x # basic prefix
402 # check that a PC protocol field is valid text
405 return undef unless length $_[0];
406 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
410 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
413 return $_[0] =~ /^[01\*\-]+$/;
416 # check that a thing is a frequency
419 return $_[0] =~ /^\d+(?:\.\d+)?$/;
422 # check that a thing is just digits
425 return $_[0] =~ /^[\d]+$/;
428 # does it look like a qra locator?
431 return unless length $_[0] == 4 || length $_[0] == 6;
432 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d(?:[A-Xa-x][A-Xa-x])?$/;
435 # does it look like a valid lat/long
438 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
441 # is it an ip address?
444 return $_[0] =~ /^\d+\.\d+\.\d+\.\d+$/ || $_[0] =~ /^[0-9a-f:,]+$/;
447 # insert an item into a list if it isn't already there returns 1 if there 0 if not
453 return 1 if grep {$_ eq $item } @$list;
458 # delete an item from a list if it is there returns no deleted
465 @$list = grep {$_ ne $item } @$list;
469 # find the correct local_data directory
470 # basically, if there is a local_data directory with this filename and it is younger than the
471 # equivalent one in the (system) data directory then return that name rather than the system one
475 my $ofn = "$main::data/$ifn";
478 if (-e "$main::local_data") {
479 $tfn = "$main::local_data/$ifn";
480 if (-e $tfn && -e $ofn) {
481 $ofn = $tfn if -M $tfn < -M $ofn;
490 # move a file or a directory from data -> local_data if isn't there already
494 if (-e "$main::data/$ifn" ) {
495 unless (-e "$main::local_data/$ifn") {
496 move("$main::data/$ifn", "$main::local_data/$ifn") or die "localdata_mv: cannot move $ifn from '$main::data' -> '$main::local_data' $!\n";
501 # measure the time taken for something to happen; use Time::HiRes qw(gettimeofday tv_interval);
508 my $tb = shift || [gettimeofday];
510 my $a = int($ta->[0] * 1000) + int($ta->[1] / 1000);
511 my $b = int($tb->[0] * 1000) + int($tb->[1] / 1000);
515 my $s = "subprocess stats cmd: '$line' $call ${msecs}mS";
516 $s .= " $no lines" if $no;