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

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

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