/*
 *   module: depend.c
 *   System dependencies of the PASCAL-XSC managers
 *
{*  last modification:	02.04.92  16:37   }

 * uli 02.08.91 : VAX_VMS adaptions
 * D. Roethig 09.08.91 : i860 and HELIOS adaptions
 * uli 05.09.91 : filename and on_debug added.
 * uli 06.09.91 : libinit and p88_lib and cfg_name added.
 * uli 12.09.91 : ZORTECH added.
 * uli 13.09.91 : libinit: binpath added.
 * uli 22.09.91 : ATARI modifications: pre.lnk and post.lnk prefixed by sysdir.
 * uli 31.01.92 : PXSCBIN defined. 
 * uli 10.02.92 : C compiler options for Convex. 
 * uli 30.03.92 : p88_lib checked before concat.
 * uli 30.03.92 : on_fastfor defined here. 
 * dima 2.04.92 : rts.lib for MS_C modified, append_stub added.
 *dima 09.04.92 : BORLAND_C added, cc made patchable.
 * PJ  06.04.95 : casts for ANSI compatibility added.
                  correct assignment of C++-compiler command for SUN SOLARIS
                  added.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define global extern

#include "defs.h"
#include "sysutil.h"
#include "depend.h"


#define TEST_RUN false    /* display debug information */

#define PXSCBIN true      /* if true then pxsc executables will be called */
                          /*              via sysdir/../bin               */
                          /* if false then pxsc executables will be called */
                          /*               without path name               */

/*****  variable definition *********/

char * filename = NULL;
char * p88_lib = NULL;  /* library specification defined by user or P88LIB
			      or sysdir||post_l_opt */
char * binpath = NULL ; /* directory of pxsc executables */
boolean on_debug = TEST_RUN ;       /* controls test output */
boolean verbose = true ;       /* program reports what it's doing */
#if VAX_VMS_C /* formerly DOSsys */
boolean ok_retc    = false ;
#else
boolean ok_retc    = true ;     /* is true, if call of system(p) returns */
				/* the exit value of called program 'p'  */
#endif

				/* string to clear screen */
#if ATARI_TURBO_C
char * clearstr = "\x01b" "E" ;
#else
char * clearstr = "\x1b[2J\x1b[;H" ;  /* ANSI code */
/* char * clearstr = "\n\n***** CLEAR SCREEN ******\n\n" ; */
#endif

				/* system command to clear screen */
#if UNIXsys
char * clearcmd = "clear" ;     /*  for Unix */
#elif DOSsys - ATARI_TURBO_C
char * clearcmd = "cls" ;       /*  for DOS */
#else
char * clearcmd = NULL ;        /* not available ==> use clearstr */
#endif


#if IBM_AT_HIGH_C
char * p88_name   = "run386 \\pxsc\\bin\\pxsc";
char * cfg_name   = "run386 \\pxsc\\bin\\pxsccfg";
#elif i860_HIGH_C
char * p88_name   = "run860 /usr/davidoff/pxsc/bin/pxsc";
char * cfg_name   = "run860 /usr/davidoff/pxsc/bin/pxsccfg";
#else
char * p88_name   = "pxsc    "; /* pxsc : executable file with path */
char * cfg_name   = "pxsccfg "; /* executable file with path */
#endif

#if UNIXsys
#if IBM_PS2_C + IBM_RT_C
char * ed_name    = "e" ;        /* editor : INed for AIX */
#elif T800_HELIOS_C + i860_HIGH_C
char * ed_name    = "emacs";    /* emacs editor for Helios and the i860 */
#else
char * ed_name    = "vi";       /* editor : visual editor for UNIX */
#endif

char * setenvcmd  = "setenv" ;  /* system command to set an environment var. */
char * setenvdelim= " " ;       /* delimiter for envir.variable and envir.value */
char * profile    = " $HOME/.login  and login again" ;
				/* name of the login profile or autoexec.bat */
		/* end of UNIX */
#elif VAX_VMS_C
char * ed_name    = "edit/tpu " ;
char * setenvcmd  = "" ;     /* system command to set an environment var. */
char * setenvdelim= ":==" ;       /* delimiter for envir.variable and envir.value */
char * profile    = "login.com  and login again" ;
				/* name of the login profile or autoexec.bat */
