PageRenderTime 17ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/choicetool-preprocessor.in

#
Autoconf | 236 lines | 127 code | 44 blank | 65 comment | 15 complexity | 0ac829bc54d71a4b416d2d098018c527 MD5 | raw file
  1#! @PERL@
  2
  3#
  4# choicetool-preprocessor
  5#
  6# Copyright (C) 2008, 2009 Francesco Salvestrini
  7#
  8# This program is free software; you can redistribute it and/or modify
  9# it under the terms of the GNU General Public License as published by
 10# the Free Software Foundation; either version 2 of the License, or
 11# (at your option) any later version.
 12#
 13# This program is distributed in the hope that it will be useful,
 14# but WITHOUT ANY WARRANTY; without even the implied warranty of
 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16# GNU General Public License for more details.
 17#
 18# You should have received a copy of the GNU General Public License along
 19# with this program; if not, write to the Free Software Foundation, Inc.,
 20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 21#
 22
 23eval 'case $# in 0) exec @PERL@ -S "$0";; *) exec @PERL@ -S "$0" "$@";; esac'
 24    if 0;
 25
 26use File::Spec;
 27
 28BEGIN
 29{
 30    # Retrieve our perl libraries path
 31    my $perllibdir;
 32
 33    $perllibdir = defined($ENV{'CHOICETOOL_LIBRARY_PATH'}) ?
 34        $ENV{'CHOICETOOL_LIBRARY_PATH'} : '@pkgvlibsdir@';
 35
 36    unshift(@INC, map(File::Spec->catfile($_, "perl"),
 37                      (split '@PATH_SEPARATOR@', $perllibdir)));
 38
 39    # Override SHELL.  On DJGPP SHELL may not be set to a shell
 40    # that can handle redirection and quote arguments correctly,
 41    # e.g.: COMMAND.COM.  For DJGPP always use the shell that configure
 42    # has detected.
 43    $ENV{'SHELL'} = '@SHELL@' if ($^O eq 'dos');
 44}
 45
 46#
 47# Misc
 48#
 49
 50use strict;
 51use warnings;
 52use diagnostics;
 53
 54use Choicetool::Autoconfig;
 55use Choicetool::Base::Trace;
 56use Choicetool::Base::Debug;
 57use Choicetool::Base::Program;
 58use Choicetool::Base::Options;
 59use Choicetool::OS::File;
 60use Choicetool::OS::String;
 61use Choicetool::OS::Home;
 62use Choicetool::OS::Environment;
 63use Choicetool::Frontends::KConfig::Preprocess;
 64
 65#
 66# Globals
 67#
 68
 69our $PROGRAM_NAME = "choicetool-preprocessor";
 70
 71my $OUTBASE = "choose";
 72my $DFLTIN  = $OUTBASE . ".ct";
 73my $DFLTOUT = $OUTBASE . ".pp";
 74
 75sub help_environment ()
 76{
 77    print "The environment variables CHOICETOOL_BINARY_PATH, CHOICETOOL_LIBRARY_PATH are\n";
 78    print "honored.\n";
 79}
 80
 81sub help ()
 82{
 83    print "Usage: $PROGRAM_NAME [OPTIONS]\n";
 84    print "\n";
 85    print "Preprocess an input file ifgiven, or \`$DFLTIN' if present. Output\n";
 86    print "is sent to the output file if it is given, otherwise into \`$DFLTOUT'.\n";
 87    print "\n";
 88    print "  -i, --input=FILE           get input from FILE\n";
 89    print "  -o, --output=FILE          output to file FILE\n";
 90    print "  -n, --dry-run              display actions without modifying any files\n";
 91    print "  -W, --warnings=CATEGORY    report the warnings falling in CATEGORY\n";
 92    print "  -d, --debug                run in debugging mode\n";
 93    print "  -v, --verbose              verbose mode\n";
 94    print "  -h, --help                 print this help, then exit\n";
 95    print "  -V, --version              print version number, then exit\n";
 96    print "\n";
 97
 98    help_environment();
 99
100    print "\n";
101    print "Report bugs to <$PACKAGE_BUGREPORT>\n";
102}
103
104#
105# Main
106#
107
108trace_prefix_set($PROGRAM_NAME);
109
110my $input_file   = "";
111my $output_file  = "";
112my $force_mode   = 0;
113my $dry_run      = 0;
114my $run          = 1;
115
116debug_set(0);
117verbose_set(0);
118warning_set("none");
119
120my $options = Choicetool::Base::Options->new();
121assert(defined($options));
122
123my @options_list = (
124    [ "i", "input",    sub { $input_file = $_[0];          return 1; }, 1 ],
125    [ "o", "output",   sub { $output_file = $_[0];         return 1; }, 1 ],
126    [ "n", "dry-run",  sub { $dry_run = 1;                 return 1; }, 0 ],
127    [ "f", "force",    sub { $force_mode = 1;              return 1; }, 0 ],
128    [ "W", "warnings", sub { warning_set($_[0]);           return 1; }, 1 ],
129    [ "d", "debug",    sub { debug_inc();                  return 1; }, 0 ],
130    [ "v", "verbose",  sub { verbose_inc();                return 1; }, 0 ],
131    [ "h", "help",     sub { help();             $run = 0; return 0; }, 0 ],
132    [ "V", "version",  sub { version();          $run = 0; return 0; }, 0 ],
133    );
134
135if (!$options->config(\@options_list)) {
136    bug("Options configuration problem (" . $options->strerror() . ")");
137}
138if (!$options->parse(\@ARGV)) {
139    hint($options->strerror());
140    exit 1;
141}
142
143my @options_slack;
144
145assert($options->{OPTIND} >= 0);
146@options_slack = @ARGV[$options->{OPTIND} .. $#ARGV];
147
148debug("Running \`" . $PROGRAM_NAME . "' version \`" . $PACKAGE_VERSION . "'");
149
150#
151# Parameters check
152#
153
154if (!$run) {
155    exit 0;
156}
157
158if ($input_file eq "") {
159    $input_file = $DFLTIN;
160}
161
162if ($output_file eq "") {
163    $output_file = $DFLTOUT;
164}
165
166if (!file_ispresent($input_file)) {
167    error("Input file \`" . $input_file . "' is missing");
168    exit 1;
169}
170
171assert(defined($input_file));
172assert(defined($output_file));
173
174#
175# Some preliminary check(s)
176#
177
178if (!$force_mode) {
179    if (!file_isnewer($input_file, $output_file)) {
180        warning("Output file "              .
181                "\`" . $output_file . "' "  .
182                "seems not to be obsolete " .
183                "(input file is "           .
184                "\`" . $input_file . "'). " .
185                "Use --force to rebuild");
186        exit 0;
187    }
188}
189
190##
191## Dump the environment, useful for debugging purposes
192##
193#{
194#    sub callback ($$)
195#    {
196#        my $key   = shift;
197#        my $value = shift;
198#
199#        if (!defined($key)) {
200#            $key = "undef";
201#        }
202#        if (!defined($value)) {
203#            $value = "undef";
204#        }
205#        debug("Environment `" . $key . "' = `" . $value . "'")
206#    }
207#    environment_foreach(\&callback)
208#}
209
210#
211# Preprocess the input file
212#
213
214my $string;
215
216$string = "";
217
218if (!Choicetool::Frontends::KConfig::Preprocess::preprocess($input_file,
219                                                            \$string)) {
220    error("Failed to preprocess file `" . $input_file . "\'");
221    exit 1;
222}
223
224#
225# Write output file at last
226#
227
228if ($dry_run) {
229    exit 0;
230}
231
232if (!string_tofile($string, $output_file)) {
233    exit 1;
234}
235
236exit 0;