Added some includes
[h316.git] / pc-tools / ldc2 / src / config.cpp
1 /******************************************************************************
2 *
3 * LDC2 source code
4 *
5 * $Date: 2008/10/01 13:30:14 $
6 * $Author: hachti $
7 *
8 * $Log: config.cpp,v $
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
13 * *** empty log message ***
14 *
15 * Revision 2.1 2007-12-23 15:25:11 hachti
16 * *** empty log message ***
17 *
18 * Revision 2.0 2007-03-26 01:00:38 hachti
19 * *** empty log message ***
20 *
21 *
22 ******************************************************************************/
23
24 /*
25 * Program configuration management
26 */
27
28 #include <vector>
29 #include <string>
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
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;
54 int cfg_split_objects=0;
55 int cfg_split_objects_numbered=0;
56 int cfg_split_blocks=0;
57 int cfg_ignore_block_errors=0;
58 int cfg_ignore_checksum_errors=0;
59 //int cfg_pause_on_checksum_error=0;
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;
64 int cfg_quiet=0;
65 int cfg_dump_block_contents=0;
66
67 int cfg_version=0;
68
69 int in_fd=0; //! File descriptor for data input
70 int out_fd=1; //! File descriptor for text output
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
90 ar.add_option_switch("d","dump_block_contents",
91 "Dump block contents",
92 &cfg_dump_block_contents,true,true);
93
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
102 ar.add_option_switch("s","split_objects",
103 "Split input data into distinct object files.",
104 &cfg_split_objects,true,true);
105
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
114 ar.add_option_switch("b","ignore_block_errors",
115 "Ignore block integrity errors. This will output broken blocks,too",
116 &cfg_ignore_block_errors,true,true);
117
118 ar.add_option_switch("k","ignore_checksum_errors",
119 "Ignore block checksum errors. Errors will be converted to warnings.",
120 &cfg_ignore_checksum_errors,true,true);
121
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);
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",
136 "Be a bit more verbose. This overrides -q.",
137 &cfg_verbose,true,true);
138
139 ar.add_option_switch("q","quiet",
140 "Be quiet. All warning messages are suppressed.",
141 &cfg_quiet,true,true);
142
143 ar.add_option_switch("V","version",
144 "Output version information and exit.",
145 &cfg_version,false,true);
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){
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);
177 exit(1);
178 }
179 }
180
181 // Process command line first time
182 if(dump_vector_fp(ar.read_args(argc,args),stderr)){
183 dump_vector_fp(ar.get_help(),stderr);
184 exit(7);
185 }
186
187 if (cfg_do_help) {
188 dump_vector_fp(ar.get_help(),stderr);
189 exit(0);
190 }
191
192 // If user has a config file, use it.
193 if (cfg_config_file_set){
194 if (cfg_verbose)
195 fprintf(stderr,"Using config file: \"%s\"\n",cfg_config_file.c_str());
196 if(dump_vector_fp(ar.read_file(cfg_config_file),stderr)){
197 dump_vector_fp(ar.get_help(),stderr);
198 exit(7);
199 }
200
201 // Process command line a second time to override values from config file.
202 if(dump_vector_fp(ar.read_args(argc,args),stderr)||cfg_do_help){
203 dump_vector_fp(ar.get_help(),stderr);
204 exit(7);
205 }
206 }
207
208 if (cfg_infile_set==1){
209 if (cfg_verbose)
210 fprintf(stderr,"Opening file for input: \"%s\"\n",cfg_infile.c_str());
211 in_fd=open(cfg_infile.c_str(),O_RDONLY);
212 if (in_fd<0){
213 fprintf(stderr,"Error: could not open file \"%s\" for reading!\n",
214 cfg_infile.c_str());
215 exit (1);
216 }
217 }
218
219 if (cfg_outfile_set==1){
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);
223 if (out_fd<0){
224 fprintf(stderr,"Error: could not open file \"%s\" for writing!\n",
225 cfg_outfile.c_str());
226 exit (1);
227 }
228 }
229
230 // Verbose overrides quiet:
231 if (cfg_verbose)cfg_quiet=0;
232
233 // The configuration implications according to /cfg007/
234 // if (in_fd==0) cfg_pause_on_checksum_error=0;
235 // if (cfg_pause_on_checksum_error==1) cfg_ignore_checksum_errors=1;
236
237 if (cfg_output_info||cfg_output_called||cfg_output_exported||
238 cfg_output_unsatisfied) cfg_list_contents=0;
239 }
240
241