#else
/*   char * ed_name    = "ME";  /* editor : multi edit */
char * ed_name    = NULL ;      /* indicates: use envir. var. P88EDIT */
char * setenvcmd  = "set" ;     /* system command to set an environment var. */
char * setenvdelim= "=" ;       /* delimiter for envir.variable and envir.value */
char * profile    = "autoexec.bat  and reboot" ;
				/* name of the login profile or autoexec.bat */
#endif

#if VAX_VMS_C
char * r_command  = "mcr ";     /* system command for running a program */
#elif i860_HIGH_C
char * r_command  = "run860 ";  /* command for running a program on the i860 */
#elif IBM_AT_HIGH_C
char * r_command  = "run386 ";  /* system command for running a program */
#else
char * r_command  = NULL;       /* select this if not required */
#endif

/*----------------------------------------------------------------------------*/
#if IBM_AT_WATCOM_C
char * c_command  = "wcl      "; /* system command for C-compiler */
char * pre_c_opt  = "-c -ml                   "; /* prefix compiler options */
char * inc_c_opt = "/i=";       /* Here the compiler must look for include files */
				/* P88SYS or sysdir will be added*/
char * post_c_opt = " ";        /* postfix compiler options */

char * l_command  = "wlink" ;   /* system command for Linker */
char * exe_l_opt  = 0;          /* select this, if not required */
char * pre_l_opt  = "";         /* prefix Linker options */
char * post_l_opt = "stack=12000"; /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif IBM_AT_MS_C
char * c_command  = "cl        "; /* system command for C-compiler */
char * pre_c_opt  = "/c /AL                 ";    /* prefix compiler options */
char * inc_c_opt = "/I";         /* Here the compiler must look for include files */
				 /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";         /* postfix compiler options */

char * l_command  = "link" ;     /* system command for Linker */
char * exe_l_opt  = 0;           /* select this, if not required */
char * pre_l_opt  = "";          /* prefix Linker options */
char * post_l_opt = "/st:12000 /se:512\n";
				/* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif ZORTECH_C
char * c_command  = "ztc       "; /* system command for C-compiler */
char * pre_c_opt  = "-c -mx -Jm -Ju -bx -w                ";
  				  /* prefix compiler options */
char * inc_c_opt = "-I";         /* Here the compiler must look for include files */
				 /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";         /* postfix compiler options */

char * l_command  = "blinkx" ;     /* system command for Linker */
char * exe_l_opt  = 0;           /* select this, if not required */
char * pre_l_opt  = "";          /* prefix Linker options */
char * post_l_opt = "rts_zor.lib\n";  /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif GNU_386_C
char * c_command  = "gcc";      /* system command for C-compiler */
char * pre_c_opt  = "-c -w                      "; /* prefix compiler options */
char * inc_c_opt = "-I" ;       /* Here the compiler must look for include files */
				 /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";         /* postfix compiler options */

char * l_command  = "gcc" ;     /* system command for Linker */
char * exe_l_opt  = NULL ;   /* Linker option: output file */
char * pre_l_opt  = "";         /* prefix Linker options */
char * post_l_opt = "rts.a -lm ";  /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif ATARI_TURBO_C
char * c_command  = "cc" ;    /* alias required !
		    "e:\\turbo_c.2\\tcc.ttp";
				/* system command for C-compiler */
char * pre_c_opt  = "-A -T -K -J -R -S -V -Z -P -Wpro                    " ;
			 /* "-Ie:\\turbo_c.2\\include" */
				/* prefix compiler options */
char * inc_c_opt = 0/*"-I"*/;   /* Here the compiler must look for include files */
				/* P88SYS or sysdir will be added*/
char * post_c_opt = " ";        /* postfix compiler options */

char * l_command  = "link" ;    /* alias required !
		    "e:\\turbo_c.2\\tlink.ttp" ;
				/* system command for Linker */
char * exe_l_opt  = "-O=";      /* Linker option: output file */
char * pre_l_opt  = "pre.lnk " /* -C=" LINKinfo " " */ ;
				/* prefix Linker options */
