summaryrefslogtreecommitdiff
path: root/web/glasgow/lit2x-0.16/literate/lit-inputter.prl
blob: 9f3e1deca0fb07c4bd2a655e60bdd53b52dc3f33 (plain)
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
($Pgm = $0) =~ s/.*\/([^\/]+)$/\1/;
$Verbose       = shift(@ARGV); # they had better be there!
$Follow_inputs = shift(@ARGV);
$Litinputs     = shift(@ARGV);
@Litinput      = split(/:/,$Litinputs);
$Exit_status   = 0; # everything OK

# read STDIN ("-") if no files specified
#
unshift(@ARGV, '-') if $#ARGV < $[;
while ($ARGV = shift) {
    &send_file($ARGV, 'fh00');
}
exit $Exit_status;

sub send_file { # follows an example in the `open' item in perl man page

    local($fname,$fhandle) = @_;
    local($l_no) = 1;
    local($follow_file);
    $fhandle++; # a string increment

    local($last_seen_dir) = $fname;
    $last_seen_dir =~ s/\/[^\/]+$//; # strip to dir name
    $last_seen_dir = '.' if ($last_seen_dir eq $fname);

    unless (open($fhandle, $fname)) {
         print STDERR "$Pgm: Can't open $fname: $!\n";
         $Exit_status++;
         return;
    }

    local($is_intermed_file) = ($fname =~ /\.(itxi|itex)$/) ? 1 : 0;

    while (<$fhandle>) {
        if ($l_no == 1 && ! $is_intermed_file) { # beginning
            print "srcfile!_!$fname!_!1!_!\n";
        }

        # we do *nothing* to rawlatex environments
        if ( /^\\begin\{rawlatex\}/ || /\001beginrawlatex\003/ ) {
            while ($_ ne '' && ! (/^\\end\{rawlatex\}/ || /\001endrawlatex\003/ )) {
                print $_;
                $_ = <$fhandle>;
            }
        }

        if (/^\\inputnow\{(.*)\}\s*(%.*)?$/ # dubious ...
          || ($Follow_inputs
              && (/^\\input\s*\{(.*)\}\s*(%.*)?$/
                 || /^\@input\s*\{(.*\.itxi)\}\s*$/))) {
            $follow_file = &find_on_Litinputs($1, $last_seen_dir);

            if ($follow_file) { # it found something
                print STDERR "reading $follow_file ...\n" if $Verbose;
                &send_file($follow_file,$fhandle);
                # add a couple of blank lines in case
                # the last thing was some Miranda-style code
                print "\n\n\n";
                print "srcfile!_!$fname!_!".($l_no+1)."!_!\n" if ! $is_intermed_file;
            } else {
                print STDERR "$fname:$l_no: Couldn't handle: $_";
                $Exit_status++;
            }
        } else {
            print $_;
        }
    } continue { $l_no++; }
    close($fhandle);
}

sub find_on_Litinputs {
    local($name, $last_seen_dir) = @_;
    local($lit);

    if ($name =~ /^\//) { # absolute pathname
        print STDERR "trying $name...\n" if $Verbose;
        if (-f $name) { # exists
            return($name);
        } else {
            print STDERR " file `$name' doesn't exist\n";
            $Exit_status++;
            return('');
        }
    } else { # hop along $Litinputs
        foreach $lit (@Litinput) {
            # handle . magically
            if ($lit eq '.') { # SPECIAL try for a file in last_seen_dir
                
                print STDERR "trying $last_seen_dir/$name...\n" if $Verbose;
                if (-f "$last_seen_dir/$name") {
                    return("$last_seen_dir/$name");
                }
            }
            print STDERR "trying $lit/$name...\n" if $Verbose;
            if (-f "$lit/$name") {
                return("$lit/$name");
            }
        }
        $Exit_status++;
        print STDERR "No file `$name' along LITINPUTS path `$Litinputs'\n";
        return (''); # sigh
    }
}