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

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/RCS/state.c,v 1.1 1994/10/06 17:41:25 dvadura Exp $
  2. -- SYNOPSIS -- .KEEP_STATE state file management
  3. -- 
  4. -- DESCRIPTION
  5. --     Three routines to interface to the .KEEP_STATE state file.
  6. --
  7. --        Read_state()    - reads the state file if any.
  8. --        Write_state()    - writes the state file.
  9. --
  10. --        Check_state(cp,how) - checks an entry returns 0 or 1
  11. --                      and updates the entry.
  12. --
  13. -- AUTHOR
  14. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  15. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  16. --
  17. -- COPYRIGHT
  18. --      Copyright (c) 1992,1994 by Dennis Vadura.  All rights reserved.
  19. -- 
  20. --      This program is free software; you can redistribute it and/or
  21. --      modify it under the terms of the GNU General Public License
  22. --      (version 1), as published by the Free Software Foundation, and
  23. --      found in the file 'LICENSE' included with this distribution.
  24. -- 
  25. --      This program is distributed in the hope that it will be useful,
  26. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  27. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  28. --      GNU General Public License for more details.
  29. -- 
  30. --      You should have received a copy of the GNU General Public License
  31. --      along with this program;  if not, write to the Free Software
  32. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. --
  34. -- LOG
  35. --     $Log: state.c,v $
  36.  * Revision 1.1  1994/10/06  17:41:25  dvadura
  37.  * dmake Release Version 4.0, Initial revision
  38.  *
  39. */
  40.  
  41. #include "extern.h"
  42.  
  43. typedef struct se {
  44.    char        *st_name;        /* name of cell        */
  45.    uint32    st_nkey;        /* name hash key    */
  46.    int        st_count;        /* how count for how    */
  47.    uint32    st_dkey;        /* directory hash key    */
  48.    uint32    st_key;            /* hash key        */
  49.    struct se    *st_next;
  50. } KSTATE, *KSTATEPTR;
  51.  
  52. static KSTATEPTR _st_head  = NIL(KSTATE);
  53. static KSTATEPTR _st_tail  = NIL(KSTATE);
  54. static int       _st_upd   = FALSE;
  55. static char     *_st_file  = NIL(char);
  56.  
  57. static int  _my_fgets ANSI((char *, int, FILE *));
  58.  
  59. PUBLIC void
  60. Read_state()
  61. {
  62.    char *buf;
  63.    char sizeb[20];
  64.    int  size;
  65.    FILE *fp;
  66.    KSTATEPTR sp;
  67.  
  68.    if( (fp = Search_file(".KEEP_STATE", &_st_file)) != NIL(FILE) ) {
  69.       if( _my_fgets( sizeb, 20, fp ) ) {
  70.      size = atol(sizeb);
  71.      buf = MALLOC(size+2, char);
  72.  
  73.      while( _my_fgets(buf, size, fp) ) {
  74.         TALLOC(sp, 1, KSTATE);
  75.         sp->st_name = DmStrDup(buf);
  76.         (void) Hash(buf, &sp->st_nkey);
  77.  
  78.         if( _my_fgets(buf, size, fp) ) sp->st_count = atoi(buf);
  79.         if( _my_fgets(buf, size, fp) ) sp->st_dkey   = (uint32) atol(buf);
  80.  
  81.         if( _my_fgets(buf, size, fp) )
  82.            sp->st_key = (uint32) atol(buf);
  83.         else {
  84.            FREE(sp);
  85.            break;
  86.         }
  87.  
  88.         if( _st_head == NIL(KSTATE) )
  89.            _st_head = sp;
  90.         else
  91.            _st_tail->st_next = sp;
  92.  
  93.         _st_tail = sp;
  94.      }
  95.  
  96.      FREE(buf);
  97.       }
  98.  
  99.       Closefile();
  100.    }
  101. }
  102.  
  103.  
  104. PUBLIC void
  105. Write_state()
  106. {
  107.    static int in_write = 0;
  108.    register KSTATEPTR sp;
  109.    FILE *fp;
  110.  
  111.    if( !_st_upd || !_st_file || (_st_file && !*_st_file) ||
  112.        Trace || in_write ) return;
  113.  
  114.    in_write++;
  115.    if( (fp = Openfile(_st_file, TRUE, TRUE)) != NIL(FILE) ) {
  116.       int maxlen = 0;
  117.       int tmplen;
  118.  
  119.       for( sp = _st_head; sp; sp=sp->st_next )
  120.      if( (tmplen = strlen(sp->st_name)+2) > maxlen )
  121.         maxlen = tmplen;
  122.  
  123.       /* A nice arbitrary minimum size */
  124.       if( maxlen < 20 ) maxlen = 20;
  125.       fprintf( fp, "%d\n", maxlen );
  126.  
  127.       for( sp = _st_head; sp; sp=sp->st_next ) {
  128.      uint16 hv;
  129.      uint32 hk;
  130.  
  131.      if( Search_table(Defs, sp->st_name, &hv, &hk) ) {
  132.         fprintf( fp, "%s\n",  sp->st_name   );
  133.         fprintf( fp, "%d\n",  sp->st_count );
  134.         fprintf( fp, "%lu\n", sp->st_dkey   );
  135.         fprintf( fp, "%lu\n", sp->st_key    );
  136.      }
  137.       }
  138.  
  139.       Closefile();
  140.    }
  141.    else
  142.       Fatal("Cannot open STATE file %s", _st_file);
  143.  
  144.    in_write = 0;
  145. }
  146.  
  147.  
  148. PUBLIC int
  149. Check_state( cp, recipes, maxrcp )
  150. CELLPTR cp;
  151. STRINGPTR *recipes;
  152. int  maxrcp;
  153. {
  154.    KSTATEPTR  st;
  155.    STRINGPTR sp;
  156.    int    i;
  157.    uint32 thkey;
  158.    uint32 hkey;
  159.    uint32 nkey;
  160.    uint32 dkey;
  161.    int    update = FALSE;
  162.  
  163.    if( !_st_file || (_st_file && !*_st_file) || Trace )
  164.       return(FALSE);
  165.  
  166.    if(    strcmp(cp->CE_NAME,".REMOVE") == 0
  167.        || (cp->ce_attr & (A_PHONY|A_NOSTATE)) )
  168.       return(FALSE);
  169.  
  170.    (void) Hash( cp->CE_NAME, &nkey ); thkey = nkey + (uint32) cp->ce_count;
  171.    (void) Hash( Pwd,  &dkey ); thkey += dkey;
  172.  
  173.    Suppress_temp_file = TRUE;
  174.    for( i=0 ; i<maxrcp; i++ )
  175.       for(sp=recipes[i]; sp != NIL(STRING); sp=sp->st_next ) {
  176.      CELLPTR svct = Current_target;
  177.      char *cmnd;
  178.      t_attr silent = (Glob_attr & A_SILENT);
  179.  
  180.      Current_target = cp;
  181.      Glob_attr |= A_SILENT;
  182.      cmnd = Expand(sp->st_string);
  183.      Glob_attr = (Glob_attr & ~A_SILENT)|silent;
  184.      Current_target = svct;
  185.  
  186.      (void) Hash(cmnd, &hkey); thkey += hkey;
  187.      FREE(cmnd);
  188.       }
  189.    Suppress_temp_file = FALSE;
  190.  
  191.    for( st=_st_head; st != NIL(KSTATE); st=st->st_next ) {
  192.       if(    st->st_nkey   == nkey
  193.       && st->st_dkey   == dkey
  194.       && st->st_count  == cp->ce_count
  195.       && !strcmp(cp->CE_NAME, st->st_name) )
  196.      break;
  197.    }
  198.  
  199.    if( st == NIL(KSTATE) ) {
  200.       KSTATEPTR nst;
  201.  
  202.       TALLOC(nst, 1, KSTATE);
  203.       nst->st_name = cp->CE_NAME;
  204.       nst->st_nkey = nkey;
  205.       nst->st_dkey = dkey;
  206.       nst->st_key  = thkey;
  207.       nst->st_count = cp->ce_count;
  208.  
  209.       if( _st_head == NIL(KSTATE) )
  210.      _st_head = nst;
  211.       else
  212.      _st_tail->st_next = nst;
  213.  
  214.       _st_tail = nst;
  215.       _st_upd  = TRUE;
  216.    }
  217.    else if( st->st_key != thkey ) {
  218.       st->st_key = thkey;
  219.       _st_upd = update = TRUE;
  220.    }
  221.  
  222.    return(st != NIL(KSTATE) && update);
  223. }
  224.  
  225.  
  226. static int
  227. _my_fgets(buf, size, fp)
  228. char *buf;
  229. int  size;
  230. FILE *fp;
  231. {
  232.    char *p;
  233.  
  234.    if( fgets(buf, size, fp) == NULL ) return(0);
  235.  
  236.    if( (p=strrchr(buf,'\n')) != NIL(char) ) *p='\0';
  237.    return(1);
  238. }
  239.