X-Git-Url: http://gitweb.hachti.de/?a=blobdiff_plain;f=pc-tools%2Fldc2%2Fsrc%2Fmain.cpp;h=d2270b68aaffdeeb5d8169b1582a714afb13d31d;hb=5e2538182c99ec053b457255722f704927a5f003;hp=068ca6e22439e3649ca89c65f6da2fd322ba07e8;hpb=fed2c751d37bf5e314fd26c98909471157cc86c0;p=h316.git diff --git a/pc-tools/ldc2/src/main.cpp b/pc-tools/ldc2/src/main.cpp index 068ca6e..d2270b6 100644 --- a/pc-tools/ldc2/src/main.cpp +++ b/pc-tools/ldc2/src/main.cpp @@ -1,354 +1,491 @@ -/* ldc2 preliminary main program */ - -#include -#include - -#include -#include +/****************************************************************************** + * + * LDC2 source code + * + * $Date: 2007/05/30 02:51:16 $ + * $Author: hachti $ + * + * $Log: main.cpp,v $ + * 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 #include #include +#include +#include + +#include "config.hh" +#include "tool.hh" + #include "tape_block.hh" #include "data_block_0.hh" -#include "configuration_manager.hh" - using namespace std; -static int in_fd, out_fd; +#ifndef BUILD_STAMP +#define BUILD_STAMP "" +#endif +#ifndef VERSION +#define VERSION "0.0" +#endif -int dump_vector(vector arguments){ - int res=0; - for (vector::iterator iter=arguments.begin();iter tape; //! The whole tape contents. +static vector > 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. - * - * Here we do the configuration management and process the data. + *\brief Error exit routine. * + * This routine tests the global error counter and terminates the program + * if necessary. */ -int main(int argc, char ** args){ - - /* Configuration data */ - string config_file; - string infile, outfile; - int infile_set=0, outfile_set=0, config_file_set=0; - - int - do_help=0, - output_info=0, - output_called=0, - output_exported=0, - output_unsatisfied=0, - split_objects=0, - split_objects_numbered=0, - ignore_block_errors=0, - ignore_checksum_errors=0, - pause_on_checksum_error=0, - ignore_unknown_block_errors=0, - ignore_object_integrity_errors=0, - list_contents=1, - verbose=0; - - 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,false,true); - - ar.add_option_switch("a","output_info", - "Print tape data information (default)", - &output_info,true,true); - - ar.add_option_switch("c","output_called", - "Print all called symbols from the object(s).", - &output_called,true,true); - - ar.add_option_switch("e","output_exported", - "Print all exported symbols from the object(s).", - &output_exported,true,true); - - ar.add_option_switch("u","output_unsatisfied", - "List all unsatisfied symbols.", - &output_unsatisfied,true,true); - - ar.add_option_switch("S","split_objects_numbered", - "Split input data into distinct numbered files", - &split_objects_numbered,true,true); - - ar.add_option_switch("s","split_objects", - "Split input data into distinct object files.", - &split_objects,true,true); - - ar.add_option_switch("b","ignore_block_errors", - "Ignore block integrity errors. This will output broken blocks,too", - &ignore_block_errors,true,true); - - ar.add_option_switch("k","ignore_checksum_errors", - "Ignore block checksum errors. Errors will be converted to warnings.", - &ignore_checksum_errors,true,true); +void exit_on_error(){ + if (errors){ + fprintf(stderr,"Failed. (%i)\n",errcode); + exit(errcode); + } +} - ar.add_option_switch("p","pause_on_checksum_error", - "Wait for user input on checksum error.", - &pause_on_checksum_error,true,true); - - 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,true,true); - - 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,true,true); - - ar.add_option_switch("v","verbose", - "Be a bit more verbose.", - &verbose,true,true); - ar.add_option_value("i","in_file", - "specify input file", - &infile_set,&infile, - "",true,false); - - ar.add_option_value("o","out_file", - "Specify output file.", - &outfile_set,&outfile, - "",true,true); - - ar.add_argument("File from where data is read", - &infile_set,&infile, - ""); +/******************************************************************************/ +/*! + *\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_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, - "",false,true); - + bool spaced=false; //! Help flag for spaced messages. - // If there's a config file mentioned in the environment, take it! - char * env_ldc_config=getenv("LDC_CONFIG"); - if(env_ldc_config){ - fprintf(stderr,"Using config file:\"%s\"\n",env_ldc_config); - if(dump_vector(ar.read_file(env_ldc_config))){ - dump_vector(ar.get_help()); - exit(1); - } - } + string message; //! A warning or help message. - // Process command line first time - if(dump_vector(ar.read_args(argc,args))){ - dump_vector(ar.get_help()); - exit(7); - } - if (do_help) { - dump_vector(ar.get_help()); - exit(0); - } + while(read_ahead){ - // If user has a config file, use it. - if (config_file_set){ - fprintf(stderr,"Using config file:\"%s\"\n",config_file.c_str()); - if(dump_vector(ar.read_file(config_file))){ - dump_vector(ar.get_help()); - exit(7); + 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); + } + + 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; - // 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(7); + 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 (infile_set==1){ - fprintf(stderr,"Opening file for input:%s\n",infile.c_str()); - in_fd=open(infile.c_str(),O_RDONLY); - if (in_fd<0){ - fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",infile.c_str()); - exit (1); + + if (error) fprintf(stderr,message.c_str()); + if (warning) fprintf(stdwarn,message.c_str()); + + 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 (outfile_set==1){ - fprintf(stderr,"Opening file for output:%s\n",outfile.c_str()); - out_fd=open(outfile.c_str(),O_WRONLY|O_TRUNC); - if (out_fd<0){ - fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",outfile.c_str()); - exit (1); + + if (error||warning||cfg_verbose){ + fprintf(fp,"\n"); + spaced=true; } - } - - // The configuration implications according to /cfg007/ - if (in_fd==0) pause_on_checksum_error=0; - if (pause_on_checksum_error==1) ignore_checksum_errors=1; - if (output_info||output_called||output_exported||output_unsatisfied) - list_contents=0; + 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() - - // Now we go to read the data! - vector tape; - vector > objects; - vector current_object; +/******************************************************************************/ +/*! + *\brief Do integrity test and object or block dumps. + */ +void process_tape(){ + if (cfg_verbose) fprintf(stdwarn,"\nProcessing Data.\n"); - 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; - - while(read_ahead){ - block=tape_block::gen_from_fd(in_fd); + 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; iget_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 + + obj_no++; + obj_start_block=i; + + objname="UNKNOWN"; + unknown_count++; + + vectornames=block->get_exported_symbols(); + for (unsigned int a=0; aget_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", + 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; + } + } - 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 (ignore_object_integrity_errors){ - fprintf(stderr,"Warning: Object integrity check failed!\n"); - warnings++; - } else { - fprintf(stderr,"Error: Object integrity check failed!\n"); + // 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=6; + errcode=1; + return; } } - read_ahead=false; - break; - case tape_block::TBS_EOF_ILLEGAL: - delete block; - if (ignore_block_errors){ - fprintf(stderr,"Warning: Block integrity check failed!\n"); - warnings++; - } else { - fprintf(stderr,"Error: Block integrity check failed!\n"); - errors++; - errcode=3; + 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); } - read_ahead=false; - break; - case tape_block::TBS_CHECKSUM: - delete block; - if (ignore_checksum_errors){ - fprintf(stderr,"Warning: Block checksum wrong!\n"); - warnings++; - } else { - fprintf(stderr,"Error: Block checksum wrong!\n"); + catch (tape_block::io_error_exception e){ + fprintf(stderr,"Error: could write to file \"%s\"!\n", + filename); errors++; - read_ahead=false; - errcode=4; + errcode=1; + return; } - 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",infile.c_str()); - } else { - fprintf(stderr,"Error: Could not read from stdin!\n"); + } // if (cfg_split_objects) + + // Output block data + if (cfg_split_objects_numbered){ + try{ + block->dump_to_fd(fd_numbered); } - errors++; - read_ahead=false; - errcode=2; - case tape_block::TBS_OK: - tape.insert(tape.end(),block); - blocks_read++; - - if (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)); + catch (tape_block::io_error_exception e){ + fprintf(stderr,"Error: could write to file \"%s\"!\n", + filename_numbered); + errors++; + errcode=1; + return; } - - if(list_contents) - dump_vector(block->get_description()); - - if (block->get_type()==0x10){ // Unknown data block! - if (ignore_unknown_block_errors){ - fprintf(stderr,"Warning: Unknown block type!\n"); - warnings++; - }else { - fprintf(stderr,"Error: Unknown block type!\n"); + } // 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=5; - read_ahead=false; - } + errcode=1; + close(fd); + close(fd_numbered); + return; } - - 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; + try{ + block->dump_to_fd(fd_block); } - - 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(output_info){ - write (out_fd,buffer,strlen(buffer)); - } else { - fprintf(stderr,buffer); + 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(){ + vectorexported; + vectorcalled; + vector unsatisfied; + + if (cfg_verbose) fprintf(stdwarn,"\nProcessing Symbols.\n"); + + for (unsigned int i=0; iget_exported_symbols()); + merge_vector_unique(called,tape[i]->get_called_symbols()); } - if (errors>0){ - fprintf(stderr,"Errors make me sick. Goodbye!\n"); - exit(errcode); + for (unsigned int icalled=0;icalled0){ + 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() +/******************************************************************************/