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/>.
30 $SIG{'__WARN__'} = \&warning;
32 Getopt::Long::Configure("no_ignore_case", "bundling", "gnu_compat",
34 if (not GetOptions(\%opts,
45 if (exists($opts{'h'})) {
49 if (exists($opts{'V'})) {
55 error(4, "No input files\n");
57 if (exists($opts{'o'})) {
59 error(4, "Cannot specify -o with multiple files\n");
61 compile($ARGV[0], $opts{'o'}, $opts{'D'}, $opts{'I'});
65 $output =~ s/\.[^.]+$/.html/;
66 compile($input, $output, $opts{'D'}, $opts{'I'});
75 printf($fh "Usage: %s [-o <output>] <input> ...\n", $0);
84 print(" -D <name>=<value> Set the variable <name> to <value>\n");
85 print(" -I <directory> Set the document root to <directory>\n");
86 print(" -o <output> Place the output into <output>\n");
87 print(" -h, --help Display this information\n");
88 print(" -V, --version Display compiler version information\n");
95 print("ssic 0.1.0\n");
96 print("Copyright (C) 2013 Patrick \"P. J.\" McDermott\n");
97 print("License GPLv3+: GNU GPL version 3 or later " .
98 "<http://gnu.org/licenses/gpl.html>.\n");
99 print("This is free software: you are free to change and " .
100 "redistribute it.\n");
101 print("There is NO WARRANTY, to the extent permitted by law.\n");
106 my ($fmt, @args) = @_;
108 printf(STDERR "ssic: Warning: " . $fmt, @args);
113 my ($status, $fmt, @args) = @_;
115 printf(STDERR "ssic: Error: " . $fmt, @args);
121 my ($input, $output, $vars, $root) = @_;
129 if ($input eq $output and $input ne "-") {
130 warning("Input and output files are equal\n");
135 $input_abs = File::Spec->rel2abs(".");
137 if (not open($input_fh, "<", $input)) {
138 error(4, "%s: %s\n", $input, $!);
140 $input_abs = File::Spec->rel2abs($input);
142 if ($output eq "-") {
143 $output_fh = *STDOUT;
145 if (not open($output_fh, ">", $output . "~")) {
146 error(4, "%s: %s\n", $output . "~", $!);
150 # CGI::SSI uses SCRIPT_FILENAME to determine the value of LAST_MODIFIED.
152 "DOCUMENT_NAME" => $input,
153 "DOCUMENT_URI" => $input,
154 "DOCUMENT_ROOT" => $root,
155 "SCRIPT_FILENAME" => $input_abs,
158 $CGI::SSI::DEBUG = 0;
159 $ssi = CGI::SSI->new(
160 "DOCUMENT_NAME" => $input,
161 "DOCUMENT_URI" => $input,
162 "DOCUMENT_ROOT" => $root,
165 while (($var_name, $var_value) = each(%{$vars})) {
166 $ssi->set($var_name => $var_value);
169 print($output_fh $ssi->process(<$input_fh>));
174 if ($output ne "-") {
176 if (not rename($output . "~", $output)) {
177 error(4, "%s: %s\n", $output, $!);