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