… | |
… | |
31 | #ifdef HAVE_UNISTD_H |
31 | #ifdef HAVE_UNISTD_H |
32 | #include <unistd.h> |
32 | #include <unistd.h> |
33 | #endif |
33 | #endif |
34 | |
34 | |
35 | #ifdef IMAGEMAGICK |
35 | #ifdef IMAGEMAGICK |
36 | #include <magick/magick.h> |
36 | #include "magick-aux.h" |
37 | #endif |
37 | #endif |
38 | |
38 | |
39 | #include "psiconv/data.h" |
39 | #include <psiconv/data.h> |
|
|
40 | #include <psiconv/list.h> |
40 | #include "psiconv/parse.h" |
41 | #include <psiconv/parse.h> |
|
|
42 | #include <psiconv/configuration.h> |
|
|
43 | #include "psiconv.h" |
41 | #include "gen.h" |
44 | #include "gen.h" |
42 | #include "psiconv.h" |
|
|
43 | |
45 | |
44 | static void print_help(void); |
46 | static void print_help(void); |
45 | static void print_version(void); |
47 | static void print_version(void); |
46 | static void strtoupper(char *str); |
48 | static void strtoupper(char *str); |
47 | |
49 | |
48 | int psiconv_verbosity = PSICONV_VERB_WARN; |
|
|
49 | |
|
|
50 | void print_help(void) |
50 | void print_help(void) |
51 | { |
51 | { |
52 | psiconv_fileformat ff; |
52 | fileformat ff; |
53 | int i,j; |
53 | int i,j; |
54 | |
54 | |
55 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
55 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
56 | puts("Convert the psion Word file FILE to a HTML file"); |
56 | puts("Convert the psion file FILE to other formats"); |
57 | puts("If FILE is not specified, use stdin"); |
57 | puts("If FILE is not specified, use stdin"); |
58 | puts(" -d, --debug Show debug information on stderr"); |
58 | puts(" -e, --encoding=ENC Output encoding (default: UTF8)"); |
59 | puts(" -e, --exact Quit on any parse errors or warnings"); |
|
|
60 | puts(" -h, --help Display this help and exit"); |
59 | puts(" -h, --help Display this help and exit"); |
|
|
60 | puts(" -n, --noise=LEVEL Select what to print on stderr (overrides psiconv.conf)"); |
61 | puts(" -o, --outputfile Output to file instead of stdout"); |
61 | puts(" -o, --outputfile Output to file instead of stdout"); |
62 | puts(" -s, --silent Do not even show warnings on stderr"); |
|
|
63 | puts(" -T, --type=FILETYPE Output type"); |
62 | puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF"); |
64 | puts(" -V, --version Display the program version and exit"); |
63 | puts(" -V, --version Display the program version and exit"); |
65 | puts(" -v, --verbose Show progress indicators on stderr"); |
64 | puts(""); |
|
|
65 | puts("The following encodings are currently supported:"); |
|
|
66 | puts(" UTF8 Variable length Unicode encoding"); |
|
|
67 | puts(" UCS2 Fixed 16-bit length Unicode encoding"); |
|
|
68 | puts(" Psion The encoding your Psion uses (as in psiconv.conf)"); |
|
|
69 | puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')"); |
|
|
70 | puts(""); |
|
|
71 | puts("The following noise levels are currently supported:"); |
|
|
72 | puts(" 1 or F: Fatal errors only"); |
|
|
73 | puts(" 2 or E: Errors"); |
|
|
74 | puts(" 3 or W: Warnings"); |
|
|
75 | puts(" 4 or P: Progress indicators"); |
|
|
76 | puts(" 5 or D: Debug data"); |
|
|
77 | puts(""); |
|
|
78 | puts("The following abbreviations are used below:"); |
|
|
79 | puts(" C - processes ClipArt files"); |
|
|
80 | puts(" c - processes ClipArt files containing only one image"); |
|
|
81 | puts(" M - processes MBM files"); |
|
|
82 | puts(" m - processes MBM files containing only one image"); |
|
|
83 | puts(" S - processes Sketch files"); |
|
|
84 | puts(" T - processes TextEd files"); |
|
|
85 | puts(" W - processes Word files"); |
66 | puts(""); |
86 | puts(""); |
67 | puts("The following output types are known:"); |
87 | puts("The following output types are known:"); |
68 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
88 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
69 | ff = psiconv_list_get(fileformat_list,i); |
89 | ff = psiconv_list_get(fileformat_list,i); |
70 | printf(" %s",ff->name); |
90 | printf(" %s",ff->name); |
71 | for (j = strlen(ff->name); j < 15; j++) |
91 | for (j = strlen(ff->name); j < 10; j++) |
72 | putchar(' '); |
92 | putchar(' '); |
|
|
93 | printf("[%c%c%c%c%c] ", |
|
|
94 | ff->supported_format & FORMAT_CLIPART_MULTIPLE?'C': |
|
|
95 | ff->supported_format & FORMAT_CLIPART_SINGLE?'c':' ', |
|
|
96 | ff->supported_format & FORMAT_MBM_MULTIPLE?'M': |
|
|
97 | ff->supported_format & FORMAT_MBM_SINGLE?'m':' ', |
|
|
98 | ff->supported_format & FORMAT_SKETCH?'S':' ', |
|
|
99 | ff->supported_format & FORMAT_TEXTED?'T':' ', |
|
|
100 | ff->supported_format & FORMAT_WORD?'W':' '); |
73 | puts(ff->description); |
101 | puts(ff->description); |
74 | } |
102 | } |
|
|
103 | puts(""); |
|
|
104 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
|
|
105 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
75 | } |
106 | } |
76 | |
107 | |
77 | void print_version(void) |
108 | void print_version(void) |
78 | { |
109 | { |
79 | printf("Version %s\n",VERSION); |
110 | printf("Version %s\n",VERSION); |
… | |
… | |
90 | { |
121 | { |
91 | struct option long_options[] = |
122 | struct option long_options[] = |
92 | { |
123 | { |
93 | {"help",no_argument,NULL,'h'}, |
124 | {"help",no_argument,NULL,'h'}, |
94 | {"version",no_argument,NULL,'V'}, |
125 | {"version",no_argument,NULL,'V'}, |
95 | {"verbose",no_argument,NULL,'v'}, |
126 | {"noise",required_argument,NULL,'n'}, |
96 | {"debug",no_argument,NULL,'d'}, |
|
|
97 | {"silent",no_argument,NULL,'s'}, |
|
|
98 | {"outputfile",required_argument,NULL,'o'}, |
127 | {"outputfile",required_argument,NULL,'o'}, |
99 | {"type",required_argument,NULL,'T'}, |
128 | {"type",required_argument,NULL,'T'}, |
100 | {"exact",no_argument,NULL,'e'}, |
129 | {"encoding",no_argument,NULL,'e'}, |
101 | {0,0,0,0} |
130 | {0,0,0,0} |
102 | }; |
131 | }; |
103 | const char* short_options = "hVvsdo:eT:"; |
132 | const char* short_options = "hVn:o:e:T:"; |
104 | int option_index; |
133 | int option_index; |
105 | FILE * f; |
134 | FILE * f; |
106 | struct stat fbuf; |
135 | struct stat fbuf; |
107 | |
136 | |
108 | const char *inputfilename = ""; |
137 | const char *inputfilename = ""; |
109 | const char *outputfilename = ""; |
138 | const char *outputfilename = ""; |
110 | char *type = strdup("HTML3"); |
139 | char *type = NULL; |
111 | int exact=0; |
140 | encoding encoding_type=ENCODING_UTF8; |
|
|
141 | psiconv_list outputlist; |
112 | |
142 | |
113 | int c,i,res; |
143 | int c,i,res; |
114 | psiconv_buffer buf; |
144 | psiconv_buffer buf; |
115 | psiconv_file file; |
145 | psiconv_file file; |
116 | psiconv_fileformat ff; |
146 | fileformat ff = NULL; |
117 | |
147 | |
|
|
148 | psiconv_config config = psiconv_config_default(); |
|
|
149 | psiconv_config_read(NULL,&config); |
|
|
150 | |
118 | fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
151 | if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { |
|
|
152 | fputs("Out of memory error",stderr); |
|
|
153 | exit(1); |
|
|
154 | } |
|
|
155 | |
119 | init_txt(); |
156 | init_txt(); |
120 | init_html(); |
157 | init_xhtml(); |
121 | init_html4(); |
158 | init_html4(); |
122 | init_rtf(); |
|
|
123 | init_image(); |
159 | init_image(); |
124 | |
160 | |
125 | while(1) { |
161 | while(1) { |
126 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
162 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
127 | if (c == -1) |
163 | if (c == -1) |
128 | break; |
164 | break; |
129 | switch(c) { |
165 | switch(c) { |
130 | case 'h': print_help(); exit(0); |
166 | case 'h': print_help(); exit(0); |
131 | case 'V': print_version(); exit(0); |
167 | case 'V': print_version(); exit(0); |
132 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
168 | case 'n': switch(optarg[0]) { |
133 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
169 | case '1': case 'F':case 'f': |
|
|
170 | config->verbosity=PSICONV_VERB_FATAL; |
|
|
171 | break; |
|
|
172 | case '2': case 'E':case 'e': |
|
|
173 | config->verbosity=PSICONV_VERB_ERROR; |
|
|
174 | break; |
|
|
175 | case '3': case 'W':case 'w': |
|
|
176 | config->verbosity=PSICONV_VERB_WARN; |
|
|
177 | break; |
|
|
178 | case '4': case 'P':case 'p': |
|
|
179 | config->verbosity=PSICONV_VERB_PROGRESS; |
|
|
180 | break; |
|
|
181 | case '5': case 'D':case 'd': |
|
|
182 | config->verbosity=PSICONV_VERB_DEBUG; |
|
|
183 | break; |
|
|
184 | default: |
|
|
185 | fputs("Unknown noise level\n",stderr); |
|
|
186 | exit(1); |
|
|
187 | } |
134 | break; |
188 | break; |
135 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
|
|
136 | case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; |
|
|
137 | case 'e': exact = 1; break; |
|
|
138 | case 'o': outputfilename = strdup(optarg); break; |
189 | case 'o': outputfilename = strdup(optarg); break; |
139 | case 'T': type = strdup(optarg); break; |
190 | case 'T': type = strdup(optarg); break; |
|
|
191 | case 'e': if(!strcmp(optarg,"UTF8")) |
|
|
192 | encoding_type = ENCODING_UTF8; |
|
|
193 | else if (!strcmp(optarg,"UCS2")) |
|
|
194 | encoding_type = ENCODING_UCS2; |
|
|
195 | else if (!strcmp(optarg,"ASCII")) |
|
|
196 | encoding_type = ENCODING_ASCII; |
|
|
197 | else if (!strcmp(optarg,"Psion")) |
|
|
198 | encoding_type = ENCODING_PSION; |
|
|
199 | else { |
|
|
200 | fputs("Unknown encoding type " |
|
|
201 | "(try '-h' for more information\n",stderr); |
|
|
202 | exit(1); |
|
|
203 | } |
|
|
204 | break; |
140 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
205 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
141 | exit(1); |
206 | exit(1); |
142 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
207 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
143 | "code 0%o ?? (contact the author)\n", c); |
208 | "code 0%o ?? (contact the author)\n", c); |
144 | exit(1); break; |
209 | exit(1); break; |
… | |
… | |
147 | if (optind < argc-1) { |
212 | if (optind < argc-1) { |
148 | fputs("I can only convert one file!\n" |
213 | fputs("I can only convert one file!\n" |
149 | "Try `-h' for more information\n",stderr); |
214 | "Try `-h' for more information\n",stderr); |
150 | exit(1); |
215 | exit(1); |
151 | } else if (optind == argc-1) |
216 | } else if (optind == argc-1) |
152 | inputfilename = strdup(argv[optind]); |
217 | if (!(inputfilename = strdup(argv[optind]))) { |
|
|
218 | fputs("Out of memory error",stderr); |
|
|
219 | exit(1); |
|
|
220 | } |
|
|
221 | |
153 | |
222 | |
154 | /* Open inputfile for reading */ |
223 | /* Open inputfile for reading */ |
155 | |
224 | |
156 | if (strlen(inputfilename) != 0) { |
225 | if (strlen(inputfilename) != 0) { |
157 | if(stat(inputfilename,&fbuf) < 0) { |
226 | if(stat(inputfilename,&fbuf) < 0) { |
… | |
… | |
164 | exit(1); |
233 | exit(1); |
165 | } |
234 | } |
166 | } else |
235 | } else |
167 | f = stdin; |
236 | f = stdin; |
168 | |
237 | |
169 | buf = psiconv_list_new(sizeof(psiconv_u8)); |
238 | if (!(buf = psiconv_buffer_new())) { |
170 | while (! feof(f)) |
239 | fputs("Out of memory error",stderr); |
171 | psiconv_list_fread(buf,1024,f); |
240 | exit(1); |
|
|
241 | } |
|
|
242 | if (psiconv_buffer_fread_all(buf,f)) { |
|
|
243 | fprintf(stderr,"Failure reading file"); |
|
|
244 | exit(1); |
|
|
245 | } |
172 | |
246 | |
173 | if (strlen(inputfilename) != 0) |
247 | if (strlen(inputfilename) != 0) |
174 | if (fclose(f)) { |
248 | if (fclose(f)) { |
175 | perror(inputfilename); |
249 | perror(inputfilename); |
176 | exit(1); |
250 | exit(1); |
177 | } |
251 | } |
178 | |
252 | |
179 | if (psiconv_parse(buf,&file)) |
253 | if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file)) |
180 | if(exact) |
254 | { |
|
|
255 | fprintf(stderr,"Parse error\n"); |
181 | exit(1); |
256 | exit(1); |
|
|
257 | } |
182 | |
258 | |
183 | /* Set correct output file */ |
259 | if (!type) { |
184 | if (strlen(outputfilename) == 0) |
260 | switch(file->type) { |
185 | outputfilename = "/dev/stdout"; |
261 | case psiconv_word_file: |
186 | |
262 | case psiconv_texted_file: |
|
|
263 | default: |
|
|
264 | type = "XHTML"; break; |
|
|
265 | case psiconv_mbm_file: |
|
|
266 | case psiconv_clipart_file: |
|
|
267 | case psiconv_sketch_file: |
|
|
268 | type = "TIFF"; break; |
|
|
269 | } |
|
|
270 | } else |
187 | strtoupper(type); |
271 | strtoupper(type); |
|
|
272 | |
188 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
273 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
189 | ff = psiconv_list_get(fileformat_list,i); |
274 | ff = psiconv_list_get(fileformat_list,i); |
190 | if (! strcmp(type,ff->name)) { |
275 | if (! strcasecmp(type,ff->name)) { |
191 | res = ff->output(outputfilename,file,type); |
|
|
192 | if (res) { |
|
|
193 | fprintf(stderr, |
|
|
194 | "Output format `%s' not permitted for this file type\n",type); |
|
|
195 | exit(1); |
|
|
196 | } |
|
|
197 | break; |
276 | break; |
198 | } |
277 | } |
199 | } |
278 | } |
200 | |
279 | |
201 | if (i == psiconv_list_length(fileformat_list)) { |
280 | if (i == psiconv_list_length(fileformat_list)) { |
202 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
281 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
203 | exit(1); |
282 | exit(1); |
204 | } |
283 | } |
205 | |
284 | |
|
|
285 | if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) { |
|
|
286 | fputs("Out of memory error\n",stderr); |
|
|
287 | exit(1); |
|
|
288 | } |
|
|
289 | |
|
|
290 | res = ff->output(config,outputlist,file,type,encoding_type); |
|
|
291 | if (res) { |
|
|
292 | fprintf(stderr, |
|
|
293 | "Output format `%s' not permitted for this file type\n",type); |
|
|
294 | exit(1); |
|
|
295 | } |
|
|
296 | |
206 | psiconv_free_file(file); |
297 | psiconv_free_file(file); |
|
|
298 | |
|
|
299 | if (strlen(outputfilename) != 0) { |
|
|
300 | f = fopen(outputfilename,"w"); |
|
|
301 | if (! f) { |
|
|
302 | perror(inputfilename); |
|
|
303 | exit(1); |
|
|
304 | } |
|
|
305 | } else |
|
|
306 | f = stdout; |
|
|
307 | |
|
|
308 | psiconv_list_fwrite_all(outputlist,f); |
|
|
309 | |
|
|
310 | if (fclose(f)) { |
|
|
311 | perror(inputfilename); |
|
|
312 | exit(1); |
|
|
313 | } |
|
|
314 | |
|
|
315 | psiconv_list_free(outputlist); |
207 | |
316 | |
208 | exit(0); |
317 | exit(0); |
209 | } |
318 | } |