… | |
… | |
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"); |
57 | puts(" -e, --exact Quit on any parse errors or warnings"); |
… | |
… | |
55 | puts(" -T, --type=FILETYPE Output type"); |
61 | puts(" -T, --type=FILETYPE Output type"); |
56 | puts(" -V, --version Display the program version and exit"); |
62 | puts(" -V, --version Display the program version and exit"); |
57 | puts(" -v, --verbose Show progress indicators on stderr"); |
63 | puts(" -v, --verbose Show progress indicators on stderr"); |
58 | puts(""); |
64 | puts(""); |
59 | puts("The following output types are known:"); |
65 | puts("The following output types are known:"); |
60 | puts(" HTML (default)"); |
66 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
61 | puts(" HTML4"); |
67 | ff = psiconv_list_get(fileformat_list,i); |
62 | puts(" ASCII"); |
68 | printf(" %s",ff->name); |
63 | puts(" RTF"); |
69 | for (j = strlen(ff->name); j < 15; j++) |
|
|
70 | putchar(' '); |
|
|
71 | puts(ff->description); |
|
|
72 | } |
64 | } |
73 | } |
65 | |
74 | |
66 | void print_version(void) |
75 | void print_version(void) |
67 | { |
76 | { |
68 | printf("Version %s\n",VERSION); |
77 | printf("Version %s\n",VERSION); |
… | |
… | |
94 | FILE * f; |
103 | FILE * f; |
95 | struct stat fbuf; |
104 | struct stat fbuf; |
96 | |
105 | |
97 | const char *inputfilename = ""; |
106 | const char *inputfilename = ""; |
98 | const char *outputfilename = ""; |
107 | const char *outputfilename = ""; |
99 | char *type = strdup("HTML"); |
108 | char *type = strdup("HTML3"); |
100 | int exact=0; |
109 | int exact=0; |
101 | |
110 | |
102 | int c; |
111 | int c,i,res; |
103 | psiconv_buffer buf; |
112 | psiconv_buffer buf; |
104 | psiconv_file file; |
113 | psiconv_file file; |
|
|
114 | psiconv_fileformat ff; |
|
|
115 | |
|
|
116 | psiconv_verbosity = PSICONV_VERB_WARN; |
|
|
117 | |
|
|
118 | fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
|
|
119 | init_txt(); |
|
|
120 | init_html(); |
|
|
121 | init_html4(); |
|
|
122 | init_rtf(); |
|
|
123 | init_image(); |
105 | |
124 | |
106 | while(1) { |
125 | while(1) { |
107 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
126 | c = getopt_long(argc,argv,short_options, long_options, &option_index); |
108 | if (c == -1) |
127 | if (c == -1) |
109 | break; |
128 | break; |
… | |
… | |
112 | case 'V': print_version(); exit(0); |
131 | case 'V': print_version(); exit(0); |
113 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
132 | case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
114 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
133 | psiconv_verbosity = PSICONV_VERB_PROGRESS; |
115 | break; |
134 | break; |
116 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
135 | case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
117 | case 's': psiconv_verbosity = PSICONV_VERB_SILENT; break; |
136 | case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; |
118 | case 'e': exact = 1; break; |
137 | case 'e': exact = 1; break; |
119 | case 'o': outputfilename = strdup(optarg); break; |
138 | case 'o': outputfilename = strdup(optarg); break; |
120 | case 'T': type = strdup(optarg); break; |
139 | case 'T': type = strdup(optarg); break; |
121 | case '?': case ':': fputs("Try `-h' for more information",stderr); |
140 | case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
122 | exit(1); |
141 | exit(1); |
123 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
142 | default: fprintf(stderr,"Internal error: getopt_long returned character " |
124 | "code 0%o ?? (contact the author)\n", c); |
143 | "code 0%o ?? (contact the author)\n", c); |
125 | exit(1); break; |
144 | exit(1); break; |
126 | } |
145 | } |
… | |
… | |
130 | "Try `-h' for more information\n",stderr); |
149 | "Try `-h' for more information\n",stderr); |
131 | exit(1); |
150 | exit(1); |
132 | } else if (optind == argc-1) |
151 | } else if (optind == argc-1) |
133 | inputfilename = strdup(argv[optind]); |
152 | inputfilename = strdup(argv[optind]); |
134 | |
153 | |
|
|
154 | |
135 | /* Open inputfile for reading */ |
155 | /* Open inputfile for reading */ |
136 | |
156 | |
137 | if (strlen(inputfilename) != 0) { |
157 | if (strlen(inputfilename) != 0) { |
138 | if(stat(inputfilename,&fbuf) < 0) { |
158 | if(stat(inputfilename,&fbuf) < 0) { |
139 | perror(inputfilename); |
159 | perror(inputfilename); |
… | |
… | |
159 | |
179 | |
160 | if (psiconv_parse(buf,&file)) |
180 | if (psiconv_parse(buf,&file)) |
161 | if(exact) |
181 | if(exact) |
162 | exit(1); |
182 | exit(1); |
163 | |
183 | |
164 | /* Open outputfile for writing */ |
184 | /* Set correct output file */ |
165 | if (strlen(outputfilename) != 0) { |
185 | if (strlen(outputfilename) == 0) |
166 | f = fopen(outputfilename,"w"); |
186 | outputfilename = "/dev/stdout"; |
167 | if (! f) { |
|
|
168 | perror(outputfilename); |
|
|
169 | exit(1); |
|
|
170 | } |
|
|
171 | } else |
|
|
172 | f = stdout; |
|
|
173 | |
187 | |
174 | strtoupper(type); |
188 | strtoupper(type); |
175 | if (! strcmp(type,"HTML")) |
189 | for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
176 | psiconv_gen_html(f,file); |
190 | ff = psiconv_list_get(fileformat_list,i); |
177 | else if (! strcmp(type,"HTML4")) |
191 | if (! strcmp(type,ff->name)) { |
178 | psiconv_gen_html4(f,file); |
192 | res = ff->output(outputfilename,file,type); |
179 | else if (! strcmp(type,"ASCII")) |
193 | if (res) { |
180 | psiconv_gen_txt(f,file); |
194 | fprintf(stderr, |
181 | else if (! strcmp(type,"RTF")) |
195 | "Output format `%s' not permitted for this file type\n",type); |
182 | psiconv_gen_rtf(f,file); |
196 | exit(1); |
183 | else if (! strcmp(type,"IMAGE")) |
197 | } |
184 | psiconv_gen_image(f,file); |
198 | break; |
185 | else { |
199 | } |
|
|
200 | } |
|
|
201 | |
|
|
202 | if (i == psiconv_list_length(fileformat_list)) { |
186 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
203 | fprintf(stderr,"Unknown output type: `%s'\n",type); |
187 | exit(1); |
204 | exit(1); |
188 | } |
205 | } |
189 | |
206 | |
190 | if (strlen(outputfilename) != 0) |
|
|
191 | if (fclose(f)) { |
|
|
192 | perror(outputfilename); |
|
|
193 | exit(1); |
|
|
194 | } |
|
|
195 | |
|
|
196 | psiconv_free_file(file); |
207 | psiconv_free_file(file); |
197 | |
208 | |
198 | exit(0); |
209 | exit(0); |
199 | } |
210 | } |