home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mdepsrc.zip / include.c < prev    next >
C/C++ Source or Header  |  1996-12-10  |  7KB  |  309 lines

  1. /* $XConsortium: include.c,v 1.17 94/12/05 19:33:08 gildea Exp $ */
  2. /*
  3.  
  4. Copyright (c) 1993, 1994  X Consortium
  5.  
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15.  
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  19. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  
  23. Except as contained in this notice, the name of the X Consortium shall not be
  24. used in advertising or otherwise to promote the sale, use or other dealings
  25. in this Software without prior written authorization from the X Consortium.
  26.  
  27. */
  28.  
  29.  
  30. #include "def.h"
  31.  
  32. extern struct    inclist    inclist[ MAXFILES ],
  33.             *inclistp;
  34. extern char    *includedirs[ ];
  35. extern char    *notdotdot[ ];
  36. extern boolean show_where_not;
  37. extern boolean warn_multiple;
  38.  
  39. struct inclist *inc_path(file, include, dot)
  40.     register char    *file,
  41.             *include;
  42.     boolean    dot;
  43. {
  44.     static char    path[ BUFSIZ ];
  45.     register char        **pp, *p;
  46.     register struct inclist    *ip;
  47.     struct stat    st;
  48.     boolean    found = FALSE;
  49.  
  50.     /*
  51.      * Check all previously found include files for a path that
  52.      * has already been expanded.
  53.      */
  54.     for (ip = inclist; ip->i_file; ip++)
  55.         if ((strcmp(ip->i_incstring, include) == 0) && !ip->i_included_sym)
  56.         {
  57.         found = TRUE;
  58.         break;
  59.         }
  60.  
  61.     /*
  62.      * If the path was surrounded by "" or is an absolute path,
  63.      * then check the exact path provided.
  64.      */
  65.     if (!found && (dot || *include == '/')) {
  66.         if (stat(include, &st) == 0) {
  67.             ip = newinclude(include, include);
  68.             found = TRUE;
  69.         }
  70.         else if (show_where_not)
  71.             warning1("\tnot in %s\n", include);
  72.     }
  73.  
  74.     /*
  75.      * See if this include file is in the directory of the
  76.      * file being compiled.
  77.      */
  78.     if (!found) {
  79.         for (p=file+strlen(file); p>file; p--)
  80.             if (*p == '/')
  81.                 break;
  82.         if (p == file)
  83.             strcpy(path, include);
  84.         else {
  85.             strncpy(path, file, (p-file) + 1);
  86.             path[ (p-file) + 1 ] = '\0';
  87.             strcpy(path + (p-file) + 1, include);
  88.         }
  89.         remove_dotdot(path);
  90.         if (stat(path, &st) == 0) {
  91.             ip = newinclude(path, include);
  92.             found = TRUE;
  93.         }
  94.         else if (show_where_not)
  95.             warning1("\tnot in %s\n", path);
  96.     }
  97.  
  98.     /*
  99.      * Check the include directories specified. (standard include dir
  100.      * should be at the end.)
  101.      */
  102.     if (!found)
  103.         for (pp = includedirs; *pp; pp++) {
  104.             sprintf(path, "%s/%s", *pp, include);
  105.             remove_dotdot(path);
  106.             if (stat(path, &st) == 0) {
  107.                 ip = newinclude(path, include);
  108.                 found = TRUE;
  109.                 break;
  110.             }
  111.             else if (show_where_not)
  112.                 warning1("\tnot in %s\n", path);
  113.         }
  114.  
  115.     if (!found)
  116.         ip = NULL;
  117.     return(ip);
  118. }
  119.  
  120. /*
  121.  * Occasionally, pathnames are created that look like .../x/../y
  122.  * Any of the 'x/..' sequences within the name can be eliminated.
  123.  * (but only if 'x' is not a symbolic link!!)
  124.  */
  125. remove_dotdot(path)
  126.     char    *path;
  127. {
  128.     register char    *end, *from, *to, **cp;
  129.     char        *components[ MAXFILES ],
  130.             newpath[ BUFSIZ ];
  131.     boolean        component_copied;
  132.  
  133.     /*
  134.      * slice path up into components.
  135.      */
  136.     to = newpath;
  137.     if (*path == '/')
  138.         *to++ = '/';
  139.     *to = '\0';
  140.     cp = components;
  141.     for (from=end=path; *end; end++)
  142.         if (*end == '/') {
  143.             while (*end == '/')
  144.                 *end++ = '\0';
  145.             if (*from)
  146.                 *cp++ = from;
  147.             from = end;
  148.         }
  149.     *cp++ = from;
  150.     *cp = NULL;
  151.  
  152.     /*
  153.      * Recursively remove all 'x/..' component pairs.
  154.      */
  155.     cp = components;
  156.     while(*cp) {
  157.         if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
  158.             && !issymbolic(newpath, *cp))
  159.         {
  160.             char **fp = cp + 2;
  161.             char **tp = cp;
  162.  
  163.             do 
  164.             *tp++ = *fp; /* move all the pointers down */
  165.             while (*fp++);
  166.             if (cp != components)
  167.             cp--;    /* go back and check for nested ".." */
  168.         } else {
  169.             cp++;
  170.         }
  171.     }
  172.     /*
  173.      * Concatenate the remaining path elements.
  174.      */
  175.     cp = components;
  176.     component_copied = FALSE;
  177.     while(*cp) {
  178.         if (component_copied)
  179.             *to++ = '/';
  180.         component_copied = TRUE;
  181.         for (from = *cp; *from; )
  182.             *to++ = *from++;
  183.         *to = '\0';
  184.         cp++;
  185.     }
  186.     *to++ = '\0';
  187.  
  188.     /*
  189.      * copy the reconstituted path back to our pointer.
  190.      */
  191.     strcpy(path, newpath);
  192. }
  193.  
  194. isdot(p)
  195.     register char    *p;
  196. {
  197.     if(p && *p++ == '.' && *p++ == '\0')
  198.         return(TRUE);
  199.     return(FALSE);
  200. }
  201.  
  202. isdotdot(p)
  203.     register char    *p;
  204. {
  205.     if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
  206.         return(TRUE);
  207.     return(FALSE);
  208. }
  209.  
  210. issymbolic(dir, component)
  211.     register char    *dir, *component;
  212. {
  213. #ifdef S_IFLNK
  214.     struct stat    st;
  215.     char    buf[ BUFSIZ ], **pp;
  216.  
  217.     sprintf(buf, "%s%s%s", dir, *dir ? "/" : "", component);
  218.     for (pp=notdotdot; *pp; pp++)
  219.         if (strcmp(*pp, buf) == 0)
  220.             return (TRUE);
  221.     if (lstat(buf, &st) == 0
  222.     && (st.st_mode & S_IFMT) == S_IFLNK) {
  223.         *pp++ = copy(buf);
  224.         if (pp >= ¬dotdot[ MAXDIRS ])
  225.             fatalerr("out of .. dirs, increase MAXDIRS\n");
  226.         return(TRUE);
  227.     }
  228. #endif
  229.     return(FALSE);
  230. }
  231.  
  232. /*
  233.  * Add an include file to the list of those included by 'file'.
  234.  */
  235. struct inclist *newinclude(newfile, incstring)
  236.     register char    *newfile, *incstring;
  237. {
  238.     register struct inclist    *ip;
  239.  
  240.     /*
  241.      * First, put this file on the global list of include files.
  242.      */
  243.     ip = inclistp++;
  244.     if (inclistp == inclist + MAXFILES - 1)
  245.         fatalerr("out of space: increase MAXFILES\n");
  246.     ip->i_file = copy(newfile);
  247.     ip->i_included_sym = FALSE;
  248.     if (incstring == NULL)
  249.         ip->i_incstring = ip->i_file;
  250.     else
  251.         ip->i_incstring = copy(incstring);
  252.  
  253.     return(ip);
  254. }
  255.  
  256. included_by(ip, newfile)
  257.     register struct inclist    *ip, *newfile;
  258. {
  259.     register i;
  260.  
  261.     if (ip == NULL)
  262.         return;
  263.     /*
  264.      * Put this include file (newfile) on the list of files included
  265.      * by 'file'.  If 'file' is NULL, then it is not an include
  266.      * file itself (i.e. was probably mentioned on the command line).
  267.      * If it is already on the list, don't stick it on again.
  268.      */
  269.     if (ip->i_list == NULL)
  270.         ip->i_list = (struct inclist **)
  271.             malloc(sizeof(struct inclist *) * ++ip->i_listlen);
  272.     else {
  273.         for (i=0; i<ip->i_listlen; i++)
  274.             if (ip->i_list[ i ] == newfile) {
  275.                 i = strlen(newfile->i_file);
  276.                 if (!ip->i_included_sym &&
  277.                 !(i > 2 &&
  278.                   newfile->i_file[i-1] == 'c' &&
  279.                   newfile->i_file[i-2] == '.'))
  280.                 {
  281.                 /* only bitch if ip has */
  282.                 /* no #include SYMBOL lines  */
  283.                 /* and is not a .c file */
  284.                 if (warn_multiple)
  285.                 {
  286.                     warning("%s includes %s more than once!\n",
  287.                         ip->i_file, newfile->i_file);
  288.                     warning1("Already have\n");
  289.                     for (i=0; i<ip->i_listlen; i++)
  290.                         warning1("\t%s\n", ip->i_list[i]->i_file);
  291.                 }
  292.                 }
  293.                 return;
  294.             }
  295.         ip->i_list = (struct inclist **) realloc(ip->i_list,
  296.             sizeof(struct inclist *) * ++ip->i_listlen);
  297.     }
  298.     ip->i_list[ ip->i_listlen-1 ] = newfile;
  299. }
  300.  
  301. inc_clean ()
  302. {
  303.     register struct inclist *ip;
  304.  
  305.     for (ip = inclist; ip < inclistp; ip++) {
  306.         ip->i_marked = FALSE;
  307.     }
  308. }
  309.