OOO330
[LibreOffice.git] / soltools / mkdepend / include.c
blob45d0730393a383995cb93dc462bbfe323b773920
1 /* $XConsortium: include.c,v 1.17 94/12/05 19:33:08 gildea Exp $ */
2 /*
4 Copyright (c) 1993, 1994 X Consortium
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
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.
30 #include "def.h"
31 #include <string.h>
33 void remove_dotdot( char * );
34 int isdot( char * );
35 int isdotdot( char * );
36 int issymbolic(char * dir, char * component);
37 int exists_path(struct IncludesCollection*, char*);
40 extern struct inclist inclist[ MAXFILES ],
41 *inclistp;
42 extern char *includedirs[ ];
43 extern char *notdotdot[ ];
44 extern boolean show_where_not;
45 extern boolean warn_multiple;
47 struct inclist *inc_path(file, include, dot, incCollection)
48 register char *file,
49 *include;
50 boolean dot;
51 struct IncludesCollection* incCollection;
53 static char path[ BUFSIZ ];
54 register char **pp, *p;
55 register struct inclist *ip;
56 struct stat st;
57 boolean found = FALSE;
60 * Check all previously found include files for a path that
61 * has already been expanded.
63 for (ip = inclist; ip->i_file; ip++)
64 if ((strcmp(ip->i_incstring, include) == 0) && !ip->i_included_sym)
66 found = TRUE;
67 break;
71 * If the path was surrounded by "" or is an absolute path,
72 * then check the exact path provided.
74 if (!found && (dot || *include == '/')) {
76 if ((exists_path(incCollection, include)) && stat(include, &st) == 0 && !( st.st_mode & S_IFDIR)) {
77 ip = newinclude(include, include);
78 found = TRUE;
80 else if (show_where_not)
81 warning1("\tnot in %s\n", include);
85 * See if this include file is in the directory of the
86 * file being compiled.
88 if (!found) {
89 for (p=file+strlen(file); p>file; p--)
90 if (*p == '/')
91 break;
92 if (p == file)
93 strcpy(path, include);
94 else {
95 strncpy(path, file, (p-file) + 1);
96 path[ (p-file) + 1 ] = '\0';
97 strcpy(path + (p-file) + 1, include);
99 remove_dotdot(path);
100 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !( st.st_mode & S_IFDIR)) {
101 ip = newinclude(path, include);
102 found = TRUE;
104 else if (show_where_not)
105 warning1("\tnot in %s\n", path);
109 * Check the include directories specified. (standard include dir
110 * should be at the end.)
112 if (!found)
113 for (pp = includedirs; *pp; pp++) {
114 sprintf(path, "%s/%s", *pp, include);
115 remove_dotdot(path);
116 if ((exists_path(incCollection, path)) && stat(path, &st) == 0 && !(st.st_mode & S_IFDIR)) {
117 ip = newinclude(path, include);
118 found = TRUE;
119 break;
121 else if (show_where_not)
122 warning1("\tnot in %s\n", path);
125 if (!found)
126 ip = NULL;
127 return(ip);
130 int exists_path(incCollection, path)
131 struct IncludesCollection* incCollection;
132 char* path;
134 convert_slashes(path);
135 return call_IncludesCollection_exists(incCollection, path);
139 * Occasionally, pathnames are created that look like .../x/../y
140 * Any of the 'x/..' sequences within the name can be eliminated.
141 * (but only if 'x' is not a symbolic link!!)
143 void remove_dotdot(path)
144 char *path;
146 register char *end, *from, *to, **cp;
147 char *components[ MAXFILES ],
148 newpath[ BUFSIZ ];
149 boolean component_copied;
152 * slice path up into components.
154 to = newpath;
155 if (*path == '/')
156 *to++ = '/';
157 *to = '\0';
158 cp = components;
159 for (from=end=path; *end; end++)
160 if (*end == '/') {
161 while (*end == '/')
162 *end++ = '\0';
163 if (*from)
164 *cp++ = from;
165 from = end;
167 *cp++ = from;
168 *cp = NULL;
171 * Recursively remove all 'x/..' component pairs.
173 cp = components;
174 while(*cp) {
175 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
176 && !issymbolic(newpath, *cp))
178 char **fp = cp + 2;
179 char **tp = cp;
182 *tp++ = *fp; /* move all the pointers down */
183 while (*fp++);
184 if (cp != components)
185 cp--; /* go back and check for nested ".." */
186 } else {
187 cp++;
191 * Concatenate the remaining path elements.
193 cp = components;
194 component_copied = FALSE;
195 while(*cp) {
196 if (component_copied)
197 *to++ = '/';
198 component_copied = TRUE;
199 for (from = *cp; *from; )
200 *to++ = *from++;
201 *to = '\0';
202 cp++;
204 *to++ = '\0';
207 * copy the reconstituted path back to our pointer.
209 strcpy(path, newpath);
212 int isdot(p)
213 register char *p;
215 if(p && *p++ == '.' && *p++ == '\0')
216 return(TRUE);
217 return(FALSE);
220 int isdotdot(p)
221 register char *p;
223 if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
224 return(TRUE);
225 return(FALSE);
228 int issymbolic(dir, component)
229 register char *dir, *component;
231 #ifdef S_IFLNK
232 struct stat st;
233 char buf[ BUFSIZ ], **pp;
235 sprintf(buf, "%s%s%s", dir, *dir ? "/" : "", component);
236 for (pp=notdotdot; *pp; pp++)
237 if (strcmp(*pp, buf) == 0)
238 return (TRUE);
239 if (lstat(buf, &st) == 0
240 && (st.st_mode & S_IFMT) == S_IFLNK) {
241 *pp++ = copy(buf);
242 if (pp >= &notdotdot[ MAXDIRS ])
243 fatalerr("out of .. dirs, increase MAXDIRS\n");
244 return(TRUE);
246 #endif
247 return(FALSE);
251 * Add an include file to the list of those included by 'file'.
253 struct inclist *newinclude(newfile, incstring)
254 register char *newfile, *incstring;
256 register struct inclist *ip;
259 * First, put this file on the global list of include files.
261 ip = inclistp++;
262 if (inclistp == inclist + MAXFILES - 1)
263 fatalerr("out of space: increase MAXFILES\n");
264 ip->i_file = copy(newfile);
265 ip->i_included_sym = FALSE;
266 if (incstring == NULL)
267 ip->i_incstring = ip->i_file;
268 else
269 ip->i_incstring = copy(incstring);
271 return(ip);
274 void included_by(ip, newfile)
275 register struct inclist *ip, *newfile;
277 register int i;
279 if (ip == NULL)
280 return;
282 * Put this include file (newfile) on the list of files included
283 * by 'file'. If 'file' is NULL, then it is not an include
284 * file itself (i.e. was probably mentioned on the command line).
285 * If it is already on the list, don't stick it on again.
287 if (ip->i_list == NULL)
288 ip->i_list = (struct inclist **)
289 malloc(sizeof(struct inclist *) * ++ip->i_listlen);
290 else {
291 for (i=0; i<ip->i_listlen; i++)
292 if (ip->i_list[ i ] == newfile) {
293 i = strlen(newfile->i_file);
294 if (!ip->i_included_sym &&
295 !(i > 2 &&
296 newfile->i_file[i-1] == 'c' &&
297 newfile->i_file[i-2] == '.'))
299 /* only complain if ip has */
300 /* no #include SYMBOL lines */
301 /* and is not a .c file */
302 if (warn_multiple)
304 warning("%s includes %s more than once!\n",
305 ip->i_file, newfile->i_file);
306 warning1("Already have\n");
307 for (i=0; i<ip->i_listlen; i++)
308 warning1("\t%s\n", ip->i_list[i]->i_file);
311 return;
313 ip->i_list = (struct inclist **) realloc(ip->i_list,
314 sizeof(struct inclist *) * ++ip->i_listlen);
316 ip->i_list[ ip->i_listlen-1 ] = newfile;
319 void inc_clean ()
321 register struct inclist *ip;
323 for (ip = inclist; ip < inclistp; ip++) {
324 ip->i_marked = FALSE;