… | |
… | |
13 | GNU General Public License for more details. |
13 | GNU General Public License for more details. |
14 | |
14 | |
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
|
|
19 | |
|
|
20 | /* |
|
|
21 | 2002/Apr. Keita KAWABE |
|
|
22 | * Support for narrow build Asian Psions added. |
|
|
23 | |
|
|
24 | Now psiconv understands -u or --UTF8 option. |
|
|
25 | This option is passed to output_function as the fourth parameter |
|
|
26 | (thus the typedef of output_function was changed). |
|
|
27 | |
|
|
28 | If a called generator (gen_*.c) can handle UTF8, then it handles |
|
|
29 | the character conversion etc. accordingly. |
|
|
30 | |
|
|
31 | * psiconv seemed to want to take the argument of the options |
|
|
32 | case-insensitively (i.e. LaTeX -> LATEX), but that was not working |
|
|
33 | under my environment. Fixed it. |
|
|
34 | */ |
18 | */ |
35 | |
19 | |
36 | /* Driver program */ |
20 | /* Driver program */ |
37 | |
21 | |
38 | #include "config.h" |
22 | #include "config.h" |
… | |
… | |
40 | #include <getopt.h> |
24 | #include <getopt.h> |
41 | #include <stdio.h> |
25 | #include <stdio.h> |
42 | #include <sys/stat.h> |
26 | #include <sys/stat.h> |
43 | #include <stdlib.h> |
27 | #include <stdlib.h> |
44 | #include <string.h> |
28 | #include <string.h> |
|
|
29 | #include <strings.h> |
45 | #include <ctype.h> |
30 | #include <ctype.h> |
46 | |
31 | |
47 | #ifdef HAVE_UNISTD_H |
32 | #ifdef HAVE_UNISTD_H |
48 | #include <unistd.h> |
33 | #include <unistd.h> |
49 | #endif |
34 | #endif |
50 | |
35 | |
51 | #ifdef IMAGEMAGICK |
36 | #ifdef IMAGEMAGICK |
52 | #include "magick-aux.h" |
37 | #include "magick-aux.h" |
53 | #endif |
38 | #endif |
54 | |
39 | |
55 | #include "psiconv/data.h" |
40 | #include <psiconv/data.h> |
|
|
41 | #include <psiconv/list.h> |
56 | #include "psiconv/parse.h" |
42 | #include <psiconv/parse.h> |
57 | #include "psiconv/configuration.h" |
43 | #include <psiconv/configuration.h> |
|
|
44 | #include "psiconv.h" |
58 | #include "gen.h" |
45 | #include "gen.h" |
59 | #include "psiconv.h" |
|
|
60 | |
46 | |
61 | static void print_help(void); |
47 | static void print_help(void); |
62 | static void print_version(void); |
48 | static void print_version(void); |
63 | static void strtoupper(char *str); |
49 | static void strtoupper(char *str); |
64 | |
50 | |
65 | void print_help(void) |
51 | void print_help(void) |
66 | { |
52 | { |
67 | psiconv_fileformat ff; |
53 | fileformat ff; |
68 | int i,j; |
54 | int i,j; |
69 | |
55 | |
70 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
56 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 | puts("Convert the psion Word file FILE to other formats"); |
57 | puts("Convert the psion file FILE to other formats"); |
72 | puts("If FILE is not specified, use stdin"); |
58 | puts("If FILE is not specified, use stdin"); |
73 | puts(" -d, --debug Show debug information on stderr"); |
59 | puts(" -e, --encoding=ENC Output encoding (default: UTF8)"); |
74 | 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)"); |
75 | puts(" -o, --outputfile Output to file instead of stdout"); |
62 | puts(" -o, --outputfile Output to file instead of stdout"); |
76 | puts(" -s, --silent Do not even show warnings on stderr"); |
|
|
77 | puts(" -T, --type=FILETYPE Output type"); |
63 | puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF"); |
78 | puts(" -V, --version Display the program version and exit"); |
64 | puts(" -V, --version Display the program version and exit"); |
79 | puts(" -v, --verbose Show progress indicators on stderr"); |
65 | puts(""); |
80 | puts(" -u, --UTF8 Input file is encoded in UTF8"); |
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"); |
81 | puts(""); |
87 | puts(""); |
82 | puts("The following output types are known:"); |
88 | puts("The following output types are known:"); |
83 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
89 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
84 | ff = psiconv_list_get(fileformat_list,i); |
90 | ff = psiconv_list_get(fileformat_list,i); |
85 | printf(" %s",ff->name); |
91 | printf(" %s",ff->name); |
86 | for (j = strlen(ff->name); j < 15; j++) |
92 | for (j = strlen(ff->name); j < 10; j++) |
87 | putchar(' '); |
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':' '); |
88 | puts(ff->description); |
102 | puts(ff->description); |
89 | } |
103 | } |
90 | puts(""); |
104 | puts(""); |
91 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
105 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
92 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
106 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
… | |
… | |
108 | { |
122 | { |
109 | struct option long_options[] = |
123 | struct option long_options[] = |
110 | { |
124 | { |
111 | {"help",no_argument,NULL,'h'}, |
125 | {"help",no_argument,NULL,'h'}, |
112 | {"version",no_argument,NULL,'V'}, |
126 | {"version",no_argument,NULL,'V'}, |
113 | {"verbose",no_argument,NULL,'v'}, |
127 | {"noise",required_argument,NULL,'n'}, |
114 | {"debug",no_argument,NULL,'d'}, |
|
|
115 | {"silent",no_argument,NULL,'s'}, |
|
|
116 | {"outputfile",required_argument,NULL,'o'}, |
128 | {"outputfile",required_argument,NULL,'o'}, |
117 | {"type",required_argument,NULL,'T'}, |
129 | {"type",required_argument,NULL,'T'}, |
118 | {"UTF8",no_argument,NULL,'u'}, |
130 | {"encoding",no_argument,NULL,'e'}, |
119 | {0,0,0,0} |
131 | {0,0,0,0} |
120 | }; |
132 | }; |
121 | const char* short_options = "hVvsdo:uT:"; |
133 | const char* short_options = "hVn:o:e:T:"; |
122 | int option_index; |
134 | int option_index; |
123 | FILE * f; |
135 | FILE * f; |
124 | struct stat fbuf; |
136 | struct stat fbuf; |
125 | |
137 | |
126 | const char *inputfilename = ""; |
138 | const char *inputfilename = ""; |
127 | const char *outputfilename = ""; |
139 | const char *outputfilename = ""; |
128 | char *type = strdup("HTML3"); |
140 | char *type = NULL; |
129 | psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; |
141 | encoding encoding_type=ENCODING_UTF8; |
|
|
142 | psiconv_list outputlist; |
130 | |
143 | |
131 | int c,i,res; |
144 | int c,i,res; |
132 | psiconv_buffer buf; |
145 | psiconv_buffer buf; |
133 | psiconv_file file; |
146 | psiconv_file file; |
134 | psiconv_fileformat ff; |
147 | fileformat ff = NULL; |
135 | |
148 | |
136 | psiconv_config config = psiconv_config_read(NULL); |
149 | psiconv_config config = psiconv_config_default(); |
|
|
150 | psiconv_config_read(NULL,&config); |
137 | |
151 | |
138 | fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
152 | if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { |
|
|
153 | fputs("Out of memory error",stderr); |
|
|
154 | exit(1); |
|
|
155 | } |
|
|
156 | |
139 | init_txt(); |
157 | init_txt(); |
140 | init_html(); |
158 | init_xhtml(); |
141 | init_html4(); |
159 | init_html4(); |
142 | init_latex(); |
|
|
143 | init_rtf(); |
|
|
144 | init_image(); |
160 | init_image(); |
145 | |
161 | |
146 | while(1) { |
162 | while(1) { |
147 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
163 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
148 | if (c == -1) |
164 | if (c == -1) |
149 | break; |
165 | break; |
150 | switch(c) { |
166 | switch(c) { |
151 | case 'h': print_help(); exit(0); |
167 | case 'h': print_help(); exit(0); |
152 | case 'V': print_version(); exit(0); |
168 | case 'V': print_version(); exit(0); |
153 | case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
169 | case 'n': switch(optarg[0]) { |
|
|
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': |
154 | config->verbosity = PSICONV_VERB_PROGRESS; |
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 | } |
155 | break; |
189 | break; |
156 | case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
|
|
157 | case 's': config->verbosity = PSICONV_VERB_FATAL; break; |
|
|
158 | case 'o': outputfilename = strdup(optarg); break; |
190 | case 'o': outputfilename = strdup(optarg); break; |
159 | case 'T': type = strdup(optarg); break; |
191 | case 'T': type = strdup(optarg); break; |
160 | case 'u': encoding_type = PSICONV_ENCODING_UTF8; 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; |
161 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
206 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
162 | exit(1); |
207 | exit(1); |
163 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
208 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
164 | "code 0%o ?? (contact the author)\n", c); |
209 | "code 0%o ?? (contact the author)\n", c); |
165 | exit(1); break; |
210 | exit(1); break; |
… | |
… | |
168 | if (optind < argc-1) { |
213 | if (optind < argc-1) { |
169 | fputs("I can only convert one file!\n" |
214 | fputs("I can only convert one file!\n" |
170 | "Try `-h' for more information\n",stderr); |
215 | "Try `-h' for more information\n",stderr); |
171 | exit(1); |
216 | exit(1); |
172 | } else if (optind == argc-1) |
217 | } else if (optind == argc-1) |
173 | inputfilename = strdup(argv[optind]); |
218 | if (!(inputfilename = strdup(argv[optind]))) { |
|
|
219 | fputs("Out of memory error",stderr); |
|
|
220 | exit(1); |
|
|
221 | } |
174 | |
222 | |
175 | |
223 | |
176 | /* Open inputfile for reading */ |
224 | /* Open inputfile for reading */ |
177 | |
225 | |
178 | if (strlen(inputfilename) != 0) { |
226 | if (strlen(inputfilename) != 0) { |
… | |
… | |
186 | exit(1); |
234 | exit(1); |
187 | } |
235 | } |
188 | } else |
236 | } else |
189 | f = stdin; |
237 | f = stdin; |
190 | |
238 | |
191 | buf = psiconv_buffer_new(); |
239 | if (!(buf = psiconv_buffer_new())) { |
|
|
240 | fputs("Out of memory error",stderr); |
|
|
241 | exit(1); |
|
|
242 | } |
192 | psiconv_buffer_fread_all(buf,f); |
243 | if (psiconv_buffer_fread_all(buf,f)) { |
|
|
244 | fprintf(stderr,"Failure reading file"); |
|
|
245 | exit(1); |
|
|
246 | } |
193 | |
247 | |
194 | if (strlen(inputfilename) != 0) |
248 | if (strlen(inputfilename) != 0) |
195 | if (fclose(f)) { |
249 | if (fclose(f)) { |
196 | perror(inputfilename); |
250 | perror(inputfilename); |
197 | exit(1); |
251 | exit(1); |
… | |
… | |
201 | { |
255 | { |
202 | fprintf(stderr,"Parse error\n"); |
256 | fprintf(stderr,"Parse error\n"); |
203 | exit(1); |
257 | exit(1); |
204 | } |
258 | } |
205 | |
259 | |
206 | /* Set correct output file */ |
260 | if (!type) { |
207 | if (strlen(outputfilename) == 0) |
261 | switch(file->type) { |
208 | outputfilename = "/dev/stdout"; |
262 | case psiconv_word_file: |
209 | |
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 |
210 | strtoupper(type); |
272 | strtoupper(type); |
|
|
273 | |
211 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
274 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
212 | ff = psiconv_list_get(fileformat_list,i); |
275 | ff = psiconv_list_get(fileformat_list,i); |
213 | if (! strcasecmp(type,ff->name)) { |
276 | if (! strcasecmp(type,ff->name)) { |
214 | res = ff->output(outputfilename,file,type, encoding_type); |
|
|
215 | if (res) { |
|
|
216 | fprintf(stderr, |
|
|
217 | "Output format `%s' not permitted for this file type\n",type); |
|
|
218 | exit(1); |
|
|
219 | } |
|
|
220 | break; |
277 | break; |
221 | } |
278 | } |
222 | } |
279 | } |
223 | |
280 | |
224 | if (i == psiconv_list_length(fileformat_list)) { |
281 | if (i == psiconv_list_length(fileformat_list)) { |
225 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
282 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
226 | exit(1); |
283 | exit(1); |
227 | } |
284 | } |
228 | |
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 | |
229 | psiconv_free_file(file); |
298 | psiconv_free_file(file); |
|
|
299 | |
|
|
300 | if (strlen(outputfilename) != 0) { |
|
|
301 | f = fopen(outputfilename,"w"); |
|
|
302 | if (! f) { |
|
|
303 | perror(inputfilename); |
|
|
304 | exit(1); |
|
|
305 | } |
|
|
306 | } else |
|
|
307 | f = stdout; |
|
|
308 | |
|
|
309 | psiconv_list_fwrite_all(outputlist,f); |
|
|
310 | |
|
|
311 | if (fclose(f)) { |
|
|
312 | perror(inputfilename); |
|
|
313 | exit(1); |
|
|
314 | } |
|
|
315 | |
|
|
316 | psiconv_list_free(outputlist); |
230 | |
317 | |
231 | exit(0); |
318 | exit(0); |
232 | } |
319 | } |