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