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