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