char * post_l_opt = "post.lnk";
				/* postfix Linker options */
char * indir_opt = "-C=";       /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif IBM_AT_TURBO_C
char * c_command  = "tcc    ";  /* system command for C-compiler */
char * pre_c_opt  = "-c -ml               ";   /* prefix compiler options */
char * inc_c_opt = "-I";        /* Here the compiler must look for include files */
				/* P88SYS or sysdir will be added*/
char * post_c_opt = " ";        /* postfix compiler options */

char * l_command  = "tlink" ;   /* system command for Linker */
char * exe_l_opt  = 0;          /* select this, if not required */
char * pre_l_opt  = "";         /* prefix Linker options */
char * post_l_opt = "";         /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */
 /*----------------------------------------------------------------------------*/
#elif IBM_AT_BORLAND_C
char * c_command  = "bcc  ";      /* system command for C-compiler */
char * pre_c_opt  = "-c -ml -X -H -K                    ";   /* prefix compiler options */
char * inc_c_opt = "-I";        /* Here the compiler must look for include files */
                              /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";        /* postfix compiler options */
char * l_command  = "tlink" ;   /* system command for Linker */
char * exe_l_opt  = 0;          /* select this, if not required */
char * pre_l_opt  = "";         /* prefix Linker options */
char * post_l_opt = "";         /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */

/*----------------------------------------------------------------------------*/
#elif IBM_AT_HIGH_C
char * c_command  = "hc386";    /* system command for C-compiler */
char * pre_c_opt  = "-c                        ";  /* prefix compiler options */
char * inc_c_opt  = "-I";       /* Here the compiler must look for include files */
				/* P88SYS or sysdir will be added*/
char * post_c_opt = " ";        /* postfix compiler options */

char * l_command  = "386link";  /* system command for Linker */
char * exe_l_opt  = "-e";       /* select this, if not required */
char * pre_l_opt  = "-nomap";   /* prefix Linker options */
char * post_l_opt = "";         /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */


/*----------------------------------------------------------------------------*/
#elif VAX_VMS_C
char * c_command  = "cc     "; /* system command for C-compiler */
char * pre_c_opt  = "                         ";  /* prefix compiler options */
char * inc_c_opt = 0;          /* Here the compiler must look for include files */
			       /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";       /* postfix compiler options */

char * l_command  = "link" ;   /* system command for Linker */

char * pre_l_opt  = " ";       /* prefix Linker options */
char * post_l_opt = "rts/lib";
			       /* postfix Linker options */
char * indir_opt = "/opt";     /* Linker option for indirection file */
char * exe_l_opt  = "/e=";     /* executable file name */
/*----------------------------------------------------------------------------*/
#elif T800_HELIOS_C
char * c_command  = "c        "; /* system command for C-compiler */
char * pre_c_opt  = "-c -z-ec                 ";  /* prefix compiler options */
char * inc_c_opt = "-I" ;        /* Here the compiler must look for include files */
				 /* P88SYS or sysdir will be added*/
char * post_c_opt = "";          /* postfix compiler options */
char * l_command  = "asm" ;      /* system command for Linker */
char * exe_l_opt  = "-o ";       /* Linker option: output file */
char * pre_l_opt  = "-f /helios/lib/cstart.o /helios/lib/fpclib.def \
   /helios/lib/fplib.def -h500000 -s100000";    /* prefix Linker options */
char * post_l_opt = "-l/helios/usr/roethig/pxsc/lib/comm.lib \
  -l/helios/usr/roethig/pxsc/lib/rts.lib";    /* postfix Linker options */
char * indir_opt = "";           /* Linker option for indirection file */
/*----------------------------------------------------------------------------*/
#elif i860_HIGH_C
char * c_command  = "hc860   "; /* system command for C-compiler */
char * pre_c_opt  = "-c                   ";       /* prefix compiler options */
char * inc_c_opt  = "-I";       /* Here the compiler must look for include files */
				      /* P88SYS or sysdir will be added*/
char * post_c_opt = "";         /* postfix compiler options */

