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