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

Legend:
Removed from v.20  
changed lines
  Added in v.270

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