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