ldc2: Cosmetic fixes and support for compiler orverride
[h316.git] / pc-tools / ldc2 / src / main.cpp
index ba8a36c7a28da538f525590f6dc148c182ced7e7..8b7da8788adc4e0753701545b23467a48f19bcd1 100644 (file)
@@ -1,9 +1,49 @@
-/* ldc2 preliminary main program */
+/******************************************************************************
+ * 
+ * LDC2 source code
+ *
+ * $Date: 2010/01/04 02:10:59 $
+ * $Author: hachti $
+ *
+ * $Log: main.cpp,v $
+ * Revision 2.6  2010/01/04 02:10:59  hachti
+ * *** empty log message ***
+ *
+ * Revision 2.5  2008-10-01 13:30:14  hachti
+ * Added some includes
+ *
+ * Revision 2.4  2008-08-25 21:02:24  hachti
+ * *** empty log message ***
+ *
+ * Revision 2.3  2007-05-30 02:51:16  hachti
+ * Changed everything towards LDC2 use.
+ * Worked on the buildlib.sh.
+ * Centralized  buildlib.sh to a new lib/common directory.
+ *
+ * Revision 2.2  2007-03-26 03:20:31  hachti
+ * *** empty log message ***
+ *
+ * Revision 2.1  2007-03-26 01:15:21  hachti
+ * *** empty log message ***
+ *
+ * Revision 2.0  2007-03-26 01:00:40  hachti
+ * *** empty log message ***
+ *
+ *
+ ******************************************************************************/
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdlib.h>
 
 
 #include <vector>
 #include <string>
-
+  
 #include "config.hh"
 #include "tool.hh"
 
@@ -20,69 +60,71 @@ using namespace std;
 #define VERSION "0.0"
 #endif
 
-#define ID_STRING "\n     ldc2 - The X16 object analyser\n\
+#define ID_STRING "\n     ldc2 - The X16 object tape analyser\n\
             (C) 2007 Philipp Hachtmann\n\n\
      Version %s, built %s\n     %s\n\n"
 
+/******************************************************************************/
+
+static FILE * stdwarn;       //! Suppressable warning output file pointer.
+static vector<tape_block*> tape;     //! The whole tape contents.
+static vector<vector<tape_block*> > objects; //! Tape content in objects.
+
+static int errors  = 0;  //! Global error counter.
+static int warnings= 0;  //! Global warning counter.
+static int errcode = 0;  //! Error code for error exit routine.
 
 
+/******************************************************************************/
 /*!
- *\brief The main routine.
+ *\brief Error exit routine.
+ *
+ * This routine tests the global error counter and terminates the program
+ * if necessary.
  */
