home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / pstoedit.zip / source.zip / pstoedit.2.50 / src / pstoedit.cpp < prev    next >
C/C++ Source or Header  |  1997-01-02  |  23KB  |  789 lines

  1. /*
  2.    pstoedit.cpp : This file is part of pstoedit
  3.    main control procedure
  4.  
  5.    Copyright (C) 1993,1994,1995,1996,1997 Wolfgang Glunz, Wolfgang.Glunz@zfe.siemens.de
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. */
  22. #include <stdio.h>
  23.  
  24. #include <iostream.h>
  25.  
  26. //#if !defined(unix) && !defined(__unix__) && !defined(__unix)
  27. //#include <strstrea.h>
  28. //#else
  29. //#include <strstream.h>
  30. //#endif
  31.  
  32. #include <fstream.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35.  
  36. // #ifdef _MSC_VER
  37. // for getcwd ( at least for Visual C++)
  38.  
  39. #if !defined(unix) && !defined(__unix__) && !defined(__unix)
  40. #include <direct.h>
  41. #endif
  42.  
  43. #include "pstoedit.h"
  44. #include "version.h"
  45. #include "config.h"
  46.  
  47. #ifdef WITHWMF
  48. #include "drvwmf.h"
  49. #endif    
  50.  
  51. #ifdef WITHTGIF
  52. #include "drvtgif.h"
  53. #endif    
  54.  
  55. #ifdef WITHRPL
  56. #include "drvrpl.h"
  57. #endif    
  58.  
  59. #ifdef WITHRIB
  60. #include "drvrib.h"
  61. #endif    
  62.  
  63. #ifdef WITHLWO
  64. #include "drvlwo.h"
  65. #endif    
  66.  
  67. #ifdef WITHMIF
  68. #include "drvmif.h"
  69. #endif    
  70.  
  71. #ifdef WITHFIG
  72. #include "drvfig.h"
  73. #endif    
  74.  
  75. #ifdef WITHGNUPLOT
  76. #include "drvgnplt.h"
  77. #endif    
  78.  
  79. #ifdef WITHDXF
  80. #include "drvdxf.h"
  81. #endif    
  82.  
  83. #if defined (WITHMET) && defined(__OS2__)
  84. #include <io.h>
  85. #include "drvMET.h"
  86. #endif    
  87.  
  88. #ifdef WITHPDF
  89. #include "drvpdf.h"
  90. #endif
  91.  
  92. #ifdef WITHCGM
  93. #include "drvcgm.h"
  94. #endif
  95.  
  96. #ifdef WITHJAVA
  97. #include "drvjava.h"
  98. #endif
  99.  
  100. // next include only for verifying that drvsampl is kept up to date
  101. #ifdef WITHSAMPLE
  102. #include "drvsampl.h"
  103. #endif
  104.  
  105.  
  106. #ifndef USEPROLOGFROMFILE
  107. #include "pstoedit.ph"
  108. #endif
  109.  
  110. void usage(ostream & errstream) 
  111. {
  112.     errstream << "usage: pstoedit [-help] [-dt] [-merge] [-s nn] [-dis] [-nomaptoisolatin1] [-rn1xn2] [-nq] [-flat nn] [-bo] -f format [infile [outfile]]" << endl;
  113. }
  114.  
  115.  
  116. // cannot make this static, because it's used in a template, that might be
  117. // stored in a repository (separate .o file)
  118. int checkNextArgument(int argc, char ** argv, int arg,ostream & errstream) 
  119. {
  120.     // if there is no next arg, usage is printed and
  121.     // the program is terminated
  122.     if ((arg+1>= argc ) || (argv[arg+1] == 0)) {
  123.         errstream << "missing value for option " << argv[arg] << endl;
  124.         usage(errstream);
  125.         exit(1);
  126.     }
  127.     return 1;
  128. }
  129.  
  130.  
  131. // made non template, because some compilers (cfront) have
  132. // problems with instantiating this
  133. // template <class T>
  134. // void getvalue(int argc, char ** argv, int arg, T & value) 
  135. void getvalue(int argc, char ** argv, int arg, float & value,ostream& errstream) 
  136. {
  137.     // get value for option arg (value is next argument)
  138.     if (checkNextArgument(argc,argv,arg,errstream)) {
  139.  
  140. // Work around for Linux libg++ bug (use atof instead of strstream)
  141.         value = (float) atof(argv[arg+1]);
  142. // C++'ish    istrstream strvalue(argv[arg+1]);
  143. //        strvalue >> value;
  144.  
  145.     }
  146. }
  147.  
  148. static const char * full_qualified_tempnam(const char * pref)
  149. {
  150.     const char * filename = tempnam(0,(char *) /* because of borland */ pref);
  151.     // W95: Fkt. tempnam() erzeugt Filename+Pfad
  152.       // W3.1: es wird nur der Name zurueckgegeben
  153.     if ( (strchr(filename,'\\')==0) && 
  154.          (strchr(filename,'/') ==0) ) { // keine Pfadangaben..
  155.         char cwd[400];
  156.           getcwd(cwd,400); //JW akt. Verzeichnis holen
  157.         char * result = new char [strlen(filename) + strlen(cwd) + 2];
  158.         strcpy(result,cwd);
  159.         strcat(result,"/"); 
  160.         strcat(result,filename);
  161.         free((char*) filename);
  162.         return result;
  163.     } else {
  164.         return filename;
  165.     }
  166. }
  167.  
  168. static int grep(const char * const matchstring, const char * const filename,ostream& errstream)
  169. {
  170. // for call: gsresult = grep("% normal end reached by pstoedit.pro",gsout);
  171.     ifstream inFile;
  172.     inFile.open(filename);
  173.     if (inFile.fail() ) {
  174.         errstream << "Could not open file " << filename << " in grep" << endl;
  175.         return 1; // fail
  176.     } else {
  177.         const unsigned int matchlen = strlen(matchstring) ;
  178.         const unsigned int bufferlen = matchlen + 1;
  179.         // allocate buffer for reading begin of lines
  180.         char * buffer = new char[bufferlen];
  181.         while (inFile.get(buffer,bufferlen,'\n'), !inFile.eof() ) {
  182.             // errstream << " read in grep :`" << buffer "'" << inFile.gcount() << " " << matchlen << endl;
  183.             if ((inFile.gcount() == matchlen) && 
  184.                 (strcmp(buffer,matchstring) == 0) ) {
  185.                 delete [] buffer;
  186.                 return 0;
  187.             }
  188.             if ( inFile.peek() == '\n' ) inFile.ignore();
  189.         }
  190.         delete [] buffer;
  191.     }
  192.     return 1; // fail
  193. }
  194.  
  195.  
  196. enum driverType { unknown, tgif, rpl, lwo, rib, mif, fig, cgm, cgmt, pdf, gnuplot, ps, debug, met, wmf, dxf,java, sample };
  197.  
  198. struct driverdescription_T {
  199.     driverdescription_T(const char * s_name, const char * expl, driverType code):
  200.         symbolicname(s_name),
  201.         explanation(expl),
  202.         internalcode(code) {}
  203.     const char * const symbolicname;
  204.     const char * const explanation;
  205.     const driverType internalcode;
  206. };
  207.  
  208. static const driverdescription_T driverdescription [] = {
  209. #ifdef WITHTGIF
  210.     driverdescription_T("tgif","Tgif .obj format (for tgif version >= 3)",tgif),
  211. #endif
  212. #ifdef WITHRPL
  213.     driverdescription_T("rpl","Real3D Programming Language Format",rpl),
  214. #endif
  215. #ifdef WITHLWO
  216.     driverdescription_T("lwo","LightWave 3D Object Format",lwo),
  217. #endif
  218. #ifdef WITHRIB
  219.     driverdescription_T("rib","RenderMan Interface Bytestream",rib),
  220. #endif
  221. #ifdef WITHMIF
  222.     driverdescription_T("mif","(Frame)Maker Intermediate Format",mif),
  223. #endif
  224. #ifdef WITHFIG
  225.     driverdescription_T("fig",".fig format for xfig",fig),
  226. #endif
  227. #ifdef WITHPDF
  228.     driverdescription_T("pdf","Adobe's Portable Document Format",pdf),
  229. #endif
  230. #ifdef WITHGNUPLOT
  231.     driverdescription_T("gnuplot","gnuplot format",gnuplot),
  232. #endif
  233.     driverdescription_T("ps","Flattened PostScript",ps),
  234.     driverdescription_T("debug","for test purposes ",debug),
  235.     driverdescription_T("dump","for test purposes (same as debug)",debug),
  236. #if defined (WITHMET) && defined(__OS2__)
  237.     driverdescription_T("met","OS/2 meta files",met),
  238. #endif
  239. #ifdef WITHWMF
  240.     driverdescription_T("wmf","Windows meta files",wmf),
  241. #endif
  242. #ifdef WITHDXF
  243.     driverdescription_T("dxf","CAD exchange format",dxf),
  244. #endif
  245. #ifdef WITHCGM
  246.     driverdescription_T("cgm","CGM Format (binary)",cgm),
  247.     driverdescription_T("cgmt","CGM Format (textual)",cgmt),
  248. #endif
  249. #ifdef WITHJAVA
  250.     driverdescription_T("java","java applet source code",java),
  251. #endif
  252. #ifdef WITHSAMPLE
  253.     driverdescription_T("sample","insert your new format here",sample)
  254. #endif
  255. };
  256.  
  257. driverType getdrivertype(const char * drivername)
  258. {
  259.     const unsigned int nrOfFormats = sizeof(driverdescription) / sizeof(driverdescription_T);
  260.     for (unsigned int i = 0; i < nrOfFormats; i++ ) {
  261.         if ( strcmp(drivername,driverdescription[i].symbolicname) == 0 ) {
  262.             return  driverdescription[i].internalcode ;
  263.         } 
  264.     }
  265.     return unknown;
  266. }
  267.  
  268.  
  269. static void explainformats(ostream & out)
  270. {
  271.     const unsigned int nrOfFormats = sizeof(driverdescription) / sizeof(driverdescription_T);
  272.     out << "Available formats :\n";
  273.     for (unsigned int i = 0; i < nrOfFormats; i++ ) {
  274.         out << driverdescription[i].symbolicname << ":\t" ;
  275.         if (strlen(driverdescription[i].symbolicname) <7) {
  276.             out << '\t';
  277.         }
  278.         out << driverdescription[i].explanation << endl;
  279.     }
  280. }
  281.  
  282. #if !defined(unix) && !defined(__unix__) && !defined(__unix)
  283. void convertBackSlashes(char* string) {
  284.  
  285.     char* c;
  286.  
  287.     while ((c = strchr(string,'\\')) != NULL)
  288.        *c = '/';
  289. }
  290. #endif
  291.  
  292. static void writeFileName(ostream & outstream, const char * const filename)
  293. {
  294.     const unsigned int len = strlen(filename);
  295.     for (unsigned int i = 0 ; i < len; i++ ) {
  296.         if (filename[i] =='\\') {
  297.             outstream << '/' ; // '/' works on DOS also
  298.         } else {
  299.             outstream << filename[i];
  300.         }
  301.     }
  302. }
  303.  
  304.  
  305. int pstoedit(int argc,char **argv,ostream& errstream, 
  306.         execute_interpreter_function call_PI,
  307.         const char * (*whichPI)(ostream &)
  308.         )
  309. {
  310.     // cannot be const  because it needs to be changed on non UNIX systems (convertBackSlashes)
  311.     char    *nameOfInputFile = 0;
  312.  
  313.     const char    *nameOfOutputFile = 0;
  314.     const char    *resolution_option = "-r72x72"; // default
  315.                               // can be changed via -r
  316.     bool    maptoisolatin1 = true;
  317.     bool    withdisplay = false;
  318.     bool    doquit         = true;
  319.     bool    merge       = false;
  320.     bool    drawtext    = false;
  321.     bool    escapetext  = false;                        // only for PostScript backend
  322.     bool    usepdfmark  = false;                        // only for PDF backend
  323.     float   flatness    = 1.0f;                // used for setflat
  324.                                 // in combination with -dt
  325.     char    *drivername = 0;
  326. #ifdef WITHWMF
  327.    DRVWMFSETUP * pDrvWMFsetup = 0;
  328. #endif
  329.     float   magnification = 1.0f;
  330.  
  331.     bool    backendonly = false;                        // used for easier debugging of backends
  332.     // directly read input file by backend
  333.     // bypass ghostscript. The input file
  334.     // is assumed to be produced by a
  335.     // previous call with -f debug
  336.  
  337.     errstream << "pstoedit: version " << version << " : Copyright (C) 1993,1994,1995,1996,1997 Wolfgang Glunz\n" ;
  338.     int arg = 1;
  339.     while (arg < argc) {
  340.         if ( argv[arg][0] == '-' ) {
  341.             // it is an option
  342.             if (strncmp(argv[arg],"-s",2) == 0) {
  343.                 getvalue(argc,argv,arg,magnification,errstream);
  344.                 arg++;
  345.             } else if (strncmp(argv[arg],"-help",2) == 0) {
  346.                 usage(errstream);
  347.                 errstream << "Default interpreter is " << whichPI(errstream) << endl;
  348.                 explainformats(errstream);
  349.                 return 1;
  350.             } else if (strncmp(argv[arg],"-flat",3) == 0) {
  351.                 getvalue(argc,argv,arg,flatness,errstream);
  352.                 arg++;
  353.             } else if (strncmp(argv[arg],"-bo",3) == 0) {
  354.                 backendonly = true;
  355.             } else if (strncmp(argv[arg],"-merge",6) == 0) {
  356.                 merge = true;
  357.             } else if (strncmp(argv[arg],"-dt",3) == 0) {
  358.                 drawtext = true;
  359.             } else if (strncmp(argv[arg],"-r",2) == 0) {
  360.                 resolution_option = argv[arg];
  361.             } else if (strncmp(argv[arg],"-dis",4) == 0) {
  362.                 withdisplay = true;
  363.             } else if (strncmp(argv[arg],"-nomaptoisolatin1",6) == 0) {
  364.                 maptoisolatin1 = false;
  365.             } else if (strncmp(argv[arg],"-nq",3) == 0) {
  366.                 doquit = false;
  367.             } else if (strcmp(argv[arg],"-f") == 0) {
  368.                 if (checkNextArgument(argc,argv,arg,errstream)) {
  369.                     drivername = argv[arg+1];
  370.                     arg++;
  371.                 }
  372.             } else {
  373.                 errstream << "unknown option " << argv[arg] << endl;
  374.                 usage(errstream);
  375.                 return 1;
  376.             }
  377.         } else {
  378.             if (nameOfInputFile == 0) {
  379.                 nameOfInputFile = argv[arg];
  380.             } else if (nameOfOutputFile == 0) {
  381.                 nameOfOutputFile = argv[arg];
  382.             } else {
  383.                 errstream << "more than two file arguments " << endl;
  384.                 usage(errstream);
  385.                 return 1;
  386.             }
  387.         }
  388.         //     errstream << "argv[" << arg << "] = " << argv[arg] << endl;
  389.         arg++;
  390.     }
  391.     if (drivername == 0) {
  392.         errstream << "No backend specified" << endl;
  393.         usage(errstream);
  394.         return 1;
  395.     } else {
  396.         char * driveroptions = strchr(drivername,':');
  397.         if (driveroptions) {
  398.             *driveroptions = '\0'; // replace : with 0 to separate drivername
  399.             driveroptions++;
  400.         }
  401.         driverType    currentDriver = getdrivertype(drivername);
  402.         if (currentDriver == unknown) {
  403.             errstream << "unsupported driver " << drivername << endl;
  404.             explainformats(errstream);
  405.             return 1;
  406.         } else {
  407.             //istream *inputFilePtr = 0;
  408.             // cannot be const because nameOfInputFile cannot be const
  409.             // because it needs to be changed on non UNIX systems (convertBackSlashes)
  410.             char * const stdinFileName = "%stdin"; // for PostScript
  411.             if (nameOfInputFile) {
  412.                 // an input file was given as argument
  413.  
  414.                 // just test whether InputFile is readable.
  415.                 // The file will be read directly from the PostScrip
  416.                                 // interpreter later on
  417. #if !defined(unix) && !defined(__unix__) && !defined(__unix)
  418.                                 convertBackSlashes(nameOfInputFile);
  419. #endif
  420.                 ifstream inFile(nameOfInputFile);
  421.                 if (!inFile) {
  422.                     errstream << "Could not open file " << nameOfInputFile << " for input" << endl;
  423.                     return 1;
  424.                 }
  425.                 // done by destructor  inFile.close();
  426.             } else {
  427.                 nameOfInputFile = stdinFileName;
  428.             }
  429.             ostream *outputFilePtr = 0;
  430.             ofstream outFile;
  431.             if (nameOfOutputFile) {
  432.                         if (currentDriver != wmf ){ // no need to open file for wmf
  433. #if !defined(unix) && !defined(__unix__) && !defined(__unix)
  434. // binary is not available on UNIX, only on PC
  435.                             if (currentDriver == cgm ||
  436.                         currentDriver == lwo) {
  437.                         outFile.open(nameOfOutputFile,ios::binary);
  438.                     } else 
  439. #endif
  440.                     {
  441.                         outFile.open(nameOfOutputFile);
  442.                     }
  443.                     if (outFile.fail() ) {
  444.                         errstream << "Could not open file " << nameOfOutputFile << " for output" << endl;
  445.                         return 1;
  446.                     }
  447.                     outputFilePtr = &outFile;
  448.                 }
  449.             } else {
  450.                 outputFilePtr = &cout;
  451.             }
  452.  
  453.             drvbase * outputdriver = 0;
  454.             switch (currentDriver) {
  455. #ifdef WITHCGM
  456.             case cgm:
  457.                 outputdriver = new drvCGM(driveroptions,*outputFilePtr,errstream,1);
  458.                 break;
  459.             case cgmt:
  460.                 outputdriver = new drvCGM(driveroptions,*outputFilePtr,errstream,0);
  461.                 break;
  462. #endif
  463. #ifdef WITHDXF
  464.             case dxf:
  465.                 outputdriver = new drvDXF(driveroptions,*outputFilePtr,errstream);
  466.                 break;
  467. #endif 
  468.             case debug:
  469.                 // no driver needed for debug/dump output
  470.                 usepdfmark = true;
  471.                 break;
  472. #ifdef WITHGNUPLOT
  473.             case gnuplot:
  474.                 outputdriver = new drvGNUPLOT(driveroptions,*outputFilePtr,errstream);
  475.                 break;
  476. #endif
  477. #ifdef WITHFIG
  478.             case fig:
  479.                 outputdriver = new drvFIG(driveroptions,*outputFilePtr,errstream);
  480.                 break;
  481. #endif
  482. #ifdef WITHSAMPLE
  483.             case sample:
  484.                 outputdriver = new drvSAMPL(driveroptions,*outputFilePtr,errstream);
  485.                 break;
  486. #endif
  487. #ifdef WITHJAVA
  488.             case java:
  489.                 outputdriver = new drvJAVA(driveroptions,*outputFilePtr,errstream);
  490.                 break;
  491. #endif
  492. #ifdef WITHLWO
  493.             case lwo:
  494.                 outputdriver = new drvLWO(driveroptions,*outputFilePtr,errstream);
  495.                 break;
  496. #endif
  497. #ifdef WITHRIB
  498.             case rib:
  499.                 outputdriver = new drvRIB(driveroptions,*outputFilePtr,errstream);
  500.                 break;
  501. #endif
  502. #ifdef WITHRPL
  503.             case rpl:
  504.                 outputdriver = new drvRPL(driveroptions,*outputFilePtr,errstream);
  505.                 break;
  506. #endif
  507. #ifdef WITHMIF
  508.             case mif:
  509.                 outputdriver = new drvMIF(driveroptions,*outputFilePtr,errstream);
  510.                 break;
  511. #endif
  512. #if defined (WITHMET) && defined(__OS2__)
  513.             case met:
  514.                 // driver specific options are appended to the drivers name
  515.                 // e.g. -f met:wp
  516.                 // The backend is responsible for parsing the additional option string
  517.                 outFile.close();
  518.                 pDrvMETsetup = new DRVMETSETUP(driveroptions);
  519.                 if (pDrvMETsetup->exit) {
  520.                       delete pDrvMETsetup;
  521.                       return 0;
  522.                 }
  523.                 if (!nameOfOutputFile) {
  524.                       nameOfOutputFile = "stdout.met";
  525.                 }
  526.                 pDrvMETsetup->pMetaFileName = new char[strlen(nameOfOutputFile)+1];
  527.                 strcpy(pDrvMETsetup->pMetaFileName,nameOfOutputFile);
  528.                 break;
  529. #endif
  530. // JW
  531. #if defined WITHWMF
  532.             case wmf:     {
  533.                 // driver specific options are appended to the drivers name
  534.                 // e.g. -f wmf:wp
  535.                 // The backend is responsible for parsing the additional option string
  536.                 // errstream << "wmf-driver selected" << endl;
  537. //                outFile.close(); // MetaFileDC oeffnet sein eigenes File
  538.             pDrvWMFsetup = new DRVWMFSETUP ;
  539.  
  540.               memset(pDrvWMFsetup,'\0',sizeof(DRVWMFSETUP));
  541.  
  542.                 if (pDrvWMFsetup->exit) {
  543.                       delete pDrvWMFsetup;
  544.                       return 0;
  545.                 }
  546.  
  547.                 if(driveroptions)
  548.                     strcpy(pDrvWMFsetup->wmf_options, driveroptions);
  549.  
  550.                 if (nameOfOutputFile==0) {
  551.                       nameOfOutputFile="drvWMF.out";
  552.                 }
  553.                 pDrvWMFsetup->pOutFileName = new char[strlen(nameOfOutputFile)+1];
  554.                 strcpy(pDrvWMFsetup->pOutFileName,nameOfOutputFile);
  555.                 pDrvWMFsetup->pInFileName = new char[strlen(nameOfInputFile)+1];
  556.                 strcpy(pDrvWMFsetup->pInFileName,nameOfInputFile);
  557.  
  558.                 errstream << "WMF Driver Options: " << pDrvWMFsetup->wmf_options << endl;
  559.                 for(int i=0;i<strlen(pDrvWMFsetup->wmf_options);i++)  {
  560.                    switch((int)pDrvWMFsetup->wmf_options[i]) {
  561.  
  562.                        case('v'):
  563.                            errstream << "wmf: verbose option selected" << endl;
  564.                             pDrvWMFsetup->info=1;
  565.                             break;
  566.                        case('e'):
  567.                            errstream << "wmf: enhanced meta file format selected" << endl;
  568.                             pDrvWMFsetup->enhanced=1;
  569.                             break;
  570.                         default:
  571.                            errstream << "wmf: unknown option: '" << pDrvWMFsetup->wmf_options[i] <<
  572.                             "'" << endl;
  573.                        break;
  574.  
  575.                     }
  576.                 }
  577.             outputFilePtr=&cout;
  578.                outputdriver = new drvWMF(*outputFilePtr,errstream ,magnification,pDrvWMFsetup->wmf_options ,pDrvWMFsetup);
  579.             }
  580.                 break;
  581. #endif
  582.  
  583. #ifdef WITHPDF
  584.             case pdf:
  585.                 outputdriver = new drvPDF(driveroptions,*outputFilePtr,errstream);
  586.                 usepdfmark = true;
  587.                 break;
  588. #endif
  589.             case ps:
  590.                 escapetext = true;
  591.                 break;
  592. #ifdef WITHTGIF
  593.             case tgif:
  594.                 outputdriver = new drvTGIF(driveroptions,*outputFilePtr,errstream,magnification);
  595.                 break;
  596. #endif
  597.             default:
  598.                 errstream << "unsupported driver " << drivername << endl;
  599.                 explainformats(errstream);
  600.                 return 1;
  601.                 // unreachable break;
  602.             }
  603.  
  604.             const char * gsoutName = 0;
  605.             const char * gsout;
  606.             int gsresult = 0;
  607.             if (backendonly) {
  608.                 gsout = nameOfInputFile;
  609.                 gsresult = 0;                       // gs was skipped, so there is no problem
  610.             } else {
  611.                 const char * gsin = full_qualified_tempnam("psin");
  612.                 ofstream inFileStream(gsin);
  613.                 if (!maptoisolatin1) {
  614.                     inFileStream << "/maptoisolatin1 false def" << endl;
  615.                 }
  616.                 if (drawtext) {
  617.                     inFileStream << "/textastext false def" << endl;
  618.                     inFileStream << "/flatnesstouse " << flatness << " def" << endl;
  619.                 }
  620.                 if (escapetext) {
  621.                     inFileStream << "/escapetext true def" << endl;
  622.                 }
  623.                 if (usepdfmark) {
  624.                     inFileStream << "/usepdfmark true def" << endl;
  625.                 }
  626.     
  627.                 gsout = gsoutName = full_qualified_tempnam("psout");
  628.                 inFileStream << "/outputfilename (" ;
  629.                 writeFileName(inFileStream,gsout);
  630.                 inFileStream <<") def" << endl;
  631.  
  632.                 inFileStream << "/inputfilename  (" ;
  633.                 writeFileName(inFileStream,nameOfInputFile);
  634.                 inFileStream <<") def" << endl;
  635.  
  636.                 if (outputdriver && outputdriver->backendSupportsCurveto ) {
  637.                     inFileStream << "/doflatten false def" << endl;
  638.                 } 
  639.  
  640.                 if (outputdriver && (outputdriver->knownFontNames() != 0) ) {
  641.                     const char * const * fnames = outputdriver->knownFontNames();
  642.                     unsigned int size = 0;
  643.                     while (*fnames) { size++; fnames++; }
  644.                     inFileStream << "/pstoedit.knownFontNames " << size << " dict def" << endl;
  645.  
  646.                     inFileStream << "pstoedit.knownFontNames begin" << endl;
  647.                     fnames = outputdriver->knownFontNames();
  648.                     while (*fnames) {
  649.                         inFileStream << "/" << *fnames << " true def"<< endl;
  650.                         fnames++;
  651.                     }
  652.                     inFileStream << "end" << endl;
  653.                 } 
  654.     
  655. #ifdef     USEPROLOGFROMFILE
  656.                 ifstream prologue("pstoedit.pro");
  657.                 //     errstream << " copying prologue file to " << gsin << endl;
  658.                     copy_file(prologue,inFileStream);
  659. #else
  660.                 const char * * prologueline = PS_prologue;
  661.                 while (prologueline && *prologueline ) {
  662.                     inFileStream << *prologueline << '\n';
  663.                     prologueline++;
  664.                 }
  665. #endif
  666.                 inFileStream.close();
  667.                 // now call ghostscript
  668.                 char commandline[1000];
  669.                 // TODO check for overflow
  670.                 commandline[0]= '\0';
  671.                 const char * gstocall = whichPI(errstream);
  672.                 strcat(commandline,gstocall);
  673.                 strcat(commandline," -q ");
  674.                 // NOBIND disables bind in, e.g, gs_init.ps
  675.                 // these files are loaded before pstoedit.pro
  676.                 // so any already bound call to, e.g., show could
  677.                 // not be intercepted by pstoedit's show
  678.                 strcat(commandline," -dNOBIND ");
  679.                 strcat(commandline,resolution_option);
  680.                 strcat(commandline," ");
  681. #if 0
  682.                 if (usepdfmark) {
  683.                     // for overloading pdfmark, we need write access to systemdict
  684.                 }
  685. #endif
  686.                 strcat(commandline,"-dWRITESYSTEMDICT ");
  687.                 if (withdisplay) {
  688.                     strcat(commandline,"-dNOPAUSE ");
  689.                 } else {
  690.                     strcat(commandline,"-dNODISPLAY ");
  691.                 }
  692.                 strcat(commandline,gsin);
  693.                 if (doquit) {
  694.                     strcat(commandline," -c quit ");
  695.                 }
  696.                 errstream << "now calling the interpreter via: " << commandline << endl;
  697.                 // gsresult = system(commandline);
  698.                 gsresult = call_PI(commandline);
  699.                 errstream << "Interpreter finished. Return status " << gsresult << endl;
  700.                 // ghostscript seems to return always 0, so
  701.                 // check whether the normal end was reached by pstoedit.pro
  702.                 remove(gsin); 
  703.                 free((char*)gsin); 
  704.                 // if really returned !0 don't grep
  705.                 if (!gsresult) gsresult = grep("% normal end reached by pstoedit.pro",gsout,errstream);
  706.             }
  707.             if (gsresult != 0) {
  708.                 errstream << "The interpreter seems to have failed, cannot proceed !" << endl;
  709.                 remove(gsout);
  710.                 free((char*)gsoutName);
  711.                 return 1;
  712.             } else {
  713.                     if ((currentDriver != debug) && (currentDriver != ps )) {
  714.                     extern FILE * yyin;         // used by lexer
  715.                     if ( backendonly && (nameOfInputFile == stdinFileName) ) {
  716.                         yyin = stdin;
  717.                     } else {
  718.                         yyin = fopen(gsout,"r");
  719.                         if (!yyin) {
  720.                             errstream << "Error opening file " << gsout << endl;
  721.                             return 1;
  722.                         }
  723.                     }
  724. //                    errstream << "now postprocessing the interpreter output" << endl;
  725.                     if (currentDriver != met && currentDriver != wmf) {  //JW
  726.                         outputdriver->run(merge);
  727.                         delete outputdriver;
  728.                     }
  729. // JW WITHWMF addiert
  730. #ifdef WITHWMF
  731.             else if (currentDriver == wmf) { // driver is wmf
  732.  
  733.                 errstream << "Processing driver wmf" << endl;
  734.  
  735.                 // Input-Filenamen setzen
  736.                 pDrvWMFsetup->infile = new char[strlen(gsout)+1];
  737.                 strcpy(pDrvWMFsetup->infile, gsout);
  738.  
  739.             outputdriver->run(0); // 0: merging wird nicht unterstⁿtzt
  740.  
  741.           delete [] pDrvWMFsetup->infile;
  742.           delete [] pDrvWMFsetup->pOutFileName;
  743.           delete [] pDrvWMFsetup->pInFileName;
  744.           delete outputdriver;
  745.           delete pDrvWMFsetup;
  746.  
  747.  
  748.             errstream << "wmf-backend finished." << endl;
  749.  
  750. #if 0
  751.                 sprintf(tmpstring,"Output written to: '%s'",nameOfOutputFile);
  752.                 MessageBox(NULL,tmpstring,"Conversion finished", MB_OK);
  753. #endif
  754.  
  755.  
  756. //             remove(gsout);
  757.  
  758. //                return 0;
  759.             }
  760. #endif
  761. #if defined (WITHMET) && defined(__OS2__)
  762.                              else if (currentDriver == wmf) {          // driver is met
  763.                              OS2WIN os2win;
  764.                           os2win.run();
  765.                           delete pDrvMETsetup->pMetaFileName;
  766.                         delete pDrvMETsetup;
  767.                     }
  768. #endif
  769. //                    errstream << "done " << endl;
  770.                     if ( !( backendonly && (nameOfInputFile == stdinFileName) ) ) {
  771.                         fclose(yyin);
  772.                     }
  773.                 } else {
  774.                     // Debug or PostScript driver
  775.                     ifstream gsoutStream(gsout);
  776.                             // errstream << "now copying  " << gsout << " to output " << endl;
  777.                     copy_file(gsoutStream,*outputFilePtr);
  778.                     // errstream << " done \n";
  779.                 }
  780.                 if ( !backendonly ) {
  781.                     remove(gsout);
  782.                 }
  783.             }
  784.             free((char*)gsoutName);
  785.         }
  786.     }
  787.     return 0;
  788. }
  789.