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 |
58 |
#include "psiconv/data.h" |
56 |
|
|
#include "psiconv/parse.h" |
57 |
frodo |
168 |
#include "psiconv/configuration.h" |
58 |
frodo |
2 |
#include "gen.h" |
59 |
frodo |
34 |
#include "psiconv.h" |
60 |
frodo |
2 |
|
61 |
|
|
static void print_help(void); |
62 |
|
|
static void print_version(void); |
63 |
|
|
static void strtoupper(char *str); |
64 |
|
|
|
65 |
|
|
void print_help(void) |
66 |
|
|
{ |
67 |
frodo |
34 |
psiconv_fileformat ff; |
68 |
|
|
int i,j; |
69 |
|
|
|
70 |
frodo |
2 |
puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 |
frodo |
147 |
puts("Convert the psion Word file FILE to other formats"); |
72 |
frodo |
2 |
puts("If FILE is not specified, use stdin"); |
73 |
|
|
puts(" -d, --debug Show debug information on stderr"); |
74 |
|
|
puts(" -h, --help Display this help and exit"); |
75 |
|
|
puts(" -o, --outputfile Output to file instead of stdout"); |
76 |
|
|
puts(" -s, --silent Do not even show warnings on stderr"); |
77 |
|
|
puts(" -T, --type=FILETYPE Output type"); |
78 |
|
|
puts(" -V, --version Display the program version and exit"); |
79 |
|
|
puts(" -v, --verbose Show progress indicators on stderr"); |
80 |
frodo |
147 |
puts(" -u, --UTF8 Input file is encoded in UTF8"); |
81 |
frodo |
2 |
puts(""); |
82 |
|
|
puts("The following output types are known:"); |
83 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
84 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
85 |
|
|
printf(" %s",ff->name); |
86 |
|
|
for (j = strlen(ff->name); j < 15; j++) |
87 |
|
|
putchar(' '); |
88 |
|
|
puts(ff->description); |
89 |
|
|
} |
90 |
frodo |
147 |
puts(""); |
91 |
|
|
puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
92 |
|
|
puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
93 |
frodo |
2 |
} |
94 |
|
|
|
95 |
|
|
void print_version(void) |
96 |
|
|
{ |
97 |
|
|
printf("Version %s\n",VERSION); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
void strtoupper(char *str) |
101 |
|
|
{ |
102 |
|
|
int i; |
103 |
|
|
for (i = 0; i < strlen(str); i ++) |
104 |
|
|
str[i] = toupper(str[i]); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
int main(int argc, char *argv[]) |
108 |
|
|
{ |
109 |
|
|
struct option long_options[] = |
110 |
|
|
{ |
111 |
|
|
{"help",no_argument,NULL,'h'}, |
112 |
|
|
{"version",no_argument,NULL,'V'}, |
113 |
|
|
{"verbose",no_argument,NULL,'v'}, |
114 |
|
|
{"debug",no_argument,NULL,'d'}, |
115 |
|
|
{"silent",no_argument,NULL,'s'}, |
116 |
|
|
{"outputfile",required_argument,NULL,'o'}, |
117 |
|
|
{"type",required_argument,NULL,'T'}, |
118 |
frodo |
147 |
{"UTF8",no_argument,NULL,'u'}, |
119 |
frodo |
2 |
{0,0,0,0} |
120 |
|
|
}; |
121 |
frodo |
147 |
const char* short_options = "hVvsdo:uT:"; |
122 |
frodo |
2 |
int option_index; |
123 |
|
|
FILE * f; |
124 |
|
|
struct stat fbuf; |
125 |
|
|
|
126 |
|
|
const char *inputfilename = ""; |
127 |
|
|
const char *outputfilename = ""; |
128 |
frodo |
46 |
char *type = strdup("HTML3"); |
129 |
frodo |
147 |
psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; |
130 |
frodo |
2 |
|
131 |
frodo |
34 |
int c,i,res; |
132 |
frodo |
2 |
psiconv_buffer buf; |
133 |
|
|
psiconv_file file; |
134 |
frodo |
34 |
psiconv_fileformat ff; |
135 |
frodo |
2 |
|
136 |
frodo |
168 |
psiconv_config config = psiconv_config_read(NULL); |
137 |
frodo |
65 |
|
138 |
frodo |
56 |
fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
139 |
frodo |
34 |
init_txt(); |
140 |
|
|
init_html(); |
141 |
|
|
init_html4(); |
142 |
frodo |
109 |
init_latex(); |
143 |
frodo |
34 |
init_rtf(); |
144 |
|
|
init_image(); |
145 |
|
|
|
146 |
frodo |
2 |
while(1) { |
147 |
|
|
c = getopt_long(argc,argv,short_options, long_options, &option_index); |
148 |
|
|
if (c == -1) |
149 |
|
|
break; |
150 |
|
|
switch(c) { |
151 |
|
|
case 'h': print_help(); exit(0); |
152 |
|
|
case 'V': print_version(); exit(0); |
153 |
frodo |
168 |
case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
154 |
|
|
config->verbosity = PSICONV_VERB_PROGRESS; |
155 |
frodo |
2 |
break; |
156 |
frodo |
168 |
case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
157 |
|
|
case 's': config->verbosity = PSICONV_VERB_FATAL; break; |
158 |
frodo |
2 |
case 'o': outputfilename = strdup(optarg); break; |
159 |
|
|
case 'T': type = strdup(optarg); break; |
160 |
frodo |
147 |
case 'u': encoding_type = PSICONV_ENCODING_UTF8; break; |
161 |
frodo |
34 |
case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
162 |
frodo |
2 |
exit(1); |
163 |
|
|
default: fprintf(stderr,"Internal error: getopt_long returned character " |
164 |
|
|
"code 0%o ?? (contact the author)\n", c); |
165 |
|
|
exit(1); break; |
166 |
|
|
} |
167 |
|
|
} |
168 |
|
|
if (optind < argc-1) { |
169 |
|
|
fputs("I can only convert one file!\n" |
170 |
|
|
"Try `-h' for more information\n",stderr); |
171 |
|
|
exit(1); |
172 |
|
|
} else if (optind == argc-1) |
173 |
|
|
inputfilename = strdup(argv[optind]); |
174 |
|
|
|
175 |
frodo |
65 |
|
176 |
frodo |
2 |
/* Open inputfile for reading */ |
177 |
|
|
|
178 |
|
|
if (strlen(inputfilename) != 0) { |
179 |
|
|
if(stat(inputfilename,&fbuf) < 0) { |
180 |
|
|
perror(inputfilename); |
181 |
|
|
exit(1); |
182 |
|
|
} |
183 |
|
|
f = fopen(inputfilename,"r"); |
184 |
|
|
if (! f) { |
185 |
|
|
perror(inputfilename); |
186 |
|
|
exit(1); |
187 |
|
|
} |
188 |
|
|
} else |
189 |
|
|
f = stdin; |
190 |
|
|
|
191 |
frodo |
80 |
buf = psiconv_buffer_new(); |
192 |
frodo |
79 |
psiconv_buffer_fread_all(buf,f); |
193 |
frodo |
2 |
|
194 |
|
|
if (strlen(inputfilename) != 0) |
195 |
|
|
if (fclose(f)) { |
196 |
|
|
perror(inputfilename); |
197 |
|
|
exit(1); |
198 |
|
|
} |
199 |
|
|
|
200 |
frodo |
168 |
if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file)) |
201 |
frodo |
66 |
{ |
202 |
|
|
fprintf(stderr,"Parse error\n"); |
203 |
|
|
exit(1); |
204 |
|
|
} |
205 |
frodo |
2 |
|
206 |
frodo |
53 |
/* Set correct output file */ |
207 |
|
|
if (strlen(outputfilename) == 0) |
208 |
|
|
outputfilename = "/dev/stdout"; |
209 |
frodo |
2 |
|
210 |
|
|
strtoupper(type); |
211 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
212 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
213 |
frodo |
147 |
if (! strcasecmp(type,ff->name)) { |
214 |
|
|
res = ff->output(outputfilename,file,type, encoding_type); |
215 |
frodo |
34 |
if (res) { |
216 |
|
|
fprintf(stderr, |
217 |
|
|
"Output format `%s' not permitted for this file type\n",type); |
218 |
|
|
exit(1); |
219 |
|
|
} |
220 |
|
|
break; |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
if (i == psiconv_list_length(fileformat_list)) { |
225 |
frodo |
2 |
fprintf(stderr,"Unknown output type: `%s'\n",type); |
226 |
|
|
exit(1); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
psiconv_free_file(file); |
230 |
|
|
|
231 |
|
|
exit(0); |
232 |
|
|
} |