*** empty log message ***
[h316.git] / pc-tools / ldc2 / src / main.cpp
CommitLineData
798b0c1d 1/* ldc2 preliminary main program */
2
3#include <vector>
4#include <string>
f9d603d2 5
97b26985 6#include <stdio.h>
7#include <unistd.h>
ad324d29 8#include <sys/types.h>
9#include <sys/stat.h>
10#include <fcntl.h>
97b26985 11
12#include "tape_block.hh"
fed2c751 13#include "data_block_0.hh"
14
909d3603 15#include "configuration_manager.hh"
798b0c1d 16
17using namespace std;
18
fed2c751 19static int in_fd, out_fd;
20
21
ad324d29 22int dump_vector(vector<string> arguments){
23 int res=0;
798b0c1d 24 for (vector<string>::iterator iter=arguments.begin();iter<arguments.end();iter++){
fed2c751 25 char buffer[2000];
26 snprintf(buffer,2000,"%s\n",(*iter).c_str());
27 write (out_fd,buffer,strlen(buffer));
ad324d29 28 res=1;
798b0c1d 29 }
ad324d29 30 return res;
798b0c1d 31}
32
ad324d29 33
fed2c751 34/*!
35 *\brief The main routine.
36 *
37 * Here we do the configuration management and process the data.
38 *
39 */
798b0c1d 40int main(int argc, char ** args){
fed2c751 41
909d3603 42 /* Configuration data */
43 string config_file;
ad324d29 44 string infile, outfile;
fed2c751 45 int infile_set=0, outfile_set=0, config_file_set=0;
ad324d29 46
909d3603 47 int
fed2c751 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;
ad324d29 62
909d3603 63 in_fd=0; /* stdin {O _ \ */
64 out_fd=1; /* stdout {O ^ / */
798b0c1d 65
909d3603 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.",
fed2c751 71 &do_help,false,true);
909d3603 72
73 ar.add_option_switch("a","output_info",
74 "Print tape data information (default)",
fed2c751 75 &output_info,true,true);
909d3603 76
77 ar.add_option_switch("c","output_called",
78 "Print all called symbols from the object(s).",
fed2c751 79 &output_called,true,true);
909d3603 80
81 ar.add_option_switch("e","output_exported",
82 "Print all exported symbols from the object(s).",
fed2c751 83 &output_exported,true,true);
909d3603 84
85 ar.add_option_switch("u","output_unsatisfied",
86 "List all unsatisfied symbols.",
fed2c751 87 &output_unsatisfied,true,true);
909d3603 88
89 ar.add_option_switch("S","split_objects_numbered",
90 "Split input data into distinct numbered files",
fed2c751 91 &split_objects_numbered,true,true);
909d3603 92
fed2c751 93 ar.add_option_switch("s","split_objects",
94 "Split input data into distinct object files.",
95 &split_objects,true,true);
96
909d3603 97 ar.add_option_switch("b","ignore_block_errors",
98 "Ignore block integrity errors. This will output broken blocks,too",
fed2c751 99 &ignore_block_errors,true,true);
909d3603 100
101 ar.add_option_switch("k","ignore_checksum_errors",
102 "Ignore block checksum errors. Errors will be converted to warnings.",
fed2c751 103 &ignore_checksum_errors,true,true);
909d3603 104
105 ar.add_option_switch("p","pause_on_checksum_error",
106 "Wait for user input on checksum error.",
fed2c751 107 &pause_on_checksum_error,true,true);
909d3603 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.",
fed2c751 111 &ignore_unknown_block_errors,true,true);
909d3603 112
113 ar.add_option_switch("g","ignore_object_integrity_errors",
114 "Ignore errors caused by objects without proper end block. \
115Errors will be converted to warnings.",
fed2c751 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);
909d3603 121
fed2c751 122 ar.add_option_value("i","in_file",
909d3603 123 "specify input file",
124 &infile_set,&infile,
fed2c751 125 "<input-file>",true,false);
909d3603 126
fed2c751 127 ar.add_option_value("o","out_file",
909d3603 128 "Specify output file.",
129 &outfile_set,&outfile,
fed2c751 130 "<output_file>",true,true);
131
909d3603 132 ar.add_argument("File from where data is read",
133 &infile_set,&infile,
134 "<input-file>");
fed2c751 135
909d3603 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,
fed2c751 143 "<file-name>",false,true);
144
909d3603 145
fed2c751 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
50c86ded 156 // Process command line first time
fed2c751 157 if(dump_vector(ar.read_args(argc,args))){
632a71a2 158 dump_vector(ar.get_help());
fed2c751 159 exit(7);
632a71a2 160 }
ad324d29 161
fed2c751 162 if (do_help) {
163 dump_vector(ar.get_help());
164 exit(0);
165 }
166
50c86ded 167 // If user has a config file, use it.
168 if (config_file_set){
fed2c751 169 fprintf(stderr,"Using config file:\"%s\"\n",config_file.c_str());
50c86ded 170 if(dump_vector(ar.read_file(config_file))){
171 dump_vector(ar.get_help());
fed2c751 172 exit(7);
50c86ded 173 }
fed2c751 174
50c86ded 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());
fed2c751 178 exit(7);
50c86ded 179 }
180 }
181
ad324d29 182 if (infile_set==1){
fed2c751 183 fprintf(stderr,"Opening file for input:%s\n",infile.c_str());
ad324d29 184 in_fd=open(infile.c_str(),O_RDONLY);
185 if (in_fd<0){
fed2c751 186 fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",infile.c_str());
187 exit (1);
ad324d29 188 }
189 }
190
fed2c751 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 }
909d3603 199
fed2c751 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
ad324d29 209 vector<tape_block*> tape;
fed2c751 210 vector<vector<tape_block *> > objects;
211 vector<tape_block *> current_object;
ad324d29 212
213 tape_block * block=0;
fed2c751 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){
ad324d29 225 block=tape_block::gen_from_fd(in_fd);
fed2c751 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:
ad324d29 290 tape.insert(tape.end(),block);
fed2c751 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);
798b0c1d 335
fed2c751 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);
ad324d29 349 }
350
fed2c751 351
ad324d29 352 return 0;
353} // main()
798b0c1d 354