-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.perl
executable file
·2387 lines (2055 loc) · 75.9 KB
/
common.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# *-*-perl-*-*
eval 'exec perl -Ssw $0 "$@"'
if 0;
# common.perl: Commonly used perl routines (variables & constants)
#
# global variables:
# $script_dir directory where the main script resides
# $script_name file name for main script without directory path
# $debug_level debugging trace level (higher is more verbose)
# $disable_commands disables the execution of external commands
# $TRUE, $FALSE boolean constants (old version)
# &TRUE, &FALSE boolean constants (preferred due to Perl warnings)
# $MAXINT constant for largest integer in a word
#
# Developed 1997-1999
# Tom O'Hara
# New Mexico State University
#
# Revised 1999-2001
# Tom O'Hara
# Cycorp Inc.
#
# Revised 2002-2003
# Tom O'Hara
# New Mexico State University
#
# TODO3:
# - Use TL_MOST_VERBOSE for &TL_VERBOSE+4; likewise for similar constructs.
#
# TODO:
# - * Add gotcha's for modification (e.g., consider Python port instead)!
# - Make changes to take advantage of Perl 6???.
# - Have common.pm be a wrapper around this.
# - Add additional common arguments like -verbose (e.g., -para).
# - Change the assumed baseline Perl version from Perl 4.031 to Perl 5.
# - Make sure that global variables such as path_delim don't conflict with
# user variables.
# - Use debug_print unless formatted I/O needed.
# - Use my instead of local unless old-style references assumed.
# - Revise the function synopsis.
# - Put extraneous stuff in extra.perl.
# - Track down other scripts that are redundantly initializing $TEMP, etc.
# - Specify 'use strict' and 'use diagnostics'.
# - Weed out init_var's usages here and elsewhere with default of &TRUE;
# rework via separate init_var for opposite and one with negative of that
# (eg., '&init_var(*do_it, &TRUE);' => '&init_var(*skip_it, &TRUE); &init_var(*do_it, ! $skip_it); ').
# - Try to weed out remaining local's (e.g., in init_var and lock)
# - Work around stupid problem using &DEBUGGING when 'use English' in effect (Undefined subroutine &main::).
# - Use built-in variable for $path_delim (path delimiter).
#
# Portions Copyright (c) 1997-1999, 2002-2003 Tom O'Hara
# Portions Copyright (c) 1999-2001 Cycorp, Inc. All rights reserved.
#
#--------------------------------------------------------------------------------
# Notes:
# - Warning: Dynamic use calls should be invoked via eval (see set_strict_mode).
# - If not familiar with Perl, use ChatGPT-like tool to explain the code.
#
#------------------------------------------------------------------------
# Function synopsis:
# append_entry(array, key, text): Append to the value of an associative array by the given text.
# asctime(): return the time formatted as with the asctime library function
# assert(expression) Issues an error message if the expression evaluates to 0.
# basename(filename, extension): returns the filename w/o the extension.
# blocking_stdin(): determine whether input from STDIN would block
# capitalize(word): returns the word (or text) capitalized.
# cleanup_common(): module termination cleanup routine
# cmd(command_line): same as issue_command(command_line)
# copy_file(source_file, destination_file) Copies the source file to the destination. (OS-independent)
# debug_out(trace_level, format_string, argument, ...) Prints a formatted trace message to STDERR when the current debugging level is at or above the specified level.
# difference(list1, list2) Returns the difference of the two lists (passed as references).
# dirname(file): returns the directory for the file
# dump_line([line], [debug_level]): display line in trace (w/ line number)
# error_out(format_string, argument, ...) Prints a formatted error message to STDERR
# filter_warnings(warning_text): Filter benign warnings.
# find(array_ref, item): returns 0-based position of item in the array
# get_entry(array, key, [default=0]) Get the value for the key in the associative array, using the given default if no corresponding entry.
# get_env(environment_var, default_value, [trace_level]): returns the value of the environment variable, defaulting to the specified value.
# get_time(): alias for asctime()
# incr_entry(array, key, [increment=1]) Increment the value of an associative array by the given amount, which defaults to 1.
# init_common(): initialize this common module.
# init_var(variable_name, initial_value) Initializes a variable unless it is already defined.
# intersection(list1, list2) Returns the intersection of the two lists (passed as references).
# iso_lower(text): lowercase text accounting for ISO-9660 accents (TODO3: ISO-8859)
# iso_remove_diacritics($text): remove diacritic marks from the text
# issue_command(command_line, [trace_level]) Issue the specified command and ignores the result.
# lock(*FILE): locks the specified file for exclusive access
# make_full_path(filename) Returns the fully-specified pathname to the file.
# make_path(directory, filename) Appends the filename to the directory name to form a full-path file specification.
# pwd(): returns the current directory
# read_file(file_name): Reads in the entire file, returned as a text string.
# remove_dir(full_file_name): removes the directory component from file name
# reset_trace(): reset the line number and other trace information
# run_command(command_line, [trace_level], [disable]) Runs the specified command and returns the result as a string.
# run_perl(command_line) Invoke perl to process the specified command-line.
# tokenize (text) Returns the list of whitespace-delimited tokens from the text
# trace_array(array_ref, [debug_level], [label]) Outputs the (list) array to the trace file, unless current debug level is lower than specified trace level.
# trace_assoc_array(associative_array_ref, [debug_level]) Outputs the associative array to the trace file, unless current debug level is lower than specified trace level.
# trim(text): removes leading and trailing whitespace
# unlock(*FILE): unlocks the specified file
# write_file(file_name, text): Writes the text to the specified file.
#
#
# TODO: use 'our' rather than 'use vars':
##
## our($debug_level, $d, $o, $redirect, $script_dir, $script_name, $TRUE, $FALSE, $MAXINT,
## $disable_commands, $under_WIN32, $unix, $delim, $precision, $verbose, $TEMP, $TMP,
## $OSTYPE, $OS, $HOST, $debugging_timestamps, $unbuffered, $para, $slurp, $PATH, @PATH,
## $common_options);
##
use vars qw/$debug_level $d $o $redirect $script_dir $script_name $TRUE $FALSE $MAXINT
$disable_commands $unix $under_WIN32 $delim $path_delim $path_var_delim $precision $verbose $help $TEMP $TMP
$OSTYPE $OS $HOST $debugging_timestamps $disable_assertions $unbuffered $para $slurp $PATH @PATH
$force_WIN32 $force_unix $osname $common_options $utf8 $BOM $timeout $timeout_seconds $timeout_script $wait_for_user/;
use vars qw/$preserve_temp $strict/;
sub COMMON_OPTIONS { $common_options; }
# NOTE: stupid Perl: the __WARN__ signal is disabled when the -w switch is used
# Also, this doesn't seem to trap warnings due to command-line variables,
## $SIG{__WARN__} = 'filter_warnings';
# Define constants
sub TRUE { 1; }
sub FALSE { 0; }
sub MAXINT { $MAXINT; }
sub MININT { -$MAXINT; }
sub MAXIMUM_INTEGER { $MAXINT; }
sub DELIM { $path_delim; }
sub under_WIN32 { $under_WIN32 && (! $force_unix); }
sub use_WIN32 { $under_WIN32 && ((! $unix) || $force_WIN32); }
sub under_CYGWIN { $under_WIN32 && $unix; }
sub WIN32 { $under_WIN32; }
sub SOLARIS { ($OSTYPE eq "solaris" ? &TRUE : &FALSE); }
# Constants for controlling program trace output.
#
# NOTES:
#
# The higher the value, the more output produced. In practice, start
# out at detailed or verbose during debugging, and then set the values
# higher once debugged, especially for frequently called functions.
#
# *** Resist the temptation to remove debugging trace code, instead
# assign higher trace levels. The person who inherits your code can
# benefit from these trace statements (or even yourself if you
# revisit the code after a long absence).
#
sub TL_ALWAYS {0;} # message always displayed
sub TL_ERROR {1;}; # only information about errors
sub TL_BASIC {2;}; # include important intermediate results
sub TL_WARNING { &TL_BASIC; } # alias for TL_BASIC
sub TL_USUAL {3;}; # a compromise between TL_BASIC & TL_DETAILED
sub TL_DETAILED {4;}; # detailed information (eg, subroutine calls)
sub TL_VERBOSE {5;}; # extra information to help with debugging
sub TL_VERY_DETAILED {6;}; # ex: line-by-line file operations
sub TL_VERY_VERBOSE {7;}; # ex: string manipulation (eg., tokenization)
sub TL_MOST_DETAILED {8;}; # ex: frequently called support functions
sub TL_MOST_VERBOSE {9;}; # ex: results of such functions
sub TL_ALL {99;} # all debugging output
sub DEBUG_LEVEL {$debug_level;};
sub DEBUGGING { return ($debug_level >= TL_USUAL) };
sub DETAILED_DEBUGGING { return ($debug_level >= TL_DETAILED) };
sub VERBOSE_DEBUGGING { return ($debug_level >= TL_VERBOSE) };
sub SCRIPT_DIR {$script_dir;};
sub TEMP_DIR {$TEMP;};
# set_strict_mode(enable): set Perl strict interpretation mode with diagnostics
sub set_strict_mode {
my($strict_mode) = $_[0];
## DEBUG: print STDERR "set_strict_mode($strict_mode)\n";
if ($strict_mode) {
## DEBUG: print STDERR "using strict and diagnostics\n";
eval "use strict";
## TODO: no strict "refs"; # to allow for symbolic file handles
eval "use diagnostics";
}
}
# The -strict option can help track down undeclared variables.
# Be prepared for a plethora of warnings. One that is important is
# 'Global symbol "xyz" requires explicit package name'
# when NOT preceded by 'Global symbol "xyz" requires explicit package name'
# NOTE: not used since other scripts might fail with it
# TODO: fix all client scripts to be strict as well
#
our($strict);
# note: enable here for common.perl and later for client scripts
&set_strict_mode(defined($strict) && $strict);
# init_common()
#
# Initialize this common module. The main purpose is to set up the
# variables used for controlling the debugging traces. This also
# determines the script file name (for usage statements) and directory
# (for locating other scripts).
# EX: init_common() => 1
#
sub init_common {
# This function is automatically called during module loading.
# So if it is explictly invoked, there's no need to proceed.
$initialized = &FALSE if (!defined($initialized));
if ($initialized) {
return ($initialized);
}
$initialized = &FALSE if (!defined($initialized));
# Set debugging level to either $d, DEBUG_LEVEL evironment variable or 3 (the default)
# Note: environment check ignored if DURING_ALIAS set (see tomohara-aliases.bash)
# TODO2: make the debug level initialization more intuitive wrt alias usage
my($env_debug_level) = undef;
my($env_during_alias) = $ENV{DURING_ALIAS};
## DEBUG: print STDERR "\$ENV{DURING_ALIAS}=$env_during_alias\n";
if (! defined($env_during_alias) || (! $env_during_alias)) {
$env_debug_level = $ENV{DEBUG_LEVEL};
}
## DEBUG: print STDERR "\$ENV{DEBUG_LEVEL}=$env_debug_level\n";
## OLD: map { $debug_level = $_ unless defined($debug_level); } ($d, $ENV{"DEBUG_LEVEL"}, &TL_USUAL);
map {
## DEBUG: print STDERR "debug_level=$debug_level _=$_\n";
$debug_level = $_ unless defined($debug_level);
# note: uses first of the three following values that is defined
} ($d, $env_debug_level, &TL_USUAL);
## DEBUG: print STDERR "\$ENV{DEBUG_LEVEL}=$env_debug_level\n";
## $debug_line_num = 0;
&debug_print(&TL_VERBOSE, "init_common(@_)\n");
&trace_assoc_array(\%ENV, &TL_ALL, "%ENV");
# Initialization of important constants
# NOTE: redundant initializations as above due to unit testing problem with
# test-perl-examples.perl
$script_dir = ".";
$script_name = $0;
$TRUE = 1;
$FALSE = 0;
$MAXINT = 2147483647;
&init_var_exp(*disable_commands, $FALSE);
&init_var_exp(*force_WIN32, &FALSE); # force WIN32 file usage, etc.
&init_var_exp(*force_unix, &FALSE); # force unix file usage, etc.
$under_WIN32 = defined($ENV{WINDIR});
$unix = (! $under_WIN32);
&debug_print(&TL_VERBOSE, "take 1: under_WIN32=$under_WIN32 unix=$unix\n");
# Common arguments for the scripts using common.perl
#
$common_options = "[-verbose] [-help]";
&init_var_exp(*precision, 3); # default number of decimal places for rounding
&init_var_exp(*verbose, &FALSE); # verbose output mode
&init_var(*help, &FALSE); # show usage
&init_var_exp(*strict, &FALSE); # use strict perl type checking
&set_strict_mode($strict); # note: could be set via environment so reinvoked (i.e., recheck)
#
&init_var_exp(*unbuffered, &FALSE); # unbuffered I/O
&init_var_exp(*debugging_timestamps, &FALSE); # timestamp all debug output
&init_var_exp(*disable_assertions, &FALSE); # don't check for assertions
&init_var_exp(*preserve_temp, # preserve temporary files
&VERBOSE_DEBUGGING);
# Check options for changing line-input mode
&init_var_exp(*para, &FALSE); # read paragraphs not lines
&init_var_exp(*slurp, &FALSE); # read entire files not lines
&assert(! ($para && $slurp));
$/ = "" if ($para); # paragraph input mode
$/ = 0777 if ($slurp); # complete-file input mode
## OLD:
## # Make sure debugging level corresponds to DEBUG_LEVEL environment variable.
## # Also, the value gets overridden by -d=N command-line switch.
## $debug_level = &get_env("DEBUG_LEVEL", &TL_BASIC);
## if (defined($d)) {
## $debug_level = $d;
## }
# Set or override DEBUG_LEVEL environment variable based on value determined above.
# NOTE: This helps propagate -d settings to perl scripts invoked by the script
$ENV{"DEBUG_LEVEL"} = $debug_level;
# Assign values to basic constants
## $TRUE = 1;
## $FALSE = 0;
## &assert($TRUE != $FALSE);
# See if running under Windows NT or Win95 instead of Unix
# note: OSTYPE normally not set in this case
&init_var_exp(*OSTYPE, "???"); # Unix operating system type
&init_var_exp(*OS, "???"); # Windows operating system
&init_var_exp(*HOST, "???"); # system host name
$osname = (defined($^O) ? $^O : "???"); # name of OS under which Perl built
if ($osname =~ /Win32/i) {
$under_WIN32 = &TRUE;
$unix = &FALSE;
## $redirect = ($debug_level > 3);
}
elsif ($OS eq "Windows_NT") {
$under_WIN32 = &TRUE;
if ($OSTYPE eq "cygwin") {
$unix = &TRUE;
}
}
# Account for Unix/Windows differences in filename and path var. delimiter
$path_delim = "\/";
$path_var_delim = ":";
if (&use_WIN32) {
$path_delim = "\\";
$path_var_delim = ";";
}
# TEMP HACK: support for old name (until client scripts revised)
## $delim = $path_delim;
# Pattern for use in regex (e.g., '$dir =~ /^[$PD]/')
$PD = "\\\\\/";
# Determine the directory for the scripts
$script_name = $0;
$script_dir = $0;
if ($script_dir =~ /[$PD]/) {
$script_dir =~ s/[$PD][^$PD]*$/$path_delim\./; # chop off the file name
$script_name =~ s/\.?\.?[$PD]?.*[$PD]//; # chop off the directory
}
else {
$script_dir = ".";
}
$script_dir = &make_full_path($script_dir);
# Miscellaneous initialization
&init_punctuation unless (defined($punctuation_pattern));
# Temporary directory location
# TMP: system temporary directory
# TEMP: either $TMP or current directory if debugging
# NOTE: this must be done after $unix/$under_WIN32 check
&init_var_exp(*TMP, "/tmp/");
&init_var_exp(*TEMP, (&VERBOSE_DEBUGGING ? &pwd() : $TMP));
$TEMP .= $path_delim unless ($TEMP =~ /[$PD]$/);
if ($unbuffered || &DETAILED_DEBUGGING) {
&debug_print(&TL_VERBOSE, "setting unbuffered I/O\n");
select(STDIN); $| = 1; # set stderr unbuffered
select(STDERR); $| = 1; # set stderr unbuffered
select(STDOUT); $| = 1; # set stdout unbuffered
}
# Redirect stdout to file specified with -o option
if (defined($o)) {
&debug_print(&TL_VERY_DETAILED, "redirecting STDOUT to $o\n");
close(STDOUT);
open(STDOUT, "> $o") || # redefine stdout to the file
die "Unable to create output file $o ($!)\n";
}
# Redirect stderr to stdout if desired (support for Windows)
if (defined($redirect) && ($redirect == &TRUE)) {
select(STDOUT); $| = 1; # set stdout unbuffered
open(STDERR, ">&STDOUT"); # redefine stderr
select(STDERR); $| = 1; # set stderr unbuffered
}
# Make sure input and output use UTF-8
# NOTE: Needed mainly for win32
# TODO: add support for automatic decode_utf8 of input (see count_it.perl)
&init_var_exp(*utf8, &FALSE);
if ($utf8) {
## OLD:
## eval "use Encode;";
eval "use Encode 'decode_utf8'";
## binmode(STDIN, ":utf8");
## binmode(STDOUT, ":utf8");
## binmode(STDERR, ":utf8");
eval 'use open ":std", ":encoding(UTF-8)";';
&debug_print(&TL_DETAILED, "Enabled UTF-8 support\n");
}
# Trace out a few values
my($args) = join(" ", @ARGV);
&debug_print(&TL_VERY_VERBOSE, "$0 @ARGV\n");
&debug_out(&TL_VERBOSE, "starting %s %s [%s]\n", $script_name, $args, &get_time());
## &debug_print(&TL_DETAILED, "$0 $args\n");
&debug_print(&TL_VERBOSE, "debug_level=$debug_level; #ARGV=$#ARGV; script_dir=$script_dir path_delim=$path_delim temp=$TEMP\n");
&debug_print(&TL_VERBOSE, "host=$HOST OS=$OS; OSTYPE=$OSTYPE; osname=$osname under_WIN32=$under_WIN32; Unix=$unix\n");
&trace_array(*ARGV, &TL_VERY_DETAILED, "\@ARGV");
# Setup @PATH global to combination of @INC and $PATH environemnt var
&trace_array(\@INC, &TL_MOST_DETAILED, "\@INC");
&trace_array(\@PATH, &TL_MOST_DETAILED, "default \@PATH");
&init_var_exp(*PATH, "");
@PATH = @INC;
## BAD: map { &pushnew(\@PATH, $_); } split($path_delim, $PATH);
map { &pushnew(\@PATH, $_); } split($path_var_delim, $PATH);
&trace_array(\@PATH, &TL_MOST_DETAILED, "\@PATH");
## NOTE: following assertion yields a warning: Useless use of sort in scalar context ..
## TODO: &assertion(sort(&intersection(\@PATH, \@INC)) == sort(@INC)));
my(@sorted_PATH_INC_intersection) = sort(&intersection(\@PATH, \@INC));
my(@sorted_INC) = sort(@INC);
&assertion(@sorted_PATH_INC_intersection == @sorted_INC);
&trace_array(\@sorted_PATH_INC_intersection, &TL_MOST_DETAILED, "\@sorted_PATH_INC_intersection");
&trace_array(\@sorted_INC, &TL_MOST_DETAILED, "\@sorted_INC");
&init_var_exp(*timeout_script, "cmd.sh"); # script for running commands with timeout check
&init_var_exp(*timeout, &FALSE); # check for command timeout
&init_var_exp(*timeout_seconds, # seconds to wait for timeout
$timeout ? 60 : -1);
&init_var(*wait_for_user, &FALSE); # wait for user at end of script
$initialized = &TRUE;
# Print optional UTF-8 byte order mark (BOM): (U+FEFF)
&init_var_exp(*BOM, &FALSE);
if ($BOM) {
print "\xEF\xBB\xBF\n";
}
&debug_print(&TL_VERY_VERBOSE, "init_common() => $initialized\n");
return ($initialized);
}
# cleanup_common(): module termination cleanup routine
# NOTE: This must be explicitly invoked.
#
# TODO: see if there's a way to invoke it automatically
#
sub cleanup_common {
&debug_out(&TL_VERBOSE, "ending %s: %s\n", $script_name, &get_time());
# Optionally, wait for the user response before terminating.
if ($wait_for_user) {
require 'extra.perl';
&get_user_response("Hit any key to exit: ");
}
}
# force_win32_usage([force]): enables WIN32 usage even when under CygWin
#
sub force_win32_usage {
my($force) = @_;
$force = &TRUE unless (defined($force));
$force_WIN32 = $force;
}
# exit([error_message]): terminates the program, optionally displaying an error message
#
sub exit {
my($error_message) = @_;
if (defined($error_message)) {
chomp $error_message;
&error_out("%s\n", $error_message);
}
&cleanup_common();
exit;
}
# get_time(): alias for asctime()
#
sub get_time {
return (&asctime());
}
# get_env(environment_var, default_value, [trace_level])
#
# Returns the value of the environment variable, defaulting to the specified
# value.
#
sub get_env {
my($var, $default, $trace_level) = @_;
my($var_text, $value);
$trace_level = &TL_VERY_VERBOSE unless (defined($trace_level));
$value = $default;
$var_text = $ENV{$var};
if (defined($var_text)) {
$value = $var_text;
}
&debug_out($trace_level,
"get_env(\"%s\", \"%s\", trace_level=%d) => %s\n",
$var, $default, $trace_level, $value);
return $value
}
# set_env(environment_var, value, [trace_level])
#
# Sets the value of the environment variable. (Useful for tracing.)
#
sub set_env {
my($var, $value, $trace_level) = @_;
$trace_level = &TL_VERY_VERBOSE unless (defined($trace_level));
## TODO: &assert(defined($value));
$ENV{$var} = $value;
&debug_out($trace_level, "set_env(%s, %s)\n", $var, $value);
}
# run_command(command_line, [trace_level], [disable])
#
# Runs the specified command and returns the result as a string.
#
# NOTES:
#
# Command execution is disabled with $disable_commands = &TRUE.
#
# This is leading to some obscure Perl bug in the reporting of
# undefined values in dump_links.perl (perhaps an internal perl variable).
# This has not been noticed with other scripts.
#
# EX: run_command("echo a b c") => "a b c"
#
# TODO:
# - Use a more intuitive way of disabling commands
# - Try to detect with run_command_win32 needed under CygWin (e.g., trapping output from C# applications)
#
sub ALWAYS_RUN { &FALSE };
#
sub run_command {
if (&use_WIN32) {
return (&run_command_win32(@_));
}
my($command, $trace_level, $disable) = @_;
$trace_level = &TL_VERBOSE+1 unless (defined($trace_level));
$disable = $disable_commands unless (defined($disable));
my($result) = "";
# See if system commands have been disabled (for testing purposes)
if ($disable) {
&debug_out($trace_level - 1, "(disabled) run_command: %s\n", $command);
return ("");
}
# Add optional timeout check (only works for Unix or CygWin)
my($temp_script) = "";
if ($timeout_seconds > 0) {
## $command =~ s/([^\\])([*?&])/$1\\$2/g;
# TODO: Quote arguments if special shell characters
## if ($command =~ /([^\\])([*?&])/) {
## my($command_name, @args) = split(/ +/, $command);
## $command = sprintf "$command_name %s", join(" ", map { "\"$_\""; } @args);
## }
# Use temporary script to run command as eval environment in cmd.sh runs into problems with special shell characters
$temp_script = "temp-$$.sh";
&write_file($temp_script, "$command\n");
$command = "$timeout_script --time-out $timeout_seconds source $temp_script";
}
&debug_print($trace_level, "run_command: $command\n");
open(RUN_COMMAND_FILE, "$command|");
while (<RUN_COMMAND_FILE>) {
$_ =~ s/[\n\r]+$//;
$result .= "$_\n";
}
$result =~ s/[\n\r]+$//;
close(RUN_COMMAND_FILE);
&debug_out($trace_level+1, "run_command => {\n%s\n}\n", $result);
if ($temp_script ne "") {
## OLD: unlink $temp_script unless (&VERBOSE_DEBUGGING);
unlink $temp_script unless ($preserve_temp);
}
return ($result);
}
# run_command_win32: version of run_command for Windows
#
# NOTE: output redirection is not supported
#
my($run_output_num) = 0;
#
sub run_command_win32 {
my($command, $trace_level, $disable) = @_;
$trace_level = &TL_VERBOSE+1 unless (defined($trace_level));
$disable = $disable_commands unless (defined($disable));
my($result) = "";
&assert($command !~ /\>/);
# See if system commands have been disabled (for testing purposes)
if ($disable) {
&debug_out($trace_level - 1, "(disabled) run_command: %s\n", $command);
return ("");
}
# Issue the command (indirectly via system)
# NOTE: output redirection not preceded by space (e.g. the space could be part of output if echoed)
my($temp_file) = sprintf "%s%s-%d-%d.data", &TEMP_DIR, "temp-run-output", $$, $run_output_num++;
&issue_command("$command > $temp_file", $trace_level+1);
$result = &read_file($temp_file, $trace_level+2);
$result =~ s/\r\n/\n/g;
chomp $result;
&debug_out($trace_level+1, "run_command_win32 => {\n%s\n}\n", $result);
## OLD: unlink $temp_file unless (&DEBUG_LEVEL > &TL_VERY_DETAILED);
## TODO: see why following didn't generate warning
## BAD: unlink $temp_temp unless ($preserve_temp);
unlink $temp_file unless ($preserve_temp);
return ($result);
}
# run_command_fmt(command_format_string, arg1, ...)
#
# Version of run_command that allows sprintf format string and arguments
# NOTE: trace-level and disable-command optional arguments are not supported
#
sub run_command_fmt {
my($command_fmt, @args) = @_;
return (&run_command(sprintf $command_fmt, @args));
}
# run_command_over(command, data, [trace_level])
#
# Run's the specified command using the data as input returning the output
# as a string.
#
# NOTE: The commmand is assumed to take a file name as the last input. A
# temporary file is created for this purpose, which uses a unique ID and
# is retained during debugging.
#
# TODO: just use a single temp file (eg, temp-run-command-$$.data) rather
# than separate ones (e.g., temp-run-command-$$-$num.data) unless verbose debugging
#
my($run_num) = 0;
#
sub run_command_over {
my($command, $input, $trace_level) = @_;
$trace_level = &TL_VERY_DETAILED unless (defined($trace_level));
# Output input to temp file and then run command using the file as input
my($temp_file) = sprintf("%s%s-%d-%d.data", &TEMP_DIR, "temp-run-command", $$, $run_num++);
&write_file($temp_file, $input);
my($result) = &run_command(sprintf("%s %s", $command, $temp_file), $trace_level);
## TODO: add $preserve_temp_files option
unlink $temp_file unless (&DEBUG_LEVEL >= $trace_level);
return ($result);
}
# issue_command(command_line, [trace_level])
#
# Issue the specified command and ignores the result.
#
# NOTE: Command execution is disabled with $disable_commands = &TRUE
#
sub issue_command {
my($command, $trace_level) = @_;
$trace_level = &TL_DETAILED unless (defined($trace_level));
my($result);
# Under Win32, commands to be run interpretted by the shell are run via 'cmd /c'
# NOTE: This is used mainly when running perl scripts
# Also, the command needs to be properly escaped
# TODO: Add support for Win95 and Win98
if (&use_WIN32 && ($command !~ /\.exe/)) {
$command = "cmd /c " . $command;
# Make sure backslashes are properly escaped
$command =~ s/([^\\])\\([^\\])/$1\\\\$2/g;
}
# Add optional timeout check (only works for Unix or CygWin)
if ($timeout_seconds > 0) {
$command = "$timeout_script --time-out $timeout_seconds $command";
}
&debug_print($trace_level, "issue_command: $command\n");
system("$command") unless ($disable_commands);
}
# issue_command_over(command, data)
#
# Issues the specified command using the data as input.
# NOTE: The commmand is assumed to take a file name as the last input. A
# temporary file is created for this purpose, which uses a unique ID and
# is retained during debugging.
# TODO: consolidate with run_command_over
#
my($issue_num) = 0;
#
sub issue_command_over {
my($command, $input) = @_;
my($temp_file) = sprintf "%s%s.%d.%d", &TEMP_DIR, "temp_run_command", $$, $issue_num++;
&write_file($temp_file, $input);
my($result) = &issue_command(sprintf "%s %s", $command, $temp_file);
## OLD: unlink $temp_file unless (&DEBUG_LEVEL > &TL_VERBOSE);
unlink $temp_file unless ($preserve_temp);
return ($result);
}
# issue_command_fmt(command_format_string, arg1, ...)
#
# Version of issue_command that allows sprintf format string and arguments
# NOTE: trace-level optional argument is not supported
#
sub issue_command_fmt {
my($command_fmt, @args) = @_;
return (&issue_command(sprintf $command_fmt, @args));
}
# cmd(command_line): same as issue_command(command_line)
#
sub cmd {
&issue_command(@_);
}
# shell(command_format_string, format_arguments)
#
# Run a command that is specified via an sprintf format string with arguments.
# NOTE: This is equivalent to issue_command_fmt
#
sub shell {
my($command_fmt, @args) = @_;
&issue_command(sprintf $command_fmt, @args);
}
# debug_on([new-trace-level=TL_DETAILED]): turn on detailed debugging
#
sub debug_on {
## BAD: my($level) = $_;
my($level) = @_;
$level = &TL_DETAILED if (!defined($level));
$debug_level = $level;
}
# debug_out(trace_level, format_string, argument, ...)
#
# Prints a formatted trace message to STDERR when the current debugging
# level is at or above the specified level.
#
# TODO: Introduce debug_out_fmt for the printf version to avoid problems
# with % characters occurring in the format text. Then this version
# (debug_out) would just print the text.
#
sub debug_out {
my($level, $format_text, @args) = @_;
&debug_print(&TL_ALL, "debug_out(@_)\n");
# Do sanity check to ensure printf formatting present
# TODO: move inside trace level test to avoid too many warnings.
if (&DETAILED_DEBUGGING && ($format_text !~ /%/)) {
my($package, $filename, $line) = caller;
&warning("No printf format specification at $filename:$line: Consider using debug_print instead of debug_out\n");
}
# Perform the formatted output unless trace level not sufficiently high.
if ($level <= $debug_level) {
printf STDERR "[%s] ", &asctime, if ($debugging_timestamps);
printf STDERR $format_text, @args;
}
return (1);
}
# debug_out_fmt(trace_level, format_string, argument, ...)
# alias for debug_out
#
sub debug_out_fmt {
return (&debug_out(@_));
}
# debug_out_new(trace_level, text)
# version of debug_out that doesn't using printf for formatting the results
# NOTE: this avoids problems with strings containing printf formatting codes
# TODO: rename debug_out as debug_out_fmt and debug_out_new as debug_out???
#
sub debug_out_new {
my($level, $text) = @_;
if ($level <= $debug_level) {
print STDERR $text;
}
}
# debug_trace(trace_level, text, ...)
# version of debug_out that doesn't using printf for formatting the results
# NOTE: this avoids problems with strings containing printf formatting codes
# TODO: rename debug_out as debug_out_fmt and debug_out_new and debug_out
#
sub debug_trace {
my($level, @text_args) = @_;
if ($level <= $debug_level) {
printf STDERR "[%s] ", &asctime, if ($debugging_timestamps);
print STDERR @text_args;
}
}
# debug_print(args): alias for debug_trace
#
sub debug_print {
return (&debug_trace(@_));
}
# debug_printf(format, args): alias for debug_out_fmt
#
sub debug_printf {
return (&debug_out_fmt(@_));
}
# TODO: add function for conditional evaluation of debug code:
## # debug_code(trace_level, perl_code)
## # Evaluate given Perl code if debug level in effect.
## # EX: debug_code(&TL_VERBSOSE, "$file =~ s/\\n/\\n /g;");
## #
## sub debug_code {
## my($level, @command_args) = @_;
## if ($level <= $debug_level) {
## eval "@command_args";
## }
## }
# error_out(format_string, argument, ...)
#
# Prints a formatted error message to STDERR
#
sub error_out {
&debug_print(&TL_ERROR, "ERROR: ");
return &debug_out(&TL_ERROR, @_);
}
# error_printf(arg, ...): alias for error_out
#
sub error_printf {
&error_out(@_);
}
# error_print(argument, ...)
#
# Prints an error message to STDERR using concatenation of args
#
sub error_print {
&error_out("%s", join("", @_));
}
# error(arg, ...): alias for error_print
#
sub error {
&error_print(@_);
}
# warning_out(format_string, argument, ...)
#
# Prints a formatted warning message to STDERR
#
sub warning_out {
&debug_print(&TL_WARNING, "WARNING: ");
return &debug_out(&TL_WARNING, @_);
}
# warning_printf(arg, ...): alias for warning_out
#
sub warning_printf {
&warning_out(@_);
}
# warning_print(argument, ...)
#
# Prints an warning message to STDERR using concatenation of args
#
sub warning_print {
&warning_out("%s", join("", @_));
}
# warning(arg, ...): alias for warning_print
#
sub warning {
&warning_print(@_);
}
# assert(expression)
#
# Issues an error message if the expression evaluates to 0, showing
# the expression that failed and the file name and line number, as in
# the C assert macro. This faciliates debugging, especially when the
# problem is due to unexpected input.
#
# example: &assert((scalar @all_senses) == (scalar @singleton_senses));
#
# TODO:
# - have version that doesn't evaluate the expression
#
sub assert {
my($expression, $package, $filename, $line) = @_;
if ($disable_assertions) {
&debug_print(99, "[common]assert(@_) {checking disabled}\n");
return;
}
&debug_print(&TL_VERBOSE+4, "[common]assert(@_)\n");
# Determine the package to use for the evaluation environment,
# as well as the filename and line number for the assertion call.
if (! defined($package)) {
($package, $filename, $line) = caller;
}
# Evaluate the expression in the caller's package
## &debug_print(&TL_VERBOSE+4, "eval: 'package $package; $expression;'\n");
my($result) = eval("package $package; $expression;") if (defined($expression));
# If failed, then display the failed expression along with it's
# filename and line number (as in the assert macro for C).
if (!defined($result) || ($result eq "0")) {
## OLD: $expression = &run_command("tail +$line '$filename' | head -1", &TL_VERY_VERBOSE);
$expression = &run_command("tail --lines=+$line '$filename' | head -1", &TL_VERY_VERBOSE);
$expression =~ s/^\s*&?assert(\(.*\));\s*(\#.*)?$/$1/;
print STDERR "*** Assertion failed: $expression [$filename: $line] (input line=$.)\n";
}
else {
&debug_print(&TL_VERBOSE+4, "result: $result\n");
}
}
# soft_assertion(expression): Alias for assert
# assertion(expression): ditto
# TODO: Rework so soft_assertion is the defined function (i.e., not alias).
#
sub soft_assertion {
my($package, $filename, $line) = caller;
&assert(@_, $package, $filename, $line);
}
#
sub assertion {
my($package, $filename, $line) = caller;
&assert(@_, $package, $filename, $line);
}
# reset_trace(): reset the line number and other trace information
# NOTE: obsolete function
#
sub reset_trace {
## $debug_line_num = 0;
}
# dump_line([line], [debug_level]): display line in trace (w/ line number)
#
sub dump_line {
my($line, $level) = @_;
$line = $_ unless defined($line);
$level = &TL_VERY_DETAILED unless defined($level);
chop $line if ($line =~ /\n$/);
&debug_out($level, "%d:\t%s\n", $., $line);
}
# trace_assoc_array(associative_array_ref, [debug_level], [label])
#
# Outputs the associative array to the trace file, unless current debug
# level is lower than specified trace level.
#
sub trace_assoc_array {
my($assoc_array_ref, $trace_level, $label) = @_;
$trace_level = &TL_VERBOSE + 2 if (!defined($trace_level));
$label = $assoc_array_ref if (!defined($label));
if ($trace_level <= &DEBUG_LEVEL) {
my($key);
&debug_out($trace_level, "%s: {\n", $label);
foreach $key (sort(keys(%{$assoc_array_ref}))) {
my($value) = ${$assoc_array_ref}{$key};
$value = "(undefined)" if (!defined($value));
&debug_out($trace_level, "\t%s: %s\n", $key, $value);
}
&debug_print($trace_level, "\t}\n");
}
return;
}
# trace_associative_array: alias for trace_assoc_array
#
sub trace_associative_array {
return (&trace_assoc_array(@_));
}
# trace_array(array_ref, [debug_level], [label])
#
# Outputs the (list) array to STDERR, unless current debug
# level is lower than specified trace level.
#
sub trace_array {
my($array_ref, $trace_level, $label) = @_;
$trace_level = &TL_VERBOSE + 2 if (!defined($trace_level));
$label = $array_ref if (!defined($label));
if ($trace_level <= &DEBUG_LEVEL) {
my($i);
&debug_out($trace_level, "%s:\n", $label);
for ($i = 0; $i <= $#{$array_ref}; $i++) {
&debug_out($trace_level, "\t%d:'%s'\n", $i, ${$array_ref}[$i]);
}
}