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