tags.c revision 128345
1/*
2 * Copyright (C) 1984-2002  Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
10
11
12#include "less.h"
13
14#define	WHITESP(c)	((c)==' ' || (c)=='\t')
15
16#if TAGS
17
18public char *tags = "tags";
19
20static int total;
21static int curseq;
22
23extern int linenums;
24extern int sigs;
25
26enum tag_result {
27	TAG_FOUND,
28	TAG_NOFILE,
29	TAG_NOTAG,
30	TAG_NOTYPE,
31	TAG_INTR
32};
33
34/*
35 * Tag type
36 */
37enum {
38	T_CTAGS,	/* 'tags': standard and extended format (ctags) */
39	T_CTAGS_X,	/* stdin: cross reference format (ctags) */
40	T_GTAGS,	/* 'GTAGS': function defenition (global) */
41	T_GRTAGS,	/* 'GRTAGS': function reference (global) */
42	T_GSYMS,	/* 'GSYMS': other symbols (global) */
43	T_GPATH		/* 'GPATH': path name (global) */
44};
45
46static enum tag_result findctag();
47static enum tag_result findgtag();
48static char *nextgtag();
49static char *prevgtag();
50static POSITION ctagsearch();
51static POSITION gtagsearch();
52static int getentry();
53
54/*
55 * The list of tags generated by the last findgtag() call.
56 *
57 * Use either pattern or line number.
58 * findgtag() always uses line number, so pattern is always NULL.
59 * findctag() usually either pattern (in which case line number is 0),
60 * or line number (in which case pattern is NULL).
61 */
62struct taglist {
63	struct tag *tl_first;
64	struct tag *tl_last;
65};
66#define TAG_END  ((struct tag *) &taglist)
67static struct taglist taglist = { TAG_END, TAG_END };
68struct tag {
69	struct tag *next, *prev; /* List links */
70	char *tag_file;		/* Source file containing the tag */
71	LINENUM tag_linenum;	/* Appropriate line number in source file */
72	char *tag_pattern;	/* Pattern used to find the tag */
73	char tag_endline;	/* True if the pattern includes '$' */
74};
75static struct tag *curtag;
76
77#define TAG_INS(tp) \
78	(tp)->next = taglist.tl_first; \
79	(tp)->prev = TAG_END; \
80	taglist.tl_first->prev = (tp); \
81	taglist.tl_first = (tp);
82
83#define TAG_RM(tp) \
84	(tp)->next->prev = (tp)->prev; \
85	(tp)->prev->next = (tp)->next;
86
87/*
88 * Delete tag structures.
89 */
90	public void
91cleantags()
92{
93	register struct tag *tp;
94
95	/*
96	 * Delete any existing tag list.
97	 * {{ Ideally, we wouldn't do this until after we know that we
98	 *    can load some other tag information. }}
99	 */
100	while ((tp = taglist.tl_first) != TAG_END)
101	{
102		TAG_RM(tp);
103		free(tp);
104	}
105	curtag = NULL;
106	total = curseq = 0;
107}
108
109/*
110 * Create a new tag entry.
111 */
112	static struct tag *
113maketagent(name, file, linenum, pattern, endline)
114	char *name;
115	char *file;
116	LINENUM linenum;
117	char *pattern;
118	int endline;
119{
120	register struct tag *tp;
121
122	tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
123	tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
124	strcpy(tp->tag_file, file);
125	tp->tag_linenum = linenum;
126	tp->tag_endline = endline;
127	if (pattern == NULL)
128		tp->tag_pattern = NULL;
129	else
130	{
131		tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
132		strcpy(tp->tag_pattern, pattern);
133	}
134	return (tp);
135}
136
137/*
138 * Get tag mode.
139 */
140	public int
141gettagtype()
142{
143	int f;
144
145	if (strcmp(tags, "GTAGS") == 0)
146		return T_GTAGS;
147	if (strcmp(tags, "GRTAGS") == 0)
148		return T_GRTAGS;
149	if (strcmp(tags, "GSYMS") == 0)
150		return T_GSYMS;
151	if (strcmp(tags, "GPATH") == 0)
152		return T_GPATH;
153	if (strcmp(tags, "-") == 0)
154		return T_CTAGS_X;
155	f = open(tags, OPEN_READ);
156	if (f >= 0)
157	{
158		close(f);
159		return T_CTAGS;
160	}
161	return T_GTAGS;
162}
163
164/*
165 * Find tags in tag file.
166 * Find a tag in the "tags" file.
167 * Sets "tag_file" to the name of the file containing the tag,
168 * and "tagpattern" to the search pattern which should be used
169 * to find the tag.
170 */
171	public void
172findtag(tag)
173	register char *tag;
174{
175	int type = gettagtype();
176	enum tag_result result;
177
178	if (type == T_CTAGS)
179		result = findctag(tag);
180	else
181		result = findgtag(tag, type);
182	switch (result)
183	{
184	case TAG_FOUND:
185	case TAG_INTR:
186		break;
187	case TAG_NOFILE:
188		error("No tags file", NULL_PARG);
189		break;
190	case TAG_NOTAG:
191		error("No such tag in tags file", NULL_PARG);
192		break;
193	case TAG_NOTYPE:
194		error("unknown tag type", NULL_PARG);
195		break;
196	}
197}
198
199/*
200 * Search for a tag.
201 */
202	public POSITION
203tagsearch()
204{
205	if (curtag == NULL)
206		return (NULL_POSITION);  /* No gtags loaded! */
207	if (curtag->tag_linenum != 0)
208		return gtagsearch();
209	else
210		return ctagsearch();
211}
212
213/*
214 * Go to the next tag.
215 */
216	public char *
217nexttag(n)
218	int n;
219{
220	char *tagfile = (char *) NULL;
221
222	while (n-- > 0)
223		tagfile = nextgtag();
224	return tagfile;
225}
226
227/*
228 * Go to the previous tag.
229 */
230	public char *
231prevtag(n)
232	int n;
233{
234	char *tagfile = (char *) NULL;
235
236	while (n-- > 0)
237		tagfile = prevgtag();
238	return tagfile;
239}
240
241/*
242 * Return the total number of tags.
243 */
244	public int
245ntags()
246{
247	return total;
248}
249
250/*
251 * Return the sequence number of current tag.
252 */
253	public int
254curr_tag()
255{
256	return curseq;
257}
258
259/*****************************************************************************
260 * ctags
261 */
262
263/*
264 * Find tags in the "tags" file.
265 * Sets curtag to the first tag entry.
266 */
267	static enum tag_result
268findctag(tag)
269	register char *tag;
270{
271	char *p;
272	register FILE *f;
273	register int taglen;
274	LINENUM taglinenum;
275	char *tagfile;
276	char *tagpattern;
277	int tagendline;
278	int search_char;
279	int err;
280	char tline[TAGLINE_SIZE];
281	struct tag *tp;
282
283	p = shell_unquote(tags);
284	f = fopen(p, "r");
285	free(p);
286	if (f == NULL)
287		return TAG_NOFILE;
288
289	cleantags();
290	total = 0;
291	taglen = strlen(tag);
292
293	/*
294	 * Search the tags file for the desired tag.
295	 */
296	while (fgets(tline, sizeof(tline), f) != NULL)
297	{
298		if (tline[0] == '!')
299			/* Skip header of extended format. */
300			continue;
301		if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
302			continue;
303
304		/*
305		 * Found it.
306		 * The line contains the tag, the filename and the
307		 * location in the file, separated by white space.
308		 * The location is either a decimal line number,
309		 * or a search pattern surrounded by a pair of delimiters.
310		 * Parse the line and extract these parts.
311		 */
312		tagpattern = NULL;
313
314		/*
315		 * Skip over the whitespace after the tag name.
316		 */
317		p = skipsp(tline+taglen);
318		if (*p == '\0')
319			/* File name is missing! */
320			continue;
321
322		/*
323		 * Save the file name.
324		 * Skip over the whitespace after the file name.
325		 */
326		tagfile = p;
327		while (!WHITESP(*p) && *p != '\0')
328			p++;
329		*p++ = '\0';
330		p = skipsp(p);
331		if (*p == '\0')
332			/* Pattern is missing! */
333			continue;
334
335		/*
336		 * First see if it is a line number.
337		 */
338		tagendline = 0;
339		taglinenum = getnum(&p, 0, &err);
340		if (err)
341		{
342			/*
343			 * No, it must be a pattern.
344			 * Delete the initial "^" (if present) and
345			 * the final "$" from the pattern.
346			 * Delete any backslash in the pattern.
347			 */
348			taglinenum = 0;
349			search_char = *p++;
350			if (*p == '^')
351				p++;
352			tagpattern = p;
353			while (*p != search_char && *p != '\0')
354			{
355				if (*p == '\\')
356					p++;
357				p++;
358			}
359			tagendline = (p[-1] == '$');
360			if (tagendline)
361				p--;
362			*p = '\0';
363		}
364		tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
365		TAG_INS(tp);
366		total++;
367	}
368	fclose(f);
369	if (total == 0)
370		return TAG_NOTAG;
371	curtag = taglist.tl_first;
372	curseq = 1;
373	return TAG_FOUND;
374}
375
376/*
377 * Edit current tagged file.
378 */
379	public int
380edit_tagfile()
381{
382	if (curtag == NULL)
383		return (1);
384	return (edit(curtag->tag_file));
385}
386
387/*
388 * Search for a tag.
389 * This is a stripped-down version of search().
390 * We don't use search() for several reasons:
391 *   -	We don't want to blow away any search string we may have saved.
392 *   -	The various regular-expression functions (from different systems:
393 *	regcmp vs. re_comp) behave differently in the presence of
394 *	parentheses (which are almost always found in a tag).
395 */
396	static POSITION
397ctagsearch()
398{
399	POSITION pos, linepos;
400	LINENUM linenum;
401	int len;
402	char *line;
403
404	pos = ch_zero();
405	linenum = find_linenum(pos);
406
407	for (;;)
408	{
409		/*
410		 * Get lines until we find a matching one or
411		 * until we hit end-of-file.
412		 */
413		if (ABORT_SIGS())
414			return (NULL_POSITION);
415
416		/*
417		 * Read the next line, and save the
418		 * starting position of that line in linepos.
419		 */
420		linepos = pos;
421		pos = forw_raw_line(pos, &line);
422		if (linenum != 0)
423			linenum++;
424
425		if (pos == NULL_POSITION)
426		{
427			/*
428			 * We hit EOF without a match.
429			 */
430			error("Tag not found", NULL_PARG);
431			return (NULL_POSITION);
432		}
433
434		/*
435		 * If we're using line numbers, we might as well
436		 * remember the information we have now (the position
437		 * and line number of the current line).
438		 */
439		if (linenums)
440			add_lnum(linenum, pos);
441
442		/*
443		 * Test the line to see if we have a match.
444		 * Use strncmp because the pattern may be
445		 * truncated (in the tags file) if it is too long.
446		 * If tagendline is set, make sure we match all
447		 * the way to end of line (no extra chars after the match).
448		 */
449		len = strlen(curtag->tag_pattern);
450		if (strncmp(curtag->tag_pattern, line, len) == 0 &&
451		    (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
452		{
453			curtag->tag_linenum = find_linenum(linepos);
454			break;
455		}
456	}
457
458	return (linepos);
459}
460
461/*******************************************************************************
462 * gtags
463 */
464
465/*
466 * Find tags in the GLOBAL's tag file.
467 * The findgtag() will try and load information about the requested tag.
468 * It does this by calling "global -x tag" and storing the parsed output
469 * for future use by gtagsearch().
470 * Sets curtag to the first tag entry.
471 */
472	static enum tag_result
473findgtag(tag, type)
474	char *tag;		/* tag to load */
475	int type;		/* tags type */
476{
477	char buf[256];
478	FILE *fp;
479	struct tag *tp;
480
481	if (type != T_CTAGS_X && tag == NULL)
482		return TAG_NOFILE;
483
484	cleantags();
485	total = 0;
486
487	/*
488	 * If type == T_CTAGS_X then read ctags's -x format from stdin
489	 * else execute global(1) and read from it.
490	 */
491	if (type == T_CTAGS_X)
492	{
493		fp = stdin;
494		/* Set tag default because we cannot read stdin again. */
495		tags = "tags";
496	} else
497	{
498#if !HAVE_POPEN
499		return TAG_NOFILE;
500#else
501		char command[512];
502		char *flag;
503		char *qtag;
504		char *cmd = lgetenv("LESSGLOBALTAGS");
505
506		if (cmd == NULL || *cmd == '\0')
507			return TAG_NOFILE;
508		/* Get suitable flag value for global(1). */
509		switch (type)
510		{
511		case T_GTAGS:
512			flag = "" ;
513			break;
514		case T_GRTAGS:
515			flag = "r";
516			break;
517		case T_GSYMS:
518			flag = "s";
519			break;
520		case T_GPATH:
521			flag = "P";
522			break;
523		default:
524			return TAG_NOTYPE;
525		}
526
527		/* Get our data from global(1). */
528		qtag = shell_quote(tag);
529		if (qtag == NULL)
530			qtag = tag;
531		sprintf(command, "%s -x%s %s", cmd, flag, qtag);
532		if (qtag != tag)
533			free(qtag);
534		fp = popen(command, "r");
535#endif
536	}
537	if (fp != NULL)
538	{
539		while (fgets(buf, sizeof(buf), fp))
540		{
541			char *name, *file, *line;
542
543			if (sigs)
544			{
545#if HAVE_POPEN
546				if (fp != stdin)
547					pclose(fp);
548#endif
549				return TAG_INTR;
550			}
551			if (buf[strlen(buf) - 1] == '\n')
552				buf[strlen(buf) - 1] = 0;
553			else
554			{
555				int c;
556				do {
557					c = fgetc(fp);
558				} while (c != '\n' && c != EOF);
559			}
560
561 			if (getentry(buf, &name, &file, &line))
562			{
563				/*
564				 * Couldn't parse this line for some reason.
565				 * We'll just pretend it never happened.
566				 */
567				break;
568			}
569
570			/* Make new entry and add to list. */
571			tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
572			TAG_INS(tp);
573			total++;
574		}
575		if (fp != stdin)
576		{
577			if (pclose(fp))
578			{
579				curtag = NULL;
580				total = curseq = 0;
581				return TAG_NOFILE;
582			}
583		}
584	}
585
586	/* Check to see if we found anything. */
587	tp = taglist.tl_first;
588	if (tp == TAG_END)
589		return TAG_NOTAG;
590	curtag = tp;
591	curseq = 1;
592	return TAG_FOUND;
593}
594
595static int circular = 0;	/* 1: circular tag structure */
596
597/*
598 * Return the filename required for the next gtag in the queue that was setup
599 * by findgtag().  The next call to gtagsearch() will try to position at the
600 * appropriate tag.
601 */
602	static char *
603nextgtag()
604{
605	struct tag *tp;
606
607	if (curtag == NULL)
608		/* No tag loaded */
609		return NULL;
610
611	tp = curtag->next;
612	if (tp == TAG_END)
613	{
614		if (!circular)
615			return NULL;
616		/* Wrapped around to the head of the queue */
617		curtag = taglist.tl_first;
618		curseq = 1;
619	} else
620	{
621		curtag = tp;
622		curseq++;
623	}
624	return (curtag->tag_file);
625}
626
627/*
628 * Return the filename required for the previous gtag in the queue that was
629 * setup by findgtat().  The next call to gtagsearch() will try to position
630 * at the appropriate tag.
631 */
632	static char *
633prevgtag()
634{
635	struct tag *tp;
636
637	if (curtag == NULL)
638		/* No tag loaded */
639		return NULL;
640
641	tp = curtag->prev;
642	if (tp == TAG_END)
643	{
644		if (!circular)
645			return NULL;
646		/* Wrapped around to the tail of the queue */
647		curtag = taglist.tl_last;
648		curseq = total;
649	} else
650	{
651		curtag = tp;
652		curseq--;
653	}
654	return (curtag->tag_file);
655}
656
657/*
658 * Position the current file at at what is hopefully the tag that was chosen
659 * using either findtag() or one of nextgtag() and prevgtag().  Returns -1
660 * if it was unable to position at the tag, 0 if succesful.
661 */
662	static POSITION
663gtagsearch()
664{
665	if (curtag == NULL)
666		return (NULL_POSITION);  /* No gtags loaded! */
667	return (find_pos(curtag->tag_linenum));
668}
669
670/*
671 * The getentry() parses both standard and extended ctags -x format.
672 *
673 * [standard format]
674 * <tag>   <lineno>  <file>         <image>
675 * +------------------------------------------------
676 * |main     30      main.c         main(argc, argv)
677 * |func     21      subr.c         func(arg)
678 *
679 * The following commands write this format.
680 *	o Traditinal Ctags with -x option
681 *	o Global with -x option
682 *		See <http://www.gnu.org/software/global/global.html>
683 *
684 * [extended format]
685 * <tag>   <type>  <lineno>   <file>        <image>
686 * +----------------------------------------------------------
687 * |main     function 30      main.c         main(argc, argv)
688 * |func     function 21      subr.c         func(arg)
689 *
690 * The following commands write this format.
691 *	o Exuberant Ctags with -x option
692 *		See <http://ctags.sourceforge.net>
693 *
694 * Returns 0 on success, -1 on error.
695 * The tag, file, and line will each be NUL-terminated pointers
696 * into buf.
697 */
698
699#ifndef isspace
700#define isspace(c)	((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' || (c) == '\f')
701#endif
702#ifndef isdigit
703#define isdigit(c)	((c) >= '0' && (c <= '9'))
704#endif
705
706	static int
707getentry(buf, tag, file, line)
708	char *buf;	/* standard or extended ctags -x format data */
709	char **tag;	/* name of the tag we actually found */
710	char **file;	/* file in which to find this tag */
711	char **line;	/* line number of file where this tag is found */
712{
713	char *p = buf;
714
715	for (*tag = p;  *p && !isspace(*p);  p++)	/* tag name */
716		;
717	if (*p == 0)
718		return (-1);
719	*p++ = 0;
720	for ( ;  *p && isspace(*p);  p++)		/* (skip blanks) */
721		;
722	if (*p == 0)
723		return (-1);
724	/*
725	 * If the second part begin with other than digit,
726	 * it is assumed tag type. Skip it.
727	 */
728	if (!isdigit(*p))
729	{
730		for ( ;  *p && !isspace(*p);  p++)	/* (skip tag type) */
731			;
732		for (;  *p && isspace(*p);  p++)	/* (skip blanks) */
733			;
734	}
735	if (!isdigit(*p))
736		return (-1);
737	*line = p;					/* line number */
738	for (*line = p;  *p && !isspace(*p);  p++)
739		;
740	if (*p == 0)
741		return (-1);
742	*p++ = 0;
743	for ( ; *p && isspace(*p);  p++)		/* (skip blanks) */
744		;
745	if (*p == 0)
746		return (-1);
747	*file = p;					/* file name */
748	for (*file = p;  *p && !isspace(*p);  p++)
749		;
750	if (*p == 0)
751		return (-1);
752	*p = 0;
753
754	/* value check */
755	if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)
756		return (0);
757	return (-1);
758}
759
760#endif
761