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

  1. /* $Header: suffix.c,v 1.2 85/03/21 10:19:36 nicklin Exp $ */
  2.  
  3. /*
  4.  * Author: Peter J. Nicklin
  5.  */
  6. #include <stdio.h>
  7. #include "Mkmf.h"
  8. #include "hash.h"
  9. #include "macro.h"
  10. #include "null.h"
  11. #include "suffix.h"
  12. #include "yesno.h"
  13.  
  14. static int SFX1[SFXTABSIZE];        /* single character suffixes */
  15. static int INC1[SFXTABSIZE];        /* include file types for 1 char sfx */
  16. static SFXBLK *SFX2[SFXTABSIZE];    /* 2+ character suffixes */
  17.  
  18. /*
  19.  * buildsfxtable() converts a suffix list into a hash table for fast lookup.
  20.  * Returns YES if successful, otherwise NO.
  21.  */
  22. buildsfxtable()
  23. {
  24.     extern HASH *MDEFTABLE;        /* macro definition table */
  25.     extern SUFFIX DEFSFX[];        /* default suffix list */
  26.     HASHBLK *htb;            /* hash table block */
  27.     HASHBLK *htlookup();        /* find hash table entry */
  28.     int i;                /* suffix list counter */
  29.     int installsfx();        /* install suffix in hash table */
  30.     int sfxbuftotable();        /* feed suffixes to installsfx() */
  31.  
  32.     /* default suffix list */
  33.     for (i = 0; DEFSFX[i].suffix != NULL; i++)
  34.         if (installsfx(DEFSFX[i].suffix, DEFSFX[i].sfxtyp,
  35.             DEFSFX[i].inctyp) == NO)
  36.             return(NO);
  37.     
  38.     /* supplementary suffix definitions */
  39.     if ((htb = htlookup(MSUFFIX, MDEFTABLE)) != NULL)
  40.         {
  41.         if (sfxbuftotable(htb->h_def) == NO)
  42.             return(NO);
  43.         }
  44.     return(YES);
  45. }
  46.  
  47.  
  48.  
  49. /*
  50.  * installsfx() installs a suffix in one of two suffix tables: SFX1 for
  51.  * one character suffixes, and SFX2 for two or more character suffixes.
  52.  * For a suffix that already exists, only its type and corresponding
  53.  * included file type is updated. Returns integer YES if successful,
  54.  * otherwise NO.
  55.  */
  56. installsfx(suffix, sfxtyp, inctyp)
  57.     char *suffix;            /* suffix string */
  58.     int sfxtyp;            /* suffix type */
  59.     int inctyp;            /* include file type */
  60. {
  61.     char *malloc();            /* memory allocator */
  62.     char *strsav();            /* save a string somewhere */
  63.     int sfxindex;            /* index into suffix tables */
  64.     SFXBLK *sfxblk;            /* suffix list block */
  65.  
  66.     if (*suffix == '.')
  67.         suffix++;
  68.     sfxindex = suffix[0];
  69.     if (suffix[0] == '\0' || suffix[1] == '\0')
  70.         {
  71.         SFX1[sfxindex] = sfxtyp;    /* 0 or 1 character suffix */
  72.         INC1[sfxindex] = inctyp;
  73.         }
  74.     else    {                /* 2+ character suffix */
  75.         if ((sfxblk = (SFXBLK *) malloc(sizeof(SFXBLK))) == NULL)
  76.             return(NO);
  77.         if ((sfxblk->sfx.suffix = strsav(suffix)) == NULL)
  78.             return(NO);
  79.         sfxblk->sfx.sfxtyp = sfxtyp;
  80.         sfxblk->sfx.inctyp = inctyp;
  81.         sfxblk->next = SFX2[sfxindex];
  82.         SFX2[sfxindex] = sfxblk;
  83.         }
  84.     return(YES);
  85. }
  86.  
  87.  
  88.  
  89. /*
  90.  * lookuptypeofinclude() returns the include file type for suffix, or 0 if
  91.  * unknown suffix.
  92.  */
  93. lookuptypeofinclude(suffix)
  94.     char *suffix;            /* suffix string */
  95. {
  96.     SFXBLK *sfxblk;            /* suffix block pointer */
  97.  
  98.     if (suffix[0] == '\0' || suffix[1] == '\0')
  99.         return(INC1[*suffix]);        /* 0 or 1 char suffix */
  100.                         /* 2+ character suffix */
  101.     for (sfxblk = SFX2[*suffix]; sfxblk != NULL; sfxblk = sfxblk->next)
  102.         if (EQUAL(suffix, sfxblk->sfx.suffix))
  103.             return(sfxblk->sfx.inctyp);
  104.     return(0);
  105. }
  106.  
  107.  
  108.  
  109. /*
  110.  * lookupsfx() returns the suffix type, or 0 if unknown suffix.
  111.  */
  112. lookupsfx(suffix)
  113.     char *suffix;            /* suffix string */
  114. {
  115.     SFXBLK *sfxblk;            /* suffix block pointer */
  116.  
  117.     if (suffix[0] == '\0' || suffix[1] == '\0')
  118.         return(SFX1[*suffix]);        /* 0 or 1 char suffix */
  119.                         /* 2+ character suffix */
  120.     for (sfxblk = SFX2[*suffix]; sfxblk != NULL; sfxblk = sfxblk->next)
  121.         if (EQUAL(suffix, sfxblk->sfx.suffix))
  122.             return(sfxblk->sfx.sfxtyp);
  123.     return(0);
  124. }
  125.  
  126.  
  127.  
  128. /*
  129.  * sfxbuftotable() parses a buffer containing suffixes and presents them
  130.  * to installsfx() for installation into the appropriate hash table.
  131.  * The suffix type may be altered by attaching a modifier :suffixtype.
  132.  *    :h    --> header file type
  133.  *    :o    --> object file type
  134.  *    :s    --> source file type (default)
  135.  *    :x    --> executable file type
  136.  *    :    --> unknown file type
  137.  * The include file type may be altered by attaching an additional
  138.  * modifier includetype.
  139.  *    C    --> C source code
  140.  *    F    --> Fortran, Ratfor, Efl source code
  141.  *    P    --> Pascal source code
  142.  * If the suffix is object file type, the OBJSFX default object suffix
  143.  * is modified accordingly. Returns YES if successful, otherwise NO.
  144.  */
  145. sfxbuftotable(sfxbuf)
  146.     char *sfxbuf;            /* buffer conatining suffixes */
  147. {
  148.     extern char OBJSFX[];        /* object file name suffix */
  149.     char *gettoken();        /* get next token */
  150.     char *rindex();            /* find last occurrence of character */
  151.     char *sfxtyp;            /* suffix type */
  152.     char *strcpy();            /* string copy */
  153.     char suffix[SUFFIXSIZE+2];    /* suffix + modifier */
  154.     int installsfx();        /* install suffix in hash table */
  155.  
  156.     while ((sfxbuf = gettoken(suffix, sfxbuf)) != NULL)
  157.         if ((sfxtyp = rindex(suffix, ':')) == NULL)
  158.             {
  159.             if (installsfx(suffix, SFXSRC, INCLUDE_NONE) == NO)
  160.                 return(NO);
  161.             }
  162.         else    {
  163.             *sfxtyp = '\0';
  164.             if (installsfx(suffix, sfxtyp[1], sfxtyp[2]) == NO)
  165.                 return(NO);
  166.             if (sfxtyp[1] == SFXOBJ)
  167.                 strcpy(OBJSFX, suffix);
  168.             }
  169.     return(YES);
  170. }
  171.