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