home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / scripts / pearize.in < prev    next >
Text File  |  2001-08-28  |  5KB  |  226 lines

  1. #!@prefix@/bin/php -Cq
  2. <?php // -*- PHP -*-
  3.  
  4. main($argc, $argv, $_ENV);
  5.  
  6. // {{{ main()
  7.  
  8. function main(&$argc, &$argv, &$env)
  9. {
  10.     global $debug;
  11.     $debug = false;
  12.     $file = check_options($argc, $argv, $env);
  13.     parse_package_file($file);
  14.     make_makefile_in($env);
  15. }
  16.  
  17. // }}}
  18. // {{{ check_options()
  19.  
  20. function check_options($argc, $argv, $env)
  21. {
  22.     global $debug;
  23.     array_shift($argv);
  24.     while ($argv[0]{0} == '-' && $argv[0] != '-') {
  25.         $opt = array_shift($argv);
  26.         switch ($opt) {
  27.             case '--': {
  28.                 break 2;
  29.             }
  30.             case '-d': {
  31.                 $debug = true;
  32.                 break;
  33.             }
  34.             default: {
  35.                 die("pearize: unrecognized option `$opt'\n");
  36.             }
  37.         }
  38.     }
  39.     $file = array_shift($argv);
  40.     if (empty($file)) {
  41.         $file = "package.xml";
  42.     } elseif ($file == '-') {
  43.         $file = "php://stdin";
  44.     }
  45.     return $file;
  46. }
  47.  
  48. // }}}
  49. // {{{ make_makefile_in()
  50.  
  51. function make_makefile_in(&$env)
  52. {
  53.     global $libdata, $debug;
  54.     if (sizeof($libdata) > 1) {
  55.         die("No support yet for multiple libraries in one package.\n");
  56.     }
  57.  
  58.     if ($debug) {
  59.         $wp = fopen("php://stdout", "w");
  60.     } else {
  61.         $wp = @fopen("Makefile.in", "w");
  62.     }
  63.     if (is_resource($wp)) {
  64.         print "Creating Makefile.in...";
  65.         flush();
  66.     } else {
  67.         die("Could not create Makefile.in in current directory.\n");
  68.     }
  69.  
  70.     $who = $env["USER"];
  71.     $when = gmdate('Y-m-d h:i');
  72.     fwrite($wp, "# This file was generated by `pearize' by $who at $when GMT\n\n");
  73.  
  74.     foreach ($libdata as $lib => $info) {
  75.         extract($info);
  76.         fwrite($wp, "\
  77. INCLUDES                = $includes
  78. LTLIBRARY_NAME            = lib{$lib}.la
  79. LTLIBRARY_SOURCES        = $sources
  80. LTLIBRARY_SHARED_NAME    = {$lib}.la
  81. LTLIBRARY_SHARED_LIBADD = $libadd
  82. ");
  83.     }
  84.  
  85.     if (sizeof($libdata) > 0) {
  86.         fwrite($wp, "include \$(top_srcdir)/build/dynlib.mk\n");
  87.     }
  88.     fclose($wp);
  89.     print "done.\n";
  90. }
  91.  
  92. // }}}
  93. // {{{ parse_package_file()
  94.  
  95. function parse_package_file($file)
  96. {
  97.     global $in_file, $curlib, $curelem, $libdata, $cdata;
  98.     global $currinstalldir, $baseinstalldir;
  99.     
  100.     $in_file = false;
  101.     $curlib = '';
  102.     $curelem = '';
  103.     $libdata = array();
  104.     $cdata = array();
  105.     $baseinstalldir = array();
  106.     $currinstalldir = array();
  107.     
  108.     $xp = xml_parser_create();
  109.     xml_set_element_handler($xp, "start_handler", "end_handler");
  110.     xml_set_character_data_handler($xp, "cdata_handler");
  111.     xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, true);
  112.     
  113.     $fp = @fopen($file, "r");
  114.     if (!is_resource($fp)) {
  115.         die("Could not open file `$file'.\n");
  116.     }
  117.     while (!feof($fp)) {
  118.         xml_parse($xp, fread($fp, 2048), feof($fp));
  119.     }
  120.     xml_parser_free($xp);
  121. }
  122.  
  123. // }}}
  124. // {{{ start_handler()
  125.  
  126. function start_handler($xp, $elem, $attrs)
  127. {
  128.     global $cdata, $in_file, $curelem, $curfile, $filerole;
  129.     global $baseinstalldir, $currinstalldir;
  130.     switch ($elem) {
  131.         case "FILE": {
  132.             $curfile = '';
  133.             $filerole = $attrs['ROLE'];
  134.             switch ($filerole) {
  135.                 case "ext": {
  136.                     $in_file = true;
  137.                     $cdata = array();
  138.                     break;
  139.                 }
  140.                 case "php": default: {
  141.                     break;
  142.                 }
  143.             }
  144.             break;
  145.         }
  146.         case "DIR": {
  147.             $cdir = $currinstalldir[sizeof($currinstalldir)-1];
  148.             $bdir = $baseinstalldir[sizeof($baseinstalldir)-1];
  149.             array_push($currinstalldir, "$cdir/{$attrs[NAME]}");
  150.             if (isset($attrs["BASEINSTALLDIR"])) {
  151.                 array_push($baseinstalldir, "$bdir/{$attrs[BASEINSTALLDIR]}");
  152.             } else {
  153.                 array_push($baseinstalldir, $bdir);
  154.             }
  155.             break;
  156.         }
  157.         case "INCLUDES":
  158.         case "LIBNAME":
  159.         case "LIBADD":
  160.         case "SOURCES": {
  161.             $curelem = $elem;
  162.             break;
  163.         }
  164.     }
  165. }
  166.  
  167. // }}}
  168. // {{{ end_handler()
  169.  
  170. function end_handler($xp, $elem)
  171. {
  172.     global $in_file, $curlib, $curelem, $libdata, $cdata;
  173.     global $baseinstalldir, $currinstalldir;
  174.     switch ($elem) {
  175.         case "FILE": {
  176.             if ($in_file === true) {
  177.                 $libname = trim($cdata['LIBNAME']);
  178.                 $libdata[$libname] = array(
  179.                     "sources" => trim($cdata['SOURCES']),
  180.                     "includes" => trim($cdata['INCLUDES']),
  181.                     "libadd" => trim($cdata['LIBADD']),
  182.                 );
  183.                 $in_file = false;
  184.             }
  185.             break;
  186.         }
  187.         case "DIR": {
  188.             array_pop($currinstalldir);
  189.             array_pop($baseinstalldir);
  190.             break;
  191.         }
  192.     }
  193. }
  194.  
  195. // }}}
  196. // {{{ cdata_handler()
  197.  
  198. function cdata_handler($xp, $data)
  199. {
  200.     global $curelem, $cdata, $curfile;
  201.     switch ($curelem) {
  202.         case "FILE": {
  203.             $curfile .= $data;
  204.             break;
  205.         }
  206.         case "INCLUDES":
  207.         case "LIBADD":
  208.         case "LIBNAME":
  209.         case "SOURCES": {
  210.             $cdata[$curelem] .= $data;
  211.             break;
  212.         }
  213.     }
  214. }
  215.  
  216. // }}}
  217.  
  218. /*
  219.  * Local variables:
  220.  * tab-width: 4
  221.  * c-basic-offset: 4
  222.  * indent-tabs-mode: t
  223.  * End:
  224.  */
  225. ?>
  226.