.ad 8
.bm 8
.fm 4
.bt $Copyright (c) 1992-2004 SAP AG-2002$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $SQL$Project Distributed Database System$VPS30C$
.tt 2 $$$
.tt 3 $RaymondR$Driver for the C-Precompiler$2000-12-29$
*************************************************************
.nf

.nf


    ========== licence begin  GPL
    Copyright (c) 1992-2004 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end

.fo


.fo
Module  :
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
Define  :
.CM *-END-* define --------------------------------------
Use     :
.CM *-END-* use -----------------------------------------
Synonym :
.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  :
.sp
.cp 3
Created : 1994-03-15
.sp
.cp 3
Version : 1994-03-15
.sp
.cp 3
Release :      Date : 2000-12-29
Specification:
.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:
.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:
.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.sp
.CM -lll-
Code    :
/*PRETTY*/
#line 65 "vps30c"

#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdlib.h>
#include <ctype.h>
&if $OSSPEC in [ OSF1 ]
#include <alloca.h>
&endif
#include "gip00.h"
#include "vpr100.h"
#include "heo01.h" /* PTS 1108827 */

#define ERR_EXAMINE_DBROOT_OR_FILENAME "can't get dbroot or filename from '%s'"

extern void exit (int);
extern char *getenv (const char*);
extern int p10_optind;

int main (int argc, char **argv)

/*      Driver program for the C precompiler
*/

