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; |
29 | int cfg_splitobjects=0; |
30 | int cfg_splitobjects_numbered=0; |
7880ae2d |
31 | int cfg_ignore_block_errors=0; |
32 | int cfg_ignore_checksum_errors=0; |
ea4c19a4 |
33 | int cfg_pause_on_checksum_error=0; |
34 | int cfg_ignore_unknown_block_errors=0; |
35 | int cfg_ignore_object_integrity_errors=0; |
36 | int cfg_list_contents=1; |
37 | int cfg_verbose=0; |
7880ae2d |
38 | int cfg_version=0; |
ea4c19a4 |
39 | |
40 | int in_fd=0; //! File descriptor for data input |
41 | int out_fd=1; //! File descriptor for text output |
42 | |
43 | |
44 | void do_config(int argc, char ** args){ |
45 | |
46 | string cfg_config_file; |
47 | configuration_manager ar("ldc2"); |
48 | |
49 | /* Here come the configuration switches */ |
50 | ar.add_option_switch("h","help", |
51 | "Output this help text and exit.", |
52 | &cfg_do_help,false,true); |
53 | |
54 | ar.add_option_switch("a","output_info", |
55 | "Print tape data information", |
56 | &cfg_output_info,true,true); |
57 | |
58 | ar.add_option_switch("c","output_called", |
59 | "Print all called symbols from the object(s).", |
60 | &cfg_output_called,true,true); |
61 | |
62 | ar.add_option_switch("e","output_exported", |
63 | "Print all exported symbols from the object(s).", |
64 | &cfg_output_exported,true,true); |
65 | |
66 | ar.add_option_switch("u","output_unsatisfied", |
67 | "List all unsatisfied symbols.", |
68 | &cfg_output_unsatisfied,true,true); |
69 | |
70 | ar.add_option_switch("S","split_objects_numbered", |
71 | "Split input data into distinct numbered files", |
72 | &cfg_splitobjects_numbered,true,true); |
73 | |
74 | ar.add_option_switch("s","split_objects", |
75 | "Split input data into distinct object files.", |
76 | &cfg_splitobjects,true,true); |
77 | |
78 | ar.add_option_switch("b","ignore_block_errors", |
79 | "Ignore block integrity errors. This will output broken blocks,too", |
7880ae2d |
80 | &cfg_ignore_block_errors,true,true); |
ea4c19a4 |
81 | |
82 | ar.add_option_switch("k","ignore_checksum_errors", |
83 | "Ignore block checksum errors. Errors will be converted to warnings.", |
7880ae2d |
84 | &cfg_ignore_checksum_errors,true,true); |
ea4c19a4 |
85 | |
86 | ar.add_option_switch("p","pause_on_checksum_error", |
87 | "Wait for user input on checksum error.", |
88 | &cfg_pause_on_checksum_error,true,true); |
89 | |
90 | ar.add_option_switch("n","ignore_unknown_block_errors", |
91 | "Ignore errors caused by unknown block types. Errors will be converted to warnings.", |
92 | &cfg_ignore_unknown_block_errors,true,true); |
93 | |
94 | ar.add_option_switch("g","ignore_object_integrity_errors", |
95 | "Ignore errors caused by objects without proper end block. \ |
96 | Errors will be converted to warnings.", |
97 | &cfg_ignore_object_integrity_errors,true,true); |
98 | |
99 | ar.add_option_switch("v","verbose", |
100 | "Be a bit more verbose.", |
101 | &cfg_verbose,true,true); |
7880ae2d |
102 | |
103 | ar.add_option_switch("V","version", |
104 | "Output version information and exit.", |
105 | &cfg_version,false,true); |
ea4c19a4 |
106 | |
107 | ar.add_option_value("i","in_file", |
108 | "specify input file", |
109 | &cfg_infile_set,&cfg_infile, |
110 | "<input-file>",true,false); |
111 | |
112 | ar.add_option_value("o","out_file", |
113 | "Specify output file.", |
114 | &cfg_outfile_set,&cfg_outfile, |
115 | "<output_file>",true,true); |
116 | |
117 | ar.add_argument("File from where data is read", |
118 | &cfg_infile_set,&cfg_infile, |
119 | "<input-file>"); |
120 | |
121 | ar.add_argument("File to where output is redirected", |
122 | &cfg_outfile_set,&cfg_outfile, |
123 | "<output-file>"); |
124 | |
125 | ar.add_option_value("F","cfg_config_file", |
126 | "Use the specified configuration file.", |
127 | &cfg_config_file_set,&cfg_config_file, |
128 | "<file-name>",false,true); |
129 | |
130 | |
131 | // If there's a config file mentioned in the environment, take it! |
132 | char * env_ldc_config=getenv("LDC_CONFIG"); |
133 | if(env_ldc_config){ |
134 | fprintf(stderr,"Using config file: \"%s\"\n",env_ldc_config); |
135 | if(dump_vector_err(ar.read_file(env_ldc_config))){ |
136 | dump_vector_err(ar.get_help()); |
137 | exit(1); |
138 | } |
139 | } |
140 | |
141 | // Process command line first time |
142 | if(dump_vector_err(ar.read_args(argc,args))){ |
143 | dump_vector_err(ar.get_help()); |
144 | exit(7); |
145 | } |
146 | |
147 | if (cfg_do_help) { |
148 | dump_vector_err(ar.get_help()); |
149 | exit(0); |
150 | } |
151 | |
152 | // If user has a config file, use it. |
153 | if (cfg_config_file_set){ |
7880ae2d |
154 | if (cfg_verbose) |
155 | fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str()); |
ea4c19a4 |
156 | if(dump_vector_err(ar.read_file(cfg_config_file))){ |
157 | dump_vector_err(ar.get_help()); |
158 | exit(7); |
159 | } |
160 | |
161 | // Process command line a second time to override values from config file. |
162 | if(dump_vector_err(ar.read_args(argc,args))||cfg_do_help){ |
163 | dump_vector_err(ar.get_help()); |
164 | exit(7); |
165 | } |
166 | } |
167 | |
168 | if (cfg_infile_set==1){ |
7880ae2d |
169 | if (cfg_verbose) |
170 | fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str()); |
ea4c19a4 |
171 | in_fd=open(cfg_infile.c_str(),O_RDONLY); |
172 | if (in_fd<0){ |
7880ae2d |
173 | fprintf(stderr,"Error: could not open file \"%s\" for reading!\n", |
174 | cfg_infile.c_str()); |
ea4c19a4 |
175 | exit (1); |
176 | } |
177 | } |
178 | |
179 | if (cfg_outfile_set==1){ |
7880ae2d |
180 | if (cfg_verbose) |
181 | fprintf(stderr,"Opening file for output: \"%s\"\n",cfg_outfile.c_str()); |
182 | out_fd=open(cfg_outfile.c_str(),O_CREAT|O_WRONLY|O_TRUNC,0666); |
ea4c19a4 |
183 | if (out_fd<0){ |
7880ae2d |
184 | fprintf(stderr,"Error: could not open file \"%s\" for writing!\n", |
185 | cfg_outfile.c_str()); |
ea4c19a4 |
186 | exit (1); |
187 | } |
188 | } |
189 | |
190 | // The configuration implications according to /cfg007/ |
191 | if (in_fd==0) cfg_pause_on_checksum_error=0; |
7880ae2d |
192 | if (cfg_pause_on_checksum_error==1) cfg_ignore_checksum_errors=1; |
ea4c19a4 |
193 | |
194 | if (cfg_output_info||cfg_output_called||cfg_output_exported||cfg_output_unsatisfied) |
195 | cfg_list_contents=0; |
196 | } |
197 | |
198 | |