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