{
&if $OS in [ WIN32 ]
        static struct tsp4_args_options opt;
        static tsp4_xuser_record userparams;
        char *ccname = PS01_COMPILER" -nologo";
        char *cc = NULL;
	char *qte = "\"";
&else
        tsp4_xuser_record userparams;
        struct tsp4_args_options opt;
        char *ccname = PS01_COMPILER;
        char *cc = PS01_COMPILER;
	char *qte = "";
&endif
        const char *szPrecomName = "pc3"EEXT_IP00;
        const char *cppext = CPPEXT_IP00;
        const char *oext = OEXT_IP00;
        const char *hlprel = "/env/precopt.hlf";
        const char *inclrel = "/incl";
        const char *prext = ".cpc";
        const char *orext = ".pc";
        const char *cext = ".c";
        const char *clext = ".lst";
        const char *pclext = ".pcl";
        const char *pcldef = "sqlerror.pcl";

	static const char *ormode [] = {"ORACLE", "SAPR3", "ANSIORA"};
	const int nor = sizeof (ormode) / sizeof (char*);
        char *dbroot;
        const char *basename;
        char *prpath;
	char *prpathext;
        char *hlppath;
        char *inclpath;
        char *sdkincl;
        char *prfile;
        char *clistfile;
        char *pclistfile;
        char *cfile;
        char *ofile;
        char *s;
        char **prarg;
        int last;
        int stat;
        int len;
        int i, j;

        tsp00_Pathc szDBRoot;
        tsp00_Pathc szProgName;

        int vargc;
        char **vargv;
        char *ProgToCall;
        char szThisVersion[6];
        char *szVersionArg = szThisVersion;
        const char *szSDKPath = "/sdk/";
        const char *szPGMPath = EXEPATH_IP00"/";
        const char *szPGM64Path = EXE64PATH_IP00"/";
        boolean Bit32 = true;
        boolean BitOption = false; /* no -BIT* Option was set */
        boolean hasSDK = true;

        sprintf(szThisVersion, "%d%d%02d", MAJOR_VERSION_NO_SP100, MINOR_VERSION_NO_SP100, CORRECTION_LEVEL_SP100);
        if (argc) {
          /* strip -<version> option from argument */
          /* all other arguments are maped to vargv */
          vargv = (char**) calloc (argc, sizeof (char*));
          for(i=0, vargc=0; i < argc; i++) {
            if (strlen(argv[i]) > 1) {
              if (argv[i][0] == '-') {
                if (argv[i][1] >= '0' && argv[i][1] <= '9') {
                  szVersionArg = &argv[i][1];
                  if (strncmp (szVersionArg, "74", 2))
                    hasSDK = false;
                  continue;
                }
                if (!strcmp(&argv[i][1], "BIT32")) {
                  BitOption = true;
                  Bit32 = true;
                  continue;
                }
                if (!strcmp(&argv[i][1], "BIT64")) {
                  BitOption = true;
                  Bit32 = false;
                  continue;
                }
              }
            }
            vargv[vargc] = argv[i];
            vargc++;
          }
        }
        else {
          vargv = argv;
          vargc = argc;
        }

	{   /* PTS 1108827 */
	  tsp01_RteError          RteError;

      if ( !sqlGetMyModuleFilename (vargv[0], szProgName, sizeof(szProgName), &RteError) )
	  {
        fprintf ( stderr, "vps30c: GetModuleFileName failed!\n");
        fprintf ( stderr, "OS_ERROR  %d: %s\n" , RteError.OsErrCode, RteError.OsErrText);
        fprintf ( stderr, "RTE_ERROR %d: %s\n" , RteError.RteErrCode, RteError.RteErrText);
        exit (1);
	  }
	}
        sqlGetDBrootFromExecPath (szProgName, szDBRoot, TERM_WITHOUT_DELIMITER_EO01);

        if (szDBRoot[0] == '\0' ||
            (ProgToCall = (char *) strrchr (szProgName , PATH_DELIMITER_EO01)) == NULL) {
          fprintf (stderr, ERR_EXAMINE_DBROOT_OR_FILENAME, szProgName);
          exit (1);
        }

        dbroot = szDBRoot;
        prpath = (char*) malloc (strlen(dbroot) +
                                 strlen(szSDKPath) +
                                 strlen(szVersionArg) +
                                 strlen(szPGMPath) +
                                 strlen(szPGM64Path) +
                                 strlen(szPrecomName) + 10 );
        strcpy (prpath, dbroot);
        if (hasSDK) {
	  strcat (prpath, szSDKPath);
	  strcat (prpath, szVersionArg);
	}        
        strcat (prpath, "/");
        if (Bit32)
          strcat (prpath, szPGMPath);
        else
          strcat (prpath, szPGM64Path);
        strcat (prpath, szPrecomName);

        if (!p01file(prpath)) {
          if (BitOption) {
            fprintf (stderr, "Error: program %s not found\n", prpath);
            free (prpath);
            exit (1);
          }
          else {
            strcpy (prpath, dbroot);
            if (hasSDK) {
	      strcat (prpath, szSDKPath);
	      strcat (prpath, szVersionArg);
	    }
            strcat (prpath, "/");
            if (Bit32)
              strcat (prpath, szPGM64Path);
            else
              strcat (prpath, szPGMPath);
            strcat (prpath, szPrecomName);
          }
          if (!p01file(prpath)){
            fprintf (stderr, "Error: program %s not found\n", prpath);
            free (prpath);
            exit (1);
          }
        }
        hlppath = (char*) malloc (strlen (dbroot) + strlen (hlprel) + 3);
	strcpy (hlppath, qte);
        strcat (hlppath, dbroot);
        strcat (hlppath, hlprel);
	strcat (hlppath, qte);
        p10opt (vargc, vargv, prpath, hlppath, &userparams, &opt);
        s = (char*) memchr (opt.variant.C_sp4co_sql_precompiler.opt_modulefn_F, ' ',
                sizeof (tsp00_VFilename));
        len = (s) ? s - (char *)opt.variant.C_sp4co_sql_precompiler.opt_modulefn_F :
                sizeof (tsp00_VFilename);
        prfile = (char*) malloc (len + strlen (prext) + 1);
        memcpy (prfile, opt.variant.C_sp4co_sql_precompiler.opt_modulefn_F, len);
        prfile [len] = '\0';
        strcat (prfile, prext);
        if (!p01file (prfile))
        {
		for (i = 0; i < nor; i++)
		{
			for (j = 0; j <= sizeof (userparams.xu_sqlmode); j++)
				userparams.xu_sqlmode [j] = (char) toupper (
					userparams.xu_sqlmode [j]);
			if (strncmp ((char*)userparams.xu_sqlmode, ormode [i],
				strlen (ormode [i])) == 0)
			{
				prfile [len] = '\0';
				strcat (prfile, orext);
				break;
			}
		}
		if (!p01file (prfile))
		{
			fprintf (stderr, "Error: file %s not found\n",prfile);
			free (prpath);
			free (prfile);
			exit (1);
		}
        }
        prarg = (char**) malloc ((p10_optind + 2) * sizeof (char*));
 	prpathext = (char*) malloc (strlen(prpath)+3);
 	strcpy (prpathext, qte);
 	strcat (prpathext, prpath);
 	strcat (prpathext, qte);
	prarg [0] = prpathext;
        printf ("cpc options: ");
        for (i = 1; i < p10_optind; i++)
        {
                prarg [i] = vargv [i];
                printf ("%s ", vargv [i]);
        }
        printf ("   file: %s\n", prfile);
        prarg [p10_optind] = prfile;
        prarg [p10_optind + 1] = 0;
        basename = p01basen (vargv [p10_optind]);
        len = strlen (basename);
        cfile = (char*) malloc (len + strlen (cext) + 1);
        ofile = (char*) malloc (len + strlen (oext) + 1);
        clistfile = (char*) malloc (len + strlen (clext) + 1);
        pclistfile = (char*) malloc (len + strlen (pclext) + 1);
        strcpy (cfile, basename);
	if (opt.variant.C_sp4co_sql_precompiler.opt_cansi_F == cpc_oc_cplus) {
	  strcat (cfile, cppext);
	}
	else
	  strcat (cfile, cext);
        strcpy (ofile, basename);
        strcat (ofile, oext);
        strcpy (clistfile, basename);
        strcat (clistfile, clext);
        strcpy (pclistfile, basename);
        strcat (pclistfile, pclext);
        p01rmv (cfile);
        p01rmv (ofile);
        p01rmv (clistfile);
        p01rmv (pclistfile);
        p01rmv (pcldef);
        stat = p01exec (prpath, prarg, 0, 0, 0);
        if (stat == 0)
        {
                free (prarg);
                sdkincl = (char*) malloc (strlen(dbroot) +
                                           strlen(szSDKPath)+
                                           strlen(szVersionArg)+
                                           strlen (inclrel) + 7);
		strcpy (sdkincl, qte);
                strcat (sdkincl, dbroot);
		strcat (sdkincl, szSDKPath);
		strcat (sdkincl, szVersionArg);
                strcat (sdkincl, inclrel);
		strcat (sdkincl, qte);

                inclpath = (char*) malloc (strlen(dbroot) +
                                           strlen (inclrel) + 7);
		strcpy (inclpath, qte);
                strcat (inclpath, dbroot);
                strcat (inclpath, inclrel);
		strcat (inclpath, qte);

                prarg = (char**) malloc ((vargc -p10_optind + 11) * sizeof (char*));
                prarg [0] = ccname;
&if $OS = MSDOS
                prarg [1] = "/c";
                prarg [2] = "/AL";
                prarg [3] = "/G2";
		if (hasSDK) {
		  prarg [4] = "/I";
		  prarg [5] = sdkincl;
		  prarg [6] = "/I";
		  prarg [7] = inclpath;
		  last = 7;
		} else {
		  prarg [4] = "/I";
		  prarg [5] = inclpath;
		  last = 5;		  
		}
&else
                prarg [1] = "-c";
&if $OSSPEC in [ OSF1 ]
                {
                  char *sdkarg = alloca(strlen(sdkincl) + 3);
                  char *inclarg = alloca(strlen(inclpath) + 3);
                  strcpy(sdkarg, "-I");
                  strcat(sdkarg, sdkincl);

                  strcpy(inclarg, "-I");
                  strcat(inclarg, inclpath);
		  if (hasSDK) {
		    prarg [2] = sdkarg;
		    prarg [3] = inclarg;
		    last = 3;
		  } else {
		    prarg [2] = inclarg;
		    last = 2;
		  }
                }
&else
                if (hasSDK) {
                  prarg [2] = "-I";
                  prarg [3] = sdkincl;
                  prarg [4] = "-I";
                  prarg [5] = inclpath;
                  last = 5;
                } else {
		  prarg [2] = "-I";
                  prarg [3] = inclpath;
                  last = 3;
		}
&if $MACH = HP9
		prarg [++last] = "-Ae";
		prarg [++last] = "+DA1.0";
		prarg [++last] = "+DS1.0";
&endif
&if $MACH = PA20W
		prarg [++last] = "+DA2.0W";
		prarg [++last] = "+DS2.0";
&endif
&if $MACH = PA11
		prarg [++last] = "+DA1.1";
		prarg [++last] = "+DS2.0";
&endif
&if $MACH = HP_IA64
                prarg [++last] = "+DD64";
&endif
&if $MACH = SUN
		prarg [++last] = "-Xt";     /* Allow ANSI + K&R Style */
		prarg [++last] = "-xCC";    /* Allow C++ comments in C Sources */
#ifdef BIT64
		prarg [++last] = "-xtarget=ultra";
		prarg [++last] = "-xarch=v9";
#endif
&endif
&if $MACH = _IBMR2
#ifdef BIT64
		prarg [++last] = "-q64";     /* Set OBJECT_MODE to 64bit */
#endif
		prarg [++last] = "-qcpluscmt"; /* Allow C++ comments in C Sources */
&endif
&endif
&if $MACH = NMP
#ifdef BIT64
		prarg [++last] = "-Klp64";     /* Set OBJECT_MODE to 64bit */
#endif
&endif
&endif
		for (i = 0; i <= last; i++)
		    printf ("%s ", prarg [i]);
                for (i = p10_optind + 1; i < vargc; i++)
                {
                        prarg [i - p10_optind + last] = vargv [i];
                        printf ("%s ", vargv [i]);
                }
                printf ("%s\n", cfile);
                prarg [vargc - p10_optind + last] = cfile;
                prarg [vargc - p10_optind + last + 1] = 0;
                stat = p01exec (cc, prarg, 0, clistfile, 3);
                if (stat == 0)
                {
                        p01rmv (cfile);
&if $OS = WIN32
                        /* Compiler listing contains at least the filename */
                        /* and <CR><LF>. 12.11.96 R.A. */
                        if (p01size (clistfile) > (strlen (cfile) + 2))
&else
                        if (p01size (clistfile))
&endif
                                p01displ (clistfile);
                        else
                                p01rmv (clistfile);
                        printf ("%s successfully compiled\n%s removed\n",
                                cfile, cfile);
                }
                else
                {
                        p01displ (clistfile);
                        fprintf (stderr, "%s compilation errors\n", cfile);
                        printf ("error messages written to file %s\n"
                                "%s saved\n", clistfile, cfile);
                }
                free (inclpath);
                free (sdkincl);
        }
        else if (stat == 127)
                printf ("%s no compilation with option -c\n%s saved\n",
                        prfile, cfile);
        else
                if (p01file (pcldef))
                        fprintf (stderr, "option error(s) - see %s\n"
                                "get help with option -h\n", pcldef);
                else
                        fprintf (stderr, "%s precompile errors\n"
                                "error messages written to %s\n",
                                prfile, pclistfile);
        free (cfile);
        free (clistfile);
        free (pclistfile);
        free (prpath);
	free (prpathext);
        free (prfile);
        free (prarg);
        if (vargv != argv) free (vargv);
        exit ((stat == 127) ? 0 : stat);
}

.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
