home *** CD-ROM | disk | FTP | other *** search
/ Total Destruction / Total_Destruction.iso / addons / Lccwin32.exe / Lccwin32 / lccpub / driver / lcc.c
Encoding:
C/C++ Source or Header  |  1997-04-27  |  4.3 KB  |  176 lines

  1. /*
  2.    lcc.c
  3.  
  4.    Simple cc like interface, for rcc (lcc) and lcclnk.
  5.  
  6.    By - Scott Beasley, 03/24/97.
  7.         scottb@lowcountry.com   
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <windows.h>
  14.  
  15. /* Useful Macros. */
  16. #define GetCommandLineArg(argnum) ((argnum)<=(argc-1))?argv[argnum]:""
  17. #define NumOfCmdArgs() ((argc-1)>0?argc-1:0)
  18. #define CheckArgCnt(reqnum) ((argc-1)>=(reqnum)?1:0)
  19.  
  20. /* Define for compiler and linker names. */
  21. #define COMPILER "rcc "
  22. #define LINKER   "lcclnk "
  23.  
  24. int main ( int argc, char **argv );
  25.  
  26. int main ( int argc, char **argv )
  27. {
  28.    int iRet = 0, iArgCnt = 1, iFileCnt = 0;
  29.    int iCflgCnt = 0, iLdflgCnt = 0, iCnt = 0;
  30.    int iLnkFlgs = 0, iNoLd = 0;
  31.    char cFilesDone = 0;
  32.    char strArg[256];
  33.    char strCpFlgs[256], strLdFlgs[256];
  34.    char *strFileLst[100], *strCflagLst[50];
  35.    char *strLdflagLst[50], *strOffset;
  36.      
  37.    if ( !CheckArgCnt ( 1 ) ) {
  38.       puts ( "Usage: lcc [-c] [compiler flags] files... [linker flags]" );
  39.       puts ( "   ie: lcc hello.c -o hello.exe -s" );
  40.       puts ( "lcc flag: -c  compile file(s) only, with no linking done" );
  41.       exit ( 1 );
  42.    }
  43.  
  44.    while ( 1 ) {
  45.       strcpy ( strArg, GetCommandLineArg ( iArgCnt ) );
  46.  
  47.       if ( !strcmp ( strArg, "-c" ) && !cFilesDone ) {
  48.          iNoLd = 1;
  49.          iArgCnt++;
  50.          continue;
  51.       }
  52.     
  53.       if ( *strArg == '-' && !cFilesDone )
  54.          *( strCflagLst + iCflgCnt++ ) = ( char * ) *( argv + iArgCnt );
  55.  
  56.       if ( *strArg == '-' && cFilesDone ) {
  57.          iLnkFlgs = 1;
  58.          *( strLdflagLst + iLdflgCnt++ ) = ( char * ) *( argv + iArgCnt );
  59.       }
  60.  
  61.       if ( *strArg != '-' && !iLnkFlgs ) {
  62.          cFilesDone = 1;
  63.          *( strFileLst + iFileCnt++ ) = ( char * ) *( argv + iArgCnt );
  64.       }
  65.  
  66.       if ( *strArg != '-' && iLnkFlgs ) {
  67.          cFilesDone = 1;
  68.          *( strLdflagLst + iLdflgCnt++ ) = ( char * ) *( argv + iArgCnt );
  69.       }
  70.  
  71.       if ( iArgCnt == NumOfCmdArgs ( ) )
  72.          break;
  73.   
  74.       iArgCnt++;
  75.    }
  76.  
  77.    iCnt = 0;
  78.    strcpy ( strCpFlgs, COMPILER );
  79.    while ( iCnt != iCflgCnt ) {
  80.       strcat ( strCpFlgs, *( strCflagLst + iCnt++ ) );
  81.       strcat ( strCpFlgs, " " );      
  82.    }
  83.  
  84.    iCnt = 0;
  85.    strcpy ( strLdFlgs, LINKER );
  86.    while ( iCnt != iLdflgCnt ) {
  87.       strcat ( strLdFlgs, *( strLdflagLst + iCnt++ ) );
  88.       strcat ( strLdFlgs, " " );      
  89.    }
  90.  
  91.    iCnt = 0;
  92.    memset ( strArg, 0, sizeof ( strArg ) );
  93.    while ( iCnt != iFileCnt ) {
  94.       strcpy ( strArg, strCpFlgs );
  95.       strcat ( strArg, *( strFileLst + iCnt ) );
  96.  
  97.       if ( !strchr ( *( strFileLst + iCnt++ ), '.' ) ) {
  98.          strcat ( strArg, ".c" );         
  99.       }
  100.  
  101.       puts ( strArg );
  102.       iRet = _system ( strArg );
  103.       if ( iRet )
  104.          break; 
  105.    }   
  106.  
  107.    iCnt = 0;
  108.    while ( !iRet && iCnt != iFileCnt && !iNoLd ) {
  109.       strOffset = strchr ( *( strFileLst + iCnt++ ), '.' );
  110.       if ( strOffset ) {
  111.          *strOffset = 0;
  112.       }
  113.    }   
  114.  
  115.    iCnt = 0;
  116.    memset ( strArg, 0, sizeof ( strArg ) );
  117.    strcpy ( strArg, strLdFlgs );
  118.    strcat ( strArg, " " );
  119.    while ( !iRet && iCnt != iFileCnt && !iNoLd ) {
  120.       strcat ( strArg, *( strFileLst + iCnt++ ) );
  121.       strcat ( strArg, ".obj " );
  122.    }
  123.  
  124.    if ( !iRet && *strArg && !iNoLd ) {
  125.       puts ( strArg );
  126.       iRet = _system ( strArg );
  127.    }
  128.  
  129.    if ( iRet )
  130.       printf ( "\nlcc ended with a return code of %d\n", iRet );
  131.  
  132.    return iRet;
  133. }
  134.  
  135. /* 
  136.    Taken from the make project.
  137. */
  138. /*
  139.  * This way, child programs will execute and return the
  140.  * right exit code values.
  141.  */
  142.  
  143. int _system(const char *cmd)
  144. {
  145.   BOOL                bResult;
  146.   STARTUPINFO         si;
  147.   PROCESS_INFORMATION pi;
  148.   DWORD               exitcode;
  149.  
  150.   memset(&si,0,sizeof(STARTUPINFO));
  151.   si.cb = sizeof(STARTUPINFO);
  152.   
  153.   bResult = CreateProcess(NULL,
  154.               cmd,
  155.               NULL,NULL, /* No security */
  156.               TRUE,         /* Inherits handles */
  157.               0,         /* Runs normally  */
  158.               NULL,NULL, /* Inherits env & cwd */
  159.               &si,&pi);
  160.  
  161.   if (bResult == FALSE) 
  162.     return system(cmd);
  163.    
  164.   while (bResult = GetExitCodeProcess(pi.hProcess,&exitcode))
  165.     if (exitcode != STILL_ACTIVE)
  166.       break;
  167.  
  168.   if (bResult == FALSE) {
  169.     printf("GetExitCodeProcess() returned FALSE\n");
  170.     return -1;
  171.   }
  172.  
  173.   return (int)exitcode;
  174. }
  175.  
  176.