*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / main.cpp
index 53c764e0f6e31870aa6da8122b46d4c1eb4da41c..34f2279b29a479e04191030fa77c47e986175156 100644 (file)
@@ -1,5 +1,6 @@
 /* ldc2 preliminary main program */
 
+
 #include <vector>
 #include <string>
 
 
 using namespace std;
 
+#ifndef BUILD_STAMP
+#define BUILD_STAMP ""
+#endif
+
+#ifndef VERSION
+#define VERSION "0.0"
+#endif
+
+#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; //! Represents the whole tape contents.
+static vector<vector<tape_block*> > objects; //! Tape content in objects.
+
+static int errors=0;
+static int warnings=0;
+static int errcode=0;  //! Variable for error codes.
+
+
+void exit_on_error(){
+  if (errors){
+    fprintf(stderr,"Failed. (%i)\n",errcode);
+    exit(errcode);
+  }
+}
+  
+/******************************************************************************/
 /*!
- *\brief The main routine.
+ *\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.
  */
-int main(int argc, char ** args){
-  
-  // Do all the configuration stuff
-  do_config(argc,args);
-  // Now we go to read the data!
-  
+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;
+  string message;        //! A warning or help message.
 
+  
   while(read_ahead){
-    block=tape_block::gen_from_fd(in_fd);
 
-    block_start=read_pointer+block->get_discarded_bytes();
-    block_end=block_start+block->get_raw_size()-1;
-    read_pointer=block_end+1;
-    //printf("Discarded:%2x, Raw Size:%2x\n",block->get_discarded_bytes(),block->get_raw_size());
-    switch(block->get_state()){ // switchy
-    case tape_block::TBS_EOF_LEGAL: 
-      delete block;
-      if (!in_object){
-       fprintf(stderr,"File successfully read.\n");
+    bool err_checksum=false;
+    bool err_integrity=false;
+    
+    bool warning=false;
+    bool error=false;
+    
+    try{
+      block=tape_block::gen_from_fd(in_fd);
+    }
+    
+    catch(tape_block::eof_legal_exception &e){
+      read_pointer+=e.get_consumed();
+      break; // Immediately leave read loop.
+    }
+    
+    catch(tape_block::io_error_exception){
+      if (in_fd){
+       fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str());
       } 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;
-       }
+       fprintf(stderr,"Error: Could not read from stdin!\n");
       }
+      error=true;
+      errcode=2;
+      break; // Immediately leave read loop.
+    }
+    
+    catch(tape_block::eof_illegal_exception &e){
+      block=e.get_block();
+      err_integrity=true;
       read_ahead=false;
-      break;
-    case tape_block::TBS_EOF_ILLEGAL:
-      delete block;
-      if (cfg_ignoreblock_errors){
-       fprintf(stderr,"Warning: Block integrity check failed!\n");
-       warnings++;
+      if (cfg_ignore_block_errors){
+       message="Warning: Block integrity check failed!\n";
+       warning=true;
       } else {
-       fprintf(stderr,"Error: Block integrity check failed!\n");
-       errors++;
-       errcode=3;
+       message="Error: Block integrity check failed!\n";
+       error=true;
+       errcode=3;
       }
-      read_ahead=false;
-      break;
-    case tape_block::TBS_CHECKSUM:
-      delete block;
-      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());
-      if (cfg_ignorechecksum_errors){
-       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));
-       fprintf(stderr,"Warning: Block checksum wrong!\n");
-       fprintf(stderr,buffer);
-       warnings++;
+    }
+    
+    catch(tape_block::checksum_error_exception &e){
+      block=e.get_block();
+      err_checksum=true;
+      if (cfg_ignore_checksum_errors){
+       message="Warning: Block checksum wrong!\n";
+       warning=true;
       } else {
-       fprintf(stderr,"Error: Block checksum wrong!\n");
-       fprintf(stderr,buffer);
-       errors++;
+       message="Error: Block checksum wrong!\n";
+       error=true;
+       read_ahead=false;
+       errcode=4;
+      }
+    }
+    
+    // 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=4;
+       errcode=5;
       }
-      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:
-      delete block;
-      if (in_fd){
-       fprintf(stderr,"Error: Could not read from \"%s\"!\n",cfg_infile.c_str());
+    }
+    
+    // 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());
+    
+    if(cfg_list_contents){
+      dump_vector(block->get_description());
+      if (!error&&!warning) spaced=false;
+    }
+
+    if (error||warning||cfg_verbose){
+      fprintf(fp,"\n");
+      spaced=true;
+    }
+    if (error) errors++;
+    if (warning) warnings++;
+  } // while....
+  if (cfg_verbose){
+    close(in_fd);
+    fprintf(stderr,"Reading finished.\n");
+    fprintf(stderr,"Bytes read:%i, Blocks read:%i\n",read_pointer,blocks_read);
+    }
+} // read_tape()
+
+/******************************************************************************/
+
+void type_check(){
+  bool warning=false;
+  bool error=false;
+
+  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 {
-       fprintf(stderr,"Error: Could not read from stdin!\n");
+       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;
       }
-      errors++;
-      read_ahead=false;
-      errcode=2;
-    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_ignore_unknown_block_errors){
+       int errcode=5;
+       fprintf(stdwarn,"Failed. (%i)\n",errcode);
+       exit(errcode);
       }
+    }
+  }
+} // type_check()
 
-      if(cfg_list_contents)
-       dump_vector(block->get_description());
-      
-      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;
-       }
-      }
-      
-      current_object.insert(current_object.end(),block);
-      
-      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;
-      }
-      
-      break;
-    } // switch
-  } // generate loop
-  close(in_fd);
-
-  // Now we have our blocks, tell the user about that!
-  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);
+/******************************************************************************/
+/*!
+ *\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);
   }
   
-  if (errors>0){
-    fprintf(stderr,"Errors make me sick. Goodbye!\n");
-    exit(errcode);
+  // 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();
+  
+  
+  
+  if (cfg_split_objects){
+    for (unsigned int i=0; i<tape.size();i++){
+      tape[i]->dump_to_fd(out_fd);
+    }
   }
-
+  
  
   return 0;
 } // main()