File: getdbpath.c

package info (click to toggle)
global 6.6.14-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,612 kB
  • sloc: ansic: 132,180; sh: 5,017; javascript: 4,891; perl: 811; lisp: 676; makefile: 340; yacc: 122
file content (397 lines) | stat: -rw-r--r-- 10,455 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 * Copyright (c) 1997, 1998, 1999, 2000, 2002, 2008, 2011, 2014, 2015
 *	Tama Communications Corporation
 *
 * This file is part of GNU GLOBAL.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gparam.h"
#include "abs2rel.h"
#include "die.h"
#include "getdbpath.h"
#include "gtagsop.h"
#include "locatestring.h"
#include "makepath.h"
#include "path.h"
#include "strlimcpy.h"
#include "test.h"

/**
 * define the position of the root slash.
 */
#if defined(_WIN32) || defined(__DJGPP__)
#define ROOT 2
#else
#define ROOT 0
#endif

static const char *gtagsobjdirprefix;	/**< tag partition	*/
static const char *gtagsobjdir;		/**< tag directory	*/
char const *gtags_dbpath_error;		/**< error message	*/

/**
 * setupvariables: load variables regard to BSD OBJ directory.
 */
static void
setupvariables(int verbose)
{
	const char *p;

	if ((p = getenv("GTAGSOBJDIRPREFIX")) != NULL ||
	    (p = getenv("MAKEOBJDIRPREFIX")) != NULL)
	{
		gtagsobjdirprefix = p;
		if (verbose)
			fprintf(stderr, "GTAGSOBJDIRPREFIX is set to '%s'.\n", p);
	} else {
		gtagsobjdirprefix = "/usr/obj";
	}
	if ((p = getenv("GTAGSOBJDIR")) != NULL ||
	    (p = getenv("MAKEOBJDIR")) != NULL)
	{
		gtagsobjdir = p;
		if (verbose)
			fprintf(stderr, "GTAGSOBJDIR is set to '%s'.\n", p);
	} else {
		gtagsobjdir = "obj";
	}
}
/**
 * getobjdir: get objdir if it exists.
 *
 *	@param[in]	candidate candidate root directory
 *	@param[in]	verbose	verbose mode 1: on, 0: off
 *	@return		objdir(NULL: not found)
 */
char *
getobjdir(const char *candidate, int verbose)
{
	static char path[MAXPATHLEN];

	/*
	 * setup gtagsobjdir and gtagsobjdirprefix (only first time).
	 */
	if (gtagsobjdir == NULL)
		setupvariables(0);
	snprintf(path, sizeof(path), "%s/%s", candidate, gtagsobjdir);
	if (test("d", path)) {
		if (!test("drw", path))
			die("Found objdir '%s', but you don't have read/write permission for it.", path);
		if (verbose)
			fprintf(stderr, "Using objdir '%s'.\n", path);
		return path;
	}
#if !defined(_WIN32) && !defined(__DJGPP__)
	if (test("d", gtagsobjdirprefix)) {
		snprintf(path, sizeof(path), "%s%s", gtagsobjdirprefix, candidate);
		if (test("d", path)) {
			if (!test("drw", path))
				die("Found objdir '%s', but you don't have read/write permission for it.", path);
			if (verbose)
				fprintf(stderr, "Using objdir '%s'.\n", path);
			return path;
		}
		if (makedirectories(gtagsobjdirprefix, candidate + 1, verbose) < 0)
			die("Found the base for objdir '%s', but you cannot create new directory in it.", path);
		if (verbose)
			fprintf(stderr, "Using objdir '%s'.\n", path);
		return path;
	}
#endif
	return NULL;
}
/**
 * gtagsexist: test whether GTAGS's existence.
 *
 *	@param[in]	candidate candidate root directory
 *	@param[out]	dbpath	directory which "GTAGS" exist
 *	@param[in]	size	size of dbpath buffer
 *	@param[in]	verbose	verbose mode 1: on, 0: off
 *	@return		0: not found, 1: found
 *
 * Gtagsexist locate "GTAGS" file in "$candidate/", "$candidate/obj/" and
 * "/usr/obj/$candidate/" in this order by default.
 * This behavior is same with BSD make(1)'s one.
 */
