home *** CD-ROM | disk | FTP | other *** search
/ Vectronix 2 / VECTRONIX2.iso / FILES_01 / LATTIC_3.LZH / SRC / _SETARGV.C < prev    next >
C/C++ Source or Header  |  1990-06-11  |  3KB  |  138 lines

  1. /*
  2.  * _setargv.c - argument set up for Lattice C 5 programs run from desktop
  3.  *
  4.  * _setargv is called by the initial startup routine, when an ARGV
  5.  * environment variable could not be located. It is up to this routine
  6.  * to parse the command line, building the argv[] vector, and doing any
  7.  * other environmental setup the user requires when a shell is not present.
  8.  *
  9.  * This module must be compiled without stack checking, this is because
  10.  * _setargv is called prior to the stack base variable being
  11.  * initialised, or memory being sized. Similarly it may not call any
  12.  * subroutines which contravene these limitations. The subroutine itself
  13.  * must use registerised parameter passing, otherwise the startup stub will
  14.  * break.
  15.  *
  16.  * Started 20/8/89 Alex G. Kiernan
  17.  *
  18.  * 22/8/89 - Changed to register convention, fixed up exact types of parms
  19.  * 28/8/89 - Added stderr, stdaux and stdprt support
  20.  *
  21.  * Copyright (c) 1989 HiSoft
  22.  */
  23.  
  24. #include <stddef.h>
  25. #include <dos.h>
  26.  
  27. __regargs __redirect(const char *infile,const char *outfile,short *old)
  28. {
  29.     /* perform input redirection */
  30.     if (infile)
  31.     {
  32.         long fh;
  33.         
  34.         if ((fh=Fopen(infile,0))>=0)
  35.         {
  36.             if (old)
  37.                 old[0]=Fdup(0);
  38.             Fforce(0,fh);
  39.         }
  40.     }
  41.  
  42.     /* perform output redirection */
  43.     if (outfile)
  44.     {
  45.         long fh;
  46.         int append;
  47.  
  48.         if (*outfile=='>')
  49.         {
  50.             append=1;
  51.             if (outfile[1]=='&')    /* redirect stderr as well */
  52.             {
  53.                 fh=Fopen(outfile+2,2);
  54.                 outfile=NULL;        /* flag to do stderr as well */
  55.             }
  56.             else
  57.                 fh=Fopen(outfile+1,1);
  58.         }
  59.         else
  60.         {
  61.             append=0;
  62.             if (*outfile=='&')        /* redirect stderr as well */
  63.             {
  64.                 fh=Fcreate(outfile+1,0);
  65.                 outfile=NULL;        /* flag to do stderr as well */
  66.             }
  67.             else
  68.                 fh=Fcreate(outfile,0);
  69.         }
  70.         
  71.         if (fh>=0)
  72.         {
  73.             if (append)
  74.                 Fseek(0,fh,2);
  75.             if (old)
  76.                 old[1]=Fdup(1);
  77.             Fforce(1,fh);
  78.             if (!outfile)        /* redirect stderr as well */
  79.             {
  80.                 if (old)
  81.                     old[2]=Fdup(2);
  82.                 Fforce(2,fh);
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. __regargs char **_setargv(char *line, char **argv)
  89. {
  90.     char *infile=NULL, *outfile=NULL;
  91.     
  92.     *argv++="";        /* ANSI requirement when program name unavailable */
  93.     for (;;)
  94.     {
  95.         register char c;
  96.         
  97.         while (*line==' '||*line=='\t')
  98.             line++;
  99.         if (!*line)
  100.             break;
  101.         switch(c=*line)
  102.         {
  103.             case '<':
  104.                 infile=++line;
  105.                 c='\0';
  106.                 break;
  107.                 
  108.             case '>':
  109.                 outfile=++line;
  110.                 c='\0';
  111.                 break;
  112.  
  113.             case '\'':
  114.             case '"':
  115.                 *argv++=++line;
  116.                 break;
  117.  
  118.             default:
  119.                 *argv++=line;
  120.                 c='\0';
  121.                 break;
  122.         }
  123.         while (*line && (c?*line!=c:(*line!=' '&&*line!='\t')))
  124.             line++;
  125.         if (!*line)
  126.             break;
  127.         *line++='\0';
  128.     }
  129.     
  130.     if (_disatty(2))
  131.         Fforce(2,-1);    /* point stderr to console */
  132.  
  133.     __redirect(infile,outfile,NULL);
  134.  
  135.     *argv++=NULL;        /* UNIX requirement */
  136.     return argv;        /* return first free byte after argv[] */
  137. }
  138.