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