PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/choicetool-preprocessor.in

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