home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / dag.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  16KB  |  565 lines

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/RCS/dag.c,v 1.1 1994/10/06 17:41:22 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) 1992,1994 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  1994/10/06  17:41:22  dvadura
  32.  * dmake Release Version 4.0, Initial revision
  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) && !iswhite(*p) && *p != '\n' )
  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  = DmStrDup( 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. Push_macro(hp)
  104. HASHPTR hp;
  105. {
  106.    HASHPTR cur,prev;
  107.    uint16  hv;
  108.    uint32  key;
  109.  
  110.    hv = Hash(hp->ht_name, &key);
  111.  
  112.    for(prev=NIL(HASH),cur=Macs[hv]; cur != NIL(HASH); prev=cur,cur=hp->ht_next)
  113.       if(    cur->ht_hash == key
  114.       && !strcmp(cur->ht_name, hp->ht_name) )
  115.          break;
  116.  
  117.    if (cur == NIL(HASH) || prev == NIL(HASH)) {
  118.       hp->ht_next  = Macs[hv];
  119.       Macs[hv] = hp;
  120.    }
  121.    else {
  122.       hp->ht_next = prev->ht_next;
  123.       prev->ht_next = hp;
  124.    }
  125.  
  126.    return(hp);   
  127. }
  128.  
  129.  
  130. PUBLIC HASHPTR
  131. Pop_macro(hp)
  132. HASHPTR hp;
  133. {
  134.    HASHPTR cur,prev;
  135.    uint16  hv;
  136.    uint32  key;
  137.  
  138.    hv = Hash(hp->ht_name, &key);
  139.  
  140.    for(prev=NIL(HASH),cur=Macs[hv]; cur != NIL(HASH);prev=cur,cur=hp->ht_next)
  141.       if (cur == hp)
  142.      break;
  143.  
  144.    if (cur == NIL(HASH))
  145.       return(NIL(HASH));
  146.  
  147.    if (prev)
  148.       prev->ht_next = cur->ht_next;
  149.    else
  150.       Macs[hv] = hp->ht_next;
  151.    hp->ht_next = NIL(HASH);
  152.  
  153.    return(hp);   
  154. }
  155.  
  156.  
  157.  
  158. PUBLIC HASHPTR
  159. Def_macro( name, value, flags )/*
  160. =================================
  161.    This routine is used to define a macro, and it's value.
  162.    The flags indicates if it is a permanent macro or if it's value
  163.    can be redefined.  A flags of M_PRECIOUS means it is a precious
  164.    macro and cannot be further redefined.  If the flags flag also
  165.    contains the M_MULTI bit it means that the macro can be redefined
  166.    multiple times and no warning of the redefinitions should be issued.
  167.    Once a macro's VAR flags are set they are preserved through all future
  168.    macro definitions.
  169.    
  170.    Macro definitions that have one of the variable bits set are treated
  171.    specially.  In each case the hash table entry var field points at the
  172.    global variable that can be set by assigning to the macro.
  173.    
  174.    bit valued global vars must be computed when the macro value is changed.
  175.    char valued global vars must have the first char of ht_value copied to
  176.    them.  string valued global vars have the same value as ht_value and should
  177.    just have the new value of ht_value copied to them.  */
  178.  
  179. char    *name;            /* macro name to define    */
  180. char    *value;            /* macro value to set    */
  181. int     flags;            /* initial ht_flags    */
  182. {
  183.    register HASHPTR   hp;
  184.    register char      *p, *q;
  185.  
  186.    DB_ENTER( "Def_macro" );
  187.    DB_PRINT( "mac", ("Defining macro %s = %s, %x", name, value, flags) );
  188.  
  189.    /* check to see if name is in the table, if so then just overwrite
  190.       the previous definition.  Otherwise allocate a new node, and
  191.       stuff it in the hash table, at the front of any linked list */
  192.  
  193.    if( Readenv ) flags |= M_LITERAL|M_EXPANDED;
  194.  
  195.    hp = Get_name( name, Macs, TRUE );
  196.  
  197.    if ((flags & M_PUSH) && hp->ht_name != NIL(char)) {
  198.       HASHPTR thp=hp;
  199.       TALLOC(hp,1,HASH);
  200.       hp->ht_name  = DmStrDup(thp->ht_name);
  201.       hp->ht_hash  = thp->ht_hash;
  202.       Push_macro(hp);
  203.    }
  204.    flags &= ~M_PUSH;
  205.  
  206.    if( (hp->ht_flag & M_PRECIOUS) && !(flags & M_FORCE) ) {
  207.       Warning( "Macro `%s' cannot be redefined", name );
  208.       DB_RETURN( hp );
  209.    }
  210.  
  211.    /* Make sure we don't export macros whose names contain legal macro
  212.     * assignment operators, since we can't do proper quoting in the
  213.     * environment. */
  214.    if( *DmStrPbrk(name, "*+:=") != '\0' ) flags |= M_NOEXPORT;
  215.  
  216.    if( hp->ht_value != NIL(char) ) FREE( hp->ht_value );
  217.  
  218.    if( (hp->ht_flag & M_USED) && !((flags | hp->ht_flag) & M_MULTI) )
  219.       Warning( "Macro `%s' redefined after use", name );
  220.  
  221.    if( (value != NIL(char)) && (*value) ) {
  222.       /* strip out any \<nl> combinations where \ is the current CONTINUATION
  223.        * char */
  224.  
  225.       for( p = value; (p = strchr(p, CONTINUATION_CHAR)) != NIL(char); )
  226.          if( p[1] == '\n' )
  227.         strcpy( p, p+2 );
  228.      else
  229.         p++;
  230.  
  231.       if( !(flags & M_LITERAL) ) {
  232.      p = DmStrDup( DmStrSpn(value," \t")); /* strip white space before */
  233.                            /* ... and after value       */
  234.      if( *p ) {
  235.         for(q=p+strlen(p)-1; ((*q == ' ')||(*q == '\t')); q--);
  236.         *++q = '\0';
  237.      }
  238.      flags &= ~M_LITERAL;
  239.       }
  240.       else
  241.      p = DmStrDup( value );               /* take string literally   */
  242.       
  243.       if( !*p )    {                /* check if result is ""   */
  244.          FREE( p );
  245.          p = NIL(char);
  246.      flags |= M_EXPANDED;
  247.       }
  248.       else if( *DmStrPbrk( p, "${}" ) == '\0' )
  249.      flags |= M_EXPANDED;
  250.  
  251.       hp->ht_value = p;
  252.    }
  253.    else
  254.       hp->ht_value = NIL(char);
  255.  
  256.    /* Assign the hash table flag less the M_MULTI flag, it is used only
  257.     * to silence the warning.  But carry it over if it was previously
  258.     * defined in ht_flag, as this is a permanent M_MULTI variable. */
  259.  
  260.    hp->ht_flag = (flags & ~(M_MULTI|M_FORCE)) |
  261.          (hp->ht_flag & (M_VAR_MASK | M_MULTI));
  262.    
  263.    /* Check for macro variables and make the necessary adjustment in the
  264.     * corresponding global variables */
  265.     
  266.    if( hp->ht_flag & M_VAR_MASK )
  267.       if( !(flags & M_EXPANDED) )
  268.      Error( "Macro variable '%s' must be assigned with :=", name );
  269.       else switch( hp->ht_flag & M_VAR_MASK )    /* only one var type per var */
  270.       {
  271.      case M_VAR_STRING:
  272.         *hp->MV_SVAR = hp->ht_value;
  273.         break;
  274.  
  275.      case M_VAR_CHAR:
  276.         *hp->MV_CVAR = (hp->ht_value == NIL(char)) ? '\0':*hp->ht_value;
  277.         break;
  278.   
  279.      case M_VAR_INT: {
  280.         int tvalue;
  281.         if( hp->MV_IVAR == NIL(int) ) break;    /* first time */
  282.  
  283.         tvalue = atoi(hp->ht_value);
  284.         if( hp->MV_IVAR == &Buffer_size ) {
  285.            /* If Buffer_size is modified then make sure you change the
  286.             * size of the real buffer as well. */
  287.            tvalue = (tvalue < (BUFSIZ-2)) ? BUFSIZ : tvalue+2;
  288.            if( Buffer_size == tvalue ) break;
  289.            if( Buffer ) FREE(Buffer);
  290.            if((Buffer=MALLOC(tvalue, char)) == NIL(char)) No_ram();
  291.            *Buffer = '\0';
  292.         }
  293.         *hp->MV_IVAR = tvalue;
  294.  
  295.         if( hp->MV_IVAR == &Max_proc || hp->MV_IVAR == &Max_proclmt ) {
  296.            if( tvalue < 1 )
  297.           Fatal( "Process limit value must be > 1" );
  298.  
  299.            if( Max_proc > Max_proclmt )
  300.           Fatal( "Specified # of processes exceeds limit of [%d]",
  301.              Max_proclmt );
  302.         }
  303.      } break;
  304.  
  305.      case M_VAR_BIT:
  306.         /* Bit variables are set to 1 if ht_value is not NULL and 0
  307.          * otherwise */
  308.          
  309.         if( hp->ht_value == NIL(char) )
  310.            *hp->MV_BVAR &= ~hp->MV_MASK;
  311.         else
  312.            *hp->MV_BVAR |= hp->MV_MASK;
  313.         break;
  314.       }
  315.    
  316.    DB_RETURN( hp );
  317. }
  318.  
  319.  
  320.  
  321. PUBLIC CELLPTR
  322. Def_cell( name )/*
  323. ==================
  324.    Take a string passed in and define it as a cell
  325.    If the cell exists then return a pointer to it. */
  326. char    *name;
  327. {
  328.    register HASHPTR  hp;
  329.    register CELLPTR  cp;
  330.    register CELLPTR  lib;
  331.    char             *member;
  332.    char             *end;
  333.  
  334.    DB_ENTER( "Def_cell" );
  335.  
  336.    /* Check to see if the cell is a member of the form lib(member) or
  337.     * lib((symbol)) and handle the cases appropriately.
  338.     * What we do is we look at the target, if it is of the above two
  339.     * forms we get the lib, and add the member/symbol to the list of
  340.     * prerequisites for the library.  If this is a symbol name def'n
  341.     * we additionally add the attribute A_SYMBOL, so that stat can
  342.     * try to do the right thing.  */
  343.  
  344.    if( ((member = strchr(name, '('))     != NIL(char)) &&
  345.        ((end    = strrchr(member,  ')')) != NIL(char)) &&
  346.        (member > name) && (member[-1] != '$') &&
  347.        (end > member+1)  && (end[1] == '\0') )
  348.    {
  349.       *member++ = *end = '\0';
  350.  
  351.       if( (*member == '(') && (member[strlen(member)-1] == ')') ) {
  352.      member[ strlen(member)-1 ] = '\0';
  353.      cp = Def_cell( member+1 );
  354.      cp->ce_attr |= A_SYMBOL;
  355.       }
  356.       else
  357.      cp = Def_cell( member );
  358.  
  359.       lib  = Def_cell( name );
  360.  
  361.       Add_prerequisite( lib, cp, FALSE, FALSE );
  362.       lib->ce_attr |= A_LIBRARY | A_COMPOSITE;
  363.  
  364.       if( !Def_targets ) cp = lib;
  365.    }
  366.    else {
  367.       hp = Get_name( name, Defs, TRUE );/* get the name from hash table */
  368.  
  369.       if( hp->CP_OWNR == NIL(CELL) )    /* was it previously defined    */
  370.       {                    /* NO, so define a new cell    */
  371.      DB_PRINT( "cell", ("Defining cell [%s]", name) );
  372.  
  373.      TALLOC( cp, 1, CELL );
  374.      hp->CP_OWNR = cp;
  375.      cp->ce_name = hp;
  376.      cp->ce_fname = hp->ht_name;
  377.      cp->ce_all.cl_prq = cp;
  378.       }
  379.       else                 /* YES, so return the old cell    */
  380.       {
  381.      DB_PRINT( "cell", ("Getting cell [%s]", hp->ht_name) );
  382.      cp = hp->CP_OWNR;
  383.       }
  384.    }
  385.  
  386.    DB_RETURN( cp );
  387. }
  388.  
  389.  
  390.  
  391.  
  392. PUBLIC LINKPTR
  393. Add_prerequisite( cell, prq, head, force )/*
  394. ============================================
  395.     Add a dependency node to the dag.  It adds it to the prerequisites,
  396.     if any, of the cell and makes certain they are in linear order.
  397.     If head == 1, then add to head of the prerequisite list, else
  398.     add to tail. */
  399. CELLPTR cell;
  400. CELLPTR prq;
  401. int     head;
  402. int     force;
  403. {
  404.    register LINKPTR lp, tlp;
  405.  
  406.    DB_ENTER( "Add_prerequisite" );
  407.    DB_PRINT( "cell", ("Defining prerequisite %s", prq->CE_NAME) );
  408.  
  409.    if( (prq->ce_flag & (F_MAGIC | F_PERCENT)) && !force )
  410.       Fatal( "Special target [%s] cannot be a prerequisite",
  411.              prq->CE_NAME );
  412.  
  413.    if( cell->ce_prq == NIL(LINK) ) {    /* it's the first one    */
  414.       TALLOC( lp, 1, LINK );
  415.       lp->cl_prq   = prq;
  416.       cell->ce_prq = lp;
  417.    }
  418.    else    {    /* search the list, checking for duplicates    */
  419.       for( lp = cell->ce_prq;
  420.        (lp->cl_next != NIL(LINK)) && (lp->cl_prq != prq);
  421.        lp = lp->cl_next );
  422.  
  423.       /* If the prq is not found and we are at the last prq in the list,
  424.        * allocate a new prq and place it into the list, insert it at the
  425.        * head if head == 1, else we add it to the end. */
  426.  
  427.       if( lp->cl_prq != prq ) {
  428.      TALLOC( tlp, 1, LINK );
  429.      tlp->cl_prq = prq;
  430.  
  431.      if( head ) {
  432.         tlp->cl_next = cell->ce_prq;
  433.         cell->ce_prq = tlp;
  434.      }
  435.      else
  436.         lp->cl_next  = tlp;
  437.  
  438.      lp = tlp;
  439.       }
  440.    }
  441.  
  442.    DB_RETURN( lp );
  443. }
  444.  
  445.  
  446.  
  447. PUBLIC void
  448. Clear_prerequisites( cell )/*
  449. =============================
  450.     Clear out the list of prerequisites, freeing all of the LINK nodes,
  451.     and setting the list to NULL */
  452. CELLPTR cell;
  453. {
  454.    LINKPTR lp, tlp;
  455.  
  456.    DB_ENTER( "Clear_prerequisites" );
  457.    DB_PRINT( "cell", ("Nuking prerequisites") );
  458.  
  459.    if( cell == NIL(CELL) ) { DB_VOID_RETURN; }
  460.  
  461.    for( lp=cell->ce_prq; lp != NIL(LINK); lp=tlp ) {
  462.       tlp=lp->cl_next;
  463.       FREE( lp );
  464.    }
  465.  
  466.    cell->ce_prq = NIL(LINK);
  467.  
  468.    DB_VOID_RETURN;
  469. }
  470.  
  471.  
  472. PUBLIC int
  473. Test_circle( cp, fail )/*
  474. =========================
  475.     Actually run through the graph */
  476. CELLPTR cp;
  477. int     fail;
  478. {
  479.    register LINKPTR lp;
  480.    int res = 0;
  481.  
  482.    DB_ENTER( "Test_circle" );
  483.    DB_PRINT( "tc", ("checking [%s]", cp->CE_NAME) );
  484.  
  485.    if( cp->ce_flag & F_MARK )
  486.       if( fail )
  487.      Fatal("Detected circular dependency in graph at [%s]", cp->CE_NAME);
  488.       else
  489.      DB_RETURN( 1 );
  490.  
  491.    cp->ce_flag |= F_MARK;
  492.    for( lp = cp->ce_prq; !res && lp != NIL(LINK); lp = lp->cl_next )
  493.       res = Test_circle( lp->cl_prq, fail );
  494.    cp->ce_flag ^= F_MARK;
  495.  
  496.    DB_RETURN( res );
  497. }
  498.  
  499.  
  500.  
  501. PUBLIC STRINGPTR
  502. Def_recipe( rcp, sp, white_too, no_check )/*
  503. =============================================
  504.     Take the recipe and add it to the list of recipes
  505.     pointed to by sp.  sp points to the last element.
  506.     return a pointer to the new recipe.  If white_too == TRUE add the
  507.     recipe even if it contains only white space.
  508.     If no_check is true then don't look for -@ at the start of the
  509.     recipe line. */
  510. char      *rcp;
  511. STRINGPTR sp;
  512. int       white_too;
  513. int       no_check;
  514. {
  515.    register STRINGPTR nsp;
  516.    register char      *rp;
  517.    register char      *dm;
  518.  
  519.    DB_ENTER( "Def_recipe" );
  520.    DB_PRINT( "rul", ("Defining recipe %s", rcp) );
  521.  
  522.    if( !white_too ) rcp = DmStrSpn( rcp, " \t" );
  523.    if( (rcp == NIL(char)) || (*rcp == 0 && !white_too) )
  524.       DB_RETURN( sp );         /* return last recipe when new recipe not added */
  525.  
  526.    rp = no_check ? rcp : DmStrSpn( rcp, " \t@-+%" );
  527.  
  528.    TALLOC(nsp, 1, STRING);
  529.    nsp->st_string = DmStrDup( rp );
  530.  
  531.    if( sp != NIL(STRING) ) sp->st_next = nsp;
  532.    nsp->st_next = NIL(STRING);
  533.  
  534.    if( !no_check ) nsp->st_attr |= Rcp_attribute( rcp );
  535.  
  536.    DB_RETURN( nsp );
  537. }
  538.  
  539.  
  540. PUBLIC t_attr
  541. Rcp_attribute( rp )/*
  542. ======================
  543.    Look at the recipe and return the set of attributes that it defines. */
  544. char *rp;
  545. {
  546.    t_attr flag = A_DEFAULT;
  547.    int    done = FALSE;
  548.  
  549.    while( !done )
  550.       switch( *rp++ )
  551.       {
  552.      case '@' : flag |= A_SILENT; break;
  553.      case '-' : flag |= A_IGNORE; break;
  554.      case '+' : flag |= A_SHELL;  break;
  555.      case '%' : flag |= A_SWAP;   break;
  556.  
  557.      case ' ' :
  558.      case '\t': break;
  559.  
  560.      default: done = TRUE; break;
  561.       }
  562.  
  563.    return(flag);
  564. }
  565.