switched to GPLv3 only, because i don't trust FSF anymore
[k8jam.git] / src / search.c
blob52fd97b0a90199d48241acc09077896ef60020a5
1 /*
2 * Copyright 1993-2002 Christopher Seiwald and Perforce Software, Inc.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * search.c - find a target along $(SEARCH) or $(LOCATE)
20 #include "jam.h"
21 #include "lists.h"
22 #include "search.h"
23 #include "timestamp.h"
24 #include "pathsys.h"
25 #include "variable.h"
26 #include "newstr.h"
27 #include "parse.h"
28 #include "compile.h"
31 static void call_bind_rule (const char *target, const char *boundname) {
32 LIST *bind_rule = var_get("BINDRULE");
33 if (bind_rule) {
34 LOL lol;
35 lol_init(&lol);
36 lol_add(&lol, list_new(L0, target, 1));
37 lol_add(&lol, list_new(L0, boundname, 1));
38 if (lol_get(&lol, 1)) list_free(evaluate_rule(bind_rule->string, &lol, L0));
39 lol_free(&lol);
44 const char *search (const char *target, time_t *time) {
45 PATHNAME f[1];
46 LIST *varlist;
47 static char buf[MAXJPATH];
48 /* parse the filename */
49 path_parse(target, f);
50 f->f_grist.ptr = 0;
51 f->f_grist.len = 0;
52 if ((varlist = var_get("LOCATE"))) {
53 f->f_root.ptr = varlist->string;
54 f->f_root.len = strlen(varlist->string);
55 path_build(buf, f); /* was with binding, but it does nothing now */
56 if (DEBUG_SEARCH) printf("locate %s: %s\n", target, buf);
57 timestamp(buf, time);
58 return newstr(buf);
60 if ((varlist = var_get("SEARCH"))) {
61 while (varlist) {
62 f->f_root.ptr = varlist->string;
63 f->f_root.len = strlen(varlist->string);
64 path_build(buf, f); /* was with binding, but it does nothing now */
65 if (DEBUG_SEARCH) printf("search %s: %s\n", target, buf);
66 timestamp(buf, time);
67 if (*time) return newstr(buf);
68 varlist = list_next(varlist);
71 /* look for the obvious */
72 /* this is a questionable move: should we look in the obvious place if SEARCH is set? */
73 f->f_root.ptr = 0;
74 f->f_root.len = 0;
75 path_build(buf, f); /* was with binding, but it does nothing now */
76 if (DEBUG_SEARCH) printf("search %s: %s\n", target, buf);
77 timestamp(buf, time);
78 call_bind_rule(target, buf);
79 return newstr(buf);