/[public]/psiconv/trunk/program/psiconv/psiconv.c
ViewVC logotype

Diff of /psiconv/trunk/program/psiconv/psiconv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.168  
changed lines
  Added in v.219

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26