*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / main.cpp
index 34f2279b29a479e04191030fa77c47e986175156..eafdf6a1d309fc38aeedb5ec2f5765e778638f5e 100644 (file)
@@ -1,5 +1,8 @@
 /* ldc2 preliminary main program */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include <vector>
 #include <string>
@@ -157,10 +160,15 @@ void read_tape(){
     
     if (error)   fprintf(stderr,message.c_str());
     if (warning) fprintf(stdwarn,message.c_str());
-    
-    if(cfg_list_contents){
+        
+    if(error||warning){
       dump_vector(block->get_description());
-      if (!error&&!warning) spaced=false;
+    } else {
+      if(cfg_list_contents){
+       FILE * tmp_fp=fdopen(out_fd,"w");
+       dump_vector_fp(block->get_description(),tmp_fp);
+       if (!error&&!warning) spaced=false;
+      }
     }
 
     if (error||warning||cfg_verbose){
@@ -177,38 +185,202 @@ void read_tape(){
     }
 } // read_tape()
 
+
 /******************************************************************************/
+void process_tape(){
+  if (cfg_verbose) fprintf(stdwarn,"Processing Tape.\n");
+  
+  bool in_object=false;
+  char filename[100];
+  char filename_numbered[100];
+  string objname;
+  int unknown_count=-1;
+  int fd=-1, fd_numbered=-1;
+  int obj_no=0;
+  int obj_start_block;
+  
+  for (unsigned int i=0; i<tape.size();i++){
+    tape_block * block=tape[i];
+
+      if (block->get_type()==tape_block::TBT_EOT){
+       objname="EOT";
+       if (in_object){
+         if (cfg_ignore_object_integrity_errors){
+           fprintf(stdwarn,"Warning: Object integrity error!\n");
+           warnings++;
+         } else {
+           fprintf(stderr,"Error: Object integrity error!\
+ (Object no %i, Block %i unexpected)\n",obj_no,i);
+           errors++;
+           errcode=6;
+           return;
+         }
+       }
+      }
+    
+    if (!in_object){ // object begin
+      obj_no++;
+      obj_start_block=i;
+      
+      objname="UNKNOWN";
+      unknown_count++;
+      
+      vector<string>names=block->get_exported_symbols();
+      for (unsigned int a=0; a<names.size();a++){
+       objname=names[0];
+       if (objname!="      "){
+         objname=objname.substr(0,objname.find_last_not_of(" ")+1);
+         unknown_count--;
+         break;
+         
+       }
+      }
+           
+      if (block->get_type()==tape_block::TBT_EOT){
+       objname="EOT";
+      }
+      
+      // Open file for split objects
+      if (cfg_split_objects && (block->get_type()!=tape_block::TBT_EOT)){
+       if (objname=="UNKNOWN")snprintf(filename,99,"UNKNOWN%02i.obj",unknown_count);
+       else snprintf(filename,99,"%s.obj",objname.c_str());
+       if (fd>-1) close (fd);
+       fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666);
+       printf("opening \"%s\"\n",filename);
+       if (fd<0){
+         fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
+                 filename);
+       exit (1);
+       }
+      }
+
+      // Open file for split objects numbered
+      snprintf(filename_numbered,99,"%03i0-%s.obj",obj_no,objname.c_str());
+      if (cfg_split_objects_numbered){
+       if (fd_numbered>-1) close (fd_numbered);
+       fd_numbered=open(filename_numbered,O_WRONLY|O_CREAT|O_TRUNC,0666);
+       if (fd_numbered<0){
+         fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
+                 filename_numbered);
+       exit (1);
+       }
+      }
+      in_object=true;
+    } // object begin
+    
+    // Output block data
+    if (cfg_split_objects){
+      try{
+       if (block->get_type()!=tape_block::TBT_EOT) // Don't want EOT here
+         block->dump_to_fd(fd);
+      }
+      catch (tape_block::io_error_exception e){
+       fprintf(stderr,"Error: could write to file \"%s\"!\n",
+               filename);
+       exit (1);
+      }
+    } // if (cfg_split_objects)
+
+    // Output block data
+    if (cfg_split_objects_numbered){
+      try{
+       block->dump_to_fd(fd_numbered);
+      }
+      catch (tape_block::io_error_exception e){
+       fprintf(stderr,"Error: could write to file \"%s\"!\n",
+               filename_numbered);
+       exit (1);
+      }
+    } // if (cfg_split_objects_numbered)
+
+    // Output individual block file if desired
+    if (cfg_split_blocks){
+      char fname[100];
+      snprintf(fname,99,"%03i0-%s-%03i.block",obj_no,objname.c_str(),i-obj_start_block);
+      int fd_block=open(fname,O_WRONLY|O_TRUNC|O_CREAT,0666);
+      if (fd_block<0){
+         fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
+                 fname);
+       exit (1);
+      }
+      try{
+       block->dump_to_fd(fd_block);
+      }
+      catch (tape_block::io_error_exception e){
+       fprintf(stderr,"Error: could write to file \"%s\"!\n",
+               fname);
+       exit (1);
+      }
+      close(fd_block);
+    }
+    
+    if (block->is_endblock()||(block->get_type()==tape_block::TBT_EOT)) {
+      in_object=false; 
+      if (fd!=-1) close(fd);
+      if (fd_numbered!=-1) close(fd_numbered);
+    }
+  } // for (...)
+  
+  if (in_object){
+    if (cfg_ignore_object_integrity_errors){
+      fprintf(stdwarn,"Warning: Object integrity error!\n");
+      warnings++;
+    } else {
+      fprintf(stdwarn,"Error: Object integrity error!\n");
+      errors++;
+      errcode=6;
+      return;
+    }
+  }
+   
 
