… | |
… | |
50 | |
50 | |
51 | #ifdef IMAGEMAGICK |
51 | #ifdef IMAGEMAGICK |
52 | #include "magick-aux.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> |
57 | #include "psiconv/configuration.h" |
58 | #include <psiconv/configuration.h> |
|
|
59 | #include "psiconv.h" |
58 | #include "gen.h" |
60 | #include "gen.h" |
59 | #include "psiconv.h" |
|
|
60 | |
61 | |
61 | static void print_help(void); |
62 | static void print_help(void); |
62 | static void print_version(void); |
63 | static void print_version(void); |
63 | static void strtoupper(char *str); |
64 | static void strtoupper(char *str); |
64 | |
65 | |
65 | void print_help(void) |
66 | void print_help(void) |
66 | { |
67 | { |
67 | psiconv_fileformat ff; |
68 | fileformat ff; |
68 | int i,j; |
69 | int i,j; |
69 | |
70 | |
70 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 | puts("Convert the psion Word file FILE to other formats"); |
72 | puts("Convert the psion file FILE to other formats"); |
72 | puts("If FILE is not specified, use stdin"); |
73 | puts("If FILE is not specified, use stdin"); |
73 | 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)"); |
74 | puts(" -h, --help Display this help and exit"); |
76 | puts(" -h, --help Display this help and exit"); |
75 | puts(" -o, --outputfile Output to file instead of stdout"); |
77 | puts(" -o, --outputfile Output to file instead of stdout"); |
76 | puts(" -s, --silent Do not even show warnings on stderr"); |
78 | puts(" -s, --silent Do not even show warnings on stderr"); |
77 | puts(" -T, --type=FILETYPE Output type"); |
79 | puts(" -T, --type=FILETYPE Output type"); |
78 | puts(" -V, --version Display the program version and exit"); |
80 | puts(" -V, --version Display the program version and exit"); |
79 | puts(" -v, --verbose Show progress indicators on stderr"); |
81 | puts(" -v, --verbose Show progress indicators on stderr"); |
80 | 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 '?')"); |
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); |
… | |
… | |
113 | {"verbose",no_argument,NULL,'v'}, |
120 | {"verbose",no_argument,NULL,'v'}, |
114 | {"debug",no_argument,NULL,'d'}, |
121 | {"debug",no_argument,NULL,'d'}, |
115 | {"silent",no_argument,NULL,'s'}, |
122 | {"silent",no_argument,NULL,'s'}, |
116 | {"outputfile",required_argument,NULL,'o'}, |
123 | {"outputfile",required_argument,NULL,'o'}, |
117 | {"type",required_argument,NULL,'T'}, |
124 | {"type",required_argument,NULL,'T'}, |
118 | {"UTF8",no_argument,NULL,'u'}, |
125 | {"encoding",no_argument,NULL,'e'}, |
119 | {0,0,0,0} |
126 | {0,0,0,0} |
120 | }; |
127 | }; |
121 | const char* short_options = "hVvsdo:uT:"; |
128 | const char* short_options = "hVvsdo:e:T:"; |
122 | int option_index; |
129 | int option_index; |
123 | FILE * f; |
130 | FILE * f; |
124 | struct stat fbuf; |
131 | struct stat fbuf; |
125 | |
132 | |
126 | const char *inputfilename = ""; |
133 | const char *inputfilename = ""; |
127 | const char *outputfilename = ""; |
134 | const char *outputfilename = ""; |
128 | char *type = strdup("HTML3"); |
135 | char *type = strdup("HTML3"); |
129 | psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; |
136 | encoding encoding_type=ENCODING_UTF8; |
|
|
137 | psiconv_list outputlist; |
130 | |
138 | |
131 | int c,i,res; |
139 | int c,i,res; |
132 | psiconv_buffer buf; |
140 | psiconv_buffer buf; |
133 | psiconv_file file; |
141 | psiconv_file file; |
134 | psiconv_fileformat ff; |
142 | fileformat ff = NULL; |
135 | |
143 | |
136 | psiconv_config config = psiconv_config_read(NULL); |
144 | psiconv_config config = psiconv_config_default(); |
|
|
145 | psiconv_config_read(NULL,&config); |
137 | |
146 | |
138 | 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 | |
139 | init_txt(); |
152 | init_txt(); |
|
|
153 | init_image(); |
|
|
154 | #if 0 |
140 | init_html(); |
155 | init_html(); |
141 | init_html4(); |
156 | init_html4(); |
142 | init_latex(); |
157 | init_latex(); |
143 | init_rtf(); |
158 | init_rtf(); |
144 | init_image(); |
159 | #endif |
145 | |
160 | |
146 | while(1) { |
161 | while(1) { |
147 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
162 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
148 | if (c == -1) |
163 | if (c == -1) |
149 | break; |
164 | break; |
… | |
… | |
152 | case 'V': print_version(); exit(0); |
167 | case 'V': print_version(); exit(0); |
153 | case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
168 | case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
154 | config->verbosity = PSICONV_VERB_PROGRESS; |
169 | config->verbosity = PSICONV_VERB_PROGRESS; |
155 | break; |
170 | break; |
156 | case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
171 | case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
157 | case 's': config->verbosity = PSICONV_VERB_FATAL; break; |
172 | case 's': config->verbosity = PSICONV_VERB_ERROR; break; |
158 | case 'o': outputfilename = strdup(optarg); break; |
173 | case 'o': outputfilename = strdup(optarg); break; |
159 | case 'T': type = strdup(optarg); break; |
174 | case 'T': type = strdup(optarg); break; |
160 | 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; |
161 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
189 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
162 | exit(1); |
190 | exit(1); |
163 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
191 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
164 | "code 0%o ?? (contact the author)\n", c); |
192 | "code 0%o ?? (contact the author)\n", c); |
165 | exit(1); break; |
193 | exit(1); break; |
… | |
… | |
168 | if (optind < argc-1) { |
196 | if (optind < argc-1) { |
169 | fputs("I can only convert one file!\n" |
197 | fputs("I can only convert one file!\n" |
170 | "Try `-h' for more information\n",stderr); |
198 | "Try `-h' for more information\n",stderr); |
171 | exit(1); |
199 | exit(1); |
172 | } else if (optind == argc-1) |
200 | } else if (optind == argc-1) |
173 | inputfilename = strdup(argv[optind]); |
201 | if (!(inputfilename = strdup(argv[optind]))) { |
|
|
202 | fputs("Out of memory error",stderr); |
|
|
203 | exit(1); |
|
|
204 | } |
174 | |
205 | |
175 | |
206 | |
176 | /* Open inputfile for reading */ |
207 | /* Open inputfile for reading */ |
177 | |
208 | |
178 | if (strlen(inputfilename) != 0) { |
209 | if (strlen(inputfilename) != 0) { |
… | |
… | |
186 | exit(1); |
217 | exit(1); |
187 | } |
218 | } |
188 | } else |
219 | } else |
189 | f = stdin; |
220 | f = stdin; |
190 | |
221 | |
191 | buf = psiconv_buffer_new(); |
222 | if (!(buf = psiconv_buffer_new())) { |
|
|
223 | fputs("Out of memory error",stderr); |
|
|
224 | exit(1); |
|
|
225 | } |
192 | psiconv_buffer_fread_all(buf,f); |
226 | if (psiconv_buffer_fread_all(buf,f)) { |
|
|
227 | fprintf(stderr,"Failure reading file"); |
|
|
228 | exit(1); |
|
|
229 | } |
193 | |
230 | |
194 | if (strlen(inputfilename) != 0) |
231 | if (strlen(inputfilename) != 0) |
195 | if (fclose(f)) { |
232 | if (fclose(f)) { |
196 | perror(inputfilename); |
233 | perror(inputfilename); |
197 | exit(1); |
234 | exit(1); |
… | |
… | |
201 | { |
238 | { |
202 | fprintf(stderr,"Parse error\n"); |
239 | fprintf(stderr,"Parse error\n"); |
203 | exit(1); |
240 | exit(1); |
204 | } |
241 | } |
205 | |
242 | |
206 | /* Set correct output file */ |
|
|
207 | if (strlen(outputfilename) == 0) |
|
|
208 | outputfilename = "/dev/stdout"; |
|
|
209 | |
|
|
210 | strtoupper(type); |
243 | strtoupper(type); |
211 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
244 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
212 | ff = psiconv_list_get(fileformat_list,i); |
245 | ff = psiconv_list_get(fileformat_list,i); |
213 | if (! strcasecmp(type,ff->name)) { |
246 | 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; |
247 | break; |
221 | } |
248 | } |
222 | } |
249 | } |
223 | |
250 | |
224 | if (i == psiconv_list_length(fileformat_list)) { |
251 | if (i == psiconv_list_length(fileformat_list)) { |
225 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
252 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
226 | exit(1); |
253 | exit(1); |
227 | } |
254 | } |
228 | |
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 | |
229 | psiconv_free_file(file); |
268 | psiconv_free_file(file); |
230 | |
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 | |
231 | exit(0); |
288 | exit(0); |
232 | } |
289 | } |