char * l_command  = "hc860 ";   /* system command for Linker */
char * exe_l_opt  = "-o ";      /* select this, if not required */
char * pre_l_opt  = "-g ";      /* prefix Linker options */
char * post_l_opt = "/usr/davidoff/pxsc/lib/rts.a";         /* postfix Linker options */
char * indir_opt = "@";         /* Linker option for indirection file */


/*----------------------------------------------------------------------------*/
#else
			       /* This is for UNIX */

/* PJ 06.04.95  #if SUN_SOLRARIS_C added */

#if SUN_SOLARIS_C
char * c_command  = "CC";      /* system command for C++-compiler */
#else
char * c_command  = "cc";      /* system command for C-compiler */
#endif

#if CONVEX2_UNIX_C
char * pre_c_opt  = "-c -fi -no"; /* prefix compiler options */
                               /* IEEE arith and no optimize */
#else
char * pre_c_opt  = "-c                         "; /* prefix compiler options */
#endif
char * inc_c_opt = 0;          /* Here the compiler must look for include files */
			       /* P88SYS or sysdir will be added*/
char * post_c_opt = " ";       /* postfix compiler options */

/* PJ 06.04.95  #if SUN_SOLARIS_C added */

#if SUN_SOLARIS_C
char * l_command  = "CC";
#else
char * l_command  = "cc" ;     /* system command for Linker */
#endif

#if UNIXsys
char * exe_l_opt  = "-o ";     /* Linker option: output file */
#elif DOSsys
char * exe_l_opt  = 0;         /* select this, if not required */
#endif
#if CONVEX2_UNIX_C
char * pre_l_opt  = " -fi ";        /* prefix Linker options */
#else
char * pre_l_opt  = "";        /* prefix Linker options */
#endif
char * post_l_opt = "rts.a -lm";
			       /* postfix Linker options */
#endif

#if 0 /* ATARI_TURBO_C */
char * e88_name   = "e:\\p88\\exsc.ttp";     /* short Listing Generator */
char * lister     = "e:\\p88\\psclist.ttp";  /* long */
char * l2p_command= "e:\\p88\\l2p.ttp";      /* listing to pascal */
#elif i860_HIGH_C
char * e88_name   = "run860 /usr/davidoff/pxsc/bin/exsc";
char * lister     = "run860 /usr/davidoff/pxsc/bin/psclist";
char * l2p_command= "run860 /usr/davidoff/pxsc/bin/l2p";
#else
char * e88_name   = "exsc";      /* system command for short Listing Generator */
char * lister     = "psclist";   /* system command for long */
char * l2p_command= "l2p";      /* system command for listing to pascal */
#endif

char * pr_command = "print";     /* system command for print */

#if UNIXsys
#if T800_HELIOS_C
char * sh_command = "shell" ;    /* system command for system entry */
char * sysreturn  = "exit";      /* command to return from shell, */
				 /* informal string for user help */
#else
char * sh_command = "sh" ;       /* system command for system entry */
char * sysreturn  = "Ctrl-D";    /* command to return from shell, */
				 /* informal string for user help */
#endif
char * t_command  = "more";      /* system command for Type file */
#elif DOSsys
char * sysreturn  = "exit";      /* command to return from shell, */
				 /* informal string for user help */
#if ATARI_TURBO_C
char * sh_command = "sh" ;    /* alias required ! */
char * t_command  = "l2p";       /* system command for Type file */
#else
char * sh_command = "command" ; /* system command for system entry */
char * t_command  = "type";      /* system command for Type file */
#endif   /* not ATARI */
#else    /* not UNIX nor DOS */
char * t_command  = "type";      /* system command for Type file */
#endif
	   /******* end of system commands *******/


char    batname[16] = "lxsc.bat";  /* "   "  "       */
#if INDIRECT
char   loptname[16] = "lxsc.opt";  /* "   "  "       */
#endif


#ifdef OLD
				 /* possible extensions for filename */
