home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8712 / mkmf / 2 / src / rule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  4.6 KB  |  190 lines

  1. /* $Header: rule.c,v 1.2 85/05/16 12:49:56 nicklin Exp $ */
  2.  
  3. /*
  4.  * Author: Peter J. Nicklin
  5.  */
  6. #include "Mkmf.h"
  7. #include "null.h"
  8. #include "rule.h"
  9. #include "slist.h"
  10. #include "suffix.h"
  11. #include "system.h"
  12. #include "yesno.h"
  13.  
  14. #define MAXNAMLEN    255
  15.  
  16. static RULEBLK *Ruletab[RULETABSIZE];    /* rules table */
  17. static SLIST *Rulelist = NULL;        /* transformation rule list */
  18.  
  19. /*
  20.  * buildruletable() converts a list of transformation rules into a hash table
  21.  * for fast lookup. Returns YES if successful, otherwise NO.
  22.  */
  23. buildruletable()
  24. {
  25.     extern char *DEFRULE[];        /* default preprocessor rules */
  26.     int i;                /* default rule list counter */
  27.     int instalrule();        /* instale rule in hash table */
  28.     SLBLK *rblk;            /* singly-linked rulename block */
  29.  
  30.     /* process default rules */
  31.     for (i = 0; DEFRULE[i] != NULL; i++)
  32.         {
  33.         if (instalrule(DEFRULE[i]) == NO)
  34.             {
  35.             warn("out of memory");
  36.             return(NO);
  37.             }
  38.         }
  39.  
  40.     /* process rules found in makefile */
  41.     if (Rulelist != NULL)
  42.         {
  43.         for (rblk = Rulelist->head; rblk != NULL; rblk = rblk->next)
  44.             {
  45.             if (instalrule(rblk->key) == NO)
  46.                 {
  47.                 warn("out of memory");
  48.                 return(NO);
  49.                 }
  50.             }
  51.         }
  52.     return(YES);
  53. }
  54.  
  55.  
  56.  
  57. /*
  58.  * findrule() searchs a line for a transformation rule. Returns the
  59.  * name of the transformation rule, or NULL if not found.
  60.  */
  61. char *
  62. findrule(rulename, bp)
  63.     char *rulename;            /* transformation rule buffer */
  64.     register char *bp;        /* I/O buffer pointer */
  65.     register char *rp;        /* rule name pointer */
  66.     int dotcount = 0;        /* number of '.'s in rule */
  67.  
  68.     for (rp = rulename; *bp != ':' && *bp != ' ' && *bp != '\t'; rp++, bp++)
  69.         {
  70.         if ((*rp = *bp) == '.')
  71.             dotcount++;
  72.         }
  73.     *rp = '\0';
  74.  
  75.     /* eat up white space between rule and ':' */
  76.     if (*bp != ':')
  77.         {
  78.         while (*bp == ' ' || *bp == '\t')
  79.             bp++;
  80.         if (*bp != ':')
  81.             return(NULL);
  82.         }
  83.  
  84.     return((dotcount == 2) ? rulename : NULL);
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  * instalrule() installs a source transformation rule in the rule lookup
  91.  * table. The rule table consists of a set of singly-linked lists, indexed
  92.  * by the first character of the suffix of the target file. The index of
  93.  * the target file is used by lookuprule() to find out the name of the file
  94.  * from which it was derived. Returns YES if successful, otherwise NO.
  95.  */
  96. instalrule(rule)
  97.     char *rule;            /* rule to be installed in Rule table */
  98. {
  99.     char *malloc();            /* memory allocator */
  100.     char *rindex();            /* find last occurrence of character */
  101.     char *strsav();            /* save a string somewhere */
  102.     char *target;            /* target suffix */
  103.     int lookupsfx();        /* get suffix type */
  104.     int ruleindex;            /* index into rule table */
  105.     RULEBLK *rblk;            /* rule list block */
  106.  
  107.     target = rindex(rule, '.') + 1;
  108.     if (lookupsfx(target) == SFXSRC)
  109.         {
  110.         ruleindex = target[0];
  111.         if ((rblk = (RULEBLK *) malloc(sizeof(RULEBLK))) == NULL)
  112.             return(NO);
  113.         if ((rblk->r_rule = strsav(rule)) == NULL)
  114.             return(NO);
  115.         rblk->r_next = Ruletab[ruleindex];
  116.         Ruletab[ruleindex] = rblk;
  117.         }
  118.     return(YES);
  119. }
  120.  
  121.  
  122.  
  123. /*
  124.  * lookuprule() applies successive transformation rules to filename, and
  125.  * checks to see if the file exists. Returns YES if filename exists,
  126.  * otherwise NO.
  127.  */
  128. lookuprule(target, source)
  129.     char *target;            /* name of (transformed) file */
  130.     char *source;            /* name of source file */
  131. {
  132.     register char *r;        /* rule pointer */
  133.     register char *s;        /* source buffer pointer */
  134.     char *rindex();            /* find last occurrence of character */
  135.     char *sourcesuffix;        /* source file suffix */
  136.     char *strcpy();            /* string copy */
  137.     char *rulesuffix;        /* target suffix in each rule */
  138.     char *targetsuffix;        /* transformed file suffix string */
  139.     int ruleindex;            /* index into rule table */
  140.     int strcmp();            /* string comparison */
  141.     RULEBLK *rblk;            /* rule list block */
  142.  
  143.     if ((targetsuffix = rindex(target, '.')) == NULL)
  144.         return(NO);
  145.     ruleindex = targetsuffix[1];
  146.     if (Ruletab[ruleindex] != NULL)
  147.         {
  148.         strcpy(source, target);
  149.         sourcesuffix = rindex(source, '.');
  150.         for (rblk=Ruletab[ruleindex]; rblk != NULL; rblk=rblk->r_next)
  151.             {
  152.             rulesuffix = rindex(rblk->r_rule, '.');
  153.             if (strcmp(rulesuffix, targetsuffix) == 0)
  154.                 {
  155.                 r = rblk->r_rule;
  156.                 s = sourcesuffix;
  157.                 while (*++s = *++r)
  158.                     if (*s == '.')
  159.                         {
  160.                         *s = '\0';
  161.                         break;
  162.                         }
  163.                 if (FILEXIST(source))
  164.                     return(YES);
  165.                 }
  166.             }
  167.         }
  168.     return(NO);
  169. }
  170.  
  171.  
  172.  
  173. /*
  174.  * storerule() appends a transformation rule to the end of a singly-linked
  175.  * list. Returns integer NO if out of memory, otherwise YES.
  176.  */
  177. storerule(rulename)
  178.     char *rulename;            /* transformation rule name */
  179. {
  180.     char *slappend();        /* append rule to list */
  181.     SLIST *slinit();        /* initialize transformation list */
  182.  
  183.     if (Rulelist == NULL)
  184.         Rulelist = slinit();
  185.     if (slappend(rulename, Rulelist) == NULL)
  186.         return(NO);
  187.     return(YES);
  188. }
  189.