1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A/*
1N/A * Glenn Fowler
1N/A * AT&T Research
1N/A *
1N/A * include style search support
1N/A */
1N/A
1N/A#include <ast.h>
1N/A#include <error.h>
1N/A#include <ls.h>
1N/A
1N/A#define directory(p,s) (stat((p),(s))>=0&&S_ISDIR((s)->st_mode))
1N/A#define regular(p,s) (stat((p),(s))>=0&&(S_ISREG((s)->st_mode)||streq(p,"/dev/null")))
1N/A
1N/Atypedef struct Dir_s /* directory list element */
1N/A{
1N/A struct Dir_s* next; /* next in list */
1N/A char dir[1]; /* directory path */
1N/A} Dir_t;
1N/A
1N/Astatic struct /* directory list state */
1N/A{
1N/A Dir_t* head; /* directory list head */
1N/A Dir_t* tail; /* directory list tail */
1N/A} state;
1N/A
1N/A/*
1N/A * append dir to pathfind() include list
1N/A */
1N/A
1N/Aint
1N/Apathinclude(const char* dir)
1N/A{
1N/A register Dir_t* dp;
1N/A struct stat st;
1N/A
1N/A if (dir && *dir && !streq(dir, ".") && directory(dir, &st))
1N/A {
1N/A for (dp = state.head; dp; dp = dp->next)
1N/A if (streq(dir, dp->dir))
1N/A return 0;
1N/A if (!(dp = oldof(0, Dir_t, 1, strlen(dir))))
1N/A return -1;
1N/A strcpy(dp->dir, dir);
1N/A dp->next = 0;
1N/A if (state.tail)
1N/A state.tail = state.tail->next = dp;
1N/A else
1N/A state.head = state.tail = dp;
1N/A }
1N/A return 0;
1N/A}
1N/A
1N/A/*
1N/A * return path to name using pathinclude() list
1N/A * path placed in <buf,size>
1N/A * if lib!=0 then pathpath() attempted after include search
1N/A * if type!=0 and name has no '.' then file.type also attempted
1N/A * any *: prefix in lib is ignored (discipline library dictionary support)
1N/A */
1N/A
1N/Achar*
1N/Apathfind(const char* name, const char* lib, const char* type, char* buf, size_t size)
1N/A{
1N/A register Dir_t* dp;
1N/A register char* s;
1N/A char tmp[PATH_MAX];
1N/A struct stat st;
1N/A
1N/A if (((s = strrchr(name, '/')) || (s = (char*)name)) && strchr(s, '.'))
1N/A type = 0;
1N/A
1N/A /*
1N/A * always check the unadorned path first
1N/A * this handles . and absolute paths
1N/A */
1N/A
1N/A if (regular(name, &st))
1N/A {
1N/A strncopy(buf, name, size);
1N/A return buf;
1N/A }
1N/A if (type)
1N/A {
1N/A sfsprintf(buf, size, "%s.%s", name, type);
1N/A if (regular(buf, &st))
1N/A return buf;
1N/A }
1N/A if (*name == '/')
1N/A return 0;
1N/A
1N/A /*
1N/A * check the directory of the including file
1N/A * on the assumption that error_info.file is properly stacked
1N/A */
1N/A
1N/A if (error_info.file && (s = strrchr(error_info.file, '/')))
1N/A {
1N/A sfsprintf(buf, size, "%-.*s%s", s - error_info.file + 1, error_info.file, name);
1N/A if (regular(buf, &st))
1N/A return buf;
1N/A if (type)
1N/A {
1N/A sfsprintf(buf, size, "%-.*s%s%.s", s - error_info.file + 1, error_info.file, name, type);
1N/A if (regular(buf, &st))
1N/A return buf;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * check the include dir list
1N/A */
1N/A
1N/A for (dp = state.head; dp; dp = dp->next)
1N/A {
1N/A sfsprintf(tmp, sizeof(tmp), "%s/%s", dp->dir, name);
1N/A if (pathpath(tmp, "", PATH_REGULAR, buf, size))
1N/A return buf;
1N/A if (type)
1N/A {
1N/A sfsprintf(tmp, sizeof(tmp), "%s/%s.%s", dp->dir, name, type);
1N/A if (pathpath(tmp, "", PATH_REGULAR, buf, size))
1N/A return buf;
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * finally a lib related search on PATH
1N/A */
1N/A
1N/A if (lib)
1N/A {
1N/A if (s = strrchr((char*)lib, ':'))
1N/A lib = (const char*)s + 1;
1N/A sfsprintf(tmp, sizeof(tmp), "lib/%s/%s", lib, name);
1N/A if (pathpath(tmp, "", PATH_REGULAR, buf, size))
1N/A return buf;
1N/A if (type)
1N/A {
1N/A sfsprintf(tmp, sizeof(tmp), "lib/%s/%s.%s", lib, name, type);
1N/A if (pathpath(tmp, "", PATH_REGULAR, buf, size))
1N/A return buf;
1N/A }
1N/A }
1N/A return 0;
1N/A}