char * srcfext    = ".p";
char * codfext    = ".c";
char * dclfext    = ".h";
char * modfext    = ".mod";
char * intfext    = ".mod";
char * lstfext    = ".lst";
#if UNIXsys
char * libfext    = ".a";
char * objfext    = ".o";
char * exefext    = "";
#else
#if DOSsys
char * libfext    = ".lib";
char * objfext    = ".obj";
#if IBM_AT_HIGH_C
char * exefext    = ".exp";
#else
char * exefext    = ".exe";
#endif
#endif
#endif
#endif

char * noofext    = ".0";       /* indicates: do not link this object */
char * lnkfext    = ".lnk";
char * savfext    = ".sav";     /* backup of source file */



							/*-----------------*/
#ifdef LINT_ARGS
global char* exefnam (void)                             /*  e x e f n a m  */
#else
global char* exefnam ()
  /* returns the filename of executable program */
#endif
BEGIN (exefnam)
  static char  exename [FILENAME_MAX] ;

  if (on_srcdir and (strcmp (filename, prognam()) != 0))
    exename [0] = '\0' ;
  else                /* add pathname of current directory */
#if UNIXsys + DOSsys
    begin
      exename [0] = '.' ;
      exename [1] = pathch() ;
      exename [2] = '\0' ;
    end
#elif VAX_VMS_C
    strcpy( exename, "[]") ;
#else
    exename [0] = '\0' ;
#endif

  if (on_srcdir)
    strcat (exename, filename);   /* executable to source-directory*/
  else
    strcat (exename, prognam());  /* executable to current dir. */
  return (strcat(exename,exeext()));

END  /* exefnam */                                      /*  e x e f n a m  */
							/*-----------------*/


#if OWN_SYSTEM
#include <process.h>
#ifdef LINT_ARGS
extern int msc_system (const char * s)
#else
extern int msc_system (s)
  const char * s ;
#endif
{ char * par [10] ;
  int i = 0 ;
  char * p = s;

  while (*p == ' ')  p++ ;      /* skip leading blanks */
  while (*p != '\0') {
    par[i++] = p ;
    while (*p != ' ' and *p != '\0')  p++ ;      /* search next blank */
    if  (*p == ' ')  {
      *p = '\0' ;             /* store null char */
      p++ ;
      while (*p == ' ')  p++ ;      /* skip leading blanks */
    } ;
  }
  par [i] = NULL ;

  return  spawnvp (P_WAIT, par[0],par ) ;
  }
#endif


#ifdef LINT_ARGS
							      /*-----------*/
extern int exist (char * name, char * ext)                    /* e x i s t */
#else
extern int exist (name, ext)
  char * name;
  char * ext;
#endif
{
  char fn[256];
  FILE * f ;

  strcpy (fn, name);
  strcat (fn, ext);

  f = fopen (fn, "r") ;
  if (f) {
    fclose (f) ;
    return (1);
   }
  else
    return (0);
}                                                             /* e x i s t */
							      /*-----------*/


#ifdef LINT_ARGS
						/*-----------------*/
extern void  libinit (void)                     /*  l i b i n i t  */
#else
extern void  libinit ()
  /* initializes the extern variables binpath and p88_lib */
#endif
{ char * p, * sys ;

  sys = getenv (P88SYS);
  if  (not sys)   sys = sysdir ;

#if PXSCBIN
  binpath = (char*) malloc (strlen (sys)+1) ;
  strcpy (binpath, sys) ;
  memcpy (binpath + strlen (binpath) - 4 , "bin", 3) ;
#endif

  p = getenv (P88LIB);
  if  (p)   {
     /* PJ 06.04.95  cast to char* inserted */
     p88_lib = ( char * ) malloc (strlen (p)+1) ;
     strcpy (p88_lib, p) ;
  } else {
#if ATARI_TURBO_C
     p88_lib = malloc (strlen(indir_opt) + strlen(sys) + strlen(post_l_opt)+1) ;
     strcpy (p88_lib, indir_opt) ;
     strcat (p88_lib, sys) ;
     strcat (p88_lib, post_l_opt ) ;
#elif IBM_AT_MS_C
     p88_lib = NULL ;
#else
     /* PJ 06.04.95  cast to char* added */
     p88_lib = ( char * ) malloc (strlen (sys) + strlen (post_l_opt) + 1) ;
     strcpy (p88_lib, sys) ;
     strcat (p88_lib, post_l_opt ) ;
#endif
  }
}                                               /*  l i b i n i t  */
						/*-----------------*/

