| 1 |
frodo |
2 |
/* |
| 2 |
|
|
psiconv.c - Part of psiconv, a PSION 5 file formats converter |
| 3 |
|
|
Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl> |
| 4 |
|
|
|
| 5 |
|
|
This program is free software; you can redistribute it and/or modify |
| 6 |
|
|
it under the terms of the GNU General Public License as published by |
| 7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 8 |
|
|
(at your option) any later version. |
| 9 |
|
|
|
| 10 |
|
|
This program is distributed in the hope that it will be useful, |
| 11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
|
|
GNU General Public License for more details. |
| 14 |
|
|
|
| 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 |
| 17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 |
|
|
*/ |
| 19 |
|
|
|
| 20 |
|
|
/* Driver program */ |
| 21 |
|
|
|
| 22 |
|
|
#include "config.h" |
| 23 |
frodo |
20 |
#include "compat.h" |
| 24 |
frodo |
2 |
#include <getopt.h> |
| 25 |
|
|
#include <stdio.h> |
| 26 |
|
|
#include <sys/stat.h> |
| 27 |
|
|
#include <stdlib.h> |
| 28 |
frodo |
34 |
#include <string.h> |
| 29 |
|
|
#include <ctype.h> |
| 30 |
frodo |
2 |
|
| 31 |
|
|
#ifdef HAVE_UNISTD_H |
| 32 |
|
|
#include <unistd.h> |
| 33 |
|
|
#endif |
| 34 |
|
|
|
| 35 |
frodo |
34 |
#ifdef IMAGEMAGICK |
| 36 |
|
|
#include <magick/magick.h> |
| 37 |
|
|
#endif |
| 38 |
|
|
|
| 39 |
frodo |
58 |
#include "psiconv/data.h" |
| 40 |
|
|
#include "psiconv/parse.h" |
| 41 |
frodo |
2 |
#include "gen.h" |
| 42 |
frodo |
34 |
#include "psiconv.h" |
| 43 |
frodo |
2 |
|
| 44 |
|
|
static void print_help(void); |
| 45 |
|
|
static void print_version(void); |
| 46 |
|
|
static void strtoupper(char *str); |
| 47 |
|
|
|
| 48 |
|
|
void print_help(void) |
| 49 |
|
|
{ |
| 50 |
frodo |
34 |
psiconv_fileformat ff; |
| 51 |
|
|
int i,j; |
| 52 |
|
|
|
| 53 |
frodo |
2 |
puts("Syntax: psiconv [OPTIONS..] [FILE]"); |
| 54 |
|
|
puts("Convert the psion Word file FILE to a HTML file"); |
| 55 |
|
|
puts("If FILE is not specified, use stdin"); |
| 56 |
|
|
puts(" -d, --debug Show debug information on stderr"); |
| 57 |
|
|
puts(" -e, --exact Quit on any parse errors or warnings"); |
| 58 |
|
|
puts(" -h, --help Display this help and exit"); |
| 59 |
|
|
puts(" -o, --outputfile Output to file instead of stdout"); |
| 60 |
|
|
puts(" -s, --silent Do not even show warnings on stderr"); |
| 61 |
|
|
puts(" -T, --type=FILETYPE Output type"); |
| 62 |
|
|
puts(" -V, --version Display the program version and exit"); |
| 63 |
|
|
puts(" -v, --verbose Show progress indicators on stderr"); |
| 64 |
|
|
puts(""); |
| 65 |
|
|
puts("The following output types are known:"); |
| 66 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
| 67 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
| 68 |
|
|
printf(" %s",ff->name); |
| 69 |
|
|
for (j = strlen(ff->name); j < 15; j++) |
| 70 |
|
|
putchar(' '); |
| 71 |
|
|
puts(ff->description); |
| 72 |
|
|
} |
| 73 |
frodo |
2 |
} |
| 74 |
|
|
|
| 75 |
|
|
void print_version(void) |
| 76 |
|
|
{ |
| 77 |
|
|
printf("Version %s\n",VERSION); |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
void strtoupper(char *str) |
| 81 |
|
|
{ |
| 82 |
|
|
int i; |
| 83 |
|
|
for (i = 0; i < strlen(str); i ++) |
| 84 |
|
|
str[i] = toupper(str[i]); |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
int main(int argc, char *argv[]) |
| 88 |
|
|
{ |
| 89 |
|
|
struct option long_options[] = |
| 90 |
|
|
{ |
| 91 |
|
|
{"help",no_argument,NULL,'h'}, |
| 92 |
|
|
{"version",no_argument,NULL,'V'}, |
| 93 |
|
|
{"verbose",no_argument,NULL,'v'}, |
| 94 |
|
|
{"debug",no_argument,NULL,'d'}, |
| 95 |
|
|
{"silent",no_argument,NULL,'s'}, |
| 96 |
|
|
{"outputfile",required_argument,NULL,'o'}, |
| 97 |
|
|
{"type",required_argument,NULL,'T'}, |
| 98 |
|
|
{"exact",no_argument,NULL,'e'}, |
| 99 |
|
|
{0,0,0,0} |
| 100 |
|
|
}; |
| 101 |
|
|
const char* short_options = "hVvsdo:eT:"; |
| 102 |
|
|
int option_index; |
| 103 |
|
|
FILE * f; |
| 104 |
|
|
struct stat fbuf; |
| 105 |
|
|
|
| 106 |
|
|
const char *inputfilename = ""; |
| 107 |
|
|
const char *outputfilename = ""; |
| 108 |
frodo |
46 |
char *type = strdup("HTML3"); |
| 109 |
frodo |
2 |
int exact=0; |
| 110 |
|
|
|
| 111 |
frodo |
34 |
int c,i,res; |
| 112 |
frodo |
2 |
psiconv_buffer buf; |
| 113 |
|
|
psiconv_file file; |
| 114 |
frodo |
34 |
psiconv_fileformat ff; |
| 115 |
frodo |
2 |
|
| 116 |
frodo |
65 |
psiconv_verbosity = PSICONV_VERB_WARN; |
| 117 |
|
|
|
| 118 |
frodo |
56 |
fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); |
| 119 |
frodo |
34 |
init_txt(); |
| 120 |
|
|
init_html(); |
| 121 |
|
|
init_html4(); |
| 122 |
|
|
init_rtf(); |
| 123 |
|
|
init_image(); |
| 124 |
|
|
|
| 125 |
frodo |
2 |
while(1) { |
| 126 |
|
|
c = getopt_long(argc,argv,short_options, long_options, &option_index); |
| 127 |
|
|
if (c == -1) |
| 128 |
|
|
break; |
| 129 |
|
|
switch(c) { |
| 130 |
|
|
case 'h': print_help(); exit(0); |
| 131 |
|
|
case 'V': print_version(); exit(0); |
| 132 |
|
|
case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) |
| 133 |
|
|
psiconv_verbosity = PSICONV_VERB_PROGRESS; |
| 134 |
|
|
break; |
| 135 |
|
|
case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; |
| 136 |
frodo |
61 |
case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; |
| 137 |
frodo |
2 |
case 'e': exact = 1; break; |
| 138 |
|
|
case 'o': outputfilename = strdup(optarg); break; |
| 139 |
|
|
case 'T': type = strdup(optarg); break; |
| 140 |
frodo |
34 |
case '?': case ':': fputs("Try `-h' for more information\n",stderr); |
| 141 |
frodo |
2 |
exit(1); |
| 142 |
|
|
default: fprintf(stderr,"Internal error: getopt_long returned character " |
| 143 |
|
|
"code 0%o ?? (contact the author)\n", c); |
| 144 |
|
|
exit(1); break; |
| 145 |
|
|
} |
| 146 |
|
|
} |
| 147 |
|
|
if (optind < argc-1) { |
| 148 |
|
|
fputs("I can only convert one file!\n" |
| 149 |
|
|
"Try `-h' for more information\n",stderr); |
| 150 |
|
|
exit(1); |
| 151 |
|
|
} else if (optind == argc-1) |
| 152 |
|
|
inputfilename = strdup(argv[optind]); |
| 153 |
|
|
|
| 154 |
frodo |
65 |
|
| 155 |
frodo |
2 |
/* Open inputfile for reading */ |
| 156 |
|
|
|
| 157 |
|
|
if (strlen(inputfilename) != 0) { |
| 158 |
|
|
if(stat(inputfilename,&fbuf) < 0) { |
| 159 |
|
|
perror(inputfilename); |
| 160 |
|
|
exit(1); |
| 161 |
|
|
} |
| 162 |
|
|
f = fopen(inputfilename,"r"); |
| 163 |
|
|
if (! f) { |
| 164 |
|
|
perror(inputfilename); |
| 165 |
|
|
exit(1); |
| 166 |
|
|
} |
| 167 |
|
|
} else |
| 168 |
|
|
f = stdin; |
| 169 |
|
|
|
| 170 |
|
|
buf = psiconv_list_new(sizeof(psiconv_u8)); |
| 171 |
|
|
while (! feof(f)) |
| 172 |
|
|
psiconv_list_fread(buf,1024,f); |
| 173 |
|
|
|
| 174 |
|
|
if (strlen(inputfilename) != 0) |
| 175 |
|
|
if (fclose(f)) { |
| 176 |
|
|
perror(inputfilename); |
| 177 |
|
|
exit(1); |
| 178 |
|
|
} |
| 179 |
|
|
|
| 180 |
|
|
if (psiconv_parse(buf,&file)) |
| 181 |
|
|
if(exact) |
| 182 |
|
|
exit(1); |
| 183 |
|
|
|
| 184 |
frodo |
53 |
/* Set correct output file */ |
| 185 |
|
|
if (strlen(outputfilename) == 0) |
| 186 |
|
|
outputfilename = "/dev/stdout"; |
| 187 |
frodo |
2 |
|
| 188 |
|
|
strtoupper(type); |
| 189 |
frodo |
34 |
for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { |
| 190 |
|
|
ff = psiconv_list_get(fileformat_list,i); |
| 191 |
|
|
if (! strcmp(type,ff->name)) { |
| 192 |
frodo |
53 |
res = ff->output(outputfilename,file,type); |
| 193 |
frodo |
34 |
if (res) { |
| 194 |
|
|
fprintf(stderr, |
| 195 |
|
|
"Output format `%s' not permitted for this file type\n",type); |
| 196 |
|
|
exit(1); |
| 197 |
|
|
} |
| 198 |
|
|
break; |
| 199 |
|
|
} |
| 200 |
|
|
} |
| 201 |
|
|
|
| 202 |
|
|
if (i == psiconv_list_length(fileformat_list)) { |
| 203 |
frodo |
2 |
fprintf(stderr,"Unknown output type: `%s'\n",type); |
| 204 |
|
|
exit(1); |
| 205 |
|
|
} |
| 206 |
|
|
|
| 207 |
|
|
psiconv_free_file(file); |
| 208 |
|
|
|
| 209 |
|
|
exit(0); |
| 210 |
|
|
} |