home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 529b.lha / BMake_v1.1 / recipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-03  |  2.8 KB  |  116 lines

  1. /*    recipe.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  */
  5.  
  6. #include <clib/exec_protos.h>
  7. #include <clib/dos_protos.h>
  8.  
  9. #include <string.h>
  10. #include <ctype.h>
  11.  
  12. #include "make.h"
  13. #include "depend.h"
  14. #include "cond.h"
  15.  
  16. static
  17. struct List Cstack =
  18. {
  19.     (struct Node *)&Cstack.lh_Tail,    /* lh_Head */
  20.     (struct Node *)NULL,            /* lh_Tail */
  21.     (struct Node *)&Cstack.lh_Head,    /* lh_TailPred */
  22.     (UBYTE)NT_USER,
  23.     (UBYTE)0
  24. };
  25.  
  26.  
  27. static int
  28. changedir( char *newdir )
  29. {
  30.     BPTR oldlock = 0L, newlock = 0L;
  31.  
  32.     while( isspace( *newdir )) newdir++;
  33.     newlock = ( *newdir ) ? Lock( newdir, MODE_OLDFILE ) : Global.oldcwd;
  34.     if( newlock ) {
  35.         oldlock = CurrentDir( newlock );
  36.         if( *newdir && !Global.oldcwd )
  37.             Global.oldcwd = oldlock; /* bug if oldlock is the root */
  38.         else UnLock( oldlock );
  39.         return( 0 );
  40.     }
  41.     logprintf( "Unable to Lock directory %s\n", newdir );    
  42.     return( 1 );
  43. }
  44.  
  45. /* execute the command List to make a target */
  46. int
  47. recipe( const char *goalname, struct List *cmdlist )
  48. {
  49.     char special_cmd[ 10 ];
  50.     struct command *cmd;
  51.     char *expansion = NULL;
  52.     char *next;
  53.     int retval = 0, state;
  54.  
  55.     if( !cmdlist ) return( 0 ); /* no command list */
  56.  
  57.     if( Param.touch_mode ) {
  58.         logprintf( "\ttouch(%s)\n", goalname );
  59.         if( !Param.pretend_mode ) touch( goalname );
  60.         return( 0 );
  61.     }
  62.  
  63.     expansion = (char *)malloc( Param.MaxLine );
  64.  
  65.     for( cmd = (struct command *) cmdlist->lh_Head;    cmd->node.ln_Succ;
  66.         cmd = cmd->node.ln_Succ ) {
  67.         if( next = cmd->cmd )
  68.             while( isspace( *next )) next++;
  69.         if( next && *next ) {
  70.             if( expand_macros( expansion, next, Param.MaxLine )) {
  71.                 logprintf( "Error expanding macros on commandline:\n"
  72.                     "\t%s\n", next );
  73.                 break;
  74.             };
  75.             next = expansion;
  76.             while( isspace( *next )) next++;
  77.  
  78.             next = parse_str( special_cmd, next, sizeof(special_cmd));
  79.             if( !strcmp( special_cmd, "cd" )) {
  80.                 if( retval = changedir( next )) break;
  81.                 continue;
  82.             }
  83.             else if( !strcmp( special_cmd, "if" )) {
  84.                 if( retval = drctv_if( next, &Cstack )) break;
  85.                 continue;
  86.             }
  87.             else if( !strcmp( special_cmd, "else" )) {
  88.                 if( retval = drctv_else( next, &Cstack )) break;
  89.                 continue;
  90.             }
  91.             else if( !strcmp( special_cmd, "endif" )) {
  92.                 if( retval = drctv_endif( next, &Cstack )) break;
  93.                 continue;
  94.             }
  95.  
  96.             state = get_directive_state( &Cstack );
  97.             if( state == STATE_IF_F || state == STATE_EL_T ) continue;
  98.  
  99.             if( !(cmd->flags & CF_NOECHO ) || Param.debug )
  100.                 logprintf( "\t%s\n", expansion );
  101.             retval = (Param.pretend_mode ) ? 0 :xsystem( expansion );
  102.             if( retval ) {
  103.                 char *s = (cmd->flags & CF_IGNORE ) ? "(Ignored)" : "" ;
  104.                 logprintf( "command returned error %d %s\n", retval, s );
  105.                 if( !(cmd->flags & CF_IGNORE )) break;
  106.             }
  107.         }
  108.     }
  109.     if( !retval && get_directive_state( &Cstack )) {
  110.         logprintf( "Missing endif conditional command for %s\n", goalname );
  111.         retval = 1;
  112.     }
  113.     if( expansion ) free( expansion );
  114.     return( retval );
  115. }
  116.