#ifdef LINT_ARGS
						/*-----------------------*/
extern char * linkstring (char * prgname)       /*  l i n k s t r i n g  */
#else
extern char * linkstring (prgname)
  /* returns the link command string */
  /* returns NULL if fails */
  char * prgname ;  /* unused! Replaced by exefnam and objmain */
#endif
{
  static char   fn[255];  /* static, because used as buffer behind res */
#if INDIRECT
  static char  res[255];
#else
#define CMDLENGTH 10000
  static char  res[CMDLENGTH+1];
#endif
  static char *tc_lib = "\\tc\\lib";
  int    c;
  int    i;
  char *p, *f ;
  char * sys = getenv (P88SYS);
  FILE * lnkfile;
  FILE * optfile;

  if  (not sys)   sys = sysdir ;
  strcpy (res, l_command);              /* start of linkstring */
  strcat (res, " ");
#if  ATARI_TURBO_C
  strcat (res, indir_opt);
  strcat (res, sys);
  strcat (res, pre_l_opt);
#else
  strcat (res, pre_l_opt);
#endif
/*  strcat (res, objfnam());  */

  if  (exe_l_opt) {                     /* executable option */
    strcat (res, exe_l_opt);
    strcat (res, exefnam());
    strcat (res, " ");
  }

#if INDIRECT
#if VAX_VMS_C
  strcat (res, loptname);               /* indirection option */
  strcat (res, indir_opt);
#else
  strcat (res, indir_opt);
  strcat (res, loptname);
#endif
  strcat (res, " ");
#endif

  lnkfile = fopen (linkinfo, "r");      /* open linkinfo */
  if (not lnkfile)  return NULL ;

#if INDIRECT
  optfile = fopen (loptname, "w") ;     /* open option file */
  if  (not optfile)  {
    printf ("Can not write on file %s\n", loptname) ;
    return NULL ;
  }
#endif
					/* startup and object of main */
#if IBM_AT_TURBO_C + IBM_AT_BORLAND_C
   p=getenv("LIB");
   fprintf( optfile, " %s%s+\n", (p)? p: tc_lib, "\\c0l" ); /* Startup code */
#elif ZORTECH_C
   p=getenv("LIB");
   /* Startup code */
   fprintf( optfile, " %s%s+\n", p, "\\cx.OBJ" );
#elif GNU_386_C
   fprintf (optfile, "-o %s\n", prognam()) ;
   fprintf( optfile, "%s\n", objmain() );
#elif IBM_AT_HIGH_C + ATARI_TURBO_C
   fprintf (optfile, "%s\n", objmain()) ;
#elif VAX_VMS_C
   fprintf (optfile, "%s,-\n", objmain()) ;
#endif

/****                                                       *****/
/*** First pass: all object files are put to the option file ****/

#if INDIRECT
  while ((c=fgetc(lnkfile)) != '\n' and c !=EOF);  /* skip program filename */
#endif

  while ((c=fgetc(lnkfile)) != EOF) {   /* read linkinfo */
    fn[0] = c;
    i = 1;
    while ((c=fgetc(lnkfile)) != '\n' and c !=EOF)
      fn[i++] = c;                      /* read object filename */
    /* fn[i] = '\0'; */

    i = i - objelen() ;   /* position of "." in filename */
    fn [i] = '\0' ;

    if  (exist (fn,noofext) or exist (fn,libext()) )
      continue ;          /* skip this object */

    strcpy (fn+i, objext()) ;   /*  restore object extension  */
				/*  write object file name  */
#if   IBM_AT_MS_C + IBM_AT_TURBO_C + ZORTECH_C + IBM_AT_BORLAND_C
    fprintf (optfile, "%s+\n", fn) ;
#elif VAX_VMS_C
    fprintf (optfile, "%s,-\n", fn) ;
#elif IBM_AT_WATCOM_C
    fprintf (optfile, "file %s\n", fn) ;
#elif  INDIRECT
    fprintf (optfile, "%s\n", fn) ;
#else
    strcat (res, " ");          /* UNIX */
    strcat (res, fn);
#endif
  }
  strcat (res, " ");

#if  IBM_AT_MS_C + ZORTECH_C
  fprintf (optfile, "%s\n" , objmain()) ;       /*Last OBJ*/
  fprintf (optfile, "%s\n" , exefnam()) ;       /*EXE*/
  fprintf (optfile, "\n" ) ;                  /*No map*/
#elif IBM_AT_WATCOM_C
  fprintf (optfile, "file %s\n" , objmain()) ;
  fprintf (optfile, "name %s\n", exefnam()) ;
#elif IBM_AT_TURBO_C + IBM_AT_BORLAND_C
  fprintf (optfile, "stacksz+\n") ;  /*Stack size !*/
  fprintf (optfile, "%s,+\n", objmain()) ;  /*Last OBJ*/
  fprintf (optfile, "%s,+\n", exefnam()) ;  /*EXE*/
  fprintf (optfile, ",+\n" ) ;           /*NO map*/
#endif

/*****                                                      *****/
/**** Second pass: all library files are put to the FILE.OPT ****/

  rewind(lnkfile);
  while ((c=fgetc(lnkfile)) != '\n' and c !=EOF);  /* skip program filename */

  while ((c=fgetc(lnkfile)) != EOF) {   /* read linkinfo */
    fn[0] = c;
    i = 1;
    while ((c=fgetc(lnkfile)) != '\n' and c !=EOF)  /* read object filename */
      fn[i++] = c;
    /* fn[i] = '\0'; */

    i = i - strlen (objext()) ;   /* position of "." in filename */
    fn [i] = '\0' ;
    if  (exist (fn,libext())){
#if VAX_VMS_C
      strcpy (fn+i, "/lib") ;     /* append library option */
#else
      strcpy (fn+i, libext()) ;   /* change obj.extension to lib.extension */
#endif

#if IBM_AT_MS_C + IBM_AT_TURBO_C + ZORTECH_C + IBM_AT_BORLAND_C
      fprintf( optfile, "%s+\n",fn  );
#elif VAX_VMS_C
      fprintf( optfile, "%s,-\n",fn  );
#elif IBM_AT_WATCOM_C
      fprintf( optfile, "library %s+\n",fn  );
#elif GNU_386_C + IBM_AT_HIGH_C + ATARI_TURBO_C
      fprintf (optfile, "%s\n", fn) ;
#else
      strcat (res, " ");
      strcat (res, fn);
#endif
    }
  }

  /*** End of second Pass */

  fclose (lnkfile); /*  CLOSE linkfile*/

					/* append post options */
  p=getenv(P88SYS);
#if IBM_AT_HIGH_C
   fprintf( optfile, "-lib e:\\pxsc\\lib\\mw_rts.lib\n");
   fprintf( optfile, "-lib d:\\meta\\small\\HCc.lib\n");
   fprintf( optfile, "-lib d:\\meta\\small\\na.lib\n");
/*
   fprintf( optfile, "-lib c:\\meta\\small\\hc386.lib\n");
   fprintf( optfile, "-lib c:\\meta\\small\\hc387.lib\n");
   fprintf( optfile, "-lib c:\\meta\\small\\hcna.lib\n");
*/
   fprintf( optfile, "-exe %s\n", exefnam());
   fprintf( optfile, "-nomap \n");
#elif IBM_AT_MS_C
  if (p88_lib){
    fprintf( optfile,  "%s\n", p88_lib);
    }
  else
    fprintf( optfile, "%scl_rts1.lib+%scl_rts2.lib\n%s",
             sys, sys, post_l_opt);

#elif IBM_AT_TURBO_C+IBM_AT_BORLAND_C
   fprintf( optfile, "%stcc_rts.lib+\n",  sys);

   p=getenv("LIB");
   fprintf( optfile, "%s%s+\n", (p)? p: tc_lib, "\\emu" );   /* FP emulator */
   fprintf( optfile, "%s%s+\n", (p)? p: tc_lib, "\\mathl" ); /* math. lib. */
   fprintf( optfile, "%s%s\n",  (p)? p: tc_lib, "\\cl" );    /* Large mem. mod*/

#elif IBM_AT_WATCOM_C
    fprintf( optfile, "library %swcl_rts.lib\n",  sys );

    /* Options */
    p = post_l_opt;
    f = fn;    /* fn is a buffer */
    while( *p ){
      for(f=fn; *p and *p != ' ';){
	*f++ = *p++;
      }
    if(*p)               /*Is not the end of str.*/
      ++p;               /*Skip blank*/
    *f = '\0';
    fprintf( optfile, "option %s\n", fn );
    }
#elif  ATARI_TURBO_C
    strcat (res, indir_opt);
    strcat (res, sys);
    strcat (res, post_l_opt);
#elif   INDIRECT
  if (p88_lib)
    fprintf( optfile,  " %s\n", p88_lib);
  /* else
    fprintf( optfile,  " %s%s", sys , post_l_opt); */
#else   /* not indirect */
  strcat (res, " ");
  if (p88_lib)
    strcat (res, p88_lib);
  /* else
   {
    strcat (res, sys );
    strcat (res, post_l_opt);
   } */
#endif

#if INDIRECT
    fclose (optfile);
#else
   if  (strlen (res) > CMDLENGTH)   {
     printf ("Sorry, the generated command line is longer than %d.\n", CMDLENGTH) ;
     printf ("The manager gives up.\n") ;
     exit (-1) ;
   }
#endif

#if TEST
  printf (res) ;
#endif
  return (res) ;
} /* linkstring */                              /*  l i n k s t r i n g  */
						/*-----------------------*/


   /*-----------------------------------*/
   /*  variables from option.h of pxsc  */

