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