*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / main.cpp
index 282ce9a6eff410624f1cea52da3158d074dc864e..53c764e0f6e31870aa6da8122b46d4c1eb4da41c 100644 (file)
 #include <vector>
 #include <string>
 
-#include <stdio.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+#include "config.hh"
+#include "tool.hh"
 
 #include "tape_block.hh"
-#include "configuration_manager.hh"
+#include "data_block_0.hh"
 
 using namespace std;
 
-int dump_vector(vector<string> arguments){
-  int res=0;
-  for (vector<string>::iterator iter=arguments.begin();iter<arguments.end();iter++){
-    printf("%s\n",(*iter).c_str());
-    res=1;
-  }
-  return res;
-}
-
-
+/*!
+ *\brief The main routine.
+ */
 int main(int argc, char ** args){
   
-  /* Configuration data */
-  string config_file;
-  string infile, outfile;
-  int infile_set, outfile_set, config_file_set;
-  
-  int 
-    do_help,
-    output_info,
-    output_called,
-    output_exported,
-    output_unsatisfied,
-    split_objects,
-    split_objects_numbered,
-    ignore_block_errors,
-    ignore_checksum_errors,
-    pause_on_checksum_error,
-    ignore_unknown_block_errors,
-    ignore_object_integrity_errors;
-  
-  int in_fd, out_fd;
-
-  in_fd=0;  /* stdin   {O _ \ */
-  out_fd=1; /* stdout  {O ^ / */
-
-  configuration_manager ar("ldc2");
-  
-  /* Here come the configuration switches */
-  ar.add_option_switch("h","help",
-                      "Output this help text and exit.",
-                      &do_help,true,false);
-
-  ar.add_option_switch("a","output_info",
-                      "Print tape data information (default)",
-                      &output_info);
-
-  ar.add_option_switch("c","output_called",
-                      "Print all called symbols from the object(s).",
-                      &output_called);
-
-  ar.add_option_switch("e","output_exported",
-                      "Print all exported symbols from the object(s).",
-                      &output_exported);
-
-  ar.add_option_switch("u","output_unsatisfied",
-                      "List all unsatisfied symbols.",
-                      &output_unsatisfied);
-
-  ar.add_option_switch("s","split_objects",
-                      "Split input data into distinct object files.",
-                      &split_objects);
-
-  ar.add_option_switch("S","split_objects_numbered",
-                      "Split input data into distinct numbered files",
-                      &split_objects_numbered);
-
-  ar.add_option_switch("b","ignore_block_errors",
-                      "Ignore block integrity errors. This will output broken blocks,too",
-                      &ignore_block_errors);
-
-  ar.add_option_switch("k","ignore_checksum_errors",
-                      "Ignore block checksum errors. Errors will be converted to warnings.",
-                      &ignore_checksum_errors);
-
-  ar.add_option_switch("p","pause_on_checksum_error",
-                      "Wait for user input on checksum error.",
-                      &pause_on_checksum_error);
-  
-  ar.add_option_switch("n","ignore_unknown_block_errors",
-                      "Ignore errors caused by unknown block types. Errors will be converted to warnings.",
-                      &ignore_unknown_block_errors);
-
-  ar.add_option_switch("g","ignore_object_integrity_errors",
-                      "Ignore errors caused by objects without proper end block. \
-Errors will be converted to warnings.",
-                      &ignore_object_integrity_errors);
-  
-  ar.add_option_value("i","input_file",
-                     "specify input file",
-                 &infile_set,&infile,
-                 "<input-file>");
-                     
-  ar.add_option_value("o","output_file",
-                     "Specify output file.",
-                     &outfile_set,&outfile,
-                     "<output_file>");
-                     
-
-  ar.add_argument("File from where data is read",
-                 &infile_set,&infile,
-                 "<input-file>");
-
-  ar.add_argument("File to where output is redirected",
-                 &outfile_set,&outfile,
-                 "<output-file>");
-
-  ar.add_option_value("F","config_file",
-                     "Use the specified configuration file.", 
-                     &config_file_set,&config_file,
-                     "<file-name>");
-  
-  // Process command line first time
-  if((dump_vector(ar.read_args(argc,args))||do_help)){
-    dump_vector(ar.get_help());
-    exit(1);
-  }
-  
-  // If user has a config file, use it.
-  if (config_file_set){
-    if(dump_vector(ar.read_file(config_file))){
-      dump_vector(ar.get_help());
-      exit(1);
-    }
-    // Process command line a second time to override values from config file.
-    if(dump_vector(ar.read_args(argc,args))||do_help){
-      dump_vector(ar.get_help());
-      exit(1);
-    }
-  }
+  // Do all the configuration stuff
+  do_config(argc,args);
  
-
-
-  
-  if (infile_set==1){
-    printf("Opening file for input:%s\n",infile.c_str());
-    in_fd=open(infile.c_str(),O_RDONLY);
-    if (in_fd<0){
-      printf("Error: could not open file:%s\n",infile.c_str());
-      exit (3);
-    }
-  }
-  
-  
+  // Now we go to read the data!
   
   
   vector<tape_block*> tape;
-  
+  vector<vector<tape_block *> > objects;
+  vector<tape_block *> current_object;  
   
   tape_block * block=0;
-  while(1){
+  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;
+
+  while(read_ahead){
     block=tape_block::gen_from_fd(in_fd);
-    if (block->get_state()==tape_block::TBS_OK){
+
+    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");
+      } 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:
+      delete block;
+      if (cfg_ignoreblock_errors){
+       fprintf(stderr,"Warning: Block integrity check failed!\n");
+       warnings++;
+      } else {
+       fprintf(stderr,"Error: Block integrity check failed!\n");
+       errors++;
+       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++;
+      } else {
+       fprintf(stderr,"Error: Block checksum wrong!\n");
+       fprintf(stderr,buffer);
+       errors++;
+       read_ahead=false;
+       errcode=4;
+      }
+      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());
+      } else {
+       fprintf(stderr,"Error: Could not read from stdin!\n");
+      }
+      errors++;
+      read_ahead=false;
+      errcode=2;
+    case tape_block::TBS_OK:
       tape.insert(tape.end(),block);
-      dump_vector(block->get_description());
-    }
-    else break;
+      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());
+      
+      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);
   }
-
-  switch(block->get_state()){
-  case tape_block::TBS_EOF_LEGAL: printf("File successfully read.\n");
-    break;
-  case tape_block::TBS_EOF_ILLEGAL: printf("EOF while in block!\n");
-    break;
-  case tape_block::TBS_CHECKSUM:
-    printf("Checksum error!\n");
-    break;
-  case tape_block::TBS_DEFAULT:
-    printf("TBS_DEFAULT encountered ->> SEVERY INTERNAL ERROR!\n");
-    exit(88);
-  case tape_block::TBS_IOERR:
-    printf("I/O Error... Why?\n");
-    exit (43);
+  
+  if (errors>0){
+    fprintf(stderr,"Errors make me sick. Goodbye!\n");
+    exit(errcode);
   }
 
   return 0;
 } // main()