boolean
 /* initialize Compiler flags:
  user specified        on or off    default    comment
 +-------------------+-------------+---------+---------------------- */
  os_verbose = false ,  on_verbose = true ,  /* verbose compiler     */
  os_list    = false ,  on_list    = false,  /* listing to file      */
  os_warning = false ,  on_warning = true ,  /* report warnings      */
  os_merge   = false ,  on_merge   = false,  /* merge source in code */
  os_code    = false ,  on_code    = false,  /* code generation      */
  os_rename  = false ,  on_rename  = true ,  /* rename Pascal idents */
  os_index   = false ,  on_index   = true ,  /* index check          */
  os_number  = false ,  on_number  = true ,  /* linenumbers in code  */
  os_include = false ,  on_include = false,  /* include "filename"   */
  os_dump    = false ,  on_dump    = false,  /* structab dump        */
  os_terminal= false ,  on_terminal= false,  /* code to terminal     */
  os_handling= false ,  on_handling= false,  /* explain error hanndling */
  os_fastfor = false ,  on_fastfor = false,  /* for-loops for vector processors */
			on_srcdir  = false,  /* output directory     */
			on_remove  = true ,  /* remove output files  */
			on_prototyp= false,  /* new style prototypes */
			on_reverse = true ,  /* link modules in reverse */
  /* . . . . . to be continued */
  os_dummy      ;
char
  *ov_real    = (char*)0 ,                  /* value of real option */
  *ov_include = (char*)0 ;                  /* value of include option */
int
   ov_idlength = 8 ;           /* identifier length accepted by linker */


#if GNU_386_C
global void append_stub(char *sys_call)
{
  char *p;
#define system system

  strcpy (sys_call, "copy /b ");

  p=getenv("GCCBIN");
  if (p)
    strcat(sys_call,p);
  strcat(sys_call,"\\stub.exe+");
  p = strchr (filename,'.');
  if(p)
    *p = NULL;
  strcat (sys_call, filename) ;
  strcat (sys_call, " ") ;
  strcat (sys_call, exefnam()) ;
  strcat (sys_call, " >nul") ;
  /*printf("%s\n",sys_call);*/
  if (on_debug)    printf ("copy= %s\n", sys_call) ;
  retc = system (sys_call) ;
  remove ( filename ) ;
  strcpy (sys_call, filename) ;
  strcat (sys_call, ".c") ;
  remove (sys_call) ;
#define system msc_system
}
#endif
