--- psiconv/trunk/program/psiconv/psiconv.c 1999/10/03 21:10:47 2 +++ psiconv/trunk/program/psiconv/psiconv.c 2004/01/26 21:56:49 190 @@ -17,37 +17,62 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +/* + 2002/Apr. Keita KAWABE + * Support for narrow build Asian Psions added. + + Now psiconv understands -u or --UTF8 option. + This option is passed to output_function as the fourth parameter + (thus the typedef of output_function was changed). + + If a called generator (gen_*.c) can handle UTF8, then it handles + the character conversion etc. accordingly. + + * psiconv seemed to want to take the argument of the options + case-insensitively (i.e. LaTeX -> LATEX), but that was not working + under my environment. Fixed it. +*/ + /* 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 "magick-aux.h" +#endif + +#include +#include +#include +#include +#include "psiconv.h" #include "gen.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) { + fileformat ff; + int i,j; + puts("Syntax: psiconv [OPTIONS..] [FILE]"); - puts("Convert the psion Word file FILE to a HTML file"); + puts("Convert the psion file FILE to other formats"); 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(" -e, --encoding Output encoding (default: UTF8)"); 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"); @@ -55,10 +80,23 @@ puts(" -V, --version Display the program version and exit"); puts(" -v, --verbose Show progress indicators on stderr"); puts(""); + puts("The following encodings are currently supported:"); + puts(" UTF8 Variable length Unicode encoding"); + puts(" UCS2 Fixed 16-bit length Unicode encoding"); + puts(" Psion The encoding your Psion uses (as in psiconv.conf)"); + puts(" ASCII 7-bit ASCII (other symbols are substituted by '?')"); + puts(""); puts("The following output types are known:"); - puts(" HTML (default)"); - puts(" HTML4"); - puts(" ASCII"); + 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); + } + puts(""); + puts("When using UTF8 with LaTeX type, the resulting LaTeX source should be converted"); + puts(" to a suitable encoding for your LaTeX installation before being typeset"); } void print_version(void) @@ -84,22 +122,41 @@ {"silent",no_argument,NULL,'s'}, {"outputfile",required_argument,NULL,'o'}, {"type",required_argument,NULL,'T'}, - {"exact",no_argument,NULL,'e'}, + {"encoding",no_argument,NULL,'e'}, {0,0,0,0} }; - const char* short_options = "hVvsdo:eT:"; + const char* short_options = "hVvsdo:e:T:"; int option_index; FILE * f; struct stat fbuf; const char *inputfilename = ""; const char *outputfilename = ""; - char *type = strdup("HTML"); - int exact=0; + char *type = strdup("HTML4"); + encoding encoding_type=ENCODING_UTF8; + psiconv_list outputlist; - int c; + int c,i,res; psiconv_buffer buf; psiconv_file file; + fileformat ff = NULL; + + psiconv_config config = psiconv_config_default(); + psiconv_config_read(NULL,&config); + + if (!(fileformat_list = psiconv_list_new(sizeof(struct fileformat_s)))) { + fputs("Out of memory error",stderr); + exit(1); + } + + init_txt(); + init_image(); + init_html(); +#if 0 + init_html4(); + init_latex(); + init_rtf(); +#endif while(1) { c = getopt_long(argc,argv,short_options, long_options, &option_index); @@ -108,15 +165,28 @@ switch(c) { case 'h': print_help(); exit(0); case 'V': print_version(); exit(0); - case 'v': if (psiconv_verbosity < PSICONV_VERB_PROGRESS) - psiconv_verbosity = PSICONV_VERB_PROGRESS; + case 'v': if (config->verbosity < PSICONV_VERB_PROGRESS) + config->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 'd': config->verbosity = PSICONV_VERB_DEBUG; break; + case 's': config->verbosity = PSICONV_VERB_ERROR; break; case 'o': outputfilename = strdup(optarg); break; case 'T': type = strdup(optarg); break; - case '?': case ':': fputs("Try `-h' for more information",stderr); + case 'e': if(!strcmp(optarg,"UTF8")) + encoding_type = ENCODING_UTF8; + else if (!strcmp(optarg,"UCS2")) + encoding_type = ENCODING_UCS2; + else if (!strcmp(optarg,"ASCII")) + encoding_type = ENCODING_ASCII; + else if (!strcmp(optarg,"Psion")) + encoding_type = ENCODING_PSION; + else { + fputs("Unknown encoding type " + "(try '-h' for more information\n",stderr); + exit(1); + } + break; + 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); @@ -128,7 +198,11 @@ "Try `-h' for more information\n",stderr); exit(1); } else if (optind == argc-1) - inputfilename = strdup(argv[optind]); + if (!(inputfilename = strdup(argv[optind]))) { + fputs("Out of memory error",stderr); + exit(1); + } + /* Open inputfile for reading */ @@ -145,9 +219,14 @@ } else f = stdin; - buf = psiconv_list_new(sizeof(psiconv_u8)); - while (! feof(f)) - psiconv_list_fread(buf,1024,f); + if (!(buf = psiconv_buffer_new())) { + fputs("Out of memory error",stderr); + exit(1); + } + if (psiconv_buffer_fread_all(buf,f)) { + fprintf(stderr,"Failure reading file"); + exit(1); + } if (strlen(inputfilename) != 0) if (fclose(f)) { @@ -155,39 +234,56 @@ exit(1); } - if (psiconv_parse(buf,&file)) - if(exact) - exit(1); + if (psiconv_parse(config,buf,&file) || (file->type == psiconv_unknown_file)) + { + fprintf(stderr,"Parse error\n"); + exit(1); + } + + strtoupper(type); + for (i = 0; i < psiconv_list_length(fileformat_list); i ++) { + ff = psiconv_list_get(fileformat_list,i); + if (! strcasecmp(type,ff->name)) { + break; + } + } + + if (i == psiconv_list_length(fileformat_list)) { + fprintf(stderr,"Unknown output type: `%s'\n",type); + exit(1); + } + + if (!(outputlist = psiconv_list_new(sizeof(psiconv_u8)))) { + fputs("Out of memory error\n",stderr); + exit(1); + } + + res = ff->output(config,outputlist,file,type,encoding_type); + if (res) { + fprintf(stderr, + "Output format `%s' not permitted for this file type\n",type); + exit(1); + } + + psiconv_free_file(file); - /* Open outputfile for writing */ if (strlen(outputfilename) != 0) { f = fopen(outputfilename,"w"); if (! f) { - perror(outputfilename); + perror(inputfilename); exit(1); } } else f = 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 { - fprintf(stderr,"Unknown output type: `%s'\n",type); + psiconv_list_fwrite_all(outputlist,f); + + if (fclose(f)) { + perror(inputfilename); exit(1); } - if (strlen(outputfilename) != 0) - if (fclose(f)) { - perror(outputfilename); - exit(1); - } - - psiconv_free_file(file); + psiconv_list_free(outputlist); exit(0); }