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