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