/[public]/psiconv/trunk/program/psiconv/psiconv.c
ViewVC logotype

Diff of /psiconv/trunk/program/psiconv/psiconv.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 17 Revision 187
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*/ 18*/
19 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*/
35
20/* Driver program */ 36/* Driver program */
21 37
22#include "config.h" 38#include "config.h"
39#include "compat.h"
23#include <getopt.h> 40#include <getopt.h>
24#include <stdio.h> 41#include <stdio.h>
25#include <sys/stat.h> 42#include <sys/stat.h>
26#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h>
45#include <ctype.h>
27 46
28#ifdef HAVE_UNISTD_H 47#ifdef HAVE_UNISTD_H
29#include <unistd.h> 48#include <unistd.h>
30#endif 49#endif
31 50
32#include <string.h> 51#ifdef IMAGEMAGICK
33#include <ctype.h>
34#include "data.h" 52#include "magick-aux.h"
53#endif
54
55#include <psiconv/data.h>
56#include <psiconv/list.h>
57#include <psiconv/parse.h>
58#include <psiconv/configuration.h>
35#include "parse.h" 59#include "psiconv.h"
36#include "gen.h" 60#include "gen.h"
37 61
38static void print_help(void); 62static void print_help(void);
39static void print_version(void); 63static void print_version(void);
40static void strtoupper(char *str); 64static void strtoupper(char *str);
41 65
42int psiconv_verbosity = PSICONV_VERB_WARN;
43
44void print_help(void) 66void print_help(void)
45{ 67{
68 fileformat ff;
69 int i,j;
70
46 puts("Syntax: psiconv [OPTIONS..] [FILE]"); 71 puts("Syntax: psiconv [OPTIONS..] [FILE]");
47 puts("Convert the psion Word file FILE to a HTML file"); 72 puts("Convert the psion file FILE to other formats");
48 puts("If FILE is not specified, use stdin"); 73 puts("If FILE is not specified, use stdin");
49 puts(" -d, --debug Show debug information on stderr"); 74 puts(" -d, --debug Show debug information on stderr");
50 puts(" -e, --exact Quit on any parse errors or warnings"); 75 puts(" -e, --encoding Output encoding (default: UTF8)");
51 puts(" -h, --help Display this help and exit"); 76 puts(" -h, --help Display this help and exit");
52 puts(" -o, --outputfile Output to file instead of stdout"); 77 puts(" -o, --outputfile Output to file instead of stdout");
53 puts(" -s, --silent Do not even show warnings on stderr"); 78 puts(" -s, --silent Do not even show warnings on stderr");
54 puts(" -T, --type=FILETYPE Output type"); 79 puts(" -T, --type=FILETYPE Output type");
55 puts(" -V, --version Display the program version and exit"); 80 puts(" -V, --version Display the program version and exit");
56 puts(" -v, --verbose Show progress indicators on stderr"); 81 puts(" -v, --verbose Show progress indicators on stderr");
57 puts(""); 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 '?')");
88 puts("");
58 puts("The following output types are known:"); 89 puts("The following output types are known:");
59 puts(" HTML (default)"); 90 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
60 puts(" HTML4"); 91 ff = psiconv_list_get(fileformat_list,i);
61 puts(" ASCII"); 92 printf(" %s",ff->name);
93 for (j = strlen(ff->name); j < 15; j++)
94 putchar(' ');
95 puts(ff->description);
96 }
62 puts(" RTF"); 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");
63} 100}
64 101
65void print_version(void) 102void print_version(void)
66{ 103{
67 printf("Version %s\n",VERSION); 104 printf("Version %s\n",VERSION);
83 {"verbose",no_argument,NULL,'v'}, 120 {"verbose",no_argument,NULL,'v'},
84 {"debug",no_argument,NULL,'d'}, 121 {"debug",no_argument,NULL,'d'},
85 {"silent",no_argument,NULL,'s'}, 122 {"silent",no_argument,NULL,'s'},
86 {"outputfile",required_argument,NULL,'o'}, 123 {"outputfile",required_argument,NULL,'o'},
87 {"type",required_argument,NULL,'T'}, 124 {"type",required_argument,NULL,'T'},
88 {"exact",no_argument,NULL,'e'}, 125 {"encoding",no_argument,NULL,'e'},
89 {0,0,0,0} 126 {0,0,0,0}
90 }; 127 };
91 const char* short_options = "hVvsdo:eT:"; 128 const char* short_options = "hVvsdo:e:T:";
92 int option_index; 129 int option_index;
93 FILE * f; 130 FILE * f;
94 struct stat fbuf; 131 struct stat fbuf;
95 132
96 const char *inputfilename = ""; 133 const char *inputfilename = "";
97 const char *outputfilename = ""; 134 const char *outputfilename = "";
98 char *type = strdup("HTML"); 135 char *type = strdup("HTML3");
99 int exact=0; 136 encoding encoding_type=ENCODING_UTF8;
137 psiconv_list outputlist;
100 138
101 int c; 139 int c,i,res;
102 psiconv_buffer buf; 140 psiconv_buffer buf;
103 psiconv_file file; 141 psiconv_file file;
142 fileformat ff = NULL;
143
144 psiconv_config config = psiconv_config_default();
145 psiconv_config_read(NULL,&config);
146
147 if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) {
148 fputs("Out of memory error",stderr);
149 exit(1);
150 }
151
152 init_txt();
153 init_image();
154#if 0
155 init_html();
156 init_html4();
157 init_latex();
158 init_rtf();
159#endif
104 160
105 while(1) { 161 while(1) {
106 c = getopt_long(argc,argv,short_options, long_options, &option_index); 162 c = getopt_long(argc,argv,short_options, long_options, &option_index);
107 if (c == -1) 163 if (c == -1)
108 break; 164 break;
109 switch(c) { 165 switch(c) {
110 case 'h': print_help(); exit(0); 166 case 'h': print_help(); exit(0);
111 case 'V': print_version(); exit(0); 167 case 'V': print_version(); exit(0);
112 case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) 168 case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS)
113 psiconv_verbosity = PSICONV_VERB_PROGRESS; 169 config->verbosity = PSICONV_VERB_PROGRESS;
114 break; 170 break;
115 case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; 171 case 'd': config->verbosity = PSICONV_VERB_DEBUG; break;
116 case 's': psiconv_verbosity = PSICONV_VERB_SILENT; break; 172 case 's': config->verbosity = PSICONV_VERB_ERROR; break;
117 case 'e': exact = 1; break;
118 case 'o': outputfilename = strdup(optarg); break; 173 case 'o': outputfilename = strdup(optarg); break;
119 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;
120 case '?': case ':': fputs("Try `-h' for more information",stderr); 189 case '?': case ':': fputs("Try `-h' for more information\n",stderr);
121 exit(1); 190 exit(1);
122 default: fprintf(stderr,"Internal error: getopt_long returned character " 191 default: fprintf(stderr,"Internal error: getopt_long returned character "
123 "code 0%o ?? (contact the author)\n", c); 192 "code 0%o ?? (contact the author)\n", c);
124 exit(1); break; 193 exit(1); break;
125 } 194 }
127 if (optind < argc-1) { 196 if (optind < argc-1) {
128 fputs("I can only convert one file!\n" 197 fputs("I can only convert one file!\n"
129 "Try `-h' for more information\n",stderr); 198 "Try `-h' for more information\n",stderr);
130 exit(1); 199 exit(1);
131 } else if (optind == argc-1) 200 } else if (optind == argc-1)
132 inputfilename = strdup(argv[optind]); 201 if (!(inputfilename = strdup(argv[optind]))) {
202 fputs("Out of memory error",stderr);
203 exit(1);
204 }
205
133 206
134 /* Open inputfile for reading */ 207 /* Open inputfile for reading */
135 208
136 if (strlen(inputfilename) != 0) { 209 if (strlen(inputfilename) != 0) {
137 if(stat(inputfilename,&fbuf) < 0) { 210 if(stat(inputfilename,&fbuf) < 0) {
144 exit(1); 217 exit(1);
145 } 218 }
146 } else 219 } else
147 f = stdin; 220 f = stdin;
148 221
149 buf = psiconv_list_new(sizeof(psiconv_u8)); 222 if (!(buf = psiconv_buffer_new())) {
150 while (! feof(f)) 223 fputs("Out of memory error",stderr);
151 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 }
152 230
153 if (strlen(inputfilename) != 0) 231 if (strlen(inputfilename) != 0)
154 if (fclose(f)) { 232 if (fclose(f)) {
155 perror(inputfilename); 233 perror(inputfilename);
156 exit(1); 234 exit(1);
157 } 235 }
158 236
159 if (psiconv_parse(buf,&file)) 237 if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file))
160 if(exact) 238 {
239 fprintf(stderr,"Parse error\n");
161 exit(1); 240 exit(1);
241 }
162 242
163 /* Open outputfile for writing */ 243 strtoupper(type);
244 for (i = 0; i < psiconv_list_length(fileformat_list); i ++) {
245 ff = psiconv_list_get(fileformat_list,i);
246 if (! strcasecmp(type,ff->name)) {
247 break;
248 }
249 }
250
251 if (i == psiconv_list_length(fileformat_list)) {
252 fprintf(stderr,"Unknown output type: `%s'\n",type);
253 exit(1);
254 }
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
268 psiconv_free_file(file);
269
164 if (strlen(outputfilename) != 0) { 270 if (strlen(outputfilename) != 0) {
165 f = fopen(outputfilename,"w"); 271 f = fopen(outputfilename,"w");
166 if (! f) { 272 if (! f) {
167 perror(outputfilename); 273 perror(inputfilename);
168 exit(1); 274 exit(1);
169 } 275 }
170 } else 276 } else
171 f = stdout; 277 f = stdout;
172 278
173 strtoupper(type); 279 psiconv_list_fwrite_all(outputlist,f);
174 if (! strcmp(type,"HTML"))
175 psiconv_gen_html(f,file);
176 else if (! strcmp(type,"HTML4"))
177 psiconv_gen_html4(f,file);
178 else if (! strcmp(type,"ASCII"))
179 psiconv_gen_txt(f,file);
180 else if (! strcmp(type,"RTF"))
181 psiconv_gen_rtf(f,file);
182 else if (! strcmp(type,"IMAGE"))
183 psiconv_gen_image(f,file);
184 else {
185 fprintf(stderr,"Unknown output type: `%s'\n",type);
186 exit(1);
187 }
188 280
189 if (strlen(outputfilename) != 0)
190 if (fclose(f)) { 281 if (fclose(f)) {
191 perror(outputfilename); 282 perror(inputfilename);
192 exit(1); 283 exit(1);
193 } 284 }
194 285
195 psiconv_free_file(file); 286 psiconv_list_free(outputlist);
196 287
197 exit(0); 288 exit(0);
198} 289}

Legend:
Removed from v.17  
changed lines
  Added in v.187

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26