-int main(int argc, char ** args){
+void exit_on_error(){
+  if (errors){
+    fprintf(stderr,"Failed. (%i)\n",errcode);
+    exit(errcode);
+  }
+}
 
   
-  // Do all the configuration stuff
-  do_config(argc,args);
-  if (cfg_version){
-    fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP);
-    exit(0);
-  }
+/******************************************************************************/
+/*!
+ *\brief Read in Tape data and do some checks and messages.
+ *
+ * This routine reads in the tape data.
+ * During reading in, checksums are checked and
+ * /da001/ information is generated. No object integrity test.
+ * Tape block object pointers are put into the global vector 
+ * tape.
+ */
+void read_tape(){
+
+  tape_block * block=0;  //! Pointer to current block.
+  bool read_ahead=true;  //! Continue scanning for blocks.
+  int read_pointer=0;    //! Read position in input.
+  int block_start=0;     //! Start of block position in input.
+  int block_end=0;       //! End of block position in input.
+  int blocks_read=0;     //! Number of blocks read in.
   
-  vector<tape_block*> tape;
-  vector<vector<tape_block *> > objects;
-  vector<tape_block *> current_object;  
+  bool spaced=false;     //! Help flag for spaced messages. 
   
-  tape_block * block=0;
-  bool read_ahead=true;
-
-  int read_pointer=0;
-  int block_start=0;
-  int block_end=0;
-  int blocks_read=0;
-
-  bool in_object=false;
-
-  int errors=0;
-  int warnings=0;
-  int errcode=0;
-
-  bool spaced=false;
+  string message;        //! A warning or help message.
 
+  
   while(read_ahead){
+
+         //  bool err_checksum=false;  //! Checksum error flag.
+         //    bool err_integrity=false; //! Integrity error flag.
+    
+    bool warning=false;       //! Warning flag.
+    bool error=false;         //! Error flag.
+    
+    // Try to generate block
     try{
       block=tape_block::gen_from_fd(in_fd);
-      blocks_read++;
-      tape.insert(tape.end(),block);
-      block_start=read_pointer+block->get_discarded_bytes();
-      block_end=block_start+block->get_raw_size()-1;
-      read_pointer=block_end+1;
-      
-      if (cfg_verbose) {
-       char buffer[200];
-       if (!spaced) fprintf(stderr,"\n");
-       spaced=false;
-       
-       snprintf(buffer,200,"Block No.%03i:  Start:0x%04x  End:0x%04x  Size:0x%02x\n",
-                blocks_read-1,
-                block_start,
-                block_end,
-                block->get_raw_size());
-       fprintf(stderr,buffer);
-      }
-      if(cfg_list_contents)
-       dump_vector(block->get_description());
     }
     
     catch(tape_block::eof_legal_exception &e){
@@ -92,186 +134,372 @@ int main(int argc, char ** args){
     
     catch(tape_block::io_error_exception){
       if (in_fd){
-       fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str());
+       fprintf(stderr,"Error: Could not read from \"%s\"!\n",
+               cfg_infile.c_str());
       } else {
        fprintf(stderr,"Error: Could not read from stdin!\n");
       }
-      errors++;
+      error=true;
       errcode=2;
       break; // Immediately leave read loop.
     }
     
     catch(tape_block::eof_illegal_exception &e){
       block=e.get_block();
-      blocks_read++;
-      tape.insert(tape.end(),block);
-      block_start=read_pointer+block->get_discarded_bytes();
-      block_end=block_start+block->get_raw_size()-1;
-      read_pointer=block_end+1;
-      
-      if (!spaced) fprintf(stderr,"\n");
-      spaced=false;
-      char buffer[200];
-      snprintf(buffer,200,"Block No.%03i:  Start:0x%04x  End:0x%04x  Size:0x%02x\n",
-              blocks_read-1,
-              block_start,
-              block_end,
-              block->get_raw_size());
-      fprintf(stderr,buffer);
-      
+      //      err_integrity=true;
+      read_ahead=false;
       if (cfg_ignore_block_errors){
-       fprintf(stderr,"Warning: Block integrity check failed!\n");
-       warnings++;
+       message="Warning: Block integrity check failed!\n";
+       warning=true;
       } else {
-       fprintf(stderr,"Error: Block integrity check failed!\n");
-       errors++;
+       message="Error: Block integrity check failed!\n";
+       error=true;
        errcode=3;
-       read_ahead=false;
       }
-      if(cfg_list_contents)
-       dump_vector(block->get_description());
-      fprintf(stderr,"\n");
-      spaced=true;
     }
     
     catch(tape_block::checksum_error_exception &e){
       block=e.get_block();
-      blocks_read++;
-      tape.insert(tape.end(),block);
-      block_start=read_pointer+block->get_discarded_bytes();
-      block_end=block_start+block->get_raw_size()-1;
-      read_pointer=block_end+1;
-      
-      
-      if (!spaced) fprintf(stderr,"\n");
-      spaced=false;
-      char buffer[200];
-      snprintf(buffer,200,"Block No.%03i:  Start:0x%04x  End:0x%04x  Size:0x%02x\n",
-              blocks_read-1,
-              block_start,
-              block_end,
-              block->get_raw_size());
-      fprintf(stderr,buffer);
-      
+      //      err_checksum=true;
       if (cfg_ignore_checksum_errors){
-       fprintf(stderr,"Warning: Block checksum wrong!\n");
-       warnings++;
+       message="Warning: Block checksum wrong!\n";
+       warning=true;
       } else {
-       fprintf(stderr,"Error: Block checksum wrong!\n");
-       errors++;
+       message="Error: Block checksum wrong!\n";
+       error=true;
        read_ahead=false;
        errcode=4;
       }
-      if(cfg_list_contents)
-       dump_vector(block->get_description());
-      fprintf(stderr,"\n");
-      spaced=true;
     }
     
+    // Now let's check for block type
+    if ((!error)&&(!warning)&&!block->has_known_type()){
+      if (cfg_ignore_unknown_block_errors){
+       message="Warning: Unknown Block type!";
+       warning=true;
+      }else{
+       message="Error: Unknown Block type!";
+       error=true;
+       read_ahead=false;
+       errcode=5;
+      }
+    }
+    
+    // Count block.
+    blocks_read++;
+    tape.insert(tape.end(),block);
+    block_start=read_pointer+block->get_discarded_bytes();
+    block_end=block_start+block->get_raw_size()-1;
+    read_pointer=block_end+1;
+
+    FILE * fp=stdwarn;
+    if (error) fp=stderr;
+         
+    if (cfg_verbose||error||warning){
+      if (!spaced) fprintf(fp,"\n");
+      spaced=false;
+      fprintf(fp,"Block No.%03i:  Start:0x%04x  End:0x%04x  Size:0x%02x\n",
+             blocks_read-1,
+             block_start,
+             block_end,
+             block->get_raw_size());
+    }
+    
+    if (error)   fprintf(stderr,message.c_str());
+    if (warning) fprintf(stdwarn,message.c_str());
         
-  } // while (read_ahead)
-
-  // Now we have our blocks, tell the user about that!
-  if (!spaced) fprintf(stderr,"\n");
-  spaced=false;
-  char buffer[200];
-  sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n",
-         read_pointer,blocks_read,errors,warnings);
-  if(cfg_output_info){
-    write (out_fd,buffer,strlen(buffer));
-  } else {
-    fprintf(stderr,buffer);
-  }
+    if(error||warning){
+      dump_vector(block->get_description());
+    } 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 (errors>0){
-    fprintf(stderr,"Failed. (%i)\n",errcode);
-    exit(errcode);
-  }
-  
+    if (error||warning||cfg_verbose){
+      fprintf(fp,"\n");
+      spaced=true;
+    }
+    if (error) errors++;
+    if (warning) warnings++;
+  } // while....
   if (cfg_verbose){
-    fprintf(stderr,"Data read successfully.\n");
-  }
-  close(in_fd);
-//     switch(block->get_state()){ // switchy
-//     case tape_block::TBS_EOF_LEGAL: 
-//       delete block;
-//       if (!in_object){
-//     fprintf(stderr,"File successfully read.\n");
-//       } else {
-//     if (cfg_ignore_object_integrity_errors){
-//       fprintf(stderr,"Warning: Object integrity check failed!\n");
-//       warnings++;
-//     } else {
-//       fprintf(stderr,"Error: Object integrity check failed!\n");
-//       errors++;
-//       errcode=6;
-//     }
-//       }
-//       read_ahead=false;
-//       break;
-//     case tape_block::TBS_EOF_ILLEGAL:
-//       break;
-//     case tape_block::TBS_CHECKSUM:
-//       break;
-//     case tape_block::TBS_DEFAULT:
-//       delete block;
-//       fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n");
-//       errors++;
-//       errcode=100;
-//       read_ahead=false;
-//       break;
-//     case tape_block::TBS_IOERR:
-//     case tape_block::TBS_OK:
-//       tape.insert(tape.end(),block);
-//       blocks_read++;
-      
-//       if (cfg_verbose){
-//     char buffer[200];
-//     snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n",
-//              blocks_read-1,
-//              block_start,
-//              block_end,
-//              block->get_raw_size());
-//     write (out_fd,buffer,strlen(buffer));
-//       }
-
-//       if(cfg_list_contents)
-//     dump_vector(block->get_description());
+    close(in_fd);
+    fprintf(stderr,"Reading finished.\n");
+    fprintf(stderr,"Bytes read:%i, Blocks read:%i\n",read_pointer,blocks_read);
+    }
+} // read_tape()
+
+
+/******************************************************************************/
+/*!
+ *\brief Do integrity test and object or block dumps.
+ */
+void process_tape(){
+  if (cfg_verbose) fprintf(stdwarn,"\nProcessing Data.\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!\
+ (Object no %i, Block %i unexpected)\n",obj_no,i);
+           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
       
-//       if (block->get_type()==0x10){ // Unknown data block!
-//     if (cfg_ignore_unknown_block_errors){
-//       fprintf(stderr,"Warning: Unknown block type!\n");
-//       warnings++;
-//     }else {
-//       fprintf(stderr,"Error: Unknown block type!\n");
-//       errors++;
-//       errcode=5;
-//       read_ahead=false;
-//     }
-//       }
+      obj_no++;
+      obj_start_block=i;
       
-//       current_object.insert(current_object.end(),block);
+      objname="UNKNOWN";
+      unknown_count++;
       
-//       if ((block->get_type()==data_block::TBT_EOT)
-//       ||((block->get_type()==0)&&(block->get_subtype()==3))
-//       ||((block->get_type()==4)||(block->get_type()==3))
-//       ||((block->get_type()==0)&&(block->get_subtype()==014))){
-//     in_object=false;
-//     objects.insert(objects.end(),current_object);
-//     current_object.clear();
-//       } else {
-//     in_object=true;
-//       }
+      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";
+      }
       
