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