1 |
frodo |
2 |
/* |
2 |
|
|
psiconv.c - Part of psiconv, a PSION 5 file formats converter |
3 |
|
|
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
4 |
|
|
|
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 |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
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 |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
frodo |
147 |
/* |
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 |
|
|
*/ |
35 |
|
|
|
36 |
frodo |
2 |
/* Driver program */ |
37 |
|
|
|
38 |
|
|
#include "config.h" |
39 |
frodo |
20 |
#include "compat.h" |
40 |
frodo |
2 |
#include <getopt.h> |
41 |
|
|
#include <stdio.h> |
42 |
|
|
#include <sys/stat.h> |
43 |
|
|
#include <stdlib.h> |
44 |
frodo |
34 |
#include <string.h> |
45 |
|
|
#include <ctype.h> |
46 |
frodo |
2 |
|
47 |
|
|
#ifdef HAVE_UNISTD_H |
48 |
|
|
#include <unistd.h> |
49 |
|
|
#endif |
50 |
|
|
|
51 |
frodo |
34 |
#ifdef IMAGEMAGICK |
52 |
frodo |
152 |
#include "magick-aux.h" |
53 |
frodo |
34 |
#endif |
54 |
|
|
|
55 |
frodo |
185 |
#include <psiconv/data.h> |
56 |
|
|
#include <psiconv/list.h> |
57 |
|
|
#include <psiconv/parse.h> |
58 |
|
|
#include <psiconv/configuration.h> |
59 |
|
|
#include "psiconv.h" |
60 |
frodo |
2 |
#include "gen.h" |
61 |
|
|
|
62 |
|
|
static void print_help(void); |
63 |
|
|
static void print_version(void); |
64 |
|
|
static void strtoupper(char *str); |
65 |
|
|
|
66 |
|
|
void print_help(void) |
67 |
|
|
{ |
68 |
frodo |
185 |
fileformat ff; |
69 |
frodo |
34 |
int i,j; |
70 |
|
|
|
71 |
frodo |
2 |
puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
72 |
frodo |
185 |
puts("Convert the psion file FILE to other formats"); |
73 |
frodo |
2 |
puts("If FILE is not specified, use stdin"); |
74 |
|
|
puts(" -d, --debug Show debug information on stderr"); |
75 |
frodo |
185 |
puts(" -e, --encoding Output encoding (default: UTF8)"); |
76 |
frodo |
2 |
puts(" -h, --help Display this help and exit"); |
77 |
|
|
puts(" -o, --outputfile Output to file instead of stdout"); |
78 |
|
|
puts(" -s, --silent Do not even show warnings on stderr"); |
79 |
|
|
puts(" -T, --type=FILETYPE Output type"); |
80 |
|
|
puts(" -V, --version Display the program version and exit"); |
81 |
|
|
puts(" -v, --verbose Show progress indicators on stderr"); |
82 |
|
|
puts(""); |
83 |
frodo |
185 |
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 '?')"); |
88 |
|
|
puts(""); |
89 |
frodo |
2 |
puts("The following output types are known:"); |
90 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
91 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
92 |
|
|
printf(" %s",ff->name); |
93 |
|
|
for (j = strlen(ff->name); j < 15; j++) |
94 |
|
|
putchar(' '); |
95 |
|
|
puts(ff->description); |
96 |
|
|
} |
97 |
frodo |
147 |
puts(""); |
98 |
|
|
puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
99 |
|
|
puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
100 |
frodo |
2 |
} |
101 |
|
|
|
102 |
|
|
void print_version(void) |
103 |
|
|
{ |
104 |
|
|
printf("Version %s\n",VERSION); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
void strtoupper(char *str) |
108 |
|
|
{ |
109 |
|
|
int i; |
110 |
|
|
for (i = 0; i < strlen(str); i ++) |
111 |
|
|
str[i] = toupper(str[i]); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
int main(int argc, char *argv[]) |
115 |
|
|
{ |
116 |
|
|
struct option long_options[] = |
117 |
|
|
{ |
118 |
|
|
{"help",no_argument,NULL,'h'}, |
119 |
|
|
{"version",no_argument,NULL,'V'}, |
120 |
|
|
{"verbose",no_argument,NULL,'v'}, |
121 |
|
|
{"debug",no_argument,NULL,'d'}, |
122 |
|
|
{"silent",no_argument,NULL,'s'}, |
123 |
|
|
{"outputfile",required_argument,NULL,'o'}, |
124 |
|
|
{"type",required_argument,NULL,'T'}, |
125 |
frodo |
185 |
{"encoding",no_argument,NULL,'e'}, |
126 |
frodo |
2 |
{0,0,0,0} |
127 |
|
|
}; |
128 |
frodo |
185 |
const char* short_options = "hVvsdo:e:T:"; |
129 |
frodo |
2 |
int option_index; |
130 |
|
|
FILE * f; |
131 |
|
|
struct stat fbuf; |
132 |
|
|
|
133 |
|
|
const char *inputfilename = ""; |
134 |
|
|
const char *outputfilename = ""; |
135 |
frodo |
190 |
char *type = strdup("HTML4"); |
136 |
frodo |
185 |
encoding encoding_type=ENCODING_UTF8; |
137 |
|
|
psiconv_list outputlist; |
138 |
frodo |
2 |
|
139 |
frodo |
34 |
int c,i,res; |
140 |
frodo |
2 |
psiconv_buffer buf; |
141 |
|
|
psiconv_file file; |
142 |
frodo |
185 |
fileformat ff = NULL; |
143 |
frodo |
2 |
|
144 |
frodo |
180 |
psiconv_config config = psiconv_config_default(); |
145 |
|
|
psiconv_config_read(NULL,&config); |
146 |
frodo |
65 |
|
147 |
frodo |
185 |
if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { |
148 |
|
|
fputs("Out of memory error",stderr); |
149 |
|
|
exit(1); |
150 |
|
|
} |
151 |
|
|
|
152 |
frodo |
34 |
init_txt(); |
153 |
frodo |
187 |
init_image(); |
154 |
frodo |
190 |
init_html(); |
155 |
frodo |
185 |
#if 0 |
156 |
frodo |
34 |
init_html4(); |
157 |
frodo |
109 |
init_latex(); |
158 |
frodo |
34 |
init_rtf(); |
159 |
frodo |
185 |
#endif |
160 |
frodo |
34 |
|
161 |
frodo |
2 |
while(1) { |
162 |
|
|
c = getopt_long(argc,argv,short_options, long_options, &option_index); |
163 |
|
|
if (c == -1) |
164 |
|
|
break; |
165 |
|
|
switch(c) { |
166 |
|
|
case 'h': print_help(); exit(0); |
167 |
|
|
case 'V': print_version(); exit(0); |
168 |
frodo |
168 |
case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
169 |
|
|
config->verbosity = PSICONV_VERB_PROGRESS; |
170 |
frodo |
2 |
break; |
171 |
frodo |
168 |
case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
172 |
frodo |
185 |
case 's': config->verbosity = PSICONV_VERB_ERROR; break; |
173 |
frodo |
2 |
case 'o': outputfilename = strdup(optarg); break; |
174 |
|
|
case 'T': type = strdup(optarg); break; |
175 |
frodo |
185 |
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; |
189 |
frodo |
34 |
case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
190 |
frodo |
2 |
exit(1); |
191 |
|
|
default: fprintf(stderr,"Internal error: getopt_long returned character " |
192 |
|
|
"code 0%o ?? (contact the author)\n", c); |
193 |
|
|
exit(1); break; |
194 |
|
|
} |
195 |
|
|
} |
196 |
|
|
if (optind < argc-1) { |
197 |
|
|
fputs("I can only convert one file!\n" |
198 |
|
|
"Try `-h' for more information\n",stderr); |
199 |
|
|
exit(1); |
200 |
|
|
} else if (optind == argc-1) |
201 |
frodo |
185 |
if (!(inputfilename = strdup(argv[optind]))) { |
202 |
|
|
fputs("Out of memory error",stderr); |
203 |
|
|
exit(1); |
204 |
|
|
} |
205 |
frodo |
2 |
|
206 |
frodo |
65 |
|
207 |
frodo |
2 |
/* Open inputfile for reading */ |
208 |
|
|
|
209 |
|
|
if (strlen(inputfilename) != 0) { |
210 |
|
|
if(stat(inputfilename,&fbuf) < 0) { |
211 |
|
|
perror(inputfilename); |
212 |
|
|
exit(1); |
213 |
|
|
} |
214 |
|
|
f = fopen(inputfilename,"r"); |
215 |
|
|
if (! f) { |
216 |
|
|
perror(inputfilename); |
217 |
|
|
exit(1); |
218 |
|
|
} |
219 |
|
|
} else |
220 |
|
|
f = stdin; |
221 |
|
|
|
222 |
frodo |
185 |
if (!(buf = psiconv_buffer_new())) { |
223 |
|
|
fputs("Out of memory error",stderr); |
224 |
|
|
exit(1); |
225 |
|
|
} |
226 |
|
|
if (psiconv_buffer_fread_all(buf,f)) { |
227 |
|
|
fprintf(stderr,"Failure reading file"); |
228 |
|
|
exit(1); |
229 |
|
|
} |
230 |
frodo |
2 |
|
231 |
|
|
if (strlen(inputfilename) != 0) |
232 |
|
|
if (fclose(f)) { |
233 |
|
|
perror(inputfilename); |
234 |
|
|
exit(1); |
235 |
|
|
} |
236 |
|
|
|
237 |
frodo |
168 |
if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file)) |
238 |
frodo |
66 |
{ |
239 |
|
|
fprintf(stderr,"Parse error\n"); |
240 |
|
|
exit(1); |
241 |
|
|
} |
242 |
frodo |
2 |
|
243 |
|
|
strtoupper(type); |
244 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
245 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
246 |
frodo |
147 |
if (! strcasecmp(type,ff->name)) { |
247 |
frodo |
34 |
break; |
248 |
|
|
} |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
if (i == psiconv_list_length(fileformat_list)) { |
252 |
frodo |
2 |
fprintf(stderr,"Unknown output type: `%s'\n",type); |
253 |
|
|
exit(1); |
254 |
|
|
} |
255 |
|
|
|
256 |
frodo |
185 |
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 |
|
|
|
268 |
frodo |
2 |
psiconv_free_file(file); |
269 |
|
|
|
270 |
frodo |
185 |
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 |
|
|
|
288 |
frodo |
2 |
exit(0); |
289 |
|
|
} |