welcome GPLv3! yes, k8jam is GPL'ed now, along with Jambase. because i can.
[k8jam.git] / src / search.c
blob4a0be6145c50b86180d0fd6db0e423281f60d722
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, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * search.c - find a target along $(SEARCH) or $(LOCATE)
21 #include "jam.h"
22 #include "lists.h"
23 #include "search.h"
24 #include "timestamp.h"
25 #include "pathsys.h"
26 #include "variable.h"
27 #include "newstr.h"
28 #include "parse.h"
29 #include "compile.h"
32 static void call_bind_rule (const char *target, const char *boundname) {
33 LIST *bind_rule = var_get("BINDRULE");
34 if (bind_rule) {
35 LOL lol;
36 lol_init(&lol);
37 lol_add(&lol, list_new(L0, target, 1));
38 lol_add(&lol, list_new(L0, boundname, 1));
39 if (lol_get(&lol, 1)) list_free(evaluate_rule(bind_rule->string, &lol, L0));
40 lol_free(&lol);
45 const char *search (const char *target, time_t *time) {
46 PATHNAME f[1];
47 LIST *varlist;
48 static char buf[MAXJPATH];
49 /* parse the filename */
50 path_parse(target, f);
51 f->f_grist.ptr = 0;
52 f->f_grist.len = 0;
53 if ((varlist = var_get("LOCATE"))) {
54 f->f_root.ptr = varlist->string;
55 f->f_root.len = strlen(varlist->string);
56 path_build(buf, f); /* was with binding, but it does nothing now */
57 if (DEBUG_SEARCH) printf("locate %s: %s\n", target, buf);
58 timestamp(buf, time);
59 return newstr(buf);
61 if ((varlist = var_get("SEARCH"))) {
62 while (varlist) {
63 f->f_root.ptr = varlist->string;
64 f->f_root.len = strlen(varlist->string);
65 path_build(buf, f); /* was with binding, but it does nothing now */
66 if (DEBUG_SEARCH) printf("search %s: %s\n", target, buf);
67 timestamp(buf, time);
68 if (*time) return newstr(buf);
69 varlist = list_next(varlist);
72 /* look for the obvious */
73 /* this is a questionable move: should we look in the obvious place if SEARCH is set? */
74 f->f_root.ptr = 0;
75 f->f_root.len = 0;
76 path_build(buf, f); /* was with binding, but it does nothing now */
77 if (DEBUG_SEARCH) printf("search %s: %s\n", target, buf);
78 timestamp(buf, time);
79 call_bind_rule(target, buf);
80 return newstr(buf);