X-Git-Url: http://gitweb.hachti.de/?a=blobdiff_plain;f=pc-tools%2Fldc2%2Fsrc%2Fmain.cpp;h=34f2279b29a479e04191030fa77c47e986175156;hb=16b4d690e1a97837a883abe60d30b5cd032be935;hp=a86291b519c9ed0d5e2750050a6c091d3b6e8dec;hpb=909d36034504f2511a5bc4ef7e50d407964e247a;p=h316.git diff --git a/pc-tools/ldc2/src/main.cpp b/pc-tools/ldc2/src/main.cpp index a86291b..34f2279 100644 --- a/pc-tools/ldc2/src/main.cpp +++ b/pc-tools/ldc2/src/main.cpp @@ -1,178 +1,247 @@ /* ldc2 preliminary main program */ + #include #include -#include -#include -#include -#include -#include +#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 arguments){ - int res=0; - for (vector::iterator iter=arguments.begin();iter tape; //! Represents the whole tape contents. +static vector > objects; //! Tape content in objects. - 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); +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); + } +} - 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); +/******************************************************************************/ +/*! + *\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. - ar.add_option_value("i","input_file", - "specify input file", - &infile_set,&infile, - ""); - - ar.add_option_value("o","output_file", - "Specify output file.", - &outfile_set,&outfile, - ""); - - - ar.add_argument("File from where data is read", - &infile_set,&infile, - ""); - - ar.add_argument("File to where output is redirected", - &outfile_set,&outfile, - ""); - - ar.add_option_value("F","config_file", - "Use the specified configuration file.", - &config_file_set,&config_file, - ""); + bool spaced=false; //! Help flag for spaced messages. - if((dump_vector(ar.read_args(argc,args))||do_help)){ - dump_vector(ar.get_help()); - exit(1); - } + string message; //! A warning or help message. + - 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); + while(read_ahead){ + + 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 { + 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; + if (cfg_ignore_block_errors){ + message="Warning: Block integrity check failed!\n"; + warning=true; + } else { + message="Error: Block integrity check failed!\n"; + error=true; + errcode=3; + } + } + + 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 { + 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=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()); + + 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; ihas_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); + } } } +} // type_check() + +/******************************************************************************/ +/*! + *\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(); - vector tape; - - tape_block * block=0; - while(1){ - block=tape_block::gen_from_fd(in_fd); - if (block->get_state()==tape_block::TBS_OK){ - tape.insert(tape.end(),block); - dump_vector(block->get_description()); + if (cfg_split_objects){ + for (unsigned int i=0; idump_to_fd(out_fd); } - else break; } - - 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); - } - + + return 0; } // main()