2 * Copyright (C) 1984-2015 Mark Nudelman
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.
7 * For more information, see the README file.
13 #define WHITESP(c) ((c)==' ' || (c)=='\t')
17 public char ztags[] = "tags";
18 public char *tags = ztags;
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) */
46 static enum tag_result findctag(char *tag);
47 static enum tag_result findgtag(char *tag, int type);
48 static char *nextgtag(void);
49 static char *prevgtag(void);
50 static POSITION ctagsearch(void);
51 static POSITION gtagsearch(void);
52 static int getentry(char *buf, char **tag, char **file, char **line);
55 * The list of tags generated by the last findgtag() call.
57 * Use either pattern or line number.
58 * findgtag() always uses line number, so pattern is always NULL.
59 * findctag() uses either pattern (in which case line number is 0),
60 * or line number (in which case pattern is NULL).
66 #define TAG_END ((struct tag *) &taglist)
67 static struct taglist taglist = { TAG_END, TAG_END };
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 '$' */
75 static struct tag *curtag;
78 (tp)->next = TAG_END; \
79 (tp)->prev = taglist.tl_last; \
80 taglist.tl_last->next = (tp); \
81 taglist.tl_last = (tp);
84 (tp)->next->prev = (tp)->prev; \
85 (tp)->prev->next = (tp)->next;
88 * Delete tag structures.
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. }}
100 while ((tp = taglist.tl_first) != TAG_END)
110 * Create a new tag entry.
113 maketagent(char *name, char *file, LINENUM linenum, char *pattern, int endline)
117 tp = (struct tag *) ecalloc(sizeof(struct tag), 1);
118 tp->tag_file = (char *) ecalloc(strlen(file) + 1, sizeof(char));
119 strcpy(tp->tag_file, file);
120 tp->tag_linenum = linenum;
121 tp->tag_endline = endline;
123 tp->tag_pattern = NULL;
126 tp->tag_pattern = (char *) ecalloc(strlen(pattern) + 1, sizeof(char));
127 strcpy(tp->tag_pattern, pattern);
140 if (strcmp(tags, "GTAGS") == 0)
142 if (strcmp(tags, "GRTAGS") == 0)
144 if (strcmp(tags, "GSYMS") == 0)
146 if (strcmp(tags, "GPATH") == 0)
148 if (strcmp(tags, "-") == 0)
150 f = open(tags, OPEN_READ);
160 * Find tags in tag file.
161 * Find a tag in the "tags" file.
162 * Sets "tag_file" to the name of the file containing the tag,
163 * and "tagpattern" to the search pattern which should be used
169 int type = gettagtype();
170 enum tag_result result;
173 result = findctag(tag);
175 result = findgtag(tag, type);
182 error("No tags file", NULL_PARG);
185 error("No such tag in tags file", NULL_PARG);
188 error("unknown tag type", NULL_PARG);
200 return (NULL_POSITION); /* No gtags loaded! */
201 if (curtag->tag_linenum != 0)
208 * Go to the next tag.
213 char *tagfile = (char *) NULL;
216 tagfile = nextgtag();
221 * Go to the previous tag.
226 char *tagfile = (char *) NULL;
229 tagfile = prevgtag();
234 * Return the total number of tags.
243 * Return the sequence number of current tag.
251 /*****************************************************************************
256 * Find tags in the "tags" file.
257 * Sets curtag to the first tag entry.
259 static enum tag_result
271 char tline[TAGLINE_SIZE];
274 p = shell_unquote(tags);
282 taglen = (int) strlen(tag);
285 * Search the tags file for the desired tag.
287 while (fgets(tline, sizeof(tline), f) != NULL)
290 /* Skip header of extended format. */
292 if (strncmp(tag, tline, taglen) != 0 || !WHITESP(tline[taglen]))
297 * The line contains the tag, the filename and the
298 * location in the file, separated by white space.
299 * The location is either a decimal line number,
300 * or a search pattern surrounded by a pair of delimiters.
301 * Parse the line and extract these parts.
306 * Skip over the whitespace after the tag name.
308 p = skipsp(tline+taglen);
310 /* File name is missing! */
314 * Save the file name.
315 * Skip over the whitespace after the file name.
318 while (!WHITESP(*p) && *p != '\0')
323 /* Pattern is missing! */
327 * First see if it is a line number.
330 taglinenum = getnum(&p, 0, &err);
334 * No, it must be a pattern.
335 * Delete the initial "^" (if present) and
336 * the final "$" from the pattern.
337 * Delete any backslash in the pattern.
344 while (*p != search_char && *p != '\0')
350 tagendline = (p[-1] == '$');
355 tp = maketagent(tag, tagfile, taglinenum, tagpattern, tagendline);
362 curtag = taglist.tl_first;
368 * Edit current tagged file.
375 return (edit(curtag->tag_file));
380 * This is a stripped-down version of search().
381 * We don't use search() for several reasons:
382 * - We don't want to blow away any search string we may have saved.
383 * - The various regular-expression functions (from different systems:
384 * regcmp vs. re_comp) behave differently in the presence of
385 * parentheses (which are almost always found in a tag).
390 POSITION pos, linepos;
396 linenum = find_linenum(pos);
401 * Get lines until we find a matching one or
402 * until we hit end-of-file.
405 return (NULL_POSITION);
408 * Read the next line, and save the
409 * starting position of that line in linepos.
412 pos = forw_raw_line(pos, &line, (int *)NULL);
416 if (pos == NULL_POSITION)
419 * We hit EOF without a match.
421 error("Tag not found", NULL_PARG);
422 return (NULL_POSITION);
426 * If we're using line numbers, we might as well
427 * remember the information we have now (the position
428 * and line number of the current line).
431 add_lnum(linenum, pos);
434 * Test the line to see if we have a match.
435 * Use strncmp because the pattern may be
436 * truncated (in the tags file) if it is too long.
437 * If tagendline is set, make sure we match all
438 * the way to end of line (no extra chars after the match).
440 len = (int) strlen(curtag->tag_pattern);
441 if (strncmp(curtag->tag_pattern, line, len) == 0 &&
442 (!curtag->tag_endline || line[len] == '\0' || line[len] == '\r'))
444 curtag->tag_linenum = find_linenum(linepos);
452 /*******************************************************************************
457 * Find tags in the GLOBAL's tag file.
458 * The findgtag() will try and load information about the requested tag.
459 * It does this by calling "global -x tag" and storing the parsed output
460 * for future use by gtagsearch().
461 * Sets curtag to the first tag entry.
463 static enum tag_result
464 findgtag(char *tag, int type)
470 if (type != T_CTAGS_X && tag == NULL)
477 * If type == T_CTAGS_X then read ctags's -x format from stdin
478 * else execute global(1) and read from it.
480 if (type == T_CTAGS_X)
483 /* Set tag default because we cannot read stdin again. */
493 char *cmd = lgetenv("LESSGLOBALTAGS");
495 if (cmd == NULL || *cmd == '\0')
497 /* Get suitable flag value for global(1). */
516 /* Get our data from global(1). */
517 qtag = shell_quote(tag);
520 command = (char *) ecalloc(strlen(cmd) + strlen(flag) +
521 strlen(qtag) + 5, sizeof(char));
522 sprintf(command, "%s -x%s %s", cmd, flag, qtag);
525 fp = popen(command, "r");
531 while (fgets(buf, sizeof(buf), fp))
533 char *name, *file, *line;
544 len = (int) strlen(buf);
545 if (len > 0 && buf[len-1] == '\n')
552 } while (c != '\n' && c != EOF);
555 if (getentry(buf, &name, &file, &line))
558 * Couldn't parse this line for some reason.
559 * We'll just pretend it never happened.
564 /* Make new entry and add to list. */
565 tp = maketagent(name, file, (LINENUM) atoi(line), NULL, 0);
580 /* Check to see if we found anything. */
581 tp = taglist.tl_first;
589 static int circular = 0; /* 1: circular tag structure */
592 * Return the filename required for the next gtag in the queue that was setup
593 * by findgtag(). The next call to gtagsearch() will try to position at the
610 /* Wrapped around to the head of the queue */
611 curtag = taglist.tl_first;
618 return (curtag->tag_file);
622 * Return the filename required for the previous gtag in the queue that was
623 * setup by findgtat(). The next call to gtagsearch() will try to position
624 * at the appropriate tag.
640 /* Wrapped around to the tail of the queue */
641 curtag = taglist.tl_last;
648 return (curtag->tag_file);
652 * Position the current file at at what is hopefully the tag that was chosen
653 * using either findtag() or one of nextgtag() and prevgtag(). Returns -1
654 * if it was unable to position at the tag, 0 if successful.
660 return (NULL_POSITION); /* No gtags loaded! */
661 return (find_pos(curtag->tag_linenum));
665 * The getentry() parses both standard and extended ctags -x format.
668 * <tag> <lineno> <file> <image>
669 * +------------------------------------------------
670 * |main 30 main.c main(argc, argv)
671 * |func 21 subr.c func(arg)
673 * The following commands write this format.
674 * o Traditinal Ctags with -x option
675 * o Global with -x option
676 * See <http://www.gnu.org/software/global/global.html>
679 * <tag> <type> <lineno> <file> <image>
680 * +----------------------------------------------------------
681 * |main function 30 main.c main(argc, argv)
682 * |func function 21 subr.c func(arg)
684 * The following commands write this format.
685 * o Exuberant Ctags with -x option
686 * See <http://ctags.sourceforge.net>
688 * Returns 0 on success, -1 on error.
689 * The tag, file, and line will each be NUL-terminated pointers
693 getentry(char *buf, char **tag, char **file, char **line)
697 for (*tag = p; *p && !IS_SPACE(*p); p++) /* tag name */
702 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
707 * If the second part begin with other than digit,
708 * it is assumed tag type. Skip it.
712 for ( ; *p && !IS_SPACE(*p); p++) /* (skip tag type) */
714 for (; *p && IS_SPACE(*p); p++) /* (skip blanks) */
719 *line = p; /* line number */
720 for (*line = p; *p && !IS_SPACE(*p); p++)
725 for ( ; *p && IS_SPACE(*p); p++) /* (skip blanks) */
729 *file = p; /* file name */
730 for (*file = p; *p && !IS_SPACE(*p); p++)
737 if (strlen(*tag) && strlen(*line) && strlen(*file) && atoi(*line) > 0)