PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/choicetool-parser.in

#
Autoconf | 292 lines | 160 code | 59 blank | 73 comment | 17 complexity | a0a3277dbf8c24956ee82d59c20f044e MD5 | raw file
Possible License(s): GPL-2.0
  1. #! @PERL@
  2. #
  3. # choicetool-parser
  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 Data::Dumper;
  45. use Choicetool::Autoconfig;
  46. use Choicetool::Base::Trace;
  47. use Choicetool::Base::Debug;
  48. use Choicetool::Base::Program;
  49. use Choicetool::Base::Options;
  50. use Choicetool::OS::File;
  51. use Choicetool::OS::String;
  52. use Choicetool::OS::Home;
  53. use Choicetool::OS::Environment;
  54. use Choicetool::Frontends::KConfig::Parse;
  55. use Choicetool::Data::Tree;
  56. use Choicetool::Widgets::Widget;
  57. use Choicetool::Data::Set;
  58. #
  59. # Globals
  60. #
  61. our $PROGRAM_NAME = "choicetool-parser";
  62. my $OUTBASE = "choose";
  63. my $DFLTIN = $OUTBASE . ".pp";
  64. my $DFLTOUT = $OUTBASE . ".pa";
  65. sub help_environment ()
  66. {
  67. print "The environment variables CHOICETOOL_BINARY_PATH, CHOICETOOL_LIBRARY_PATH are\n";
  68. print "honored.\n";
  69. }
  70. sub help ()
  71. {
  72. print "Usage: $PROGRAM_NAME [OPTIONS]\n";
  73. print "\n";
  74. print "Parse an input file if given, or \`$DFLTIN' if present. Output is\n";
  75. print "sent to the output file if it is given, otherwise into \`$DFLTOUT'.\n";
  76. print "\n";
  77. print " -i, --input=FILE get input from FILE\n";
  78. print " -o, --output=FILE output to file FILE\n";
  79. print " -n, --dry-run display actions without modifying any files\n";
  80. print " -W, --warnings=CATEGORY report the warnings falling in CATEGORY\n";
  81. print " -d, --debug run in debugging mode\n";
  82. print " -v, --verbose verbose mode\n";
  83. print " -h, --help print this help, then exit\n";
  84. print " -V, --version print version number, then exit\n";
  85. print "\n";
  86. help_environment();
  87. print "\n";
  88. print "Report bugs to <$PACKAGE_BUGREPORT>\n";
  89. }
  90. #
  91. # Main
  92. #
  93. trace_prefix_set($PROGRAM_NAME);
  94. my $input_file = "";
  95. my $output_file = "";
  96. my $running_mode = "";
  97. my $force_mode = 0;
  98. my $dry_run = 0;
  99. my $run = 1;
  100. debug_set(0);
  101. verbose_set(0);
  102. warning_set("none");
  103. my $options = Choicetool::Base::Options->new();
  104. assert(defined($options));
  105. my @options_list = (
  106. [ "i", "input", sub { $input_file = $_[0]; return 1; }, 1 ],
  107. [ "o", "output", sub { $output_file = $_[0]; return 1; }, 1 ],
  108. [ "n", "dry-run", sub { $dry_run = 1; return 1; }, 0 ],
  109. [ "f", "force", sub { $force_mode = 1; return 1; }, 0 ],
  110. [ "W", "warnings", sub { warning_set($_[0]); return 1; }, 1 ],
  111. [ "d", "debug", sub { debug_inc(); return 1; }, 0 ],
  112. [ "v", "verbose", sub { verbose_inc(); return 1; }, 0 ],
  113. [ "h", "help", sub { help(); $run = 0; return 0; }, 0 ],
  114. [ "V", "version", sub { version(); $run = 0; return 0; }, 0 ],
  115. );
  116. if (!$options->config(\@options_list)) {
  117. bug("Options configuration problem (" . $options->strerror() . ")");
  118. }
  119. if (!$options->parse(\@ARGV)) {
  120. hint($options->strerror());
  121. exit 1;
  122. }
  123. my @options_slack;
  124. assert($options->{OPTIND} >= 0);
  125. @options_slack = @ARGV[$options->{OPTIND} .. $#ARGV];
  126. debug("Running \`" . $PROGRAM_NAME . "' version \`" . $PACKAGE_VERSION . "'");
  127. #
  128. # Parameters check
  129. #
  130. if (!$run) {
  131. exit 0;
  132. }
  133. if ($input_file eq "") {
  134. $input_file = $DFLTIN;
  135. }
  136. if ($output_file eq "") {
  137. $output_file = $DFLTOUT;
  138. }
  139. if (!file_ispresent($input_file)) {
  140. error("Input file \`" . $input_file . "' is missing");
  141. exit 1;
  142. }
  143. assert(defined($input_file));
  144. assert(defined($output_file));
  145. #
  146. # Some preliminary check(s)
  147. #
  148. if (!$force_mode) {
  149. if (!file_isnewer($input_file, $output_file)) {
  150. warning("Output file " .
  151. "\`" . $output_file . "' " .
  152. "seems not to be obsolete " .
  153. "(input file is " .
  154. "\`" . $input_file . "'). " .
  155. "Use --force to rebuild");
  156. exit 0;
  157. }
  158. }
  159. ##
  160. ## Dump the environment, useful for debugging purposes
  161. ##
  162. #{
  163. # sub callback ($$)
  164. # {
  165. # my $key = shift;
  166. # my $value = shift;
  167. #
  168. # if (!defined($key)) {
  169. # $key = "undef";
  170. # }
  171. # if (!defined($value)) {
  172. # $value = "undef";
  173. # }
  174. # debug("Environment `" . $key . "' = `" . $value . "'")
  175. # }
  176. # environment_foreach(\&callback)
  177. #}
  178. #
  179. # Parse the input file
  180. #
  181. my $ui;
  182. my $set;
  183. $ui = Choicetool::Widgets::Widget->new(0);
  184. assert(defined($ui));
  185. $set = Choicetool::Data::Set->new();
  186. assert(defined($set));
  187. my $string;
  188. $string = "";
  189. if (!Choicetool::Frontends::KConfig::Parse::parse($input_file, \$ui, \$set)) {
  190. error("Failed to parse file `" . $input_file . "\'");
  191. exit 1;
  192. }
  193. assert(defined($ui));
  194. {
  195. my $level;
  196. debug("Menu tree:");
  197. $level = 0;
  198. $ui->foreach(
  199. sub {
  200. my $node_ref = shift;
  201. assert(defined($node_ref));
  202. debug(" Menu");
  203. debug(" ID: \`" . ${$node_ref}->id() . "'");
  204. my $parent_ref;
  205. $parent_ref = ${$node_ref}->parent();
  206. if (defined($parent_ref)) {
  207. debug(" Parent ID: \`" . ${$parent_ref}->id() . "'");
  208. }
  209. debug("");
  210. }
  211. );
  212. }
  213. #
  214. # NOTE: Tree packing will be performed later on
  215. #
  216. #
  217. # Freeze the parsed data structure to string
  218. #
  219. my %data;
  220. $data{TREE} = $ui;
  221. $string = Data::Dumper->Dump([ \%data ], [ qw(*data) ]);
  222. #debug("Frozen data:");
  223. #debug("\`" . $string . "'");
  224. #
  225. # Write output file at last
  226. #
  227. if ($dry_run) {
  228. exit 0;
  229. }
  230. if (!string_tofile($string, $output_file)) {
  231. exit 1;
  232. }
  233. exit 0;