int
gtagsexist(const char *candidate, char *dbpath, int size, int verbose)
{
	char path[MAXPATHLEN];
	const char *candidate_without_slash;

	/*
	 * setup gtagsobjdir and gtagsobjdirprefix (only first time).
	 */
	if (gtagsobjdir == NULL)
		setupvariables(verbose);

	if (strcmp(candidate, "/") == 0)
		candidate_without_slash = "";
	else
		candidate_without_slash = candidate;
	snprintf(path, sizeof(path), "%s/%s", candidate_without_slash, dbname(GTAGS));
	if (verbose)
		fprintf(stderr, "checking %s\n", path);
	if (test("fr", path)) {
		if (verbose)
			fprintf(stderr, "GTAGS found at '%s'.\n", path);
		snprintf(dbpath, size, "%s", candidate);
		return 1;
	}
	snprintf(path, sizeof(path),
		"%s/%s/%s", candidate_without_slash, gtagsobjdir, dbname(GTAGS));
	if (verbose)
		fprintf(stderr, "checking %s\n", path);
	if (test("fr", path)) {
		if (verbose)
			fprintf(stderr, "GTAGS found at '%s'.\n", path);
		snprintf(dbpath, size, "%s/%s", candidate_without_slash, gtagsobjdir);
		return 1;
	}
#if !defined(_WIN32) && !defined(__DJGPP__)
	snprintf(path, sizeof(path),
		"%s%s/%s", gtagsobjdirprefix, candidate_without_slash, dbname(GTAGS));
	if (verbose)
		fprintf(stderr, "checking %s\n", path);
	if (test("fr", path)) {
		if (verbose)
			fprintf(stderr, "GTAGS found at '%s'.\n", path);
		snprintf(dbpath, size, "%s%s", gtagsobjdirprefix, candidate_without_slash);
		return 1;
	}
#endif
	return 0;
}
static char dbpath[MAXPATHLEN];
static char root[MAXPATHLEN];
static char root_with_slash[MAXPATHLEN];
static char cwd[MAXPATHLEN];
static char relative_cwd_with_slash[MAXPATHLEN+2];
/*
 * setupdbpath: setup dbpath directory
 *
 *	@param[in]	verbose	verbose mode 1: on, 0: off
 *	@return	0: normal, 0<: error
 *	Globals used (output):
 *		cwd:	current directory,
 *		root:	root of source tree,
 *		dbpath: directory which "GTAGS" exist,
 *		gtags_dbpath_error: set if status (return value) < 0
 *
 */
