| 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 "configuration_manager.hh" |
| 14 | |
| 15 | using namespace std; |
| 16 | |
| 17 | int dump_vector(vector<string> arguments){ |
| 18 | int res=0; |
| 19 | for (vector<string>::iterator iter=arguments.begin();iter<arguments.end();iter++){ |
| 20 | printf("%s\n",(*iter).c_str()); |
| 21 | res=1; |
| 22 | } |
| 23 | return res; |
| 24 | } |
| 25 | |
| 26 | |
| 27 | int main(int argc, char ** args){ |
| 28 | |
| 29 | /* Configuration data */ |
| 30 | string config_file; |
| 31 | string infile, outfile; |
| 32 | int infile_set, outfile_set, config_file_set; |
| 33 | |
| 34 | int |
| 35 | do_help, |
| 36 | output_info, |
| 37 | output_called, |
| 38 | output_exported, |
| 39 | output_unsatisfied, |
| 40 | split_objects, |
| 41 | split_objects_numbered, |
| 42 | ignore_block_errors, |
| 43 | ignore_checksum_errors, |
| 44 | pause_on_checksum_error, |
| 45 | ignore_unknown_block_errors, |
| 46 | ignore_object_integrity_errors; |
| 47 | |
| 48 | int in_fd, out_fd; |
| 49 | |
| 50 | in_fd=0; /* stdin {O _ \ */ |
| 51 | out_fd=1; /* stdout {O ^ / */ |
| 52 | |
| 53 | configuration_manager ar("ldc2"); |
| 54 | |
| 55 | /* Here come the configuration switches */ |
| 56 | ar.add_option_switch("h","help", |
| 57 | "Output this help text and exit.", |
| 58 | &do_help,true,false); |
| 59 | |
| 60 | ar.add_option_switch("a","output_info", |
| 61 | "Print tape data information (default)", |
| 62 | &output_info); |
| 63 | |
| 64 | ar.add_option_switch("c","output_called", |
| 65 | "Print all called symbols from the object(s).", |
| 66 | &output_called); |
| 67 | |
| 68 | ar.add_option_switch("e","output_exported", |
| 69 | "Print all exported symbols from the object(s).", |
| 70 | &output_exported); |
| 71 | |
| 72 | ar.add_option_switch("u","output_unsatisfied", |
| 73 | "List all unsatisfied symbols.", |
| 74 | &output_unsatisfied); |
| 75 | |
| 76 | ar.add_option_switch("s","split_objects", |
| 77 | "Split input data into distinct object files.", |
| 78 | &split_objects); |
| 79 | |
| 80 | ar.add_option_switch("S","split_objects_numbered", |
| 81 | "Split input data into distinct numbered files", |
| 82 | &split_objects_numbered); |
| 83 | |
| 84 | ar.add_option_switch("b","ignore_block_errors", |
| 85 | "Ignore block integrity errors. This will output broken blocks,too", |
| 86 | &ignore_block_errors); |
| 87 | |
| 88 | ar.add_option_switch("k","ignore_checksum_errors", |
| 89 | "Ignore block checksum errors. Errors will be converted to warnings.", |
| 90 | &ignore_checksum_errors); |
| 91 | |
| 92 | ar.add_option_switch("p","pause_on_checksum_error", |
| 93 | "Wait for user input on checksum error.", |
| 94 | &pause_on_checksum_error); |
| 95 | |
| 96 | ar.add_option_switch("n","ignore_unknown_block_errors", |
| 97 | "Ignore errors caused by unknown block types. Errors will be converted to warnings.", |
| 98 | &ignore_unknown_block_errors); |
| 99 | |
| 100 | ar.add_option_switch("g","ignore_object_integrity_errors", |
| 101 | "Ignore errors caused by objects without proper end block. \ |
| 102 | Errors will be converted to warnings.", |
| 103 | &ignore_object_integrity_errors); |
| 104 | |
| 105 | ar.add_option_value("i","input_file", |
| 106 | "specify input file", |
| 107 | &infile_set,&infile, |
| 108 | "<input-file>"); |
| 109 | |
| 110 | ar.add_option_value("o","output_file", |
| 111 | "Specify output file.", |
| 112 | &outfile_set,&outfile, |
| 113 | "<output_file>"); |
| 114 | |
| 115 | |
| 116 | ar.add_argument("File from where data is read", |
| 117 | &infile_set,&infile, |
| 118 | "<input-file>"); |
| 119 | |
| 120 | ar.add_argument("File to where output is redirected", |
| 121 | &outfile_set,&outfile, |
| 122 | "<output-file>"); |
| 123 | |
| 124 | ar.add_option_value("F","config_file", |
| 125 | "Use the specified configuration file.", |
| 126 | &config_file_set,&config_file, |
| 127 | "<file-name>"); |
| 128 | |
| 129 | // Process command line first time |
| 130 | if((dump_vector(ar.read_args(argc,args))||do_help)){ |
| 131 | dump_vector(ar.get_help()); |
| 132 | exit(1); |
| 133 | } |
| 134 | |
| 135 | // If user has a config file, use it. |
| 136 | if (config_file_set){ |
| 137 | if(dump_vector(ar.read_file(config_file))){ |
| 138 | dump_vector(ar.get_help()); |
| 139 | exit(1); |
| 140 | } |
| 141 | // Process command line a second time to override values from config file. |
| 142 | if(dump_vector(ar.read_args(argc,args))||do_help){ |
| 143 | dump_vector(ar.get_help()); |
| 144 | exit(1); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | if (infile_set==1){ |
| 152 | printf("Opening file for input:%s\n",infile.c_str()); |
| 153 | in_fd=open(infile.c_str(),O_RDONLY); |
| 154 | if (in_fd<0){ |
| 155 | printf("Error: could not open file:%s\n",infile.c_str()); |
| 156 | exit (3); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | vector<tape_block*> tape; |
| 164 | |
| 165 | |
| 166 | tape_block * block=0; |
| 167 | while(1){ |
| 168 | block=tape_block::gen_from_fd(in_fd); |
| 169 | if (block->get_state()==tape_block::TBS_OK){ |
| 170 | tape.insert(tape.end(),block); |
| 171 | dump_vector(block->get_description()); |
| 172 | } |
| 173 | else break; |
| 174 | } |
| 175 | |
| 176 | switch(block->get_state()){ |
| 177 | case tape_block::TBS_EOF_LEGAL: printf("File successfully read.\n"); |
| 178 | break; |
| 179 | case tape_block::TBS_EOF_ILLEGAL: printf("EOF while in block!\n"); |
| 180 | break; |
| 181 | case tape_block::TBS_CHECKSUM: |
| 182 | printf("Checksum error!\n"); |
| 183 | break; |
| 184 | case tape_block::TBS_DEFAULT: |
| 185 | printf("TBS_DEFAULT encountered ->> SEVERY INTERNAL ERROR!\n"); |
| 186 | exit(88); |
| 187 | case tape_block::TBS_IOERR: |
| 188 | printf("I/O Error... Why?\n"); |
| 189 | exit (43); |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } // main() |
| 194 | |