/[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 187
47#ifdef HAVE_UNISTD_H 47#ifdef HAVE_UNISTD_H
48#include <unistd.h> 48#include <unistd.h>
49#endif 49#endif
50 50
51#ifdef IMAGEMAGICK 51#ifdef IMAGEMAGICK
52#include <magick/magick.h> 52#include "magick-aux.h"
53#endif 53#endif
54 54
55#include "psiconv/data.h" 55#include <psiconv/data.h>
56#include <psiconv/list.h>
56#include "psiconv/parse.h" 57#include <psiconv/parse.h>
58#include <psiconv/configuration.h>
59#include "psiconv.h"
57#include "gen.h" 60#include "gen.h"
58#include "psiconv.h"
59 61
60static void print_help(void); 62static void print_help(void);
61static void print_version(void); 63static void print_version(void);
62static void strtoupper(char *str); 64static void strtoupper(char *str);
63 65
64void print_help(void) 66void print_help(void)
65{ 67{
66 psiconv_fileformat ff; 68 fileformat ff;
67 int i,j; 69 int i,j;
68 70
69 puts("Syntax: psiconv [OPTIONS..] [FILE]"); 71 puts("Syntax: psiconv [OPTIONS..] [FILE]");
70 puts("Convert the psion Word file FILE to other formats"); 72 puts("Convert the psion file FILE to other formats");
71 puts("If FILE is not specified, use stdin"); 73 puts("If FILE is not specified, use stdin");
72 puts(" -d, --debug Show debug information on stderr"); 74 puts(" -d, --debug Show debug information on stderr");
75 puts(" -e, --encoding Output encoding (default: UTF8)");
73 puts(" -h, --help Display this help and exit"); 76 puts(" -h, --help Display this help and exit");
74 puts(" -o, --outputfile Output to file instead of stdout"); 77 puts(" -o, --outputfile Output to file instead of stdout");
75 puts(" -s, --silent Do not even show warnings on stderr"); 78 puts(" -s, --silent Do not even show warnings on stderr");
76 puts(" -T, --type=FILETYPE Output type"); 79 puts(" -T, --type=FILETYPE Output type");
77 puts(" -V, --version Display the program version and exit"); 80 puts(" -V, --version Display the program version and exit");
78 puts(" -v, --verbose Show progress indicators on stderr"); 81 puts(" -v, --verbose Show progress indicators on stderr");
79 puts(" -u, --UTF8 Input file is encoded in UTF8"); 82 puts("");
83 puts("The following encodings are currently supported:");
84 puts(" UTF8 Variable length Unicode encoding");
85 puts(" UCS2 Fixed 16-bit length Unicode encoding");
86 puts(" Psion The encoding your Psion uses (as in psiconv.conf)");
87 puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')");
80 puts(""); 88 puts("");
81 puts("The following output types are known:"); 89 puts("The following output types are known:");
82 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 90 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
83 ff = psiconv_list_get(fileformat_list,i); 91 ff = psiconv_list_get(fileformat_list,i);
84 printf(" %s",ff->name); 92 printf(" %s",ff->name);
112 {"verbose",no_argument,NULL,'v'}, 120 {"verbose",no_argument,NULL,'v'},
113 {"debug",no_argument,NULL,'d'}, 121 {"debug",no_argument,NULL,'d'},
114 {"silent",no_argument,NULL,'s'}, 122 {"silent",no_argument,NULL,'s'},
115 {"outputfile",required_argument,NULL,'o'}, 123 {"outputfile",required_argument,NULL,'o'},
116 {"type",required_argument,NULL,'T'}, 124 {"type",required_argument,NULL,'T'},
117 {"UTF8",no_argument,NULL,'u'}, 125 {"encoding",no_argument,NULL,'e'},
118 {0,0,0,0} 126 {0,0,0,0}
119 }; 127 };
120 const char* short_options = "hVvsdo:uT:"; 128 const char* short_options = "hVvsdo:e:T:";
121 int option_index; 129 int option_index;
122 FILE * f; 130 FILE * f;
123 struct stat fbuf; 131 struct stat fbuf;
124 132
125 const char *inputfilename = ""; 133 const char *inputfilename = "";
126 const char *outputfilename = ""; 134 const char *outputfilename = "";
127 char *type = strdup("HTML3"); 135 char *type = strdup("HTML3");
128 psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; 136 encoding encoding_type=ENCODING_UTF8;
137 psiconv_list outputlist;
129 138
130 int c,i,res; 139 int c,i,res;
131 psiconv_buffer buf; 140 psiconv_buffer buf;
132 psiconv_file file; 141 psiconv_file file;
133 psiconv_fileformat ff; 142 fileformat ff = NULL;
134 143
135 psiconv_verbosity = PSICONV_VERB_WARN; 144 psiconv_config config = psiconv_config_default();
145 psiconv_config_read(NULL,&config);
136 146
137 fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); 147 if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) {
148 fputs("Out of memory error",stderr);
149 exit(1);
150 }
151
138 init_txt(); 152 init_txt();
153 init_image();
154#if 0
139 init_html(); 155 init_html();
140 init_html4(); 156 init_html4();
141 init_latex(); 157 init_latex();
142 init_rtf(); 158 init_rtf();
143 init_image(); 159#endif
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 'v': if (config->verbosity < PSICONV_VERB_PROGRESS)
153 psiconv_verbosity = PSICONV_VERB_PROGRESS; 169 config->verbosity = PSICONV_VERB_PROGRESS;
154 break; 170 break;
155 case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; 171 case 'd': config->verbosity = PSICONV_VERB_DEBUG; break;
156 case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; 172 case 's': config->verbosity = PSICONV_VERB_ERROR; break;
157 case 'o': outputfilename = strdup(optarg); break; 173 case 'o': outputfilename = strdup(optarg); break;
158 case 'T': type = strdup(optarg); break; 174 case 'T': type = strdup(optarg); break;
159 case 'u': encoding_type = PSICONV_ENCODING_UTF8; break; 175 case 'e': if(!strcmp(optarg,"UTF8"))
176 encoding_type = ENCODING_UTF8;
177 else if (!strcmp(optarg,"UCS2"))
178 encoding_type = ENCODING_UCS2;
179 else if (!strcmp(optarg,"ASCII"))
180 encoding_type = ENCODING_ASCII;
181 else if (!strcmp(optarg,"Psion"))
182 encoding_type = ENCODING_PSION;
183 else {
184 fputs("Unknown encoding type "
185 "(try '-h' for more information\n",stderr);
186 exit(1);
187 }
188 break;
160 case '?': case ':': fputs("Try `-h' for more information\n",stderr); 189 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
161 exit(1); 190 exit(1);
162 default: fprintf(stderr,"Internal error: getopt_long returned character " 191 default: fprintf(stderr,"Internal error: getopt_long returned character "
163 "code 0%o ?? (contact the author)\n", c); 192 "code 0%o ?? (contact the author)\n", c);
164 exit(1); break; 193 exit(1); break;
167 if (optind < argc-1) { 196 if (optind < argc-1) {
168 fputs("I can only convert one file!\n" 197 fputs("I can only convert one file!\n"
169 "Try `-h' for more information\n",stderr); 198 "Try `-h' for more information\n",stderr);
170 exit(1); 199 exit(1);
171 } else if (optind == argc-1) 200 } else if (optind == argc-1)
172 inputfilename = strdup(argv[optind]); 201 if (!(inputfilename = strdup(argv[optind]))) {
202 fputs("Out of memory error",stderr);
203 exit(1);
204 }
173 205
174 206
175 /* Open inputfile for reading */ 207 /* Open inputfile for reading */
176 208
177 if (strlen(inputfilename) != 0) { 209 if (strlen(inputfilename) != 0) {
185 exit(1); 217 exit(1);
186 } 218 }
187 } else 219 } else
188 f = stdin; 220 f = stdin;
189 221
190 buf = psiconv_buffer_new(); 222 if (!(buf = psiconv_buffer_new())) {
223 fputs("Out of memory error",stderr);
224 exit(1);
225 }
191 psiconv_buffer_fread_all(buf,f); 226 if (psiconv_buffer_fread_all(buf,f)) {
227 fprintf(stderr,"Failure reading file");
228 exit(1);
229 }
192 230
193 if (strlen(inputfilename) != 0) 231 if (strlen(inputfilename) != 0)
194 if (fclose(f)) { 232 if (fclose(f)) {
195 perror(inputfilename); 233 perror(inputfilename);
196 exit(1); 234 exit(1);
197 } 235 }
198 236
199 if (psiconv_parse(buf,&file) || (file->type == psiconv_unknown_file)) 237 if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file))
200 { 238 {
201 fprintf(stderr,"Parse error\n"); 239 fprintf(stderr,"Parse error\n");
202 exit(1); 240 exit(1);
203 } 241 }
204
205 /* Set correct output file */
206 if (strlen(outputfilename) == 0)
207 outputfilename = "/dev/stdout";
208 242
209 strtoupper(type); 243 strtoupper(type);
210 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { 244 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
211 ff = psiconv_list_get(fileformat_list,i); 245 ff = psiconv_list_get(fileformat_list,i);
212 if (! strcasecmp(type,ff->name)) { 246 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; 247 break;
220 } 248 }
221 } 249 }
222 250
223 if (i == psiconv_list_length(fileformat_list)) { 251 if (i == psiconv_list_length(fileformat_list)) {
224 fprintf(stderr,"Unknown output type: `%s'\n",type); 252 fprintf(stderr,"Unknown output type: `%s'\n",type);
225 exit(1); 253 exit(1);
226 } 254 }
227 255
256 if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) {
257 fputs("Out of memory error\n",stderr);
258 exit(1);
259 }
260
261 res = ff->output(config,outputlist,file,type,encoding_type);
262 if (res) {
263 fprintf(stderr,
264 "Output format `%s' not permitted for this file type\n",type);
265 exit(1);
266 }
267
228 psiconv_free_file(file); 268 psiconv_free_file(file);
229 269
270 if (strlen(outputfilename) != 0) {
271 f = fopen(outputfilename,"w");
272 if (! f) {
273 perror(inputfilename);
274 exit(1);
275 }
276 } else
277 f = stdout;
278
279 psiconv_list_fwrite_all(outputlist,f);
280
281 if (fclose(f)) {
282 perror(inputfilename);
283 exit(1);
284 }
285
286 psiconv_list_free(outputlist);
287
230 exit(0); 288 exit(0);
231} 289}

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

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