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

  1. /* $Header: buildlist.c,v 1.3 86/01/12 00:49:30 lepreau Exp $ */
  2.  
  3. /*
  4.  * Author: Peter J. Nicklin
  5.  */
  6. #include <sys/types.h>
  7. #include <sys/dir.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include "Mkmf.h"
  11. #include "hash.h"
  12. #include "null.h"
  13. #include "path.h"
  14. #include "slist.h"
  15. #include "suffix.h"
  16. #include "system.h"
  17. #include "yesno.h"
  18.  
  19. /*
  20.  * buftolist() copies the items from a buffer to a singly-linked list.
  21.  * Returns integer YES if successful, otherwise NO.
  22.  */
  23. buftolist(buf, list)
  24.     char *buf;            /* item buffer */
  25.     SLIST *list;            /* receiving list */
  26. {
  27.     char *gettoken();        /* get next token */
  28.     char *slappend();        /* append file name to list */
  29.     char token[MAXNAMLEN];        /* item buffer */
  30.  
  31.     while ((buf = gettoken(token, buf)) != NULL)
  32.         {
  33.         if (slappend(token, list) == NULL)
  34.             return(NO);
  35.         }
  36.     return(YES);
  37. }
  38.  
  39.  
  40.  
  41. /*
  42.  * buildliblist() reads library pathnames from the LIBLIST macro
  43.  * definition, and adds them to the library pathname list. Libraries
  44.  * may be specified as `-lx'. Returns integer YES if successful,
  45.  * otherwise NO.
  46.  */
  47. buildliblist()
  48. {
  49.     extern SLIST *LIBLIST;        /* library pathname list */
  50.     extern HASH *MDEFTABLE;        /* macro definition table */
  51.     HASHBLK *htb;            /* hash table block */
  52.     HASHBLK *htlookup();        /* find hash table entry */
  53.     int libbuftolist();        /* load library pathnames into list */
  54.     SLIST *slinit();        /* initialize singly-linked list */
  55.     void htrm();            /* remove hash table entry */
  56.  
  57.     LIBLIST = NULL;
  58.     if ((htb = htlookup(MLIBLIST, MDEFTABLE)) != NULL)
  59.         {
  60.         LIBLIST = slinit();
  61.         if (libbuftolist(htb->h_def, LIBLIST) == NO)
  62.             return(NO);
  63.         }
  64.     htrm(MLIBLIST, MDEFTABLE);
  65.     return(YES);
  66. }
  67.  
  68.  
  69.  
  70. /*
  71.  * buildsrclist() takes source and header file names from command line
  72.  * macro definitions or the current directory and appends them to source
  73.  * or header file name lists as appropriate. Returns integer YES if
  74.  * if successful, otherwise NO.
  75.  */
  76. buildsrclist()
  77. {
  78.     extern HASH *MDEFTABLE;        /* macro definition table */
  79.     extern SLIST *HEADLIST;        /* header file name list */
  80.     extern SLIST *SRCLIST;        /* source file name list */
  81.     char *slappend();        /* append file name to list */
  82.     HASHBLK *headhtb;        /* HEADERS macro hash table block */
  83.     HASHBLK *htlookup();        /* find hash table entry */
  84.     HASHBLK *srchtb;        /* SOURCE macro hash table block */
  85.     int buftolist();        /* copy items from buffer to list */
  86.     int needheaders = 1;        /* need header file names */
  87.     int needsource = 1;        /* need source file names */
  88.     int read_dir();            /* read dir for source and headers */
  89.     int slsort();            /* sort singly-linked list */
  90.     int strcmp();            /* string comparison */
  91.     SLIST *slinit();        /* initialize singly-linked list */
  92.  
  93.     HEADLIST = slinit();
  94.     SRCLIST = slinit();
  95.  
  96.     /* build lists from command line macro definitions */
  97.     if ((headhtb = htlookup(MHEADERS, MDEFTABLE)) != NULL)
  98.         {
  99.         if (buftolist(headhtb->h_def, HEADLIST) == NO)
  100.             return(NO);
  101.         needheaders--;
  102.         }
  103.     if ((srchtb = htlookup(MSOURCE, MDEFTABLE)) != NULL)
  104.         {
  105.         if (buftolist(srchtb->h_def, SRCLIST) == NO)
  106.             return(NO);
  107.         needsource--;
  108.         }
  109.     
  110.     /* read the current directory to get source and header file names */
  111.     if (needheaders || needsource)
  112.         if (read_dir(needheaders, needsource) == NO)
  113.             return(NO);
  114.  
  115.     if (slsort(strcmp, SRCLIST) == NO)
  116.         return(NO);
  117.     if (slsort(strcmp, HEADLIST) == NO)
  118.         return(NO);
  119.     return(YES);
  120. }
  121.  
  122.  
  123.  
  124. /*
  125.  * expandlibpath() converts a library file specified by `-lx' into a full
  126.  * pathname. /lib and /usr/lib are searched for the library in the form
  127.  * libx.a. An integer YES is returned if the library was found, otherwise NO.
  128.  * A library file which doesn't begin with `-' is left unchanged.
  129.  */
  130. expandlibpath(libpath)
  131.     char *libpath;            /* library pathname buffer */
  132. {
  133.     char *lib;            /* /lib library pathname template */
  134.     char *strcpy();            /* string copy */
  135.     char *usrlib;            /* /usr/lib library pathname template */
  136.     int i;                /* library pathname index */
  137.  
  138. #ifndef ORIGINAL
  139.     return (YES);
  140. #endif !ORIGINAL
  141.  
  142.     lib = "/lib/libxxxxxxxxxxxxxxxxxxxxxxxxx";
  143.     usrlib = "/usr/lib/libxxxxxxxxxxxxxxxxxxxxxxxxx";
  144.  
  145.     if (libpath[0] == '-' && libpath[1] == 'l')
  146.         {
  147.         for (i = 0; libpath[i+2] != '\0' && i < 22; i++)
  148.             {
  149.             lib[i+8] = libpath[i+2];
  150.             usrlib[i+12] = libpath[i+2];
  151.             }
  152.         lib[i+8] = usrlib[i+12] = '.';
  153.         lib[i+9] = usrlib[i+13] = 'a';
  154.         lib[i+10] = usrlib[i+14] = '\0';
  155.         if (FILEXIST(lib))
  156.             {
  157.             strcpy(libpath, lib);
  158.             return(YES);
  159.             }
  160.         else if (FILEXIST(usrlib))
  161.             {
  162.             strcpy(libpath, usrlib);
  163.             return(YES);
  164.             }
  165.         else
  166.             return(NO);
  167.         }
  168.     return(YES);
  169. }
  170.  
  171.  
  172.  
  173. /*
  174.  * libbuftolist() appends each library pathname specified in libbuf to
  175.  * the liblist library pathname list.
  176.  */
  177. libbuftolist(libbuf, liblist)
  178.     char *libbuf;            /* library pathname buffer */
  179.     SLIST *liblist;            /* library pathname list */
  180. {
  181.     char *gettoken();        /* get next token */
  182.     char libpath[PATHSIZE];        /* library file pathname */
  183.     char *slappend();        /* append file name to list */
  184.     int expandlibpath();        /* -lx -> full library pathname */
  185.  
  186.     while ((libbuf = gettoken(libpath, libbuf)) != NULL)
  187.         {
  188.         if (expandlibpath(libpath) == NO)
  189.             {
  190.             warns("can't find library %s", libpath);
  191.             return(NO);
  192.             }
  193.         if (slappend(libpath, liblist) == NULL)
  194.             return(NO);
  195.         }
  196.     return(YES);
  197. }
  198.  
  199.  
  200.  
  201. /*
  202.  * read_dir() reads filenames from the current directory and adds them
  203.  * to the source or header file name lists as appropriate. Returns
  204.  * integer YES if successful, otherwise NO.
  205.  */
  206. read_dir(needheaders, needsource)
  207.     int needheaders;        /* need header file names */
  208.     int needsource;            /* need source file names */
  209. {
  210.     extern int AFLAG;        /* accept src files w/ leading dots? */
  211.     extern SLIST *HEADLIST;        /* header file name list */
  212.     extern SLIST *SRCLIST;        /* source file name list */
  213.     char *rindex();            /* find last occurrence of character */
  214.     char *slappend();        /* append file name to list */
  215.     char *suffix;            /* pointer to file name suffix */
  216.     char *p;            /* beginning of file name component */
  217.     DIR *dirp;            /* directory stream */
  218.     DIR *opendir();            /* open directory stream */
  219.     int lookupsfx();        /* get suffix type */
  220.     int sfxtyp;            /* type of suffix */
  221.     struct direct *dp;        /* directory entry pointer */
  222.     struct direct *readdir();    /* read a directory entry */
  223.  
  224.     if ((dirp = opendir(CURDIR)) == NULL)
  225.         {
  226.         warn("can't open current directory");
  227.         return(NO);
  228.         }
  229.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  230.         if ((suffix = rindex(dp->d_name, '.')) != NULL)
  231.             {
  232.             if ((p = rindex(dp->d_name, '/')) != NULL)
  233.                 p++;
  234.             else
  235.                 p = dp->d_name;
  236.             if (*p == '.' && (AFLAG == 0))
  237.                 continue;
  238.             suffix++;
  239.             sfxtyp = lookupsfx(suffix);
  240.             if (sfxtyp == SFXSRC)
  241.                 {
  242.                 if (needsource)
  243.                     if (slappend(dp->d_name, SRCLIST) == NULL)
  244.                         return(NO);
  245.                 }
  246.             else if (sfxtyp == SFXHEAD)
  247.                 {
  248.                 if (needheaders)
  249.                     if (slappend(dp->d_name, HEADLIST) == NULL)
  250.                         return(NO);
  251.                 }
  252.             }
  253.     closedir(dirp);
  254.     return(YES);
  255. }
  256.