-//       break;
-//     } // switch
-//   } // generate loop
+      // Open file for split objects
+      if (cfg_split_objects && (block->get_type()!=tape_block::TBT_EOT)){
+       if (objname=="UNKNOWN")snprintf(filename,99,"UNKNOWN%02i",
+                                       unknown_count);
+       else snprintf(filename,99,"%s",objname.c_str());
+       if (fd>-1) close (fd);
+       if (cfg_verbose) fprintf(stdwarn,"Writing file: %s\n",filename);
+       fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0666);
+       if (fd<0){
+         fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
+                 filename);
+         errors++;
+         errcode=1;
+         return;
+       }
+      }
 
+      // Open file for split objects numbered
+      snprintf(filename_numbered,99,"%03i0-%s",obj_no,objname.c_str());
+      if (cfg_split_objects_numbered){
+       close (fd_numbered);
+       if (cfg_verbose) fprintf(stdwarn,"Writing file: %s\n",filename_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);
+         errors++;
+         errcode=1;
+         return;
+       }
+      }
+      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);
+       errors++;
+       errcode=1;
+       return;
+      }
+    } // 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);
+         errors++;
+         errcode=1;
+         return;
+      }
+    } // 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);
+      if (cfg_verbose) fprintf(stdwarn,"Writing file: %s\n",fname);
+      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);
+         errors++;
+         errcode=1;
+         close(fd);
+         close(fd_numbered);
+         return;
+      }
+      try{
+       block->dump_to_fd(fd_block);
+      }
+      catch (tape_block::io_error_exception e){
+       fprintf(stderr,"Error: could write to file \"%s\"!\n",
+               fname);
+       errors++;
+       errcode=1;
+       close(fd);
+       close(fd_numbered);
+       return;
+      }
+      close(fd_block);
+    }
+    
+    if (block->is_endblock()||(block->get_type()==tape_block::TBT_EOT)) {
+      in_object=false; 
+      close(fd);
+      close(fd_numbered);
+    }
+  } // for (...)
   
