home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DMAKE37S.ZIP / DMAKE / DAG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-06  |  13.5 KB  |  493 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/dag.c,v 1.1 91/05/06 15:23:04 dvadura Exp $
  2. -- SYNOPSIS -- Routines to construct the internal dag.
  3. -- 
  4. -- DESCRIPTION
  5. --    This file contains all the routines that are responsible for
  6. --    defining and manipulating all objects used by the make facility.
  7. -- 
  8. -- AUTHOR
  9. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  10. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  11. --
  12. -- COPYRIGHT
  13. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  14. -- 
  15. --      This program is free software; you can redistribute it and/or
  16. --      modify it under the terms of the GNU General Public License
  17. --      (version 1), as published by the Free Software Foundation, and
  18. --      found in the file 'LICENSE' included with this distribution.
  19. -- 
  20. --      This program is distributed in the hope that it will be useful,
  21. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  22. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. --      GNU General Public License for more details.
  24. -- 
  25. --      You should have received a copy of the GNU General Public License
  26. --      along with this program;  if not, write to the Free Software
  27. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. --
  29. -- LOG
  30. --     $Log:    dag.c,v $
  31.  * Revision 1.1  91/05/06  15:23:04  dvadura
  32.  * dmake Release Version 3.7
  33.  * 
  34. */
  35.  
  36. #include "extern.h"
  37.  
  38.  
  39. PUBLIC HASHPTR
  40. Get_name( name, tab, define )/*
  41. ===============================
  42.     Look to see if the name is defined, if it is then return
  43.     a pointer to its node, if not return NIL(HASH).
  44.     If define is TRUE and the name is not found it will be added. */
  45.  
  46. char    *name;            /* name we are looking for   */
  47. HASHPTR *tab;            /* the hash table to look in */
  48. int    define;            /* TRUE => add to table      */
  49. {
  50.    register HASHPTR hp;
  51.    register char    *p;
  52.    uint16           hv;
  53.    uint32           hash_key;
  54.  
  55.    DB_ENTER( "Get_name" );
  56.    DB_PRINT( "name", ("Looking for %s", name) );
  57.  
  58.    hp = Search_table( tab, name, &hv, &hash_key );
  59.  
  60.    if( hp == NIL(HASH) && define ) {
  61.       /* Check to make sure that CELL name contains only printable chars */
  62.       for( p=name; *p; p++ )
  63.      if( !isprint(*p) )
  64.         Fatal( "Name contains non-printable character [0x%02x]", *p );
  65.  
  66.       TALLOC( hp, 1, HASH );        /* allocate a cell and add it in */
  67.  
  68.       hp->ht_name  = _strdup( name );
  69.       hp->ht_hash  = hash_key;
  70.       hp->ht_next  = tab[ hv ];
  71.       tab[ hv ]    = hp;
  72.  
  73.       DB_PRINT( "name", ("Adding %s", name) );
  74.    }
  75.  
  76.    DB_PRINT( "name",("Returning: [%s,%lu]",
  77.          (hp == NIL(HASH)) ? "":hp->ht_name, hv) );
  78.    DB_RETURN( hp );
  79. }
  80.  
  81.  
  82. PUBLIC HASHPTR
  83. Search_table( tab, name, phv, phkey )
  84. HASHPTR *tab;
  85. char    *name;
  86. uint16  *phv;
  87. uint32  *phkey;
  88. {
  89.    HASHPTR hp;
  90.  
  91.    *phv = Hash( name, phkey );
  92.  
  93.    for( hp = tab[ *phv ]; hp != NIL(HASH); hp = hp->ht_next )
  94.       if(    hp->ht_hash == *phkey
  95.       && !strcmp(hp->ht_name, name) )
  96.          break;
  97.  
  98.    return( hp );
  99. }
  100.  
  101.  
  102. PUBLIC HASHPTR
  103. Def_macro( name, value, flags )/*
  104. =================================
  105.    This routine is used to define a macro, and it's value.
  106.    The flags indicates if it is a permanent macro or if it's value
  107.    can be redefined.  A flags of M_PRECIOUS means it is a precious
  108.    macro and cannot be further redefined.  If the flags flag also
  109.    contains the M_MULTI bit it means that the macro can be redefined
  110.    multiple times and no warning of the redefinitions should be issued.
  111.    Once a macro's VAR flags are set they are preserved through all future
  112.    macro definitions.
  113.    
  114.    Macro definitions that have one of the variable bits set are treated
  115.    specially.  In each case the hash table entry var field points at the
  116.    global variable that can be set by assigning to the macro.
  117.    
  118.    bit valued global vars must be computed when the macro value is changed.
  119.    char valued global vars must have the first char of ht_value copied to
  120.    them.  string valued global vars have the same value as ht_value and should
  121.    just have the new value of ht_value copied to them.  */
  122.  
  123. char    *name;            /* macro name to define    */
  124. char    *value;            /* macro value to set    */
  125. int     flags;            /* initial ht_flags    */
  126. {
  127.    register HASHPTR   hp;
  128.    register char      *p, *q;
  129.  
  130.    DB_ENTER( "Def_macro" );
  131.    DB_PRINT( "mac", ("Defining macro %s = %s, %x", name, value, flags) );
  132.  
  133.    /* check to see if name is in the table, if so then just overwrite
  134.       the previous definition.  Otherwise allocate a new node, and
  135.       stuff it in the hash table, at the front of any linked list */
  136.  
  137.    if( Readenv ) flags |= M_LITERAL;
  138.  
  139.    hp = Get_name( name, Macs, TRUE );
  140.  
  141.    if( (hp->ht_flag & M_PRECIOUS) && !(flags & M_FORCE) ) {
  142.       Warning( "Macro `%s' cannot be redefined", name );
  143.       DB_RETURN( hp );
  144.    }
  145.  
  146.    if( hp->ht_value != NIL(char) ) FREE( hp->ht_value );
  147.  
  148.    if( (hp->ht_flag & M_USED) && !((flags | hp->ht_flag) & M_MULTI) )
  149.       Warning( "Macro `%s' redefined after use", name );
  150.  
  151.    if( (value != NIL(char)) && (*value) ) {
  152.       /* strip out any \<nl> combinations where \ is the current CONTINUATION
  153.        * char */
  154.  
  155.       for( p = value; (p = strchr(p, CONTINUATION_CHAR)) != NIL(char); )
  156.          if( p[1] == '\n' )
  157.         strcpy( p, p+2 );
  158.      else
  159.         p++;
  160.  
  161.       if( !(flags & M_LITERAL) ) {
  162.      p = _strdup( _strspn( value, " \t" ) ); /* strip white space before */
  163.                          /* ... and after value         */
  164.      if( *p ) {
  165.         for(q=p+strlen(p)-1; ((*q == ' ')||(*q == '\t')); q--);
  166.         *++q = '\0';
  167.      }
  168.      flags &= ~M_LITERAL;
  169.       }
  170.       else
  171.      p = _strdup( value );               /* take string literally   */
  172.       
  173.       if( !*p )    {                /* check if result is ""   */
  174.          FREE( p );
  175.          p = NIL(char);
  176.      flags |= M_EXPANDED;
  177.       }
  178.       else if( *_strpbrk( p, "${}" ) == '\0' )
  179.      flags |= M_EXPANDED;
  180.  
  181.       hp->ht_value = p;
  182.    }
  183.    else
  184.       hp->ht_value = NIL(char);
  185.  
  186.    /* Assign the hash table flag less the M_MULTI flag, it is used only
  187.     * to silence the warning.  But carry it over if it was previously
  188.     * defined in ht_flag, as this is a permanent M_MULTI variable. */
  189.  
  190.    hp->ht_flag = (flags & ~(M_MULTI|M_FORCE)) |
  191.          (hp->ht_flag & (M_VAR_MASK | M_MULTI));
  192.    
  193.    /* Check for macro variables and make the necessary adjustment in the
  194.     * corresponding global variables */
  195.     
  196.    if( hp->ht_flag & M_VAR_MASK )
  197.       if( !(flags & M_EXPANDED) )
  198.      Error( "Macro variables must be assigned with :=" );
  199.       else switch( hp->ht_flag & M_VAR_MASK )    /* only one var type per var */
  200.       {
  201.      case M_VAR_STRING:
  202.         *hp->MV_SVAR = hp->ht_value;
  203.         break;
  204.  
  205.      case M_VAR_CHAR:
  206.         *hp->MV_CVAR = (hp->ht_value == NIL(char)) ? '\0':*hp->ht_value;
  207.         break;
  208.   
  209.      case M_VAR_INT: {
  210.         int tvalue;
  211.         if( hp->MV_IVAR == NIL(int) ) break;    /* first time */
  212.  
  213.         tvalue = atoi(hp->ht_value);
  214.         if( hp->MV_IVAR == &Buffer_size ) {
  215.            /* If Buffer_size is modified then make sure you change the
  216.             * size of the real buffer as well. */
  217.            tvalue = (tvalue < (BUFSIZ-2)) ? BUFSIZ : tvalue+2;
  218.            if( Buffer_size == tvalue ) break;
  219.            if( Buffer ) FREE(Buffer);
  220.            if((Buffer=MALLOC(tvalue, char)) == NIL(char)) No_ram();
  221.            *Buffer = '\0';
  222.         }
  223.         *hp->MV_IVAR = tvalue;
  224.  
  225.         if( hp->MV_IVAR == &Max_proc || hp->MV_IVAR == &Max_proclmt ) {
  226.            if( tvalue < 1 )
  227.           Fatal( "Process limit value must be > 1" );
  228.  
  229.            if( Max_proc > Max_proclmt )
  230.           Fatal( "Specified # of processes exceeds limit of [%d]",
  231.              Max_proclmt );
  232.         }
  233.      } break;
  234.  
  235.      case M_VAR_BIT:
  236.         /* Bit variables are set to 1 if ht_value is not NULL and 0
  237.          * otherwise */
  238.          
  239.         if( hp->ht_value == NIL(char) )
  240.            *hp->MV_BVAR &= ~hp->MV_MASK;
  241.         else
  242.            *hp->MV_BVAR |= hp->MV_MASK;
  243.         break;
  244.       }
  245.    
  246.    DB_RETURN( hp );
  247. }
  248.  
  249.  
  250.  
  251. PUBLIC CELLPTR
  252. Def_cell( name )/*
  253. ==================
  254.    Take a string passed in and define it as a cell
  255.    If the cell exists then return a pointer to it. */
  256. char    *name;
  257. {
  258.    register HASHPTR  hp;
  259.    register CELLPTR  cp;
  260.    register CELLPTR  lib;
  261.    char             *member;
  262.    char             *end;
  263.  
  264.    DB_ENTER( "Def_cell" );
  265.  
  266.    /* Check to see if the cell is a member of the form lib(member) or
  267.     * lib((symbol)) and handle the cases appropriately.
  268.     * What we do is we look at the target, if it is of the above two
  269.     * forms we get the lib, and add the member/symbol to the list of
  270.     * prerequisites for the library.  If this is a symbol name def'n
  271.     * we additionally add the attribute A_SYMBOL, so that stat can
  272.     * try to do the right thing.  */
  273.  
  274.    if( ((member = strchr(name, '('))     != NIL(char)) &&
  275.        ((end    = strrchr(member,  ')')) != NIL(char)) &&
  276.        (member > name) && (member[-1] != '$') &&
  277.        (end > member+1)  && (end[1] == '\0') )
  278.    {
  279.       *member++ = *end = '\0';
  280.  
  281.       if( (*member == '(') && (member[strlen(member)-1] == ')') ) {
  282.      member[ strlen(member)-1 ] = '\0';
  283.      cp = Def_cell( member+1 );
  284.      cp->ce_attr |= A_SYMBOL;
  285.       }
  286.       else
  287.      cp = Def_cell( member );
  288.  
  289.       lib  = Def_cell( name );
  290.  
  291.       Add_prerequisite( lib, cp, FALSE, FALSE );
  292.       lib->ce_attr |= A_LIBRARY | A_COMPOSITE;
  293.  
  294.       if( !Def_targets ) cp = lib;
  295.    }
  296.    else {
  297.       hp = Get_name( name, Defs, TRUE );/* get the name from hash table */
  298.  
  299.       if( hp->CP_OWNR == NIL(CELL) )    /* was it previously defined    */
  300.       {                    /* NO, so define a new cell    */
  301.      DB_PRINT( "cell", ("Defining cell [%s]", name) );
  302.  
  303.      TALLOC( cp, 1, CELL );
  304.      hp->CP_OWNR = cp;
  305.      cp->ce_name = hp;
  306.      cp->ce_fname = hp->ht_name;
  307.       }
  308.       else                 /* YES, so return the old cell    */
  309.       {
  310.      DB_PRINT( "cell", ("Getting cell [%s]", hp->ht_name) );
  311.      cp = hp->CP_OWNR;
  312.       }
  313.    }
  314.  
  315.    DB_RETURN( cp );
  316. }
  317.  
  318.  
  319.  
  320.  
  321. PUBLIC LINKPTR
  322. Add_prerequisite( cell, prq, head, force )/*
  323. ============================================
  324.     Add a dependency node to the dag.  It adds it to the prerequisites,
  325.     if any, of the cell and makes certain they are in linear order.
  326.     If head == 1, then add to head of the prerequisite list, else
  327.     add to tail. */
  328. CELLPTR cell;
  329. CELLPTR prq;
  330. int     head;
  331. int     force;
  332. {
  333.    register LINKPTR lp, tlp;
  334.  
  335.    DB_ENTER( "Add_prerequisite" );
  336.    DB_PRINT( "cell", ("Defining prerequisite %s", prq->CE_NAME) );
  337.  
  338.    if( (prq->ce_flag & (F_MAGIC | F_PERCENT)) && !force )
  339.       Fatal( "Special target [%s] cannot be a prerequisite",
  340.              prq->CE_NAME );
  341.  
  342.    if( cell->ce_prq == NIL(LINK) ) {    /* it's the first one    */
  343.       TALLOC( lp, 1, LINK );
  344.       lp->cl_prq   = prq;
  345.       cell->ce_prq = lp;
  346.    }
  347.    else    {    /* search the list, checking for duplicates    */
  348.       for( lp = cell->ce_prq;
  349.        (lp->cl_next != NIL(LINK)) && (lp->cl_prq != prq);
  350.        lp = lp->cl_next );
  351.  
  352.       /* If the prq is not found and we are at the last prq in the list,
  353.        * allocate a new prq and place it into the list, insert it at the
  354.        * head if head == 1, else we add it to the end. */
  355.  
  356.       if( lp->cl_prq != prq ) {
  357.      TALLOC( tlp, 1, LINK );
  358.      tlp->cl_prq = prq;
  359.  
  360.      if( head ) {
  361.         tlp->cl_next = cell->ce_prq;
  362.         cell->ce_prq = tlp;
  363.      }
  364.      else
  365.         lp->cl_next  = tlp;
  366.  
  367.      lp = tlp;
  368.       }
  369.    }
  370.  
  371.    DB_RETURN( lp );
  372. }
  373.  
  374.  
  375.  
  376. PUBLIC void
  377. Clear_prerequisites( cell )/*
  378. =============================
  379.     Clear out the list of prerequisites, freeing all of the LINK nodes,
  380.     and setting the list to NULL */
  381. CELLPTR cell;
  382. {
  383.    LINKPTR lp, tlp;
  384.  
  385.    DB_ENTER( "Clear_prerequisites" );
  386.    DB_PRINT( "cell", ("Nuking prerequisites") );
  387.  
  388.    if( cell == NIL(CELL) ) { DB_VOID_RETURN; }
  389.  
  390.    for( lp=cell->ce_prq; lp != NIL(LINK); lp=tlp ) {
  391.       tlp=lp->cl_next;
  392.       FREE( lp );
  393.    }
  394.  
  395.    cell->ce_prq = NIL(LINK);
  396.  
  397.    DB_VOID_RETURN;
  398. }
  399.  
  400.  
  401. PUBLIC int
  402. Test_circle( cp, fail )/*
  403. =========================
  404.     Actually run through the graph */
  405. CELLPTR cp;
  406. int     fail;
  407. {
  408.    register LINKPTR lp;
  409.    int res = 0;
  410.  
  411.    DB_ENTER( "Test_circle" );
  412.    DB_PRINT( "tc", ("checking [%s]", cp->CE_NAME) );
  413.  
  414.    if( cp->ce_flag & F_MARK )
  415.       if( fail )
  416.      Fatal("Detected circular dependency in graph at [%s]", cp->CE_NAME);
  417.       else
  418.      DB_RETURN( 1 );
  419.  
  420.    cp->ce_flag |= F_MARK;
  421.    for( lp = cp->ce_prq; !res && lp != NIL(LINK); lp = lp->cl_next )
  422.       res = Test_circle( lp->cl_prq, fail );
  423.    cp->ce_flag ^= F_MARK;
  424.  
  425.    DB_RETURN( res );
  426. }
  427.  
  428.  
  429.  
  430. PUBLIC STRINGPTR
  431. Def_recipe( rcp, sp, white_too, no_check )/*
  432. =============================================
  433.     Take the recipe and add it to the list of recipes
  434.     pointed to by sp.  sp points to the last element.
  435.     return a pointer to the new recipe.  If white_too == TRUE add the
  436.     recipe even if it contains only white space.
  437.     If no_check is true then don't look for -@ at the start of the
  438.     recipe line. */
  439. char      *rcp;
  440. STRINGPTR sp;
  441. int       white_too;
  442. int       no_check;
  443. {
  444.    register STRINGPTR nsp;
  445.    register char      *rp;
  446.  
  447.    DB_ENTER( "Def_recipe" );
  448.    DB_PRINT( "rul", ("Defining recipe %s", rcp) );
  449.  
  450.    if( !white_too ) rcp = _strspn( rcp, " \t" );
  451.    if( (rcp == NIL(char)) || (*rcp == 0 && !white_too) )
  452.       DB_RETURN( sp );         /* return last recipe when new recipe not added */
  453.  
  454.    rp = no_check ? rcp : _strspn( rcp, " \t@-+%" );
  455.  
  456.    TALLOC( nsp, 1, STRING );
  457.    nsp->st_string = _strdup( rp );
  458.  
  459.    if( sp != NIL(STRING) ) sp->st_next = nsp;
  460.    nsp->st_next = NIL(STRING);
  461.  
  462.    if( !no_check ) nsp->st_attr |= Rcp_attribute( rcp );
  463.  
  464.    DB_RETURN( nsp );
  465. }
  466.  
  467.  
  468. PUBLIC t_attr
  469. Rcp_attribute( rp )/*
  470. ======================
  471.    Look at the recipe and return the set of attributes that it defines. */
  472. char *rp;
  473. {
  474.    t_attr flag = A_DEFAULT;
  475.    int    done = FALSE;
  476.  
  477.    while( !done )
  478.       switch( *rp++ )
  479.       {
  480.      case '@' : flag |= A_SILENT; break;
  481.      case '-' : flag |= A_IGNORE; break;
  482.      case '+' : flag |= A_SHELL;  break;
  483.      case '%' : flag |= A_SWAP;   break;
  484.  
  485.      case ' ' :
  486.      case '\t': break;
  487.  
  488.      default: done = TRUE; break;
  489.       }
  490.  
  491.    return(flag);
  492. }
  493.