int
setupdbpath(int verbose)
{
	struct stat sb;
	char *p;
	static char msg[1024];

	if (!vgetcwd(cwd, MAXPATHLEN)) {
		gtags_dbpath_error = "cannot get current directory.";
		return -1;
	}
	canonpath(cwd);

	if ((p = getenv("GTAGSROOT")) != NULL) {
		if (verbose)
			fprintf(stderr, "GTAGSROOT is set to '%s'.\n", p);
		if (!isabspath(p)) {
			gtags_dbpath_error = "GTAGSROOT must be an absolute path.";
			return -1;
		}
		if (stat(p, &sb) || !S_ISDIR(sb.st_mode)) {
			snprintf(msg, sizeof(msg), "directory '%s' not found.", p);
			gtags_dbpath_error = msg;
			return -1;
		}
		if (getenv("GTAGSLOGICALPATH")) {
			strlimcpy(root, p, sizeof(root));
		} else {
			if (realpath(p, root) == NULL) {
				snprintf(msg, sizeof(msg), "cannot get real path of '%s'.", p);
				gtags_dbpath_error = msg;
				return -1;
			}
		}
		/*
		 * GTAGSDBPATH is meaningful only when GTAGSROOT exist.
		 */
		if ((p = getenv("GTAGSDBPATH")) != NULL) {
			if (verbose)
				fprintf(stderr, "GTAGSDBPATH is set to '%s'.\n", p);
			if (!isabspath(p)) {
				gtags_dbpath_error = "GTAGSDBPATH must be an absolute path.";
				return -1;
			}
			if (stat(p, &sb) || !S_ISDIR(sb.st_mode)) {
				snprintf(msg, sizeof(msg), "directory '%s' not found.", p);
				gtags_dbpath_error = msg;
				return -1;
			}
			strlimcpy(dbpath, getenv("GTAGSDBPATH"), MAXPATHLEN);
		} else {
			if (!gtagsexist(root, dbpath, MAXPATHLEN, verbose)) {
				gtags_dbpath_error = "GTAGS not found.";
				return -3;
			}
		}
	} else {
		if (verbose && getenv("GTAGSDBPATH"))
			fprintf(stderr, "warning: GTAGSDBPATH is ignored because GTAGSROOT is not set.\n");
		/*
		 * start from current directory to '/' directory.
		 */
		strlimcpy(root, cwd, MAXPATHLEN);
		p = root + strlen(root);
		while (!gtagsexist(root, dbpath, MAXPATHLEN, verbose)) {
			if (!strcmp(root+ROOT, "/")) { 	/* reached the system's root directory */
				*(root+ROOT) = '\0';
				break;
			}
			while (*--p != '/' && p > (root+ROOT))
				;
			if (p == (root+ROOT))
				p++;
			*p = 0;
		}
		if (*(root+ROOT) == 0) {
			gtags_dbpath_error = "GTAGS not found.";
			return -3;
		}
		/*
		 * If file 'GTAGSROOT' found without environment variable
		 * GTAGSDBPATH, use the value of it as GTAGSROOT.
		 */
		do {
			FILE *fp;
			STRBUF *sb;
			const char *s, *path;

			path = makepath(root, "GTAGSROOT", NULL);
			if (!test("fr", path)) {
				break;
			}
			fp = fopen(path, "r");
			if (fp == NULL) {
				if (verbose)
					fprintf(stderr, "'%s' ignored because it cannot be opened.\n", path);
				break;
			}
			sb = strbuf_open(0);
			s = strbuf_fgets(sb, fp, STRBUF_NOCRLF);
			if (s == NULL) {
				/* Empty file? */
				if (verbose)
					fprintf(stderr, "'%s' ignored; strbuf_fgets() returned NULL.\n", path);
				goto ignore_lab;
			}
			if (!test("d", s)) {	/* XXX: if s == NULL, test() will use previous path used. */
				if (verbose)
					fprintf(stderr, "'%s' ignored because it doesn't include existent directory name.\n", path);
			} else {
				char buf[MAXPATHLEN];

				if (verbose)
					fprintf(stderr, "GTAGSROOT found at '%s'.\n", path);
				if (!isabspath(s))
					s = realpath(makepath(root, s, NULL), buf);
				strlimcpy(root, s, MAXPATHLEN);
			}
ignore_lab:;
			fclose(fp);
			strbuf_close(sb);
			break;
		} while (0);
	}
	if (!strcmp(root+ROOT, "/"))
		strlimcpy(root_with_slash, root, sizeof(root_with_slash));
	else
		snprintf(root_with_slash, sizeof(root_with_slash), "%s/", root);
	/*
	 * relative from the project root directory.
	 */
	snprintf(relative_cwd_with_slash,
		sizeof(relative_cwd_with_slash), "./%s/", cwd + strlen(root) + 1);
	return 0;
}
/**
 * in_the_project: test whether path is in the project.
 *
 *	@param[in]	path	target file or directory
 *	@return		0: out of the project, 1: in the project
 *
 * [Note] Please pass an absolute path name which does not include "." (current directory) or
 *       ".." (parent directory).
 */
int
in_the_project(const char *path)
{
	if (!strcmp(path, root) || locatestring(path, root_with_slash, MATCH_AT_FIRST))
		return 1;
	return 0;
}
/*
 * return saved values.
 */
const char *
get_dbpath(void)
{
	return (const char *)dbpath;
}
const char *
get_root(void)
{
	return (const char *)root;
}
const char *
get_root_with_slash(void)
{
	return (const char *)root_with_slash;
}
const char *
get_cwd(void)
{
	return (const char *)cwd;
}
const char *
get_relative_cwd_with_slash(void)
{
	return (const char *)relative_cwd_with_slash;
}
void
dump_dbpath(void)
{
	fprintf(stderr, "db path: %s\n", dbpath);
	fprintf(stderr, "root path: %s\n", root);
	fprintf(stderr, "current directory: %s\n", cwd);
}