--- psiconv/trunk/program/psiconv/psiconv.c 1999/10/13 19:15:59 17 +++ psiconv/trunk/program/psiconv/psiconv.c 2000/12/15 17:17:45 66 @@ -20,34 +20,40 @@ /* Driver program */ #include "config.h" +#include "compat.h" #include #include #include #include +#include +#include #ifdef HAVE_UNISTD_H #include #endif -#include -#include -#include "data.h" -#include "parse.h" +#ifdef IMAGEMAGICK +#include +#endif + +#include "psiconv/data.h" +#include "psiconv/parse.h" #include "gen.h" +#include "psiconv.h" static void print_help(void); static void print_version(void); static void strtoupper(char *str); -int psiconv_verbosity = PSICONV_VERB_WARN; - void print_help(void) { + psiconv_fileformat ff; + int i,j; + puts("Syntax: psiconv [OPTIONS..] [FILE]"); puts("Convert the psion Word file FILE to a HTML file"); puts("If FILE is not specified, use stdin"); puts(" -d, --debug Show debug information on stderr"); - puts(" -e, --exact Quit on any parse errors or warnings"); puts(" -h, --help Display this help and exit"); puts(" -o, --outputfile Output to file instead of stdout"); puts(" -s, --silent Do not even show warnings on stderr"); @@ -56,10 +62,13 @@ puts(" -v, --verbose Show progress indicators on stderr"); puts(""); puts("The following output types are known:"); - puts(" HTML (default)"); - puts(" HTML4"); - puts(" ASCII"); - puts(" RTF"); + for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { + ff = psiconv_list_get(fileformat_list,i); + printf(" %s",ff->name); + for (j = strlen(ff->name); j < 15; j++) + putchar(' '); + puts(ff->description); + } } void print_version(void) @@ -85,7 +94,6 @@ {"silent",no_argument,NULL,'s'}, {"outputfile",required_argument,NULL,'o'}, {"type",required_argument,NULL,'T'}, - {"exact",no_argument,NULL,'e'}, {0,0,0,0} }; const char* short_options = "hVvsdo:eT:"; @@ -95,12 +103,21 @@ const char *inputfilename = ""; const char *outputfilename = ""; - char *type = strdup("HTML"); - int exact=0; + char *type = strdup("HTML3"); - int c; + int c,i,res; psiconv_buffer buf; psiconv_file file; + psiconv_fileformat ff; + + psiconv_verbosity = PSICONV_VERB_WARN; + + fileformat_list = psiconv_list_new(sizeof(struct psiconv_fileformat_s)); + init_txt(); + init_html(); + init_html4(); + init_rtf(); + init_image(); while(1) { c = getopt_long(argc,argv,short_options, long_options, &option_index); @@ -113,11 +130,10 @@ psiconv_verbosity = PSICONV_VERB_PROGRESS; break; case 'd': psiconv_verbosity = PSICONV_VERB_DEBUG; break; - case 's': psiconv_verbosity = PSICONV_VERB_SILENT; break; - case 'e': exact = 1; break; + case 's': psiconv_verbosity = PSICONV_VERB_FATAL; break; case 'o': outputfilename = strdup(optarg); break; case 'T': type = strdup(optarg); break; - case '?': case ':': fputs("Try `-h' for more information",stderr); + case '?': case ':': fputs("Try `-h' for more information\n",stderr); exit(1); default: fprintf(stderr,"Internal error: getopt_long returned character " "code 0%o ?? (contact the author)\n", c); @@ -131,6 +147,7 @@ } else if (optind == argc-1) inputfilename = strdup(argv[optind]); + /* Open inputfile for reading */ if (strlen(inputfilename) != 0) { @@ -156,42 +173,35 @@ exit(1); } - if (psiconv_parse(buf,&file)) - if(exact) - exit(1); - - /* Open outputfile for writing */ - if (strlen(outputfilename) != 0) { - f = fopen(outputfilename,"w"); - if (! f) { - perror(outputfilename); - exit(1); - } - } else - f = stdout; + if (psiconv_parse(buf,&file) || (file->type == psiconv_unknown_file)) + { + fprintf(stderr,"Parse error\n"); + exit(1); + } + + /* Set correct output file */ + if (strlen(outputfilename) == 0) + outputfilename = "/dev/stdout"; strtoupper(type); - if (! strcmp(type,"HTML")) - psiconv_gen_html(f,file); - else if (! strcmp(type,"HTML4")) - psiconv_gen_html4(f,file); - else if (! strcmp(type,"ASCII")) - psiconv_gen_txt(f,file); - else if (! strcmp(type,"RTF")) - psiconv_gen_rtf(f,file); - else if (! strcmp(type,"IMAGE")) - psiconv_gen_image(f,file); - else { + for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { + ff = psiconv_list_get(fileformat_list,i); + if (! strcmp(type,ff->name)) { + res = ff->output(outputfilename,file,type); + if (res) { + fprintf(stderr, + "Output format `%s' not permitted for this file type\n",type); + exit(1); + } + break; + } + } + + if (i == psiconv_list_length(fileformat_list)) { fprintf(stderr,"Unknown output type: `%s'\n",type); exit(1); } - if (strlen(outputfilename) != 0) - if (fclose(f)) { - perror(outputfilename); - exit(1); - } - psiconv_free_file(file); exit(0);