3 # ssic - Server Side Includes Compiler
5 # Copyright (C) 2013 Patrick "P. J." McDermott
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
33 $SIG{'__WARN__'} = \&warning;
35 Getopt::Long::Configure("no_ignore_case", "bundling", "gnu_compat",
37 if (not GetOptions(\%opts,
48 if (exists($opts{'h'})) {
52 if (exists($opts{'V'})) {
58 error(4, "No input files\n");
61 $ssi = init_compiler($opts{'D'}, $opts{'I'});
63 if (exists($opts{'o'})) {
65 error(4, "Cannot specify -o with multiple files\n");
67 compile($ssi, $ARGV[0], $opts{'o'});
71 $output =~ s/\.[^.]+$/.html/;
72 compile($ssi, $input, $output);
83 printf($fh "Usage: %s [-o <output>] <input> ...\n", $0);
92 print(" -D <name>=<value> Set the variable <name> to <value>\n");
93 print(" -I <directory> Set the document root to <directory>\n");
94 print(" -o <output> Place the output into <output>\n");
95 print(" -h, --help Display this information\n");
96 print(" -V, --version Display compiler version information\n");
103 print("ssic 0.1.0\n");
104 print("Copyright (C) 2013 Patrick \"P. J.\" McDermott\n");
105 print("License GPLv3+: GNU GPL version 3 or later " .
106 "<http://gnu.org/licenses/gpl.html>.\n");
107 print("This is free software: you are free to change and " .
108 "redistribute it.\n");
109 print("There is NO WARRANTY, to the extent permitted by law.\n");
114 my ($fmt, @args) = @_;
116 printf(STDERR "ssic: Warning: " . $fmt, @args);
121 my ($status, $fmt, @args) = @_;
123 printf(STDERR "ssic: Error: " . $fmt, @args);
129 my ($vars, $root) = @_;
135 "DOCUMENT_ROOT" => $root,
138 $CGI::SSI::DEBUG = 0;
139 $ssi = CGI::SSI->new();
141 $ssi->set("DOCUMENT_ROOT" => $root);
143 while (($var_name, $var_value) = each(%{$vars})) {
144 $ssi->set($var_name => $var_value);
152 my ($ssi, $input, $output) = @_;
157 if ($input eq $output and $input ne "-") {
158 warning("Input and output files are equal\n");
163 $input_abs = File::Spec->rel2abs(".");
165 if (not open($input_fh, "<", $input)) {
166 error(4, "%s: %s\n", $input, $!);
168 $input_abs = File::Spec->rel2abs($input);
170 if ($output eq "-") {
171 $output_fh = *STDOUT;
173 if (not open($output_fh, ">", $output . "~")) {
174 error(4, "%s: %s\n", $output . "~", $!);
178 # CGI::SSI uses SCRIPT_FILENAME to determine the value of LAST_MODIFIED.
179 $ENV{"DOCUMENT_NAME"} = $input;
180 $ENV{"DOCUMENT_URI"} = $input;
181 $ENV{"SCRIPT_FILENAME"} = $input_abs;
183 # Reset config tags to default values.
184 $ssi->config("errmsg",
185 "[an error occurred while processing this directive]");
186 $ssi->config("sizefmt", "abbrev");
187 $ssi->config("timefmt", undef);
189 $ssi->set("DOCUMENT_NAME" => $input);
190 $ssi->set("DOCUMENT_URI" => $input);
192 print($output_fh $ssi->process(<$input_fh>));
197 if ($output ne "-") {
199 if (not rename($output . "~", $output)) {
200 error(4, "%s: %s\n", $output, $!);