home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / DAG.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  14KB  |  498 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/dag.c,v 1.1 1992/01/24 03:26:55 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  1992/01/24  03:26:55  dvadura
  32.  * dmake Version 3.8, 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  = _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|M_EXPANDED;
  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.    /* Make sure we don't export macros whose names contain legal macro
  147.     * assignment operators, since we can't do proper quoting in the
  148.     * environment. */
  149.    if( *_strpbrk(name, "*+:=") != '\0' ) flags |= M_NOEXPORT;
  150.  
  151.    if( hp->ht_value != NIL(char) ) FREE( hp->ht_value );
  152.  
  153.    if( (hp->ht_flag & M_USED) && !((flags | hp->ht_flag) & M_MULTI) )
  154.       Warning( "Macro `%s' redefined after use", name );
  155.  
  156.    if( (value != NIL(char)) && (*value) ) {
  157.       /* strip out any \<nl> combinations where \ is the current CONTINUATION
  158.        * char */
  159.  
  160.       for( p = value; (p = strchr(p, CONTINUATION_CHAR)) != NIL(char); )
  161.          if( p[1] == '\n' )
  162.         strcpy( p, p+2 );
  163.      else
  164.         p++;
  165.  
  166.       if( !(flags & M_LITERAL) ) {
  167.      p = _strdup( _strspn( value, " \t" ) ); /* strip white space before */
  168.                          /* ... and after value         */
  169.      if( *p ) {
  170.         for(q=p+strlen(p)-1; ((*q == ' ')||(*q == '\t')); q--);
  171.         *++q = '\0';
  172.      }
  173.      flags &= ~M_LITERAL;
  174.       }
  175.       else
  176.      p = _strdup( value );               /* take string literally   */
  177.       
  178.       if( !*p )    {                /* check if result is ""   */
  179.          FREE( p );
  180.          p = NIL(char);
  181.      flags |= M_EXPANDED;
  182.       }
  183.       else if( *_strpbrk( p, "${}" ) == '\0' )
  184.      flags |= M_EXPANDED;
  185.  
  186.       hp->ht_value = p;
  187.    }
  188.    else
  189.       hp->ht_value = NIL(char);
  190.  
  191.    /* Assign the hash table flag less the M_MULTI flag, it is used only
  192.     * to silence the warning.  But carry it over if it was previously
  193.     * defined in ht_flag, as this is a permanent M_MULTI variable. */
  194.  
  195.    hp->ht_flag = (flags & ~(M_MULTI|M_FORCE)) |
  196.          (hp->ht_flag & (M_VAR_MASK | M_MULTI));
  197.    
  198.    /* Check for macro variables and make the necessary adjustment in the
  199.     * corresponding global variables */
  200.     
  201.    if( hp->ht_flag & M_VAR_MASK )
  202.       if( !(flags & M_EXPANDED) )
  203.      Error( "Macro variable '%s' must be assigned with :=", name );
  204.       else switch( hp->ht_flag & M_VAR_MASK )    /* only one var type per var */
  205.       {
  206.      case M_VAR_STRING:
  207.         *hp->MV_SVAR = hp->ht_value;
  208.         break;
  209.  
  210.      case M_VAR_CHAR:
  211.         *hp->MV_CVAR = (hp->ht_value == NIL(char)) ? '\0':*hp->ht_value;
  212.         break;
  213.   
  214.      case M_VAR_INT: {
  215.         int tvalue;
  216.         if( hp->MV_IVAR == NIL(int) ) break;    /* first time */
  217.  
  218.         tvalue = atoi(hp->ht_value);
  219.         if( hp->MV_IVAR == &Buffer_size ) {
  220.            /* If Buffer_size is modified then make sure you change the
  221.             * size of the real buffer as well. */
  222.            tvalue = (tvalue < (BUFSIZ-2)) ? BUFSIZ : tvalue+2;
  223.            if( Buffer_size == tvalue ) break;
  224.            if( Buffer ) FREE(Buffer);
  225.            if((Buffer=MALLOC(tvalue, char)) == NIL(char)) No_ram();
  226.            *Buffer = '\0';
  227.         }
  228.         *hp->MV_IVAR = tvalue;
  229.  
  230.         if( hp->MV_IVAR == &Max_proc || hp->MV_IVAR == &Max_proclmt ) {
  231.            if( tvalue < 1 )
  232.           Fatal( "Process limit value must be > 1" );
  233.  
  234.            if( Max_proc > Max_proclmt )
  235.           Fatal( "Specified # of processes exceeds limit of [%d]",
  236.              Max_proclmt );
  237.         }
  238.      } break;
  239.  
  240.      case M_VAR_BIT:
  241.         /* Bit variables are set to 1 if ht_value is not NULL and 0
  242.          * otherwise */
  243.          
  244.         if( hp->ht_value == NIL(char) )
  245.            *hp->MV_BVAR &= ~hp->MV_MASK;
  246.         else
  247.            *hp->MV_BVAR |= hp->MV_MASK;
  248.         break;
  249.       }
  250.    
  251.    DB_RETURN( hp );
  252. }
  253.  
  254.  
  255.  
  256. PUBLIC CELLPTR
  257. Def_cell( name )/*
  258. ==================
  259.    Take a string passed in and define it as a cell
  260.    If the cell exists then return a pointer to it. */
  261. char    *name;
  262. {
  263.    register HASHPTR  hp;
  264.    register CELLPTR  cp;
  265.    register CELLPTR  lib;
  266.    char             *member;
  267.    char             *end;
  268.  
  269.    DB_ENTER( "Def_cell" );
  270.  
  271.    /* Check to see if the cell is a member of the form lib(member) or
  272.     * lib((symbol)) and handle the cases appropriately.
  273.     * What we do is we look at the target, if it is of the above two
  274.     * forms we get the lib, and add the member/symbol to the list of
  275.     * prerequisites for the library.  If this is a symbol name def'n
  276.     * we additionally add the attribute A_SYMBOL, so that stat can
  277.     * try to do the right thing.  */
  278.  
  279.    if( ((member = strchr(name, '('))     != NIL(char)) &&
  280.        ((end    = strrchr(member,  ')')) != NIL(char)) &&
  281.        (member > name) && (member[-1] != '$') &&
  282.        (end > member+1)  && (end[1] == '\0') )
  283.    {
  284.       *member++ = *end = '\0';
  285.  
  286.       if( (*member == '(') && (member[strlen(member)-1] == ')') ) {
  287.      member[ strlen(member)-1 ] = '\0';
  288.      cp = Def_cell( member+1 );
  289.      cp->ce_attr |= A_SYMBOL;
  290.       }
  291.       else
  292.      cp = Def_cell( member );
  293.  
  294.       lib  = Def_cell( name );
  295.  
  296.       Add_prerequisite( lib, cp, FALSE, FALSE );
  297.       lib->ce_attr |= A_LIBRARY | A_COMPOSITE;
  298.  
  299.       if( !Def_targets ) cp = lib;
  300.    }
  301.    else {
  302.       hp = Get_name( name, Defs, TRUE );/* get the name from hash table */
  303.  
  304.       if( hp->CP_OWNR == NIL(CELL) )    /* was it previously defined    */
  305.       {                    /* NO, so define a new cell    */
  306.      DB_PRINT( "cell", ("Defining cell [%s]", name) );
  307.  
  308.      TALLOC( cp, 1, CELL );
  309.      hp->CP_OWNR = cp;
  310.      cp->ce_name = hp;
  311.      cp->ce_fname = hp->ht_name;
  312.       }
  313.       else                 /* YES, so return the old cell    */
  314.       {
  315.      DB_PRINT( "cell", ("Getting cell [%s]", hp->ht_name) );
  316.      cp = hp->CP_OWNR;
  317.       }
  318.    }
  319.  
  320.    DB_RETURN( cp );
  321. }
  322.  
  323.  
  324.  
  325.  
  326. PUBLIC LINKPTR
  327. Add_prerequisite( cell, prq, head, force )/*
  328. ============================================
  329.     Add a dependency node to the dag.  It adds it to the prerequisites,
  330.     if any, of the cell and makes certain they are in linear order.
  331.     If head == 1, then add to head of the prerequisite list, else
  332.     add to tail. */
  333. CELLPTR cell;
  334. CELLPTR prq;
  335. int     head;
  336. int     force;
  337. {
  338.    register LINKPTR lp, tlp;
  339.  
  340.    DB_ENTER( "Add_prerequisite" );
  341.    DB_PRINT( "cell", ("Defining prerequisite %s", prq->CE_NAME) );
  342.  
  343.    if( (prq->ce_flag & (F_MAGIC | F_PERCENT)) && !force )
  344.       Fatal( "Special target [%s] cannot be a prerequisite",
  345.              prq->CE_NAME );
  346.  
  347.    if( cell->ce_prq == NIL(LINK) ) {    /* it's the first one    */
  348.       TALLOC( lp, 1, LINK );
  349.       lp->cl_prq   = prq;
  350.       cell->ce_prq = lp;
  351.    }
  352.    else    {    /* search the list, checking for duplicates    */
  353.       for( lp = cell->ce_prq;
  354.        (lp->cl_next != NIL(LINK)) && (lp->cl_prq != prq);
  355.        lp = lp->cl_next );
  356.  
  357.       /* If the prq is not found and we are at the last prq in the list,
  358.        * allocate a new prq and place it into the list, insert it at the
  359.        * head if head == 1, else we add it to the end. */
  360.  
  361.       if( lp->cl_prq != prq ) {
  362.      TALLOC( tlp, 1, LINK );
  363.      tlp->cl_prq = prq;
  364.  
  365.      if( head ) {
  366.         tlp->cl_next = cell->ce_prq;
  367.         cell->ce_prq = tlp;
  368.      }
  369.      else
  370.         lp->cl_next  = tlp;
  371.  
  372.      lp = tlp;
  373.       }
  374.    }
  375.  
  376.    DB_RETURN( lp );
  377. }
  378.  
  379.  
  380.  
  381. PUBLIC void
  382. Clear_prerequisites( cell )/*
  383. =============================
  384.     Clear out the list of prerequisites, freeing all of the LINK nodes,
  385.     and setting the list to NULL */
  386. CELLPTR cell;
  387. {
  388.    LINKPTR lp, tlp;
  389.  
  390.    DB_ENTER( "Clear_prerequisites" );
  391.    DB_PRINT( "cell", ("Nuking prerequisites") );
  392.  
  393.    if( cell == NIL(CELL) ) { DB_VOID_RETURN; }
  394.  
  395.    for( lp=cell->ce_prq; lp != NIL(LINK); lp=tlp ) {
  396.       tlp=lp->cl_next;
  397.       FREE( lp );
  398.    }
  399.  
  400.    cell->ce_prq = NIL(LINK);
  401.  
  402.    DB_VOID_RETURN;
  403. }
  404.  
  405.  
  406. PUBLIC int
  407. Test_circle( cp, fail )/*
  408. =========================
  409.     Actually run through the graph */
  410. CELLPTR cp;
  411. int     fail;
  412. {
  413.    register LINKPTR lp;
  414.    int res = 0;
  415.  
  416.    DB_ENTER( "Test_circle" );
  417.    DB_PRINT( "tc", ("checking [%s]", cp->CE_NAME) );
  418.  
  419.    if( cp->ce_flag & F_MARK )
  420.       if( fail )
  421.      Fatal("Detected circular dependency in graph at [%s]", cp->CE_NAME);
  422.       else
  423.      DB_RETURN( 1 );
  424.  
  425.    cp->ce_flag |= F_MARK;
  426.    for( lp = cp->ce_prq; !res && lp != NIL(LINK); lp = lp->cl_next )
  427.       res = Test_circle( lp->cl_prq, fail );
  428.    cp->ce_flag ^= F_MARK;
  429.  
  430.    DB_RETURN( res );
  431. }
  432.  
  433.  
  434.  
  435. PUBLIC STRINGPTR
  436. Def_recipe( rcp, sp, white_too, no_check )/*
  437. =============================================
  438.     Take the recipe and add it to the list of recipes
  439.     pointed to by sp.  sp points to the last element.
  440.     return a pointer to the new recipe.  If white_too == TRUE add the
  441.     recipe even if it contains only white space.
  442.     If no_check is true then don't look for -@ at the start of the
  443.     recipe line. */
  444. char      *rcp;
  445. STRINGPTR sp;
  446. int       white_too;
  447. int       no_check;
  448. {
  449.    register STRINGPTR nsp;
  450.    register char      *rp;
  451.  
  452.    DB_ENTER( "Def_recipe" );
  453.    DB_PRINT( "rul", ("Defining recipe %s", rcp) );
  454.  
  455.    if( !white_too ) rcp = _strspn( rcp, " \t" );
  456.    if( (rcp == NIL(char)) || (*rcp == 0 && !white_too) )
  457.       DB_RETURN( sp );         /* return last recipe when new recipe not added */
  458.  
  459.    rp = no_check ? rcp : _strspn( rcp, " \t@-+%" );
  460.  
  461.    TALLOC( nsp, 1, STRING );
  462.    nsp->st_string = _strdup( rp );
  463.  
  464.    if( sp != NIL(STRING) ) sp->st_next = nsp;
  465.    nsp->st_next = NIL(STRING);
  466.  
  467.    if( !no_check ) nsp->st_attr |= Rcp_attribute( rcp );
  468.  
  469.    DB_RETURN( nsp );
  470. }
  471.  
  472.  
  473. PUBLIC t_attr
  474. Rcp_attribute( rp )/*
  475. ======================
  476.    Look at the recipe and return the set of attributes that it defines. */
  477. char *rp;
  478. {
  479.    t_attr flag = A_DEFAULT;
  480.    int    done = FALSE;
  481.  
  482.    while( !done )
  483.       switch( *rp++ )
  484.       {
  485.      case '@' : flag |= A_SILENT; break;
  486.      case '-' : flag |= A_IGNORE; break;
  487.      case '+' : flag |= A_SHELL;  break;
  488.      case '%' : flag |= A_SWAP;   break;
  489.  
  490.      case ' ' :
  491.      case '\t': break;
  492.  
  493.      default: done = TRUE; break;
  494.       }
  495.  
  496.    return(flag);
  497. }
  498.