/[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 147 Revision 192
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"
47#ifdef HAVE_UNISTD_H 31#ifdef HAVE_UNISTD_H
48#include <unistd.h> 32#include <unistd.h>
49#endif 33#endif
50 34
51#ifdef IMAGEMAGICK 35#ifdef IMAGEMAGICK
52#include <magick/magick.h> 36#include "magick-aux.h"
53#endif 37#endif
54 38
55#include "psiconv/data.h" 39#include <psiconv/data.h>
40#include <psiconv/list.h>
56#include "psiconv/parse.h" 41#include <psiconv/parse.h>
42#include <psiconv/configuration.h>
43#include "psiconv.h"
57#include "gen.h" 44#include "gen.h"
58#include "psiconv.h"
59 45
60static void print_help(void); 46static void print_help(void);
61static void print_version(void); 47static void print_version(void);
62static void strtoupper(char *str); 48static void strtoupper(char *str);
63 49
64void print_help(void) 50void print_help(void)
65{ 51{
66 psiconv_fileformat ff; 52 fileformat ff;
67 int i,j; 53 int i,j;
68 54
69 puts("Syntax: psiconv [OPTIONS..] [FILE]"); 55 puts("Syntax: psiconv [OPTIONS..] [FILE]");
70 puts("Convert the psion Word file FILE to other formats"); 56 puts("Convert the psion file FILE to other formats");
71 puts("If FILE is not specified, use stdin"); 57 puts("If FILE is not specified, use stdin");
72 puts(" -d, --debug Show debug information on stderr"); 58 puts(" -e, --encoding=ENC Output encoding (default: UTF8)");
73 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)");
74 puts(" -o, --outputfile Output to file instead of stdout"); 61 puts(" -o, --outputfile Output to file instead of stdout");
75 puts(" -s, --silent Do not even show warnings on stderr");
76 puts(" -T, --type=FILETYPE Output type"); 62 puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF");
77 puts(" -V, --version Display the program version and exit"); 63 puts(" -V, --version Display the program version and exit");
78 puts(" -v, --verbose Show progress indicators on stderr"); 64 puts("");
79 puts(" -u, --UTF8 Input file is encoded in UTF8"); 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");
80 puts(""); 86 puts("");
81 puts("The following output types are known:"); 87 puts("The following output types are known:");
82 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 88 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
83 ff = psiconv_list_get(fileformat_list,i); 89 ff = psiconv_list_get(fileformat_list,i);
84 printf(" %s",ff->name); 90 printf(" %s",ff->name);
85 for (j = strlen(ff->name); j < 15; j++) 91 for (j = strlen(ff->name); j < 10; j++)
86 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':' ');
87 puts(ff->description); 101 puts(ff->description);
88 } 102 }
89 puts(""); 103 puts("");
90 puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); 104 puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted");
91 puts(" to a suitable encoding for your LaTeX installation before being typeset"); 105 puts(" to a suitable encoding for your LaTeX installation before being typeset");
107{ 121{
108 struct option long_options[] = 122 struct option long_options[] =
109 { 123 {
110 {"help",no_argument,NULL,'h'}, 124 {"help",no_argument,NULL,'h'},
111 {"version",no_argument,NULL,'V'}, 125 {"version",no_argument,NULL,'V'},
112 {"verbose",no_argument,NULL,'v'}, 126 {"noise",required_argument,NULL,'n'},
113 {"debug",no_argument,NULL,'d'},
114 {"silent",no_argument,NULL,'s'},
115 {"outputfile",required_argument,NULL,'o'}, 127 {"outputfile",required_argument,NULL,'o'},
116 {"type",required_argument,NULL,'T'}, 128 {"type",required_argument,NULL,'T'},
117 {"UTF8",no_argument,NULL,'u'}, 129 {"encoding",no_argument,NULL,'e'},
118 {0,0,0,0} 130 {0,0,0,0}
119 }; 131 };
120 const char* short_options = "hVvsdo:uT:"; 132 const char* short_options = "hVn:o:e:T:";
121 int option_index; 133 int option_index;
122 FILE * f; 134 FILE * f;
123 struct stat fbuf; 135 struct stat fbuf;
124 136
125 const char *inputfilename = ""; 137 const char *inputfilename = "";
126 const char *outputfilename = ""; 138 const char *outputfilename = "";
127 char *type = strdup("HTML3"); 139 char *type = NULL;
128 psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; 140 encoding encoding_type=ENCODING_UTF8;
141 psiconv_list outputlist;
129 142
130 int c,i,res; 143 int c,i,res;
131 psiconv_buffer buf; 144 psiconv_buffer buf;
132 psiconv_file file; 145 psiconv_file file;
133 psiconv_fileformat ff; 146 fileformat ff = NULL;
134 147
135 psiconv_verbosity = PSICONV_VERB_WARN; 148 psiconv_config config = psiconv_config_default();
149 psiconv_config_read(NULL,&config);
136 150
137 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
138 init_txt(); 156 init_txt();
139 init_html(); 157 init_xhtml();
140 init_html4(); 158 init_html4();
141 init_latex();
142 init_rtf();
143 init_image(); 159 init_image();
144 160
145 while(1) { 161 while(1) {
146 c = getopt_long(argc,argv,short_options, long_options, &option_index); 162 c = getopt_long(argc,argv,short_options, long_options, &option_index);
147 if (c == -1) 163 if (c == -1)
148 break; 164 break;
149 switch(c) { 165 switch(c) {
150 case 'h': print_help(); exit(0); 166 case 'h': print_help(); exit(0);
151 case 'V': print_version(); exit(0); 167 case 'V': print_version(); exit(0);
152 case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) 168 case 'n': switch(optarg[0]) {
153 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 }
154 break; 188 break;
155 case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break;
156 case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break;
157 case 'o': outputfilename = strdup(optarg); break; 189 case 'o': outputfilename = strdup(optarg); break;
158 case 'T': type = strdup(optarg); break; 190 case 'T': type = strdup(optarg); break;
159 case 'u': encoding_type = PSICONV_ENCODING_UTF8; 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;
160 case '?': case ':': fputs("Try `-h' for more information\n",stderr); 205 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
161 exit(1); 206 exit(1);
162 default: fprintf(stderr,"Internal error: getopt_long returned character " 207 default: fprintf(stderr,"Internal error: getopt_long returned character "
163 "code 0%o ?? (contact the author)\n", c); 208 "code 0%o ?? (contact the author)\n", c);
164 exit(1); break; 209 exit(1); break;
167 if (optind < argc-1) { 212 if (optind < argc-1) {
168 fputs("I can only convert one file!\n" 213 fputs("I can only convert one file!\n"
169 "Try `-h' for more information\n",stderr); 214 "Try `-h' for more information\n",stderr);
170 exit(1); 215 exit(1);
171 } else if (optind == argc-1) 216 } else if (optind == argc-1)
172 inputfilename = strdup(argv[optind]); 217 if (!(inputfilename = strdup(argv[optind]))) {
218 fputs("Out of memory error",stderr);
219 exit(1);
220 }
173 221
174 222
175 /* Open inputfile for reading */ 223 /* Open inputfile for reading */
176 224
177 if (strlen(inputfilename) != 0) { 225 if (strlen(inputfilename) != 0) {
185 exit(1); 233 exit(1);
186 } 234 }
187 } else 235 } else
188 f = stdin; 236 f = stdin;
189 237
190 buf = psiconv_buffer_new(); 238 if (!(buf = psiconv_buffer_new())) {
239 fputs("Out of memory error",stderr);
240 exit(1);
241 }
191 psiconv_buffer_fread_all(buf,f); 242 if (psiconv_buffer_fread_all(buf,f)) {
243 fprintf(stderr,"Failure reading file");
244 exit(1);
245 }
192 246
193 if (strlen(inputfilename) != 0) 247 if (strlen(inputfilename) != 0)
194 if (fclose(f)) { 248 if (fclose(f)) {
195 perror(inputfilename); 249 perror(inputfilename);
196 exit(1); 250 exit(1);
197 } 251 }
198 252
199 if (psiconv_parse(buf,&file) || (file->type == psiconv_unknown_file)) 253 if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file))
200 { 254 {
201 fprintf(stderr,"Parse error\n"); 255 fprintf(stderr,"Parse error\n");
202 exit(1); 256 exit(1);
203 } 257 }
204 258
205 /* Set correct output file */ 259 if (!type) {
206 if (strlen(outputfilename) == 0) 260 switch(file->type) {
207 outputfilename = "/dev/stdout"; 261 case psiconv_word_file:
208 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
209 strtoupper(type); 271 strtoupper(type);
272
210 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 273 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
211 ff = psiconv_list_get(fileformat_list,i); 274 ff = psiconv_list_get(fileformat_list,i);
212 if (! strcasecmp(type,ff->name)) { 275 if (! strcasecmp(type,ff->name)) {
213 res = ff->output(outputfilename,file,type, encoding_type);
214 if (res) {
215 fprintf(stderr,
216 "Output format `%s' not permitted for this file type\n",type);
217 exit(1);
218 }
219 break; 276 break;
220 } 277 }
221 } 278 }
222 279
223 if (i == psiconv_list_length(fileformat_list)) { 280 if (i == psiconv_list_length(fileformat_list)) {
224 fprintf(stderr,"Unknown output type: `%s'\n",type); 281 fprintf(stderr,"Unknown output type: `%s'\n",type);
225 exit(1); 282 exit(1);
226 } 283 }
227 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
228 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);
229 316
230 exit(0); 317 exit(0);
231} 318}

Legend:
Removed from v.147  
changed lines
  Added in v.192

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