+  if (in_object){
+    if (cfg_ignore_object_integrity_errors){
+      fprintf(stdwarn,"Warning: Object integrity error! Last object incomplete!\n");
+      warnings++;
+    } else {
+      fprintf(stdwarn,"Error: Object integrity error! Last Object incomplete!\n");
+      errors++;
+      errcode=6;
+      if (fd!=-1) close(fd);
+      if (fd_numbered!=-1) close(fd_numbered);
+      return;
+    }
+  }
+} // process_tape()
+
+/******************************************************************************/
+/*!
+ *\brief Everything that has to do with symbol listing.
+ */
+void process_symbols(){
+  vector<string>exported;
+  vector<string>called;
+  vector <string>unsatisfied;
+
+  if (cfg_verbose) fprintf(stdwarn,"\nProcessing Symbols.\n");  
+
+  for (unsigned int i=0; i<tape.size();i++){
+    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]);
+    }
+  }
+  
+  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);
+  }
+
+} // process_symbols()
+
+/******************************************************************************/
+/*!
+ *\brief The main routine.
+ *
+ */
+int main(int argc, char ** args){
+
+  // Do all the configuration stuff
+  do_config(argc,args);
+
+  // Output a version message if desired
+  if (cfg_version){
+    fprintf(stderr, ID_STRING, VERSION, BUILD_DATE, BUILD_STAMP);
+    exit(0);
+  }
+  
+  // Assign warning output according to configuration
+  if (cfg_quiet) stdwarn=fopen("/dev/null","w");
+  else stdwarn=stderr;
+  
+  read_tape(); // Process input to completion.
+  exit_on_error();
+  
+  process_tape();
+  exit_on_error();
+
+  process_symbols();
+  exit_on_error();
+    
+  if (warnings>0){
+    fprintf(stdwarn,"Warnings:%i\n",warnings);
+    return 99;
+  }
+  if (cfg_verbose) fprintf(stdwarn,"\nSuccess.\n");
   return 0;
+
+  /*
+   * All over this program, no memory is freed. This is not necessary because
+   * everything is allocated once and that's it.
+   * The classes support proper freeing of their internal buffers.
+   */
+
 } // main()
 
+/******************************************************************************/