Visit this project's SourceForge Project Page
Main Page | Class List | Directories | File List | Class Members | File Members | Related Pages

phpcompiler.php

Go to the documentation of this file.
00001 <?php
00002 
00065 function Main($argv)
00066 {
00067     // Get compiler itself
00068     $class_name = require_once("PhpCompiler.class.php");
00069     $compiler = new $class_name();
00070 
00071     // Get option details
00072     $modes = $compiler->GetModes();
00073     $mode_by_name = array();
00074     foreach($modes as $mode)
00075     {
00076         $mode_by_name[$mode["Name"]] = $mode["Default"];
00077     }
00078 
00079     // Pull self off list
00080     array_shift($argv);
00081 
00082     // Parse list of arguments
00083     $InFile     = array();
00084     $OutFile    = array();
00085     $show_usage = FALSE;
00086     while($argv)
00087     {
00088         $arg = array_shift($argv);
00089 
00090         if("-o" == $arg)
00091         {
00092             $OutFile[] = array_shift($argv);
00093         }
00094         elseif(preg_match("/^--(.*?)=(.*)$/", $arg, $match))
00095         {
00096             $arg = $match[1];
00097             $val = $match[2];
00098 
00099             if(isset($mode_by_name[$arg]))
00100             {
00101                 if("TRUE" === $val)
00102                 {
00103                     $mode_by_name[$arg] = TRUE;
00104                 }
00105                 elseif("FALSE" === $val)
00106                 {
00107                     $mode_by_name[$arg] = FALSE;
00108                 }
00109                 else
00110                 {
00111                     $mode_by_name[$arg] = $arg;
00112                 }
00113             }
00114             else
00115             {
00116                 print("Unknown argument $arg\n");
00117                 $show_usage = TRUE;
00118             }
00119         }
00120         elseif(file_exists($arg))
00121         {
00122             $InFile[] = $arg;
00123         }
00124         else
00125         {
00126             print("Cannot find file $arg\n");
00127             $show_usage = TRUE;
00128         }
00129     }
00130     if(0 == count($InFile))
00131     {
00132         $show_usage = TRUE;
00133     }
00134     if(!$compiler->ChangeMode($mode_by_name))
00135     {
00136         print("Error assigning options\n");
00137         $show_usage = TRUE;
00138     }
00139     if($show_usage)
00140     {
00141         $opts = array(
00142                 "[-o OutFile]",
00143                 "InFile",
00144                 "[InFile...]",
00145                 );
00146         foreach($modes as $mode)
00147         {
00148             $param = "--".$mode["Name"];
00149             $value = "";
00150             if(TRUE === $mode["Default"])
00151             {
00152                 $value = "=TRUE";
00153             }
00154             elseif(FALSE === $mode["Default"])
00155             {
00156                 $value = "=FALSE";
00157             }
00158             $opts[] = "[$param$value]";
00159         }
00160 
00161         print("Usage: php Compiler.php ".join(" ",$opts)."\n");
00162 
00163         die(1);
00164     }
00165 
00166     // Get contents
00167     $Input = "";
00168     foreach($InFile as $filename)
00169     {
00170         $file = file_get_contents($filename);
00171         if(FALSE === $file)
00172         {
00173             print("Error reading contents of $filename\n");
00174             die(1);
00175         }
00176 
00177         $Input .= $file;
00178     }
00179 
00180     // Output errors should exit status 1, but not die immediately
00181     $die = FALSE;
00182 
00183     // Open outfiles
00184     $count = count($OutFile);
00185     $out_handles = array();
00186     for($x = 0; $x < $count; $x++)
00187     {
00188         $out = $OutFile[$x];
00189 
00190         $fh = fopen($out, "w");
00191         if(!is_resource($fh))
00192         {
00193             print("Failed to open $out for write\n");
00194             $die = TRUE;
00195         }
00196         else
00197         {
00198             $out_handles[$out] = $fh;
00199         }
00200     }
00201 
00202     // Perform the heart of the class
00203     $output = $compiler->Compile($Input);
00204     $errors = $compiler->GetErrors();
00205 
00206     // If errors
00207     if(0 < count($errors))
00208     {
00209         $fh = fopen("php://STDERR", "w");
00210         foreach($errors as $pos=>$error)
00211         {
00212             $lines = 0;
00213             $chars = 0;
00214             for($x = $pos-1; $x >= 0; $x --)
00215             {
00216                 if("\n" == $Input[$x])
00217                 {
00218                     $lines++;
00219                 }
00220                 elseif(0 == $lines)
00221                 {
00222                     $chars++;
00223                 }
00224             }
00225             $lines++;
00226             $chars++;
00227 
00228             fwrite($fh, "$lines,$chars: $error\n");
00229         }
00230         fclose($fh);
00231 
00232         if("" == $output)
00233         {
00234             die(1);
00235         }
00236     }
00237 
00238     // Output as appropriate
00239     if(0 == count($out_handles) && !$die)
00240     {
00241         print($output);
00242     }
00243     else
00244     {
00245         foreach($out_handles as $fn=>$fh)
00246         {
00247             if(FALSE === fwrite($fh, $output))
00248             {
00249                 print("Failed writing $fn\n");
00250                 $die = TRUE;
00251             }
00252             elseif(TRUE !== fclose($fh))
00253             {
00254                 print("Failed closing $fn\n");
00255                 $die = TRUE;
00256             }
00257             else
00258             {
00259                 print("Wrote $fn\n");
00260             }
00261         }
00262     }
00263 
00264     // Exit with appropriate status on error
00265     if($die)
00266     {
00267         die(1);
00268     }
00269 }
00270 
00271 return(Main($argv));
00272 
00273 ?>



Generously hosted by   SourceForge.net Logo
Generated on Wed Jun 1 11:01:06 2005 for EUrl by   Doxygen 1.4.1