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

Legend:
Removed from v.53  
changed lines
  Added in v.190

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