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