Convert to Getopt::Long.
[ssic.git] / src / ssic.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use CGI::SSI;
8
9 sub main
10 {
11         my %opts;
12         my $input;
13         my $output;
14
15         $SIG{'__WARN__'} = \&warning;
16
17         Getopt::Long::Configure("no_ignore_case", "bundling", "gnu_compat",
18                 "no_getopt_compat");
19         if (not GetOptions(\%opts,
20                         "o=s",
21                         "h|help",
22                         "V|version",
23                 )) {
24                 usage(*STDERR);
25         }
26
27         if (exists($opts{'h'})) {
28                 help(*STDERR);
29                 exit(0);
30         }
31         if (exists($opts{'V'})) {
32                 version(*STDERR);
33                 exit(0);
34         }
35
36         if ($#ARGV lt 0) {
37                 error(4, "No input files\n");
38         }
39         if (exists($opts{'o'})) {
40                 if ($#ARGV gt 0) {
41                         error(4, "Cannot specify -o with multiple files\n");
42                 }
43                 compile($ARGV[0], $opts{'o'});
44         } else {
45                 for $input (@ARGV) {
46                         $output = $input;
47                         $output =~ s/\.[^.]+$/.html/;
48                         compile($input, $output);
49                 }
50         }
51 }
52
53 sub usage
54 {
55         my ($fh) = @_;
56
57         printf($fh "Usage: %s [-o <output>] <input> ...\n", $0);
58 }
59
60 sub help
61 {
62         my ($fh) = @_;
63
64         usage($fh);
65         print("Options:\n");
66         print("  -o <output>    Place the output into <output>\n");
67         print("  -h, --help     Display this information\n");
68         print("  -V, --version  Display compiler version information\n");
69 }
70
71 sub version
72 {
73         my ($fh) = @_;
74
75         print("ssic 0.1.0\n");
76         print("Copyright (C) 2013 Patrick \"P. J.\" McDermott\n");
77         print("License GPLv3+: GNU GPL version 3 or later " .
78                 "<http://gnu.org/licenses/gpl.html>.\n");
79         print("This is free software: you are free to change and " .
80                 "redistribute it.\n");
81         print("There is NO WARRANTY, to the extent permitted by law.\n");
82 }
83
84 sub warning
85 {
86         my ($fmt, $args) = @_;
87
88         printf("ssic: Warning: " . $fmt, $args);
89 }
90
91 sub error
92 {
93         my ($status, $fmt, $args) = @_;
94
95         printf("ssic: Error: " . $fmt, $args);
96         exit($status);
97 }
98
99 sub compile
100 {
101         my ($input, $output) = @_;
102         my $input_fh;
103         my $output_fh;
104         my $ssi;
105
106         if ($input eq $output) {
107                 error(4, "Input and output files are equal\n");
108         }
109
110         open($input_fh, "<", $input);
111         open($output_fh, ">", $output);
112
113         $CGI::SSI::DEBUG = 0;
114         $ssi = CGI::SSI->new();
115
116         print($output_fh $ssi->process(<$input_fh>));
117 }
118
119 main();