home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d523 / bmake.lha / BMake / source.lzh / macro.c < prev    next >
C/C++ Source or Header  |  1991-07-21  |  4KB  |  161 lines

  1. /*    macro.c
  2.  *    (c) Copyright 1991 by Ben Eng, All Rights Reserved
  3.  *
  4.  *    USER DEFINABLE variables
  5.  *
  6.  *    $x        = the variable x
  7.  *    $(var)    = the variable var
  8.  *    ${var}    = the same variable var
  9.  *
  10.  *    INTERNAL AUTOMATIC variables (not for user definitions)
  11.  *
  12.  *    $$ = $
  13.  *    $@ = target filename
  14.  *    $* = basename of target
  15.  *    $< = dependent filename
  16.  *    $^ = all dependents of target newer than target (not-imp)
  17.  *    $% = dependent member of the target archive (not-imp)
  18.  *    $? = all dependents of the target archive (not-imp)
  19.  */
  20.  
  21. #include <clib/exec_protos.h>
  22.  
  23. #include "make.h"
  24. #include "depend.h"
  25.  
  26. /*    internal function used to find an existing variable */
  27. struct target *
  28. find_macro( char *macroname )
  29. {
  30.     struct macro *mac = NULL;
  31.  
  32.     for( struct macro *ln = Global.macrolist.lh_Head; ln->node.ln_Succ;
  33.         ln = ln->node.ln_Succ ) {
  34.         if( !stricmp( macroname, ln->name )) {
  35.             mac = ln;
  36.             break;
  37.         }
  38.     }
  39.  
  40.     return( mac );
  41. }
  42.  
  43. /*    internal allocation function */
  44. struct macro *
  45. new_macro( char *name, char *expansion )
  46. {
  47.     long size = sizeof(struct macro);
  48.     struct macro *new = (struct macro *)calloc( size, 1 );
  49.     if( new ) {
  50.         if( name )
  51.             if( !(new->name = strdup( name ))) goto death;
  52.         if( expansion )
  53.             if( !(new->expansion = strdup( expansion ))) goto death;
  54.         new->flags = 0;
  55.     }
  56.     return( new );
  57. death:
  58.     if( new ) {
  59.         if( new->name ) free( new->name );
  60.         if( new->expansion ) free( new->expansion );
  61.         free( new );
  62.     }
  63.     return( NULL );
  64. }
  65.  
  66. /*    internal deallocation function */
  67. int
  68. delete_macro( struct macro *mac )
  69. {
  70.     if( mac->name ) free( mac->name );
  71.     if( mac->expansion ) free( mac->expansion );
  72.     free( mac );
  73.     return( 0 );
  74. }
  75.  
  76. /*    perform a variable assignment
  77.  *    this is the entry point from outside
  78.  *    a NULL or null string will delete the variable
  79.  */
  80. struct macro *
  81. set_macro( char *name, char *expansion )
  82. {
  83.     struct macro *mac = NULL;
  84.     int create_flag = (expansion && !isemptyline( expansion ));
  85.  
  86.     if( mac = find_macro( name )) {
  87.         if( create_flag ) {
  88.             if( mac->expansion ) free( mac->expansion );
  89.             mac->expansion = strdup( expansion );
  90.         }
  91.         else {
  92.             Remove( &mac->node );
  93.             delete_macro( mac );
  94.             mac = NULL;
  95.         }
  96.     }
  97.     else
  98.         if( create_flag ) mac = new_macro( name, expansion );
  99.  
  100.     if( mac ) {
  101.         mac->flags &= ~MF_SIMPLE;
  102.         if( !(mac->flags & MF_ADDED )) {
  103.             mac->flags |= MF_ADDED;
  104.             AddTail( &Global.macrolist, &mac->node );
  105.         }
  106.     }
  107.     return( mac );
  108. }
  109.  
  110. /*    Set's the automatic variables $@ $* and $<  */
  111. void
  112. set_target_macros( char *tarname, char *depname )
  113. {
  114.     struct macro *mac;
  115.  
  116.     if( tarname && *tarname ) {
  117.         char *dupname = strdup( tarname );
  118.         char *next;
  119.         if( dupname ) { /* try the more efficient way first */
  120.             next = strrchr( dupname, '.' );
  121.             if( next ) *next = (char)0; /* remove the extension */
  122.             mac    = set_macro( "*", dupname );
  123.             free( dupname );
  124.         }
  125.         else {
  126.             mac = set_macro( "*", tarname );
  127.             next = strrchr( mac->name, '.' );
  128.             if( next ) *next = (char)0; /* remove the extension */
  129.         }
  130.     }
  131.     else mac = set_macro( "*", NULL );
  132.     mac = set_macro( "@", tarname );
  133.     mac = set_macro( "<", depname );
  134. }
  135.  
  136. void
  137. delete_macrolist( struct List *list )
  138. {
  139.     for_list( list, delete_macro );
  140.     NewList( list );
  141. }
  142.  
  143. /* set a variable by expanding all macros in its value */
  144. struct macro *
  145. set_simplemacro( char *name, char *value )
  146. {
  147.     char *expansion = NULL;
  148.     struct macro *mac = NULL;
  149.  
  150.     expansion = (char *)malloc( Param.MaxLine );
  151.     if( !expansion ) goto death;
  152.  
  153.     if( expand_macros( expansion, value, Param.MaxLine )) goto death;
  154.     if( mac = set_macro( name, expansion )) {
  155.         mac->flags |= MF_SIMPLE;
  156.     }
  157. death:
  158.     if( expansion ) free( expansion );
  159.     return( mac );
  160. }
  161.