798b0c1d |
1 | /* ldc2 preliminary main program */ |
2 | |
3 | #include <vector> |
4 | #include <string> |
f9d603d2 |
5 | |
97b26985 |
6 | #include <stdio.h> |
7 | #include <unistd.h> |
ad324d29 |
8 | #include <sys/types.h> |
9 | #include <sys/stat.h> |
10 | #include <fcntl.h> |
97b26985 |
11 | |
12 | #include "tape_block.hh" |
909d3603 |
13 | #include "configuration_manager.hh" |
798b0c1d |
14 | |
15 | using namespace std; |
16 | |
ad324d29 |
17 | int dump_vector(vector<string> arguments){ |
18 | int res=0; |
798b0c1d |
19 | for (vector<string>::iterator iter=arguments.begin();iter<arguments.end();iter++){ |
20 | printf("%s\n",(*iter).c_str()); |
ad324d29 |
21 | res=1; |
798b0c1d |
22 | } |
ad324d29 |
23 | return res; |
798b0c1d |
24 | } |
25 | |
ad324d29 |
26 | |
798b0c1d |
27 | int main(int argc, char ** args){ |
ad324d29 |
28 | |
909d3603 |
29 | /* Configuration data */ |
30 | string config_file; |
ad324d29 |
31 | string infile, outfile; |
909d3603 |
32 | int infile_set, outfile_set, config_file_set; |
ad324d29 |
33 | |
909d3603 |
34 | int |
35 | do_help, |
36 | output_info, |
37 | output_called, |
38 | output_exported, |
39 | output_unsatisfied, |
40 | split_objects, |
41 | split_objects_numbered, |
42 | ignore_block_errors, |
43 | ignore_checksum_errors, |
44 | pause_on_checksum_error, |
45 | ignore_unknown_block_errors, |
46 | ignore_object_integrity_errors; |
ad324d29 |
47 | |
48 | int in_fd, out_fd; |
49 | |
909d3603 |
50 | in_fd=0; /* stdin {O _ \ */ |
51 | out_fd=1; /* stdout {O ^ / */ |
798b0c1d |
52 | |
909d3603 |
53 | configuration_manager ar("ldc2"); |
54 | |
55 | /* Here come the configuration switches */ |
56 | ar.add_option_switch("h","help", |
57 | "Output this help text and exit.", |
58 | &do_help,true,false); |
59 | |
60 | ar.add_option_switch("a","output_info", |
61 | "Print tape data information (default)", |
62 | &output_info); |
63 | |
64 | ar.add_option_switch("c","output_called", |
65 | "Print all called symbols from the object(s).", |
66 | &output_called); |
67 | |
68 | ar.add_option_switch("e","output_exported", |
69 | "Print all exported symbols from the object(s).", |
70 | &output_exported); |
71 | |
72 | ar.add_option_switch("u","output_unsatisfied", |
73 | "List all unsatisfied symbols.", |
74 | &output_unsatisfied); |
75 | |
76 | ar.add_option_switch("s","split_objects", |
77 | "Split input data into distinct object files.", |
78 | &split_objects); |
79 | |
80 | ar.add_option_switch("S","split_objects_numbered", |
81 | "Split input data into distinct numbered files", |
82 | &split_objects_numbered); |
83 | |
84 | ar.add_option_switch("b","ignore_block_errors", |
85 | "Ignore block integrity errors. This will output broken blocks,too", |
86 | &ignore_block_errors); |
87 | |
88 | ar.add_option_switch("k","ignore_checksum_errors", |
89 | "Ignore block checksum errors. Errors will be converted to warnings.", |
90 | &ignore_checksum_errors); |
91 | |
92 | ar.add_option_switch("p","pause_on_checksum_error", |
93 | "Wait for user input on checksum error.", |
94 | &pause_on_checksum_error); |
95 | |
96 | ar.add_option_switch("n","ignore_unknown_block_errors", |
97 | "Ignore errors caused by unknown block types. Errors will be converted to warnings.", |
98 | &ignore_unknown_block_errors); |
99 | |
100 | ar.add_option_switch("g","ignore_object_integrity_errors", |
101 | "Ignore errors caused by objects without proper end block. \ |
102 | Errors will be converted to warnings.", |
103 | &ignore_object_integrity_errors); |
104 | |
105 | ar.add_option_value("i","input_file", |
106 | "specify input file", |
107 | &infile_set,&infile, |
108 | "<input-file>"); |
109 | |
110 | ar.add_option_value("o","output_file", |
111 | "Specify output file.", |
112 | &outfile_set,&outfile, |
113 | "<output_file>"); |
114 | |
115 | |
116 | ar.add_argument("File from where data is read", |
117 | &infile_set,&infile, |
118 | "<input-file>"); |
119 | |
120 | ar.add_argument("File to where output is redirected", |
121 | &outfile_set,&outfile, |
122 | "<output-file>"); |
123 | |
124 | ar.add_option_value("F","config_file", |
125 | "Use the specified configuration file.", |
126 | &config_file_set,&config_file, |
127 | "<file-name>"); |
128 | |
50c86ded |
129 | // Process command line first time |
909d3603 |
130 | if((dump_vector(ar.read_args(argc,args))||do_help)){ |
632a71a2 |
131 | dump_vector(ar.get_help()); |
132 | exit(1); |
133 | } |
ad324d29 |
134 | |
50c86ded |
135 | // If user has a config file, use it. |
136 | if (config_file_set){ |
137 | if(dump_vector(ar.read_file(config_file))){ |
138 | dump_vector(ar.get_help()); |
139 | exit(1); |
140 | } |
141 | // Process command line a second time to override values from config file. |
142 | if(dump_vector(ar.read_args(argc,args))||do_help){ |
143 | dump_vector(ar.get_help()); |
144 | exit(1); |
145 | } |
146 | } |
147 | |
148 | |
149 | |
150 | |
ad324d29 |
151 | if (infile_set==1){ |
152 | printf("Opening file for input:%s\n",infile.c_str()); |
153 | in_fd=open(infile.c_str(),O_RDONLY); |
154 | if (in_fd<0){ |
155 | printf("Error: could not open file:%s\n",infile.c_str()); |
156 | exit (3); |
157 | } |
158 | } |
159 | |
909d3603 |
160 | |
161 | |
162 | |
ad324d29 |
163 | vector<tape_block*> tape; |
164 | |
165 | |
166 | tape_block * block=0; |
167 | while(1){ |
168 | block=tape_block::gen_from_fd(in_fd); |
169 | if (block->get_state()==tape_block::TBS_OK){ |
170 | tape.insert(tape.end(),block); |
171 | dump_vector(block->get_description()); |
172 | } |
173 | else break; |
174 | } |
798b0c1d |
175 | |
ad324d29 |
176 | switch(block->get_state()){ |
177 | case tape_block::TBS_EOF_LEGAL: printf("File successfully read.\n"); |
178 | break; |
179 | case tape_block::TBS_EOF_ILLEGAL: printf("EOF while in block!\n"); |
180 | break; |
181 | case tape_block::TBS_CHECKSUM: |
182 | printf("Checksum error!\n"); |
183 | break; |
184 | case tape_block::TBS_DEFAULT: |
185 | printf("TBS_DEFAULT encountered ->> SEVERY INTERNAL ERROR!\n"); |
186 | exit(88); |
187 | case tape_block::TBS_IOERR: |
188 | printf("I/O Error... Why?\n"); |
189 | exit (43); |
190 | } |
191 | |
192 | return 0; |
193 | } // main() |
798b0c1d |
194 | |