| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * LDC2 source code |
| 4 | * |
| 5 | * $Date: 2007/12/23 15:25:11 $ |
| 6 | * $Author: hachti $ |
| 7 | * |
| 8 | * $Log: config.cpp,v $ |
| 9 | * Revision 2.1 2007/12/23 15:25:11 hachti |
| 10 | * *** empty log message *** |
| 11 | * |
| 12 | * Revision 2.0 2007-03-26 01:00:38 hachti |
| 13 | * *** empty log message *** |
| 14 | * |
| 15 | * |
| 16 | ******************************************************************************/ |
| 17 | |
| 18 | /* |
| 19 | * Program configuration management |
| 20 | */ |
| 21 | |
| 22 | #include <vector> |
| 23 | #include <string> |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <fcntl.h> |
| 30 | |
| 31 | #include "configuration_manager.hh" |
| 32 | #include "tool.hh" |
| 33 | |
| 34 | /* Global configuration data */ |
| 35 | string cfg_infile; |
| 36 | string cfg_outfile; |
| 37 | int cfg_infile_set=0; |
| 38 | int cfg_outfile_set=0; |
| 39 | int cfg_config_file_set=0; |
| 40 | |
| 41 | int cfg_do_help=0; |
| 42 | int cfg_output_info=0; |
| 43 | int cfg_output_called=0; |
| 44 | int cfg_output_exported=0; |
| 45 | int cfg_output_unsatisfied=0; |
| 46 | int cfg_split_objects=0; |
| 47 | int cfg_split_objects_numbered=0; |
| 48 | int cfg_split_blocks=0; |
| 49 | int cfg_ignore_block_errors=0; |
| 50 | int cfg_ignore_checksum_errors=0; |
| 51 | //int cfg_pause_on_checksum_error=0; |
| 52 | int cfg_ignore_unknown_block_errors=0; |
| 53 | int cfg_ignore_object_integrity_errors=0; |
| 54 | int cfg_list_contents=1; |
| 55 | int cfg_verbose=0; |
| 56 | int cfg_quiet=0; |
| 57 | int cfg_dump_block_contents=0; |
| 58 | |
| 59 | int cfg_version=0; |
| 60 | |
| 61 | int in_fd=0; //! File descriptor for data input |
| 62 | int out_fd=1; //! File descriptor for text output |
| 63 | |
| 64 | void do_config(int argc, char ** args){ |
| 65 | |
| 66 | string cfg_config_file; |
| 67 | configuration_manager ar("ldc2"); |
| 68 | |
| 69 | /* Here come the configuration switches */ |
| 70 | ar.add_option_switch("h","help", |
| 71 | "Output this help text and exit.", |
| 72 | &cfg_do_help,false,true); |
| 73 | |
| 74 | ar.add_option_switch("a","output_info", |
| 75 | "Print tape data information", |
| 76 | &cfg_output_info,true,true); |
| 77 | |
| 78 | ar.add_option_switch("c","output_called", |
| 79 | "Print all called symbols from the object(s).", |
| 80 | &cfg_output_called,true,true); |
| 81 | |
| 82 | ar.add_option_switch("d","dump_block_contents", |
| 83 | "Dump block contents", |
| 84 | &cfg_dump_block_contents,true,true); |
| 85 | |
| 86 | ar.add_option_switch("e","output_exported", |
| 87 | "Print all exported symbols from the object(s).", |
| 88 | &cfg_output_exported,true,true); |
| 89 | |
| 90 | ar.add_option_switch("u","output_unsatisfied", |
| 91 | "List all unsatisfied symbols.", |
| 92 | &cfg_output_unsatisfied,true,true); |
| 93 | |
| 94 | ar.add_option_switch("s","split_objects", |
| 95 | "Split input data into distinct object files.", |
| 96 | &cfg_split_objects,true,true); |
| 97 | |
| 98 | ar.add_option_switch("S","split_objects_numbered", |
| 99 | "Split input data into distinct numbered files", |
| 100 | &cfg_split_objects_numbered,true,true); |
| 101 | |
| 102 | ar.add_option_switch("B","split_blocks", |
| 103 | "Split tape into named and numbered block files", |
| 104 | &cfg_split_blocks,true,true); |
| 105 | |
| 106 | ar.add_option_switch("b","ignore_block_errors", |
| 107 | "Ignore block integrity errors. This will output broken blocks,too", |
| 108 | &cfg_ignore_block_errors,true,true); |
| 109 | |
| 110 | ar.add_option_switch("k","ignore_checksum_errors", |
| 111 | "Ignore block checksum errors. Errors will be converted to warnings.", |
| 112 | &cfg_ignore_checksum_errors,true,true); |
| 113 | |
| 114 | // ar.add_option_switch("p","pause_on_checksum_error", |
| 115 | // "Wait for user input on checksum error.", |
| 116 | // &cfg_pause_on_checksum_error,true,true); |
| 117 | |
| 118 | ar.add_option_switch("n","ignore_unknown_block_errors", |
| 119 | "Ignore errors caused by unknown block types. Errors will be converted to warnings.", |
| 120 | &cfg_ignore_unknown_block_errors,true,true); |
| 121 | |
| 122 | ar.add_option_switch("g","ignore_object_integrity_errors", |
| 123 | "Ignore errors caused by objects without proper end block. \ |
| 124 | Errors will be converted to warnings.", |
| 125 | &cfg_ignore_object_integrity_errors,true,true); |
| 126 | |
| 127 | ar.add_option_switch("v","verbose", |
| 128 | "Be a bit more verbose. This overrides -q.", |
| 129 | &cfg_verbose,true,true); |
| 130 | |
| 131 | ar.add_option_switch("q","quiet", |
| 132 | "Be quiet. All warning messages are suppressed.", |
| 133 | &cfg_quiet,true,true); |
| 134 | |
| 135 | ar.add_option_switch("V","version", |
| 136 | "Output version information and exit.", |
| 137 | &cfg_version,false,true); |
| 138 | |
| 139 | ar.add_option_value("i","in_file", |
| 140 | "specify input file", |
| 141 | &cfg_infile_set,&cfg_infile, |
| 142 | "<input-file>",true,false); |
| 143 | |
| 144 | ar.add_option_value("o","out_file", |
| 145 | "Specify output file.", |
| 146 | &cfg_outfile_set,&cfg_outfile, |
| 147 | "<output_file>",true,true); |
| 148 | |
| 149 | ar.add_argument("File from where data is read", |
| 150 | &cfg_infile_set,&cfg_infile, |
| 151 | "<input-file>"); |
| 152 | |
| 153 | ar.add_argument("File to where output is redirected", |
| 154 | &cfg_outfile_set,&cfg_outfile, |
| 155 | "<output-file>"); |
| 156 | |
| 157 | ar.add_option_value("F","cfg_config_file", |
| 158 | "Use the specified configuration file.", |
| 159 | &cfg_config_file_set,&cfg_config_file, |
| 160 | "<file-name>",false,true); |
| 161 | |
| 162 | |
| 163 | // If there's a config file mentioned in the environment, take it! |
| 164 | char * env_ldc_config=getenv("LDC_CONFIG"); |
| 165 | if(env_ldc_config){ |
| 166 | if (cfg_verbose) fprintf(stderr,"Using config file: \"%s\"\n",env_ldc_config); |
| 167 | if(dump_vector_fp(ar.read_file(env_ldc_config),stderr)){ |
| 168 | dump_vector_fp(ar.get_help(),stderr); |
| 169 | exit(1); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Process command line first time |
| 174 | if(dump_vector_fp(ar.read_args(argc,args),stderr)){ |
| 175 | dump_vector_fp(ar.get_help(),stderr); |
| 176 | exit(7); |
| 177 | } |
| 178 | |
| 179 | if (cfg_do_help) { |
| 180 | dump_vector_fp(ar.get_help(),stderr); |
| 181 | exit(0); |
| 182 | } |
| 183 | |
| 184 | // If user has a config file, use it. |
| 185 | if (cfg_config_file_set){ |
| 186 | if (cfg_verbose) |
| 187 | fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str()); |
| 188 | if(dump_vector_fp(ar.read_file(cfg_config_file),stderr)){ |
| 189 | dump_vector_fp(ar.get_help(),stderr); |
| 190 | exit(7); |
| 191 | } |
| 192 | |
| 193 | // Process command line a second time to override values from config file. |
| 194 | if(dump_vector_fp(ar.read_args(argc,args),stderr)||cfg_do_help){ |
| 195 | dump_vector_fp(ar.get_help(),stderr); |
| 196 | exit(7); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (cfg_infile_set==1){ |
| 201 | if (cfg_verbose) |
| 202 | fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str()); |
| 203 | in_fd=open(cfg_infile.c_str(),O_RDONLY); |
| 204 | if (in_fd<0){ |
| 205 | fprintf(stderr,"Error: could not open file \"%s\" for reading!\n", |
| 206 | cfg_infile.c_str()); |
| 207 | exit (1); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (cfg_outfile_set==1){ |
| 212 | if (cfg_verbose) |
| 213 | fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str()); |
| 214 | out_fd=open(cfg_outfile.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666); |
| 215 | if (out_fd<0){ |
| 216 | fprintf(stderr,"Error: could not open file \"%s\" for writing!\n", |
| 217 | cfg_outfile.c_str()); |
| 218 | exit (1); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Verbose overrides quiet: |
| 223 | if (cfg_verbose)cfg_quiet=0; |
| 224 | |
| 225 | // The configuration implications according to /cfg007/ |
| 226 | // if (in_fd==0) cfg_pause_on_checksum_error=0; |
| 227 | // if (cfg_pause_on_checksum_error==1) cfg_ignore_checksum_errors=1; |
| 228 | |
| 229 | if (cfg_output_info||cfg_output_called||cfg_output_exported|| |
| 230 | cfg_output_unsatisfied) cfg_list_contents=0; |
| 231 | } |
| 232 | |
| 233 | |