3 # Copyright (C) 2013 Patrick "P. J." McDermott
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 $SIG{'__WARN__'} = \&warning;
33 Getopt::Long::Configure("no_ignore_case", "bundling", "gnu_compat",
35 if (not GetOptions(\%opts,
46 if (exists($opts{'h'})) {
50 if (exists($opts{'V'})) {
56 error(4, "No input files\n");
59 $ssi = init_compiler($opts{'D'}, $opts{'I'});
61 if (exists($opts{'o'})) {
63 error(4, "Cannot specify -o with multiple files\n");
65 compile($ssi, $ARGV[0], $opts{'o'});
69 $output =~ s/\.[^.]+$/.html/;
70 compile($ssi, $input, $output);
81 printf($fh "Usage: %s [-o <output>] <input> ...\n", $0);
90 print(" -D <name>=<value> Set the variable <name> to <value>\n");
91 print(" -I <directory> Set the document root to <directory>\n");
92 print(" -o <output> Place the output into <output>\n");
93 print(" -h, --help Display this information\n");
94 print(" -V, --version Display compiler version information\n");
101 print("ssic 0.1.0\n");
102 print("Copyright (C) 2013 Patrick \"P. J.\" McDermott\n");
103 print("License GPLv3+: GNU GPL version 3 or later " .
104 "<http://gnu.org/licenses/gpl.html>.\n");
105 print("This is free software: you are free to change and " .
106 "redistribute it.\n");
107 print("There is NO WARRANTY, to the extent permitted by law.\n");
112 my ($fmt, @args) = @_;
114 printf(STDERR "ssic: Warning: " . $fmt, @args);
119 my ($status, $fmt, @args) = @_;
121 printf(STDERR "ssic: Error: " . $fmt, @args);
127 my ($vars, $root) = @_;
133 "DOCUMENT_ROOT" => $root,
136 $CGI::SSI::DEBUG = 0;
137 $ssi = CGI::SSI->new();
139 $ssi->set("DOCUMENT_ROOT" => $root);
141 while (($var_name, $var_value) = each(%{$vars})) {
142 $ssi->set($var_name => $var_value);
150 my ($ssi, $input, $output) = @_;
155 if ($input eq $output and $input ne "-") {
156 warning("Input and output files are equal\n");
161 $input_abs = File::Spec->rel2abs(".");
163 if (not open($input_fh, "<", $input)) {
164 error(4, "%s: %s\n", $input, $!);
166 $input_abs = File::Spec->rel2abs($input);
168 if ($output eq "-") {
169 $output_fh = *STDOUT;
171 if (not open($output_fh, ">", $output . "~")) {
172 error(4, "%s: %s\n", $output . "~", $!);
176 # CGI::SSI uses SCRIPT_FILENAME to determine the value of LAST_MODIFIED.
177 $ENV{"DOCUMENT_NAME"} = $input;
178 $ENV{"DOCUMENT_URI"} = $input;
179 $ENV{"SCRIPT_FILENAME"} = $input_abs;
181 $ssi->set("DOCUMENT_NAME" => $input);
182 $ssi->set("DOCUMENT_URI" => $input);
184 print($output_fh $ssi->process(<$input_fh>));
189 if ($output ne "-") {
191 if (not rename($output . "~", $output)) {
192 error(4, "%s: %s\n", $output, $!);