| 1 | /* ldc2 preliminary main program */ |
| 2 | |
| 3 | #include <vector> |
| 4 | #include <string> |
| 5 | |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
| 11 | |
| 12 | #include "tape_block.hh" |
| 13 | #include "data_block_0.hh" |
| 14 | |
| 15 | #include "configuration_manager.hh" |
| 16 | |
| 17 | using namespace std; |
| 18 | |
| 19 | static int in_fd, out_fd; |
| 20 | |
| 21 | |
| 22 | int dump_vector(vector<string> arguments){ |
| 23 | int res=0; |
| 24 | for (vector<string>::iterator iter=arguments.begin();iter<arguments.end();iter++){ |
| 25 | char buffer[2000]; |
| 26 | snprintf(buffer,2000,"%s\n",(*iter).c_str()); |
| 27 | write (out_fd,buffer,strlen(buffer)); |
| 28 | res=1; |
| 29 | } |
| 30 | return res; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /*! |
| 35 | *\brief The main routine. |
| 36 | * |
| 37 | * Here we do the configuration management and process the data. |
| 38 | * |
| 39 | */ |
| 40 | int main(int argc, char ** args){ |
| 41 | |
| 42 | /* Configuration data */ |
| 43 | string config_file; |
| 44 | string infile, outfile; |
| 45 | int infile_set=0, outfile_set=0, config_file_set=0; |
| 46 | |
| 47 | int |
| 48 | do_help=0, |
| 49 | output_info=0, |
| 50 | output_called=0, |
| 51 | output_exported=0, |
| 52 | output_unsatisfied=0, |
| 53 | split_objects=0, |
| 54 | split_objects_numbered=0, |
| 55 | ignore_block_errors=0, |
| 56 | ignore_checksum_errors=0, |
| 57 | pause_on_checksum_error=0, |
| 58 | ignore_unknown_block_errors=0, |
| 59 | ignore_object_integrity_errors=0, |
| 60 | list_contents=1, |
| 61 | verbose=0; |
| 62 | |
| 63 | in_fd=0; /* stdin {O _ \ */ |
| 64 | out_fd=1; /* stdout {O ^ / */ |
| 65 | |
| 66 | configuration_manager ar("ldc2"); |
| 67 | |
| 68 | /* Here come the configuration switches */ |
| 69 | ar.add_option_switch("h","help", |
| 70 | "Output this help text and exit.", |
| 71 | &do_help,false,true); |
| 72 | |
| 73 | ar.add_option_switch("a","output_info", |
| 74 | "Print tape data information (default)", |
| 75 | &output_info,true,true); |
| 76 | |
| 77 | ar.add_option_switch("c","output_called", |
| 78 | "Print all called symbols from the object(s).", |
| 79 | &output_called,true,true); |
| 80 | |
| 81 | ar.add_option_switch("e","output_exported", |
| 82 | "Print all exported symbols from the object(s).", |
| 83 | &output_exported,true,true); |
| 84 | |
| 85 | ar.add_option_switch("u","output_unsatisfied", |
| 86 | "List all unsatisfied symbols.", |
| 87 | &output_unsatisfied,true,true); |
| 88 | |
| 89 | ar.add_option_switch("S","split_objects_numbered", |
| 90 | "Split input data into distinct numbered files", |
| 91 | &split_objects_numbered,true,true); |
| 92 | |
| 93 | ar.add_option_switch("s","split_objects", |
| 94 | "Split input data into distinct object files.", |
| 95 | &split_objects,true,true); |
| 96 | |
| 97 | ar.add_option_switch("b","ignore_block_errors", |
| 98 | "Ignore block integrity errors. This will output broken blocks,too", |
| 99 | &ignore_block_errors,true,true); |
| 100 | |
| 101 | ar.add_option_switch("k","ignore_checksum_errors", |
| 102 | "Ignore block checksum errors. Errors will be converted to warnings.", |
| 103 | &ignore_checksum_errors,true,true); |
| 104 | |
| 105 | ar.add_option_switch("p","pause_on_checksum_error", |
| 106 | "Wait for user input on checksum error.", |
| 107 | &pause_on_checksum_error,true,true); |
| 108 | |
| 109 | ar.add_option_switch("n","ignore_unknown_block_errors", |
| 110 | "Ignore errors caused by unknown block types. Errors will be converted to warnings.", |
| 111 | &ignore_unknown_block_errors,true,true); |
| 112 | |
| 113 | ar.add_option_switch("g","ignore_object_integrity_errors", |
| 114 | "Ignore errors caused by objects without proper end block. \ |
| 115 | Errors will be converted to warnings.", |
| 116 | &ignore_object_integrity_errors,true,true); |
| 117 | |
| 118 | ar.add_option_switch("v","verbose", |
| 119 | "Be a bit more verbose.", |
| 120 | &verbose,true,true); |
| 121 | |
| 122 | ar.add_option_value("i","in_file", |
| 123 | "specify input file", |
| 124 | &infile_set,&infile, |
| 125 | "<input-file>",true,false); |
| 126 | |
| 127 | ar.add_option_value("o","out_file", |
| 128 | "Specify output file.", |
| 129 | &outfile_set,&outfile, |
| 130 | "<output_file>",true,true); |
| 131 | |
| 132 | ar.add_argument("File from where data is read", |
| 133 | &infile_set,&infile, |
| 134 | "<input-file>"); |
| 135 | |
| 136 | ar.add_argument("File to where output is redirected", |
| 137 | &outfile_set,&outfile, |
| 138 | "<output-file>"); |
| 139 | |
| 140 | ar.add_option_value("F","config_file", |
| 141 | "Use the specified configuration file.", |
| 142 | &config_file_set,&config_file, |
| 143 | "<file-name>",false,true); |
| 144 | |
| 145 | |
| 146 | // If there's a config file mentioned in the environment, take it! |
| 147 | char * env_ldc_config=getenv("LDC_CONFIG"); |
| 148 | if(env_ldc_config){ |
| 149 | fprintf(stderr,"Using config file:\"%s\"\n",env_ldc_config); |
| 150 | if(dump_vector(ar.read_file(env_ldc_config))){ |
| 151 | dump_vector(ar.get_help()); |
| 152 | exit(1); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Process command line first time |
| 157 | if(dump_vector(ar.read_args(argc,args))){ |
| 158 | dump_vector(ar.get_help()); |
| 159 | exit(7); |
| 160 | } |
| 161 | |
| 162 | if (do_help) { |
| 163 | dump_vector(ar.get_help()); |
| 164 | exit(0); |
| 165 | } |
| 166 | |
| 167 | // If user has a config file, use it. |
| 168 | if (config_file_set){ |
| 169 | fprintf(stderr,"Using config file:\"%s\"\n",config_file.c_str()); |
| 170 | if(dump_vector(ar.read_file(config_file))){ |
| 171 | dump_vector(ar.get_help()); |
| 172 | exit(7); |
| 173 | } |
| 174 | |
| 175 | // Process command line a second time to override values from config file. |
| 176 | if(dump_vector(ar.read_args(argc,args))||do_help){ |
| 177 | dump_vector(ar.get_help()); |
| 178 | exit(7); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (infile_set==1){ |
| 183 | fprintf(stderr,"Opening file for input:%s\n",infile.c_str()); |
| 184 | in_fd=open(infile.c_str(),O_RDONLY); |
| 185 | if (in_fd<0){ |
| 186 | fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",infile.c_str()); |
| 187 | exit (1); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (outfile_set==1){ |
| 192 | fprintf(stderr,"Opening file for output:%s\n",outfile.c_str()); |
| 193 | out_fd=open(outfile.c_str(),O_WRONLY|O_TRUNC); |
| 194 | if (out_fd<0){ |
| 195 | fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",outfile.c_str()); |
| 196 | exit (1); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // The configuration implications according to /cfg007/ |
| 201 | if (in_fd==0) pause_on_checksum_error=0; |
| 202 | if (pause_on_checksum_error==1) ignore_checksum_errors=1; |
| 203 | if (output_info||output_called||output_exported||output_unsatisfied) |
| 204 | list_contents=0; |
| 205 | |
| 206 | |
| 207 | // Now we go to read the data! |
| 208 | |
| 209 | vector<tape_block*> tape; |
| 210 | vector<vector<tape_block *> > objects; |
| 211 | vector<tape_block *> current_object; |
| 212 | |
| 213 | tape_block * block=0; |
| 214 | bool read_ahead=true; |
| 215 | int read_pointer=0; |
| 216 | int block_start=0; |
| 217 | int block_end=0; |
| 218 | int blocks_read=0; |
| 219 | bool in_object=false; |
| 220 | int errors=0; |
| 221 | int warnings=0; |
| 222 | int errcode=0; |
| 223 | |
| 224 | while(read_ahead){ |
| 225 | block=tape_block::gen_from_fd(in_fd); |
| 226 | |
| 227 | block_start=read_pointer+block->get_discarded_bytes(); |
| 228 | block_end=block_start+block->get_raw_size()-1; |
| 229 | read_pointer=block_end+1; |
| 230 | //printf("Discarded:%2x, Raw Size:%2x\n",block->get_discarded_bytes(),block->get_raw_size()); |
| 231 | switch(block->get_state()){ // switchy |
| 232 | case tape_block::TBS_EOF_LEGAL: |
| 233 | delete block; |
| 234 | if (!in_object){ |
| 235 | fprintf(stderr,"File successfully read.\n"); |
| 236 | } else { |
| 237 | if (ignore_object_integrity_errors){ |
| 238 | fprintf(stderr,"Warning: Object integrity check failed!\n"); |
| 239 | warnings++; |
| 240 | } else { |
| 241 | fprintf(stderr,"Error: Object integrity check failed!\n"); |
| 242 | errors++; |
| 243 | errcode=6; |
| 244 | } |
| 245 | } |
| 246 | read_ahead=false; |
| 247 | break; |
| 248 | case tape_block::TBS_EOF_ILLEGAL: |
| 249 | delete block; |
| 250 | if (ignore_block_errors){ |
| 251 | fprintf(stderr,"Warning: Block integrity check failed!\n"); |
| 252 | warnings++; |
| 253 | } else { |
| 254 | fprintf(stderr,"Error: Block integrity check failed!\n"); |
| 255 | errors++; |
| 256 | errcode=3; |
| 257 | } |
| 258 | read_ahead=false; |
| 259 | break; |
| 260 | case tape_block::TBS_CHECKSUM: |
| 261 | delete block; |
| 262 | if (ignore_checksum_errors){ |
| 263 | fprintf(stderr,"Warning: Block checksum wrong!\n"); |
| 264 | warnings++; |
| 265 | } else { |
| 266 | fprintf(stderr,"Error: Block checksum wrong!\n"); |
| 267 | errors++; |
| 268 | read_ahead=false; |
| 269 | errcode=4; |
| 270 | } |
| 271 | break; |
| 272 | case tape_block::TBS_DEFAULT: |
| 273 | delete block; |
| 274 | fprintf(stderr,"TBS_DEFAULT encountered ->> SEVERE INTERNAL ERROR!\n"); |
| 275 | errors++; |
| 276 | errcode=100; |
| 277 | read_ahead=false; |
| 278 | break; |
| 279 | case tape_block::TBS_IOERR: |
| 280 | delete block; |
| 281 | if (in_fd){ |
| 282 | fprintf(stderr,"Error: Could not read from \"%s\"!\n",infile.c_str()); |
| 283 | } else { |
| 284 | fprintf(stderr,"Error: Could not read from stdin!\n"); |
| 285 | } |
| 286 | errors++; |
| 287 | read_ahead=false; |
| 288 | errcode=2; |
| 289 | case tape_block::TBS_OK: |
| 290 | tape.insert(tape.end(),block); |
| 291 | blocks_read++; |
| 292 | |
| 293 | if (verbose){ |
| 294 | char buffer[200]; |
| 295 | snprintf(buffer,200,"Block No. %3i: Start(hex):%5x End(hex):%5x Size(hex):%3x\n", |
| 296 | blocks_read-1, |
| 297 | block_start, |
| 298 | block_end, |
| 299 | block->get_raw_size()); |
| 300 | write (out_fd,buffer,strlen(buffer)); |
| 301 | } |
| 302 | |
| 303 | if(list_contents) |
| 304 | dump_vector(block->get_description()); |
| 305 | |
| 306 | if (block->get_type()==0x10){ // Unknown data block! |
| 307 | if (ignore_unknown_block_errors){ |
| 308 | fprintf(stderr,"Warning: Unknown block type!\n"); |
| 309 | warnings++; |
| 310 | }else { |
| 311 | fprintf(stderr,"Error: Unknown block type!\n"); |
| 312 | errors++; |
| 313 | errcode=5; |
| 314 | read_ahead=false; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | current_object.insert(current_object.end(),block); |
| 319 | |
| 320 | if ((block->get_type()==data_block::TBT_EOT) |
| 321 | ||((block->get_type()==0)&&(block->get_subtype()==3)) |
| 322 | ||((block->get_type()==4)||(block->get_type()==3)) |
| 323 | ||((block->get_type()==0)&&(block->get_subtype()==014))){ |
| 324 | in_object=false; |
| 325 | objects.insert(objects.end(),current_object); |
| 326 | current_object.clear(); |
| 327 | } else { |
| 328 | in_object=true; |
| 329 | } |
| 330 | |
| 331 | break; |
| 332 | } // switch |
| 333 | } // generate loop |
| 334 | close(in_fd); |
| 335 | |
| 336 | // Now we have our blocks, tell the user about that! |
| 337 | char buffer[200]; |
| 338 | sprintf(buffer,"Bytes read:%i Blocks read:%i\nErrors:%i Warnings:%i\n", |
| 339 | read_pointer,blocks_read,errors,warnings); |
| 340 | if(output_info){ |
| 341 | write (out_fd,buffer,strlen(buffer)); |
| 342 | } else { |
| 343 | fprintf(stderr,buffer); |
| 344 | } |
| 345 | |
| 346 | if (errors>0){ |
| 347 | fprintf(stderr,"Errors make me sick. Goodbye!\n"); |
| 348 | exit(errcode); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | return 0; |
| 353 | } // main() |
| 354 | |