-void type_check(){
-  bool warning=false;
-  bool error=false;
+} // process_tape()
 
+void process_called_imports(){
+  vector<string>exported;
+  vector<string>called;
+  vector <string>unsatisfied;
+  
   for (unsigned int i=0; i<tape.size();i++){
-    bool spaced=false;
-    if (!tape[i]->has_known_type()){
-      FILE * fp;
-      if (cfg_ignore_unknown_block_errors){
-       fp=stdwarn;
-       warning=true;
-      } else {
-       fp=stderr;
-       error=true;
-      }        
-      if (!spaced) {
-       spaced=false;fprintf(fp,"\n");
-       fprintf(fp,"Warning: Unknown Block type! Block No.%i\n",i);
-       dump_vector_fp(tape[i]->get_description(),fp);
-       fprintf(fp,"\n");
-       spaced=true;
-      }
-      if (!cfg_ignore_unknown_block_errors){
-       int errcode=5;
-       fprintf(stdwarn,"Failed. (%i)\n",errcode);
-       exit(errcode);
+    merge_vector_unique(exported,tape[i]->get_exported_symbols());
+    merge_vector_unique(called,tape[i]->get_called_symbols());
+  }
+  
+  for (unsigned int icalled=0;icalled<called.size();icalled++){
+    bool found=false;
+    for (unsigned int iexported=0;iexported<exported.size();iexported++){
+      if (exported[iexported].compare(called[icalled])==0){
+       found=true;
+       break;
       }
     }
+    if (!found) {
+      bool present=false;
+      for (unsigned int i=0;i<unsatisfied.size();i++){
+       if (unsatisfied[i].compare(called[icalled])==0){
+         present=true;
+         break;
+       }
+      }
+      if (!present) unsatisfied.insert(unsatisfied.end(),called[icalled]);
+    }
   }
-} // type_check()
+  
+  FILE * outp=fdopen(out_fd,"w");
+
+  if (cfg_output_called){
+    dump_vector_fp(called,outp);
+  }
+
+  if (cfg_output_exported){
+    dump_vector_fp(exported,outp);
+  }
+
+  if (cfg_output_unsatisfied){
+    dump_vector_fp(unsatisfied,outp);
+  }
+
+}
 
 /******************************************************************************/
 /*!
@@ -216,7 +388,6 @@ void type_check(){
  */
 int main(int argc, char ** args){
 
-
   // Do all the configuration stuff
   do_config(argc,args);
 
@@ -233,15 +404,18 @@ int main(int argc, char ** args){
   read_tape(); // Process input to completion.
   exit_on_error();
   
+  process_tape();
+  exit_on_error();
+
+  process_called_imports();
+  exit_on_error();
   
   
-  if (cfg_split_objects){
-    for (unsigned int i=0; i<tape.size();i++){
-      tape[i]->dump_to_fd(out_fd);
-    }
+  if (warnings>0){
+    fprintf(stdwarn,"Warnings:%i\n",warnings);
+    return 99;
   }
-  
+  if (cfg_verbose) fprintf(stdwarn,"Success.\n");
   return 0;
 } // main()