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

Legend:
Removed from v.2  
changed lines
  Added in v.168

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