forked from verhas/ScriptBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjamal.pl
More file actions
1811 lines (1562 loc) · 54.8 KB
/
jamal.pl
File metadata and controls
1811 lines (1562 loc) · 54.8 KB
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
#!/usr/bin/perl
# LEGAL WARNING:
#
# This software is provided on an "AS IS", basis,
# without warranty of any kind, including without
# limitation the warranties of merchantability, fitness for
# a particular purpose and non-infringement. The entire
# risk as to the quality and performance of the Software is
# borne by you. Should the Software prove defective, you
# and not the author assume the entire cost of any service
# and repair.
#
# online documentation can be converted to html
# using esd2html.pl from http://www.isys.hu/c/verhas
=pod
=H jamal main file
=abstract
This is the main Perl file for the jamal distribution.
=end
The functions and other entities are defined here in the order
as they appear in the source file. In case you are interested in
writing extensions for jamal in Perl see the functions that are in
the package jamal. These can be found after the chapter T<packageJamal>.
See the chapter R<packageJamal> for rules that a jamal compliant extension
should implement.
=cut
=pod
=section history
=H Jamal version history
=itemize
=item 2.03 {#define? ...} and {?macro} feature introduced
=item 2.021 {#define A/X=BXB}{A 0} formerly resulted BB. Now it works fine (results B0B, where 0 is a zero not an oh).
=item 2.02 {#sep[[/]]} drowe jamal to infinite loop (macro open char is null string)
=item ---- create directory for trace and output if it did not exist
=item 2.01 {#-t operator is introduced for file time
=item ---- line number tracking for error messages
=item ---- first operand bug of 'and' and 'or' fixed
=item ---- new operator 'not' (how could i miss it?)
=item 2.0 release includes Perl extension interface
=item 1.0 release
=item 1.0beta6 split macro to that many argument as many needed. The last contains the 'rest'
=item 1.0beta5 correction to handle new line as a list separator correctly
=item 1.0beta4 first public release December 15, 1997.
=noitemize
=cut
*VERSION = \'2.03'; # the version of jamal
*umask = \0777; # directory permission when a new dir is created
$timeNow = time(); #
*SYSmacroOpen = \'{'; # macro opening string default value
*SYSmacroClose = \'}'; # macro closing string default value
$macroOpen = $SYSmacroOpen; # current macro opening string
$macroClose = $SYSmacroClose; # current macro closing string
$MacroOpen = quotemeta $macroOpen; # current macro opening match
$MacroClose = quotemeta $macroClose; # current macro closing match
$mopenCNL = &CNL($macroOpen); # new line characters in macro opening string
$mcloseCNL = &CNL($macroClose); # new line characters in macro closing string
# CNL=Count New Lines
@MacStack = ();
# preset command line arguments
$OutputError = 0; #by default erroneous macros result empty string
$SuppressOutput = 0;
$TraceFile = undef;
$LastFileNameDisplayedOnTraceOutput = '';
$Source = undef;
$Destination = undef;
@Macrofiles = ();
# Hashes to store macros and argument lists
%Macro = (); # $Macro {'name'} = 'body of the macro';
%MacArg = (); # $MacArg{'name'} = ref to argument string array
# Stack to store old macro values when they are redefined w/o undefing first
%MacroStack = ();
# The loaded extension packages
%Module = ();
# minimal and maximal version numbers of a module
%minV = (); %maxV = ();
# required jamal version of a module
%ModRequire = ();
# to store the current file list along includes (how did we get to the file
# we are currently prpcessing, who included who, and at what line in which file)
@FILE = ();
%LINE = ();
# base module stack
@BaseModules = ();
$BaseModule = undef; # no base module by default
# a global variable telling jamal that an
# extension macro should not be processed
# after it has been evaluated
$ExtensionVerbatim = 0;
while( $_ = shift @ARGV ){
if( s/^\-// ){#this is option
if( $_ eq '?' || $_ eq 'h' ){
&help; exit;
}
if( $_ eq 'm' ){
push @Macrofiles, shift @ARGV;
next;
}
if( $_ eq 'I' ){
push @INC, shift @ARGV;
next;
}
if( $_ eq '0' ){
$SuppressOutput = 1;
next;
}
if( $_ =~ /\D(\w+)((?:.|\n)*?)=((?:.|\n)*)$/ ){
&DefineMacro($1,$2,$3);
next;
}
if( $_ eq 'd' ){
$OutputError = 1;
next;
}
if( $_ eq 't' ){
$TraceFile = shift @ARGV;
next
}
&Error("Invalid option. Ignored.",$_);
}else{
# file name source then destination
unless( defined($Source) ){
$Source = $_; next;
}
unless( defined($Destination) ){
$Destination = $_; next;
}
# if both files are defined
print STDERR "There are too many command line arguments\njamal -?";
exit;
}
}
unless( defined $Source ){
&Error('No input is defined.');
exit;
}
if( $TraceFile && &maked($TraceFile) && !open(TRACE,">$TraceFile")){
&Error("Trace file can not be opened.",$TraceFile);
$TraceFile = undef;
}
# make file names UNIX compliant under Windows NT
# this should not harm under UNIX (usually :-)
# don't be pervert to include a \ in a file name
$Source =~ s{\\}{/}g;
$Destination =~ s{\\}{/}g;
&PredefineMacro;
for $macrofile ( @Macrofiles ){
&DoFile($macrofile);
}
if( $Destination && &maked($Destination) && !open($Destination,">$Destination") ){
&Error('Output file can not be opened.',$Destination);
exit;
}
$output = &DoFile($Source);
if( $Destination ){
print $Destination $$output;
close $Destination;
}else{ print $$output unless $SuppressOutput }
#we are ready
exit;
=pod
=section DoFile
=H Process an input file
This function gets an imput file name, reads the content and
processes all jamal macros in it. The result is put into a
string buffer and reference to it is returned.
Usage:
=verbatim
$sref = &DoFile('filename');
print $$sref; # dereference
=noverbatim
For the actual processing function T<DoInput> is invoked.
=cut
sub DoFile {
my $file = shift;
my $output = '';
push @FILE,$file;
$LINE{$file} = 1;
unless( open($file,"<$file") ){
print STDERR "Input file '$file' cannot be opened.\n";
if( $#FILE > 0 ){
print STDERR " Include hierarchy leading to this file name:\n";
pop @FILE;
my $lastFile = pop @FILE;
my $spc = 0;
for( @FILE ){
print STDERR ' ' x $spc++," file $_ at line ",$LINE{$_}," included\n";
}
print STDERR ' ' x $spc++," file $lastFile at line ",$LINE{$lastFile}," tried to include this file\n";
}
return \'';
}
my $o_irs = $/; undef $/;
my $input = <$file>;
close $file;
$/ = $o_irs;
# If the last character on a line is \ and the very first character on the next line is
# _ (underscore) then the backslash, the newline, the underscore and the whitespaces on that
# line are removed. This helps tabulating in an environment where spaces play important role
$input =~ s{\\\n\_\s*}{}g;
# The \ character at the end of a line removes the new line character
# this can be extremely useful in fine tuned HTML code
$input =~ s{\\\n}{}g; # escape new lines
$input =~ s{\\$}{}; # last line end w/o new line
# note that here we loose the line numbering precision
# therefore line numbers in the error messages only correlate
# with the real line numbers, and equals the real line numbers
# only if the source file does not use new line escape
my $return = &DoInput( \$input , $file , 0);
pop @FILE;
return $return;
}
=pod
=section DoInput
=H Process an input string
Get a string, resolve macro references and return a pointer
to the result.
Usage:
=verbatim
$sref = &DoInput( \$input , $file , $level );
print $$sref; # dereference
=noverbatim
The second parameter T<$file> is needed only to resolve T<{#include}>
macros with relative directory reference.
The third parameter T<$level> gives the actual deepness of the code. This is zero
for characters coming from the input file. It is T<n> for characters generated by
macros of the level T<n-1>. It is used to track line numbers for error messages.
The actual algorithm of T<DoInput> is very simple. It takes all character that do not
belong to a macro and appends to the output which has zero length at start. When it
finds the start of a macro it chops it off calling T<ChopMacro>, and evaluates the macro
calling T<EvalMacro> and appends the result to the output and goes on for other macros.
It actually calls itself recursively as well as the called T<EvalMacro> may also
call T<DoInput> recursively.
=cut
sub DoInput {
my $input = shift;
my $file = shift;
my $level = shift;
my $output = '';
while( length($$input) > 0 ){
if( index($$input,$macroOpen) == 0 ){
$$input = substr($$input,length($macroOpen));
$LINE{$file} += $mopenCNL unless $level;
#chop off the macro and return the string
my $macro = &ChopMacro($input,$level);
&Trace('M',$macro,$level);
# calculate the line we are going to be after this macro is processed
my $LineNR = $LINE{$file} + &CNL($$macro) + $mcloseCNL;
#if the macro starts with a @ character then macros used
#within are not substituted before processing the macro only after
unless( substr($$macro,0,1) eq '@' ){
my $oldLine = $LINE{$file};
$macro = &DoInput($macro,$file,$level);
$LINE{$file} = $oldLine;
&Trace('M',$macro,$level);
}
# evaluate the macro and set the new line number if it was top level macro
$output .= &EvalMacro($macro,$file,$level);
$LINE{$file} = $LineNR unless $level;
}else{
# Former version used pattern matching but Perl was not
# capable handling some of the large strings. This should also
# be faster:
my $i;
if( -1 < ($i = index($$input,$macroOpen)) ){
$lll = substr($$input,0,$i);
$output .= $lll;
&Trace('L',\$lll,$level);
$LINE{$file} += &CNL($lll) unless $level;
$$input = substr($$input,$i);
}else{
&Trace('L',$input,$level);
$output .= $$input;
$LINE{$file} += &CNL($$input) unless $level;
$$input = '';
}
}
}
return \$output;
}
=pod
=section ChopMacro
=H Chop macro off a string
Chop off a macro from the start of the input string, (passed by reference)
put it into a new buffer and return the reference of the
buffer. The input string passed by reference does NOT contain the opening
macro brace, it should already be chopped off.
Usage:
=verbatim
$sref = &ChopMacro( \$input);
=noverbatim
The result string will not contain the opening and closing
brace (or whatever the actual value of T<MactoOpen> and T<MacroClose>
are.
The function takes care of the embedded macros counting up for each opening brace
and counting down for each closing brace. When counting down result zero, the end of the
macro is found.
If the counter does not reach zero till the end of the file there is a macro nesting
error, in other words more opening than closing brace. In such a case the function
T<Error> is called and the function calls T<exit>.
=cut
sub ChopMacro {
my $input = shift;
my $Counter = 1;# we are after one macro opening brace
my $output = '';
my $chop;
while( $Counter ){# while there is any opened macro
if( length($$input) == 0 ){# some macro was not closed
&Error('Erroneous macro nesting.',$output);
return \$output;
}
if( index($$input,$macroOpen) == 0 ){
$$input = substr($$input,length($macroOpen));
$Counter ++; #count the new opening
$output .= $macroOpen;
}
elsif( index($$input,$macroClose) == 0 ){
$$input = substr($$input,length($macroClose));
$Counter --; # count the closing
return \$output unless $Counter > 0;
$output .= $macroClose;
}else{
# Former version used pattern matching but
# Perl had difficulties to handle large
# strings. This is a faster solution.
# Go and find the next macro opening or closing brace
my $i = index($$input,$macroOpen);
my $j = index($$input,$macroClose);
# whichever we have found and preceedes the other
$i = $j if $j < $i && $j != -1 || $i == -1;
if( -1 < $i){
$output .= substr($$input,0,$i);
$$input = substr($$input,$i);
}else{
$output .= $$input;
$$input = '';
}
}
}
}
=pod
=section EvalMacro
=H Evaluate a macro
Evaluate the macro given as a string reference. Returns
the result as a string.
Usage:
=verbatim
$output .= &EvalMacro(\$string,$filename,$level)
=noverbatim
T<$filename> is used merely to resolve relative file references in T<include>
macros.
The third parameter T<$level> gives the actual deepness of the code. This is zero
for characters coming from the input file. It is T<n> for characters generated by
macros of the level T<n-1>. It is used to track line numbers for error messages.
This function first checks if there is a T<@verbatim> modifier before the macro name.
After this: if the macro starts with T<#> or T<@> then it is a built in macro
otherwise this is user defined or predefined macro (functinally there are no differences).
Finally T<DoInput> is called in most cases to resolve macros in the resulted string.
When a macro starts with the T<$> character it is a loop macro and is processed only if
it is in a loop. Otherwise the result is the macro itself.
=cut
sub EvalMacro {
my $input = shift;
my $ReferenceFileName = shift;
my $level = shift;
my $verbatim = 0;
my $ReportNotDefinedMacro = 1;
$verbatim = 1 if $$input =~ s/^(?:#|@)verbatim\s+// ;
my $char,$index=0; while( ($char=substr($$input,$index,1)) eq ' ' ||
$char eq "\n" || char eq "\t" || char eq "\r" ){ $index++ }
if( ($char = substr($$input,$index,1)) eq '@' || $char eq '#' ){
$index ++;
while( ($char=substr($$input,$index,1)) eq ' ' ||
$char eq "\n" || char eq "\t" || char eq "\r" ){ $index++ }
$$input = substr($$input,$index);
$index = 1; # this is built in macro
}else{
if( substr($$input,$index,1) eq '?' ){
$index++;
while( ($char=substr($$input,$index,1)) eq ' ' ||
$char eq "\n" || char eq "\t" || char eq "\r" ){ $index++ }
$ReportNotDefinedMacro = 0;
}
$$input = substr($$input,$index) if $index > 0;
$index = 0; # this is user defined macro
}
if( $index ){
# Perl module extension macro.
if( $$input =~ /^\w*\:\:/ ){#/^(\w*)((?:\:\:\w+)+)/ ){
$$input =~ s/^(\w*)//;
my $base = $1;
$$input =~ s/((?:\:\:\w+)+)//;
my $macro = $1;
$base = $BaseModule unless $base;
$@ = '';
local $ExtensionVerbatim = 0;
local $InputFileName = $ReferenceFileName;
local $OutputFileName = $Destination;
my $result = eval "&jamal::$base$macro(" . '$$input)';
if( ! $@ ){
return $result if $ExtensionVerbatim ;
$result = &DoInput(\$result,$ReferenceFileName,$level+1);
return $$result;
}else{
return &Error("Extension macro resulted the error: $@",$$input);
}
}
if( $$input =~ /^require(\W.*)$/ ){
my $rversion = &Split($1,1);
$rversion = $rversion->[0];
if( $rversion > $VERSION ){
&Error("This file requires jamal V$rversion and I am only $VERSION",$$input);
exit;
}
return '';
}
if( $$input =~ /^with(\W.*)$/ ){
my $package = &Split($1,1);
if( $$package ){
push @BaseModules,$BaseModule;
$BaseModule = $$package;
}else{ $BaseModule = pop @BaseModules }
return '';
}
# {#use package version}
if( $$input =~ /^use(\W.*)$/ ){
my ($package,$version) = @{Split($1,2)};
$version = '' unless defined $version;
if( defined $Module{$package} ){
&Warning("Extension $package is already loaded.",$$input);
}
$@ = '';
$Module{$package} = $version;
$jamal::VersionWasCalled = 0; $jamal::RequireWasCalled = 0;
eval "use jamal::$package";
if( ! $@ ){
if( $version && (
(defined($maxV{$package}) && $maxV{$package} < $version) ||
(defined($minV{$package}) && $minV{$package} > $version) )){
&Error("Extension $package does not implement the requested version $version.",$$input);
exit;#this is a severe error
}
if( ! $jamal::VersionWasCalled ){
&Error("Extension $package does not specify its own version.",$$input);
}
if( ! $jamal::RequireWasCalled ){
&Error("Extension $package does not specify the jamal version it requires.",$$input);
}
push @BaseModules,$BaseModule;
$BaseModule = $package;
return '';
}else{
delete $Module{$package};
return &Error("Extension $package is not found. Error message: $@",$$input);
}
}
if( $$input =~ /^sep(\W.*)$/ ){
my $seps = $1;
my ($mO,$mC) = @{&Split($seps,2)};
if( $mO eq '' ){
&Error("Semantic error in macro SEP would result empty macro open string",$$input);
return '';
}
if( $mC eq '' ){
&Error("Semantic error in macro SEP would result empty macro close string",$$input);
return '';
}
$macroOpen = $mO;
$macroClose = $mC;
$MacroOpen = quotemeta $macroOpen;
$MacroClose = quotemeta $macroClose;
$mopenCNL = &CNL($macroOpen);
$mcloseCNL = &CNL($macroClose);
return '';
}
if( $$input =~ /^for\s+(\w+)\s*((?:.|\n)*)$/ ){
my $loopV = quotemeta $1;
my $loop = $2;
$loop =~ s/^(.|\n)//;
my $term = &CharPair($1);
$loop =~ s/^((?:.|\n)*?)$term//;
my $loopP = $1;
if( $loopP =~ /^\s*((?:\+|\-)?\d+)\s*\.\.\.?\s*((?:\+|\-)?\d+)\s*$/ ){
#{#for i/ 1 ... 1000 / a[i]=i;}
my $s = $1; my $e=$2;
my $output = '';
while(1){
my $wp = $loop;
$wp =~ s/$loopV/$s/g;
$output .= $wp;
last if $s == $e;
if( $s < $e ){ $s++ }else{ $s-- }
}
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
#{#for i/,1,2,3,4,kakukk/ a[i]=i;}
my $argv = &Split( $loopP );
my $output = '';
for( @$argv ){
my $wp = $loop;
$wp =~ s/$loopV/$_/g;
$output .= $wp;
}
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
#{#dir/directory/pattern/text}
if( $$input =~ /^dir(\W(?:.|\n)*)$/ ){
my $macro = $1;
my $sortorder = '';
my $html = 0;
$macro =~ s/^(.|\n)//;
my $sep = $1;
if( $sep =~ /^\s+$/ ){
$sep = '\s+';
$macro =~ s/^\s*//;
}else{ $sep = quotemeta $sep }
$macro =~ m{(.*?)$sep(.*?)$sep((?:.|\n)*)$};
my $rdir = $1;
my $pattern = $2;
my $body = $3;
my $RefD = '';
my $recurse = 0;
my $pat = $pattern;
while( $pat =~ s{^\s*\-}{} ){
while( $pat =~ s{^(\w|\.)}{} ){
my $f = $1;
if( $f eq 'h' ){ $html = 1; next; }# get html parameters from the files
if( $f eq 'S' ){
$pat =~ s{^(n|t|s|d)(a|d)?}{};
$html = 1 if $1 eq 't';
$sortorder = $1 . ($2 ? $2 : 'a') ;
next;
}
if( $f eq 'i' || $f eq 'o' ){
if( $RefD ne '' ){
&Error('Ambiguous directory definition.',$$input);
next;
}else{ $RefD = $f; next; }
}
if( $f eq 'R' ){ $recurse = 1; next; }
next if $f =~ /\.|z|s|d|f/;
&Error("Invalid dir option $f",$$input);
}
}
$RefD = 'i' unless $RefD;
my $dir;
$dir = &Relative($ReferenceFileName,$rdir) if $RefD eq 'i' || ($RefD eq 'o' && !$Destination);
$dir = &Relative($Destination,$rdir) if $RefD eq 'o' && $Destination;
my @filist;
if( $recurse ){
opendir(D,$dir);
my @f = readdir(D);
closedir(D);
my @DirsLeft = ();
my $qdir = '';
while(1){
$qdir = $qdir . '/' if $qdir;
for( @f ){
if( -d "$dir/$qdir$_" ){
push @DirsLeft , "$qdir$_" unless $_ eq '.' || $_ eq '..';
}
push @filist , "$qdir$_";
}
last if $#DirsLeft == -1;
$qdir = pop @DirsLeft;
opendir(D,"$dir/$qdir");
@f = readdir(D);
closedir(D);
}
}else{
opendir(D,$dir);
@filist = readdir(D);
closedir(D);
}
while( $pattern =~ s{^\s*\-}{} ){
# z zero size, s nonzero size, f plain file, d directory, . not . or ..
while( $pattern =~ s{^(\w|\.)}{} ){
my $f = $1;
if( $f eq 'S' ){
$pattern =~ s{^(n|t|s|d)(a|d)?}{};
next;
}
@filist = grep( -z "$dir/$_" , @filist) if $f eq 'z';
@filist = grep( -s "$dir/$_" , @filist) if $f eq 's';
@filist = grep( -d "$dir/$_" , @filist) if $f eq 'd';
@filist = grep( -f "$dir/$_" , @filist) if $f eq 'f';
# exclude the . and .. directories for subdirectories as well
@filist = grep( !/^\.$/ && !/^\.\.$/ && !/\/\.$/ && !/\/\.\.$/ , @filist) if $f eq '.';
#ignore i, o, a and R options, they have already been taken care of
}
}
$pattern =~ s{^\s*}{};
if( $pattern ){
$pattern = quotemeta $pattern;
$pattern =~ s{\\\*}{\.*}g;
@filist = grep( m/^$pattern$/ , @filist);
}
my @FileArray = ();
for $filename (@filist){
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat("$dir/$filename");
my @params = ();
push @params, ($filename, "$rdir/$filename", $size, &ConvertDate($mtime), &ConvertTime($mtime), $mtime );
if( $html ){
my ($title) = &HtmlParameters("$dir/$filename");
push @params, $title;
}else{ push @params, '' }
push @FileArray, \@params;
}
@FileArray = sort { $a->[0] cmp $b->[0] } @FileArray if $sortorder eq 'na';
@FileArray = sort { $b->[0] cmp $a->[0] } @FileArray if $sortorder eq 'nd';
@FileArray = sort { $a->[6] cmp $b->[6] } @FileArray if $sortorder eq 'ta';
@FileArray = sort { $b->[6] cmp $a->[6] } @FileArray if $sortorder eq 'td';
@FileArray = sort { $a->[2] <=> $b->[2] } @FileArray if $sortorder eq 'sa';
@FileArray = sort { $b->[2] <=> $a->[2] } @FileArray if $sortorder eq 'sd';
@FileArray = sort { $a->[5] <=> $b->[5] } @FileArray if $sortorder eq 'da';
@FileArray = sort { $b->[5] <=> $a->[5] } @FileArray if $sortorder eq 'dd';
my $output = '';
push @LoopMacroStack,$Macro{'$file$name'};
push @LoopMacroStack,$Macro{'$file$url'};
push @LoopMacroStack,$Macro{'$file$size'};
push @LoopMacroStack,$Macro{'$file$date'};
push @LoopMacroStack,$Macro{'$file$time'};
push @LoopMacroStack,$Macro{'$html$title'};
push @LoopMacroStack,$WeAreInLoop;
$WeAreInLoop = 1;
for $f (@FileArray){
$f->[0] =~ m{(.*)\.(.*)};
$Macro{'$file$nam'} = $1;
$Macro{'$file$e'} = $2;
$Macro{'$file$name'} = $f->[0];
$Macro{'$file$url'} = $f->[1];
$Macro{'$file$size'} = $f->[2];
$Macro{'$file$date'} = $f->[3];
$Macro{'$file$time'} = $f->[4];
$Macro{'$html$title'} = $f->[6];
my $bdy = $body;
$bdy = &DoInput(\$bdy,$ReferenceFileName,$level+1);
$output .= $$bdy;
}
$WeAreInLoop = pop @LoopMacroStack;
$Macro{'$html$title'}= pop @LoopMacroStack;
$Macro{'$file$time'} = pop @LoopMacroStack;
$Macro{'$file$date'} = pop @LoopMacroStack;
$Macro{'$file$size'} = pop @LoopMacroStack;
$Macro{'$file$url'} = pop @LoopMacroStack;
$Macro{'$file$name'} = pop @LoopMacroStack;
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
if( $$input =~ /^months(\W(?:.|\n)*)$/ ){
my $macro = $1;
my $list = &Split($macro,12);
$MonthNames = $list;
if( $#$list < 11 ){
return &Error('There are not enough month names defined:' , $$input);
}elsif( $#$list > 11 ){
return &Error('There are too many month names defined:' , $$input);
}
return '';
}
if( $$input =~ /^days(\W(?:.|\n)*)$/ ){
my $macro = $1;
my $list = &Split($macro,7);
$DayNames = $list;
if( $#$list < 6 ){
return &Error('There are not enough day names defined:' , $$input);
}elsif( $#$list > 6 ){
return &Error('There are too many day names defined:' , $$input);
}
return '';
}
if( $$input =~ /^format\s+time=((?:.|\n)*)$/ ){
$TimeFormat = $1;
return '';
}
if( $$input =~ /^format\s+date=((?:.|\n)*)$/ ){
$DateFormat = $1;
return '';
}
if( $$input =~ /^format\s+am=((?:.|\n)*)$/ ){
$AM = $1;
return '';
}
if( $$input =~ /^format\s+pm=((?:.|\n)*)$/ ){
$PM = $1;
return '';
}
# both macros are implemented in a way that pervert users
# including macro reference in the time and date format
# get the correct result
if( $$input =~ /^time\s*$/ ){
my $output = &ConvertTime($timeNow);
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
if( $$input =~ /^date\s*$/ ){
my $output = &ConvertDate($timeNow);
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
# {#select/n/,1,2,3,4,5}
if( $$input =~ /^select(\W(?:.|\n)*)$/ ){
my $macro = $1;
my ($n,$list) = @{&Split($macro,2)};
my @list = @{&Split($list)};
my $output = $list[$n];
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
# {#if/test/then/else}
if( $$input =~ /^if(\W(?:.|\n)*)$/ ){
my $macro = $1;
my ($test,$then,$else) = @{&Split($macro,3)};
my $output = $test ? $then : $else;
$output = &DoInput(\$output,$ReferenceFileName,$level+1);
return $$output;
}
#{#null text}
if( $$input =~ /^null\s*((?:.|\n)*)$/ ){
return $1;
}
#{#comment text}
if( $$input =~ /^comment\s*((?:.|\n)*)$/ ){
return'';
}
# {#op /exp1/exp2/.../expn}
my $f1 = substr($$input,0,1);
my $f2 = substr($$input,0,2);
my $f3 = substr($$input,0,3);
my $match = 0;
my ($op,$macro);
if( $f3 =~ m{and|not} ){
$match = 1;
$op = $f3;
$macro = substr($$input,3);
}
elsif( $f2 =~ m{<=|>=|!=|eq|ne|gt|lt|ge|le|or} ||
$f2 =~ m{-(z|s|d|f|e|t)}i ){
$match = 1;
$op = $f2;
$macro = substr($$input,2);
}
elsif( $f1 =~ m{^(=|<|>|\*|/|\+|-|)$} ){
$match = 1;
$op = $f1;
$macro = substr($$input,1);
}
if( $match ){
my $list = &Split($macro);
my $fop = shift @$list;
my $result = $fop;
# unary file operators
if( $op =~ s/\-(z|s|d|f|e|t)/$1/i ){
if( $op eq lc $op || ! defined $Destination ){
$fop = &Relative($ReferenceFileName,$fop);
}else{
$fop = &Relative($Destination,$fop);
}
$op = lc $op;
if( $op eq 'z' ){
if( -z $fop ){ return '1' }else{ return '0' }
}
if( $op eq 's' ){
if( -s $fop eq '' ){ return '0' }else{ return -s $fop }
}
if( $op eq 'd' ){
if( -d $fop ){ return 1 }else{ return 0 }
}
if( $op eq 'f' ){
if( -f $fop ){ return 1 }else{ return 0 }
}
if( $op eq 'e' ){
if( -e $fop ){ return 1 }else{ return 0 }
}
if( $op eq 't' ){
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat($fop);
return $mtime;
}
#you never get here
}
# unary minus
if( $#$list == -1 && $op eq '-' ){
$result = -1 * $fop;
return $result;
}
# unary plus
if( $#$list == -1 && $op eq '+' ){
$result = +1 * $fop;
return $result;
}
# not
if( $op eq 'not' ){
$result = $fop ? 0 : 1;
return $result if $#$list == -1 || $fop ;
}
return 1 if $op eq 'or' && $fop;
return 0 if $op eq 'and' && !$fop;
if( $#$list == -1 ){
&Error("Operational macro $op has less than two arguments:",$$input);
}
for( @$list ){
if( $op eq '+' ){ $result += $_ }
elsif( $op eq '-' ){ $result -= $_ }
elsif( $op eq '*' ){ $result *= $_ }
elsif( $op eq '/' ){ $result /= $_ }
elsif( $op eq '=' ){ $result = 1; return 0 unless $fop == $_ }
elsif( $op eq '!=' ){ $result = 1; return 0 unless $fop != $_ }
elsif( $op eq '<' ){ $result = 1; return 0 unless $fop < $_ }
elsif( $op eq '>' ){ $result = 1; return 0 unless $fop > $_ }
elsif( $op eq '<=' ){ $result = 1; return 0 unless $fop <= $_ }
elsif( $op eq '>=' ){ $result = 1; return 0 unless $fop >= $_ }
elsif( $op eq 'eq' ){ $result = 1; return 0 unless $fop eq $_ }
elsif( $op eq 'ne' ){ $result = 1; return 0 unless $fop ne $_ }
elsif( $op eq 'lt' ){ $result = 1; return 0 unless $fop lt $_ }
elsif( $op eq 'gt' ){ $result = 1; return 0 unless $fop gt $_ }
elsif( $op eq 'le' ){ $result = 1; return 0 unless $fop le $_ }
elsif( $op eq 'ge' ){ $result = 1; return 0 unless $fop ge $_ }
elsif( $op eq 'and'){ $result = 1; return 0 unless $_ }
elsif( $op eq 'not'){ $result = 1; return 0 if $_ }
elsif( $op eq 'or' ){ $result = 0; return 1 if $_ }
}
# I do not know when will it be needed, but be consistent
# a later extension might result a need.
$result = &DoInput(\$result,$ReferenceFileName,$level+1);
return $$result;
}
if( $$input =~ /^define\s*(?:(\?)\s*|\s+)(\w+)?((?:\:\:\w+)*)((?:.|\n)*?)=((?:.|\n)*)$/ ){
my $condition = $1;
my $base = $2;
my $macro = $3;
my $parameter = $4;
my $value = $5;
$base = $BaseModule unless $base;
$macro = $base . $macro;
&DefineMacro($macro,$parameter,$value) unless $condition && defined $Macro{$macro};
return '';
}
if( $$input =~ /^undef\s+(\w+)\s*$/ ){
delete $Macro{$1};
delete $MacArg{$1};
return '';
}
if( $$input =~ /^INC\s+(.*)$/ ){
my $IncludeDirectory = $1;
$IncludeDirectory =~ s/\s*$//;
$IncludeDirectory = &Relative($ReferenceFileName,$IncludeDirectory);
push @INC,$IncludeDirectory;
return '';
}
if( $$input =~ /^restore\s+(\w+)\s*$/ ){
my $name = $1;
if( defined( $MacroStack{$name} ) ){
$MacArg{$name} = pop @{$MacroStack{$name}};
$Macro{$name} = pop @{$MacroStack{$name}};
}else{
delete $Macro{$1};
delete $MacArg{$1};
}
return '';
}
# this is specially to include code into
# html text
# This macro would not exists if jamalV1.0 had j-sex
if( $$input =~ /^include\s+pre\s+\"?(.*?)\"?$/ ){
my $file = &Relative($ReferenceFileName,$1);
unless( open(F,"<$file") ){
&Error("File $file can not be read.",$file);
exit;
}
my $o_irs = $/; undef $/;
my $f = <F>;
close F;
$/ = $o_irs;
$f =~ s/\</</g;
$f =~ s/\>/>/g;
return $f;
}
# include a file as it is without macro processing
if( $$input =~ /^include\s+verbatim\s+\"?(.*?)\"?$/ ){
my $file = &Relative($ReferenceFileName,$1);
unless( open(F,"<$file") ){
&Error("File $file cann ot be read.",$file);
exit;
}
my $o_irs = $/; undef $/;
my $f = <F>;