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

  1. /*
  2.    cmdmain.cpp : This file is part of pstoedit
  3.    main program for command line usage
  4.  
  5.    Copyright (C) 1993,1994,1995,1996 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.  
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <iostream.h>
  26. #include "pstoedit.h"
  27.  
  28. static const char * whichPI(ostream & errstream)
  29. {
  30. // determines which PostScript interpreter to use
  31. #ifdef DEFAULTGS
  32. #define str(x) #x
  33. #define xstr(x) str(x)
  34.     static const char * const defaultgs = xstr(DEFAULTGS);
  35. #else
  36.     static const char * const defaultgs = "";
  37. #endif
  38.     const char * gstocall = getenv("GS");
  39.     if ( gstocall  == 0 ) {
  40.         // env var GS not set, so try default
  41.         if ( strlen(defaultgs) > 0 ) {
  42.             gstocall = defaultgs;
  43.         } else {
  44.             errstream << "Fatal: don't know which interpreter to call. "
  45.                       << "Either setenv GS or compile again with -DDEFAULTGS=..." << endl;
  46.             exit(1);
  47.         }
  48.     }
  49.     return gstocall;
  50. }
  51.  
  52. #if 0
  53. static const char * whichPI(ostream & errstream) 
  54. {
  55.     return "gswin32.exe"  ;
  56. }
  57. #endif
  58.  
  59. #ifdef _WIN32
  60. #include <windows.h>
  61. #include <stdio.h>
  62. static int callgs(const char * commandline)
  63. {
  64.     int gsresult = 0;
  65.  
  66.     STARTUPINFO MyStartupInfo; // Prozessdaten
  67.     memset(&MyStartupInfo,'\0',sizeof(MyStartupInfo));
  68.     MyStartupInfo.cb = sizeof(STARTUPINFO);
  69.     MyStartupInfo.wShowWindow=SW_SHOWMINNOACTIVE;
  70.  
  71.     PROCESS_INFORMATION MyProcessInformation;
  72.          // wird von CreateProcess gefuellt
  73.          DWORD gs_status = 0;
  74.  
  75. //            MessageBox(NULL,commandline,"Kommandozeile",MB_OK); // wogl NULL inserted
  76.  
  77.       int     status=(int)CreateProcess(
  78.               NULL, // Application Name -> wird in der naechsten Zeile mitgegeben
  79.               (LPSTR)commandline,
  80.               NULL, // Prozessattribute (NULL == Default)
  81.               NULL, // Thread-Atrribute (Default)
  82.               FALSE, // InheritHandles
  83.               CREATE_NEW_PROCESS_GROUP, // CreationFlags
  84.               NULL, // Environment (NULL: same as calling-process)
  85.               NULL, // Current Directory (NULL: same as calling process)
  86.               (LPSTARTUPINFO)(&MyStartupInfo), // Windows-state at Startup
  87.                    // das Fenster der aufzurufenden Funktion wird verkleinert + nicht aktiv
  88.                   // dargestellt
  89.              (LPPROCESS_INFORMATION)(&MyProcessInformation)
  90.             );
  91.  
  92.             if(status==TRUE) gsresult=0;
  93.             else gsresult=-1; // Failure
  94.  
  95.          while(0==0) {
  96.              status=GetExitCodeProcess(MyProcessInformation.hProcess, &gs_status);
  97.             if(status==FALSE) {  // Proze▀-Status konnte nicht ermittelt werden
  98.               gsresult=-1;
  99.               break;  }
  100.  
  101.                 if(gs_status==STILL_ACTIVE) { // gswin arbeitet noch...
  102.                         Sleep(500); // Wartezeit in msec
  103.                 }
  104.             else break; // Prozess beendet
  105.             }
  106.     if (gsresult != 0) {
  107.         char tmpstring[100];
  108.         sprintf(tmpstring,"Interpreter failure: %d", gsresult);
  109.         //MessageBox(NULL,tmpstring,"ERROR",MB_OK);
  110.         cerr << tmpstring << endl;
  111.     }
  112.     return gsresult;
  113. }
  114. #else
  115. static int callgs(const char * commandline) { return system(commandline); }
  116. #endif
  117.  
  118. int main(int argc, char **argv) 
  119. {
  120.     // on UNIX like systems, we can use cerr as error stream
  121.     // and the system-function to call GhostScript
  122.     return pstoedit(argc,argv,cerr,callgs,whichPI);
  123. }
  124.