… | |
… | |
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); |
|
|
68 | for (j = strlen(ff->name); j < 15; j++) |
|
|
69 | putchar(' '); |
|
|
70 | puts(ff->description); |
|
|
71 | } |
62 | } |
72 | } |
63 | |
73 | |
64 | void print_version(void) |
74 | void print_version(void) |
65 | { |
75 | { |
66 | printf("Version %s\n",VERSION); |
76 | printf("Version %s\n",VERSION); |
… | |
… | |
82 | {"verbose",no_argument,NULL,'v'}, |
92 | {"verbose",no_argument,NULL,'v'}, |
83 | {"debug",no_argument,NULL,'d'}, |
93 | {"debug",no_argument,NULL,'d'}, |
84 | {"silent",no_argument,NULL,'s'}, |
94 | {"silent",no_argument,NULL,'s'}, |
85 | {"outputfile",required_argument,NULL,'o'}, |
95 | {"outputfile",required_argument,NULL,'o'}, |
86 | {"type",required_argument,NULL,'T'}, |
96 | {"type",required_argument,NULL,'T'}, |
87 | {"exact",no_argument,NULL,'e'}, |
|
|
88 | {0,0,0,0} |
97 | {0,0,0,0} |
89 | }; |
98 | }; |
90 | const char* short_options = "hVvsdo:eT:"; |
99 | const char* short_options = "hVvsdo:eT:"; |
91 | int option_index; |
100 | int option_index; |
92 | FILE * f; |
101 | FILE * f; |
93 | struct stat fbuf; |
102 | struct stat fbuf; |
94 | |
103 | |
95 | const char *inputfilename = ""; |
104 | const char *inputfilename = ""; |
96 | const char *outputfilename = ""; |
105 | const char *outputfilename = ""; |
97 | char *type = strdup("HTML"); |
106 | char *type = strdup("HTML3"); |
98 | int exact=0; |
|
|
99 | |
107 | |
100 | int c; |
108 | int c,i,res; |
101 | psiconv_buffer buf; |
109 | psiconv_buffer buf; |
102 | 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_rtf(); |
|
|
120 | init_image(); |
103 | |
121 | |
104 | while(1) { |
122 | while(1) { |
105 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
123 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
106 | if (c == -1) |
124 | if (c == -1) |
107 | break; |
125 | break; |
… | |
… | |
110 | case 'V': print_version(); exit(0); |
128 | case 'V': print_version(); exit(0); |
111 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
129 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
112 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
130 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
113 | break; |
131 | break; |
114 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
132 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
115 | case 's': psiconv_verbosity = PSICONV_VERB_SILENT; break; |
133 | case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; |
116 | case 'e': exact = 1; break; |
|
|
117 | case 'o': outputfilename = strdup(optarg); break; |
134 | case 'o': outputfilename = strdup(optarg); break; |
118 | case 'T': type = strdup(optarg); break; |
135 | case 'T': type = strdup(optarg); break; |
119 | case '?': case ':': fputs("Try `-h' for more information",stderr); |
136 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
120 | exit(1); |
137 | exit(1); |
121 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
138 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
122 | "code 0%o ?? (contact the author)\n", c); |
139 | "code 0%o ?? (contact the author)\n", c); |
123 | exit(1); break; |
140 | exit(1); break; |
124 | } |
141 | } |
… | |
… | |
128 | "Try `-h' for more information\n",stderr); |
145 | "Try `-h' for more information\n",stderr); |
129 | exit(1); |
146 | exit(1); |
130 | } else if (optind == argc-1) |
147 | } else if (optind == argc-1) |
131 | inputfilename = strdup(argv[optind]); |
148 | inputfilename = strdup(argv[optind]); |
132 | |
149 | |
|
|
150 | |
133 | /* Open inputfile for reading */ |
151 | /* Open inputfile for reading */ |
134 | |
152 | |
135 | if (strlen(inputfilename) != 0) { |
153 | if (strlen(inputfilename) != 0) { |
136 | if(stat(inputfilename,&fbuf) < 0) { |
154 | if(stat(inputfilename,&fbuf) < 0) { |
137 | perror(inputfilename); |
155 | perror(inputfilename); |
… | |
… | |
143 | exit(1); |
161 | exit(1); |
144 | } |
162 | } |
145 | } else |
163 | } else |
146 | f = stdin; |
164 | f = stdin; |
147 | |
165 | |
148 | buf = psiconv_list_new(sizeof(psiconv_u8)); |
166 | buf = psiconv_buffer_new(); |
149 | while (! feof(f)) |
167 | psiconv_buffer_fread_all(buf,f); |
150 | psiconv_list_fread(buf,1024,f); |
|
|
151 | |
168 | |
152 | if (strlen(inputfilename) != 0) |
169 | if (strlen(inputfilename) != 0) |
153 | if (fclose(f)) { |
170 | if (fclose(f)) { |
154 | perror(inputfilename); |
171 | perror(inputfilename); |
155 | exit(1); |
172 | exit(1); |
156 | } |
173 | } |
157 | |
174 | |
158 | if (psiconv_parse(buf,&file)) |
175 | if (psiconv_parse(buf,&file) || (file->type == psiconv_unknown_file)) |
159 | if(exact) |
176 | { |
160 | exit(1); |
177 | 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); |
178 | exit(1); |
168 | } |
179 | } |
169 | } else |
180 | |
170 | f = stdout; |
181 | /* Set correct output file */ |
|
|
182 | if (strlen(outputfilename) == 0) |
|
|
183 | outputfilename = "/dev/stdout"; |
171 | |
184 | |
172 | strtoupper(type); |
185 | strtoupper(type); |
173 | if (! strcmp(type,"HTML")) |
186 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
174 | psiconv_gen_html(f,file); |
187 | ff = psiconv_list_get(fileformat_list,i); |
175 | else if (! strcmp(type,"HTML4")) |
188 | if (! strcmp(type,ff->name)) { |
176 | psiconv_gen_html4(f,file); |
189 | res = ff->output(outputfilename,file,type); |
177 | else if (! strcmp(type,"ASCII")) |
190 | if (res) { |
178 | psiconv_gen_txt(f,file); |
191 | fprintf(stderr, |
179 | else { |
192 | "Output format `%s' not permitted for this file type\n",type); |
|
|
193 | exit(1); |
|
|
194 | } |
|
|
195 | break; |
|
|
196 | } |
|
|
197 | } |
|
|
198 | |
|
|
199 | if (i == psiconv_list_length(fileformat_list)) { |
180 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
200 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
181 | exit(1); |
201 | exit(1); |
182 | } |
202 | } |
183 | |
203 | |
184 | if (strlen(outputfilename) != 0) |
|
|
185 | if (fclose(f)) { |
|
|
186 | perror(outputfilename); |
|
|
187 | exit(1); |
|
|
188 | } |
|
|
189 | |
|
|
190 | psiconv_free_file(file); |
204 | psiconv_free_file(file); |
191 | |
205 | |
192 | exit(0); |
206 | exit(0); |
193 | } |
207 | } |