1 /* $XConsortium: include.c /main/20 1996/12/04 10:11:18 swick $ */
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.
32 extern struct inclist inclist[ MAXFILES ],
34 extern char *includedirs[ ];
35 extern char *notdotdot[ ];
36 extern boolean show_where_not;
37 extern boolean warn_multiple;
43 if(p && *p++ == '.' && *p++ == '\0')
52 if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0')
58 issymbolic(dir, component)
59 register char *dir, *component;
63 char buf[ BUFSIZ ], **pp;
65 sprintf(buf, "%s%s%s", dir, *dir ? "/" : "", component);
66 for (pp=notdotdot; *pp; pp++)
67 if (strcmp(*pp, buf) == 0)
69 if (lstat(buf, &st) == 0
70 && (st.st_mode & S_IFMT) == S_IFLNK) {
72 if (pp >= ¬dotdot[ MAXDIRS ])
73 fatalerr("out of .. dirs, increase MAXDIRS\n");
81 * Occasionally, pathnames are created that look like .../x/../y
82 * Any of the 'x/..' sequences within the name can be eliminated.
83 * (but only if 'x' is not a symbolic link!!)
89 register char *end, *from, *to, **cp;
90 char *components[ MAXFILES ],
92 boolean component_copied;
95 * slice path up into components.
102 for (from=end=path; *end; end++)
114 * Recursively remove all 'x/..' component pairs.
118 if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1))
119 && !issymbolic(newpath, *cp))
125 *tp++ = *fp; /* move all the pointers down */
127 if (cp != components)
128 cp--; /* go back and check for nested ".." */
134 * Concatenate the remaining path elements.
137 component_copied = FALSE;
139 if (component_copied)
141 component_copied = TRUE;
142 for (from = *cp; *from; )
150 * copy the reconstituted path back to our pointer.
152 strcpy(path, newpath);
156 * Add an include file to the list of those included by 'file'.
158 struct inclist *newinclude(newfile, incstring)
159 register char *newfile, *incstring;
161 register struct inclist *ip;
164 * First, put this file on the global list of include files.
167 if (inclistp == inclist + MAXFILES - 1)
168 fatalerr("out of space: increase MAXFILES\n");
169 ip->i_file = copy(newfile);
171 if (incstring == NULL)
172 ip->i_incstring = ip->i_file;
174 ip->i_incstring = copy(incstring);
180 included_by(ip, newfile)
181 register struct inclist *ip, *newfile;
188 * Put this include file (newfile) on the list of files included
189 * by 'file'. If 'file' is NULL, then it is not an include
190 * file itself (i.e. was probably mentioned on the command line).
191 * If it is already on the list, don't stick it on again.
193 if (ip->i_list == NULL) {
194 ip->i_list = (struct inclist **)
195 malloc(sizeof(struct inclist *) * ++ip->i_listlen);
196 ip->i_merged = (boolean *)
197 malloc(sizeof(boolean) * ip->i_listlen);
199 for (i=0; i<ip->i_listlen; i++)
200 if (ip->i_list[ i ] == newfile) {
201 i = strlen(newfile->i_file);
202 if (!(ip->i_flags & INCLUDED_SYM) &&
204 newfile->i_file[i-1] == 'c' &&
205 newfile->i_file[i-2] == '.'))
207 /* only bitch if ip has */
208 /* no #include SYMBOL lines */
209 /* and is not a .c file */
212 warning("%s includes %s more than once!\n",
213 ip->i_file, newfile->i_file);
214 warning1("Already have\n");
215 for (i=0; i<ip->i_listlen; i++)
216 warning1("\t%s\n", ip->i_list[i]->i_file);
221 ip->i_list = (struct inclist **) realloc(ip->i_list,
222 sizeof(struct inclist *) * ++ip->i_listlen);
223 ip->i_merged = (boolean *)
224 realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen);
226 ip->i_list[ ip->i_listlen-1 ] = newfile;
227 ip->i_merged[ ip->i_listlen-1 ] = FALSE;
233 register struct inclist *ip;
235 for (ip = inclist; ip < inclistp; ip++) {
236 ip->i_flags &= ~MARKED;
240 struct inclist *inc_path(file, include, dot)
245 static char path[ BUFSIZ ];
246 register char **pp, *p;
247 register struct inclist *ip;
249 boolean found = FALSE;
252 * Check all previously found include files for a path that
253 * has already been expanded.
255 for (ip = inclist; ip->i_file; ip++)
256 if ((strcmp(ip->i_incstring, include) == 0) &&
257 !(ip->i_flags & INCLUDED_SYM))
264 * If the path was surrounded by "" or is an absolute path,
265 * then check the exact path provided.
267 if (!found && (dot || *include == '/')) {
268 if (stat(include, &st) == 0) {
269 ip = newinclude(include, include);
272 else if (show_where_not)
273 warning1("\tnot in %s\n", include);
277 * If the path was surrounded by "" see if this include file is in the
278 * directory of the file being parsed.
281 for (p=file+strlen(file); p>file; p--)
285 strcpy(path, include);
287 strncpy(path, file, (p-file) + 1);
288 path[ (p-file) + 1 ] = '\0';
289 strcpy(path + (p-file) + 1, include);
292 if (stat(path, &st) == 0) {
293 ip = newinclude(path, include);
296 else if (show_where_not)
297 warning1("\tnot in %s\n", path);
301 * Check the include directories specified. (standard include dir
302 * should be at the end.)
305 for (pp = includedirs; *pp; pp++) {
306 sprintf(path, "%s/%s", *pp, include);
308 if (stat(path, &st) == 0) {
309 ip = newinclude(path, include);
313 else if (show_where_not)
314 warning1("\tnot in %s\n", path);