… | |
… | |
13 | GNU General Public License for more details. |
13 | GNU General Public License for more details. |
14 | |
14 | |
15 | You should have received a copy of the GNU General Public License |
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 |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
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. |
18 | */ |
34 | */ |
19 | |
35 | |
20 | /* Driver program */ |
36 | /* Driver program */ |
21 | |
37 | |
22 | #include "config.h" |
38 | #include "config.h" |
… | |
… | |
31 | #ifdef HAVE_UNISTD_H |
47 | #ifdef HAVE_UNISTD_H |
32 | #include <unistd.h> |
48 | #include <unistd.h> |
33 | #endif |
49 | #endif |
34 | |
50 | |
35 | #ifdef IMAGEMAGICK |
51 | #ifdef IMAGEMAGICK |
36 | #include <magick/magick.h> |
52 | #include "magick-aux.h" |
37 | #endif |
53 | #endif |
38 | |
54 | |
39 | #include "psiconv/data.h" |
55 | #include <psiconv/data.h> |
|
|
56 | #include <psiconv/list.h> |
40 | #include "psiconv/parse.h" |
57 | #include <psiconv/parse.h> |
|
|
58 | #include <psiconv/configuration.h> |
|
|
59 | #include "psiconv.h" |
41 | #include "gen.h" |
60 | #include "gen.h" |
42 | #include "psiconv.h" |
|
|
43 | |
61 | |
44 | static void print_help(void); |
62 | static void print_help(void); |
45 | static void print_version(void); |
63 | static void print_version(void); |
46 | static void strtoupper(char *str); |
64 | static void strtoupper(char *str); |
47 | |
65 | |
48 | void print_help(void) |
66 | void print_help(void) |
49 | { |
67 | { |
50 | psiconv_fileformat ff; |
68 | fileformat ff; |
51 | int i,j; |
69 | int i,j; |
52 | |
70 | |
53 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
54 | puts("Convert the psion Word file FILE to a HTML file"); |
72 | puts("Convert the psion file FILE to other formats"); |
55 | puts("If FILE is not specified, use stdin"); |
73 | puts("If FILE is not specified, use stdin"); |
56 | puts(" -d, --debug Show debug information on stderr"); |
74 | puts(" -d, --debug Show debug information on stderr"); |
|
|
75 | puts(" -e, --encoding Output encoding (default: UTF8)"); |
57 | puts(" -h, --help Display this help and exit"); |
76 | puts(" -h, --help Display this help and exit"); |
58 | puts(" -o, --outputfile Output to file instead of stdout"); |
77 | puts(" -o, --outputfile Output to file instead of stdout"); |
59 | puts(" -s, --silent Do not even show warnings on stderr"); |
78 | puts(" -s, --silent Do not even show warnings on stderr"); |
60 | puts(" -T, --type=FILETYPE Output type"); |
79 | puts(" -T, --type=FILETYPE Output type"); |
61 | puts(" -V, --version Display the program version and exit"); |
80 | puts(" -V, --version Display the program version and exit"); |
62 | puts(" -v, --verbose Show progress indicators on stderr"); |
81 | puts(" -v, --verbose Show progress indicators on stderr"); |
|
|
82 | puts(""); |
|
|
83 | puts("The following encodings are currently supported:"); |
|
|
84 | puts(" UTF8 Variable length Unicode encoding"); |
|
|
85 | puts(" UCS2 Fixed 16-bit length Unicode encoding"); |
|
|
86 | puts(" Psion The encoding your Psion uses (as in psiconv.conf)"); |
|
|
87 | puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')"); |
63 | puts(""); |
88 | puts(""); |
64 | puts("The following output types are known:"); |
89 | puts("The following output types are known:"); |
65 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
90 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
66 | ff = psiconv_list_get(fileformat_list,i); |
91 | ff = psiconv_list_get(fileformat_list,i); |
67 | printf(" %s",ff->name); |
92 | printf(" %s",ff->name); |
68 | for (j = strlen(ff->name); j < 15; j++) |
93 | for (j = strlen(ff->name); j < 15; j++) |
69 | putchar(' '); |
94 | putchar(' '); |
70 | puts(ff->description); |
95 | puts(ff->description); |
71 | } |
96 | } |
|
|
97 | puts(""); |
|
|
98 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
|
|
99 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
72 | } |
100 | } |
73 | |
101 | |
74 | void print_version(void) |
102 | void print_version(void) |
75 | { |
103 | { |
76 | printf("Version %s\n",VERSION); |
104 | printf("Version %s\n",VERSION); |
… | |
… | |
92 | {"verbose",no_argument,NULL,'v'}, |
120 | {"verbose",no_argument,NULL,'v'}, |
93 | {"debug",no_argument,NULL,'d'}, |
121 | {"debug",no_argument,NULL,'d'}, |
94 | {"silent",no_argument,NULL,'s'}, |
122 | {"silent",no_argument,NULL,'s'}, |
95 | {"outputfile",required_argument,NULL,'o'}, |
123 | {"outputfile",required_argument,NULL,'o'}, |
96 | {"type",required_argument,NULL,'T'}, |
124 | {"type",required_argument,NULL,'T'}, |
|
|
125 | {"encoding",no_argument,NULL,'e'}, |
97 | {0,0,0,0} |
126 | {0,0,0,0} |
98 | }; |
127 | }; |
99 | const char* short_options = "hVvsdo:eT:"; |
128 | const char* short_options = "hVvsdo:e:T:"; |
100 | int option_index; |
129 | int option_index; |
101 | FILE * f; |
130 | FILE * f; |
102 | struct stat fbuf; |
131 | struct stat fbuf; |
103 | |
132 | |
104 | const char *inputfilename = ""; |
133 | const char *inputfilename = ""; |
105 | const char *outputfilename = ""; |
134 | const char *outputfilename = ""; |
106 | char *type = strdup("HTML3"); |
135 | char *type = strdup("HTML3"); |
|
|
136 | encoding encoding_type=ENCODING_UTF8; |
|
|
137 | psiconv_list outputlist; |
107 | |
138 | |
108 | int c,i,res; |
139 | int c,i,res; |
109 | psiconv_buffer buf; |
140 | psiconv_buffer buf; |
110 | psiconv_file file; |
141 | psiconv_file file; |
111 | psiconv_fileformat ff; |
142 | fileformat ff = NULL; |
112 | |
143 | |
113 | psiconv_verbosity = PSICONV_VERB_WARN; |
144 | psiconv_config config = psiconv_config_default(); |
|
|
145 | psiconv_config_read(NULL,&config); |
114 | |
146 | |
115 | fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
147 | if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { |
|
|
148 | fputs("Out of memory error",stderr); |
|
|
149 | exit(1); |
|
|
150 | } |
|
|
151 | |
116 | init_txt(); |
152 | init_txt(); |
|
|
153 | init_image(); |
|
|
154 | #if 0 |
117 | init_html(); |
155 | init_html(); |
118 | init_html4(); |
156 | init_html4(); |
|
|
157 | init_latex(); |
119 | init_rtf(); |
158 | init_rtf(); |
120 | init_image(); |
159 | #endif |
121 | |
160 | |
122 | while(1) { |
161 | while(1) { |
123 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
162 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
124 | if (c == -1) |
163 | if (c == -1) |
125 | break; |
164 | break; |
126 | switch(c) { |
165 | switch(c) { |
127 | case 'h': print_help(); exit(0); |
166 | case 'h': print_help(); exit(0); |
128 | case 'V': print_version(); exit(0); |
167 | case 'V': print_version(); exit(0); |
129 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
168 | case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
130 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
169 | config->verbosity = PSICONV_VERB_PROGRESS; |
131 | break; |
170 | break; |
132 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
171 | case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
133 | case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; |
172 | case 's': config->verbosity = PSICONV_VERB_ERROR; break; |
134 | case 'o': outputfilename = strdup(optarg); break; |
173 | case 'o': outputfilename = strdup(optarg); break; |
135 | case 'T': type = strdup(optarg); break; |
174 | case 'T': type = strdup(optarg); break; |
|
|
175 | case 'e': if(!strcmp(optarg,"UTF8")) |
|
|
176 | encoding_type = ENCODING_UTF8; |
|
|
177 | else if (!strcmp(optarg,"UCS2")) |
|
|
178 | encoding_type = ENCODING_UCS2; |
|
|
179 | else if (!strcmp(optarg,"ASCII")) |
|
|
180 | encoding_type = ENCODING_ASCII; |
|
|
181 | else if (!strcmp(optarg,"Psion")) |
|
|
182 | encoding_type = ENCODING_PSION; |
|
|
183 | else { |
|
|
184 | fputs("Unknown encoding type " |
|
|
185 | "(try '-h' for more information\n",stderr); |
|
|
186 | exit(1); |
|
|
187 | } |
|
|
188 | break; |
136 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
189 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
137 | exit(1); |
190 | exit(1); |
138 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
191 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
139 | "code 0%o ?? (contact the author)\n", c); |
192 | "code 0%o ?? (contact the author)\n", c); |
140 | exit(1); break; |
193 | exit(1); break; |
… | |
… | |
143 | if (optind < argc-1) { |
196 | if (optind < argc-1) { |
144 | fputs("I can only convert one file!\n" |
197 | fputs("I can only convert one file!\n" |
145 | "Try `-h' for more information\n",stderr); |
198 | "Try `-h' for more information\n",stderr); |
146 | exit(1); |
199 | exit(1); |
147 | } else if (optind == argc-1) |
200 | } else if (optind == argc-1) |
148 | inputfilename = strdup(argv[optind]); |
201 | if (!(inputfilename = strdup(argv[optind]))) { |
|
|
202 | fputs("Out of memory error",stderr); |
|
|
203 | exit(1); |
|
|
204 | } |
149 | |
205 | |
150 | |
206 | |
151 | /* Open inputfile for reading */ |
207 | /* Open inputfile for reading */ |
152 | |
208 | |
153 | if (strlen(inputfilename) != 0) { |
209 | if (strlen(inputfilename) != 0) { |
… | |
… | |
161 | exit(1); |
217 | exit(1); |
162 | } |
218 | } |
163 | } else |
219 | } else |
164 | f = stdin; |
220 | f = stdin; |
165 | |
221 | |
166 | buf = psiconv_list_new(sizeof(psiconv_u8)); |
222 | if (!(buf = psiconv_buffer_new())) { |
167 | while (! feof(f)) |
223 | fputs("Out of memory error",stderr); |
168 | psiconv_list_fread(buf,1024,f); |
224 | exit(1); |
|
|
225 | } |
|
|
226 | if (psiconv_buffer_fread_all(buf,f)) { |
|
|
227 | fprintf(stderr,"Failure reading file"); |
|
|
228 | exit(1); |
|
|
229 | } |
169 | |
230 | |
170 | if (strlen(inputfilename) != 0) |
231 | if (strlen(inputfilename) != 0) |
171 | if (fclose(f)) { |
232 | if (fclose(f)) { |
172 | perror(inputfilename); |
233 | perror(inputfilename); |
173 | exit(1); |
234 | exit(1); |
174 | } |
235 | } |
175 | |
236 | |
176 | if (psiconv_parse(buf,&file) || (file->type == psiconv_unknown_file)) |
237 | if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file)) |
177 | { |
238 | { |
178 | fprintf(stderr,"Parse error\n"); |
239 | fprintf(stderr,"Parse error\n"); |
179 | exit(1); |
240 | exit(1); |
180 | } |
241 | } |
181 | |
242 | |
182 | /* Set correct output file */ |
|
|
183 | if (strlen(outputfilename) == 0) |
|
|
184 | outputfilename = "/dev/stdout"; |
|
|
185 | |
|
|
186 | strtoupper(type); |
243 | strtoupper(type); |
187 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
244 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
188 | ff = psiconv_list_get(fileformat_list,i); |
245 | ff = psiconv_list_get(fileformat_list,i); |
189 | if (! strcmp(type,ff->name)) { |
246 | if (! strcasecmp(type,ff->name)) { |
190 | res = ff->output(outputfilename,file,type); |
|
|
191 | if (res) { |
|
|
192 | fprintf(stderr, |
|
|
193 | "Output format `%s' not permitted for this file type\n",type); |
|
|
194 | exit(1); |
|
|
195 | } |
|
|
196 | break; |
247 | break; |
197 | } |
248 | } |
198 | } |
249 | } |
199 | |
250 | |
200 | if (i == psiconv_list_length(fileformat_list)) { |
251 | if (i == psiconv_list_length(fileformat_list)) { |
201 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
252 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
202 | exit(1); |
253 | exit(1); |
203 | } |
254 | } |
204 | |
255 | |
|
|
256 | if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) { |
|
|
257 | fputs("Out of memory error\n",stderr); |
|
|
258 | exit(1); |
|
|
259 | } |
|
|
260 | |
|
|
261 | res = ff->output(config,outputlist,file,type,encoding_type); |
|
|
262 | if (res) { |
|
|
263 | fprintf(stderr, |
|
|
264 | "Output format `%s' not permitted for this file type\n",type); |
|
|
265 | exit(1); |
|
|
266 | } |
|
|
267 | |
205 | psiconv_free_file(file); |
268 | psiconv_free_file(file); |
206 | |
269 | |
|
|
270 | if (strlen(outputfilename) != 0) { |
|
|
271 | f = fopen(outputfilename,"w"); |
|
|
272 | if (! f) { |
|
|
273 | perror(inputfilename); |
|
|
274 | exit(1); |
|
|
275 | } |
|
|
276 | } else |
|
|
277 | f = stdout; |
|
|
278 | |
|
|
279 | psiconv_list_fwrite_all(outputlist,f); |
|
|
280 | |
|
|
281 | if (fclose(f)) { |
|
|
282 | perror(inputfilename); |
|
|
283 | exit(1); |
|
|
284 | } |
|
|
285 | |
|
|
286 | psiconv_list_free(outputlist); |
|
|
287 | |
207 | exit(0); |
288 | exit(0); |
208 | } |
289 | } |