… | |
… | |
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. |
|
|
34 | */ |
18 | */ |
35 | |
19 | |
36 | /* Driver program */ |
20 | /* Driver program */ |
37 | |
21 | |
38 | #include "config.h" |
22 | #include "config.h" |
… | |
… | |
50 | |
34 | |
51 | #ifdef IMAGEMAGICK |
35 | #ifdef IMAGEMAGICK |
52 | #include "magick-aux.h" |
36 | #include "magick-aux.h" |
53 | #endif |
37 | #endif |
54 | |
38 | |
55 | #include "psiconv/data.h" |
39 | #include <psiconv/data.h> |
|
|
40 | #include <psiconv/list.h> |
56 | #include "psiconv/parse.h" |
41 | #include <psiconv/parse.h> |
57 | #include "psiconv/configuration.h" |
42 | #include <psiconv/configuration.h> |
|
|
43 | #include "psiconv.h" |
58 | #include "gen.h" |
44 | #include "gen.h" |
59 | #include "psiconv.h" |
|
|
60 | |
45 | |
61 | static void print_help(void); |
46 | static void print_help(void); |
62 | static void print_version(void); |
47 | static void print_version(void); |
63 | static void strtoupper(char *str); |
48 | static void strtoupper(char *str); |
64 | |
49 | |
65 | void print_help(void) |
50 | void print_help(void) |
66 | { |
51 | { |
67 | psiconv_fileformat ff; |
52 | fileformat ff; |
68 | int i,j; |
53 | int i,j; |
69 | |
54 | |
70 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
55 | puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
71 | puts("Convert the psion Word file FILE to other formats"); |
56 | puts("Convert the psion file FILE to other formats"); |
72 | puts("If FILE is not specified, use stdin"); |
57 | puts("If FILE is not specified, use stdin"); |
73 | puts(" -d, --debug Show debug information on stderr"); |
58 | puts(" -e, --encoding=ENC Output encoding (default: UTF8)"); |
74 | puts(" -h, --help Display this help and exit"); |
59 | puts(" -h, --help Display this help and exit"); |
|
|
60 | puts(" -n, --noise=LEVEL Select what to print on stderr (overrides psiconv.conf)"); |
75 | puts(" -o, --outputfile Output to file instead of stdout"); |
61 | puts(" -o, --outputfile Output to file instead of stdout"); |
76 | puts(" -s, --silent Do not even show warnings on stderr"); |
|
|
77 | puts(" -T, --type=FILETYPE Output type"); |
62 | puts(" -T, --type=FILETYPE Output type (default: XHTML or TIFF"); |
78 | puts(" -V, --version Display the program version and exit"); |
63 | puts(" -V, --version Display the program version and exit"); |
79 | puts(" -v, --verbose Show progress indicators on stderr"); |
64 | puts(""); |
80 | puts(" -u, --UTF8 Input file is encoded in UTF8"); |
65 | puts("The following encodings are currently supported:"); |
|
|
66 | puts(" UTF8 Variable length Unicode encoding"); |
|
|
67 | puts(" UCS2 Fixed 16-bit length Unicode encoding"); |
|
|
68 | puts(" Psion The encoding your Psion uses (as in psiconv.conf)"); |
|
|
69 | puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')"); |
|
|
70 | puts(""); |
|
|
71 | puts("The following noise levels are currently supported:"); |
|
|
72 | puts(" 1 or F: Fatal errors only"); |
|
|
73 | puts(" 2 or E: Errors"); |
|
|
74 | puts(" 3 or W: Warnings"); |
|
|
75 | puts(" 4 or P: Progress indicators"); |
|
|
76 | puts(" 5 or D: Debug data"); |
|
|
77 | puts(""); |
|
|
78 | puts("The following abbreviations are used below:"); |
|
|
79 | puts(" C - processes ClipArt files"); |
|
|
80 | puts(" c - processes ClipArt files containing only one image"); |
|
|
81 | puts(" M - processes MBM files"); |
|
|
82 | puts(" m - processes MBM files containing only one image"); |
|
|
83 | puts(" S - processes Sketch files"); |
|
|
84 | puts(" T - processes TextEd files"); |
|
|
85 | puts(" W - processes Word files"); |
81 | puts(""); |
86 | puts(""); |
82 | puts("The following output types are known:"); |
87 | puts("The following output types are known:"); |
83 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
88 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
84 | ff = psiconv_list_get(fileformat_list,i); |
89 | ff = psiconv_list_get(fileformat_list,i); |
85 | printf(" %s",ff->name); |
90 | printf(" %s",ff->name); |
86 | for (j = strlen(ff->name); j < 15; j++) |
91 | for (j = strlen(ff->name); j < 10; j++) |
87 | putchar(' '); |
92 | putchar(' '); |
|
|
93 | printf("[%c%c%c%c%c] ", |
|
|
94 | ff->supported_format & FORMAT_CLIPART_MULTIPLE?'C': |
|
|
95 | ff->supported_format & FORMAT_CLIPART_SINGLE?'c':' ', |
|
|
96 | ff->supported_format & FORMAT_MBM_MULTIPLE?'M': |
|
|
97 | ff->supported_format & FORMAT_MBM_SINGLE?'m':' ', |
|
|
98 | ff->supported_format & FORMAT_SKETCH?'S':' ', |
|
|
99 | ff->supported_format & FORMAT_TEXTED?'T':' ', |
|
|
100 | ff->supported_format & FORMAT_WORD?'W':' '); |
88 | puts(ff->description); |
101 | puts(ff->description); |
89 | } |
102 | } |
90 | puts(""); |
103 | puts(""); |
91 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
104 | puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); |
92 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
105 | puts(" to a suitable encoding for your LaTeX installation before being typeset"); |
… | |
… | |
108 | { |
121 | { |
109 | struct option long_options[] = |
122 | struct option long_options[] = |
110 | { |
123 | { |
111 | {"help",no_argument,NULL,'h'}, |
124 | {"help",no_argument,NULL,'h'}, |
112 | {"version",no_argument,NULL,'V'}, |
125 | {"version",no_argument,NULL,'V'}, |
113 | {"verbose",no_argument,NULL,'v'}, |
126 | {"noise",required_argument,NULL,'n'}, |
114 | {"debug",no_argument,NULL,'d'}, |
|
|
115 | {"silent",no_argument,NULL,'s'}, |
|
|
116 | {"outputfile",required_argument,NULL,'o'}, |
127 | {"outputfile",required_argument,NULL,'o'}, |
117 | {"type",required_argument,NULL,'T'}, |
128 | {"type",required_argument,NULL,'T'}, |
118 | {"UTF8",no_argument,NULL,'u'}, |
129 | {"encoding",no_argument,NULL,'e'}, |
119 | {0,0,0,0} |
130 | {0,0,0,0} |
120 | }; |
131 | }; |
121 | const char* short_options = "hVvsdo:uT:"; |
132 | const char* short_options = "hVn:o:e:T:"; |
122 | int option_index; |
133 | int option_index; |
123 | FILE * f; |
134 | FILE * f; |
124 | struct stat fbuf; |
135 | struct stat fbuf; |
125 | |
136 | |
126 | const char *inputfilename = ""; |
137 | const char *inputfilename = ""; |
127 | const char *outputfilename = ""; |
138 | const char *outputfilename = ""; |
128 | char *type = strdup("HTML3"); |
139 | char *type = NULL; |
129 | psiconv_encoding encoding_type=PSICONV_ENCODING_CP1252; |
140 | encoding encoding_type=ENCODING_UTF8; |
|
|
141 | psiconv_list outputlist; |
130 | |
142 | |
131 | int c,i,res; |
143 | int c,i,res; |
132 | psiconv_buffer buf; |
144 | psiconv_buffer buf; |
133 | psiconv_file file; |
145 | psiconv_file file; |
134 | psiconv_fileformat ff; |
146 | fileformat ff = NULL; |
135 | |
147 | |
136 | psiconv_config config = psiconv_config_default(); |
148 | psiconv_config config = psiconv_config_default(); |
137 | psiconv_config_read(NULL,&config); |
149 | psiconv_config_read(NULL,&config); |
138 | |
150 | |
139 | fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
151 | if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { |
|
|
152 | fputs("Out of memory error",stderr); |
|
|
153 | exit(1); |
|
|
154 | } |
|
|
155 | |
140 | init_txt(); |
156 | init_txt(); |
141 | init_html(); |
157 | init_xhtml(); |
142 | init_html4(); |
158 | init_html4(); |
143 | init_latex(); |
|
|
144 | init_rtf(); |
|
|
145 | init_image(); |
159 | init_image(); |
146 | |
160 | |
147 | while(1) { |
161 | while(1) { |
148 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
162 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
149 | if (c == -1) |
163 | if (c == -1) |
150 | break; |
164 | break; |
151 | switch(c) { |
165 | switch(c) { |
152 | case 'h': print_help(); exit(0); |
166 | case 'h': print_help(); exit(0); |
153 | case 'V': print_version(); exit(0); |
167 | case 'V': print_version(); exit(0); |
154 | case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) |
168 | case 'n': switch(optarg[0]) { |
|
|
169 | case '1': case 'F':case 'f': |
|
|
170 | config->verbosity=PSICONV_VERB_FATAL; |
|
|
171 | break; |
|
|
172 | case '2': case 'E':case 'e': |
|
|
173 | config->verbosity=PSICONV_VERB_ERROR; |
|
|
174 | break; |
|
|
175 | case '3': case 'W':case 'w': |
|
|
176 | config->verbosity=PSICONV_VERB_WARN; |
|
|
177 | break; |
|
|
178 | case '4': case 'P':case 'p': |
155 | config->verbosity = PSICONV_VERB_PROGRESS; |
179 | config->verbosity=PSICONV_VERB_PROGRESS; |
|
|
180 | break; |
|
|
181 | case '5': case 'D':case 'd': |
|
|
182 | config->verbosity=PSICONV_VERB_DEBUG; |
|
|
183 | break; |
|
|
184 | default: |
|
|
185 | fputs("Unknown noise level\n",stderr); |
|
|
186 | exit(1); |
|
|
187 | } |
156 | break; |
188 | break; |
157 | case 'd': config->verbosity = PSICONV_VERB_DEBUG; break; |
|
|
158 | case 's': config->verbosity = PSICONV_VERB_FATAL; break; |
|
|
159 | case 'o': outputfilename = strdup(optarg); break; |
189 | case 'o': outputfilename = strdup(optarg); break; |
160 | case 'T': type = strdup(optarg); break; |
190 | case 'T': type = strdup(optarg); break; |
161 | case 'u': encoding_type = PSICONV_ENCODING_UTF8; break; |
191 | case 'e': if(!strcmp(optarg,"UTF8")) |
|
|
192 | encoding_type = ENCODING_UTF8; |
|
|
193 | else if (!strcmp(optarg,"UCS2")) |
|
|
194 | encoding_type = ENCODING_UCS2; |
|
|
195 | else if (!strcmp(optarg,"ASCII")) |
|
|
196 | encoding_type = ENCODING_ASCII; |
|
|
197 | else if (!strcmp(optarg,"Psion")) |
|
|
198 | encoding_type = ENCODING_PSION; |
|
|
199 | else { |
|
|
200 | fputs("Unknown encoding type " |
|
|
201 | "(try '-h' for more information\n",stderr); |
|
|
202 | exit(1); |
|
|
203 | } |
|
|
204 | break; |
162 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
205 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
163 | exit(1); |
206 | exit(1); |
164 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
207 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
165 | "code 0%o ?? (contact the author)\n", c); |
208 | "code 0%o ?? (contact the author)\n", c); |
166 | exit(1); break; |
209 | exit(1); break; |
… | |
… | |
169 | if (optind < argc-1) { |
212 | if (optind < argc-1) { |
170 | fputs("I can only convert one file!\n" |
213 | fputs("I can only convert one file!\n" |
171 | "Try `-h' for more information\n",stderr); |
214 | "Try `-h' for more information\n",stderr); |
172 | exit(1); |
215 | exit(1); |
173 | } else if (optind == argc-1) |
216 | } else if (optind == argc-1) |
174 | inputfilename = strdup(argv[optind]); |
217 | if (!(inputfilename = strdup(argv[optind]))) { |
|
|
218 | fputs("Out of memory error",stderr); |
|
|
219 | exit(1); |
|
|
220 | } |
175 | |
221 | |
176 | |
222 | |
177 | /* Open inputfile for reading */ |
223 | /* Open inputfile for reading */ |
178 | |
224 | |
179 | if (strlen(inputfilename) != 0) { |
225 | if (strlen(inputfilename) != 0) { |
… | |
… | |
187 | exit(1); |
233 | exit(1); |
188 | } |
234 | } |
189 | } else |
235 | } else |
190 | f = stdin; |
236 | f = stdin; |
191 | |
237 | |
192 | buf = psiconv_buffer_new(); |
238 | if (!(buf = psiconv_buffer_new())) { |
|
|
239 | fputs("Out of memory error",stderr); |
|
|
240 | exit(1); |
|
|
241 | } |
193 | psiconv_buffer_fread_all(buf,f); |
242 | if (psiconv_buffer_fread_all(buf,f)) { |
|
|
243 | fprintf(stderr,"Failure reading file"); |
|
|
244 | exit(1); |
|
|
245 | } |
194 | |
246 | |
195 | if (strlen(inputfilename) != 0) |
247 | if (strlen(inputfilename) != 0) |
196 | if (fclose(f)) { |
248 | if (fclose(f)) { |
197 | perror(inputfilename); |
249 | perror(inputfilename); |
198 | exit(1); |
250 | exit(1); |
… | |
… | |
202 | { |
254 | { |
203 | fprintf(stderr,"Parse error\n"); |
255 | fprintf(stderr,"Parse error\n"); |
204 | exit(1); |
256 | exit(1); |
205 | } |
257 | } |
206 | |
258 | |
207 | /* Set correct output file */ |
259 | if (!type) { |
208 | if (strlen(outputfilename) == 0) |
260 | switch(file->type) { |
209 | outputfilename = "/dev/stdout"; |
261 | case psiconv_word_file: |
210 | |
262 | case psiconv_texted_file: |
|
|
263 | default: |
|
|
264 | type = "XHTML"; break; |
|
|
265 | case psiconv_mbm_file: |
|
|
266 | case psiconv_clipart_file: |
|
|
267 | case psiconv_sketch_file: |
|
|
268 | type = "TIFF"; break; |
|
|
269 | } |
|
|
270 | } else |
211 | strtoupper(type); |
271 | strtoupper(type); |
|
|
272 | |
212 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
273 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
213 | ff = psiconv_list_get(fileformat_list,i); |
274 | ff = psiconv_list_get(fileformat_list,i); |
214 | if (! strcasecmp(type,ff->name)) { |
275 | if (! strcasecmp(type,ff->name)) { |
215 | res = ff->output(outputfilename,file,type, encoding_type); |
|
|
216 | if (res) { |
|
|
217 | fprintf(stderr, |
|
|
218 | "Output format `%s' not permitted for this file type\n",type); |
|
|
219 | exit(1); |
|
|
220 | } |
|
|
221 | break; |
276 | break; |
222 | } |
277 | } |
223 | } |
278 | } |
224 | |
279 | |
225 | if (i == psiconv_list_length(fileformat_list)) { |
280 | if (i == psiconv_list_length(fileformat_list)) { |
226 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
281 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
227 | exit(1); |
282 | exit(1); |
228 | } |
283 | } |
229 | |
284 | |
|
|
285 | if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) { |
|
|
286 | fputs("Out of memory error\n",stderr); |
|
|
287 | exit(1); |
|
|
288 | } |
|
|
289 | |
|
|
290 | res = ff->output(config,outputlist,file,type,encoding_type); |
|
|
291 | if (res) { |
|
|
292 | fprintf(stderr, |
|
|
293 | "Output format `%s' not permitted for this file type\n",type); |
|
|
294 | exit(1); |
|
|
295 | } |
|
|
296 | |
230 | psiconv_free_file(file); |
297 | psiconv_free_file(file); |
|
|
298 | |
|
|
299 | if (strlen(outputfilename) != 0) { |
|
|
300 | f = fopen(outputfilename,"w"); |
|
|
301 | if (! f) { |
|
|
302 | perror(inputfilename); |
|
|
303 | exit(1); |
|
|
304 | } |
|
|
305 | } else |
|
|
306 | f = stdout; |
|
|
307 | |
|
|
308 | psiconv_list_fwrite_all(outputlist,f); |
|
|
309 | |
|
|
310 | if (fclose(f)) { |
|
|
311 | perror(inputfilename); |
|
|
312 | exit(1); |
|
|
313 | } |
|
|
314 | |
|
|
315 | psiconv_list_free(outputlist); |
231 | |
316 | |
232 | exit(0); |
317 | exit(0); |
233 | } |
318 | } |